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.

RAVIKUMAR RONAD said:   3 years ago
In above code, The declaration of variable is "int i" for this i values is 0,

How?

Means In C Programming memory management (User Space), has a Data segment memory allocation for global variables. In Data segment there two types of Data segments are:
1. BSS(Block Started by Symbol) or unitization
2. Initialization.

In 1st first type, any variable un initialisation or initialisation with zero value then those variables called variables declaration or variable definition.

So that here i = 0 is the value, Then While the (0) condition is false, which means while the loop will execute, direct printf function prints the " Hello" string/word in the Stdout/Console.


Thank you.
(1)

Nitish anand said:   1 decade ago
@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.

Karthik said:   1 decade ago
@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.

Ravikumar Ronad said:   3 years ago
Global and static variables are by default value is 0 (zero). In the above program variable, I is declared as a global variable and not assigning any non-zero value to I variable or its means except 0 or other than zero value.

So it takes zero as the value for I then, in the main function code, while of 0 conditions is false, then while loop is not executed, next printf gets executed and print the "hello" string on the console or screen of your system.

Thank you.
(2)

Rohit said:   6 years ago
@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.

Rashid said:   1 decade ago
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.

Abhioxic said:   9 years ago
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.
}

Veerpal said:   8 years ago
#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.

Reza said:   1 decade ago
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.

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.


Post your comments here:

Your comments will be displayed after verification.