Professional Documents
Culture Documents
Computer Science
Theory - 2
on
CA3CO05
EACH ONE.
ANS.
Tokens are the smallest elements of a program, which are meaningful to the
compiler.
The following are the types of tokens: Keywords, Identifiers, Constant, Strings,
Operators, etc.
Let us begin with Keywords.
Keywords
Keywords are predefined, reserved words in C and each of which is associated with specific features. These words help us to
use the functionality of C language. They have special meaning to the compilers.
do if static while
Identifiers
Each program element in C programming is known as an identifier. They are used for naming of variables, functions, array
etc. These are user-defined names which consist of alphabets, number, underscore ‘_’. Identifier’s name should not be same or
same as keywords. Keywords are not used as identifiers.
• Only alphabets, numbers, underscore can be used, no other special characters, punctuations are allowed.
Strings
A string is an array of characters ended with a null character(\0). This null character indicates that string has ended. Strings
are always enclosed with double quotes(“ “).
Type Description
char character
You may like to store information of various data types like character, wide character, integer, floating point, double floating
point, boolean etc. ... Based on the data type of a variable, the operating system allocates memory and decides what can be
int Integer 2 or 4
float Floating-point 4
char Character 1
bool Boolean 1
void Empty 0
1. C++ int
The int keyword is used to indicate integers.
Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to
2147483647.
For example,
The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two
times the precision of float. To learn more, visit C++ float and double.
For example,
As mentioned above, these two data types are also used for exponentials. For
example,
3. C++ char
Keyword char is used for characters.
For example,
Note: In C++, an integer value is stored in a char variable rather than the character
itself. To learn more, visit C++ characters.
4. C++ wchar_t
Wide character wchar_t is similar to the char data type, except its size is 2 bytes
instead of 1.
It is used to represent characters that require more memory to represent them than
a single char.
For example,
Note: There are also two other fixed-size character types char16_t and char32_t
introduced in C++11.
5. C++ bool
The bool data type has one of two possible values: true or false.
Booleans are used in conditional statements and loops (which we will learn in later
chapters).
For example,
6. C++ void
The void keyword indicates an absence of data. It means "nothing" or "no value".
TYPES:
A+B;
Here, the + operator is used to add two variables a and b. Similarly there are various other
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
a = 5;
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
a > b;
Logical operators are used to check whether an expression is true or false. If the
| Binary OR
^ Binary XOR
endl Manipulator:
This manipulator has the same functionality as the ‘n’ newline character.
For example:
Syntax:
setw(x)
Here setw causes the number or string that follows it to be printed within a field of x characters wide and x is the argument
set in setw manipulator.
The header file that must be included while using setw manipulator is .
setfill Manipulator:
This is used after setw manipulator.
If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling
the fields.
Example:
#include <iostream>
#include <iomanip>
void main()
Output:
********9997
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.
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.
Example:
#include <iostream>
#include <iomanip>
void main( )
float x = 0.1;
Output:
0.100
1.000e-001
The first cout statement contains fixed notation and the setprecision contains argument 3.
This means that three digits after the decimal point and in fixed notation will output the first cout statement as 0.100. The
second cout produces the output in scientific notation.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------