3. C++ Character Set
• A character set of a language consists of any of the letters, digits
or special symbols that can be used to form the commands in that
language. The C++ character set consists of :
Alphabets
A-Z
a-z
Digits
0-9
Special
Symbols
#, ||+ -* ^
= !=; : &
#
[] {} < > . ‘
‘
4. Tokens in C++ Token
Keywords
Identifier Literal
Operator
The smallest executable unit in a program is known as token.
A token can be any keyword, Identifier ,Literal, Punctuator or Operator.
6. Identifiers in
C++
Examples
of
identifiers
rollno
Change_no student
Employee
An identifier is a name given to any entity of a program. It is used to identify any variable, function,
class , structure etc in c++. It may consist of a combination of letters & numbers.
NOTE: C++ is case sensitive as it treats upper and lower case letters differently so an identifier Rollno
will be diiferent from rollno.
8. Backslash character constants
• These are also known as escape sequences.
• They are used to represent certain sequences such as newline in
C++ .
• When certain letters are preceded by a backslash they will have
special meaning in the language and they are used to represent
sequences such as newline (n) or tab (t) or backspace(b).
• Let us take a look at each of these.
10. Tab (t)
cout<<“Goodt morning”;
Good morning
cout<<“”Good mot rning”;
Good mo rning
The t character when encountered by the compiler converts to a tab.
Any text or data following it appears after 8 spaces.
11. backspace
(b)
cout<<“Hellobworld”;
Hellworld
The b character when encountered by the compiler converts to a backspace.
The cursor moves one space backward in the output when this character is encountered
Here the b makes the cursor move one
space backward to ‘o’
The charcter ‘o’ is overwritten by w
12. backspace
(b)
cout<<“Good morningb”;
cout<<“Welcome friends”;
Good morninWelcome friends
The b character when encountered by the compiler converts to a backspace.
The cursor moves one space backward in the output when this character is encountered
Here the b makes the cursor move one
space backward to ‘g’. The next cout
statement will display the message
overriding the character ‘g’
13. An example program to demonstrate (n),
(b) & (t)
#include <iostream.h>
void main()
{
cout<< “Hello n world”;
cout<<“n welcome t to”;
cout<<“the worldn of t
programmingn”;
cout<<“Learbn to bcode”;
}
Hello
world
welcome to the world
of programming
Lean tocode
14. Operators in
C++
The symbols that are used to form expressions to represent equations or logical sequences
In c++ are known as operators. C++ has a number of operators which shall be discussed in detail
In a separate chapter.
Operators
Arithmetic
Logical Relational
Special
15. Comments in
C++
Comments are the lines that compiler ignores while compiling or executing
the program.
There are two types of comments in C++: Single line and multiline
// this is a constant
Single
line
/* This program
calculates
the sum of two
numbers*/
Multi
line
16. An example program to demonstrate the use
of comments
/* Name :Ankit Jain
Roll No: 2
WAP Program to display message on new line
Date: 15/4/2017 */
#include <iostream.h>
void main()
{
cout<< “Hello n world”;// this line displays text with a newline
}
Multi line
Comment
Single line
Comment
17. Types of
Errors
There are many types of errors that are encountered during the compilation and execution of a program .
Errors
in C++
Syntax
Logical
Run
Time
Linker
18. Syntax Errors
• Every language has a predefined set of its symbols and characters
which are supposed to be used while forming programs. These sets
are also known as the syntax or grammar of the language.
• These are common error and can be easily corrected.
• These are produced when we translate the source code from high
level language to machine language. Thus they are generated
when a program is compiled.
• e.g cuot<<endl; This line will produce a syntax error as there is a
grammatical mistake in the word cout . It is misspelled as cuot.
19. Logical Errors
• A logical error is simply an incorrect translation of either the
problem statement or the algorithm.
• For eg. We want to calculate the average of 4 numbers say a,
b,c,d.
• If we write it as a+b+c+d/4, we will get the wrong answer.
• This is because as division has the higher precedence than sum
first d/4 will take place and then the result will be added to
a+b+c.
• Thus we need to put brackets like (a+b+c+d)/4 to get the correct
output.
20. Linker Errors
• Errors appear during linking process e.g if the word main written
as mian . The program will compile correctly but while linking the
linking window will display errors instead of success.
21. Run time Errors
• An abnormal program termination during execution is known as
Run time Error.
• e.g. If we are writing a statement X = ( A + B) /C ;
•
• the above statement is grammatically correct and also produces
correct result. But if we gave value 0 to the variable c, this
statement will attempt a division by 0 which will result in illegal
program termination. Error will not be found until the program
will be executed because of that it is termed as run time error.
22. Output
Manipulators
Manipulators are the operators used with the insertion operator << to modify or manipulate the way data is displayed
There are two types of manipulators endl and setw.
.
Output
manipulators
endl
setw()
23. endl
manipulator
The endl manipulator outputs new line .
It takes the compiler to the end the line of display.
cout << “ SRDAV School”<<endl;
cout<< “ Teachers Orientation”;
SRDAV School
Teachers Orientation
24. setw()
manipulator
The setw manipulator causes the value of the variable or a number ( or string) that follows it
in the stream to be printed within a field n characters wide where n is the arguments in setw (n). The function
requires a header file, <iomanip.h>
cout<<“a”<<setw(6)<<“b”<<" are the values to be
printed";
a b are the values to be printed
The statement will display the message with 6 spaces between a and b.
25. COLUMN A COLUMN B
1.The smallest executable unit in a program a. Keywords
2. They are used to represent certain sequences in C++ b. Literals
3.Name given to any part of a program c. Operators
4.The data items which never change their value throughout
the program run
d. Tokens
5. Words having a special meaning to the language e. Identifiers
6.The symbols that are used to form expressions f. Backslash character constants
TEST YOURSELF
Match contents of column A with column B