Hello World Program in C

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Hello world program in C

CategoriesC programming4 mins readSeptember 16, 2017


Before reading this post, I hope you are equipped with required software’s to run a C program. If
not please first set up your C lab before writing, compiling and running a C program.
After you have installed and configured all essential software’s you can continue to first C
program.
Basic structure of a C program
Since the early days of programming, it has been a tradition to write first program as “Hello
Word” program. I will continue the legacy.
Open your favorite text editor and write below program.
/* My first C program to print Hello, World! */

#include <stdio.h>

int main()
{
printf("Hello, World!");

return 0;
}
Copy
Note: Do not make typing mistakes while writing the above program. Otherwise it may result in
various errors.
Save the above program with name helloworld.c
Before I get down in details about the code. Let us run the program and test.
Trending
Classification of programming languages
gcc helloworld.c -o helloworld
helloworld.exe
Hello, World!
Copy
The code on execution produces Hello, World! on screen.
Let us see the various elements used in the code and meaning they convey.

Understanding basic structure of C program


 /* My first C program to print Hello, World! */
Copy
The first line of the program is a comment. Comments are non-executable code used to add
inline documentation about the code or program.
Comments in C begin with /* and ends with */. Anything between are not executed by the
compiler.
 #include <stdio.h>
Copy
Almost all C program that I will write in this C tutorial series will begin with the
statement #include <stdio.h>.
#include is a pre-processor directive used to do some processing before compilation process.
The include pre-processor directive inserts all contents of stdio.h file at the beginning of our
code.
Why it is used here? stdio.h file contains various input output functions. I have
used printf() function to print a text which exists in stdio.h header file. Hence, I must tell the C
compiler explicitly to link printf() function with our program.
If it seems confusing, leave it for now just remember to add this line at the top of every C
program. Pre-processor directives are explained in depth in later sections.
 int main()
Copy
A program is a collection of functions and a function is a collection of tasks. To run any
program, we must know the starting function of a program. You may think it as an entry gate to
the program.
int main() is a special function that defines starting point of the program. A function is followed
by a pair of opening and closing curly brace { }. It defines body of the function. We write logic
of our program inside body of main i.e. inside
int main()
{
//Here goes your logic
}
Copy
Read more – Various ways to write main function and standard main function declaration
 printf("Hello world!");
Copy
printf() is a C function used to write text on screen. printf() function takes an argument that
specifies what will be printed on screen.
Don’t get confused with function, arguments, parameters and printf(). All are covered in details
in later sections.
 return 0;
Copy
Return statement specifies that a function has finished all its tasks and it will terminate now.
Here return statement is followed by 0. You are free to use any integer instead of 0. Apart from
that, return 0 has special meaning to main function. It specifies that the program has executed
successfully without any error.
Therefore, I have used 0 instead of any other integer value.
So far, we learned how a C program looks and basic structure of a C program. We created,
compiled and executed our first C program. Next, we will learn the compilation process and
fundamental of C.

You might also like