You are on page 1of 8

Source code is treated during preprocessing and compilation as a sequence of tokens.

A token is the smallest independent unit of meaning in a program, as defined by the compiler. There are five different types of tokens:
    

Keywords Identifiers Literals Punctuators Operators

Adjacent identifiers, keywords, and literals must be separated with white space. Other tokens should be separated by white space to make the source code more readable. White space includes blanks, horizontal and vertical tabs, new lines, form feeds, and comments. Keywords are identifiers reserved by the language for special use. Although you can use them for preprocessor macro names, it is considered poor programming style. Only the exact spelling of keywords is reserved. For example, auto is reserved but AUTO is not. The Keywords are in lowercase. Keywords cannot be used as identifiers. C++ keywords continu e defau lt

asm

auto

break

case

char

class

const

delete

do

double

else

enu m

extern

inline

int

float

for

friend

goto

if

long

new

operat or

privat e

protect ed

publi c

regist er

return

shor t

signed

sizeo f

stati c

struct

switc h

templat e

this

Try

typed ef

unio n

unsign ed

virtu al

void

volatile

while

Identifiers provide names for the following language elements like Functions, Objects, Labels etc. An identifier consists of an arbitrary number of letters, digits, or the underscore characters. Characters in identifiers Symbolic names can be used in C++ for various data items used by a programmer in his program. A symbolic name is generally known as an

identifier. The identifier is a sequence of characters taken from C++ character set. The rule for the formation of an identifier are:
  

    

An identifier can consist of alphabets, digits and/or underscores The first character in an identifier must be a letter or the _ (underscore) character; however, beginning identifiers with an underscore is considered poor programming style. The compiler distinguishes between uppercase and lowercase letters in identifiers. For example, PROFIT and profit represent different identifiers. If you specify a lowercase a as part of an identifier name, you cannot substitute an uppercase A in its place; you must use the lowercase letter. It can have maximum 32 characters It can begin with an alphabet or underscore and no other special character is allowed. C++ treats Int ar INT as identifier. C++ treats Age and age as 2 separate identifiers. It should not be a reserved word.

3. Literals Literals (often referred to as constants) are data items that never change their value during the execution of the program. The following types of literals are available in C++.  Integer-Constants  Floating-constants  Character-constants  Strings-constants Integer Constants Integer constants are whole number without any fractional part. C++ allows three types of integer constants. Decimal integer constants : It consists of sequence of digits and should not begin with 0 (zero). For example 124, - 179, +108. Octal integer constants: It consists of sequence of digits starting with 0 (zero). For example. 014, 012. Hexadecimal integer constant: It consists of sequence of digits preceded by ox or OX. Character constants A character constant in C++ must contain one or more characters and must be enclosed in single quotation marks. For example 'A', '9', etc. C++ allows nongraphic characters which cannot be typed directly from keyboard, e.g., backspace, tab, carriage return etc. These characters can be represented by using an escape sequence. An escape sequence represents a single character. The following table gives a listing of common escape sequences. Like \a as beep, \n as newline, \t tab spaces Floating constants They are also called real constants. They are numbers having fractional parts. They may be written in fractional form or exponent form. A real constant in fractional form consists of signed or unsigned digits including a decimal point between digits. For example 3.0, -17.0, -0.627 etc.

String Literals A sequence of character enclosed within double quotes is called a string literal. String literal is by default (automatically) added with a special character ‘\0' which denotes the end of the string. Therefore the size of the string is increased by one character. For example "COMPUTER" will re represented as "COMPUTER\0" in the memory and its size is 9 characters. 4. Punctuators The following characters are used as punctuators in C++ like {} Opening and closing braces indicate the start and end of a compound statement, ; It is used as a statement terminator. 5. Operators Operators are special symbols used for specific purposes. C++ provides six types of operators. Arithmetical operators, Relational operators, Logical operators, Unary operators, Assignment operators, Conditional operators, Comma operator Unary: Unary + , unary -, ++, -Arithmetic : +, -, *, /, % Relational : >,<,>=,<=,==,!= Logical: &&, ||, ! Conditional: ? : Lets Study a program // my first program in C++ #include <iostream.h> #include<conio.h> void main () { clrscr(); cout << "Hello World!"; getch();

This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself

Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream.h> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program. <conio.h> has console input output instructions. Without header file the program will not compile.

}

This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function. The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration. Without main() the program will complete but will not execute as main() is the first function to be executed. main is not a keyword.

Comments Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code. C++ supports two ways to insert comments: 1 // line comment or single line comment 2 /* */ block comment or multiple line

The first of them, known as line comment or single line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, known as block comment or multiple line comment entry, discards everything between the /*characters and the first appearance of the */ characters, with the possibility of including more than one line.

Typecasting
In c++ type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing a variable of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations. One example would be small integers, which can be stored in a compact format and converted to a larger representation when used in arithmetic computations. In most languages, the word coercion is used to denote an implicit conversion, either during compilation or during run time.

Implicit conversion
Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type. For example:

1 short a=2000; 2 int b; 3 b=a;

Here, the value of a has been promoted from short to int and we have not had to specify any typecasting operator. This is known as a standard conversion. Standard conversions affect fundamental data types, and allow conversions such as the conversions between numerical types (short to int, int to float, double to int...), to or from bool, and some pointer conversions. Some of these conversions may imply a loss of precision, which the compiler can signal with a warning. This can be avoided with an explicit conversion. Implicit conversions also include constructor or operator conversions, which affect classes that include

