C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 11)
11.
What will be the output of the program?
#include<stdio.h>

int main()
{
    int i=1;
    if(!i)
        printf("IndiaBIX,");
    else
    {
        i=0;
        printf("C-Program");
        main();
    }
    return 0;
}
prints "IndiaBIX, C-Program" infinitely
prints "C-Program" infinetly
prints "C-Program, IndiaBIX" infinitely
Error: main() should not inside else statement
Answer: Option
Explanation:

Step 1: int i=1; The variable i is declared as an integer type and initialized to 1(one).

Step 2: if(!i) Here the !(NOT) operator reverts the i value 1 to 0. Hence the if(0) condition fails. So it goes to else part.

Step 3: else { i=0; In the else part variable i is assigned to value 0(zero).

Step 4: printf("C-Program"); It prints the "C-program".

Step 5: main(); Here we are calling the main() function.

After calling the function, the program repeats from step 1 to step 5 infinitely.

Hence it prints "C-Program" infinitely.

Discussion:
11 comments Page 1 of 2.

Mgkr said:   6 years ago
Prints infinitely until stckoverflow occurs.

Swetha said:   6 years ago
What about the stack overflow concept?

Abhi said:   6 years ago
Will, it run until the stack overflows or will it run infinitely? Please tell me.

Parth said:   8 years ago
Instead of if (!i) we use if (i) then what will be the output of program.

Akshay said:   8 years ago
This program not run because after if { bracket is not given.

Rathna said:   8 years ago
@Ruchi.

If (not 5) is false because it's 5 hence else is executed and prints C-program infinitely because it's the similar case.

Rathna M said:   8 years ago
@Saikrishna.

Inside else main is called again, it means the program starts from starting hence prints C-program infinitely. Whenever, the main function is called i initializes to 1 always in this program.

Saikrishna.y said:   1 decade ago
After recalling the main, the value of I is not changed why. In the else part I is initialized with 0.

So calling main part I is to be reinitialized with 0. It is my doubt.

Ruchi said:   1 decade ago
What if we put i=5 in program?

Atul said:   1 decade ago
Again initialized to 1.


Post your comments here:

Your comments will be displayed after verification.