Thursday, July 30, 2009

What is the O/P of the following C Program.?

main()


{


static int var = 5;


printf("%d",var--);


if(var)


main();


}

What is the O/P of the following C Program.?
the output is the sequence 54321
Reply:static variables can only be accessed from within the function they are declared in this case main(). They are only initialized once the first time (in th is case to 5.





Tracing the action you have the declaration of the var to be 5. Then it prints the var-- which is the same thing as printing the var 1st then decrementing it. if(var) then call main(). var is not 0 so it calls main(). Then follows this process but var is now 4, then 3, then 2, then 1, then 0 but it will not call main again so it exits from all of the other previous call of main till the end of the program.





output:





54321





p.s.


2nd question I've noticed with the same subject, try writing the code and compiling it.
Reply:Hi if u declare var as static then it will come out as soon as it reaches 0.But if u declaire it as int the it goes to an infinite loop.





-Venu


No comments:

Post a Comment