Need help in Understanding an output problem of C++
Hey,
I saw a problem to know the output of the below mentioned code..
So, first of all, is this correct?
and how we judge the output..?
I saw a problem to know the output of the below mentioned code..
β#includeNow my problem is that, I've never seen that we can use a function call in initialisation, updation and conditional expression of a for loop.int fun() { static int num = 40; return num--; } int main() { for(fun(); fun(); fun()) { printf("%d ", fun()); } return 0; }
So, first of all, is this correct?
and how we judge the output..?
Replies
-
Sanyam Khurana#-Link-Snipped-# and #-Link-Snipped-#
Can you help in this..? -
Kaustubh KatdareTagging #-Link-Snipped-#
-
rahul69
OK , let me answer your questions one by oneSanyam Khurana#-Link-Snipped-# and #-Link-Snipped-#
Can you help in this..?
So, first of all, is this correct?
Yes this is technically correct, i.e it will not give any errors.
Reason being, we can write any statement inside for loop initialisation . We can even leave it empty, see for example:
for(;;;);
is a valid statement. π
and how we judge the output..?
For judging the output, let me explain the working of a for loop in a general manner:
- Initialization condition;
- loop(test condition)
- {
- body statements.....
- ...
- updation (ie increment etc.);
- }
which is written in a for loop as :
for(Initialization; test; updation)
{
body statements...
}
Notice above that initialization statement is executed only once
Now in the code u mentioned, there can be many doubts which u may face as this code uses some tricky concepts as static variable , post increment operator etc, so try solving the output, it will be along the lines of :
38,35,32.... upto 2 .
If u face any problem do mention it, I'll be happy to help π -
Sanyam KhuranaI'm not getting it, what is initialized by the function, how it is updated, and what condition is being checked..??
Please help π -
Sanyam KhuranaOk, let's take it step by step here
in Initialisation, we have fun(); so, it would return 40 and then in memory it would have value 39 due to post increment, but where is this value returned by the function going to be stored, I mean, we have to store the value in a variable which is returned by the function so as to use it, but here no variable is there..? -
Jeffrey ArulrajThe fun() returns a value 39 and that is assigned to the control variable of the for loop
I think you can look into the function of return() statement for info #-Link-Snipped-# -
Sanyam Khurana
Wouldn't it first return the value of num and then decrement it in the memory, due to post decrement operator..?ConquerorThe fun() returns a value 39 and that is assigned to the control variable of the for loop
I think you can look into the function of return() statement for info #-Link-Snipped-#
As in, it would give value of 40 only, but in memory the value would be 39.
Now, whatever value may the function has given, but in order to run the for loop, we must have some initialisation condition and test and update expression as in
. //some header files void main(){ //assume some code for(int i=1;i<10;i++){ //assume some code } }
Now, whatever value is returned, but shouldn't that be stored in a variable ?
because we have to continuously use it in iteration? -
rahul69
Yes u are right π.Sanyam KhuranaWouldn't it first return the value of num and then decrement it in the memory, due to post decrement operator..?
As in, it would give value of 40 only, but in memory the value would be 39.
See, as I told earlier these three expressions viz. initialization, test and update are not mandatory conditions, u may not initialize if u don't want to, same goes for other two, we write these conditions as per the need of our program.Sanyam KhuranaNow, whatever value may the function has given, but in order to run the for loop, we must have some initialisation condition and test and update expression as in
. //some header files void main(){ //assume some code for(int i=1;i<10;i++){ //assume some code } }
Now, whatever value is returned, but shouldn't that be stored in a variable ?
because we have to continuously use it in iteration?
Lets take each condition one by one:
We test to come out of loop, test means either true (non zero) or false (zero value). Here whenever function will return zero, loop will end, so no need to store itπ.
We update the value so that test condition can be fulfilled, now as the value is changing by means of fun() function. So no need of update too.
And, no need to initialize here as the variable is initialized to 40 in the fun() function.
Hope it clears your doubts π
-Rahul -
Sanyam Khuranarahul69Yes u are right π.
See, as I told earlier these three expressions viz. initialization, test and update are not mandatory conditions, u may not initialize if u don't want to, same goes for other two, we write these conditions as per the need of our program.
Lets take each condition one by one:
We test to come out of loop, test means either true (non zero) or false (zero value). Here whenever function will return zero, loop will end, so no need to store itπ.
We update the value so that test condition can be fulfilled, now as the value is changing by means of fun() function. So no need of update too.
And, no need to initialize here as the variable is initialized to 40 in the fun() function.
Hope it clears your doubts π
-Rahul
Yes, I got it, hows it executed, in each iteration, when update expression is executed it call the fun function , again value of num decrements, and then in test condition, it again decrements, and finally when output is given the function is called and value of num is one less than that...
Yes, I got it now..
Thanks man..!!! π -
simplycoderI think I am slightly late here, but anyways, this was a good example.
-
Vishal Sharmame too! returned home now π
-
Sanyam KhuranasimplycoderI think I am slightly late here, but anyways, this was a good example.
Don't get disappointed if I have or someone else have more doubts they will ask you here.. πVishal0203me too! returned home now π
You are reading an archived discussion.
Related Posts
Hi,
I was just wondering about LIFE as a BIG PICTURE and how to get things handled.
So I'll be making a note of the most important things that I've...
The purpose of doing this (from my INTRO THREAD) :
A little background story : I've had enough of trolls,pictures,videos and all that useless stuff which has successfully spammed such...
Well you've heard Overclocking of Processors, but Overclocking the brains ? Rubbish, isn't it?
Same were my expression when I first read the article on Overclocking the Brain.
The headset...
Guys,
I have a problem with my bike front disc. The problem is, when i applied the brake my bike stop instantly, but after release the brake i gave the...
Hey,βSo I was planning to learn the Banking Process (the basics) and need a new Bank Account for LONG-TERM use.βI know how to make online transactions and use DC/CC online.βHave...