C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 12)
                   
                                       
                                12.
What will be the output of the program?
 
                                    #include<stdio.h>
int main()
{
    int i=2;
    printf("%d, %d\n", ++i, ++i);
    return 0;
}
Answer: Option
                                                    Explanation:
                                                The order of evaluation of arguments passed to a function call is unspecified.
Anyhow, we consider ++i, ++i are Right-to-Left associativity. The output of the program is 4, 3.
In TurboC, the output will be 4, 3.
In GCC, the output will be 4, 4.
Discussion:
43 comments Page 3 of 5.
                
                        Deepanshu said: 
                         
                        1 decade ago
                
                @Neethu: 
The output in turbo C compiler is :
39 38 38 36 36.
                The output in turbo C compiler is :
39 38 38 36 36.
                        Gangadhar said: 
                         
                        1 decade ago
                
                Then why don't you mention whether it is turbo C compiler or GCC compiler?
                
                        Gaurav said: 
                         
                        1 decade ago
                
                The output varies from compiler to compiler.
                
                        Suresh said: 
                         
                        1 decade ago
                
                Hey @Neethu can you please elaborate this prog at printf function.
                
                        Neethu said: 
                         
                        1 decade ago
                
                #include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d %d %d ",a++,a++,++a,a++,++a);
return 0;
}
OUTPUT gcc: 39 38 40 36 40.
                int main()
{
int a=35;
printf("%d %d %d %d %d ",a++,a++,++a,a++,++a);
return 0;
}
OUTPUT gcc: 39 38 40 36 40.
                        Sonam said: 
                         
                        1 decade ago
                
                What is the difference between ++i and i++. Can anybody explain with an example?
                
                        Sharath said: 
                         
                        1 decade ago
                
                Could you please tell me the exact difference as to why 4 4 is printed in c compiler whereas 4 3 in gcc?
.
                .
                        Rohit nagappa said: 
                         
                        1 decade ago
                
                Output will be 3 and 4 as in printf() function expression is evaluated from right to left. and displayed from left to right. As here ++i will be evaluated first then i becomes 3 and than ++j will be evaluated than j becomes 4.
But as output is left to right.
Hence output is 4 3 (gcc compiler).
                But as output is left to right.
Hence output is 4 3 (gcc compiler).
                        RAHUL said: 
                         
                        1 decade ago
                
                OUTPUT ? AND WHY THAT O/P?
#include<stdio.h>
int main()
{
int i=2;
int j=3;
printf("%d, %d\n", ++j, ++i);
return 0;
}
                #include<stdio.h>
int main()
{
int i=2;
int j=3;
printf("%d, %d\n", ++j, ++i);
return 0;
}
                        Radha said: 
                         
                        1 decade ago
                
                The o/p of above explained program is not 0030
Its 0130
Execution in that s 4m right to left
STEP1: FIRST the variable IS assigned as 35 at the time of declaration
STEP2: Then in the printf statement variable a is assigned as a=30(as execution is from right to left)
step 3: Then a<=50 becomes 30<=50 &the value of that relational expression is 1(30<=50 is true)
step 4: Then a==40 becomes 30==40&its value is zero
The execution is from right to left but the values are printed as per the seqence. So ans is 0130.
                Its 0130
Execution in that s 4m right to left
STEP1: FIRST the variable IS assigned as 35 at the time of declaration
STEP2: Then in the printf statement variable a is assigned as a=30(as execution is from right to left)
step 3: Then a<=50 becomes 30<=50 &the value of that relational expression is 1(30<=50 is true)
step 4: Then a==40 becomes 30==40&its value is zero
The execution is from right to left but the values are printed as per the seqence. So ans is 0130.
Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers