C programming flow in relation to Stack

This is a discussion on C programming flow in relation to Stack within the C Programming forums, part of the General Programming Boards category; Hi, I was reading "Let us C" by Yashwant Kanektar (Thanks for this great book from the recomended books thread!) ...

  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
    21,589
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    "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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    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
    21,589
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,589
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21