Signup/Sign In

Compile and Run C Program

To compile and run a C language program, you need a C compiler. A compiler is software that is used to compile and execute programs. To set up a C language compiler on your Computer/laptop, there are two ways:

  1. Download a full-fledged IDE like Turbo C++ or Microsoft Visual C++ or DevC++, which comes along with a C language compiler.

  2. Or, you can use any text editor to edit the program files and download the C compiler separately and then run the C program using the command line.

If you haven't already installed an IDE for the C language - Follow this step-by-step guide to Install Turbo C++ for C Language

Using an IDE - Turbo C

We will recommend you to use Turbo C or Turbo C++ IDE, which is the oldest IDE for C programming. It is freely available over the Internet and is good for a beginner.

Step 1: Open turbo C IDE(Integrated Development Environment), click on File, and then click on New

first c program with turbo C

 

Step 2: Write a Hello World program that we created in the previous article - C Hello World program.

Write a C program with turbo C

 

Step 3: Click on Compile menu and then on Compile option, or press the keys and press Alt + F9 to compile the code.

Compiling a C program with turbo C

 

Step 4: Click on Run or press Ctrl + F9 to run the code. Yes, C programs are first compiled to generate the object code and then that object code is Run.

Running a C program with turbo C

 

Step 5: Output is here.

Output of C program with turbo C

 

Run C Program Without using any IDE

  • If you do not wish to set up an IDE and prefer the old-school way, then download the C compiler which is called gcc from the GCC website https://gcc.gnu.org/install/

  • Once you have downloaded and installed the gcc compiler, all you have to do is, open any text editor, copy and paste the C program code for C Hello World Program, and save it with the name the helloworld.c like any other file you save with a name.

  • Now, Open the Command prompt or Terminal(if you use Ubuntu or Mac OS), and go to the directory where you have saved the helloworld.c program file.

  • Type the command gcc hello.c to compile the code. This will compile the code, and if there are no errors then it will produce an output file with name a.out(default name)

  • Now, to run the program, type in ./a.out and you will see Hello, World displayed on your screen.

$ gcc hello.c
$ ./a.out

Hello, World

Difference between Compile and Run in C?

You must be thinking about why it is a 2 step process, first, we compile the code and then we run the code. We did the same thing with Turbo C and the same is with the command line or Terminal too.

  • Well, the compilation is the process where the compiler checks whether the program is correct syntax-wise and whether there are no errors in the syntax, and if the code is fine converts the C language source code into machine-understandable object code.

  • When we run a compiled program, it is just the already compiled code that is run.

  • This difference is clear when we run a C program using the command line.

  • When you compile the code, a .out file is generated, which is then Run to execute the program.

  • Once a .out file is generated, and then you make any changes to your program in the source code file, you will have to again compile the code, otherwise, the .out file will have the old source code and will keep running the old program itself.

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. What do you understand by the process of compilation?

It is a process where the compiler checks whether the program is syntactically correct or not. If there is any mistake found in the syntax then it will throw the errors. The error thrown at the time of compilation is called a Compile-time error.

2. Name the keys used in Turbo C to compile the code.

ALT + F9 is used in turbo C in order to compile the code.

3. How is a C program run?

During the compilation and execution of the C program, the compiler generates output files with the same name as that of the C program file but with different extensions.

The .c extension file is called the source file that keeps the code of the program. Now, when we compile the file, then the C compiler looks for errors.

4. Where can I write and run the C program?

You can use an IDE to write and run the C program by following these steps:

  • Step 1: Open turbo C IDE(Integrated Development Environment), click on File, and then click on New.

  • Step 2: Write the C program code.

  • Step 3: Click on Compile or press Alt + F9 to compile the code.

  • Step 4: Click on Run or press Ctrl + F9 to run the code.

  • Step 5: And turbo C will open the console to show you the output of the program.