Exforsys.com
 
Home Tutorials C Language
 

C Programming - Operators

 

C Programming - Operators - Page 2

Page 2 of 2



5. Increment and Decrement Operators

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.


6. Conditional or Ternary Operator

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


7. Bitwise Operators

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



8. Special Operators

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

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 size of Operator

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


Read Next: C Programming - Expressions



 

 

Comments


tereenanne said:

  you made the understanding so easy.especially your examples
September 28, 2007, 10:51 am

helios said:

  ur work on c operators was astounding! helped me a lot wid ma project!
thank you!
January 5, 2008, 12:16 pm

Eliwa said:

  This is work well done on operators! It has helped me alot in my studies in computer science. Thanks!
February 8, 2008, 3:52 am

GIANG HO said:

  I would like to have clearly explanation and examples about BITWISE. Please, help me.
October 23, 2008, 2:32 pm

Vijay R said:

  Hi,, i want to know one program on ~ operator in c programming.. can u pls help me
November 12, 2008, 1:15 am

Amarpreet said:

  Hi! your illustration is easier to understand and to make other know about the operators of c. thanks buddy.
November 23, 2008, 3:35 am

jagannath said:

  if u could have given all operators in one table with precedence it would be more helpful.
December 10, 2008, 7:53 pm

Princeioe said:

  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?
January 6, 2009, 9:17 am

1 said:

  What is the meaning of this sign <>
February 3, 2009, 1:28 am

Mahua Nandy said:

  helpful in my studies
February 8, 2009, 8:25 am

mukesh basnet said:

  hi
this is me mukesh . i am new to c programming so help me giving some example.
February 8, 2009, 11:08 pm

Pacifique M said:

  what is the meaning of" -= "
February 20, 2009, 11:25 am

Nikhil said:

  What is meaning of == operator it checks what address or string? If I have("john"=="john") it returns false
where as true in Java.
April 8, 2009, 6:06 pm

Vrishali said:

  What is the meaning of a=b=c;
June 18, 2009, 6:51 am

smile said:

  There are several operators more like bitwise unary ternary binary comma dot conditional etc.
August 21, 2009, 6:08 am

sindhu said:

  @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
October 25, 2009, 11:58 pm

sindhu said:

  @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;
}
October 26, 2009, 12:07 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape