How to Run C-Program in Command Prompt

How to Run C-Program in Command Prompt

Image for posthttps://www.youtube.com/watch?v=lOjZwK-6YbI&t=6s

In this article I want to show you all how to run a C-Program in command prompt/ line on a Windows operating system! Be sure to install C-Programming compiler first (gcc). If this article is helpful to ya please leave some claps ! I would really appreciate it!

Step0: Install C-Program Compiler (gcc)

You will need a C compiler to do this already installed, I use GCC. https://gcc.gnu.org/. If you are on a Windows computer, you can run the command gcc -v to check if it?s already installed. If gcc is installed then it should display a host of information starting with the sentence ?Using built-in specs?.

gcc -v

Step1: Create Your C-Program

Create your C-Program, I have created a simple program that prints ?Hello World!? to the screen, and saved it as helloWorld.c . See the code below:

# include <stdio.h> int main(){ printf(?Hello World!?); return 0;}

Step2: Open Command Prompt/Line

Open the command prompt by clicking start button ? All Apps ? Windows System folder ? Click Command Prompt. You can see the exact steps here.

Image for post

Step3: Go to the Source Code Directory

Change our directory to where you have your C program (helloWorld.c). You can do that by using the command ?cd?, I saved my helloWorld.c program on my desktop. See below example:

cd Desktop

Step4: Compile the Source Code

Run the command ?gcc?(the C-compiler ) followed by the full name of your program (helloWorld.c) in the command prompt. This will compile your source code and create an executable file on your desktop. See below example: gcc <name_of_file>

gcc helloWorld.c

NOTE: The executable file named itself probably something like ?a.exe?. Which is fine because we never told the compiler what to name the executable file.

Step4.1: Compile the Source Code

Let?s name our executable file instead of letting the compiler do it for us. We can name the executable file by running this command in command prompt

gcc -o <name_of_executable> <name_of_source_code>

For example, I will name the executable file ?hello? and the source code is ?helloWorld.c?:

gcc -o hello helloWorld.c

Step5: Run Your Program !

In this step we will run our program (the executable file) in command prompt, this can be achieved by going to the directory of the executable file in command prompt (my executable is in the same directory as my source file), then type the name of the executable file without the extension (my executable file name was hello.exe) so I typed the following:

hello

Notice: Your program should now have been executed, my program printed ?Hello World!? to the command prompt !

If you want to learn more about the C Programming Language, you have to read the book ?C Programming Language?. This book goes into topics like data structures, arrays, statement blocks, recursion, loops, arithmetic operations, type conversions, bitwise operators, and pointers. The list goes on.

Image for postC Programming Language

Thanks for reading this article I hope its helpful to you all ! Keep up the learning, and if you would like more computer science, programming and algorithm analysis videos please visit and subscribe to my YouTube channels (randerson112358 & compsci112358 )

Check Out the following for content / videos on Computer Science, Algorithm Analysis, Programming and Logic:

YouTube Channel:randerson112358: https://www.youtube.com/channel/UCaV_0qp2NZd319K4_K8Z5SQ

compsci112358:https://www.youtube.com/channel/UCbmb5IoBtHZTpYZCDBOC1CA

Website:http://everythingcomputerscience.com/

Video Tutorials on Recurrence Relation:https://www.udemy.com/recurrence-relation-made-easy/

Video Tutorial on Algorithm Analysis:https://www.udemy.com/algorithm-analysis/

Twitter:https://twitter.com/CsEverything

YouTube Channel:

Image for post

Computer Science Website:

Image for post

Udemy Videos on Algortithm Analysis:

Image for post

2

No Responses

Write a response