Increment/Decrement Operator
Pre/Post Increment/Decrement Operator
#include<stdio.h>
int main(){
int a = 10;
int *p = &a;
(*p)++; //a++;
printf("%d\n",*p);
++(*p); //++a;
printf("%d\n",*p);
(*p)--; //a--;
printf("%d\n",*p);
--(*p); //--a;
printf("%d\n",*p);
return 0;
}
#include <stdio.h>
int main() {
int a = 10;
printf("%d %d %d\n", a, a, a);//10 10 10 //10
printf("%d %d %d\n", a++, a++, a++); //12 11 10 //13
printf("%d %d %d\n", ++a, ++a, ++a); //16 16 16 //16
printf("%d %d %d\n", a++, ++a, a++); //18 19 16 //19
printf("%d %d %d\n", ++a, a++, ++a); //22 20 22 //22
return 0;
}
#include <stdio.h>
int main() {
int a = 10;
int *b = &a;
printf("%d %d %d\n", a, a, a); //10 10 10 //10
printf("%d %d %d\n", a++, a++, a++); //12 11 10 //13
printf("%d %d %d\n", ++a, ++a, ++a); //16 15 14 //16
printf("%d %d %d\n", a++, ++a, a++); //18 18 16 //19
printf("%d %d %d\n", ++a, a++, ++a); //22 20 20 //22
return 0;
}
include <stdio.h>
int main() {
int a = 10;
int *b = &a;
int **c = &b;
printf("%d %d %d\n", a, a, a); //10 10 10 //10
printf("%d %d %d\n", a++, a++, a++); //12 11 10 //13
printf("%d %d %d\n", ++a, ++a, ++a); //16 15 14 //16
printf("%d %d %d\n", a++, ++a, a++); //18 18 16 //19
printf("%d %d %d\n", ++a, a++, ++a); //22 20 20 //22
return 0;
}
#include <stdio.h>
int main() {
int a = 10;
int *b = &a;
int **c = &b;
printf("%d %d %d\n", a, *b, **c);//10 10 10 //10
printf("%d %d %d\n", ++a, ++a, ++a);//13 12 11 //13
printf("%d %d %d\n", ++a, ++(*b), ++(**c));//16 15 14 //16
printf("%d %d %d\n", a++, (*b)++, (**c)++);//18 17 16 //19
printf("%d %d %d\n", a++, ++(*b), (**c)++);//21 21 19 //22
printf("%d %d %d\n", ++a, (*b)++, ++(**c));//25 23 23 //25
return 0;
}
#include <stdio.h>
int main() {
int a = 5, x = 0;
int *p = &a;
a = 5;
x = a++ + a++ + a++ + a++;
// 5 + 6 + 7 + 8
printf("X = %d \tA=%d\n", x, a); //X = 26 A=9
a = 5;
x = ++a + ++a + ++a + ++a;
// 6 + 7 + 8 + 9
printf("X = %d \tA=%d\n", x, a); //X = 30 A=9
a = 5;
x = ++a + a++ * ++a + ++a;
// 6 + 6 * 8 * 9
printf("X = %d \tA=%d\n", x, a); //X = 63 A=9
a = 5;
x = ++a + a++ - a++ * ++a;
// 6 + 6 - 7 * 9
printf("X = %d \tA=%d\n", x, a); //X = -51 A=9
a = 5;
x = ++a + a++ * ++a * ++a;
// 6 + 6 * 8 * 9
printf("X = %d \tA=%d\n", x, a); //X = 438 A=9
return 0;
}
Some Invalid Increment/Decrement Operations
#include<stdio.h>
int main(){
int a = 10;
int *p = &a;
*p++; // Invalid Operation, precedence of "++" is heigher than "*"
//(*p)++; //Recommended Expression
printf("%d\n",*p);
++*p; // Valid Operation, But Not Recommended
//++(*p); //Recommended Expression
printf("%d\n",*p);
*p--; // Invalid Operation, precedence of "--" is heigher than "*"
//(*p)--; //Recommended Expression
printf("%d\n",*p);
--*p; // Valid Operation, But Not Recommended
//--(*p); //Recommended Expression
printf("%d\n",*p);
return 0;
}
#include<stdio.h>
int main(){
int a = 10;
int *p = &a;
p++; // Invalid Operation, Increment the value of "p" (Memory Address)
printf("%d\n",*p);
return 0;
}
Last updated