Getting Started with C++ Class 11


Token (Lexical Units) -


 The smallest individual unit in a program is known as a Token or a lexical unit.

C++ has following tokens-

Keywords

Identifiers

Literals

Punctuators

Operators


Keywords


 A keyword is that which has a special meaning, those are already been explained to the C++ language like cout, cin, for, if, else etc. These are the reserve keywords.


Identifiers


The identifiers are those which are used for giving a name to a variable like a, b etc. These are used for naming variables, objects, classes, functions, arrays etc. When we declare any variable then we specify a Name so that identifiers are used.
C++ is case sensitive as it treats upper and lower case characters differently.
Ex. myfile, MUFILE, _CHK, DATEq_


Literals (constants)


The term literals (constants) refer to the fixed value means a Constant is that whose value is never changed at the end of the program.

Ex. int x=5;

Here the value of x remains 5 at the end of program. C++ allows several kinds of literals-


bool literal

integer constant

character constant

floating constant

string constant


1.Bool literal


The bool literal in C++ is used to represent one of the two Boolean values i.e. true or false. Boolean true means value ‘1’ and false means value ‘0’.


2. Integer constant

Integer constants are whole numbers without any fractional part. C++ allows three types of integer constants-
Decimal(base 10)
Octal(base 8)
Hexadecimal(base 16)


3. Character Constants


A character constant is one character enclosed in single quotes as in ‘z’. Single character constant eg. ‘c’ or ‘A’ has type ‘char’ which is a C++ data type for characters.

C++ allows certain non graphic characters in character constants. Non- graphic characters are those characters that cannot be typed directly from keyboard. Eg. Backspace, tabs etc. 


4.Floating constant


Floating constants are also called real constants. Real constants are numbers having fractional parts. These may be written in one of two forms called fractional form or the exponent form.
Ex.  2.0, 17.5, -13.0, -0.000625


5.String literals

A string literal is a sequence of characters surrounded by double quotes. Each string is by default added with a special character ‘\0’ which makes the end of a line. Ex. “abc” size is 4.


Operators


An operator is a special symbol which is used for performing an operation. So that operator is that which has a special meaning and also have a sign.
Ex. We know that + is used for adding two numbers. There are two types of operators -

1. Unary operator

2. Binary operator


Role of compiler


 A compiler is a program that translates human readable source code into computer executable machine code. A part of compiler’s job is to analyze the program code for “correction”. Compiler can certainly detect error in the form of program. Some common form of program error are-

1.      Syntax error


Syntax error occurs when a grammatical rule of C++ is violated.
Ex.   main()
        {
         int a, b:
         cin>>a>>b;
         cout<<a+b’
         return 0
       }
  In the first and third line after main (), there is termination error. And the   last statement has missing (;).  


2.    Semantics error


Sometimes error occurs when statements are not meaningful. Semantics refer to the set of rules which give the meaning of a sentence.  Ex. The statement p*q=z; will result in a semantic error. 


3.    Type errors

Data in C++ has an associated data type .The value 6, for instance, is an integer. ‘a’ is a character constant where as “hi” is a string.
If a function is given wrong type of data, type error is signaled by the compiler.


4.    Run-time errors(Execution error)

A run-time error is that occurs during the execution of a program. If it caused of some illegal operation taking place for the execution of the program. Ex.  If an expression is typing to divide a number by zero is a run time error.


5.     Logical error

A logical error is that error which causes a program to produce incorrect or undesired output.
Ex. a=7; b=3; c=5;
       avg=(a*b*c)/3;
In the above code that is syntactically correct but will not produce correct output as it has a logical error in it.



Comments


Comments are the pieces of code that the compiler ignores or simply doesn’t execute. The purpose of comments is only to allow the programmer to insert some notes to enhance readability or understand-ability of the program.

There are two ways to insert comment-

1.      Single line comments with //

2.    Multi-line or block comment
 with /*       */ 


 Use of endl 


  The cout does not insert line break at the end of the output. endl is a newline character which is used to terminate the line.Same way new line character is '\n'.


                  
 This post is about Getting Started with C++ .In the next post we will talk  about Data Types, Variables and Constants.                 


 


 





                           
   
  








Post a Comment

0 Comments