Menu Close

“Hello World!” – Your First C Program and Its Structure

Posted in C Programming

A C program mainly consists of the following parts:

  • Preprocessor
  • Header file
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

All these are essential parts of a C language program.

Let’s take a look at a simple code that can output the word “Hello World”:”

1. Pre-processor

The #include is the first statement of any C program. It is known as a pre-processor. The task of a pre-processor is to initialize the environment of the program. As its name suggests, this line of code is responsible for doing pre-processing,  before the actual code (logic) is executed.

#include <stdio.h>, is a pre-processor directive that tells the C compiler to include the stdio.h file before the actual compilation. stdio is the standard I/O library  that defines the printf function used for generating output.

The standard I/O library lets you read input from the keyboard(i.e standard in) and then write the output to the console screen (i.e. standard out) and it is an extremely useful library.

Pre-processor is always placed at the beginning of a file.

2. Header file

A header file is a file containing C declarations and macro definitions (see Macros) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive ‘#include’.

There are some standard header files that come along with default C installation, like stdio.h header file and stdlib.h header file. There are many other such files, we will learn about them later. The stdio stands for standard input and output and .h is the extension of the file indicating that it is a header file.

Related:   What is the Difference Between printf() and scanf() ?

You can also create your own functions, group them in header files, and declare them at the top of your program to use them. To include a file in your program, use preprocessor directives.

To use any of the standard library functions, the appropriate header file must be included. This is done at the beginning of the C source code.

For example, to use the printf() function in a C program, which is used to display anything on the console screen, the line #include <stdio.h> is required, because the header file stdio.h contains the printf() function definition.

All header files will have .h extension.

3. Function

3.1 main Function

The main function is an important part of every C program, as it is where the program begins executing. There can be various forms of the main function, such as:

main()
int main()
void main()
main(void)
void main (void)
int main(void)

An empty pair of parentheses () indicates that the function takes no arguments, returns no value, or has no parameters. You can also make this explicit by placing the keyword void within the parentheses. The keyword void indicates that the function does not return any value.

In the above examples, the keyword int indicates that the function will return an integer value. In this case, the last statement should always return 0.

#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("Hello world!\n");
return 0;
}

The curly braces {} indicate the beginning and end of the program. These curly braces {} must always be placed after the main function. All program code is written within these curly braces {} , including declarative and executable parts.

Related:   while Loop Statement in C Program

3.2 The printf() Function

Printf() is a function that is used to print anything on the console as output. This function is defined in the stdio.h header file, which we must included in our C program.

We will cover how to take input and show output in one of the next tutorials.

4. Variables

Variables are containers for storing data values, like numbers and characters. We will cover this parts in next tutorials.

5. Return Statement

The return statement ends the current function and returns control in the point of invocation. It could also return value in that point.  It is generally the last statement of any C language function.

6. Semicolon

determines the end of a statement. In C, every statement must end with a semicolon.

7. Comment Line

// comment line go here

// — Indicates that the content after this line is just explanation or declaration, and does not need to be executed

/* <em>comment paragraph goes here</em> */

/*

The paragraph does not need to be executed

*/

 

Leave a Reply