Essential Knowledge Points for C Language Beginners: Series of 100 Notes – 18. Increment (++) and Decrement (–) Operators

“From today on, study hard and make progress every day”

Repetition is the best method for memory; spend one minute every day to remember the basic knowledge of C language.

Essential Knowledge Points for C Language Beginners: Series of 100 Notes

18. Increment (++) and Decrement (–) Operators: I used to get these wrong in exams, so I’m summarizing and sharing now!

1. Basic Concepts and Syntax

Increment (++) and Decrement (–) are unary operators unique to the C language, used to increase or decrease the value of a variable by 1:

Operator Name Function Description Example
++ Increment Increases the variable value by 1 i++ or ++i
Decrement Decreases the variable value by 1 j– or –j

2. Difference Between Prefix and Postfix

1. Prefix Form (operate first, then use)

int a = 5;
int b = ++a;  // a becomes 6 first, then assigned to b
// Result: a=6, b=6

2. Postfix Form (use first, then operate)

int x = 5;
int y = x++;  // y gets the value of x (5) first, then x becomes 6
// Result: x=6, y=5

3. Typical Application Scenarios

1. Loop Counter

for(int i = 0; i < 10; i++) {
    printf("%d ", i);  // 0 1 2 ... 9
}

2. Array Traversal

int arr[5] = {10,20,30,40,50};
for(int i = 0; i < 5; ) {
    printf("%d ", arr[i++]);  // 10 20 30 40 50
}

3. Pointer Operations

char str[] = "Hello";
char *p = str;
while(*p) {
    putchar(*p++);  // Output character and move pointer forward
}

4. Behavior in Compound Expressions

1. Multiple Operator Combinations

int i = 5;
int j = (i++) + (++i);  // Undefined behavior! Avoid!

2. Function Parameter Passing

int x = 5;
printf("%d %d", x++, ++x);  // Output depends on compiler implementation

5. Best Usage Practices

  1. 1. Simple Usage Principle:
    i++;  // Better than i += 1 or i = i + 1
  2. 2. Avoid Complex Expressions:
    // Not recommended
    arr[i++] = i;
    
    // Recommended
    arr[i] = i;
    i++;
  3. 3. Clarify Operation Order:
    int j = i++;  // Clearly j gets the old value of i
    int k = ++i;  // Clearly k gets the new value of i

6. Some Cool Comparisons

1. String Handling

void strcpy(char *dest, const char *src) {
    while(*dest++ = *src++);  // Classic implementation
}

2. Stack Operations

int stack[100];
int top = 0;

stack[top++] = 10;  // Push onto stack
int value = stack[--top];  // Pop from stack

Some students contacted me, wanting to have a study exchange group. I hesitated to create one before due to concerns about advertisements, but I think having a group would be convenient, so I’ll try creating one this time.

If you need it, hurry up and join; it’s valid for 7 days.

Essential Knowledge Points for C Language Beginners: Series of 100 Notes - 18. Increment (++) and Decrement (--) Operators

———- End ———-

[Special Statement: All articles in this public account are original or authorized by the author. Some content and images are sourced from the internet and AI. Please feel free to consume; the views are for learning reference only~~]

Essential Knowledge Points for C Language Beginners: Series of 100 Notes - 18. Increment (++) and Decrement (--) Operators

Essential Knowledge Points for C Language Beginners: Series of 100 Notes - 18. Increment (++) and Decrement (--) Operators

“If you like C, please like it”Essential Knowledge Points for C Language Beginners: Series of 100 Notes - 18. Increment (++) and Decrement (--) Operators Click the bottom right corner to seeEssential Knowledge Points for C Language Beginners: Series of 100 Notes - 18. Increment (++) and Decrement (--) Operators

Leave a Comment