C Programming Mastery

Chapter 1: Getting started with C Language

Introduction to C, standards, and your first program.

Section 1.1: Hello World

Version Standard Publication Date

Version Standard Publication Date
K&Rn/a1978-02-22
C89ANSI X3.159-19891989-12-14
C90ISO/IEC 9899:19901990-12-20
C95ISO/IEC 9899/AMD1:19951995-03-30
C99ISO/IEC 9899:19991999-12-16
C11ISO/IEC 9899:20112011-12-15

To create a simple C program which prints "Hello, World" on the screen, use a text editor to create a new file (e.g. hello.c — the file extension must be .c) containing the following source code:

hello.c
1234567
#include <stdio.h>

int main(void)
{
    puts("Hello, World");
    return 0;
}

Line-by-line breakdown:

#include <stdio.h>
This line tells the compiler to include the contents of the standard library header file stdio.h in the program. Headers contain function declarations and macros; you must include the header before you use them. This line includes stdio.h so it can call the function puts().

int main(void)
This line starts the definition of a function. It states the name of the function (main), the type and number of arguments it expects (void, meaning none), and the return type (int). Program execution starts in the main() function.

{ ... }
The curly braces indicate where a block of code (the function body) begins and ends.

puts("Hello, World");
This line calls the puts() function to output text to standard output (the screen), followed by a newline. "Hello, World" is a string literal. In C, string literals must be inside double quotes. Every statement needs to be terminated by a semi-colon (;).

return 0;
Since main() returns an int, it needs to return an integer value. Returning 0 indicates that the program exited successfully.

Compiling and running the program

To run the program, the source file must first be compiled into an executable using a C compiler.

Compile using GCC:
gcc hello.c -o hello
This creates a binary file named hello. You can add warning options for better error checking:
gcc -Wall -Wextra -Werror -o hello hello.c

Compile using Clang:
clang -Wall -Wextra -Werror -o hello hello.c

Compile using Microsoft C (cl.exe):
cl hello.c

Executing the program:
Once compiled, execute by typing ./hello in the terminal.

Section 1.2: Original "Hello, World!" in K&R C

The following is the original "Hello, World!" program from the book The C Programming Language by Brian Kernighan and Dennis Ritchie (K&R):

kr_hello.c
12345
#include <stdio.h>

main()
{
    printf("hello, world\n");
}

Because the C language was not standardized in 1978, this program may not compile on modern compilers without specific instructions (like accepting C90 code). This example is now considered poor quality because it lacks an explicit return type and a return statement.

Standard Evolution Comparison:

C90 §5.1.2.2.3: Reaching the } that terminates a function is equivalent to executing a return statement without an expression. If the value of the function call is used by the caller, the behavior is undefined.

C99 §5.1.2.2.3: If the return type of main is compatible with int, reaching the } returns a value of 0. This is a special case introduced in C99 specifically for the main function.

The recommended and most portable form for programs without command-line arguments is:
int main(void) { ... }