Skip to main content

examples of Increment/decrement Operators

/* Increment operators are used to increase the value of the variable by one and decrement
 operators are used to decrease the value of the variable by one in C programs.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator :  ++ i ;    i ++ ;
Decrement operator :  – – i ;   i – – ;
*/
#include <stdio.h>
int main()
{
     int i=1;
     while(i<10)
     {
         printf("%d ",i);
         i++;
     }    
}
/*output
1 2 3 4 5 6 7 8 9

DIFFERENCE BETWEEN PRE/POST INCREMENT & DECREMENT OPERATORS IN C:

             Operator                        Operator/Description
Pre increment operator (++i)       value of i is incremented before assigning it to the variable i
Post increment operator (i++)      value of i is incremented after assigning it to the variable i
Pre decrement operator (–i)        value of i is decremented before assigning it to the variable i
Post decrement operator (i–)       value of i is decremented after assigning it to variable i
*/


Comments

Popular posts from this blog

example of typecasting in c

/*Type Casting in C Typecasting allows us to convert one data type into other. */ //simple example to cast int value into the float. #include <stdio.h>    int   main (){   float   f = ( float ) 9 / 4 ;     printf ( "f : %f \n " ,  f  );     return   0 ;   }