How to set up Visual Studio Code for Executing and Debugging C Programs | Tutorial

LearningLad
26 Jan 202125:17

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

00:00

πŸŽ‰ 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.

05:00

πŸ“‚ 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.

10:01

πŸ”Œ 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.

15:04

πŸš€ 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.

20:07

πŸ”§ 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.

25:08

πŸ“‘ 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

Visual Studio Code is a popular source code editor developed by Microsoft. It supports a wide array of programming languages and is highly customizable with extensions. In the video, it is the primary tool used for writing, executing, and debugging C programs. The script mentions setting up the workspace, installing extensions, and configuring tasks within Visual Studio Code to facilitate C programming.

πŸ’‘C Program

A C Program refers to a sequence of instructions written in the C programming language, which is a general-purpose, procedural, imperative computer programming language developed in the early 1970s. The video tutorial covers how to write a simple C program that asks for the user's age and displays it.

πŸ’‘Code Runner Extension

The Code Runner is an extension for Visual Studio Code that allows for running scripts and programs directly from the editor. The script mentions it as an alternative method for quickly writing and executing C programs without setting up tasks and configurations.

πŸ’‘C Tool Set

The C Tool Set generally refers to a collection of tools required to develop C programs, including a compiler and debugger. In the context of the video, the C Tool Set is necessary for compiling and debugging C programs within Visual Studio Code.

πŸ’‘Debugger

A debugger is a software tool used by programmers to test and debug their code. The script specifically mentions the use of the GDB debugger for debugging C programs in Visual Studio Code, allowing developers to step through code, inspect variables, and catch runtime errors.

πŸ’‘GCC

GCC stands for GNU Compiler Collection, which is a compiler system produced by the GNU Project supporting various programming languages including C. In the video, the script instructs viewers to check if GCC is installed and properly set up on their systems to compile C programs.

πŸ’‘Workspace

In the context of an integrated development environment like Visual Studio Code, a workspace is a collection of folders and files that are related to a project. The script describes setting up a workspace for organizing C program files and tasks.

πŸ’‘Executable File

An executable file is a file that is ready to be run on an operating system. In the video, the process of generating an executable file from a C source file is explained, which is a crucial step in the compilation process.

πŸ’‘Task

In Visual Studio Code, a task is a configuration that defines how to run or build a program. The script details setting up tasks for building C programs and running them, which can include commands for compilation and execution.

πŸ’‘Breakpoint

A breakpoint is a marker in code that tells a debugger to pause the execution of a program at a specific point. The video demonstrates how to set a breakpoint in a C program to inspect variables and step through the code during debugging.

πŸ’‘C/C++ Configurations

These configurations in Visual Studio Code allow developers to specify settings for the C/C++ language service, such as the standard version of the language to use. The script mentions editing these configurations to tailor the development environment to specific project requirements.

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

play00:00

welcome to the channel in this tutorial

play00:02

we will see how we can set up the visual

play00:04

studio code

play00:05

for writing executing and debugging the

play00:08

c program now if you are just interested

play00:11

in writing a c program

play00:12

and executing it as quickly as possible

play00:15

then i have another method available

play00:17

where we will be using the code runner

play00:20

extension

play00:21

and the link for the tutorial that i

play00:22

have made will be in the description box

play00:24

you guys can check that out

play00:26

here we will open up the visual studio

play00:28

code set up the workspace

play00:30

set up the task and also we will see how

play00:32

we can debug the

play00:33

code now in order to continue with this

play00:36

video you need to download and install

play00:38

visual studio code and also you need to

play00:40

download and install the

play00:42

c tool set along with the debugger

play00:45

now if you don't know about it then i

play00:47

have the tutorials

play00:48

again the link will be in the

play00:50

description box so now

play00:52

before continuing first we will make

play00:54

sure that we have all the tools

play00:56

required so open up the command prompt

play00:59

and here we will type in gcc

play01:03

space minus minus version

play01:06

and hit the enter button and you will

play01:08

see some version displayed in here

play01:11

if you get an error message stating that

play01:13

gcc

play01:14

is not recognized as an internal or

play01:16

external command

play01:18

then it means that gcc is not installed

play01:20

if it is installed then you have not set

play01:23

the

play01:23

path enrollment variable properly so you

play01:26

have to watch my tutorial

play01:27

and you have to set up everything now

play01:30

after that

play01:31

we will make sure that we have a

play01:33

debugger installed

play01:35

we will be working with the gdb debugger

play01:38

so we will check whether it is installed

play01:40

or not so we will type in

play01:43

gdb space minus minus

play01:46

version and

play01:49

here also we will see a version number

play01:51

displayed you know if you get an error

play01:53

message stating that it is not an

play01:55

recognized command or something like

play01:56

that then again it means that it is not

play01:59

installed and you have to install it

play02:01

okay so now here we have the tools

play02:04

required

play02:05

so what we can do is we can open up the

play02:07

visual studio code and set it up

play02:09

so i'm gonna click on the start button

play02:11

and i'm gonna launch the

play02:12

visual studio code now here it

play02:15

is opening with the welcome screen the

play02:18

first thing that i'm gonna do is

play02:19

i'm gonna click on the file and i'm

play02:22

gonna say

play02:23

add folder to workspace click on that

play02:27

now you can choose whatever the location

play02:29

that you want but in this case i'm gonna

play02:31

save all these files in my desktop

play02:33

and here i'm to create a new folder

play02:37

and i'm going to say c programming and

play02:40

after that

play02:41

i'm going to open up this folder i'm

play02:43

going to create another folder

play02:46

and i'm going to say programs

play02:50

and after that i'm gonna select this

play02:52

folder and i will click on

play02:54

add now here in this

play02:57

explorer window we can see the programs

play03:00

folder

play03:01

if the explorer window is not visible

play03:03

then you guys can click on this

play03:06

icon which says explorer okay

play03:09

now the next thing that we're gonna do

play03:11

is we're gonna click on file

play03:12

and we will say save workspace

play03:16

as and again i'm gonna go to desktop

play03:20

and this time i'm gonna open up the c

play03:22

programming folder that we have created

play03:25

and here i'm gonna save the workspace we

play03:28

can give any name

play03:29

i'm gonna say c programming and we will

play03:32

say workspace

play03:34

now we can give any name and after that

play03:36

we will click on save

play03:37

so now if you look at the explorer here

play03:40

then we have the workspace called as c

play03:42

programming workspace

play03:43

and then we have the programs folder now

play03:47

we will select this programs folder and

play03:50

we will click on this new folder icon

play03:54

and here we can create a folder and

play03:57

what i'm going to do is i'm gonna create

play03:59

a folder and

play04:00

inside that folder i'm gonna create a

play04:02

file which has the code

play04:04

written in the c language so here i'm

play04:07

gonna say

play04:08

hello world and also make a note that

play04:12

i'm adding space here between hello and

play04:15

world

play04:15

your file and the folder name you know

play04:18

if you want you can have the

play04:20

spaces otherwise you can write it in

play04:22

just one single name

play04:24

okay now click outside and we have this

play04:27

folder

play04:28

again we will click on this new file and

play04:31

now hello space

play04:34

world dot c which will be the file

play04:39

uh you know which will contain the code

play04:41

written in the c language

play04:43

now i'm going to close this welcome tab

play04:45

and here

play04:46

i'm going to write a very simple c

play04:48

program and i'm going to fast forward

play04:50

that one

play04:51

so here i have written a very simple

play04:54

program

play04:54

which will ask the user to enter his age

play04:58

and then it will display your ages in

play05:00

whatever the value the user is gonna

play05:01

enter

play05:02

now what we have to do is we have to

play05:05

generate an executable file

play05:07

from this source file and after that we

play05:10

have to run that executable file

play05:12

now before continuing what we do is we

play05:15

will install

play05:16

an extension so here we will click on

play05:19

this

play05:19

extensions icon and it will display

play05:23

installed and popular extensions here we

play05:26

will search for

play05:27

c c plus plus and

play05:30

the first extension that we see here is

play05:33

called as

play05:34

c c principles and it has been developed

play05:37

by microsoft and we will click on that

play05:41

it provides the uh cc plus plus

play05:43

intelligence debugging and

play05:45

code browsing features so what we do is

play05:48

we will

play05:48

install this extension so i'm going to

play05:50

click on this install

play05:52

button now it will install the extension

play05:56

and in the bottom right corner you can

play05:59

see

play05:59

the installation happening so you just

play06:02

have to wait for

play06:03

the visual studio code to download and

play06:06

install

play06:06

this extension completely now this

play06:10

um progress bar has been disappeared and

play06:12

it means that

play06:13

the extension has been installed

play06:15

completely now

play06:17

we will click on the explorer icon in

play06:19

here

play06:20

and we go to the hello world.c and also

play06:23

here

play06:23

i'm going to close the extension cc plus

play06:25

plus tab

play06:27

okay now we have the source file so now

play06:30

what we have to do is we have to

play06:32

generate an executable file

play06:33

so what we do is we will set up a build

play06:36

task

play06:37

which will build an executable file from

play06:39

this

play06:40

source file so we will click on the

play06:42

terminal

play06:43

and we have to click on this configure

play06:46

default

play06:46

task and keep in mind we just have to do

play06:49

it

play06:49

only once you know the first time when

play06:52

we are setting it up

play06:53

once we have set up everything then we

play06:56

just have to write the program

play06:58

select the task that we want to run and

play07:00

everything will happen for us

play07:02

okay so now here we can see cc plus plus

play07:06

gcc.exe build active file it is

play07:09

displayed here

play07:10

in this command palette and it will also

play07:12

display

play07:13

the compiler that you have installed it

play07:15

may display the path also

play07:18

okay now what we have to do is we have

play07:20

to click on this option

play07:23

now what it does is inside the programs

play07:26

folder which is present in our workspace

play07:29

it will create another folder called as

play07:32

dot vs code and inside that folder

play07:35

we will have task.json file

play07:39

this file will contain the build task

play07:42

now here we can see some details about

play07:44

in this task

play07:45

for example the type is cpp build and we

play07:48

can see the label

play07:50

and also we can see the command used the

play07:52

arguments used

play07:53

if you want you can pass in more

play07:55

arguments in here for example

play07:57

it says our default debug information

play08:01

with this

play08:01

minus g you know if you want we can

play08:03

change that one to minus g3

play08:06

which will mean that the executable will

play08:08

contain the

play08:09

maximum debug information and also

play08:12

if you want to pass in any other

play08:14

arguments then we can specify for

play08:15

example

play08:16

uh what i want to do is i'm going to say

play08:19

if there are any

play08:21

warnings in the program i want them to

play08:23

be displayed

play08:24

so here i'm gonna specify that one here

play08:27

minus

play08:28

wall and after that we will add a comma

play08:31

in here

play08:32

so just like the way i'm doing here if

play08:34

you want you can also specify

play08:35

the specific c version to be used for

play08:38

building the executable file

play08:40

otherwise it will choose whatever the

play08:41

default c version used

play08:44

you know we can change that one later by

play08:46

using another file

play08:48

which will apply for the entire

play08:49

workspace and we will talk about that

play08:51

later

play08:52

okay after that what i'm gonna do is i'm

play08:55

gonna save this one

play08:56

and also go to the source file which is

play08:58

hello world.c in this case

play09:00

go to file and make sure that autosave

play09:03

is selected

play09:04

so that you know whenever we make any

play09:06

modifications

play09:07

the file will be saved automatically now

play09:11

what we can do is we can use the task

play09:14

that we have created and

play09:16

build an executable file now before

play09:19

building an executable file what i want

play09:21

you to do is

play09:22

close the visual studio code and open it

play09:25

up again

play09:28

so that whatever the new files that we

play09:30

have created everything will be loaded

play09:32

properly

play09:32

okay close it and relaunch now

play09:36

what we do is we will click on the

play09:38

terminal and we will go to

play09:40

run build task or you guys can use the

play09:43

shortcut

play09:44

click on that now what it does is

play09:47

it builds the executable file by using

play09:50

the task that we have mentioned if you

play09:53

look at

play09:54

the file explorer then in the hello

play09:56

world folder

play09:57

we have an executable file called as

play10:00

hello

play10:01

world.exe i misspelled world here

play10:04

but you know that's okay just the name

play10:06

of the file

play10:07

okay so now we have the executable file

play10:11

what we have to do is we have to run

play10:12

this executable file

play10:14

so for that what we need is we need a

play10:17

terminal

play10:18

so i'm going to click on this plus icon

play10:21

in here which will create a new terminal

play10:23

for us

play10:24

and here now we are in this programs

play10:27

folder

play10:28

and the executable file that we want is

play10:31

actually present

play10:32

inside another folder called as hello

play10:34

world so first

play10:36

we have to go to the hello world folder

play10:39

so we will type in

play10:40

cd space hello

play10:43

world if i hit the enter button here

play10:46

then

play10:47

we are in the hello world folder now if

play10:50

it generates an

play10:51

error then you just have to type in cd

play10:54

space

play10:55

and then in double quotes type in hello

play10:58

space

play10:59

world so now we are in the hello world

play11:01

folder where we have the executable file

play11:04

so what we have to do is we have to

play11:05

write the name of the executable file in

play11:07

here

play11:08

to run it so it will be hello

play11:11

space wr old

play11:15

that's because i misspelled it and now

play11:17

if i hit the

play11:18

enter button it says it is not a

play11:20

recognized command because

play11:22

the name contains a space so i have to

play11:25

write that in

play11:26

double quote so i'm gonna type in

play11:29

this name in between the double quotes

play11:33

so it will become one single entity now

play11:36

and if i hit the enter button

play11:38

it says enter your age i'm gonna say 30

play11:41

it says your age is 30. so what we have

play11:44

done here is we have generated an

play11:46

executable file

play11:47

and and also we have run that executable

play11:50

file

play11:51

uh one simple trick that i want to show

play11:53

is if you don't want to

play11:55

build the executable file first then you

play11:57

know

play11:58

going to the terminal and typing all

play12:00

these things what you can do is you can

play12:02

set up

play12:03

the task in such a way that it will

play12:06

build the executable file and also it

play12:08

will run it

play12:09

directly so to do that what we do is

play12:12

we will open up the task.json file that

play12:15

we have

play12:16

in this dot vs code so i'm going to open

play12:19

it up in here

play12:20

and here we can see task and

play12:25

we can see the information specified

play12:28

between the curly braces

play12:30

so this one so what we're gonna do is

play12:33

we're gonna copy

play12:34

everything which is present from

play12:37

one curly brace to another curly brace

play12:39

here like this

play12:40

um after that we will copy that

play12:44

uh before that i'm gonna close this

play12:46

explorer

play12:48

so that you know we can work with uh

play12:49

more space in here

play12:51

after that we go to the end here we will

play12:53

add a comma

play12:56

then we go to the next line and we will

play12:58

paste the content that we have copied

play13:01

okay now we will go to the second part

play13:04

in here

play13:05

and we will change something so we will

play13:07

change the type here

play13:09

from cpp build to

play13:13

shell okay

play13:16

after that we will change the label we

play13:19

will change

play13:20

the value and we can say

play13:23

