CrazyEngineers
  • Need help in Understanding an output problem of C++

    Sanyam Khurana

    Sanyam Khurana

    @sanyam-Nl7Zqc
    Updated: Oct 23, 2024
    Views: 1.1K
    Hey,

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

    ‎#include<stdio.h>
    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..?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Sanyam Khurana

    MemberJul 3, 2013

    #-Link-Snipped-# and #-Link-Snipped-#

    Can you help in this..?
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorJul 3, 2013

    Tagging #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberJul 3, 2013

    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 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberJul 3, 2013

    I'm not getting it, what is initialized by the function, how it is updated, and what condition is being checked..??

    Please help 😕
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberJul 3, 2013

    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..?
    Are you sure? This action cannot be undone.
    Cancel
  • Jeffrey Arulraj

    MemberJul 4, 2013

    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-#
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberJul 4, 2013

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberJul 4, 2013

    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
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberJul 4, 2013

    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..!!! 🎉
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberJul 4, 2013

    I think I am slightly late here, but anyways, this was a good example.
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberJul 4, 2013

    me too! returned home now 😛
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberJul 4, 2013

    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.. 👍
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register