My C code is not running in Microsoft VS CODE - c

#include <stdio.h>
int main()
{
int a,b;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);
printf("The sum of a and b is %d", a + b);
return 0;
}
When i run this code in Microsoft Visual Code the code doesn't stop executing nor do i get the output. It starts running but it does not stop. I am also not getting any output. Other type of code is working perfectly fine.
Anyone can help me with this?
click here for image from vscode showing the running window

The code you have written is 100% correct.
Take care of the following things, check it out, if you have done them:
Install gcc in your system. gcc helps u compile c or c++ codes.
Install a C/C++ extension from the extensions section in the visual studio code.
At last, if the situation is still the same, try to re-install the visual studio code.
I hope, this might helps you. If you liked my suggestion, please press the upvoke button to motivate me to write such answers for you.
Thanks!

if this thing happens again first again try to execute the same code with some other IDE or run your code separately with CMD or PowerShell.

Code runner extension runs code in the output file which can not be interacted with. You have to change it so it runs in the terminal.
Go to the menu Code > Preferences > Settings.
In the User tab on the left panel, expand the Extensions section.
Find and select Run Code Configuration.
Find and check the box Run in Terminal.

I had exact same problem and it turns out I forgot to save the code:)

The main reason why code is not running is because it is running in "Output" Window, which I had shown in the linked Image.
If you want to run code properly then you have to run it in "Terminal" Window.
For running your code in "Terminal" window you have to follow these steps:
Step 1: Open Settings.
Step 2: Then type "Run in Terminal" in search box of the setting. Scroll below and find that setting.Install code runner if you haven't.
Step 3: Tick the box which is in 2nd line.
After that your code will run properly.

Related

How can I see what I am writing in Netbeans when I run my code?

I use Netbeans for writing C.
My problem is: I cannot see the output of my code. I mean I can see the result but not the process itself.
For example:
#include <stdio.h>
int main(){
int n;
printf("please enter a number:\n");
scanf("%d",&n);
printf("your number's square is: %d", (n*n));
return 0;
}
When I run this code. Netbeans opens two windows.
one for "build, run" and one for "run". It allows me to write in the "run" window.
but I cannot see the text "please enter a number" or what I am writing. I only see a blank page but when I write a number then hit enter "twice". It shows all outputs in the same window at once. Like this:
please enter a number:
your number's square is: 25
RUN SUCCESSFUL (total time: 2s)
How can I see what I am writing?
I can reproduce your problem. This is a known issue with NetBeans 8.2, but I also see the same problem with NetBeans 11.2.
See these NetBeans Bug Reports:
Bug 165437 - Standard output in Output window is not flushed after '\n'
Bug 269406 - printf() doesnt output to console until scanf() end
Currently there is no fix for the issue, but there is a simple workaround:
Select your C project in the Projects panel.
Right click and select Properties from the context menu.
Select Run from the list of Categories.
Change Console Type from Internal Terminal to External Terminal.
Change External Terminal Type from Default to Command Window.
Click OK.
When you run your project the console will now open in a separate terminal window outside of NetBeans, and your code will work as expected:

Why code runner extension taking too much time to execute?

I have made a very simple program to test the VS code Runner extension.
My code is
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of entered numbers = %d\n",c);
return 0;
}
After running the code and waiting for many minutes it still shows
[Running] cd "/home/souro/Desktop/" && gcc souro.c -o souro && "/home/souro/Desktop/"souro
No output is showing it only shows running.
Try using the terminal for running the code, if you're currently using the output window then it might not be working.
Go to settings, in the search bar type 'terminal' and look for' code runner:run in terminal ' make sure to check the box and you should be good to go.
go to settings search for Code-runner: run in terminal
enable it
Go to VSCode settings and search for 'terminal', then scroll to find 'Code-runner: run in terminal' and enable it.
Check your IntelliSense settings, set the compiler path and IntelliSense mode to g++ and gccx-64 (if you are using C and C++ in windows). Reinstall the extension and create a new file of .c or .cpp and then try again.
After that make sure you run your output through terminal, which you can set as follows- Go to settings>extensions>C/C++>Run code configuration>Scroll down and then check the box which says run in terminal.

A program in C language made by CodeBlocks IDE runs with a double-click in Windows but not in Ubuntu. why?

I am learning C. I am using Ubuntu as main OS but I also have Windows 7 for testing in another partition. I have made a program in C in both OSes using Code Blocks. When I double click the compiled file of program in windows it runs, but when I do the same in Ubuntu it does not run. I have also created .desktop file for it, but even then it doesn't run. But using the command.someone told me code GUI in.so how can i code GUI in it? also why it runs on windows?
./addition
makes it run in terminal. But I want to run it using GUI. I am clicking on its icon but its not opening.Do i need to code GUI for it?
source code is
#include <stdio.h>
int main(){
int a,s,d;
printf("type the values u want to add and give tab between them\n");
scanf("%d %d",&a,&s);
d=a+s;
printf("addition is %d",d);
system("read -p 'Press Enter to EXIT...' var");
return 0;
}
in linux if you run a shell program from the desktop, it does not have a std input or output attached. For this reason you cannot see anything about it.
A quick solution would be to open a terminal and run it from there.
Two easy options if you don't know how to run the program from terminal:
A) Drag the icon of the program into the terminal, it will automatically build the full path to run it
B) Move to the program's home folder and run it from there "./programName"
I hope this helps mate.

