Need help in Understanding an output problem of C++

Hey,

I saw a problem to know the output of the below mentioned code..

β€Ž#include
int fun()
{
static int num = 40;
return num--;
}
 
int main()
{
for(fun(); fun(); fun())
{
printf("%d ", fun());
}
 
return 0;
}
Now 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.

So, first of all, is this correct?

and how we judge the output..?

Replies

  • Sanyam Khurana
    Sanyam Khurana
    #-Link-Snipped-# and #-Link-Snipped-#

    Can you help in this..?
  • Kaustubh Katdare
    Kaustubh Katdare
    Tagging #-Link-Snipped-#
  • rahul69
    rahul69
    Sanyam Khurana
    #-Link-Snipped-# and #-Link-Snipped-#

    Can you help in this..?
    OK , let me answer your questions one by one
    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:

    1. Initialization condition;
    2. loop(test condition)
    3. {
    4. body statements.....
    5. ...
    6. updation (ie increment etc.);
    7. }

    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 Khurana
    Sanyam Khurana
    I'm not getting it, what is initialized by the function, how it is updated, and what condition is being checked..??

    Please help πŸ˜•
  • Sanyam Khurana
    Sanyam Khurana
    Ok, 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 Arulraj
    Jeffrey Arulraj
    The 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
    Sanyam Khurana
    Conqueror
    The 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-#
    Wouldn'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.

    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
    rahul69
    Sanyam Khurana
    Wouldn'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.
    Yes u are right πŸ˜€.
    Sanyam Khurana
    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?
    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
  • Sanyam Khurana
    Sanyam Khurana
    rahul69
    Yes 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..!!! πŸŽ‰
  • simplycoder
    simplycoder
    I think I am slightly late here, but anyways, this was a good example.
  • Vishal Sharma
    Vishal Sharma
    me too! returned home now πŸ˜›
  • Sanyam Khurana
    Sanyam Khurana
    simplycoder
    I think I am slightly late here, but anyways, this was a good example.
    Vishal0203
    me too! returned home now πŸ˜›
    Don't get disappointed if I have or someone else have more doubts they will ask you here.. πŸ‘

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...