C Programming - Functions - Discussion
Discussion Forum : Functions - Find Output of Program (Q.No. 3)
3.
What will be the output of the program?
#include<stdio.h>
int i;
int fun();
int main()
{
while(i)
{
fun();
main();
}
printf("Hello\n");
return 0;
}
int fun()
{
printf("Hi");
}
Answer: Option
Explanation:
Step 1: int i; The variable i is declared as an integer type.
Step 1: int fun(); This prototype tells the compiler that the function fun() does not accept any arguments and it returns an integer value.
Step 1: while(i) The value of i is not initialized so this while condition is failed. So, it does not execute the while block.
Step 1: printf("Hello\n"); It prints "Hello".
Hence the output of the program is "Hello".
Discussion:
30 comments Page 3 of 3.
ADDY said:
1 decade ago
Yes definitely global variables are set 0 by default but this output is correct or not.
Hema said:
1 decade ago
Ya its correct global variable are initialized to zero by default.
Dileep said:
1 decade ago
Ya all the global variable are intialised to zero it, s true I executed the programe also. Yes you can check it many books also.
Vishwaroopa said:
1 decade ago
Global variable is set to 0.
But output is not "Hello"
Function 'fun' has return type 'int'. Hence it expects a 'return <value>' in the end. Hence it becomes syntax error during compilation.
But output is not "Hello"
Function 'fun' has return type 'int'. Hence it expects a 'return <value>' in the end. Hence it becomes syntax error during compilation.
Pallavi Patil said:
1 decade ago
Hey any global variable is get initialized to zero automatically.
Sudher said:
1 decade ago
Yes by default all global variables are initialized to Zero(0)
Bhushan said:
1 decade ago
Yes. Golbal variable is by default to 0 so while condition going to fail.
Sunil said:
1 decade ago
What naveen said is correct, All global variables by default initialized to Zero.
Boopathy said:
1 decade ago
Global variable never gets initialized to zero. Unless you specify them.
Naveen said:
1 decade ago
All global variables are initialized to zero.
So here i=0
while(0) //false
So it prints 'Hello'.
So here i=0
while(0) //false
So it prints 'Hello'.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers