You are on page 1of 34

Getting

Started With
C++
C++ CHARACTER SET
 Character set is a set of valid characters that a language can
recognise. A character represents any letter, digit, or any other sign.

 Letters A-Z, a-z


 Digits 0-9
 Special Symbols Space + - * / ^ \ ( ) [ ] { } = !=< > . ‘ “ $ , ; : % ! & ?
_(underscore) # <= >= @
 White Spaces Blank spaces, Horizontal tab, Carriage return, New line,
Form feed.
 Other Characters C++ can process any of the 256
 ASCII characters as data or as literals.
TOKENS(LEXICAL UNITS)
The smallest individual unit in a program is
known as a Token or lexical unit.
Types of Tokens
Keywords
Identifiers
Literals
Punctuators
Operators
KEYWORDS
KEYWORDS

Keywords are the words that convey a special meaning to the


language compiler.

These are reserved for special purpose and must not be used as
normal identifier names.
Some of the keywords in c++
asm continue float new signed try
auto default for operator sizeof typrdef
break delete friend private static union
case do goto protected struct unsigned
catch double if public switch virtual
char else inline register template void
class enum int return this volatile
const extern long short throw while
Identifiers
Identifiers are names of the program given by user.
Rules to write identifiers
1. Do not start with digits.
2. No special symbols are used except _(underscore).
3. Upper-case And Lower-case are different.
4. No spaces are used.

 Examples:- myfile , date9_2_7_6


Literals
 Literals (constants) are data items that never change their value
during a program run.
Types of Literals:
1. Bool Literal
2. Integer constant
3. Floating constants
4. Character constant
5. String literal
Integer constant
Integer constants are whole numbers without any
fractional part.

Three types of Integer constants


1. Decimal Integer constant
2. Octal Integer constant
3. Hexadecimal Integer constant
Decimal Integer constant
An integer constant consisting of a sequence of digits is
taken to be decimal integer constant unless it begins with 0
(digit zero).

Example:- 1296, 5642, 12, +69,-23,etc.,


Octal Integer constant
A sequence of digits starting with0(digit zero) is
taken to be an octal integer.

Example:-123, 456, etc.,


Character constants
A Character constant is one character
enclosed in single quotes, as in ‘z’.

Examples:- ‘a’, ‘b’, etc.,


Escape sequences
\a Audible sound \b back space
\f Formfeed \n Newline or Linefeed
\r Carriage return \t Horizontal tab
\v Vertical tab \\ Backslash
\’ single quote \” double quote
\? Question mark \on Octal number
\xHn Hexadecimal number \0 Null
String Literals
 Multiple character constants are treated
as string literals.

Examples:-”a” , “ade”, etc.,


Floating Constants
 Floating constants are also called as Real constants

Real constants are numbers having fractional parts. These


may be written in one of the two forms called fractional
form or the exponent form.

Exemples :-2.0, 3.5, 8.6, etc.,


Punctuators
The following characters are used as punctuators [ ] ( ) { } , ; :
* … = #.

 Brackets [ ] opening and closing brackets indicate single


and multidimensional array subscripts.

 Parenthesis ( ) these indicate function calls and function


parameters.
Braces { } these indicates the start and end of a compound
statement.

Comma , it is used as separator in a function argument list.

Semicolon ; it is used as statement terminator.

 Colon : it indicates a labelled statement.

Asterisk * it is used for pointer declaration.


Ellipsis … Ellipsis (...) are used in the formal argument lists
of the function prototype to indicate a variable number of
argument.

Equal to sign = It is used for variable initialisation and an


assignment operator in expressions.

Pound sign # this sign is used for pre-processor directive.


Operators
 Operators are tokens that trigger some computation
when applied to variables and other objects in an
expression.

Types of operators

1. Unary operators
2. Binary operators
3. Ternary operators
Unary operators
Unary operators are those operators that
require one operator to operate upon.

Examples :- +45, 5, -636,etc.,


Some unary operators
& Addresser operator
* Indirection operator
+ Unary plus
- Unary minus
~ Bitwise complement
++ increment operator
-- decrement operator
! Logical negation
Binary operators
Binary operators are those operators that require two
operands to operate upon.

Types of Binary operators

Arithmetic operators
Logical operators
Relational Operators
Conditional Operators
Arithmetic operators

+(addition)
–(subtraction)
*(multiplication)
 /(division)
%(reminder/modulus)
Logical operators
&& (Logical AND)
 || (Logical OR)

Logical operator

?
:
Relational operators
< (Less than)
<=(Less than or equal to)
>(Greater than)
>=(greater than or equal to)
== (equal to)
!= (not equal to)
A First look at C++ Program
Why include iostream.h ?

The header file iostream.h is included in every C++ program


to implement input/output facilities.

Input/output facilities are not defined within C++ language,


but rather are implemented in a component of C++
standard library, iostream.h which is I/O library.
Predefined streams in I/O Library
A stream is simply a sequence of bytes.

The predefined stream objects for input, output, error as


follows:

• Cin cin stands for console input.


• Cout cout stands for console output.
• Cerr cerr stands for console error.
Comments in a C++ Program
Comments are pieces of codes that the compiler discards
or ignores or simply does not execute.

Types of comments:

Single line comments


Multiline or block comments
Single line comment
These comments begins with // are single line comments.
The compiler simply ignores everything following // in that
same line.

Example:-
#include<iostream.h>
Void main() // the program about addition.
Multi line comments
The block comments, mark the beginning of comment with
/* and end with */.
 That means, everything that falls between/*and*/ is
considered as comment.
Example:-
#include<iostream.h>
Void main() /*the program is about addition*/
Using I/O operators
Output operator “ << “

 The output operator (“<<“), also called stream insertion operator is


used to direct a value top standard output.

Input operator “ >> ”

 The input operator(“>>“), also known as stream extraction operator is


used to read a value from standard input.
Variable
A variable refers to a storage area whose contents can vary
during processing.

Cascading of I/O operators

 The multiple use of input or output operators(“>>”or”<<“) in one


statement is called cascading of I/O operators.
Role of compiler
A part of the compiler’s job is to analyze the program code
for ‘correctness’. If the meaning of the program is correct,
then a compiler can not detect errors.

 Types of errors:

1. Syntax Errors
2. Semantic Errors
3. Type Errors
4. Run-time Errors
5. Logical Errors
Syntax Errors are occurred when rules of the program is
misused i.e., when grammatical rule of C++ is violated.
Ex:- int a, b (semicolon missing)

Semantic Errors are occur when statements not meaningful.


Ex:- x*y=z;

Type Errors are occurred when the data types are misused.
Ex:-int a; a=123.56;
Run-time Errors are occurred at the time of execution.

Logical Errors are occurred when the logic of program is not


proper.
Ex:- ctr=1;
While (ctr>10)
{
cout<<n*ctr;
ctr=ctr+1;
}

You might also like