How to Build / Compile and Run a C Program in Command Prompt ( cmd ) in Windows 10 ?

How to Build / Compile and Run a C Program in Command Prompt ( cmd ) in Windows 10 ?


Hi this is Anil from LearningLad and welcome
to another video on C programming language. in this video we will see how we can compile
and run a C program from the command prompt. now if you want to run a C program in your
computer then you need to install the c toolset. now all these things are explained in separate
videos and i’m gonna put the link in the description box and you guys can check that
out. I’m using the Windows operating system and
I am using mingw. mingw stands for minimalist GNU for Windows
and when you install this mingw you get the compiler linker and all these programs so
that you can create a C program and generate an executable file from that and you can execute
that program. So the first thing that we do here is we will
check whether we have properly install and configure the mingw now I have already installed the codeblocks
IDE along with the mingw and if you want to watch the tutorial and install it on your
computer then I’m gonna put the link in the description box and you guys can check it
out. Now i’m gonna open up the command prompt
and here we will check the GCC version installed so it will be gcc — version now if you’re using any other tools at other
than GCC then you may need to use different commands. you guys can check out their documentation Ok now if I hit the enter button I get the error
saying that gcc is not recognised as an internal or the external command now if you get this error then it means that
you may not have installed GCC in your computer you know mingw in your computer or you may
not have set the path environment variable. now I have installed the code blocks IDE along
with the mingw So the mingw is installed in my computer but
I have not set the path environment variable so that I can access that program from any
other location now if you don’t get this error message here,
if you can see the version of the gcc installed then you can skip to the part of the video
where I will be start writing the code. I’m gonna put the timestamp for that in
the description, you guys can go there and you guys can click on that and you can start
watching the video from that point Onwards. now in order to set the path environment variable
first we need to find the location where we have installed the mingw here I have installed it, before that let
me close this command prompt Ok now I have installed the mingw in my C drive
in this program files x86 and here codeblocks folder and inside that you guys can see mingw open that up and go to bin folder and now
we need this path I’m gonna left click on this URL section or
the address bar and then I’m going to copy this location which would be C program files
x86 codeblocks mingw bin Ok now we know where this mingw is installed now we will set the path environment variable Now to do that I’m gonna right click on this
this PC icon and then I’ll go to properties now here I am gonna go to advanced system
settings and then you will see environment variables click on that now it will open up this one and here you
guys can see system variables now scroll down and you will see path Select that by left clicking on that and then
click on edit and then it will open up another window and here you guys can see the various
programs paths specified here now we will add the path for the mingw installation
location which we have copied so you will click on this new and then we
will paste that path that we have copied which is C program files x86 code blocks mingw bin Once you do that click on Ok click on Ok again and click on this ok again and you guys can
close this window ok now we have set the path environment variable
with the location where we have installed the mingw now if I open up the command prompt and if
I type in the command gcc –version then you guys can see. now since we have set the path environment
variable for the mingw installation location we can call that command from any folder Ok now we will write a simple C program and then
compile and run it from the command prompt ok now you can write in the program using any
text editor for the simplicity purpose i’m gonna use the Notepad which is available in
all edition of Windows Ok now lets type in a simple C program now I’m not gonna explain the program here
because I have already made a video and explained all this things if you want you can write that video ok let’s include the stdio.h header file so that
we can access the input and output operations #include stdio.h then we will write the main
function which will be the starting point of our program from our perspective, from
the programmer’s perspective and lets return 0 thats because you know we are saying that
this main function is returning an integer value and then lets use the printf function
and lets print out some text. for example lets say hello world Ok now we need to save this program so I’m gonna go to file, save and i’m
gonna save this problem in my desktop I will create a new folder in my desktop and
I’m gonna call it as C and then here let’s give a name helloworld and then the file extension
should be .c and then click on the save. ok now we have the program helloworld.c Now we will compile and run this program from
the command prompt now I have saved that program in my desktop
inside this C folder if I open up that folder you guys can see
helloworld.c file in here Ok you can see this helloworld.c file in here now I want to open up the command prompt in
this location so I’m gonna go to this URL section or the
address bar or whatever we call it and then I’m gonna left click here and then I will
type in CMD ok
and then if I hit the enter button the command prompt will be opened in this location Ok Now you guys can see here C users anil Shetty
desktop and then C folder now from here if I write in the command dir
then it will list out all the files and folders present in this folder which is C folder which
is in the desktop. you guys can see helloworld.c file Ok now let’s generate the executable file from
this helloworld.c So for the GCC toolset you need to use the
command gcc and then you need to provide the name of the file which is helloworld.c Ok now if I hit the enter button nothing happens but if you look at the folder here then you
can can see a new file is created with with a.exe now in this command prompt while writing this
command we have not specified the file name for the executable file generated and if you
don’t provide the name for the executable file then by default that name will be a and
the executable file in the windows environment will have .exe Extension and you guys can
see a.exe here Ok now here what we have done is we have used
just one command and this one command will perform all Steps From prepossessing compilation
linking, it will perform all the steps and it will generate the executable file now if you want to run this program then we
need to run this executable file here we can see a.exe so I will type in a here in this command prompt
and you guys can see here we get the output hello world now what if you want to give the name for
the executable file Now one thing that you can do it you can go
here and rename the file you know you can do that but while writing this command you
know you can specify the name for the executable file which is being generated and to do that
we have to type in the command gcc then -o which means that I want to give a name for
the executable file and then we need to give the name for the executive file So here let’s say hello.exe Ok that’s gonna be name of the executable
file and then we need to specify the file which is containing the source code which
is helloworld.c Ok now if I hit the enter button nothing happens
here but here but here in this folder you guys can see hello.exe so the executable file generated has a name
now which is hello which we have specified here now if you want to run this program then all
have to do is type in hello that’s it you will get the output ok now this is how you guys can compile and run
C program from the command prompt now if you like this video hit the like button
if don’t like it then hit the dislike button if you want to say something then write that
in the comment box thank you for watching I’ll see you later
in the next video

17 thoughts to “How to Build / Compile and Run a C Program in Command Prompt ( cmd ) in Windows 10 ?”

  1. This is very powerful actually I am going to practice doing that all the time on cmd. Thanks for every new awesome feature that you provide

  2. Hello Anil how are you beside YouTube?
    I'm wondering if you can teach us how to make a C++ program that runs automatically when Windows starts. This thing is unique as well as your videos and is very important.
    As an example for that is the Discord desktop application. When you install discord on your desktop, every time you turn your computer on, your default browser will always open Discord's main page automatically.
    Hope I was clear enough. XD
    Thank you for your time .

  3. Their is a lot of tutorials on YouTube about programming in general but none of them (at least for my watching and searching) have explained how to deal with the Installer tool of your own program.

Leave a Reply

Your email address will not be published. Required fields are marked *