We can use increment operator to increase or decrease the value of variable. C increment operators support in both prefix and postfix form. Here are syntax of of increment operators:
1 | variable++; |
2 | variable--; |
3 | ++variable; |
4 | --variable; |
Here is the demonstration program:
01 | #include <stdio.h> |
02 | /* a program demonstrates C increment operators */ |
03 | void main(){ |
04 | int x = 10; |
05 | int y = 0; |
06 | printf ( "x = %d\n" ,x); |
07 | |
08 | /* demonstrate ++ prefix operator */ |
09 | y = ++x; |
10 | printf ( "y = ++x;y = %d\n" ,y); |
11 | |
12 | /* demonstrate ++ postfix operator */ |
13 | y = x++; |
14 | printf ( "y = x++;y = %d\n" ,y); |
15 | |
16 | /* demonstrate -- prefix operator */ |
17 | y = --x; |
18 | printf ( "y = --x;y = %d\n" ,y); |
19 | |
20 | /* demonstrate -- postfix operator */ |
21 | y = x--; |
22 | printf ( "y = x--;y = %d\n" ,y); |
23 | |
24 | /* keep console screen until a key stroke */ |
25 | char key; |
26 | scanf (&key); |
27 | } |
And the output is:
x = 10
y = ++x;y = 11
y = x++;y = 11
y = --x;y = 11
y = x--;y = 11
0 comments:
Post a Comment