C Programming - Control Instructions - Discussion

5. 

Which of the following statements are correct about the below program?

#include<stdio.h>
int main()
{
    int i = 0;
    i++;
    if(i <= 5)
    {
        printf("IndiaBIX\n");
        exit(0);
        main();
    }
    return 0;
}

[A]. The program prints 'IndiaBIX' 5 times
[B]. The program prints 'IndiaBIX' one time
[C]. The call to main() after exit() doesn't materialize.
[D]. The compiler reports an error since main() cannot call itself.

Answer: Option B

Explanation:

Step 1: int i = 0; here variable i is declared as an integer type and initialized to '0'(zero).
Step 2: i++; here variable i is increemented by 1(one). Hence, i = 1
Step 3: if(i <= 5) becomes if(1 <= 5) here we are checking '1' is less than or equal to '5'. Hence the if condition is satisfied.
Step 4: printf("IndiaBIX\n"); It prints "IndiaBIX"
Step 5: exit(); terminates the program execution.

Hence the output is "IndiaBIX".


Amit said: (Jul 11, 2010)  
Can main have a call to itself?

Poonam said: (Sep 9, 2011)  
Yes main can call itself

#include<stdio.h>
int i=0;
int main()
{
i++;
if(i <= 5)
{
printf("IndiaBIX\n");
main();

}
return 0;
}

o/p
IndiaBIX
IndiaBIX
IndiaBIX
IndiaBIX
IndiaBIX

Shiwam said: (Sep 11, 2011)  
It also gives error that exit() was not declared in this scope.

Vikas said: (Sep 23, 2011)  
But after exit() main() will be executed or not?

Sathuragiri said: (Oct 27, 2011)  
This is simple. It will print indiabix one time because there will be conditional statement presented. Instead of that if there while or for (looping statements) presented means it will printed 5 times.

Chaitali said: (Nov 28, 2011)  
Can we call main() function any where in the program?

Pruthvi said: (May 16, 2012)  
Will exit (0) and exit (1) both results in execution of exit statement?

Syam said: (Feb 6, 2013)  
If I remove the main() in the program what happen?

Please explain?

Keerthi said: (Jul 9, 2013)  
@Syam.

Nothing will happen when we remove the inside main() function,

Because exit(0) will terminate the program, so the printf statement will execute one time.

Keerthi said: (Jul 9, 2013)  
@syam
nothing will happen when we remove the inside main()function,
because exit(0) will terminate the program,so the printf statement will execute one time

Pikachu said: (Oct 3, 2014)  
Why option C can not be answer?

Please explain.

Jay Prakash said: (Sep 20, 2015)  
B and C will be the correct options. As program terminates when exit(0) is called. So call to main() after exit() doesn't materialize.

Omkar said: (Sep 28, 2015)  
According to me,

Answer should be C as stdlib.h isn't included and it hence cannot be resolved;

Renonsz said: (Jan 11, 2017)  
Option C is also the correct answer.

Pooja said: (Jul 30, 2017)  
Here, exit(0); requires <stdlib.h> included in file. Is'nt it?
as per this,it should give error.

Pooja said: (Jul 30, 2017)  
In function 'main':
Line 11: warning: incompatible implicit declaration of built-in function 'exit'.
Line 15: error: expected declaration or statement at end of input.

If we compile the programme given in question then it gives above error statements.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.