Apply for Zend Framework Certification Training

c

< 10 Hard Scenario-Based Problems on Operators in C Purpose of int main(),char main(),void main() with examples >




Predict output on Increment and Decrement Operators in C
Below are 10 challenging, scenario-based problems focused specifically on ++ and --. These cover pre-increment, post-increment, pre-decrement, post-decrement, expressions, loops, arrays, pointers, and function calls.
Important: Some expressions that look difficult are actually undefined behavior in C because a variable is modified multiple times without the required sequencing. Those are included as challenge questions.
Problem 1 — Mixed Increment and Decrement
Predict the output:
#include
int main() {
    int a = 10, b;
    b = a++ + ++a;
    printf("a = %d\n", a);
    printf("b = %d\n", b);
    return 0;
}
Answer
?
 
Problem 2 — Nested Increment
Predict the output:
#include
int main() {
    int x = 5;
    int y = ++x;
    int z = x++;
    printf("%d %d %d\n", x, y, z);
    return 0;
}
Solution
?
Problem 3 — Increment Inside a Loop
What is the output?
#include
int main() {
    int i = 1;
    while (i <= 5) {
        printf("%d ", i++);
        ++i;
    }
    return 0;
}
Solution
?
Problem 4 — Increment in Array Index
Predict the output:
#include
int main() {
    int a[] = {10, 20, 30, 40, 50};
    int i = 0;
    printf("%d ", a[i++]);
    printf("%d ", a[++i]);
    printf("%d ", a[i++]);
    return 0;
}
Solution
?
Problem 5 — Increment and Function Arguments
What is the output?
#include
void show(int x, int y) {
    printf("%d %d\n", x, y);
}
int main() {
    int a = 10;
    show(a++, ++a);
    return 0;
}
Answer
?
Problem 6 — Decrement Challenge
Find the output:
#include
int main() {
    int x = 10;
    printf("%d ", x--);
    printf("%d ", --x);
    printf("%d ", x--);
    printf("%d", --x);
    return 0;
}
Solution
?
Problem 7 — Complex Loop
Predict the output:
#include
int main() {
    int i = 5;
    for (; i > 0; ) {
        printf("%d ", i--);
        if (i % 2 == 0)
            i--;
        else
            i++;
    }
    return 0;
}
Solution
?
Problem 8 — Pointer and Increment
Predict the output:
#include
int main() {
    int a[] = {10, 20, 30, 40};
    int *p = a;
    printf("%d ", *p++);
    printf("%d ", (*p)++);
    printf("%d ", *++p);
    printf("\n");
    for (int i = 0; i < 4; i++)
        printf("%d ", a[i]);
    return 0;
}
Solution
?
Problem 9 — Find the Error
Is this program valid C?
#include
int main() {
    int x = 5;
    x = x++ + 1;
    printf("%d", x);
    return 0;
}
Answer
?
Bonus Challenge — Predict Carefully
#include
int main() {
    int a = 2;
    int b = 3;
    int c = a++ * ++b;
    printf("a = %d\n", a);
    printf("b = %d\n", b);
    printf("c = %d\n", c);
    return 0;
}
Solution
?
Key Rule
Remember:
++x   → increment first, use later
x++   → use first, increment later
--x   → decrement first, use later
x--   → use first, decrement later
Exam tip: Whenever ++ or -- appears multiple times in the same expression, don't immediately calculate the answer. First check whether the expression has undefined behavior.

< 10 Hard Scenario-Based Problems on Operators in C Purpose of int main(),char main(),void main() with examples >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top