Hello World - Simple C Program


Simple C Program - Hello World

#include <stdio.h>
 
int main(void)
{
    printf("hello, world\n");
 
    return 0;
}

Analysis of the above program:

#include <stdio.h>
This first line of the program is a preprocessing directive, #include. This causes the preprocessor ? the first tool to examine source code when it is compiled ? to substitute for that line the entire text of the file to which it refers. In this case, the header stdio.h, which contains declarations for standard input and output functions such as printf, will replace that line. The angle brackets surrounding stdio.h indicate that stdio.h can be found using an implementation-defined search strategy. Double quotes may also be used for headers, thus allowing the implementation to supply (up to) two strategies. Typically, angle brackets are reserved for headers supplied by the implementation, and double quotes for local or installation-specific headers.

int main(void)
This next line indicates that a function named main is being defined. The main function serves a special purpose in C programs: When the program is executed, main is the function called by the run-time environment?otherwise it acts like any other function in the program. The type specifier int indicates that the return value, the value of evaluating the main function that is returned to its invoker (in this case the run-time environment), is an integer. The keyword (void) in between the parentheses indicates that the main function takes no arguments. See also void.[5]

{
This opening curly brace indicates the beginning of the definition of the main function.

    printf("hello, world\n");
This line calls (executes the code for) a function named printf, which is declared in the included header stdio.h and supplied from a system library. In this call, the printf function is passed (provided with) a single argument, the address of the first character in the string literal "hello, world\n". The string literal is an unnamed array with elements of type char, set up automatically by the compiler with a final 0-valued character to mark the end of the array (printf needs to know this). The \n is an escape sequence that C translates to the newline character, which on output signifies the beginning of the next line. The return value of the printf function is of type int, but no use was made of it so it will be quietly discarded. (A more careful program might test this value to determine whether the operation succeeded.)

    return 0;
This line terminates the execution of the main function and causes it to return the integral value 0, which is interpreted by the run-time system as an exit code indicating successful execution.

}
This closing curly brace indicates the end of the code for the main function.

If the above code were compiled and executed, it would do the following:

Print the string "hello, world" onto the standard output device (typically but not always a terminal),
Move the current position indicator to the beginning of the next line, then
Return a "successful" exit status to the calling process (such as a command shell or script).