#include
int main(void) {
int var1 = 5;
int var2 = 5;
int var3 = 5;
int var4 = 5;
printf("var1: %d\n", var1);
printf("var2: %d\n", var2);
printf("var3: %d\n", var3);
printf("var4: %d\n", var4);
printf("\n");
printf("++var1: %d\n", ++var1);
printf("var2++: %d\n", var2++);
printf("PreIncrement(var3): %d\n", PreIncrement(&var3));
printf("PostIncrement(var4): %d\n", PostIncrement(&var4));
printf("\n");
printf("var1: %d\n", var1);
printf("var2: %d\n", var2);
printf("var3: %d\n", var3);
printf("var4: %d\n", var4);
return 0;
}
int PreIncrement(int *a) {
*a = *a + 1;
return *a;
}
int PostIncrement(int *a) {
int temp = *a;
*a = *a + 1;
return temp;
}
The Output will be:
var1: 5
var2: 5
var3: 5
var4: 5
++var1: 6
var2++: 5
PreIncrement(var3): 6
PostIncrement(var4): 5
var1: 6
var2: 6
var3: 6
var4: 6
The following is a description collected from:
The two forms of the increment operator are called the prefix form (++x) and the
postfix form (x++). Which form should you use? Actually in C your choice doesn't
matter. However, if you use C++ with its overloadable operators, the prefix version
(++x) is more efficient. So, to develop good habits for learning C++, use the prefix
form.
No comments:
Post a Comment