run this c program

play13:26

you know we can give any name because we

play13:28

are creating this task now

play13:30

and after that we have the command and

play13:32

we have the arguments

play13:34

so what we have to do here is we have to

play13:36

place these arguments

play13:38

along with the command here so what we

play13:41

do is

play13:41

after this gcc.exe we will add a space

play13:45

and we will copy these contents and we

play13:47

will paste them in here

play13:49

so it will be minus g3 space

play13:53

minus wall i'm going to copy that i'm

play13:57

going to paste it in here

play13:59

after that we have dollar file

play14:02

copy that and paste it in here

play14:06

we have negative o

play14:10

copy that paste it in here and

play14:13

by the way make sure that you separate

play14:15

them by using a space

play14:18

after the -4 we have a space and

play14:21

after that i'm going to copy this and

play14:24

i'm going to paste it in here and after

play14:27

that

play14:29

add a space type in and

play14:33

just like the way i'm doing in here and

play14:36

paste

play14:36

this last part again

play14:39

so it will be like this so this last

play14:42

part will actually contain the

play14:44

executable file name

play14:47

along with the path and and what we are

play14:49

doing here is we are stating

play14:51

two commands to be executed one is build

play14:54

the c program

play14:55

generate the executable file and after

play14:57

that call that executable file in here

play15:00

by using this okay now

play15:03

we will come back here and since we have

play15:06

specified

play15:07

these arguments in this command only we

play15:10

don't need

play15:10

the args we're gonna remove that

play15:14

and after that here we will remove this

play15:17

group also okay after that save it

play15:21

so now we have this task which we have

play15:25

created

play15:26

now we will go to the hello world and

play15:28

also i'm gonna open up the explorer

play15:31

and uh you know we can delete this uh

play15:33

executable file so i'm gonna right click

play15:36

delete it move to the recycle bin so

play15:39

in this hello world folder now we have

play15:41

only hello world.c

play15:43

so now what we're gonna do is we're

play15:44

gonna try to run this program by using

play15:46

the task that we have created

play15:49

so click on the terminal click on the

play15:51

run task

play15:53

and now we can see we have two options

play15:55

one is the build task

play15:56

which was created by the extension

play15:58

automatically and then we have

play16:00

run this program task that we have

play16:02

configured i have given this name

play16:04

you guys may have given a different name

play16:08

click on that now here it says we have

play16:11

some error

play16:12

but it's okay it is expected we can

play16:14

solve this

play16:15

so what happens here is since the folder

play16:18

name and the file name contains

play16:19

the space when we write this command

play16:22

here

play16:23

it treats them as separate entities so

play16:26

whenever we have the folder name and

play16:29

file name with space

play16:30

we need to place them between double

play16:32

quotes so

play16:34

we just have to make this one

play16:35

modification if the folder name and the

play16:38

file name doesn't contain any space

play16:40

then this will work but if it has a

play16:43

space then we get this error

play16:45

and by the way for debugging the program

play16:47

this is not required

play16:49

i just wanted to show you guys um if i

play16:51

have taken around five minutes of your

play16:52

life then sorry about that

play16:55

okay now we're gonna go to the task.json

play16:58

again

play16:58

i'm gonna close the explorer and here

play17:00

what we're gonna do is

play17:02

whenever we have something which can

play17:03

contain space we place them in double

play17:05

quotes

play17:06

so we're gonna go to the task that we

play17:08

have configured

play17:09

which is the task which has the type

play17:12

shell and

play17:13

label is run this c program so here this

play17:16

file name

play17:18

which is displayed in here after minus

play17:20

wall

play17:21

and it can contain space so we have to

play17:23

place the

play17:24

value of that file name in double quotes

play17:26

and the thing is

play17:27

this command value is actually specified

play17:30

in double quotes

play17:31

so we have to escape the double quote

play17:33

that we use in here

play17:34

so we will use the backslash we will

