Thread: Hello, World!

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Council Bluffs, Iowa, United States
    Posts
    5

    Hello, World!

    Hello C Board,

    I'm new to C programming and have a few questions...

    Code:
    # include <stdio.h>
    
    
    int main(int argc, char *argv[])
    {
        printf("Hello, World!\n");
        return 0;
    }
    Take this simple program, what does:

    The "int" in front of the "main" function mean? I've seen some examples without it.

    I know that int argc, char *argv[] has something to do with arguments, but can someone please break it down in plain english?

    How do I add a "help" argument?

    Thanks in advance for all the help!

    ~Josh

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If there is not something ( a return value) before main, then it is by default int.

    main is a function. The MAIN function. Every program you write will have one. Only one.

    The format of a function's prototype is
    Code:
    returnValue functionName(typeOfArgumentOne argumentOne, typeOfArgumentTwo argumentTwo, ...);
    So, as you can see, at line 7 of your code, you have a return 0; line of code. Zero is considered to be what main returns on success, to inform the rest of the world that everything went as they should go, that main terminated successfully!
    On different case, usually we return -1.
    Since these are numbers, the main function returns an int.

    The arguments now of the main function are useful for arguments given by the command line, not by the keyboard while on runtime. As soon as the program starts running, the command line arguments are already there. argc has the number of them and argv is an array of strings, that contains the name of the executable and the command line arguments.

    I suppose you are a beginner, so I think I should stop here. Hope this helps.

    //I don't understand the help argument you say. If you mean to add a third argument in main, then I would say that you don't really need it.

    PS - Welcome to the forum
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Location
    Council Bluffs, Iowa, United States
    Posts
    5
    Quote Originally Posted by std10093 View Post
    So, as you can see, at line 7 of your code, you have a return 0; line of code. Zero is considered to be what main returns on success, to inform the rest of the world that everything went as they should go, that main terminated successfully!
    On different case, usually we return -1.
    Since these are numbers, the main function returns an int.
    So if we return MAIN with a string, would it be char main()?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    As I understand it, "main()" should always return an int (integer), because that's what the calling program (the OS) expects.

    And a return type of char (character) would only return a single character. A string is an array of characters terminated with a null character, so returning a string from a function is a little bit more involved.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Location
    Council Bluffs, Iowa, United States
    Posts
    5
    Quote Originally Posted by Matticus View Post
    As I understand it, "main()" should always return an int (integer), because that's what the calling program (the OS) expects.
    What about the "hello world" examples that omit a return value? They begin "main()" and not "int main()" Is it normal to omit return values, or are they just trying to keep it simple?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by clonemaster View Post
    So if we return MAIN with a string, would it be char main()?
    No, it would be char*. But as Matticus said, we should not return a string.
    If a function returns a character, then the return value should be char.
    If a function returns a string, then the return value should be char*.
    Quote Originally Posted by Matticus View Post
    As I understand it, "main()" should always return an int (integer), because that's what the calling program (the OS) expects.
    [I have heard..]
    that main should return void in some special cases. But I do not really know more on this. Just saying it, because you used the word always, which is too heavy.
    [/I have heard..]
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by std10093 View Post
    If there is not something ( a return value) before main, then it is by default int.
    This is an old rule. Standards-conformant compilers will not necessarily treat a missing return type as int. To be correct you should specify a return type; it is not optional.

    Quote Originally Posted by std10093 View Post
    Zero is considered to be what main returns on success, to inform the rest of the world that everything went as they should go, that main terminated successfully!
    On different case, usually we return -1.
    You can return -1 if you want, but normally it's 1, 2, 3, etc, to signal some kind of error. For example the popular UNIX `grep' tool returns 0 if the pattern was found, and 1 if the pattern was not found. Other return codes indicate some kind of problem like disk I/O error, permissions problems, etc..

    To see the return value, the easiest way is to run the program from a command window and then "echo" it. Examples for window and linux below:

    Windows example
    Code:
    P:\> mycommand.exe
    P:\> echo %errorlevel%
    Linux example
    Code:
    ~# ./mycommand
    ~# echo $?

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by clonemaster View Post
    What about the "hello world" examples that omit a return value? They begin "main()" and not "int main()" Is it normal to omit return values, or are they just trying to keep it simple?
    As I said in post #2, by default it is int. So, when omitted the compiler supposes that there, the programmer means "int".

    If you have seen that in code that targets beginner's, then yes, they do it to keep it simple.
    In other cases, they are just lazy or forgot to added it (and the compiler never said anything, because of the default value, so nobody really spotted it).

    I would suggest you to always place it before main, since some people may be confused by the absence of it (just like you are now).

    Also, since you are new here, take as an advice, that it is really a good thing, to read the whole posts of the other members. They write things, because they have something to say (the default return value I said some posts before was not read by you )
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by c99tutorial View Post
    To see the return value, the easiest way is to run the program from a command window and then "echo" it. Examples for window and linux below:
    Or just use the EXIT_SUCCESS and EXIT_FAILURE macro's
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    that main should return void in some special cases.
    I can't speak on this for programs run on computers, but 'C' code for basic embedded devices have "void main()", simply for the fact that it is the only program being run (it is not called by a larger program), and hence has nowhere to return a value to.

    @OP: Some reading: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by clonemaster View Post
    ...
    I know that int argc, char *argv[] has something to do with arguments, but can someone please break it down in plain english?

    How do I add a "help" argument?

    Thanks in advance for all the help!

    ~Josh
    When you run your program from a command prompt you can include arguments. For example: myprogram.exe -h
    I think this is what you meant by adding a help argument. Similar to how other utilities are used.
    The variables 'argc' and 'argv' are provided to you so you can see if someone included arguments.
    'argc' will show you the number of arguments that were passed + 1. So the "-h" thing will make argc be 2. You should check that number.
    argv[1] will contain the pointer to the string "-h". It's up to you to interpret the meaning of this string and provide the code to spit out a 'help' text if that's what you want.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The original "Hello World" program can be modified slightly to demonstrate argument passing

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    	if (argc == 3) {
    		printf("Hello, %s and %s!\n", argv[1], argv[2]);
    		return 2;
    	}else if (argc == 2) {
    		printf("Hello, %s!\n", argv[1]);
    		return 1;
    	}else{
    		printf("Hello, world!\n");
    		return 0;
    	}
    }
    If you name the program `hello' then type commands like

    hello
    hello bob
    hello bob sue

    The return value tells you how many names your program read (0, 1, or 2).

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c99tutorial
    The return value tells you how many names your program read (0, 1, or 2).
    Note that this is not a requirement, but rather an example. More typically, you would return 0 (or EXIT_SUCCESS) in all three cases since the program would have terminated successfully.
    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

  14. #14
    Registered User
    Join Date
    Jan 2013
    Location
    Council Bluffs, Iowa, United States
    Posts
    5
    Thanks guys and gals! I now know that if a program terminates with an integer, the "main" function should be "int main()."

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "int argc" and "char *argv[]" are generally considered a more advanced thing to learn (especially from a "hello world" level).

    Have you every used command prompt in windows? When you type the program name and then something after it (for example, "ping www.google.com") you are calling a program "ping" with an argument "www.google.com". If you were to look at argc and argv from this program, argc would be 1 (there was one argument in the program call) and to read what that was, you would look at a "character array" located by using argv[1].

    The return value tells the Operating system how your program went - If you don't specify the value (0 in most cases where nothing went wrong), a random number is given back to your operating system. Your operating system might think that it has to correct something, which could end up in tears. Therefore, always make sure you return a number at the end of main.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hello world
    By d4xyjen in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 08:49 AM
  2. Hello World!!!
    By him61 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-06-2006, 12:56 PM
  3. Hello World 180 KB???????
    By Musicdip in forum C++ Programming
    Replies: 17
    Last Post: 06-22-2002, 07:27 PM
  4. New to the World
    By oo0speed0oo in forum Game Programming
    Replies: 12
    Last Post: 04-04-2002, 10:46 AM
  5. Hello World in C#
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 51
    Last Post: 08-24-2001, 02:09 PM