How can I see an the output of my C programs using Dev-C++?

I'm looking to follow along with The C Programming Language (Second Addition) on a machine running Vista.
So far, I've found Dev-C++ the easiest IDE to do this in. However, I still have one problem. Whenever I run my compiled code, for example: a simple hello world program, it runs, but the console window just flickers on the screen, and I can't see the output.
How can I see an the output of my C programs using Dev-C++? I found a C++ specific solution, System("pause"), and a really ugly C solution, while looping fflush(stdout), but nothing nice and pretty.
I put a getchar() at the end of my programs as a simple "pause-method". Depending on your particular details, investigate getchar, getch, or getc
In Windows when a process terminates, the OS closes the associated window. This happens with all programs (and is generally desirable behaviour), but people never cease to be surprised when it happens to the ones they write themselves.
I am being slightly harsh perhaps; many IDE's execute the user's process in a shell as a child process, so that it does not own the window so it won't close when the process terminates. Although this would be trivial, Dev-C++ does not do that.
Be aware that when Dev-C++ was popular, this question appeard at least twice a day on Dev-C++'s own forum on Sourceforge. For that reason the forum has a "Read First" thread that provides a suggested solution amongst solutions to many other common problems. You should read it here.
Note that Dev-C++ is somewhat old and no longer actively maintained. It suffers most significantly from an almost unusable and very limited debugger integration. Traffic on the Dev-C++ forum has been dropping off since the release of VC++ 2005 Express, and is now down to a two or three posts a week rather than the 10 or so a day it had in 2005. All this suggest that you should consider an alternative tool IMO.
Use #include conio.h
Then add getch(); before return 0;
The easiest thing to do is to run your program directly instead of through the IDE. Open a command prompt (Start->Run->Cmd.exe->Enter), cd to the folder where your project is, and run the program from there. That way, when the program exits, the prompt window sticks around and you can read all of the output.
Alternatively, you can also re-direct standard output to a file, but that's probably not what you are going for here.
For Dev-C++, the bits you need to add are:-
At the Beginning
#include <stdlib.h>
And at the point you want it to stop - i.e. before at the end of the program, but before the final }
system("PAUSE");
It will then ask you to "Press any key to continue..."
Add this to your header file #include
and then in the end add this line : getch();
You can open a command prompt (Start -> Run -> cmd, use the cd command to change directories) and call your program from there, or add a getchar() call at the end of the program, which will wait until you press Enter. In Windows, you can also use system("pause"), which will display a "Press enter to continue..." (or something like that) message.
Add a line getchar(); or system("pause"); before your return 0; in main function.
It will work for you.
;
It works...
#include <iostream>
using namespace std;
int main ()
{
int x,y; // (Or whatever variable you want you can)
your required process syntax type here then;
cout << result
(or your required output result statement); use without space in getchar and other syntax.
getchar();
}
Now you can save your file with .cpp extension and use ctrl + f 9 to compile and then use ctrl + f 10 to execute the program.
It will show you the output window and it will not vanish with a second Until you click enter to close the output window.
i think you should link your project in console mode
just press Ctrl+h and in General tab select console.
When a program is not showing or displaying an output on the screen, using system("pause"); is the solution to it on a Windows profile.
The use of line system("PAUSE") will fix that problem and also include the pre processor directory #include<stdlib.h>.
Well when you are writing a c program and want the output log to stay instead of flickering away you only need to import the stdlib.h header file and type "system("PAUSE");" at the place you want the output screen to halt.Look at the example here.The following simple c program prints the product of 5 and 6 i.e 30 to the output window and halts the output window.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c;
a=5;b=6;
c=a*b;
printf("%d",c);
system("PAUSE");
return 0;
}
Hope this helped.

Preventing console window from closing on Visual Studio C/C++ Console application

This is a probably an embarasing question as no doubt the answer is blindingly obvious.
I've used Visual Studio for years, but this is the first time I've done any 'Console Application' development.
When I run my application the console window pops up, the program output appears and then the window closes as the application exits.
Is there a way to either keep it open until I have checked the output, or view the results after the window has closed?
If you run without debugging (Ctrl+F5) then by default it prompts your to press return to close the window. If you want to use the debugger, you should put a breakpoint on the last line.
Right click on your project
Properties > Configuration Properties > Linker > System
Select Console (/SUBSYSTEM:CONSOLE) in SubSystem option or you can just type Console in the text field!
Now try it...it should work
Starting from Visual Studio 2017 (15.9.4) there is an option:
Tools->Options->Debugging->Automatically close the console
The corresponding fragment from the Visual Studio documentation:
Automatically close the console when debugging stops:
Tells Visual Studio to close the console at the end of a debugging session.
Here is a way for C/C++:
#include <stdlib.h>
#ifdef _WIN32
#define WINPAUSE system("pause")
#endif
Put this at the top of your program, and IF it is on a Windows system (#ifdef _WIN32), then it will create a macro called WINPAUSE. Whenever you want your program to pause, call WINPAUSE; and it will pause the program, using the DOS command. For other systems like Unix/Linux, the console should not quit on program exit anyway.
Goto Debug Menu->Press StartWithoutDebugging
If you're using .NET, put Console.ReadLine() before the end of the program.
It will wait for <ENTER>.
try to call getchar() right before main() returns.
(/SUBSYSTEM:CONSOLE) did not worked for my vs2013 (I already had it).
"run without debugging" is not an options, since I do not want to switch between debugging and seeing output.
I ended with
int main() {
...
#if _DEBUG
LOG_INFO("end, press key to close");
getchar();
#endif // _DEBUG
return 0;
}
Solution used in qtcreator pre 2.6. Now while qt is growing, vs is going other way. As I remember, in vs2008 we did not need such tricks.
just put as your last line of code:
system("pause");
Here's a solution that (1) doesn't require any code changes or breakpoints, and (2) pauses after program termination so that you can see everything that was printed. It will pause after either F5 or Ctrl+F5. The major downside is that on VS2013 Express (as tested), it doesn't load symbols, so debugging is very restricted.
Create a batch file. I called mine runthenpause.bat, with the following contents:
%1 %2 %3 %4 %5 %6 %7 %8 %9
pause
The first line will run whatever command you provide and up to eight arguments. The second line will... pause.
Open the project properties | Configuration properties | Debugging.
Change "Command Arguments" to $(TargetPath) (or whatever is in "Command").
Change "Command" to the full path to runthenpause.bat.
Hit OK.
Now, when you run, runthenpause.bat will launch your application, and after your application has terminated, will pause for you to see the console output.
I will post an update if I figure out how to get the symbols loaded. I tried /Z7 per this but without success.
add “| pause” in command arguments box under debugging section at project properties.
You could run your executable from a command prompt. This way you could see all the output. Or, you could do something like this:
int a = 0;
scanf("%d",&a);
return YOUR_MAIN_CODE;
and this way the window would not close until you enter data for the a variable.
Just press CNTRL + F5 to open it in an external command line window (Visual Studio does not have control over it).
If this doesn't work then add the following to the end of your code:
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
This wait for you to press a key to close the terminal window once the code has reached the end.
If you want to do this in multiple places, put the above code in a method (e.g. private void Pause()) and call Pause() whenever a program reaches a possible end.
A somewhat better solution:
atexit([] { system("PAUSE"); });
at the beginning of your program.
Pros:
can use std::exit()
can have multiple returns from main
you can run your program under the debugger
IDE independent (+ OS independent if you use the cin.sync(); cin.ignore(); trick instead of system("pause");)
Cons:
have to modify code
won't pause on std::terminate()
will still happen in your program outside of the IDE/debugger session; you can prevent this under Windows using:
extern "C" int __stdcall IsDebuggerPresent(void);
int main(int argc, char** argv) {
if (IsDebuggerPresent())
atexit([] {system("PAUSE"); });
...
}
Either use:
cin.get();
or
system("pause");
Make sure to make either of them at the end of main() function and before the return statement.
You can also use this option
#include <conio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
.
.
.
getch();
return 0;
}
In my case, i experienced this when i created an Empty C++ project on VS 2017 community edition. You will need to set the Subsystem to "Console (/SUBSYSTEM:CONSOLE)" under Configuration Properties.
Go to "View" then select "Property Manager"
Right click on the project/solution and select "Property". This opens a Test property page
Navigate to the linker then select "System"
Click on "SubSystem" and a drop down appears
Choose "Console (/SUBSYSTEM:CONSOLE)"
Apply and save
The next time you run your code with "CTRL +F5", you should see the output.
Sometimes a simple hack that doesnt alter your setup or code can be:
Set a breakpoint with F9, then execute Debug with F5.
Since running it from VS attaches the VS debugger, you can check for an attached debugger:
if (Debugger.IsAttached)
{
Console.WriteLine("Debugger is attached. Press any key to exit.");
Console.ReadKey();
}
I guess the only caveat is that it'll still pause if you attach any other debugger, but that may even be a wanted behavior.
Use Console.ReadLine() at the end of the program. This will keep the window open until you press the Enter key. See https://learn.microsoft.com/en-us/dotnet/api/system.console.readline for details.
Visual Studio 2015, with imports. Because I hate
when code examples don't give the needed imports.
#include <iostream>;
int main()
{
getchar();
return 0;
}
Currently there is no way to do this with apps running in WSL2. However there are two work-arounds:
The debug window retains the contents of the WSL shell window that closed.
The window remains open if your application returns a non-zero return code, so you could return non-zero in debug builds for example.
It should be added that things have changed since then. On Windows 11 (probably 10, I can't check any more) the new Terminal app that now houses the various console, PowerShell and other sessions has its own settings regarding closing. Look for it in Settings > Defaults > Advanced > Profile termination behavior.
If it's set to close when a program exits with zero, then it will close, even if VS is told otherwise.
Go to Setting>Debug>Un-check close on end.

Resources