closed by
2,325 views
0 votes
0 votes
closed as a duplicate of: Undefined Behaviour in C

Q)If a is 15, then what would be the value of:

printf("%d , %d , %d , %d ", ++a , a++, --a , a--);

A)16,16,16,16     B)15,13,15,15

C)16,15,14,14    D)16,16,14,14

 

closed by

1 Answer

3 votes
3 votes

This question is compiler dependent question, you may get different answers from different compilers but if you encounter this question you can use below approach but still it doesn't give exact answer.

The print statement will be executed from right to left -

so first a-- will happen as a-- is postfix so value of a-- =  15.

then --a so already a value is 14 and now --a is prefix so value of --a = 13

then a++ its a postfix so value of a++ =  13

then ++a, already value of a became 14 now ++a is prefix so value of ++a = 15.

But wait this all be printed in reverse direction because only execution is right to left but print will be from left to right, so answer will be  15, 13, 13, 15.

Related questions

0 votes
0 votes
3 answers
3
lalitver10 asked Oct 23, 2021
700 views
#include <stdio.h>int main(){ int a=20; int *ptr=&a; int x=a; printf ("%p\n",&*ptr); printf ("%p\n",&a); return 0;}Why both printf() line printing the s...
0 votes
0 votes
2 answers
4
Lakshman Bhaiya asked Apr 21, 2018
2,563 views
Q) What is the output of the following C Program fragment#include<stdio.h>int main(){int a = 4, b = 3;printf("%d",a+++++b);return 0;} A) 7 B) 8 C) 9 ...