Hello World C Program
In this tutorial, we will write a very simple hello world c program which will print “Hello World!” on the screen. The source code with the basic structure of a c program is also given below:
Hello World C Program Source Code
/* Hello World C Program */ #include<stdio.h> main() { printf("Hello World!"); getch(); return ; }
Output
C Program Structure
Every c program consists of several building blocks known as functions which may include one or more executable statements. The basic structure of c program is given below:
/* Comments Section */
Preprocessor Directive Section
Global Declaration Section
main() Function Section
{
Declaration part
Executable part
}
User Defined Functions
{
Function Statements
}
Comments Section
The comment section usually describes the code purpose in the file, or can be used to show some license or copyright information.
Thus, comments in c are used to make a program much more readable and understandable. These are of two types:
Single Line Comment
A single line comment starts with // and all the data written after these two slashes till the end of the line is ignored by the compiler.
// This is an example of single line comment
Multi Line Comment
Anything written between multiple comments(/*………..*/) are ignored by the compiler.
/* This is an example of multi line comment */
Preprocessor Directive Section
Preprocessor directive section is used to tell the compiler what external code to use by including header files using #include preprocessor directive. Mostly, the header file stdio.h is included in every c program which allows us to receive input display output from the terminal.
A header file offers the following benefits:
- Since, a header file stores all predefined functions and instructions, thus, it may be included in a program, making same declarations and instructions available to all the programs including the header file.
- Any update in the header file will be reflected to all the programs using that header file, thus header files can act as a really big time saver when same functions are to be used in multiple programs.
Global Declaration Section
Here, any variable or function that is to be made accessible by all the functions present in the program is declared.
main() Function
The main() function is the most important part of any c program as execution of all c program starts only from main() function. Thus, if there is no main() function, then there won’t be any execution at all.
It should be noted that if no return type is mentioned for main(), then by default the return type will be int. And, if the program run was successful then it should return 0, else if it return any other value, means that there was a problem.