First C Program

In this tutorial, you will learn how to write a simple C program and how to compile and run it using C compiler.


I hope that by this time you have installed any C compiler in your system. If not, you can do this with the help of our previous tutorial on how to install C.

1) Now, in the very first step let’s write our first C program as follows.

#include <stdio.h>
int main(){
     printf("Welcome to the world of C");
     return 0;
}

You can write the above program in any text editor and save it with .c extension. I have saved the above program as hello.c.

Note: Every C program source code must be saved with .c extension.

2) Open the command prompt and browse to the location where you have saved your c program. In my case, it is “C:\Users\hp\Desktop\C Tutorial“.

3) Now write gcc along with the C program name as below.

first c program

You can see one exe file created with the name of a.exe. This is the default behavior of the C compiler. If you do not provide any output filename by default it creates an a.exe file.

To customize the name of the output file you can use the following command.

> gcc hello.c -o hello

4) Finally to get the output you can run the binary file just giving the name in the command prompt.

first c program output

Output

Welcome to the world of C

How First C program work?

  • The #include is a preprocessor command which asks the compiler to include the stdio.h (standard input output file) file in the program.
  • The stdio.h is a header file that contains functions such as printf() and scanf() that print the output and takes input from the user respectively.
  • In case you are using printf() function without #include<stdio.h> then the program will not compile.
  • In the next statement main() is the entry point of the program that means every C program starts from this.
  • The printf() is the library function that sends the formatted output to the screen. In our case, it prints “Welcome to the world of C” in the screen.
  • The return 0; statement is the “Exit status” of the program. In simple terms, the program ends with this statement.

Please get connected & share!

Advertisement