Question:What will be output when you will execute following c code?#include<stdio.h>
void main(){
int m=5,n=10,q=20;
if(q/n*m)
printf("William Gates");
else
printf(" Warren Buffet");
printf(" Carlos Slim Helu");
}
Choose all that apply:
A William Gates
B Warren Buffet Carlos Slim Helu
C Run time error
D Compilation error
E None
+ ExplanationConsider the following expression:
q / n * m
In this expression there are two operators. They are:
/: Division operator
*: Multiplication operator
Precedence and associate of each operator is as follow:Precedence | Operator | Associate |
---|
1 | /,* | Left to right |
Precedence of both operators is same. Hence associate will decide which operator will execute first. Since Associate is left to right. So / operator will execute then * operator will execute.
= q / n * m
= 20 / 10 * 5
= 2 * 5
=10
As we know in c zero represents false and any non-zero number represents true. Since 10 is non- zero number so if clause will execute and print: William Gates
Since in else clause there is not any opening and closing curly bracket. So compiler will treat only one statement as a else part. Hence last statement i.e.
printf(" Carlos Slim Helu");
is not part of if-else statement. So at the end compiler will also print: Carlos Slim Helu
So output of above code will be:
William Gates Carlos Slim Helu