# Increment/Decrement Operator

## Pre/Post Increment/Decrement Operator

```c
#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;
}
```

```c
#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;
}
```

```c
#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;
}
```

```c
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;
}
```

```c
#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;
}
```

```c
#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

```c
#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;
}
```

```c
#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;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mun.gitbook.io/c/c/ch5/pointer-operation/pointer-and-operator/increment-decrement-operator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
