Thread: easy question, i think...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    easy question, i think...

    Hi,

    I have this code:

    Code:
    int main()
    {
    int a = 1;
    test();
    return a;
    }
    
    void test()
    {
    int b;
    int c = 4;
    b = c + a;
    }
    I know this isn't right, so how do you make it right without having to change much of the concept? I simplified it from a more tedious code. Thanks in advance.

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    int main()
    {
    int a = 1;
    test(a);
    return a;
    }
    
    void test() /* can you figure out yourself how to access the 'a' passed from main? */ 
    {
    int b;
    int c = 4;
    b = c + a;
    }

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    hey, thanks for the quick reply...

    I understand that you can pass a variable from main... is there any other way besides that?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A global variable?

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by zacs7 View Post
    A global variable?
    That sounded to me as if you cried out loud saying:
    ESBO ! ! ! !

    PS: Fond memories of him

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Another way if you knew what was going on would be inline assembly

    Neither is a good idea in this case.

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    me-eating-rice, are you from India?
    Is your rice-eating over?

    me-not-eating-rice

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    Code:
    #include <stdio.h>
    
    int a = 1;
    
    int test()
    {
            return (4 + a);
    }
    
    int main(void)
    {
            printf("&#37;d\n", test());
            return test();
    }
    or

    Code:
    #include <stdio.h>
    
    #define a 1
    
    int test()
    {
            return (4 + a);
    }
    
    int main(void)
    {
            printf("%d\n", test());
            return test();
    }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think a define is more preferable here. Or const int (though that would be more appropriate for C++).
    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.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You do know that main returning nonzero means that the program exited under faulty or erroneous conditions?

    And I don't see a problem with usig a function parameter at all.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no law for returned values. 1 can mean success and 0 can mean failure. It's up to the developer to decide...
    I won't be taking any sides on what's best, though.
    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.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    There is no law for returned values. 1 can mean success and 0 can mean failure. It's up to the developer to decide...
    I won't be taking any sides on what's best, though.
    It is however a good idea to try to keep it the same throughout the system, and not "mix and match", because sooner or later you will find yourself having to look up what the actual result should be.

    Many systems use "negative for fail", zero or greater is OK. This of course falls down if you return unsigned integers (or pointers)...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the best way would actually be to creates defines in some header file and use through your project.
    Returning 1 for failure in one and 0 for failure in one is not what I'd recommend, as you say mats.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM