In this tutorial, we are going to learn about the structure of a simple C program.
Structure of a C program
The C source code given below prints “Hello! World” to the console.
#include <stdio.h>
int main(){
printf("Hello World");
return 0;
}
The first line of the program #include <stdio.h>
has three parts included in it :
- The # character is called the preprocessor directive.
- include is a command.
- stdio.h is the name of the header file that we are including in the code.
A header file usually consists of some useful inbuilt or predefined functions.
A preprocessor directive is executed before the compilation is done. Preprocessor directives are lines included in a program that begins with the character #,
which makes them look different from a typical source code. They are invoked by the compiler to process some programs before compilation.
The #include <stdio.h>
preprocessor command includes the contents of the header file stdio.h
into our program, which makes the different I/O functions like printf(), scanf(),
etc. which are available for use.
The second line of the program int main()
contains two parts:
- In our program’s context,
int
is the return type. It means that the functionmain()
returns 0. main()
is a function (also called subroutine) which is called by the Operating System.main()
is the starting point of execution in any C program.
printf()
is a function that is used to print the text specified within double-quotes to the standard output device (i.e, console/monitor). The executable statements, which in this case, is a call to printf()
function must be enclosed within an open and close braces { }
.
Output:
Understanding C Program Execution:
The instructions (also called the source code) written in C language are saved in a file with .c
extension. For example, the above C program which prints “Hello! World” to the console can be saved as Hello.c
. This is the source file.
There is a special program called Compiler, which is used to compile the source file. Once compiled, an object file with extension .obj
is created.
There is yet another program called a linker, which combines one or more object files to create a single executable file. The executable file can have any extension. For example, files in the Windows Operating System have .exe as an extension. These files are executed to get the desired output.
For writing the code and compiling it, we use the Integrated Development Environment (simply IDE). An IDE is a software for building applications that combines common developer tools into a single Graphical User Interface (simply GUI). For example, NetBeans, Eclipse, IntelliJ, etc…..
Dev-C++ is a free full-featured integrated development environment distributed under the GNU General Public License for programming in C and C++. It uses the MinGW compiler system to create Windows as well as Console based C/C++ applications.
Please, download the source code from here:
Hello World programReferences:
Happy Learning 🙂