Tutorials
C Language
The increment and decrement operators are one of the unary operators which are very useful in C language. They are extensively used in for and while loops. The syntax of the operators is given below
1. ++ variable name
2. variable name++
3. – –variable name
4. variable name– –
The increment operator ++ adds the value 1 to the current value of operand and the decrement operator – – subtracts the value 1 from the current value of operand. ++variable name and variable name++ mean the same thing when they form statements independently, they behave differently when they are used in expression on the right hand side of an assignment statement.
Consider the following
m = 5;
y = ++m; (prefix)
In this case the value of y and m would be 6
Suppose if we rewrite the above statement as
m = 5;
y = m++; (post fix)
Then the value of y will be 5 and that of m will be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left. On the other hand, a postfix operator first assigns the value to the variable on the left and then increments the operand.
The conditional operator consists of 2 symbols the question mark (?) and the colon (:)
The syntax for a ternary operator is as follows
exp1 ? exp2 : exp3
The ternary operator works as follows
exp1 is evaluated first. If the expression is true then exp2 is evaluated & its value becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Note that only one of the expression is evaluated.
For example
a = 10;
b = 15;
x = (a > b) ? a : b
Here x will be assigned to the value of b. The condition follows that the expression is false therefore b is assigned to x.
| . /* Example : to find the maximum value using conditional operator) #include void main() //start of the program { int i,j,larger; //declaration of variables printf (“Input 2 integers : ”); //ask the user to input 2 numbers scanf(“%d %d”,&i, &j); //take the number from standard input and store it larger = i > j ? i : j; //evaluation using ternary operator printf(“The largest of two numbers is %d \n”, larger); // print the largest number } // end of the program . |
Output
Input 2 integers : 34 45
The largest of two numbers is 45
C has a distinction of supporting special operators known as bitwise operators for manipulation data at bit level. A bitwise operator operates on each bit of data. Those operators are used for testing, complementing or shifting bits to the right on left. Bitwise operators may not be applied to a float or double.
|
Operator |
Meaning |
|
& |
Bitwise AND |
|
| |
Bitwise OR |
|
^ |
Bitwise Exclusive |
|
<< |
Shift left |
|
>> |
Shift right |
C supports some special operators of interest such as comma operator, size of operator, pointer operators (& and *) and member selection operators (. and ->). The size of and the comma operators are discussed here. The remaining operators are discussed in forth coming chapters.
The comma operator can be used to link related expressions together. A comma-linked list of expressions are evaluated left to right and value of right most expression is the value of the combined expression.
For example the statement
value = (x = 10, y = 5, x + y);
First assigns 10 to x and 5 to y and finally assigns 15 to value. Since comma has the lowest precedence in operators the parenthesis is necessary. Some examples of comma operator are
In for loops:
for (n=1, m=10, n <=m; n++,m++)
In while loops
While (c=getchar(), c != ‘10’)
Exchanging values
t = x, x = y, y = t;
The operator size of gives the size of the data type or variable in terms of bytes occupied in the memory. The operand may be a variable, a constant or a data type qualifier.
Example
m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);
The size of operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables during the execution of the program.
Example program that employs different kinds of operators. The results of their evaluation are also shown in comparision
| . main() //start of program { int a, b, c, d; //declaration of variables a = 15; b = 10; c = ++a-b; //assign values to variables printf (“a = %d, b = %d, c = %d\n”, a,b,c); //print the values d=b++ + a; printf (“a = %d, b = %d, d = %d\n, a,b,d); printf (“a / b = %d\n, a / b); printf (“a %% b = %d\n, a % b); printf (“a *= b = %d\n, a *= b); printf (“%d\n, (c > d) ? 1 : 0 ); printf (“%d\n, (c < d) ? 1 : 0 ); } . |
Notice the way the increment operator ++ works when used in an expression. In the statement c = ++a – b; new value a = 16 is used thus giving value 6 to C. That is a is incremented by 1 before using in expression.
However in the statement d = b++ + a; The old value b = 10 is used in the expression. Here b is incremented after it is used in the expression.
We can print the character % by placing it immediately after another % character in the control string. This is illustrated by the statement.
printf(“a %% b = %d\n”, a%b);
This program also illustrates that the expression
c > d ? 1 : 0
Assumes the value 0 when c is less than d and 1 when c is greater than d.
First Page: C Programming - Operators
| you made the understanding so easy.especially your examples |
|
ur work on c operators was astounding! helped me a lot wid ma project! thank you! |
| This is work well done on operators! It has helped me alot in my studies in computer science. Thanks! |
| I would like to have clearly explanation and examples about BITWISE. Please, help me. |
| Hi,, i want to know one program on ~ operator in c programming.. can u pls help me |
| Hi! your illustration is easier to understand and to make other know about the operators of c. thanks buddy. |
| if u could have given all operators in one table with precedence it would be more helpful. |

|
Hi, I am new to this site. The way of expalination is excellent.From two days onwards i am visiting this site and i am learning C language.Today i have gone through "C Programming - Operators", Could you please explain Bitwise operators with some examples? |
| What is the meaning of this sign <> |
| helpful in my studies |
|
hi this is me mukesh . i am new to c programming so help me giving some example. |
| what is the meaning of" -= " |
|
What is meaning of == operator it checks what address or string? If I have("john"=="john") it returns false where as true in Java. |
| What is the meaning of a=b=c; |
| There are several operators more like bitwise unary ternary binary comma dot conditional etc. |
|
@Pacifique M lemme give u an example : #include <stdio.h> #include <string.h> int main() { int a=12; a-=5; //a=a-5 // printf("%d n",a); return 0; } OUTPUT: 5 |
|
@Nikhil: == operator checks the operands on the left hand side and right hand side i.e the value only and not the address.but if u need to compare the string,u have to use it in this way given below.strcmp is a built-in function to compare the strings. #include <stdio.h> #include <string.h> int main() { char a[]="John"; char b[]="John"; int x; x=strcmp(a,b); if(x==0) printf("the strings are equal n"); else printf("the strings are not equal n"); return 0; } |