Solved Examples · Example 14
Q.Write a C program to illustrate the use of an increment operator.
#include<stdio.h>
void main()
{
int a=6, b=8;
printf("a=%d\h",a++);
printf("b=%d\h",++b);
}
#include<stdio.h>
void main()
{
int a=6, b=8;
printf("a=%d\h",a++);
printf("b=%d\h",++b);
}
Karnataka PUCTextbookLong· 3mImportance★★★★★est
45% · 69/154 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
Post-increment (a++) yields the value before adding one, so it prints 6; pre-increment (++b) yields the value after adding one, so it prints 9.
The increment operator ++ increases an integer by one and can be written before the variable (pre-increment, ++a) or after it (post-increment, a++). The two forms differ in the value they return to the surrounding expression:
- In post-increment
a++, the current value of a is used first and only then is a increased by one. With a = 6,printf("a=%d", a++)prints 6, after which a becomes 7. - In pre-increment
++b, b is increased by one first and the new value is used. With b = 8,printf("b=%d", ++b)prints 9.
A complete, corrected version is:
#include <stdio.h>
int main(void)
{
int a = 6, b = 8;
printf("a=%d\n", a++);
printf("b=%d\n", ++b);
return 0;
}
Output:
a=6
b=9 …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.