Thread: I don't get "hello, world" program!

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    45

    Angry I don't get "hello, world" program!

    Hello everyone, I'm beginning to learn to program in C (by beginning I mean starting today). As of right now I am looking over the "hello, world" program", I have no problem getting it to run but, I don't understand what some of this means! )=

    Code:
     
    #include <stdio.h>
    
    int main(void)
    {
        printf("hello, world\n");
    
        return 0;
    }
    I understand that the first line of code:
    Code:
     #include <stdio.h>
    Is simply telling the compiler to input the file stdio.h into my "hello, world" program, so it will know how to read the printf function.

    The next line though is where I have some problems:
    Code:
    int main(void)
    I know that main() is a function that tells the compiler to start and is used to begin telling the computer what I want it to do. Then there is the "int" I looked up and found that to declare the type of function, which it says is an integer... Well I may be misunderstanding something but since when is "Hello, world" considered an integer?

    Okay, next my propblem is the main(void), when I looked up this I found this to mean that the main function takes no arguments, I would think that would mean the main() function shouldn't be doing anything, so again, maybe I'm just misunderstanding the word? This is what I got from wikipedia:
    "The keyword (void) in between the parentheses indicates that the main function takes no arguments." - http://en.wikipedia.org/wiki/C_(prog...haracteristics

    The very last line also gives me trouble )=
    Code:
       return 0;
    I read on the wikipedia that this tells the main() function to terminate, my question is why is it needed, since the curly bracket at the end of the program should have done the same thing, I decided to check another source "Teach Yourself C" second edition, but it seems to give the same definition as the wikipedia.

    Any help would be greatly appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Function arguments are things passed along to the function when it is called. Since you're not passing anything over to main, you make the argument list void. It returns an integer at the end of the function, because that's what it's supposed to return by definition as according to the C Standard.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The C standards (either C89 or C99) require that main() return an int (this is basically the exit status of the program), even if you choose to ignore it.. You should read this:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Since main() returns an int, that's why the "return 0;" is there. The C89 standard requires that you explicitly specify a return value for main(). In C99, if you don't have an explicit return statement at the closing brace, it automatically returns 0 upon reaching it. However, since at present C89 is the most commonly implemented standard, it's considered good practice to write C code that complies with both standards, so you should include it. Remember that main() is special, so you won't run into these complications with other functions.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Okay let me try to explain this to you...

    You can define a function many many ways, with ints or chars or even strings!

    The function, even if it's an int, can do just about anything within the function so long as the function returns a value.

    So int main() <--- Which is the standard, dont use void main or char main or nothin'.

    Is a function, where the program starts, and it's job is to return an integer. It doesnt matter what happens inside the function, so long as it returns an integer. Usually we have these sorts of functions return a 0, if everything went well and 1 if not. But don't worry about this.

    I'm actually happy to see a post like this, most people don't ask why? they just copy and paste code and try to memorize what's going on instead of trying to understand why.

    So good job!

    And i hope i explained that to you well enough. The function can do whatever it wants (pretty much, any capabilities that the include files allow) so long as it (in the end) returns whatever value it's defined with (so int main() can do anything, so long as in the end, it returns an integer).

    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    45

    Smile

    Thank you all who responded, I think I have it understood well enough now (=
    I appreciate your time in answering and very quickly at that (=

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    just commenting. you seem very interested and motivated to learn C, im sure youll pick it up in no time! good luck!

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Might i just ask this (and i'm in fear that the members of this board which i don't often frequent will bite my head off) why did you choose C as opposed to C++?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    45
    ]The main reason I choose C:
    1. [* is it seems "easy" to learn compared to other languages.
    2. My operating system (Linux) is composed of the C language (I would like a deeper understanding of my system).
    3. From what I have found out when researching C, C has been around for many years and still has survived and it appears likely that it will continue to survive.


    The reason I did not consider C+:

    1. It seems to be used as an enhancement to the C language (I want a understanding of the more basics before I move on to something like C+).
    2. The books that I checked out compared to C did not seem to be written as well compared to some C oriented books.



    I was going to answer sooner, but someone posted that I shouldn't answer, but since the post that I am referring to is not here anymore, I guess I can?

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    C is the "mother" of C++ and C#. You are right that C++ is an enhancement to C. C++ takes the C language and invents new and improved ways to do things, as well and introducing the advent of OOP. ( Object Orientented Programming ).

    I took a part time C course in college when I left school and loved it to be honest. Although i tend to use C++ more than C, there are aspects of C++ I would not of known or gor as quiclly if I did not learn some basic C first. Never be ashamed of learning C. It is a very powerful and portable languge. WIndows is mostly written with it
    Double Helix STL

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Actually, Windows is mostly written in C++ (see the link in my sig, also the Win2000 source leaks)

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I thought it was mixture of both to be honest. But I guess I was wrong
    Double Helix STL

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Infact your right, but C++ does heavily outweigh C (at least in the kernel) If that makes sense...

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    yea I understand the term. Thanks for the info
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM