C Programming - Functions - Discussion

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");
}

[A]. Hello
[B]. Hi Hello
[C]. No output
[D]. Infinite loop

Answer: Option A

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".


Naveen said: (Feb 16, 2011)  
All global variables are initialized to zero.

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

So it prints 'Hello'.

Boopathy said: (Feb 21, 2011)  
Global variable never gets initialized to zero. Unless you specify them.

Sunil said: (Feb 22, 2011)  
What naveen said is correct, All global variables by default initialized to Zero.

Bhushan said: (May 23, 2011)  
Yes. Golbal variable is by default to 0 so while condition going to fail.

Sudher said: (Jun 7, 2011)  
Yes by default all global variables are initialized to Zero(0)

Pallavi Patil said: (Jan 8, 2012)  
Hey any global variable is get initialized to zero automatically.

Vishwaroopa said: (Feb 15, 2012)  
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: (Mar 16, 2012)  
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: (Aug 29, 2012)  
Ya its correct global variable are initialized to zero by default.

Addy said: (Oct 18, 2012)  
Yes definitely global variables are set 0 by default but this output is correct or not.

S Satheesh Reddy said: (Feb 3, 2013)  
What @Naveen said is correct, All global variables by default initialized to(0) Zero. So the condition while(0) becomes false.

Reza said: (May 1, 2013)  
int fun()
{
printf("Hi");
}

This function fails in compile time, so to its signature type "int", it must return a value with the type "int".

Here is the compiler generated error:

Error: C4716: 'fun' : must return a value.

Rashid said: (May 5, 2013)  
Hello friend I have to make a program in function in which two prototypes are given one small(); and other is large(); and in function definition it tells small no and large no and these value returned to main and in main it shows these values like :

printf("smallest no is %d",small);
printf("largest no is %d",large);

So please tell me which the logic is used in these 2 prototypes.

Mounika said: (Jul 5, 2013)  
Hi friends:

Can anyone explain me why the program is wrong?

#include<stdio.h>
extern int j;

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

Nitish Anand said: (Sep 1, 2013)  
@Mounika.

Hey you have declared j as external reference. So its value must be initialized and it should be outside the main function.

Refer to example given below :

#include<stdio.h>
extern int j;

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

//Above program runs.

But it will not work if you initialize j within function without giving its data type or you not initialize j anywhere.

#include<stdio.h>
extern int j;

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

//Above program won't run.

Nikhil said: (Jan 3, 2014)  
Why option B is not correct?

Karthik said: (Jan 4, 2014)  
@Nikhil

Since "i" will be initialized to zero, the while condition fail's and the body of the while loop will not be executed. It directly prints "hello"(which is outside the body of the while loop) and the program will end.

The reason why "Hi" didn't display is that the function "fun()" was never called from main(). (since the call to the function "fun()"is made inside the while loop, which was failed to execute as the condition failed.)

So the right answer is option A.

Nikhil Prasad said: (Aug 5, 2016)  
What will be the output if 'while' condition gets true?

Hasshu said: (Sep 20, 2016)  
What is a global variable?

Abhioxic said: (Feb 21, 2017)  
Static variables (file scope and function static) are initialized to zero:

int x; // zero
int y = 0; // also zero

void foo()
{
static int x; // also zero
}

Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior.

void foo()
{
int x;
printf("%d", x); // the compiler is free to crash here.
}

Saieesh said: (Mar 28, 2017)  
Global variables are initialized with zero.

So, while(0) is false condition.

Veerpal said: (Jun 26, 2017)  
#include<stdio.h>
int i=1;
fun();
int main()
{
while(i<=3)
{
fun();
main();
i++;
}
printf("Weclome to IndiaBIX.com..!");
return 0;
}
fun()
{
printf("hllo\n");
}

Why above program is giving infinite loop? Find out the errors.

Vishnu said: (Sep 13, 2017)  
Some compilers give, after while print statement "because default i=0 "

But some compilers executes infinite loop.

Shilpi Karn said: (Jan 3, 2018)  
main() function is inside main() so it give infinite loop.

Newb said: (Jul 5, 2018)  
All global initialized to zero is based on the compiler. That may not be true in all cases.

Harishmahajan said: (Oct 16, 2018)  
Why option Hi Hello not print?

Rohit said: (Nov 15, 2019)  
@Harish, we know that global variable which is "i" is not with any assign value soo, in C programming global variable takes by default value "0". And we know that execution of program starts from main function, then while(0) which is treated as the false and while block will not be execute, and prints "hello" on screen.
And the int fun()
{//this block is not executed
}

Hope you will understood.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.