C Programming - Functions - Discussion

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

int main()
{
    int i=0;
    i++;
    if(i<=5)
    {
        printf("IndiaBIX");
        exit(1);
        main();
    }
    return 0;
}
Prints "IndiaBIX" 5 times
Function main() doesn't calls itself
Infinite loop
Prints "IndiaBIx"
Answer: Option
Explanation:

Step 1: int i=0; The variable i is declared as in integer type and initialized to '0'(zero).

Step 2: i++; Here variable i is increemented by 1. Hence i becomes '1'(one).

Step 3: if(i<=5) becomes if(1 <=5). Hence the if condition is satisfied and it enter into if block statements.

Step 4: printf("IndiaBIX"); It prints "IndiaBIX".

Step 5: exit(1); This exit statement terminates the program execution.

Hence the output is "IndiaBIx".

Discussion:
12 comments Page 1 of 2.

Saguuuu said:   4 years ago
exit (0) means a Program terminates normally successfully.
exit (1) means a program terminates normally unsuccessfully.

Somya said:   7 years ago
Can anybody Explain "exit"?

Tamilmaran c said:   9 years ago
It acceptable answer D:

#include<stdio.h>
#include<stdlib.h>

int main()
{
int i=0;
i++;
if(i<=5)
{
printf("IndiaBIX");
exit(1);
main();
}
return 0;
}

Dimple said:   9 years ago
exit(0) is a successful termination of a program.

Satakshi said:   9 years ago
What will happen if in this prog there will exit(0) instead of exist(1)?

PrattJ said:   1 decade ago
exit(0)=>causes the program to exit with a successful termination.

exit(1)=>causes the program to exit with a system-specific meaning.

On many systems, exit(1) signals some sort of failure, however there is no guarantee.

Rohit said:   1 decade ago
Elaborate exit(0) and exit(1)?

Rohit sharma said:   1 decade ago
Yes main() can call it self but in this program there is exit(1) just before we called the main() function in the main program so it will terminate the program execution before calling main() function again :).

Prerna said:   1 decade ago
What would be the answer if it would have been exit(0) instead of exit(1).

Leszek said:   1 decade ago
Isn't B also correct? Maybe main() COULD call itself, but in this case it doesn't because it never gets to that point.


Post your comments here:

Your comments will be displayed after verification.