C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 7)
                   
                                       
                                7.
What will be the output of the program?
 
                                    #include<stdio.h>
int main()
{
    int i=-3, j=2, k=0, m;
    m = ++i && ++j || ++k;
    printf("%d, %d, %d, %d\n", i, j, k, m);
    return 0;
}
Answer: Option
                                                    Explanation:
                                                Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.
Step 2: m = ++i && ++j || ++k;
becomes m = (-2 && 3) || ++k;
becomes m = TRUE || ++k;. 
(++k) is not executed because (-2 && 3) alone return TRUE.
 Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.
Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j are increemented by '1'(one).
Hence the output is "-2, 3, 0, 1".
Discussion:
21 comments Page 2 of 3.
                
                        Omi said: 
                         
                        1 decade ago
                
                ++Pre increment is having highest priority than (&& and ||).
Then why k=0?
                Then why k=0?
                        Saurabh said: 
                         
                        1 decade ago
                
                How could you say that if half of the expression is true then there is no need to execute complete expression?
As in above case as left side part is true so its not increment value of k by 1. Can anyone elaborate?
                As in above case as left side part is true so its not increment value of k by 1. Can anyone elaborate?
                        Haritha said: 
                         
                        10 years ago
                
                Hello and operation both true values then true but we are said one is 1 and another is 0. 
How to say value is true please explain?
                How to say value is true please explain?
                        VENKAT said: 
                         
                        10 years ago
                
                Why we are not incrementing the value of m? Please explain?
                
                        Aman said: 
                         
                        10 years ago
                
                How k = 0? Here, k should be got incremented to 1?
                
                        Premdev said: 
                         
                        9 years ago
                
                How that answer came k value is 0? 
It's that ++k means pre increment operator, So k =k+l..0+1=1 k=1.
                It's that ++k means pre increment operator, So k =k+l..0+1=1 k=1.
                        Rose said: 
                         
                        9 years ago
                
                -2&&3 how in and operation the multiplication of false value * true value becomes as false value only know?
Explain to me that how you are assuming -2 as a true value?
                Explain to me that how you are assuming -2 as a true value?
                        Akach said: 
                         
                        8 years ago
                
                Answer can be -2, 3, 1, 1 k value will be incremented to 1 since it is pre-incremented. How the value of k remains 0 here?
                
                     (2)
                
            
                        Ajay jain said: 
                         
                        8 years ago
                
                Suppose in the place of ++k. if there will be k++ then the result of k++ is false right?
Please let me know and give an explanation.
                Please let me know and give an explanation.
                        Jebby said: 
                         
                        7 years ago
                
                main() 
{
int i=1,j=1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d%d%d%d%d,,i,j,k,l,m);
}
For this, I'm getting the output of 22131.
In this case, l should not get incremented..It should be 2.I don't know why we are getting 3.
Please, anyone explain.
                {
int i=1,j=1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d%d%d%d%d,,i,j,k,l,m);
}
For this, I'm getting the output of 22131.
In this case, l should not get incremented..It should be 2.I don't know why we are getting 3.
Please, anyone explain.
Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers