Jump to content

Trouble Running C in VS Code

JensenEx

Hi beautiful people !

Alright actually I'm running out of time to complete my stuff so Im gonna get to the point here. Im an Engineering student, learning C programming for a subject. Long story short, I have VS Code and I wish to run my C code there, so I went on and google search for solutions and found out a few that seems easy for me to understand. So I went on and try one if it, which is installing the C/C++  Extension for VS Code, I saw on Youtube video that once you install it and re-launch your VS Code, you're good to go. But no it doesnt work for me, when I tried to Run my code (without Debugging), it gave me 2 Environment option to choose from, one is "C/C++ (GDB/LLDB" and the other one is "C/C++ (Windows). So anyways either one of them gave me the same result, which is putting me to a Settings.json file and I have no idea what to do there, it doesn't run my code, it is just there. 

Is there anything I have to write in the Settings.json file in order to run my C code ? I have attached screenshots of the extension I installed as well as the settings.json file. 

Any help would be appreciated, Thank You !

Regards,
Jen.

C C++ extension.png

my VS code and C code (2).png

settings.json page.png

Link to comment
Share on other sites

Link to post
Share on other sites

Since the main function is of type int you should return an int at the end - just add

return 0;

below the final print statement. Without this it shouldn't compile no matter what.

 

As for running it in vscode, I just tried it with the "C/C++", "C/C++ Compile Run" and "Code Runner" plugins and it just worked when I clicked on the "run" button in the top right, no need for restarting or anything else.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, Sauron said:

Since the main function is of type int you should return an int at the end - just add


return 0;

below the final print statement. Without this it shouldn't compile no matter what.

 

main doesn't require you to explicitly return something, it will compile without it :)

edit: could it be that I'm using the gcc compiler? this one doesn't require it

 

@JensenEx I take it that you want to do it automatically, I honestly don't remember how to do it since I haven't used VSCode in a long time, if you wanna do it manually: gcc -o outputFile sourceFile.c  :D

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Hi P said:

main doesn't require you to explicitly return something, it will compile without it :)

Weird, I thought I remembered it not working... I guess it's some compiler magic. Still I'd say it's good practice to explicitly return 0.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Sauron said:

Weird, I thought I remembered it not working... I guess it's some compiler magic. Still I'd say it's good practice to explicitly return 0.

Yea it's the gcc compiler, definitely good practice to add it :D

 

39 minutes ago, Sauron said:

As for running it in vscode, I just tried it with the "C/C++", "C/C++ Compile Run" and "Code Runner" plugins and it just worked when I clicked on the "run" button in the top right, no need for restarting or anything else.

I followed this answer and worked like a charm :)

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Sauron said:

Since the main function is of type int you should return an int at the end - just add


return 0;

below the final print statement. Without this it shouldn't compile no matter what.

 

As for running it in vscode, I just tried it with the "C/C++", "C/C++ Compile Run" and "Code Runner" plugins and it just worked when I clicked on the "run" button in the top right, no need for restarting or anything else.

Hi there, I just tried downloading "Code Runner" and it works. But um, my code requires input, it requires the cmd to pop up so that I can give input and execute the code after that. What do I need in order to run it that way ? With Code Runner it just keeps saying that my code is running and there's nothing for me to be able to interact with. Wasn't expecting this to happen..

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, JensenEx said:

Hi there, I just tried downloading "Code Runner" and it works. But um, my code requires input, it requires the cmd to pop up so that I can give input and execute the code after that. What do I need in order to run it that way ? With Code Runner it just keeps saying that my code is running and there's nothing for me to be able to interact with. Wasn't expecting this to happen..

Open the extension settings and tick the box that says "Run in Terminal"

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Sauron said:

Weird, I thought I remembered it not working... I guess it's some compiler magic. Still I'd say it's good practice to explicitly return 0.

I think it is compiler magic as I know for a fact that I need a return statement in VC++. GCC might imply a default return statement in a program which doesnt have one.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Sauron said:

Open the extension settings and tick the box that says "Run in Terminal"

I got it !! Thank you so much for everyone's help !!!!

Link to comment
Share on other sites

Link to post
Share on other sites

48 minutes ago, trag1c said:

I think it is compiler magic as I know for a fact that I need a return statement in VC++. GCC might imply a default return statement in a program which doesnt have one.

 

4 hours ago, Sauron said:

Weird, I thought I remembered it not working... I guess it's some compiler magic. Still I'd say it's good practice to explicitly return 0.

Agreed, it's always good practice to put the return 0.

 

It is not compiler magic though, it is part of the ISO standard (well at least the older standards...I am outdated on my CPP standards though, so maybe they adapted it in newer versions)

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf (Section 3.6.1 Point 5)

Quote

[...] If control reaches the endofmainwithout encountering areturnstatement, the effect is that of executingreturn 0;

Either way, yes you are right explicitly having a return 0 is the best practice

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

Technically, int main(... something ...) is not always mandatory.

 

In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined.

 

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf

 

Example: The Windows API really would like you to have a function named WinMain() instead.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×