2.1 - Hello world in C

In programming, “Hello, World!” is a classic introductory program used to demonstrate basic syntax.
Program Code: Hello World
Here is the code for the “Hello, World!” program in C:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
Now, let’s break this code down step by step.
1. #include <stdio.h>
This line is called a preprocessor directive. In C, preprocessor directives are lines that begin with a #
symbol and are processed before the actual compilation of the code begins. The directive #include <stdio.h>
tells the compiler to include the contents of the standard input-output header file stdio.h
(Standard Input Output Header).
Why stdio.h
?
stdio.h
provides the functionprintf
, which is used to print output to the standard output (usually the console or terminal).- This header file contains declarations of functions related to input and output operations like
printf()
,scanf()
, and others.
Without it, the compiler would not recognize the printf()
function.
stdio.h
is provided by operating system and the implementation may vary depending on the OS
2. int main (void)
The main()
function is the entry point of every C program. When a C program is executed, the system automatically calls the main()
function to begin the program’s execution.
Breakdown of the main()
function:
int
: This indicates that the return type of themain
function is an integer (int
). When the program ends, it returns an integer value to the operating system (typically0
for success or non-zero for failure). This is the standard form since C89 and is expect behavior in any Unix-Like operating system.
Using
void main()
is considered non-standard and should be avoided unless targeting very specific platforms or compilers that explicitly allow it.
-
main
: The name of the function. Every C program must have onemain()
function. -
void
: The parentheses aftermain
contain the function parameters.void
means thatmain()
does not take any parameters in this version. In modern C (C89 onwards, including C17), it’s valid to usevoid
to explicitly specify thatmain
takes no arguments.
If arguments are expected, you must initialize your main function as int main(int argc, char *argv[])
, where argc
is the argument count and *argv[]
an array of strings representing the arguments.
3. printf("Hello, World!\n");
This line is responsible for printing the message “Hello, World!” to the screen.
Breakdown of printf()
:
-
printf
: A standard library function declared instdio.h
. It is used to print formatted output to the console. -
"Hello, World!\n"
: This is a string literal. A string literal in C is enclosed in double quotes (""
). The contents of the string are printed exactly as they appear. -
\n
: This is an escape sequence. It represents a newline character, meaning the cursor will move to the next line after printing the string. Without the\n
, the cursor would remain on the same line after printing.Escape sequences are used to represent special characters that cannot be typed directly. Some other common escape sequences include:
\t
: Tab character.\\
: Backslash.\"
: Double quote.
4. return 0;
The return
statement in the main
function is used to return an integer value to the operating system. The value 0
typically indicates that the program has executed successfully.
0
: Conventionally, a return value of0
signals successful completion, while a non-zero return value (like1
,-1
, etc.) indicates some kind of error.
the return 0;
statement is optional inside the main()
function. If omitted, the compiler assumes an implicit return 0;
at the end of the main()
function. This is a feature carried over from C99, where it became permissible to omit this line.
Compilation and Execution
To compile and run this program, follow these steps:
-
Save the program in a file with a
.c
extension, for example,hello_world.c
. -
Compile the program using a C compiler such as GCC (GNU Compiler Collection) or Clang, Clang usually supports same arguments as glibc:
gcc hello_world.c -o hello_world
gcc
: The program binary namehello_world.c
: The source file to be compiled.-o hello_world
: This specifies the output filename ashello_world
. If omitted, the default name for the executable isa.out
on Unix-like systems ora.exe
on Windows.
- Run the program by executing the compiled output:
./hello_world
This command will produce the output:
Hello, World!
Summary
Understanding the “Hello, World!” program provides insight into how C programs are structured.