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 1 of 5.
                
                        Vishwa said: 
                         
                        1 decade ago
                
                Can anybody explain this...?
                
                        Rachit said: 
                         
                        1 decade ago
                
                Will the basic rule chan of post/pre increment/decrement in GCC ?
                
                        Pratik said: 
                         
                        1 decade ago
                
                Please elabortate it.
                
                        Pruthvi said: 
                         
                        1 decade ago
                
                Is associativity considered in the printf function also or only while evaluating an expression we consider the associativity ?
Can any one explain it. ?
                Can any one explain it. ?
                        Kamal said: 
                         
                        1 decade ago
                
                printf("%d, %d", ++i, ++i)
Here arguments in fxn prnitf() are evaluated from right to left. It is a rule in C. And at time of printing they are displayed in the Left to Right basis.
                Here arguments in fxn prnitf() are evaluated from right to left. It is a rule in C. And at time of printing they are displayed in the Left to Right basis.
                        Pooja said: 
                         
                        1 decade ago
                
                Please explain it clearly with examples.
                
                        Neethu said: 
                         
                        1 decade ago
                
                printf("%d, %d", ++i, ++i)
Here arguments in fxn prnitf() are evaluated from right to left. It is a rule in C. And at time of printing they are displayed in the Left to Right basis.
                Here arguments in fxn prnitf() are evaluated from right to left. It is a rule in C. And at time of printing they are displayed in the Left to Right basis.
                        Ravikiran said: 
                         
                        1 decade ago
                
                It is right.. for example:
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}
will u see the out put in this .. the out put will be
0 0 30
because it can read right to left in print statement, when it we write the options continuously(a==40,a<=50,a=30).
                #include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}
will u see the out put in this .. the out put will be
0 0 30
because it can read right to left in print statement, when it we write the options continuously(a==40,a<=50,a=30).
                        Yami said: 
                         
                        1 decade ago
                
                #include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}
Please explain it clearly ....
o/p : 0 0 30
                int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}
Please explain it clearly ....
o/p : 0 0 30
                        P.Vijay kumar said: 
                         
                        1 decade ago
                
                Hi Friends...
The evalution order in c is:Right to left but in c++ is Left to Right.
During the display, the values are dispaly from left to right.(both c and c++)
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}
O/P : 0 1 30
For eg::
main()
{
int i=5;
printf("%d %d",++i,++i);
}
O/P:: in c::7,6 but in c++ 6,7
But it varies depends on the compiler..
                The evalution order in c is:Right to left but in c++ is Left to Right.
During the display, the values are dispaly from left to right.(both c and c++)
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}
O/P : 0 1 30
For eg::
main()
{
int i=5;
printf("%d %d",++i,++i);
}
O/P:: in c::7,6 but in c++ 6,7
But it varies depends on the compiler..
Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers