C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 12)
12.
What will be the output of the program?
#include<stdio.h>
int main()
{
int a = 300, b, c;
if(a >= 400)
b = 300;
c = 200;
printf("%d, %d, %d\n", a, b, c);
return 0;
}
Answer: Option
Explanation:
Step 1: int a = 300, b, c; here variable a is initialized to '300', variable b and c are declared, but not initialized.
Step 2: if(a >= 400) means if(300 >= 400). Hence this condition will be failed.
Step 3: c = 200; here variable c is initialized to '200'.
Step 4: printf("%d, %d, %d\n", a, b, c); It prints "300, garbage value, 200". because variable b is not initialized.
Discussion:
16 comments Page 2 of 2.
Moni said:
1 decade ago
@Soori.
If we don't mention braces "{ }"after if then only one line gets executed.
If we don't mention braces "{ }"after if then only one line gets executed.
Soori said:
1 decade ago
Hello.
I don't understand one thing if the loop fails it should not initialize both b and c why it initializing c but not b cam any one explain?
I don't understand one thing if the loop fails it should not initialize both b and c why it initializing c but not b cam any one explain?
Harini said:
1 decade ago
if(a>=400)
b=300;
This condition is false because 300<400
Now C=200;
This statement is out of the if block so it would be executed whether "if" is true or false. Hence c=200.
Now as b is not initialized to any value it has garbage value.
So the values of a,b,c are 300, garbage, 200.
b=300;
This condition is false because 300<400
Now C=200;
This statement is out of the if block so it would be executed whether "if" is true or false. Hence c=200.
Now as b is not initialized to any value it has garbage value.
So the values of a,b,c are 300, garbage, 200.
Arun said:
1 decade ago
Can any one explian in details...
Mani said:
1 decade ago
if(a >= 400)
b = 300; hear 300>=400
This is false. So the compiler don't read the full statement .
Then goes 2 next statement ie c=200.
b = 300; hear 300>=400
This is false. So the compiler don't read the full statement .
Then goes 2 next statement ie c=200.
Soundarya said:
1 decade ago
Can't understand why variable cant be initialized to 300.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers