Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

First C Program and its Structure

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

Here is the program for printing "Hello World" in C language.

#include <stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}


Hello, World

Run Code →

To run the above code on your local machine, you will have to install a C language compiler on your Computer/Laptop. We will learn how to do that in the next tutorial - Compile and Run C Code.

If you do not want to install the C compiler on your computer, don't worry. You can use our Online Compiler to run C programs and Practice. Click on the Run Program button above to open the compiler.

Understanding Structure 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. Don't worry about all this, we will learn about everything one by one and will clear all your confusion.

Let's start with a basic introduction of various code statements that we used in the above Hello World program.

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

As its name suggests, this line of code is responsible for doing pre-processing, before the actual code (logic) is executed.

Pre-processor in C language

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

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.

By Console screen, we mean CMD or command prompt in case of Windows OS and Terminal in case you use Linux/Ubuntu/macOS.

Similarly, we can include any number of header files.

The #include is not the only pre-processor. Whenever you see any piece of code starting with a # symbol, that means it's a pre-processor in the C language. We will learn about pre-processors in detail later.

2. Header file

A Header file is a set or collection of built-in(readymade) functions, which we can directly use in our program.

Header files contain definitions of the functions which can be used in any C program by using pre-processor #include statement along with the name of the header file.

There are some standard header files that come along with default C installation, like stdio.h header file. There are many other such files, we will learn about them later.

With time, you will have a clear picture of what header files are, as of now consider them 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.

Header files in C program

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. The 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, hence the actual logic or the code is always written inside the main() function.

As the name suggests, this is the main(of prime importance or center of attraction) function.

#include <stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}

In the Hello World code example above, there was int written before the main() function, remember? Well, that is the return type of the main() function. we will discuss it in detail later.

The curly braces { } just after the main() function enclose the body of the main() function.

We will learn what functions in C language are in upcoming tutorials.

4. The printf() Function

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

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

5. Return Statement

A return statement is used to return a response to the caller function. It is generally the last statement of any C language function. Do not worry about this too, we will cover this statement when we learn about functions in the C language.

6. Semicolon

It is important to note that every statement in C should end with a semicolon(;). If you miss adding any semicolon, the compiler will give an error.

How to write a C program on my Computer?

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 and .c extension. For example, helloworld.c can be the file name for the Hello World program.

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


Frequently Asked Questions (FAQ)

1. Name different preprocessors in C programming Language?

Different preprocessors in the C language are #include, #if, #define, #ifdef, #undef, etc. and many more. A pre-processor is a statement that is executed before the main code for the C program is executed.

2. What do you understand by the main() function in C language?

The main() function in the C language acts as the entry point of any program or we can say that the execution of the code starts from here. The source code which is inside the main() function gets executed.

3. 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 and for multiline comments, you can use the following: /* comment here */.

4. What is stdio.h in C program?

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 use of this header file is, that it helps to get the input from the user(Keyboard) and return the output result text to the monitor(screen). There are readymade functions in this header file that we can use in our program, for example, printf() and scanf() functions are defined in the stdio.h header file.


Conclusion

In this tutorial, we saw a simple Hello World program in the C language and covered its structure. In the next tutorial, we will set up the C compiler on our local computer/laptop so that you can run this program on your computer too.