I can't properly run/debug my code in VS Code using the C language. I've installed C/C++ package on VSC, Mingw & applied the path for Mingw. All my files are running .c format as well.
Only the last part of my code keeps crashing in VSC, when I run this same code on website compilers, it works!
Here is my code:
#include <stdio.h>
int main(void) {
int num1;
int num2;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Answer: %d ", num1 + num2);
return 0;
}
That last printf is where VSC just shuts down the output window, so I never get to see the end result of my code. Anyone have any solutions to fix this? It'd be greatly appreciated!
When you run your console program from Visual Studio, it opens a terminal window, runs the program and the terminal window closes automatically when the program exits. This is a classic problem with the Microsoft Windows platform that they do not seem to care about despite millions of newbie programmers like you experiencing the same problem.
If you open the terminal window yourself, by running the CMD command from the start menu, you will be able to run your program manually after changing the current directory to that of the program binary.
To prevent the terminal window from closing immediately when running directly from Visual Studio, you should add 2 getchar(); statements before returning from main() to wait for user input and get a chance to see the output. Just reading a single byte with getchar() will not suffice because it will just read the pending newline entered by the user in response to the second prompt.
Also note that it is preferable to output a trailing newline to ensure the output is properly flushed on some legacy systems:
printf("Answer: %d\n", num1 + num2);
Here is a modified program you can test:
#include <stdio.h>
int main(void) {
int num1 = 0, num2 = 0;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Answer: %d\n", num1 + num2);
getchar(); // read the pending newline
getchar(); // read at least another byte from the user.
return 0;
}
run you program from the console:
In the search box type cmd
cd \path_to_your_executable
run your program.
Related
Working on a simple C program using basic I/O. printf and scanf to get started.
Using Visual Studio 2019 and Eclipse with MinGW.
The program has several printf statements followed by scanf_s to get an input. I was expecting that printf statements would be executed first and then scanf_s would read the keyboard input.
How in Eclipse that's not what happens. Seems that scanf_s is executed first and then printf statements.
#include <stdio.h>
int main(void)
{
int s;
printf("** Service Ticket Center **\n");
printf(" Choose from the following options.\n");
printf(" 1. Create new ticket.\n");
printf(" 2. Update ticket status.\n");
printf(" 3. Delete ticket.\n");
printf(" 4. Find ticket.");
scanf_s("%d", &s);
return 0;
Ran same code in Visual Studio and printf statements were executed first followed by scanf_s as expected. It seems like Eclipse is the issue.
This code runs perfectly in my IDE; I even tried it in an online compiler just to be sure, but when I try to open the .exe it will only ask for the integer and automatically close. I tried it with another program from the school where I asked for like 15 numbers but right before a goodbye message it just closes. Any idea how to fix cmd?
#include <stdio.h>
int main()
{
int numberOfWidgets;
printf("Give me a number: ");
scanf("%d", &numberOfWidgets);
printf("You choose the number: %d", numberOfWidgets);
return 0;
}
You need to add two lines to the end of your program:
int main() {
...
getchar();
getchar();
return 0;
}
The first call to getchar will clear the return key you pressed when you entered the number, the second one will stop and wait for a key to be pressed.
That way, your program will not exit until you press a key, and you will be able to read the output.
I'm learning C from a tutorial concurrently with a general programming course. The course setup advised Windows users to use SciTE, so I did. Possibly because I have Windows 8, I had to edit the SciTE cpp.properties file to get the sample programs to run. This is what the make/go section of the properties file looks like:
ccopts=-pedantic -Os
cc=g++ $(FileNameExt) -o $(FileName).exe
ccc=gcc $(FileNameExt) -o $(FileName).exe
make.command=make
command.compile.*.c=$(ccc) -std=c99
command.build.*.c=$(make.command)
command.build.*.h=$(make.command)
command.clean.*.c=$(make.command) clean
command.clean.*.h=$(make.command) clean
command.go.*.c=$(FileName)
My problem is that I cannot get this one program to execute in SciTE. It works fine in PowerShell/cmd but if I try to execute it in SciTE, I don't get the first printout and providing input does nothing. It also never ends, even if I stop executing. I have to go into task manager and end the program. I have had this problem before, but that was because I mistyped. I don't know what I've mistyped here:
#include <stdio.h>
#include <conio.h>
int main(void)
{
int num1;
int num2;
printf("Enter 2 numbers\n");
scanf("%d%d", &num1, &num2);
if(num1 == num2) {
printf("they are equal\n");
}
if(num1 < num2) {
printf("%d is less than %d\n", num1, num2);
}
if(num1 > num2) {
printf("%d is greater than %d\n", num1, num2);
}
getch();
}
SciTE's output pane is not a regular console as you would expect - you can not ask for user input in SciTE's output pane.
However you can perhaps make use of parameters and slightly change you script to accept parameters instead user input.
Another option is use of other then default subsystem for go command:
command.compile.*.c=gcc $(FileNameExt) -o $(FileName).exe
command.go.*.c="$(FileDir)\$(FileName).exe"
command.go.subsystem.*.c=2
You can paste this block at the end of you cpp.properties if you wish to try it. Even more if you would like a Go command that compiles on the fly and executes, append this line to above block:
command.go.needs.*.c=gcc $(FileNameExt) -o $(FileName).exe
Side note: You can always terminate running SciTE program with Ctrl+Break or with "Tools > Stop executing" menu command.
I am using Eclipse Indigo CDT and just running simple code which is:
#include <stdio.h>
void main()
{
int num;
printf("enter no\n");
scanf("%d",&num);
printf("no is %d\n",num);
}
Opuput:
55
enter no
no is 55
But when I run this code it won't print enter no. Instead of that it waits to enter the number. After pressing some number it is printing enter no. What could be the reason?
That would depend on the flush scheme of standard out.
Traditionally, stdout is line buffered when connected to a terminal and page buffered when it's not connected to a terminal.
Apparantly, when you run your program in eclipse, standard out is page buffered.
You can overcome this by flushing stdout:
#include <stdio.h>
void main()
{
int num;
printf("enter no\n");
fflush(stdout);
scanf("%d",&num);
printf("no is %d\n",num);
}
It is good practice to flush a file handle whenever you expect the recipient of the information to respond. That way you can be sure that the recipient gets everything you have written regardless of the buffering of the file handle.
so I've decided to install Eclipse to use for my C programming. I wanted to write a small program, just to test that everything works, however it seems that Eclipse won't let me scan in any C inputs. For any other program, that requires no input, it works fine but it seems for some reason Eclipse won't run any program that requires a use to input. I'm running the programs by going to Run->Run As-> Local C/C++ Applications. I've also tried running these programs through the command line, and they turn out fine. Any ideas?
Code:
#include <stdio.h>
int main(void) {
int length, width, height, volume, weight;
printf("Enter the length of box: ");
scanf("%d", &length);
printf("Enter the height of box: ");
scanf("%d", &height);
printf("Enter the width of box: ");
scanf("%d", &width);
volume = length * width * height;
weight = (volume+165)/166;
printf("Volume(cubic inches) %d\n", volume);
printf("Dimensional weight(pounds): %d\n", weight);
return 0;
}
Installed Packages:
After I try to run these programs, nothing appears in the console window, but after I press stop this is what comes out:
Here's a better pic: http://i.imgur.com/zgV1r.png
Try adding an fflush(stdout) after each of your printf() calls as suggested here.
Here is some more discussion of why fflush is required.