C Operators

Increment & Decrement Operators

#include 	
main()	
{		
  int count = 0, loop;		
  loop = ++count;  /* same as count = count + 1; loop = count;  */		
  printf("loop = %d, count = %d\n", loop, count);		
  loop = count++;  /* same as loop = count;  count = count + 1;  */		
  printf("loop = %d, count = %d\n", loop, count);	
} 
C allows two very useful operators not generally found in other languages. These are the increment and decrement operators: ++ and --
The Operator ++ adds 1 to the operand (variable), while - - subtracts 1. Both are unary operators and takes the following form: 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.

Ternary operator

#include 'stdio.h'
#include 'conio.h'
main() ession is true. 
Example :
     a > b && x == 10 

The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10.

#include'stdio.h'
void main()
{ 
	int i;
	printf ("value of i");
	scanf ("%d",&i);
	(i>0)?printf("positive"):printf("negative");
		
} 



Make Comments..!!


Oops!! No posts from user.

Download Android App