play17:36

type in double quote

play17:38

again after file backslash

play17:41

double quote take your time observe

play17:44

everything cleanly and

play17:45

do it after negative o we have

play17:49

folder name and the file name with the

play17:52

extension

play17:52

and this can contain the uh spaces

play17:56

so we have to place them in double

play17:58

quotes so after

play18:00

the negative oh we will type in

play18:02

backslash

play18:03

double quote after the exe we will

play18:06

we will add backslash double quote

play18:11

and here also when we call the

play18:14

executable that we want to run

play18:16

it will specify the folder name along

play18:18

with the file name

play18:19

so you know that can contain the space

play18:21

so we will add that

play18:23

after and backslash double quote

play18:27

after exe backslash double quote

play18:30

that's it after that save it

play18:35

now we will go to helloworld.c

play18:38

and here we will open up the explorer

play18:40

and we can see hello world

play18:42

folder and what i have done is uh i have

play18:44

deleted the

play18:45

executable file which was present in the

play18:47

hello world folder

play18:49

okay now we will click on the terminal

play18:51

we will say

play18:52

run task again we have two tasks

play18:56

run this c program then uh the task to

play18:59

build

play18:59

the executable file so we will click on

play19:02

run this c program

play19:05

and now you guys can see it says enter

play19:07

your age i'm gonna say 30

play19:09

and that's it you know we have the

play19:11

output of the program

play19:12

so you know if you set up this task

play19:16

then you don't have to use the

play19:17

extensions like core runner or something

play19:20

okay so every time whenever you want to

play19:23

run the program go to terminal

play19:25

run task and select the task that you

play19:27

want to

play19:28

run so you will get the output

play19:33

okay now we will see how we can debug

play19:35

this program

play19:37

so what we do is just above the

play19:40

extension

play19:41

icon we can see run click on that

play19:45

and it will display a button called as

play19:46

run and debug

play19:48

click on that and it will open up the

play19:51

command palette and

play19:52

we have two options one is gdp lldb

play19:56

or debug using the windows if you

play19:59

remember

play19:59

we have the gtb installed so we will

play20:02

choose

play20:03

uh c plus g db lldb so

play20:07

we will choose that one and now it will

play20:09

say the task

play20:10

to choose for building the executable

play20:12

file for debugging

play20:14

so here we will choose the task that

play20:17

the cc plus plus extension has

play20:20

created for us if remember that is build

play20:23

and debug active file

play20:25

okay so we will choose that

play20:30

now it will start the build it will

play20:32

finish the build it will create the

play20:34

executable file

play20:36

it will launch the debugger and here in

play20:39

the debug console we can see the debug

play20:41

information

play20:42

and if you go to the terminal then we

play20:43

can see the output of the program

play20:45

now it says interview age i'm gonna say

play20:48

20

play20:49

hit the enter button it says your age is

play20:51

20 that's the output of the program

play20:53

the program has run successfully and

play20:55

also in the debug console we can see

play20:57

the debug information now here i'm going

play21:00

to click on the

play21:01

explorer icon and what has happened here

play21:04

is

play21:05

in the dot vs code folder another file

play21:08

is created called as

play21:09

launch dot json here i'm gonna close

play21:12

this

play21:13

panel and i'm gonna open up the

play21:15

launch.json file

play21:17

and you can see the information here

play21:20

um we don't have to change anything in

play21:22

here but if you want to do any changes

play21:24

then

play21:25

you can do it in here okay so this is

play21:28

the launch file automatically generated

play21:30

for us

play21:30

by that cc plus plus extension so now

play21:34

we will see how we can debug by using a

play21:36

breakpoint

play21:38

now here what i'm going to do is i'm

play21:39

going to add a breakpoint

play21:41

let us say i'm going to add the break

play21:43

point here in this line number seven

play21:45

so if you take your mouse just before

play21:47

the line number then you guys can see a

