Jump to content
ChetanaSforum
Sign in to follow this  
chitkaraankit

predict output c++

Recommended Posts


Using the static keyword prevents the variable being re-initialized for every call of the main function. For instance, if the declaration was

int var=5;

for every call of main, the variable var would be re-initialized to 5. And hence, the answer would be an infinite number of 5's. But, when you use the static keyword, the variable is not re-initialized, and retains the last value of "var", and hence prints "5 4 3 2 1"

 

So for every call of main, the value is decremented. And as for your question, why "-1" is not printed? After the value "1" is printed, the function call changes the value of "var" to zero(0).

 

In C/C++, zero within a conditional statements such as the "if" means "false". Until the value is "1", the "main" function is called and the number printed, and when the value becomes zero, the condition becomes false, and the program exits. Posted Image

Share this post


Link to post
Share on other sites

Using the static keyword prevents the variable being re-initialized for every call of the main function. For instance, if the declaration was

 

 

for every call of main, the variable var would be re-initialized to 5. And hence, the answer would be an infinite number of 5's. But, when you use the static keyword, the variable is not re-initialized, and retains the last value of "var", and hence prints "5 4 3 2 1"

 

So for every call of main, the value is decremented. And as for your question, why "-1" is not printed? After the value "1" is printed, the function call changes the value of "var" to zero(0).

 

In C/C++, zero within a conditional statements such as the "if" means "false". Until the value is "1", the "main" function is called and the number printed, and when the value becomes zero, the condition becomes false, and the program exits. Posted Image

Hey manikanadan can u please tell me frm where should i prepeare for output questions.????

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  



×