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 2 of 3.

Mounika said:   1 decade ago
Hi friends:

Can anyone explain me why the program is wrong?

#include<stdio.h>
extern int j;

int main()
{
printf("%d",j);
return 0;
}

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.

S satheesh reddy said:   1 decade ago
What @Naveen said is correct, All global variables by default initialized to(0) Zero. So the condition while(0) becomes false.

Vishnu said:   8 years ago
Some compilers give, after while print statement "because default i=0 "

But some compilers executes infinite loop.

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

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

So it prints 'Hello'.

Newb said:   7 years ago
All global initialized to zero is based on the compiler. That may not be true in all cases.

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

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

Saieesh said:   8 years ago
Global variables are initialized with zero.

So, while(0) is false condition.

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


Post your comments here:

Your comments will be displayed after verification.