How to Compile and Run a C Program in Command Prompt?

Last updated on Jul 23,2024 646.6K Views

How to Compile and Run a 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 will understand how to compile and Run a C program in the command prompt.

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

How to Compile and Run 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

 

How to Run C program in Terminal?

If running a C program in a terminal is what you want to do, Here’s what you should do:

  • Write Your C Program: You need to prepare a file containing your C code, hello.c for example.
  • Open Terminal: Launch the terminal program you are using (Terminal as an example on macOS or Linux and Command Prompt and Powershell on Windows or Cygwin or WSL if you are using Windows).
  • Go to the Program Folder: To do this, type cd followed by the path leading to the folder containing your C file i.e. cd path/to/your/folder.
  • You can convert the C Program to a Runnable File: Transform your code so that it becomes executable by using programs such as gcc. To change hello.c into a program called hello, type gcc -o hello hello.c This means that one can also replace it with clang as follows: clang -o hello hello.c.
  • Start Running It: Once it is in a state whereby it can be executed, you may commence running this system. If using Linux or macOS, type ./hello to execute doing so.

To compile and run your C program from the terminal without any problems, simply type hello.exe on a regular command prompt in a Windows environment or in a unix-like one, use the command ./hello.exe.

Here you find lots of online compilers and integrated development environments; these are the best.

1. Replit

Go to replit.com
Start a new Repl and choose the language, C.
Write your code and just click the Run button.

2. OnlineGDB

Go to onlinegdb.com
Write your C code in the editor.
Click Run – to compile and execute the code.

3. JDoodle

Go to jdoodle.com
Choose language C, input your code and click the Execute button.

4. Compiler Explorer

Go to godbolt.com.
Select C and write your code below. This site primarily displays assembly output, but you are able to run code –
go through the exercise

5. TutorialsPoint

Open your browser to tutorialspoint.com
Write your C code below and use the “Compile & Execute” button
Practice the Steps
Open your browser to the site you selected
Set your language to programming C
Write below or copy and past your C code here
Ensure that the compile and run button is pressed to compile and run your program

These sites allow you to practice C code without having to make a whole local configuration of your own.

To execute a program in Command Prompt (CMD) of Windows:

1. Open cmd. Press Windows + R, type in cmd, and hit Enter.
2. Open a Program Directory. You can use the following cd command to navigate to the folder where the program meant to run is. For example:

cd C:pathtoyourprogram
(executable) or programmable scripts then you can just type name of your script and press Enter

program.exe
4. Script or Batch File
execute here you may have a script (eg. .bat or .py file) that also needs invoked this way

execute a batch file here:
script.bat
execute a python script file:

python script.py
Example
Goto directory:

cd C:UsersYourNameDocuments
Now execute the program:

myprogram.exe
More Help
To list contents of a directory use dir
To launch a program with administrator privilege, right-click the CMD icon and select Run as administrator
And that’s all there is to it! You’ve just executed a program via the Command Prompt.

To execute a program in Command Prompt (CMD) of Windows:

1. Open cmd. Press Windows + R, type in cmd, and hit Enter.
2. Open a Program Directory. You can use the following cd command to navigate to the folder where the program meant to run is. For example:

cd C:pathtoyourprogram
(executable) or programmable scripts then you can just type name of your script and press Enter

program.exe
4. Script or Batch File
execute here you may have a script (eg. .bat or .py file) that also needs invoked this way

execute a batch file here:
script.bat
execute a python script file:

python script.py
Example
Goto directory:

cd C:UsersYourNameDocuments
Now execute the program:

myprogram.exe
More Help
To list contents of a directory use dir
To launch a program with administrator privilege, right-click the CMD icon and select Run as administrator
And that’s all there is to it! You’ve just executed a program via the Command Prompt!.

1. Write Your C Program

Create a text file with a text editor.
For example with nano:

nano myprogram.c

2. Compile the C Program

Compile your program using gcc:

gcc myprogram.c -o my program

3. Run the Executable

Run your compiled program using:

./myprogram

Example Steps
Create a text file:

#include <stdio.h>

int main() {
printf(“Hello, World!
“);
return 0;
}

Save this file as hello.c.

Compile:
gcc hello.c -o hello

Run.

Here is how you run your C program inside an Ubuntu Terminal shell.

Compile: gcc filename.c -o outputname
Run: ./outputname.

1. Write Your C++ Program

Create a file with a .cpp extension of you favorite
text editor, for example:

nano myprogram.cpp

2 Compile the C++ Program

Then compile the C++ source file some file.cpp using
g++ GNU C++ Compiler:

g++ myprogram.cpp -o myprogram

3 Run the Executable

Execute your compile program with:

./myprogram

Detailed Example
Let’s create a simple C++ source file:

#include <iostream>

int main() {
std::cout << “Hello, World!” << std::endl;
return 0;
}
Save this
Edit: Create new file with .cpp as extension
Compile: g++ filename.cpp -o outputname
Run: ./outputname

Doesn’t it look kind of easy to edit, compile, and run C++ code in Linux with just a little help from these steps?

FAQs

1.How do I compile the c program in terminal?

you can use the gcc command followed by the name of your source file. For instance, let’s consider a file named myprogram.c which needs to be compiled using gcc command as shown below: gcc myprogram.c -o myprogram. In plain terms, gcc is an acronym for GNU Compiler Collection whose work encompasses translating C code into assembly and finally binary code that computer hardware can execute properly under Unix-like operating systems. If these terms seem strange then consider this; “myprogram.c” stands for your source file or rather what contains the instructions/syntaxes together with “-o myprogram” denoting the output file name where myprogram will be an ultimate “executable” binary file.

2. How do I execute a C code in Linux online?

Repl.it, JDoodle and others are the examples of online platforms where someone may access a Linux runtime environment in order to compile and run C programs.

3. What happens when my C program takes advantage of libraries using <math.h>?

If you utilize standard libraries as <math.h> your C program will not require any special flags for compilation.If you include it right at the beginning through(#include <math.h>). Compiling is typically the same:gcc myprogram.c -o myprogram.

4. Is it possible to compile and run a C program without saving it as a file?

No, you have to save the C program as a .c file first so that you may compile and run it. The name of the file must be specified so that the source code for the C programming language can be read and processed.

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.