First C Program and its Structure

The first program that you write in any programming language is the "Hello World" program. And in this tutorial we will learn, how to write the basic Hello World program in C language.

We will learn to write the first C program and then will understand its structure. First of all, let's have a look at how to write the Hello World(simple and most basic) program in C language. Let's get started.

#include <stdio.h>
int main()
{
 	printf("Hello, World");  //single line comment
 	return 0;
 	/*
    	multi
    	line
    	comments
	/*
}


Hello, World

NOTE: If you have not installed the C compiler in you local machine, first do that to be able to run this program - Setup C Compiler

To run the above program, you will have to create a New file, copy-paste the code in that file and save it with some name and .c extension or you can use Turbo C to run it.

After successfully running the example, you will see the above output to the console. Now, let's understand it step by step.

Essential Parts of the C Program

Given below are some of the different parts of a C Program:

All these are essential parts of a C language program.

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, i.e to link the program with the header files required in the program.

So, when we say #include<stdio.h>, it is to inform the compiler to include the stdio.h header file, which is standard I/O library, to the program before executing it.

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

Header file

A Header file is a collection of built-in(readymade) functions, which we can directly use in our program. Header files contain definitions of the functions which can be incorporated into any C program by using pre-processor #include statement with the header file. Standard header files are provided with each compiler and cover a range of areas like string handling, mathematical functions, data conversion, printing, and reading of variables.

With time, you will have a clear picture of what header files are, for now consider it as a readymade collection of functions that comes packaged with the C language and you can use them without worrying about how they work, all you have to do is include the header file in your program.

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

For example, to use the printf() function in a program, which is used to display anything on the screen, the line #include <stdio.h> is required because the header file stdio.h contains the printf() function. All header files will have an extension .h

main() Function

The main() function is a function that must be there in every C program. Everything inside this function in a C program will be executed. In the above example, int written before the main() function is the return type of main() function. We will discuss it in detail later. The curly braces { } just after the main() function encloses the body of the main() function.

We will learn what functions are in upcoming tutorial.

Comments

We can add comments in our program to describe what we are doing in the program. These comments are ignored by the compiler and are not executed.

To add a single line comment, start it by adding two forward slashes // followed by the comment.

To add a multiline comment, enclose text between /* ... */ these.

Return Statement

A return statement is used to return a response to the caller function. It is the last statement of any C function.

Return statements will make more sense when we will learn about functions in C language.

All the C programs can be written and edited in normal text editors like Notepad or Notepad++ and must be saved with a file name with extension as .c

If you do not add the extension .c then the compiler will not recognize it as a C language program file.

It is important to note that every statement in C should end with a semicolon(;).

Here is a video which covers all that we have covered in this tutorial:

Frequently Asked Questions (FAQ)

Here are some frequently asked questions related to this tutorial.

Name different preprocessors in C programming Language.

Different preprocessors in C are #include, #if, #define, #ifdef, #undef, etc.

What do you understand by the main() function?

The main() function in C mainly acts as the entry point of any program. The execution of any program in C starts from this point only.

How comments can be added to a C program?

In order to add the single line comment, you can use slash // followed by the comment text and multiline comments can be added like /* comment here */.

For which purpose stdio.h header file is used?

The stdio stands for standard input and output and .h is the extension of the file indicating that it is a header file. The main importance of this header file is, it helps to get the input from the user(Keyboard) and return the output result text to the monitor(screen), via the printf() and scanf() functions.

Every header file is a collection of functions which can be directly used in the program, just like stdio.h header file.

Conclusion

This tutorial was all about how to write the most simple and basic program in C which is the Hello World program.

And we understood various parts of the C program.