C Programming Questions and Answers – Increment and Decrement Operators
C Programming Questions and Answers – Increment and Decrement Operators
1.What is the difference between the following 2 codes?
#include <stdio.h> //Program 1
int main()
{
int d, a = 1, b = 2;
d = a++ + ++b;
printf(“%d %d %d”, d, a, b);
}
#include <stdio.h> //Program 2
int main()
{
int d, a = 1, b = 2;
d = a++ +++b;
printf(“%d %d %d”, d, a, b);
}
a) No difference as space doesn’t make any difference, values of a, b, d are same in both the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
d) Program 2 has syntax error, program 1 is not
View Answer
Answer:d
2.What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 1, b = 1, c;
c = a++ + b;
printf(“%d, %d”, a, b);
}
a) a = 1, b = 1
b) a = 2, b = 1
c) a = 1, b = 2
d) a = 2, b = 2
View Answer
Answer:b
3.What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 1, b = 1, d = 1;
printf(“%d, %d, %d”, ++a + ++a+a++, a++ + ++b, ++d + d++ + a++);
}
a) 15, 4, 5
b) 9, 6, 9
c) 9, 3, 5
d) Undefined (Compiler Dependent)
View Answer
Answer:d
4.For which of the following, “PI++;” code will fail?
a) #define PI 3.14
b) char *PI = “A”;
c) float PI = 3.14;
d) None of the Mentioned
View Answer
Answer:a
5.What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 10, b = 10;
if (a = 5)
b–;
printf(“%d, %d”, a, b–);
}
a) a = 10, b = 9
b) a = 10, b = 8
c) a = 5, b = 9
d) a = 5, b = 8
View Answer
Answer:c
6.What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 0;
int j = i++ + i;
printf(“%d\n”, j);
}
a) 0
b) 1
c) 2
d) Compile time error
View Answer
Answer:a
7.What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 2;
int j = ++i + i;
printf(“%d\n”, j);
}
a) 6
b) 5
c) 4
d) Compile time error
View Answer
Answer:a
8.Comment on the output of this C code?
#include <stdio.h>
int main()
{
int i = 2;
int i = i++ + i;
printf(“%d\n”, i);
}
a) = operator is not a sequence point
b) ++ operator may return value with or without side effects
c) it can be evaluated as (i++)+i or i+(++i)
d) Both a and b
View Answer
Answer:a
9.What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 0;
int x = i++, y = ++i;
printf(“%d % d\n”, x, y);
return 0;
}
a) 0, 2
b) 0, 1
c) 1, 2
d) Undefined
View Answer
Answer:a
10.What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 10;
int *p = &i;
printf(“%d\n”, *p++);
}
a) 10
b) 11
c) Garbage value
d) Address of i
View Answer
Answer:a
11.What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 97;
int y = sizeof(x++);
printf(“X is %d”, x);
}
a) X is 97
b) X is 98
c) X is 99
d) Run time error
View Answer
Answer:a
12.What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 4, y, z;
y = –x;
z = x–;
printf(“%d%d%d”, x, y, z);
}
a) 3 2 3
b) 2 3 3
c) 3 2 2
d) 2 3 4
View Answer
Answer:b
13.What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 4;
int *p = &x;
int *k = p++;
int r = p – k;
printf(“%d”, r);
}
a) 4
b) 8
c) 1
d) Run time error
View Answer
Answer:c
14.What is the output of this C code?
#include <stdio.h>
void main()
{
int a = 5, b = -7, c = 0, d;
d = ++a && ++b || ++c;
printf(“\n%d%d%d%d”, a, b, c, d);
}
a) 6 -6 0 0
b) 6 -5 0 1
c) -6 -6 0 1
d) 6 -6 0 1
View Answer
Answer:d
15.What is the output of this C code?
#include <stdio.h>
void main()
{
int a = -5;
int k = (a++, ++a);
printf(“%d\n”, k);
}
a) -4
b) -5
c) 4
d) -3
View Answer
Answer:d