1.1 Character Set
The study of any language begins with its alphabets.Similarly,C++ language has also its alphabets.In computer terminology,the alphabets,which are the fundamental units of a language,are collectively known as character set and only these characters or symbols are recognised by the language.The character set of c++is categorised as follows:
i) Letters: A to Z , a to z (the English alphabets)
ii) Digits :0 to 9(the numerals)
iii)Special characters:+ - * / ^ | () [] {} = ! < > . ' " $ ; : % & _ (underscore) # @
iv)White spaces;Space bar (Blank space),Horizontal Tab,Carriage Return
v)Other characters:Non-Graphic Symbols and ASCII characters.
1.2 Tokens
While learning languages such as English,Malayalam,Hindi,etc the second stage is learning words constituted by the alphabets(or characters).The term 'token'
in C++ language is similar to the term 'word' in our natural languages.It is the smallest individual unit in a program . C++ has five types of tokens as listed below :
1.2.1 Keywords
The word (token) that convey a specific meaning to the language compiler are called keywords.These are also known as reserved words as they are reserved by the language for special purposes and cannot be redefined for any other purposes.
The original C++ (developed by Stroustrup contains the following keywords (their meanings will be explained in due course):
asm class double friend
auto const else goto
break continue enum if
signed case default exturn
protected sizeof struct virtual
float integer public static
do for long register
switch this try union
volatile char template throw
unsigned typedef void while
new return operator short
private inline catch delete
1.2.1 Identifiers
Identifiers are the user defined word that are used to name different words are used to name different words that are used to name different program elements such as memory locations,functions,statements,objects,classes etc. These are the fundamental building blocks of the program.The identifiers of memory locations are called variables;of functions are called function names;of statements are labels and son on.
While constructing Identifiers certain rules are to be strictly followed for their validity in the program.The rules are as follows:
i)An identifier is an arbitrary long sequence of letters and digits.
ii)The first letter must be a letter
iii)The under(_)counts as a letter.
iv)White spaces are not allowed .
v)Keyword cannot be used as identifiers.
vi)Upper and lower case letters are different (and hence C++ is case sensitive)
The following are valid identifiers:
avg minimum india Computer COMPUTER Program num1 avg_ht a2b_2
The following are some of the invalid identifiers.
2num (First character is not letter)
stud-rec(Special symbol-(hyphen)is used)
exit(keyword)
1.2.3 Literals
Literals are the tokens that represent data items that never change their value during the program run.They are often referred to as constants.C++ has four types of literals as listed below:
(i)Integer Literals
The tokens constituted by only the digits are called integer literals and these are the whole numbers without fractional part.An integer constant must have atleast one digit and must not contain any contain either + or - sign as the first character which indicates the positive or negative nature of the number.A number will no sign is treated as positive.No other characters are allowed.C++ allows three types integers;Decimal,Octal and Hexadecimal.
An integer constant consisting of a sequence of digits is taken to be decimal integer .Eg:- 123,+51,-2890.
A sequence of digits starting with 0(zero) is taken to be octal integer.Eg:-010(equivalent of 810),017(equivalent of 1510)
A sequence of digits preceded by 0x or 0X(digit 0 and the letter x or X) is taken to be hexadecimal integer - Eg:-0XB(equivalent to 1110)ox10(equivalent to 1610)
(ii)Character Constants
A valid C++ character enclosed in a pair of single quotes(') is a character constant.Thus 'a','+','2',etc.are valid character constants,but 'cpp','12' are invalid as they contain more than one character.Though these are not numbers visually,they are stored in memory as numbers and these numbers are unique to each character constants.The numbers known as ASCII codes ,range from 0 to 255.The code of 'a' is 97 and that of 'A' is 65 .That is why we often say that character constants are internally treated as integers.
C++ language has certain non-graphic character constants,which caannot be typed directly from key board.For example,there is no way to express the Carriage Return key , Tab key ,Backspace key.These non-graphic symbols can be represented by using escape sequences,which consists of a backslash(\)followed by one or more characters.The following table lists down the escape sequences and corresponding characters.
| Escape Sequence | Non graphic character |
\a
| Audible bell(alert) |
| \b | Back Space |
| \f | Form Feed |
| \n | New line Line feed |
| \r | Carriage Return |
| \t | Horizontal Tab |
| \v | Vertical Tab |
| \\ | Back slash |
| \’ | Single quote |
| \” | Double quote |
| \? | Question mark |
| \On | Octal number(On represents the in octal) |
| \xHn |
Hexadecimal number
(Hn represents the number in hexa)
|
| \0 | Null character |
(iii)String Literals
A sequence of one or more character enclosed within a pair of double quotes is called string constant.For instance "abc","C++"are valid string constants.When these constants are stored in memory,contiguous cells(smallest unit of memory location) will be allocated for the sequence and will be delimited by null character(\0).Hence '\0' is known as string terminator.For instance the string constant "abc" is stored in the memory as
| a | b | c | \ | 0 |
1.2.4 Punctuators
Punctuators are the tokens or symbols that are used to separate the tokens used in the program and hence they are also known as separators.The punctuators in C++ are braces {and},parenthesis (and),brackets [and], comma(,),semicolon(;),asterick(*),colon(:),number sign #(also called hash).The use of punctuators will be explained in due course.
1.2.5 Operators
Operators are the tokens that triggers some kind of operations on the data.The data on which the operations are carried out are called operands.Based upon the required number of operands and the type of operations involved,the operators are classified in many ways.The detailed study on operators will be performed in the next chapter.Generally operators perform the operations and return some result.C++ has the following types of operators:
(i) The stream extraction and stream insertion operators(>> and <<):
These operators perform input and output operations respectively.
(ii)Arithmetic operators:
These operators perform the basic arithmetic operations such as addition,subtraction,multiplication and division.The tokens +,-,*,/ and % are used as arithmetic operators.The / and % operators perform division operation,but the former one returns the quotient and the latter one returns the reminder.
(iii) Relational Operators:
These operators perform comparison operations between quantities.The operators include <,>,<=,>=,== and !=.The result of these operations will be True or False(Yes or No) values known as Boolean values.
(iv) Logical Operators:
They perform logical operations that require boolean values as operands and return boolean values.These operators are used to combine relational operations.The operators include &&(Logical AND)(Logical OR) and ! (logical Nos).
(v) Conditional Operators: It is the ternary operator of C++ and hence it requires three operands to operate up on.The operator is ?:
There also some special operators such as ++,--<+ size etc.which will discussed in the next chapter.
1.3 Structure of C++ program
As you are a beginner in programming and C++ is an OOP language,we introduce the structure of C++ program in simplest form.Since C++ is a collection of one or more functions,the structure of simplest C++ program consist of single function main ().A function here means,the set of instructions referred to by a name to perform a particular task.Since there can be many functions in a C++ program,they will usually be identified by unique names.The main() is such a name of the function.The structure of a simple C++ program is given below:
#include<header file.h>
void main()
{
Statements;
:
}
Feature of the program
A C++ program consist of one or more functions.A function named main() is a must in the program.The execution starts at main() and ends within main().Call to other functions begins from within the main().
- The C++ program start with the preprocessor directives.# include (pronounced as hash include) is
0 comments:
Post a Comment