Sure! This is what a Hello World program looks like in C:
code
// This is the header file for input output functions.
// It is necessary to be able to print the "Hello World!".
#include <stdio.h>
// This is the main function.
// It is where the execution of the program begins.
// int main() is the one function all C programs must define.
int main()
{
// This prints hello world to the screen or console.
printf("Hello World!");
// Since main() returns in, we return 0.
// The 0 tells the operating system the program had no error.
// This is not strictly necessary,
// as zero will be returned if main ends without return,
// however returning zero here can prevent compiler warnings.
return 0;
}
A C program typically starts with any #include compiler directives that are necessary for its proper compiling and execution. In this case, we invoke the standard input/output library by using #include <stdio.h>. The # tells the compiler to look for a compiler directive. The include is the reserved name of the compiler directive to load a library. And stdio.h is the name of the standard input/output library header file necessary to load it. It is placed inside <> so the compiler knows where the filename begins and ends, this is only used in compiler directives. The standard input/output library is one of several standard libraries that are part of every implementation of Standard C and will always be available. It is recommended to only load libraries you need, as they make your program larger. However, we cannot use the printf function without it, so we loaded stdio it here.
As you can see, a C program must have an int main() function. This is where your C program begins executing. It defines a function, where main is the name of the function, and int is the return type of the function (in this case, an integer, as int means integer type). This will be important at the end of a program. If a function were to have parameters, they would be defined inside the (), however for main they are optional (if implemented, they would take parameters from the operating system, but this is unnecessary for a Hello World program).
After defining the name, parameters, and return type of main (or any function), a { comes next. Everything between that and the } is the body of the function, and a C program's execution will begin at the { of its main. Additionally, the presence of the {} block differentiates the defining of a function from the calling of the function in C.
Next we have printf("Hello World!");, which is an example of one way to call a function instead of define it (as printf is defined in the stdio library we loaded earlier). Notice it has no {} block, thus it is a call. We call this function to print to the screen/console. printf is the name of the function. And like main any parameters to pass to the function go inside the (). In this case, we want the function to print something, specifically Hello World! so we pass that to the function. However, so as to not confuse the compiler with text that could be a function or variable, we enclose the text in "" so it looks like "Hello World!". This is called a string. A string is a type, just like int. The printf function accepts the string type (which you can see in the stdio.h header file if you were to look, or in C reference manual), so we can pass our "Hello World!" string to the function. Altogether, it looks like printf("Hello World!");. We end the line with the ; to tell the compiler that line has ended, and the new one begins. This is because C ignores most whitespace, such as spaces, tabs, and carriage-returns/newlines, so we have to tell it we are ending the line with ;. This also means we could put more than one command or function on a line, if we wanted to, as long as they were separated by ; (although this is usually considered bad style). Additionally, it means if we need to split a function call or expression across multiple lines because it is too big to fit in our editor, we can do that. Note also, the #include at the top did not have a ;, this is because compiler directives are processed separately and are not functions or expressions and do not need a ; to tell the compiler where the end of the line is.
Then, we end execution of the program by calling return 0;. This looks and works just like a function call, but return is a reserved word that tells the compiler this function should stop and execution should return to where it came from (in this case, back to the operating system that started the program, since we are returning from main, but for other functions it will be back to the caller). return also returns a value to the caller, in this case we have specified 0, to indicate to the operating system no error occurred (other values would indicate various error codes if returned from main, but for your own functions you can define things however you like). The return value must match the return type of the function, but since 0 is of type int, just as specified when we defined int main(), we have done this correctly (otherwise it would be a compiler error). Just like if it were a function call, we end the return 0; line with the ; to tell the compiler the line has ended (even though execution will never reach here, the compiler will still give an error if we do not tell it where lines end). Note, there are two places where return is optional: when a function's return type is of type void, or in main (because main is special as the program beginning and end, and will assume 0 for no error if it ends without a return). Thus, we do not need return 0; here, but we put it just to be safe and avoid compiler warnings on some systems (a warning is like an error, but will not stop the compiling of a program, it is only information to the programmer about suspicious code that may be common mistakes and concerns, but that do not actually prevent a program from being read properly by the compiler).
Lastly, the program ends with the } that matches main's { and thus marks the end of main's function definition block. Execution would end here, if we did not have a return in the block. The C compiler will give errors if any {, }, (, or ) does not have its matching partner.
And that is how to write a Hello World program in C.