C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 6)
                   
                                       
                                6.
What will be the output of the program?
 
                                    #include<stdio.h>
int main()
{
    int i=4, j=-1, k=0, w, x, y, z;
    w = i || j || k;
    x = i && j && k;
    y = i || j &&k;
    z = i && j || k;
    printf("%d, %d, %d, %d\n", w, x, y, z);
    return 0;
}
Answer: Option
                                                    Explanation:
                                                Step 1: int i=4, j=-1, k=0, w, x, y, z; here variable i, j, k, w, x, y, z are declared as an integer type and the variable i, j, k are initialized to 4, -1, 0 respectively.
Step 2: w = i || j || k; becomes w = 4 || -1 || 0;. Hence it returns TRUE. So, w=1
Step 3: x = i && j && k; becomes x = 4 && -1 && 0; Hence it returns FALSE. So, x=0
Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1
Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.
Step 6: printf("%d, %d, %d, %d\n", w, x, y, z); Hence the output is "1, 0, 1, 1".
Discussion:
51 comments Page 3 of 6.
                
                        Richa goel said: 
                         
                        1 decade ago
                
                Since the peccedence of && operator is more than the preccedence of || operator 
so, (j&&k) is solved first in step 4 .
hence the ans true.
and similarly in step 5.
                so, (j&&k) is solved first in step 4 .
hence the ans true.
and similarly in step 5.
                        Rajesh said: 
                         
                        1 decade ago
                
                @Muesh modi.
 
In step 4: When compiled as per your logic y=4||-1&&0 then first -1&&0 returns false.
And then 4||false becomes t||f returns true.
                In step 4: When compiled as per your logic y=4||-1&&0 then first -1&&0 returns false.
And then 4||false becomes t||f returns true.
                        Ratan Jeet said: 
                         
                        10 years ago
                
                Can anybody explain what the operator && and || does in the above program as I have never come across such expression and how it results in true or false?
                
                        Raji said: 
                         
                        1 decade ago
                
                Please explain about the precedence of && and || operator and also explain the how the second and third step is executed.
                
                        Rose said: 
                         
                        9 years ago
                
                I want to know clearly that -1 is assumed as true or false value at the same time how 0 is assumed as true or false value?
                
                        Anonymous said: 
                         
                        8 years ago
                
                Anybody please clarify whether we consider associativity or precedence of && and || operators in such case?
                
                        Suneeta mishra said: 
                         
                        3 years ago
                
                And (&&)operator should be true for all conditions and in OR (||), at least one condition should be true.
                
                     (1)
                
            
                        Neha said: 
                         
                        1 decade ago
                
                According to this program they have considered only from left to right. There is no logic of precedence.
                
                        Mayank said: 
                         
                        1 decade ago
                
                Y = T||T && F/*&& HAS HIGHER PRIORITY THEN ||*/.
Y = T||F.
Y = 1;
                Y = T||F.
Y = 1;
                        Pradip walghude said: 
                         
                        1 decade ago
                
                && and || are same priority precedence. It execute right to left.
                Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers