Lec 6 7
Lec 6 7
A data type specifies the type of data that a variable can store such as integer, floating,
character etc.
The memory size of basic data types may change according to 32 or 64 bit operating
system.
Let's see the basic data types. It size is given according to 32 bit OS.
C++ Keywords
A keyword is a reserved word. You cannot use it as a variable name, constant name etc. A
list of 32 Keywords in C++ Language which are also available in C language are given
below.
A list of 30 Keywords in C++ Language which are not available in C language are given
below.
C++ Operators
An operator is simply a symbol that is used to perform operations. There can be many
types of operations like arithmetic, logical, bitwise etc.
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
o Misc Operator
Precedence of Operators in C++
The precedence of operator species that which operator will be evaluated first and next. The
associativity specifies the operators direction to be evaluated, it may be left to right or right to
left.
int data=5+10*10;
The "data" variable will contain 105 because * (multiplicative operator) is evaluated before
+ (additive operator).
C++ Identifiers
C++ identifiers in a program are used to refer to the name of the variables, functions,
arrays, or other user-defined data types created by the programmer. They are the basic
requirement of any language. Every language has its own rules for naming the identifiers.
In short, we can say that the C++ identifiers represent the essential elements in a program
which are given below:
o Constants
o Variables
o Functions
o Labels
o Defined data types
Some naming rules are common in both C and C++. They are as follows:
For example, suppose we have two identifiers, named as 'FirstName', and 'Firstname'.
Both the identifiers will be different as the letter 'N' in the first case in uppercase while
lowercase in second. Therefore, it proves that identifiers are case-sensitive.
Output
C++ Expression
C++ expression consists of operators, constants, and variables which are arranged
according to the rules of the language. It can also contain function calls which return
values. An expression can consist of one or more operands, zero or more operators to
compute a value. Every expression produces some value which is assigned to the variable
with the help of an assignment operator.
Examples of C++ expression:
1. (a+b) - c
2. (x/y) -z
3. 4a2 - 5b +c
4. (a+b) * (x+y)
An expression can be of following types:
o Constant expressions
o Integral expressions
o Float expressions
o Pointer expressions
o Relational expressions
o Logical expressions
o Bitwise expressions
o Special assignment expressions
o If the expression is a combination of the above expressions, such expressions are
known as compound expressions.
o Constant expressions
o A constant expression is an expression that consists of only constant values. It is an
expression whose value is determined at the compile-time but evaluated at the
run-time. It can be composed of integer, character, floating-point, and
enumeration constants.
In the above scenarios, the constant expression can have integer, character, and
enumeration constants. We can use the static and extern keyword with the constants to
define the function-scope.
The following table shows the expression containing constant value:
In the above code, we have first declared the 'x' variable of integer type. After declaration,
we assign the simple constant expression to the 'x' variable.
Output
Value of x is : 3
Integral Expressions
An integer expression is an expression that produces the integer value as output after performing
all the explicit and implicit conversions.
1. (x * y) -5
2. x + int(9.0)
3. where x and y are the integers.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int x; // variable declaration.
6. int y; // variable declaration
7. int z; // variable declaration
8. cout<<"Enter the values of x and y";
9. cin>>x>>y;
10. z=x+y;
11. cout<<"\n"<<"Value of z is :"<<z; // displaying the value of z.
12. return 0;
13. }
In the above code, we have declared three variables, i.e., x, y, and z. After declaration, we take
the user input for the values of 'x' and 'y'. Then, we add the values of 'x' and 'y' and stores their
result in 'z' variable.
Output
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5.
6. int x; // variable declaration
7. int y=9; // variable initialization
8. x=y+int(10.0); // integral expression
9. cout<<"Value of x : "<<x; // displaying the value of x.
10. return 0;
11. }
In the above code, we declare two variables, i.e., x and y. We store the value of expression
(y+int(10.0)) in a 'x' variable.
Output
Value of x : 19
Float Expressions
A float expression is an expression that produces floating-point value as output after performing
all the explicit and implicit conversions.
1. x+y
2. (x/10) + y
3. 34.5
4. x+float(10)
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5.
6. float x=8.9; // variable initialization
7. float y=5.6; // variable initialization
8. float z; // variable declaration
9. z=x+y;
10. std::cout <<"value of z is :" << z<<std::endl; // displaying the value of z.
11.
12.
13. return 0;
14. }
Output
value of z is :14.5
Let's see another example of float expression.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. float x=6.7; // variable initialization
6. float y; // variable declaration
7. y=x+float(10); // float expression
8. std::cout <<"value of y is :" << y<<std::endl; // displaying the value of y
9. return 0;
10. }
In the above code, we have declared two variables, i.e., x and y. After declaration, we store
the value of expression (x+float(10)) in variable 'y'.
Output
value of y is :16.7
Pointer Expressions
A pointer expression is an expression that produces address value as an output.
1. &x
2. ptr
3. ptr++
4. ptr-
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5.
6. int a[]={1,2,3,4,5}; // array initialization
7. int *ptr; // pointer declaration
8. ptr=a; // assigning base address of array to the pointer ptr
9. ptr=ptr+1; // incrementing the value of pointer
10. std::cout <<"value of second element of an array : " << *ptr<<std::endl;
11. return 0;
12. }
In the above code, we declare the array and a pointer ptr. We assign the base address to
the variable 'ptr'. After assigning the address, we increment the value of pointer 'ptr'.
When pointer is incremented then 'ptr' will be pointing to the second element of the array.
Output
Relational Expressions
A relational expression is an expression that produces a value of type bool, which can be
either true or false. It is also known as a boolean expression. When arithmetic expressions
are used on both sides of the relational operator, arithmetic expressions are evaluated
first, and then their results are compared.
1. a>b
2. a-b >= x-y
3. a+b>80
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=45; // variable declaration
6. int b=78; // variable declaration
7. bool y= a>b; // relational expression
8. cout<<"Value of y is :"<<y; // displaying the value of y.
9. return 0;
10. }
In the above code, we have declared two variables, i.e., 'a' and 'b'. After declaration, we
have applied the relational operator between the variables to check whether 'a' is greater
than 'b' or not.
Output
Value of y is :0
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=4; // variable declaration
6. int b=5; // variable declaration
7. int x=3; // variable declaration
8. int y=6; // variable declaration
9. cout<<((a+b)>=(x+y)); // relational expression
10. return 0;
11. }
In the above code, we have declared four variables, i.e., 'a', 'b', 'x' and 'y'. Then, we apply
the relational operator (>=) between these variables.
Output
Logical Expressions
A logical expression is an expression that combines two or more relational expressions
and produces a bool type value. The logical operators are '&&' and '||' that combines two
or more relational expressions.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=2;
6. int b=7;
7. int c=4;
8. cout<<((a>b)||(a>c));
9. return 0;
10. }
Output
0
Bitwise Expressions
A bitwise expression is an expression which is used to manipulate the data at a bit level.
They are basically used to shift the bits.
For example:
x=3
x>>3 // This statement means that we are shifting the three-bit position to the right.
In the above example, the value of 'x' is 3 and its binary value is 0011. We are shifting the
value of 'x' by three-bit position to the right. Let's understand through the diagrammatic
representation.
Let's see a simple example.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int x=5; // variable declaration
6. std::cout << (x>>1) << std::endl;
7. return 0;
8. }
In the above code, we have declared a variable 'x'. After declaration, we applied the bitwise
operator, i.e., right shift operator to shift one-bit position to right.
Output
2
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int x=7; // variable declaration
6. std::cout << (x<<3) << std::endl;
7. return 0;
8. }
In the above code, we have declared a variable 'x'. After declaration, we applied the left
shift operator to variable 'x' to shift the three-bit position to the left.
Output
56
Special Assignment Expressions
Special assignment expressions are the expressions which can be further classified
depending upon the value assigned to the variable.
o Chained Assignment
For example:
1. a=b=20
2. or
3. (a=b) = 20
1. #include <iostream>
2. using namespace std;
3. int main()
4.
5. int a; // variable declaration
6. int b; // variable declaration
7. a=b=80; // chained assignment
8. std::cout <<"Values of 'a' and 'b' are : " <<a<<","<<b<< std::endl;
9. return 0;
10. }
In the above code, we have declared two variables, i.e., 'a' and 'b'. Then, we have assigned
the same value to both the variables using chained assignment expression.
Output
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a; // variable declaration
6. int b; // variable declaration
7. a=10+(b=90); // embedded assignment expression
8. std::cout <<"Values of 'a' is " <<a<< std::endl;
9. return 0;
10. }
In the above code, we have declared two variables, i.e., 'a' and 'b'. Then, we applied
embedded assignment expression (a=10+(b=90)).
Output
o Compound Assignment
For example,
1. a+=10;
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=10; // variable declaration
6. a+=10; // compound assignment
7. std::cout << "Value of a is :" <<a<< std::endl; // displaying the value of a.
8. return 0;
9. }
In the above code, we have declared a variable 'a' and assigns 10 value to this variable.
Then, we applied compound assignment operator (+=) to 'a' variable, i.e., a+=10 which is
equal to (a=a+10). This statement increments the value of 'a' by 10.
Output
Value of a is :20