Arithmetic Instruction
Arithmetic instruction is used to manipulate data using operators
To understand operators in a better way we put them in 7 groups.
- Unary Operators
- Arithmetic Operators
- Bitwise Operators
- Relational Operators
- Logical Operators
- Conditional Operators
- Assignment Operators
- It increments the value of the operand by 1 (one).
- Operand must be a variable.
- The value of variable x is initialized to 5.
- In x++, x is an operand and ++ is an operator. Operator is better known as post-increment.
- x++ increases value of variable x by 1, that is value becomes 6
- printf(“%d “,x); prints 6
- In ++x, operator is better known as pre-increment operator
- ++x again increases the value by 1, that is it becomes 7
- printf(“%d “,x); prints 7
Unary Operators
Operator require operands to perform its operation. Unary operators are those, which takes one operand to perform its task.
Unary + and –
These operators should not be misinterpreted as addition and subtraction operators. These are unary + and –, used to make sign positive or negative. For example -3, +4, -345 etc
Increment Operator
Example
int main()
{
int x=5;
x++;
printf("%d ",x);
++x;
printf("%d ",x);
return(0);
}
Output
6 7
Job of pre-increment and post-increment operators are same but there is a difference in priority. Pre-increment has higher priority than post-increment. In fact, post-increment has the least priority among all the operators.
Example
int main()
{
int x=5,y;
y=x++;
printf("%d %d",x,y);
return(0);
}
Output
6 5
There are two operators in expression y=x++
. Assignment operator (=) has higher priority than post increment operator, therefore, value of x is copied to variable y. The value of y becomes 5. Now post-increment operator increases the value of variable x by 1. The variable x becomes 6.
sizeof operator
sizeof() operator is used to evaluate the size of data type, variable or constant.
Example :
int main()
{
int x,y;
float k;
char ch;
double d;
x=sizeof(float); // sizeof returns 4 and assigned to x
x=sizeof(char); // sizeof returns 1 and assigned to x
x=sizeof(int); // sizeof returns 4 and assigned to x
x=sizeof(double); // sizeof returns 8 and assigned to x
x=sizeof(d); // sizeof returns 8 and assigned to x
x=sizeof(k); // sizeof returns 4 and assigned to x
x=sizeof(ch); // sizeof returns 1 and assigned to x
x=sizeof(y); // sizeof returns 4 and assigned to x
x=sizeof(45); // sizeof returns 4 and assigned to x
x=sizeof(23.67); // sizeof returns 8 and assigned to x
x=sizeof(‘a’); // sizeof returns 4 and assigned to x
return(0);
}