How to set up Visual Studio Code for Executing and Debugging C Programs | Tutorial
Summary
TLDRThis tutorial offers a comprehensive guide on setting up Visual Studio Code for C programming, including writing, executing, and debugging C programs. It covers the installation of necessary tools, workspace setup, and the use of extensions like 'C/C++' for enhanced functionality. The video demonstrates creating a build task, generating executable files, and running them directly from VS Code. Additionally, it explains how to debug programs using breakpoints and the gdb debugger, providing a step-by-step approach to mastering C development in VS Code.
Takeaways
- π The tutorial is about setting up Visual Studio Code for writing, executing, and debugging C programs.
- π§ An alternative quick method for writing and executing C programs using the Code Runner extension is mentioned, with a link provided in the description box.
- π» Requirements include having Visual Studio Code installed, along with the C tool set and debugger.
- π οΈ Users are guided to verify the installation of gcc and gdb by using command prompt and checking for version numbers.
- π The process of setting up a workspace in Visual Studio Code involves creating folders for organizing C program files.
- π The creation of a 'hello world' C program is demonstrated, including writing a simple program to ask for the user's age and display it.
- π An extension called 'C/C++' developed by Microsoft is recommended for enhanced C/C++ support in Visual Studio Code.
- βοΈ The tutorial explains how to configure a build task in Visual Studio Code to generate an executable file from a C source file.
- πΌ The video shows how to modify the task.json file to include additional compiler arguments and to handle spaces in file and folder names.
- π The script describes how to execute the generated executable file from within Visual Studio Code using the terminal.
- π A trick to modify the task for building and running the program directly without separate steps is shared.
- π Debugging in Visual Studio Code is covered, including setting breakpoints, using the gdb debugger, and interpreting debug console output.
Q & A
What is the main purpose of the tutorial in the script?
-The main purpose of the tutorial is to guide users on how to set up Visual Studio Code for writing, executing, and debugging C programs.
Is there an alternative method mentioned for writing and executing a C program quickly?
-Yes, an alternative method is mentioned which involves using the Code Runner extension for quickly writing and executing a C program.
What are the prerequisites before starting with the tutorial?
-The prerequisites include having Visual Studio Code installed, the C tool set with the debugger, and ensuring that gcc and gdb are properly installed and the path environment variable is set.
How can one check if gcc is installed and the path is set correctly?
-One can check by opening the command prompt and typing 'gcc --version'. If gcc is installed and the path is set correctly, the version will be displayed; otherwise, an error message will indicate that gcc is not recognized.
What debugger is used in the tutorial for C programs?
-The tutorial uses the gdb debugger for C programs.
How does one verify if gdb is installed?
-To verify if gdb is installed, one can type 'gdb --version' in the command prompt. If gdb is installed, the version number will be displayed; otherwise, an error message will indicate that gdb is not recognized.
What is the first step in setting up the workspace in Visual Studio Code according to the tutorial?
-The first step is to open Visual Studio Code, click on 'File', then 'Add Folder to Workspace', and choose or create a folder where the C program files will be saved.
What is the purpose of the 'C/C++' extension in Visual Studio Code mentioned in the script?
-The 'C/C++' extension developed by Microsoft provides C/C++ intelligence, debugging, and code browsing features in Visual Studio Code.
How does one create a build task for a C program in Visual Studio Code?
-One creates a build task by clicking on 'Terminal', then 'Configure Default Build Task', and following the prompts to set up the task, which includes specifying the compiler and build arguments.
What is the significance of the 'launch.json' file in debugging a C program?
-The 'launch.json' file is used to configure the debugger settings in Visual Studio Code. It is automatically generated by the C/C++ extension and can be edited to customize the debugging process.
How can one run a C program directly from the source file without creating an executable first?
-One can modify the build task to include commands that both build the executable file and run it directly. This is done by setting up a shell task in the 'tasks.json' file with the necessary gcc command and execution command.
What is the process of adding a breakpoint in Visual Studio Code for debugging a C program?
-To add a breakpoint, one clicks on the margin next to the line number in the source code where the breakpoint is desired. A red dot appears, indicating the breakpoint has been set.
How can the user specify a particular C/C++ configuration in Visual Studio Code?
-The user can specify a particular C/C++ configuration by editing the 'c_cpp_properties.json' file, which can be accessed through the 'View', 'Command Palette', and searching for 'C/C++: Edit Configurations (UI)'.
Outlines
π Welcome and Introduction to Setting Up Visual Studio Code
In this tutorial, we will explore how to set up Visual Studio Code (VS Code) for writing, executing, and debugging C programs. If you're looking for a quick setup to write and run a C program, check out the Code Runner extension tutorial linked in the description. For this guide, you'll need to download and install VS Code, the C toolset, and the debugger. We start by ensuring that GCC and GDB are properly installed and configured on your system.
π Setting Up the Workspace and Writing the First C Program
First, open Visual Studio Code and create a workspace. Add a new folder for your C programming projects, create subfolders for organizing your files, and save your workspace. Write a simple C program, such as 'Hello World,' and save it in the created folders. The guide walks through these steps, emphasizing the importance of organizing your files properly for a smooth development experience.
π Installing the C/C++ Extension and Creating a Build Task
To enhance your development experience, install the C/C++ extension from Microsoft, which provides IntelliSense, debugging, and code browsing features. Once installed, set up a build task in VS Code to compile your C program into an executable file. This section explains how to configure the build task and includes tips on passing additional arguments for compilation, ensuring a comprehensive setup.
π Running and Debugging the C Program
Learn how to run the compiled executable file from the terminal within VS Code. If the file or folder names contain spaces, use double quotes to avoid errors. The guide also covers how to set up a task to compile and run the program in one step, streamlining the process. Instructions for modifying the task configuration to handle spaces in file names are also provided.
π§ Configuring the Debugger and Setting Breakpoints
This section covers how to set up the GDB debugger in VS Code. By configuring a launch.json file, you can specify the build task for debugging. The guide explains how to add and manage breakpoints, step through the code, and view debugging information in the Debug Console. This comprehensive setup ensures you can effectively debug your C programs.
π Customizing C/C++ Configuration and Conclusion
Customize your C/C++ development environment by editing the c_cpp_properties.json file. This file allows you to specify the C/C++ standards and other configurations for your workspace. The tutorial concludes with a summary of the steps covered and encourages viewers to subscribe for more tutorials. Thank you for watching and see you in the next video!
Mindmap
Keywords
π‘Visual Studio Code
π‘C Program
π‘Code Runner Extension
π‘C Tool Set
π‘Debugger
π‘GCC
π‘Workspace
π‘Executable File
π‘Task
π‘Breakpoint
π‘C/C++ Configurations
Highlights
Introduction to setting up Visual Studio Code for C programming with execution and debugging capabilities.
Alternative method for quick C program writing and execution using the Code Runner extension.
Prerequisite installation of Visual Studio Code and C toolchain with debugger.
Verification of GCC installation via command prompt.
Checking for GDB debugger installation and setup.
Workspace setup in Visual Studio Code for organizing C program files.
Creating a new folder and file for writing a 'Hello World' C program.
Installation of the 'C/C++' extension by Microsoft for enhanced C language support.
Configuring a build task for generating an executable file from C source code.
Explanation of modifying build task arguments for custom compiler options.
Autosave feature to ensure automatic saving of modifications in the source file.
Building an executable file using the configured task from the terminal.
Running the generated executable file and handling spaces in file names.
Creating a task to build and run the C program directly without separate steps.
Debugging a C program in Visual Studio Code using GDB with breakpoints.
Using the 'Debug' view to inspect variables and control program execution.
Editing C/C++ configurations for specific compiler settings in the IDE.
Conclusion summarizing the process of using Visual Studio Code for C program development.
Transcripts
welcome to the channel in this tutorial
we will see how we can set up the visual
studio code
for writing executing and debugging the
c program now if you are just interested
in writing a c program
and executing it as quickly as possible
then i have another method available
where we will be using the code runner
extension
and the link for the tutorial that i
have made will be in the description box
you guys can check that out
here we will open up the visual studio
code set up the workspace
set up the task and also we will see how
we can debug the
code now in order to continue with this
video you need to download and install
visual studio code and also you need to
download and install the
c tool set along with the debugger
now if you don't know about it then i
have the tutorials
again the link will be in the
description box so now
before continuing first we will make
sure that we have all the tools
required so open up the command prompt
and here we will type in gcc
space minus minus version
and hit the enter button and you will
see some version displayed in here
if you get an error message stating that
gcc
is not recognized as an internal or
external command
then it means that gcc is not installed
if it is installed then you have not set
the
path enrollment variable properly so you
have to watch my tutorial
and you have to set up everything now
after that
we will make sure that we have a
debugger installed
we will be working with the gdb debugger
so we will check whether it is installed
or not so we will type in
gdb space minus minus
version and
here also we will see a version number
displayed you know if you get an error
message stating that it is not an
recognized command or something like
that then again it means that it is not
installed and you have to install it
okay so now here we have the tools
required
so what we can do is we can open up the
visual studio code and set it up
so i'm gonna click on the start button
and i'm gonna launch the
visual studio code now here it
is opening with the welcome screen the
first thing that i'm gonna do is
i'm gonna click on the file and i'm
gonna say
add folder to workspace click on that
now you can choose whatever the location
that you want but in this case i'm gonna
save all these files in my desktop
and here i'm to create a new folder
and i'm going to say c programming and
after that
i'm going to open up this folder i'm
going to create another folder
and i'm going to say programs
and after that i'm gonna select this
folder and i will click on
add now here in this
explorer window we can see the programs
folder
if the explorer window is not visible
then you guys can click on this
icon which says explorer okay
now the next thing that we're gonna do
is we're gonna click on file
and we will say save workspace
as and again i'm gonna go to desktop
and this time i'm gonna open up the c
programming folder that we have created
and here i'm gonna save the workspace we
can give any name
i'm gonna say c programming and we will
say workspace
now we can give any name and after that
we will click on save
so now if you look at the explorer here
then we have the workspace called as c
programming workspace
and then we have the programs folder now
we will select this programs folder and
we will click on this new folder icon
and here we can create a folder and
what i'm going to do is i'm gonna create
a folder and
inside that folder i'm gonna create a
file which has the code
written in the c language so here i'm
gonna say
hello world and also make a note that
i'm adding space here between hello and
world
your file and the folder name you know
if you want you can have the
spaces otherwise you can write it in
just one single name
okay now click outside and we have this
folder
again we will click on this new file and
now hello space
world dot c which will be the file
uh you know which will contain the code
written in the c language
now i'm going to close this welcome tab
and here
i'm going to write a very simple c
program and i'm going to fast forward
that one
so here i have written a very simple
program
which will ask the user to enter his age
and then it will display your ages in
whatever the value the user is gonna
enter
now what we have to do is we have to
generate an executable file
from this source file and after that we
have to run that executable file
now before continuing what we do is we
will install
an extension so here we will click on
this
extensions icon and it will display
installed and popular extensions here we
will search for
c c plus plus and
the first extension that we see here is
called as
c c principles and it has been developed
by microsoft and we will click on that
it provides the uh cc plus plus
intelligence debugging and
code browsing features so what we do is
we will
install this extension so i'm going to
click on this install
button now it will install the extension
and in the bottom right corner you can
see
the installation happening so you just
have to wait for
the visual studio code to download and
install
this extension completely now this
um progress bar has been disappeared and
it means that
the extension has been installed
completely now
we will click on the explorer icon in
here
and we go to the hello world.c and also
here
i'm going to close the extension cc plus
plus tab
okay now we have the source file so now
what we have to do is we have to
generate an executable file
so what we do is we will set up a build
task
which will build an executable file from
this
source file so we will click on the
terminal
and we have to click on this configure
default
task and keep in mind we just have to do
it
only once you know the first time when
we are setting it up
once we have set up everything then we
just have to write the program
select the task that we want to run and
everything will happen for us
okay so now here we can see cc plus plus
gcc.exe build active file it is
displayed here
in this command palette and it will also
display
the compiler that you have installed it
may display the path also
okay now what we have to do is we have
to click on this option
now what it does is inside the programs
folder which is present in our workspace
it will create another folder called as
dot vs code and inside that folder
we will have task.json file
this file will contain the build task
now here we can see some details about
in this task
for example the type is cpp build and we
can see the label
and also we can see the command used the
arguments used
if you want you can pass in more
arguments in here for example
it says our default debug information
with this
minus g you know if you want we can
change that one to minus g3
which will mean that the executable will
contain the
maximum debug information and also
if you want to pass in any other
arguments then we can specify for
example
uh what i want to do is i'm going to say
if there are any
warnings in the program i want them to
be displayed
so here i'm gonna specify that one here
minus
wall and after that we will add a comma
in here
so just like the way i'm doing here if
you want you can also specify
the specific c version to be used for
building the executable file
otherwise it will choose whatever the
default c version used
you know we can change that one later by
using another file
which will apply for the entire
workspace and we will talk about that
later
okay after that what i'm gonna do is i'm
gonna save this one
and also go to the source file which is
hello world.c in this case
go to file and make sure that autosave
is selected
so that you know whenever we make any
modifications
the file will be saved automatically now
what we can do is we can use the task
that we have created and
build an executable file now before
building an executable file what i want
you to do is
close the visual studio code and open it
up again
so that whatever the new files that we
have created everything will be loaded
properly
okay close it and relaunch now
what we do is we will click on the
terminal and we will go to
run build task or you guys can use the
shortcut
click on that now what it does is
it builds the executable file by using
the task that we have mentioned if you
look at
the file explorer then in the hello
world folder
we have an executable file called as
hello
world.exe i misspelled world here
but you know that's okay just the name
of the file
okay so now we have the executable file
what we have to do is we have to run
this executable file
so for that what we need is we need a
terminal
so i'm going to click on this plus icon
in here which will create a new terminal
for us
and here now we are in this programs
folder
and the executable file that we want is
actually present
inside another folder called as hello
world so first
we have to go to the hello world folder
so we will type in
cd space hello
world if i hit the enter button here
then
we are in the hello world folder now if
it generates an
error then you just have to type in cd
space
and then in double quotes type in hello
space
world so now we are in the hello world
folder where we have the executable file
so what we have to do is we have to
write the name of the executable file in
here
to run it so it will be hello
space wr old
that's because i misspelled it and now
if i hit the
enter button it says it is not a
recognized command because
the name contains a space so i have to
write that in
double quote so i'm gonna type in
this name in between the double quotes
so it will become one single entity now
and if i hit the enter button
it says enter your age i'm gonna say 30
it says your age is 30. so what we have
done here is we have generated an
executable file
and and also we have run that executable
file
uh one simple trick that i want to show
is if you don't want to
build the executable file first then you
know
going to the terminal and typing all
these things what you can do is you can
set up
the task in such a way that it will
build the executable file and also it
will run it
directly so to do that what we do is
we will open up the task.json file that
we have
in this dot vs code so i'm going to open
it up in here
and here we can see task and
we can see the information specified
between the curly braces
so this one so what we're gonna do is
we're gonna copy
everything which is present from
one curly brace to another curly brace
here like this
um after that we will copy that
uh before that i'm gonna close this
explorer
so that you know we can work with uh
more space in here
after that we go to the end here we will
add a comma
then we go to the next line and we will
paste the content that we have copied
okay now we will go to the second part
in here
and we will change something so we will
change the type here
from cpp build to
shell okay
after that we will change the label we
will change
the value and we can say
run this c program
you know we can give any name because we
are creating this task now
and after that we have the command and
we have the arguments
so what we have to do here is we have to
place these arguments
along with the command here so what we
do is
after this gcc.exe we will add a space
and we will copy these contents and we
will paste them in here
so it will be minus g3 space
minus wall i'm going to copy that i'm
going to paste it in here
after that we have dollar file
copy that and paste it in here
we have negative o
copy that paste it in here and
by the way make sure that you separate
them by using a space
after the -4 we have a space and
after that i'm going to copy this and
i'm going to paste it in here and after
that
add a space type in and
just like the way i'm doing in here and
paste
this last part again
so it will be like this so this last
part will actually contain the
executable file name
along with the path and and what we are
doing here is we are stating
two commands to be executed one is build
the c program
generate the executable file and after
that call that executable file in here
by using this okay now
we will come back here and since we have
specified
these arguments in this command only we
don't need
the args we're gonna remove that
and after that here we will remove this
group also okay after that save it
so now we have this task which we have
created
now we will go to the hello world and
also i'm gonna open up the explorer
and uh you know we can delete this uh
executable file so i'm gonna right click
delete it move to the recycle bin so
in this hello world folder now we have
only hello world.c
so now what we're gonna do is we're
gonna try to run this program by using
the task that we have created
so click on the terminal click on the
run task
and now we can see we have two options
one is the build task
which was created by the extension
automatically and then we have
run this program task that we have
configured i have given this name
you guys may have given a different name
click on that now here it says we have
some error
but it's okay it is expected we can
solve this
so what happens here is since the folder
name and the file name contains
the space when we write this command
here
it treats them as separate entities so
whenever we have the folder name and
file name with space
we need to place them between double
quotes so
we just have to make this one
modification if the folder name and the
file name doesn't contain any space
then this will work but if it has a
space then we get this error
and by the way for debugging the program
this is not required
i just wanted to show you guys um if i
have taken around five minutes of your
life then sorry about that
okay now we're gonna go to the task.json
again
i'm gonna close the explorer and here
what we're gonna do is
whenever we have something which can
contain space we place them in double
quotes
so we're gonna go to the task that we
have configured
which is the task which has the type
shell and
label is run this c program so here this
file name
which is displayed in here after minus
wall
and it can contain space so we have to
place the
value of that file name in double quotes
and the thing is
this command value is actually specified
in double quotes
so we have to escape the double quote
that we use in here
so we will use the backslash we will
type in double quote
again after file backslash
double quote take your time observe
everything cleanly and
do it after negative o we have
folder name and the file name with the
extension
and this can contain the uh spaces
so we have to place them in double
quotes so after
the negative oh we will type in
backslash
double quote after the exe we will
we will add backslash double quote
and here also when we call the
executable that we want to run
it will specify the folder name along
with the file name
so you know that can contain the space
so we will add that
after and backslash double quote
after exe backslash double quote
that's it after that save it
now we will go to helloworld.c
and here we will open up the explorer
and we can see hello world
folder and what i have done is uh i have
deleted the
executable file which was present in the
hello world folder
okay now we will click on the terminal
we will say
run task again we have two tasks
run this c program then uh the task to
build
the executable file so we will click on
run this c program
and now you guys can see it says enter
your age i'm gonna say 30
and that's it you know we have the
output of the program
so you know if you set up this task
then you don't have to use the
extensions like core runner or something
okay so every time whenever you want to
run the program go to terminal
run task and select the task that you
want to
run so you will get the output
okay now we will see how we can debug
this program
so what we do is just above the
extension
icon we can see run click on that
and it will display a button called as
run and debug
click on that and it will open up the
command palette and
we have two options one is gdp lldb
or debug using the windows if you
remember
we have the gtb installed so we will
choose
uh c plus g db lldb so
we will choose that one and now it will
say the task
to choose for building the executable
file for debugging
so here we will choose the task that
the cc plus plus extension has
created for us if remember that is build
and debug active file
okay so we will choose that
now it will start the build it will
finish the build it will create the
executable file
it will launch the debugger and here in
the debug console we can see the debug
information
and if you go to the terminal then we
can see the output of the program
now it says interview age i'm gonna say
20
hit the enter button it says your age is
20 that's the output of the program
the program has run successfully and
also in the debug console we can see
the debug information now here i'm going
to click on the
explorer icon and what has happened here
is
in the dot vs code folder another file
is created called as
launch dot json here i'm gonna close
this
panel and i'm gonna open up the
launch.json file
and you can see the information here
um we don't have to change anything in
here but if you want to do any changes
then
you can do it in here okay so this is
the launch file automatically generated
for us
by that cc plus plus extension so now
we will see how we can debug by using a
breakpoint
now here what i'm going to do is i'm
going to add a breakpoint
let us say i'm going to add the break
point here in this line number seven
so if you take your mouse just before
the line number then you guys can see a
red dot appearing
if you left click in your mouse it will
place that breakpoint in that particular
point
okay i'm going to add the breakpoint
here in this printf statement
and after that what we do is we will
click on the run
you know we go to the uh this debug
section and
and here we will click on this option
which says start
debugging click on that
so what it does is it generates the
executable file
and then it will start
debugging so now here if you click on
the debug console
then we can see the debugging
information and
in terminal it says enter your age i'm
going to enter 30
and after that i'm gonna hit the enter
button in here
now you guys can see it has hit the
break point because
i have mentioned the breakpoint here at
line number seven
if we go to the debug console then we
can see it says thread has hit a
breakpoint
at lane number seven and also we can see
the uh
more information in here for example we
can see the values present in
the local variable in this case we have
age
edges containing 30 and also we can see
the call stack and all this stuff
and also we can see the options like
continue step into
step over and step out and all these
options
so here i'm gonna click on step over now
if i come back to the terminal section
then it will display your age is 30
and since we are in the end of this
program i'm going to click on this
continue option which will run the
program until it encounters the next
breakpoint and
in this case we don't have any
breakpoint so the program will terminate
so this is how we can you know quickly
set up the visual studio code for
debugging the program and one last thing
that we're gonna talk
in this tutorial is if you want to use a
specific
cc plus plus configuration then you can
do that now i'm gonna remove this
breakpoint in here
now what you can do here is if you want
you can
specify a particular cc plus plus
settings to use
to do that we will click on the view
command palette
type in c slash c plus plus
colon edit and you have the option to
edit the c c plus plus configurations
you can directly edit the json file or
you can use the
graphical user interface you know which
is ui
so i'm going to open up the graphical
user interface once we click on that
if you look at the dot vs code folder a
new file is created called as ccpp
properties
and here we can specify i know
particular cc plus plus properties to
use
for example uh c standard used is uh
gunu17
for c plus plus it is using a gunu 14.
if you want you can change that one to
uh gonna 20 and uh for c
plus plus we have the latest standard
available used
so you know if you want to make any
changes here then you guys can
make that and you know after that you
can
run your program so this is it guys this
is how we can use visual studio code
for writing executing and debugging c
programs
if you like this video hit the like
button if you don't like it hit the
dislike button
if you want to say something then write
that in the comment box for more
tutorials like this
do subscribe to the channel thank you
for watching i'll see you later in the
next video
Browse More Related Video
How To Install C Programming Software In Laptop | C Installation Tutorial For Beginners |Simplilearn
C θͺθ¨ε ₯ι | 01 - 05 | ιθ¦ζΊεηε·₯ε ·
Generate C# Model from existing Microsoft SQL Server Database
How the C++ Compiler Works
BEST WAY to read and understand code
Como usar o VS CODE para programar? Guia COMPLETO e RΓPIDO!
5.0 / 5 (0 votes)