specific constructors or operator functions to perform conversions. For example:

1 2 3 4 5

class A {}; class B { public: B (A a) {} }; A a; B b=a;

Here, a implicit conversion happened between objects of class A and class B, because B has a constructor that takes an object of class A as parameter. Therefore implicit conversions from A to B are allowed.

Explicit conversion
C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion. We have already seen two notations for explicit type conversion: functional and c-like casting:

1 2 3 4

short a=2000; int b; b = (int) a; // c-like cast notation b = int (a); // functional notation float f=(float)7/2;

Precedence of Operators
In the following table of operator precedence, the Turbo C++ operators are divided into various categories.

Operators Assoc Description ---------------------------------------------------------------------(expression) parentheses used for grouping :: RL (unary) global scope resolution operator :: LR class scope resolution operator ---------------------------------------------------------------------() LR parentheses used for a function call () LR value construction, as in type(expression . -> [] const_cast dynamic_cast reinterpret_cast static_cast typeid LR LR LR LR LR LR LR LR member selection via struct or class object member selection via pointer array element access specialized type cast specialized type cast specialized type cast specialized type cast type identification

++ -LR postfix versions of increment/decrement ---------------------------------------------------------------------All the operators in this section are unary (one argument) operators. ++ -RL prefix versions of increment/decrement + RL unary versions ! RL logical NOT ~ RL bitwise complement ("ones complement") & RL address of * RL dereference new RL allocates memory to dynamic object delete RL de-allocates memory allocated to dynamic object new [] RL allocates memory to dynamic array delete [] RL de-allocates memory allocated to dynamic array sizeof RL for computing storage size of data (type) RL cast (C-style type conversion) ---------------------------------------------------------------------.* LR struct/union/object pointer (member dereference) ->* LR pointer to struct/union/object pointer (indirect member dereference) ---------------------------------------------------------------------* / % LR multiplication and division ---------------------------------------------------------------------+ LR addition and subtraction --------------------------------------------------------------------->> << LR input and output stream operators ---------------------------------------------------------------------< <= > >= LR inequality relational ---------------------------------------------------------------------== != LR equality relational & LR bitwise AND ---------------------------------------------------------------------^ LR bitwise XOR ---------------------------------------------------------------------| LR bitwise OR ---------------------------------------------------------------------&& LR logical AND ---------------------------------------------------------------------|| LR logical OR ---------------------------------------------------------------------?: RL conditional ---------------------------------------------------------------------= RL assignment *= RL multiplication and assignment /= RL division and assignment %= RL modulus (remainder) and assignment += RL addition and assignment -= RL subtraction and assignment ---------------------------------------------------------------------throw LR throw exception ---------------------------------------------------------------------, LR the operator, not the separator (combines two expressions into one) ----------------------------------------------------------------------

All of the operators in this table can be overloaded except the following:

. .* :: ?:

C++ direct component selector C++ dereference C++ scope access/resolution Conditional

Types of Errors
Run Time Errors: Errors that occur during the execution of a program are called as run time errors. It is caused of some illegal operation taking place or in availability of desired or required conditions for the execution of the program. For instance, if a program is trying to open a file which does not exist or it could not be opened, it results into an execution error. Similarly, if enough memory is not available or an expression is trying to divide a number by zero are run-time errors. Eg: Division by zero. c=a/b ; User will give the values of a and b at the time of program execution. If the value of b as 0, then division by zero, i.e. a run time error occurs. Syntax Errors: Syntax errors occur when rules of a programming languages (syntax) is misused. i.e. when a grammatical rule of C++ is violated. Eg (i) c=a+b In this statement, since there is no semicolon at the end of the statement, there will occur a syntax error. (ii)cin<<a; In this statement, since stream insertion operator (<<) has given instead of stream extraction operation(>>), there will occurs a syntax error. Semantic Error:- Semantic error is alos called Gammartic error which occurs due to grammatical mistake or for the sentences that does not make sense. Like X+Y = Z Here, the error is Zshould places on the left side ie Z= X+Y; Logical Error: A logical error is that error which causes a program to produce incorrect or undesired output. An incorrectly implemented algorithm or use of a variable before its initialization, or unmarked end for a loop, or wrong parameters passed are causes logical errors. These must be handled carefully. For instance, if we are trying to print the table of a number 5 and if we say counter=1; while(counter>8) { cout<<n*counter; counter=counter+1; } Here the loop would not be executed even once as the condition (counter>8) is not fulfilled at all. Therefore, no output will be produced. Such an error is logical error.

Manipulator A manipulator in c++ construct is used to control formatting of output and/ or input values. Manipulator can only be present in i-o statements like endl. endl is defined in iostream.h
setprecision Manipulator: The setprecision Manipulator is used with floating point numbers. It is used to set the number of digits printed to the right of the decimal point. This may be used in two forms: fixed scientific These two forms are used when the keywords fixed or scientific are appropriately used before the setprecision manipulator. The keyword fixed before the setprecision manipulator prints the floating point number in fixed notation. The keyword scientific, before the setprecision manipulator, prints the floating point number in scientific notation.
 

Sample Code 1. 2. 3. 4. 5. 6. 7. 8. 9. #include <iostream> using namespace std; #include <iomanip> void main( ) { float x = 0.1; cout << fixed << setprecision(3) << x << endl; cout << scientific << x << endl; }