To start with the C Programming Language journey, let’s compile and execute a simple C program and understand its syntax line by line.
Below is a C program that prints “Hello World!” to the console:
#include <stdio.h> // Hello World program int main() { printf("Hello World!"); return 0; }
Line1: #include
is the header file library; it includes a “standard input-output” Library. printf()
function is available in stdio.h
Library.
Line2: //
is a single-line comment, and the compiler consistently ignores multi or single-line comments. We add the comments to increase the understanding of the reader. It will not affect program functionality and will also not get displayed on the screen.
Line3: int main()
is the main entry point of every C program. C program starts from the main()
function.
Line4: {
is the opening curly brackets. Every function contains the opening and closing curly brackets {}
after the function name, which means C executes any function code between the curly brackets {}
. Remember, you are writing opening curly brackets {
, and you must have to close it by giving closing brackets }
.
Line5: printf()
function displays the data like the “Hello World” on the screen. The Semicolon ;
ends the C statement.
Line6: return 0;
ends the main()
function by returning the implementation status to the OS. The 0 value returns successful execution, and value 1 returns unsuccessful execution.
In the next lesson, you will learn in detail about printf()
function and other important Output Functions in C.