Thread: C programming flow in relation to Stack

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    Exclamation C programming flow in relation to Stack

    Hi, I was reading "Let us C" by Yashwant Kanektar (Thanks for this great book from the recomended books thread!) when i came accross this problem:

    predict the outcome:

    Code:
    #include <stdio.h>
    
    int i = 0;
    
    int main()
    {
    printf("\nmain's i = %d", i);
    i++;
    val();
    printf("\nmain's i = %d", i);
    val();
    
    }
    
    val()
    {
    i = 100;
    printf("\nval's i = %d", i);
    i++;
    }
    When i compiled the program and run it, this was what came out:

    main's i = 0
    val's i = 100
    main's = 101
    val's i = 100

    I thought the answer should have been
    main's i = 0
    val's i = 101
    main's = 101
    val's i = 100

    i dont get the flow of the program. I mean, I understand that whenever val() is called, i resets to 100, however, I don't get why main becomes 101.

    Plus, the book talks about "stack" which is responsible for the flow of control. Can someone explain to me what happens inside the computer (the flow of the program when the stacking system is implemented).

    BTW: I have already read up on the internet about stack, I know that stack is LIFO (last in first out), sadly, I think the information I have obtained is still lacking.
    In addition, I have already read about the different variable storage types.

    I don’t get why the answer came at like that in the program.
    Can anyone please explain this to me? Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mousanony
    I mean, I understand that whenever val() is called, i resets to 100, however, I don't get why main becomes 101.
    Observe the i++; statement in val().

    By the way, you should indent the code. The val function should also be forward declared with a void return type, and you should specify void as the parameter list to indicate that it takes no arguments rather than an unknown number of arguments, although that is not a problem here:
    Code:
    #include <stdio.h>
    
    void val(void);
    
    int i = 0;
    
    int main(void)
    {
        printf("\nmain's i = %d", i);
        i++;
        val();
        printf("\nmain's i = %d", i);
        val();
    
        return 0; /* optional in C99 */
    }
    
    void val(void)
    {
        i = 100;
        printf("\nval's i = %d", i);
        i++;
    }
    That said, maybe this can be a lesson to you as to why you should avoid global variables: they can make it more difficult for you to reason about your program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    New York
    Posts
    11
    well, the outcome is simple to predict. 'i' is set to zero
    at the start of your program, then in main() 'i' is printed
    and then incremented by one. main() calls val() then,
    val() sets 'i' to 100, prints 'i', then increments 'i' by one.
    in main, it prints 'i' again. so the algorithm would be:

    Code:
    i = 0
    
    int main()
    {
    print 'i'
    increment i
    val()
    print 'i'
    val()
    return 0
    }
    
    val()
    { 
     i = 100
     print 'i'
     i++
    }
    Last edited by ComputerWarrior; 08-04-2010 at 12:12 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are taking the code and making it worse.
    - Code is not indented.
    - main and val lacks a return type.
    - val is not forward declared.
    If you're going to show code, at least set a good example.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    New York
    Posts
    11
    Elysia, im showing the code 'as is'. im pretty sure you saw i said 'algorithm' in
    my post above.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    "Let us C" by Yashwant Kanektar (Thanks for this great book from the recomended books thread!)
    Oy vey...I can't believe someone recommended that piece of Turbo C-pushing crap.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You deleted the return type of main, though I see it's there now.
    And it's not indented. Indentation is required to make things readable. Even if it's just an algorithm or example, try indenting.

    Shall we have a mod delete that recommendation?
    Last edited by Elysia; 08-04-2010 at 12:33 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    im showing the code 'as is'. im pretty sure you saw i said 'algorithm' in
    my post above.
    Yeah, except that in this case presenting pseudocode does not really make things clearer since the code itself already exists and is simple enough to analyse by inspection.

    Anyway, I shall now call everyone to support "pseudo-Python" for pseudocode: you automatically are inclined to indent properly
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Thanks everyone, I really appreciate people who take the time to help others and explain stuff thoroughly.
    BTW , laserlight, when you say forward declare the function, did you mean to give it a function prototype? Do they mean the same thing?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mousanony
    BTW , laserlight, when you say forward declare the function, did you mean to give it a function prototype? Do they mean the same thing?
    Yes. There may or may not be minor technical differences between the two terms, but here I mean them to be the same thing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Ok, got it. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

Tags for this Thread