#1
  1. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0

    Book To Old To Learn C From?


    I have the following (incredibly) simple code:

    PHP Code:
    #include <stdio.h>

    void main() {
       
    printf("Hello, world!");
       exit();

    When I feed this (incredibly) simple code to the gcc (4.8 I think) compiler, I get a warning about the return value, and it halts.

    Now, this is an exact replica of the first code presented in the C book I had intended to use for learning, but if this won't compile, that would seem to suggest that perhaps there is something that this (somewhat) dated book is not covering. Is this a warning perhaps that I should get a newer C book before I dive into learning C/C++?

    I have succesfully compiled this code on an old copy of Borland's C/C++ compiler under DOS. I just need to know if there's some new concept that my book is not incorporating here.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    the problem is unix. in unix the return code of your main function will be passed to the calling program.
    you donīt supply one.
    DOS does not need this value though.

    itīs only a warning, not an error... so you should have an "a.out" in your directory...

    the best book i found for learning c (if you already know basic or pascal or some other languages) is the "Programming C" by Kernighan and Ritchie - the inventors of C and unix :)
  4. #3
  5. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0
    Aha.... thank you! And thank you for the book reccomendation. The reason I really enjoyed Programming Perl is because it is written by someone(s) who clearly know exactly what is going on. I prefer to have all of the icky details laid out in front of me so that I can peruse them and piece things together instead of having some watered down version of the language tossed at me only to find out later that I've been using horrible programming practice in it the whole time.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    34
    Rep Power
    14
    According to the ANSI standard, main() is supposed to return an int,

    and if you specify a function to return a value, and you don't return one, the compiler will yell at you.

    Any good C programmer will always treat warnings as errors.

    You should compile with -Wall -ansi -pedantic flags, unless you are intentionally using non ANSI code.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    if you code for MSDOS, you really donīt need to care.
    but in general i agree. keep your code 100% ansi compliant! one day you might want to use it on another OS....
  10. #6
  11. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0
    Well, now I'm coding under Red Hat Linux 7.0, not MSDOS. What I need to do first, I've realized, is learn how to use gcc. I'm used to being able to just feed an interpreter my code and see the results right away, but this is more complicated, and I never really got anywhere when I originally started learning C (I've just recently come to understand why one would use pointers instead of just passing the actual variable from function to function). I coded some simple C apps for MSDOS a long time ago, but I've given up on trying to code anything useful on Windoze. So, I'm learning Linux, the various apps for Linux, and C/C++ all at once. :D No sweat!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    I never did really big projects in c/linux, but one tip from my experience:

    use emacs as editor. itīs really good. you can compile, run and debug your program in it just like in the "Turbo C" ide on dos. even better: use emacs running on xwindows. then you also have dropdowns and popups to work with :cool:
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    USA
    Posts
    0
    Rep Power
    0
    I removed the exit() part, and it worked....how it was supposed to work....I guess.
  16. #9
  17. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0
    Yea, I dropped the void and the exit() which made it stop whining about the return value. It just seemed very strange to me that it seemed NOT to want to see any return value.
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    USA
    Posts
    0
    Rep Power
    0
    I left the void part, still worked...what is void() anyway?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    "void" is nothing ;)

    you use it if your function returns no value or for parameters that have no specific type.
  22. #12
  23. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0
    Or when you get mad that it keeps returning everything but what you expect ;)
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Oxford, UK
    Posts
    28
    Rep Power
    0
    If you put a newline in your printf statement:

    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("Hello World!\n");
        return 1;
    }
    then the compiler guarantees a flush of the output buffer. Which is generally a good thing too.
  26. #14
  27. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Canada, eh?
    Posts
    1
    Rep Power
    0
    Code:
    #include <stdio.h>
    
    
    void main() {
    
       printf("Hello, world!");
    
       exit();
    
    }
    the problem is... when the program starts, main() is called. when it ends, it returns 1 or 0 (1 means it closed due to an error, and the OS should make a fuss about it, and 0 means everything went swell)

    Just change it to

    Code:
    int main(){
    
    printf("Hello, world!");
    
    exit(0);
    
    }
    "exit(0)" is basically the same as saying "return 0";)
  28. #15
  29. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Originally posted by Lord MJ
    According to the ANSI standard, main() is supposed to return an int,
    Just to confuse matters here, ISO specifies that it is illegal to declare void main() in C++, not C: http://homepages.tesco.net/~J.deBoyn...void-main.html

    However, using void main() (though legal) is a bad practice in C, IMHO :)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo