How to Compile C Program in Command Prompt?

Last updated on Feb 29,2024 608.3K Views

How to Compile C Program in Command Prompt?

We usually use a compiler with a graphical user interface, to compile our C program. This can also be done by using cmd. The command prompt has a set of steps we need to perform in order to execute our program without using a GUI compiler. In this article we would be understanding how to compile C program in command prompt.

So let us get with this article on how to compile C progam in command prompt,

How to Compile C Program in Command Prompt?

STEP 1: Check for Compiler

Run the command ‘gcc -v’ to check if you have a compiler installed. If not you need to download a gcc compiler and install it. You can search for cmd in your windows system to open the command prompt.

Output - How to Compile C Program In CMD - Edureka

STEP 2: Create the Program

Create a c program and store it in your system. I have written a program to find the Armstrong Number and store it in the directory user. We would be using following code.


#include <stdio.h>
int main()
{
int num, original, rem, sum = 0;
printf("Enter a three digit Number: ");
scanf("%d", &num);
original = num;
while (original != 0)
{
rem = original%10;
sum =sum + rem*rem*rem;
original =original/ 10;
}
if(sum == num)
printf("%d is an Armstrong number.",num);
else
printf("%d is not an Armstrong number.",num);
return 0;
}

STEP 3: Change the Directory

Change the working directory to where you have your C program. You can do that by using the command ‘cd’, which changes the directory. We need to pass the name of the directory in which the program is stored.

Example:  >cd Desktop

Our program is already in the user directory so, we don’t need to change it.

STEP 4: Compile the program

The next step is to compile the program. To do this we need to use the command gcc followed by the name of the program we are going to execute. In our case, we will use Armstrong.c.

Output - How to Compile C Program In CMD - Edureka

After this, an executable file will be created in the directory that your c file exists in. Eg: Armstrong.exe

STEP 5: Run the Program

In the next step, we can run the program. This is done by simply giving the name of the executable file without any extension. On giving this we will get an output. Here, our Armstrong code is executed and we got output for this code.

Output - How to Compile C Program In CMD - Edureka

But What Happens Inside the Compilation Process?

The term “compile” refers to the procedure by which C language source code is translated into machine code. C is a middle-level programming language, so we need a compiler to turn it into machine code before we can run it.

During the compilation process, the C program goes through the following steps:

compilation process

With this we come to the end of this blog on ‘How To Compile C program in Command Prompt’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  this blog and we will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.