play21:49

red dot appearing

play21:50

if you left click in your mouse it will

play21:52

place that breakpoint in that particular

play21:54

point

play21:55

okay i'm going to add the breakpoint

play21:57

here in this printf statement

play21:59

and after that what we do is we will

play22:01

click on the run

play22:03

you know we go to the uh this debug

play22:05

section and

play22:06

and here we will click on this option

play22:08

which says start

play22:09

debugging click on that

play22:13

so what it does is it generates the

play22:15

executable file

play22:18

and then it will start

play22:21

debugging so now here if you click on

play22:23

the debug console

play22:24

then we can see the debugging

play22:26

information and

play22:28

in terminal it says enter your age i'm

play22:29

going to enter 30

play22:31

and after that i'm gonna hit the enter

play22:34

button in here

play22:35

now you guys can see it has hit the

play22:38

break point because

play22:39

i have mentioned the breakpoint here at

play22:41

line number seven

play22:43

if we go to the debug console then we

play22:44

can see it says thread has hit a

play22:46

breakpoint

play22:47

at lane number seven and also we can see

play22:50

the uh

play22:51

more information in here for example we

play22:52

can see the values present in

play22:54

the local variable in this case we have

play22:57

age

play22:57

edges containing 30 and also we can see

play23:00

the call stack and all this stuff

play23:02

and also we can see the options like

play23:03

continue step into

play23:05

step over and step out and all these

play23:09

options

play23:10

so here i'm gonna click on step over now

play23:13

if i come back to the terminal section

play23:14

then it will display your age is 30

play23:17

and since we are in the end of this

play23:19

program i'm going to click on this

play23:20

continue option which will run the

play23:23

program until it encounters the next

play23:25

breakpoint and

play23:26

in this case we don't have any

play23:27

breakpoint so the program will terminate

play23:30

so this is how we can you know quickly

play23:32

set up the visual studio code for

play23:34

debugging the program and one last thing

play23:36

that we're gonna talk

play23:37

in this tutorial is if you want to use a

play23:40

specific

play23:41

cc plus plus configuration then you can

play23:45

do that now i'm gonna remove this

play23:48

breakpoint in here

play23:49

now what you can do here is if you want

play23:52

you can

play23:53

specify a particular cc plus plus

play23:56

settings to use

play23:57

to do that we will click on the view

play24:00

command palette

play24:01

type in c slash c plus plus

play24:05

colon edit and you have the option to

play24:08

edit the c c plus plus configurations

play24:11

you can directly edit the json file or

play24:13

you can use the

play24:15

graphical user interface you know which

play24:18

is ui

play24:19

so i'm going to open up the graphical

play24:20

user interface once we click on that

play24:23

if you look at the dot vs code folder a

play24:25

new file is created called as ccpp

play24:28

properties

play24:29

and here we can specify i know

play24:31

particular cc plus plus properties to

play24:33

use

play24:34

for example uh c standard used is uh

play24:36

gunu17

play24:38

for c plus plus it is using a gunu 14.

play24:41

if you want you can change that one to

play24:43

uh gonna 20 and uh for c

play24:46

plus plus we have the latest standard

play24:48

available used

play24:49

so you know if you want to make any

play24:51

changes here then you guys can

play24:53

make that and you know after that you

play24:56

can

play24:57

run your program so this is it guys this

play24:59

is how we can use visual studio code

play25:02

for writing executing and debugging c

play25:04

programs

play25:05

if you like this video hit the like

play25:06

button if you don't like it hit the

play25:08

dislike button

play25:09

if you want to say something then write

play25:10

that in the comment box for more

play25:12

tutorials like this

play25:13

do subscribe to the channel thank you

play25:14

for watching i'll see you later in the

play25:16

next video

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
VS CodeC ProgrammingDebuggingSetup GuideCode RunnerGCCGDBTutorialCoding TipsProgramming Basics