To write your first C program, please open the Notepad application(which comes preinstalled in Windows Operating System) and
type in your first C program explained in this article.
Although you may also use Integrated Development Environment(IDE) application like Eclipse, NetBeans to create your first C program but for a beginner in C, we would recommend using Notepad because you learn so much when you work to find your own mistakes to fix them
rather than an IDE finding them for you.
Important points about a C program
Every C program starts with including header files containing library of functions to be used in the program.
Every C program starts with a main() function.
Every statement in C program must end with a semicolon ;(unless it is a comment)
A main function defines a single or multiple set of statements to execute within opening and closing braces {}.
How to name a C program?
The name of a C program may start with any alphabet or even a digit but it must ends with a .c extension, where c stands for C programming language.
Creating our first C program
We are creating a simple C program which is saved in a file named First.c.
First.c
#include<stdio.h>
int main()
{
printf("Hello World!");
return 0; /*returning an int value before the main() function ends */
}
Understanding your first C program
Let's see what we actually did in our first C program by dividing it into a few segments -
#include<stdio.h>
Every C program starts by including header files containing library of functions to be used in the program.
In this program we are going to include header file stdio.h, which contains definition of standard input/output library functions. A header file always ends with a .h extension.
int main()
A C program begins with a call to the main() function, because it is an entry point to our program.
This main() function is always defined with a int return type,
which means that it is going to return an int value at the end of its execution.
printf("Hello World!");
This statement simply outputs the string Hello from Java in the output on the command prompt, where printf() is a predefined function defined in the header file stdio.h,
hence we have included stdio.h header file at the top of our program.
Note : The printf() function prints the string of characters(within double quotes) at the command prompt in the output of the program.
return 0; /*returning an int value before the main() function ends */
This statement returns an int value zero(0), at the end of execution of main() function and ends with a semicolon(;)
Next, we have added a comment at the end of this return statement.
A single line comment in C defined within the opening element /* and ends with the closing element
*/, on a single line.
Advertisement
How to compile a C program?
In order to compile our program, we need to call the C compiler. As we have installed and configured MinGW GCC C Compiler,
hence we are going to call this compiler
with a command gcc with the name of our file.
We have saved our C program in a file First.c in the D: Drive location. Hence, in order to compile our First.c file from command prompt, first we need to reach the
location where this C file is saved and type in the command -
D:\>gcc First.c -o First.exe
This compiles the file First.c and creates its .exe file with the name - First.exe.
This .exe file is a compiled form of our C program, which is going to be required and read in order to execute this C program.
How to run a C program?
D:\>First.exe
Hello World!
In order to run our program, we need to execute the .exe file with the name of our C program.
And, at the end of this command, we press Enter to get the output of our program.