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.

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.

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.

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.

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

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.

Nikhil said:   1 decade ago
Why option B is not correct?

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.

Nikhil Prasad said:   9 years ago
What will be the output if 'while' condition gets true?

Hasshu said:   9 years ago
What is a global variable?

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


Post your comments here:

Your comments will be displayed after verification.