Signup/Sign In
LAST UPDATED ON: SEPTEMBER 17, 2024

First C Program and its Structure

Let's start with a simple and basic Hello World program in C language.

To create a "Hello World!" program in C language, you have to use the printf function.

#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. If you need help, here is a step-by-step guide - 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 C Compiler to run C programs and Practice. Click on the Run Code button above to open the compiler.

Understanding the Structure of 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 to 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).

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

  • We can include any number of header files in a C program.

  • 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.

2. Header file

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

  • Header files contain definitions of the functions which can be used in any C program

  • To use a header file you must import it into your 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.

  • All header files will have .h extension.

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.

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.

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.

Let's see a code example,

#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.

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

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.

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.

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.

  • Semicolon (;) is called the statement terminator in C programming.

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)

Here are some frequently asked questions, that beginners have when they learn how to write Hello world program in C programming.

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.