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");
}
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.
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)
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.
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)
Shamil said:
3 years ago
I understand, thanks all.
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.
And the int fun()
{//this block is not executed
}
Hope you will understood.
HarishMahajan said:
7 years ago
Why option Hi Hello not print?
Newb said:
7 years ago
All global initialized to zero is based on the compiler. That may not be true in all cases.
Shilpi karn said:
8 years ago
main() function is inside main() so it give infinite loop.
Vishnu said:
8 years ago
Some compilers give, after while print statement "because default i=0 "
But some compilers executes infinite loop.
But some compilers executes infinite loop.
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.
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.
Saieesh said:
8 years ago
Global variables are initialized with zero.
So, while(0) is false condition.
So, while(0) is false condition.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers