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.
                
                        Ganesh marmat said: 
                         
                        1 decade ago
                
                Hi friends. 
In gcc compiler the statement use stack code of:
#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.
And only add to it a++ statement.
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d %d %d %d",a++,a++,++a,a++,++a,a++);
return 0;
}
What is output?
Stack contain 36,37,38,39,40,41 when first %d print 40 because a++ post increment if ++a o/p is 41.
2nd %d o/p is 39 if a++ o/p 41.
All post increment represent it stack value and pre increment is higest value.
                In gcc compiler the statement use stack code of:
#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.
And only add to it a++ statement.
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d %d %d %d",a++,a++,++a,a++,++a,a++);
return 0;
}
What is output?
Stack contain 36,37,38,39,40,41 when first %d print 40 because a++ post increment if ++a o/p is 41.
2nd %d o/p is 39 if a++ o/p 41.
All post increment represent it stack value and pre increment is higest value.
                        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.
                        Driteknight said: 
                         
                        1 decade ago
                
                GCC or Turbo C compilers are implementations to compile C language. C language hasn't described everything, somethings are implementation dependent. Like the output Neetu has explained is right for GCC but not for Turbo C.
To be precise a compiler works on a CFG (Please refer to it from Wikipedia and leave it if you don't about Finite Automata and Grammar). Dennis Ritchie has explained it in his book "The C Programming Language".
                To be precise a compiler works on a CFG (Please refer to it from Wikipedia and leave it if you don't about Finite Automata and Grammar). Dennis Ritchie has explained it in his book "The C Programming Language".
                        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..
                        Sathya said: 
                         
                        8 years ago
                
                int j=5,s=5,i=0,m=5,n=5,q=0,h=5;
i=m++ + ++n-j-- - --s;
q=h++ + ++h - h-- - --h;
System.out.println("q:"+q);
System.out.println("i:"+i);
System.out.println("j:"+j);
System.out.println("s:"+s);
System.out.println("j:"+m);
System.out.println("s:"+n);
can you explain this one?
                i=m++ + ++n-j-- - --s;
q=h++ + ++h - h-- - --h;
System.out.println("q:"+q);
System.out.println("i:"+i);
System.out.println("j:"+j);
System.out.println("s:"+s);
System.out.println("j:"+m);
System.out.println("s:"+n);
can you explain this one?
                        Bhushan said: 
                         
                        1 decade ago
                
                @Shefali : Difference between GCC and Turbo C compiler is that they are made by different companies.
But the standard for C language is same.
Then you must be asking why their is so much difference in output in two compilers.
So always consider GCC as prime because it is industrial standard C compiler. And Turbo C is old compiler which is not used anymore now except educational purpose to simplify complexity.
                But the standard for C language is same.
Then you must be asking why their is so much difference in output in two compilers.
So always consider GCC as prime because it is industrial standard C compiler. And Turbo C is old compiler which is not used anymore now except educational purpose to simplify complexity.
                        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).
                        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).
                        Shani dubey said: 
                         
                        1 decade ago
                
                i++ is postfix increment and ++i is prefix increment. As example:
void main()
{
int i=1;
i=i++;
printf("%d",i);
}
After execute it print output = 2.
In prefix increment:
main()
{
i=1;
i=++i;
}
After execute it print the output = 2.
                void main()
{
int i=1;
i=i++;
printf("%d",i);
}
After execute it print output = 2.
In prefix increment:
main()
{
i=1;
i=++i;
}
After execute it print the output = 2.
                        Pavani,RKValley said: 
                         
                        1 decade ago
                
                Post and pre increments evaluated in left to right order.
And displayed left to right order;
Example: i = 2;.
++i, --i.
First we have to calculate --i after that ++i.
Those values are 1, 2.
Displayed values are 2, 1.
                And displayed left to right order;
Example: i = 2;.
++i, --i.
First we have to calculate --i after that ++i.
Those values are 1, 2.
Displayed values are 2, 1.
Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers