The basic data types in C++ programming language are:
1) Character
- char (signed char)
- signed char
- unsigned char
2) Integer
- int (signed int)
- short (short int)
- long(long int)
- signed (signed int)
- unsigned (unsigned int)
3) Floating Point
- float(single precision)
- double (double precision)
- long double (extended precision)
4) Void
5) Boolean
- bool (only in ANSI C++)
Data Type | Memory in bytes |
Signed character | 1 |
Unsigned character | 1 |
Character | 1 |
Short signed integer | 2 |
Short unsigned integer | 2 |
Integer | 2 |
Long Integer | 4 |
Float | 4 |
Double | 8 |
Long double | 10 |
6) User- defined data types in C++ are
- structure
- union
- class
- enum
7) Derived data types in C++ are
- Array
- Function
- Pointer
- Reference
- Valid range of data types is given in following table:
Type and length | Bits | Valid Range |
unsigned char | 8 | 0 to 255 |
char | 8 | -128 to 127 |
enum | 16 | -32,768 to 32,767 |
unsigned int | 16 | 0 to 65,535 |
short int | 16 | -32,768 to 32,767 |
int | 16 | -32,768 to 32,767 |
unsigned long | 32 | 0 to 4,294,967,295 |
long | 32 | -2,147,483,648 to 2,147,483,647 |
float | 32 | 3.4 *(10**-38) to 3.4 * (10**+38) |
double | 64 | 1.7* (10**-308) to 1.7 *(10**+308) |
long double | 80 | 3.4*(10**-4932)to 1.1*(10**+4932) |
Operators in C++ with Example
- Operators are used with operands in expression and assignment statement.
- According to number of operands, operators are
1) Unary Operators
2) Binary Operators
3) Ternary Operators - As per the type of application , operators in C++ are:
1) Arithmetic Operators
+ Addition
– Subtraction
* Multiplication
/ Division
% Modulus (Remainder after integer division)
2) Relational Operators
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
3) Logical Operators
&& AND (Logical conjunction)
|| OR(Logical disjunction)
! NOT(Logical negation)
4) Bitwise Operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
<< Left shift
>> Right shift
5) Assignment Operators
= Simple assignment
*= Assign product
/=Assign quotient
%=Assign remainder (modulus)
+= Assign sum
-= Assign difference
&=Assign bitwise AND
^=Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift
Following examples explain the use of some operators.
1) c=a + B
Value of addition (+) of a and b is assigned (=) to c.
2) int *p,q;
p=&q;
address of (&) p is assigned to (=) pointer variable p.
3) if (a<b && a<c)
return non-zero if a is less than (<)b and (&&) less than c.
4) r = a%b;
Assigns (=) remainder i.e. modulus(%) after division of a and b.
5) if (a = =b)
Compares whether a and b are equals(==).
6) a = b*c;
Product(*) of b and c is assigned (=) to a.
7)*a=*b;
Value at(*) b is assigned (=) to value at (*) a
8) a+ = b;
This is same as a=a+b;
Value of variable a is incremented by b i.e. value of a+b is assigned to variable a.
9) a=20;
b=a<<1;
c=a<<2;
d=a>>1;
Here binary equivalent of a is 10100
Left shift operator shifts all 1 to left by specified number of places. And similarly the right shift operator.
B = a < < 1 = > 101000 = > b = 40
c = a < < 2 = > 1010000 = > c = 80
d = a > > 1 = >1010 = > d = 5
This indicates that left shift double the number and right shift half the number.
10) a > > = 2;
This is same as a=a>>2;
Right shift(>>) a by 2 and assign evaluated value to variable a.
Punctuators in C++
- Punctuators are also know as separators.
- These are the part of syntax.
- Every punctuators has its own meaning.
- Most of these punctuators also function as operators.
- C++ (turbo) provides following punctuators .
Symbol | Name | Purpose |
[ ] | Brackets | array |
( ) | Parentheses | function |
{ } | Braces | block |
, | Comma | separator |
; | Semicolon | statement terminator |
: | Colon | label |
… | Ellipsis | in generic programming |
* | Asterisk | multiplication, value at |
= | Equal Sign | assign to |
# | Pound Sign | pre-processor |
Comments in C++
- Many times in the program, it is necessary to insert some remarks for the programmer.
- These remarks can be inserted as comments.
- Comments are used to describe the particular statements.
- The comment may be of one or more lines.
- Single line comments starts with double slashes i.e //
- Multi-line comment begins with /* and ends with */
- Compiler does not pay attention towards what is written in comments.
- That is, compiler ignores comment from execution.
- Example:
1. // This is single line comment.
2. // Program to find roots of quadratic equation
3. /* this is
multiline
comment */