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");
}
Hello
Hi Hello
No output
Infinite loop
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 1 of 3.

Naveen said:   1 decade ago
All global variables are initialized to zero.

So here i=0
while(0) //false

So it prints 'Hello'.

Boopathy said:   1 decade ago
Global variable never gets initialized to zero. Unless you specify them.

Sunil said:   1 decade ago
What naveen said is correct, All global variables by default initialized to Zero.

Bhushan said:   1 decade ago
Yes. Golbal variable is by default to 0 so while condition going to fail.

Sudher said:   1 decade ago
Yes by default all global variables are initialized to Zero(0)

Pallavi Patil said:   1 decade ago
Hey any global variable is get initialized to zero automatically.

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.

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.

Hema said:   1 decade ago
Ya its correct global variable are initialized to zero by default.

ADDY said:   1 decade ago
Yes definitely global variables are set 0 by default but this output is correct or not.


Post your comments here:

Your comments will be displayed after verification.