2,126 views
3 3 votes

Why is the output of the below program 36?
 

int main(){
    int a = 1;
    int b = ++a * ++a * ++a;
    printf("%d", b );
​​​​
    return 0;
}

 

5 Answers

7 7 votes

In short, The answer to this question is completely compiler dependent.

Explanation,

Between consecutive "sequence points" an object's value can be modified only once by an expression. 

Source : https://learn.microsoft.com/en-us/cpp/c-language/c-sequence-points?view=msvc-170

So as you can read from above statement that an object’s value can be modified only once between sequence points. But in the question that has been asked by you the value of “a” is being modified thrice which will give undefined results, as according to the standards of c programming this is not correct way to modify any data object. 

But online compilers and offline compilers gives different results according to their design therefore we will get different results on different compilers.

 


 

  What are Sequence Points?

 

int main(){
    int a = 1; //This semicolon is a sequnce point. Means, from here a statement ends and a new statement begins.
    int b = ++a * ++a * ++a; // This semicolon is another sequnce point. Here the statement which was started earlier ends and new statement begins.
    printf("%d", b );
    return 0;
}

 

The statement  int b = ++a * ++a * ++a   is written between two consecutive sequence points. So we cannot modify the value of variable a more than once. If we try to do something like this we will get different results on different compilers. 

 

 


 

What is a data object ?

A data object is a memory location that stores information used by the program.

A variable is a name used in the program to refer to a data object.

So if you write:

 

int a;

 

It tells the compiler to create a data object that can hold an integer, and in the program you can use the name a to access that data object.

Source : https://stackoverflow.com/questions/40441612/difference-between-variable-and-data-object-in-c-language

• edited by
0 0 votes
Can you explain your process ?  I’ve tried in Turbo C it gave me 24 only.
As explained by @HiraThakur Its behaviour is undefined but as per my understanding I thought it to be 64

Unary has higher precedence so it increases three times x=>4, then multiply takes effect hence b=>64
0 0 votes

The correct output of the above program is 36. Check below for the explanation

a = 1

b = ++a * ++a * ++a

Compiler Schema: Compiler can run two operations, so consider first two operations from the b and again operate with the remained variable.

Scenario1: 

b = ++a * ++a

a = 1

[++] [a] = [2] [2]

[++] [a] = [3] [3] 

In such pre increment cases, the first variable a will get updated with the second variable value.

so the value of the first variable a is also 3.

The total result of the first scenario is 3 * 3

Scenario2:

From the first scenario the value stored in the variable is 3.

Now getting into the remained variable [++a]

a = 3

[++][a] = [4][4]

The total value now stored in b is 3 * 3 * 4 = 36.

I think this is how such problems are done usually. 

Correct me. If I have given any wrong statements in the above explanation.

Thank you.

  

0 0 votes
Sir told in c language course (Operator Video) if more than one increment or Decrement operator is use then answer is undefined soo…. the answer is Compiler Dependant….
Position:
Show:

Related questions

1 1 vote
1 1 answer
182
182 views
khanshahima4 asked Sep 24
182 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
2 2 votes
1 1 answer
138
138 views
GO Classes asked Jun 9
138 views
Consider the following Python code:a = 18 / 3 // 2 b = 2 3 1 c = -2 2 print(a + b + c)What is the output of the code above?77.011.0-5.0
0 0 votes
1 1 answer
3.8k
3.8k views
saumya mishra asked Jun 15, 2018
3,801 views
What is the difference between operator grammar and operator precedence grammar?
0 0 votes
1 1 answer
835
835 views
jatin khachane 1 asked Jun 7, 2017
835 views
E → E + E | E * E | ( E ) | idWhat will be operator precedence for above grammar.Is precedence differs due to ambiguity