






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Arithmetic Instructions for C Programming
Typology: Essays (university)
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Author: Saurabh Shukla
Arithmetic instruction is used to manipulate data using operators.
Here is the list of operators.
Operator Description Associativity () [] . ->
Parentheses (grouping) Brackets (array subscript) Member selection via object name Member selection via pointer
left-to-right
( type )
& sizeof
Prefix,Postfix increment/decrement Unary plus/minus Logical negation/bitwise complement Cast (change type ) Dereference Address Determine size in bytes
right-to-left
=
Relational less than/less than or equal to Relational greater than/greater than or equal to
left-to-right
== != (^) Relational is equal to/is not equal to left-to-right & Bitwise AND left-to-right ^ Bitwise exclusive OR left-to-right | Bitwise inclusive OR left-to-right && Logical AND left-to-right || (^) Logical OR left-to-right ?: Ternary conditional right-to-left = += -= *= /= %= &= ^= |= <<= >>=
Assignment Addition/subtraction assignment Multiplication/division assignment Modulus/bitwise AND assignment Bitwise exclusive/inclusive OR assignment Bitwise shift left/right assignment
right-to-left
, Comma (separate expressions) left-to-right
Author: Saurabh Shukla
To understand operators in a better way we put them in 8 groups. This also helps you in learning precedence rule of operators. Each group contains operators of same nature. So it would be better if you learn the group names in the same order as listed below.
l Unary Operators l Arithmetic Operators l Bitwise Operators l Relational Operators l Logical Operators l Conditional Operators l Assignment Operators l Miscellaneous Operators
Unary Operators (+, -, ++, --, sizeof() ) Operators required operands to perform its operation. Unary operators are those who required with only single operand to perform its task.
Unary + and โ These operators are not addition and subtraction, rather they are unary + and โ that shows direction of data. For example -3, +4, -345 etc
Increment Operator ++ Increment operator (++) increase value of variable by one. Let us assume we have an int variable x containing 4. int x=4; //declaration of variable x and assign 4 to it. x++; //Value of variable x is incremented by one and become 5 ++x; // Value of variable x is incremented by one and become 6
Here notice that we have two conventions to use increment operator. You can put operator before operand or after operand. If it is after operand known as post increment, otherwise known as pre increment. Both these convention have same effect over operand x as they increase its value by 1. But these two conventions differ in priority rule. Pre increment has the highest priority among all the operators. Post increment has the lowest priority among all the operators.
sizeof() operator sizeof() operator is used to know size of data type, variable or constant. Example : main() { int x,y; float k; char ch; double d; x=sizeof(float); // sizeof returns 4 and assigned to x
Author: Saurabh Shukla
Arithmetic Operators (+,-,,/,%)*
+,- and * operators are same as they are in mathematics. + is used to add two numbers, - is used to subtract two numbers and * is used to multiply two numbers.
Divide operator ( / )
main() { int x; x=5/2; printf(โ%dโ,x); } Output is: 2
Compiler understands numbers in two categories one is integer and second is real. We have used both the operands integer in above program. When two integers operate, result is always an integer. So 5/2 yield 2 and not 2.
Author: Saurabh Shukla
To get result 2.5 we need to make at least one of the operand real.
main() { int x; x=5/2.0; printf(โ%dโ,x); }
Output is : 2
Here we made one operand real but still result is 2. It happens because variable x is of type int and could not hold real constant. So choose float variable.
main() { float x; x=5/2.0; printf(โ%dโ,x); }
Output is: 2
Oh man! Again the output is 2. Although we made one of the operand real and variable is also of type float, but notice the format specifier used in printf to display content of x, which is wrong. You have to use %f for float variable.
main() { float x; x=5/2.0; printf(โ%fโ,x); }
Output is:
Modulo operator (%)
Modulo operator gives remainder in result. It can not work for real constants.
main() { int x;
Author: Saurabh Shukla
x=5&12; printf(โ%dโ,x); } Output is: 4
Bitwise AND applies on 5 and 12. We need to convert them in binary. 5 =00000000 00000101 12=00000000 00001100 & ------------------------- 4=00000000 00000100
Shift Operators Right shift >> Left shift <<
main() { int x; x=12>>2; printf(โ%dโ,x); } Output is: 3
Convert 12 into binary and shift bits to their right 2 times. This makes last two bits out and two new bits (always 0) append at the left.
12=00000000 00001100 Right shift two times 3 =00000000 00000011
main() { int x; x=12<<2; printf(โ%dโ,x); } Output is: 48
Convert 12 into binary and shift bits to their left 2 times. This makes last two left most bits out and two new bits (always 0) append at the right.
12=00000000 00001100 Left shift two times
Author: Saurabh Shukla
48=00000000 00110000
Relational Operators (<, >, <=, >=, ==. !=)
Relational operators are always evaluated as true or false. True is 1 and false is 0.
main() { int x; x=5<3; printf(โ%dโ,x); } Output is: 0
As the relation 5<3 is false, x will contain 0 as a result of relational operator.
Priority of <, >, <=, >= is higher than == and !=
Associativity is left to right.
Logical Operators ( &&, ||,! )
Logical operators (&& and ||) are used to combined two expressions. They work as:
C1 && C2 Result T && T T T && F F F && X F
C1 || C2 Result F || F F F || T T T || X T
Example
main() { int x; x= 5>3&&4<0; printf(โ%dโ,x); }
Author: Saurabh Shukla
Here, += is also an assignment operator, but used to store data after small manipulation. First 4 is added to the content of x and then assigned to x. The same operation can be performed using = and + operator as x=x+4, but this expression contains two operators which requires precedence rule during evaluation. Although, generated results are same that is x is containing 9.
main() { int x=3, y=3; x=4+3; y=y4+3; }
Now you can understand difference between the two expressions. First x*=4+3, + is operated first as it has higher priority then +=. So 4+3 becomes 7. x is then added to 7 to give result 10. This 10 is getting stored in x.
Second expression y=y*4+3 which is thought as the substitute of first expression but it does not work in similar fashion. * has the highest priority then + and then = comes at the last. Content of y is multiplied by 4 which give 12. 12 is added to 3 which becomes 15 and get stored in y.
Other operators -=, /= and %= are works in a similar fashion.