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.
                
                        Pavithra said: 
                         
                        8 years ago
                
                Please explain the step 2.
                
                        Mahi said: 
                         
                        8 years ago
                
                How is possible the value of w=1?
                
                        Prichita said: 
                         
                        8 years ago
                
                How did we get x=0? 
Why is it false?
                Why is it false?
                        Sid said: 
                         
                        8 years ago
                
                @Prichita.
X=I && j && k;
So substitute values,
X=4 && -1&&0;
--> here 0 means false ....&& (Anding) any value with 0 become 0 only that is false only.
                X=I && j && k;
So substitute values,
X=4 && -1&&0;
--> here 0 means false ....&& (Anding) any value with 0 become 0 only that is false only.
                        Dipak said: 
                         
                        8 years ago
                
                Binary Number is a combination of 0 and 1.
So in above example, positive(+) numbers are assumed as 1 and negative(-) numbers are assumed as 0.
We all know ANDing operation of two binary digit i.e.
1 1
1 0
-------------
1 0
Note: ANDing means multiplication of binary numbers.
we all know ORing operation of two binary digits i.e.
1 1
1 0
---------------
1 1
Note: ORing means addition of binary numbers.
                So in above example, positive(+) numbers are assumed as 1 and negative(-) numbers are assumed as 0.
We all know ANDing operation of two binary digit i.e.
1 1
1 0
-------------
1 0
Note: ANDing means multiplication of binary numbers.
we all know ORing operation of two binary digits i.e.
1 1
1 0
---------------
1 1
Note: ORing means addition of binary numbers.
                        Prashant said: 
                         
                        8 years ago
                
                Step 4:     y = i || j && k;
In step 4 put expression into brackets like : y = (i || j ) && k which will giev o/p as 0 which mean in real time && has a greater precedence than II so is the answer.
                In step 4 put expression into brackets like : y = (i || j ) && k which will giev o/p as 0 which mean in real time && has a greater precedence than II so is the answer.
                        Vicky kumar said: 
                         
                        1 decade ago
                
                In step 4 the answer should be false but Wikiok said the right answer is true. It can't be happen because logical 'and' or 'or'.
Operator have left to right precedency.
But you are saying right to left how it is possible.
Please give me details.
                Operator have left to right precedency.
But you are saying right to left how it is possible.
Please give me details.
                        Abrar said: 
                         
                        1 decade ago
                
                In step 4 how the value of y got 1?
In step 5 how the value of z got 1?
                In step 5 how the value of z got 1?
                        Nihsa said: 
                         
                        1 decade ago
                
                Please explain detail expansion.
                
                        Narendra said: 
                         
                        1 decade ago
                
                Please give complete answer.
                Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers