I'm sure you guys have gotten sick of seeing this question over and over again by now but none of the solutions displayed in the other threads have worked for me. I've tried setting the C:\MinGW Path variable, the linker flags, nothing works.
The only thing that has worked is adding:
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
to the code but I really do not want to keep writing this in every single program I make.
The console does show the red "Terminate" button as active so that at least means that the program is running but just not being displayed in the console.
To those not familiar with this bug; I wrote a simple C program in eclipse:
#include <stdio.h>
int main()
{
int n;
printf("Enter number: ");
scanf("%d", &n);
for(int i=0; i<=n; i++) {
printf("%d\n", i);
}
return 0;
}
When I run it the console looks like this:
link
And output is:
5
Enter number: 0
1
2
3
4
5
Any ideas?
Also I'm running Windows 10, maybe that makes a difference?
Related
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 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.
I'm new to the programming world, recently I began my programming path with C, and because of that I made a program that determines if a number is whether perfect or not. I use Code::Blocks IDE, and it works just fine, the problem is when I click the option "Build and run", the IDE executes the program and works perfectly, but when I select the .exe file from my desktop, it opens up, but doesn't show any output, the window just closes suddenly. Does someone have any idea on how to solve this issue?
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main() {
int N;
int j;
int Sum = 0;
printf("Input a number.\n");
scanf("%d",&N);
for (j = 1; j < N; j++)
{
if (N%j==0)
{
Sum+=j;
}
}
if (Sum==N)
{
printf("The number is perfect.\n");
} else {
printf("The number is not perfect.\n");
}
return 0;
}
Running program with Code::Blocks Build and run option
The only part of the Desktop located .exe that I can reach
If someone can suggest a solution, I will be very thankful!
As it was said, the program exits immediatly after it's completion. If you want to run the program, executing it by double click, you can place a pause condition in the program, for instance if you put getchar(); just before the return 0; statement, it will only exit after it receives an input from the keyboard, that is after you enter a key.
Is there some way to run C programs interactively through Notepad++'s console where prompts for input would appear? I can't seem to get it work as expected. I can demonstrate with this bit of code-
#include <stdio.h>
int main()
{
int number, i;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}
return 0;
}
When I run this through the NppExec plugin with this script-
NPP_CONSOLE 1
E:\Documents\Notepad++Portable\tcc\tcc.exe -run $(FULL_CURRENT_PATH)
The result is I get no prompt for input. But if I go ahead and enter it anyway, the program proceeds - prints the prompt but also completes the run as seen here.
E:\Documents\Notepad++Portable\tcc\tcc.exe -run E:\Documents\Programs_C\Factors.c
Process started >>>
60
Enter a positive integer: Factors of 60 are: 1 2 3 4 5 6 10 12 15 20 30 60 <<< Process finished. (Exit code 0)
================ READY ================
I can get it to work just fine through a cmd window which I can call if I make this small change-
npp_run cmd /k E:\Documents\Notepad++Portable\tcc\tcc.exe -run $(FULL_CURRENT_PATH)
Just curious if it is at all possible to get this to work through the console which would make testing small bits of code much more convenient.
I'm a novice programmer getting introduced to C and I'm missing something fundamental about the way my scanf() works. I want to read a single int from the keyboard with code like this:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int userBookSelection;
scanf("%i", &userBookSelection);
printf("Printing userBookSelection: %i", userBookSelection);
return EXIT_SUCCESS;
}
When I run the code, the console stays black until I stop debugging. There is never a cursor waiting for keyboard input. When I stop debug I can see this output in the console, same every time:
Printing userBookSelection: 2130567168
I'm debugging in Eclipse with MinGW GCC compiler on Windows. The code syntax seems to be correct -- is it possible there's something wrong in my build path to make this happen? I need to know why scanf() isn't reading for keyboard input.
So I've gotten a line of code from my professor which takes care of this bug -- whether it's a necessary solution particular to Eclipse and/or MinGW I'm not sure. In any case, here's the code with the additional line:
int main(void) {
int userBookSelection;
setvbuf (stdout, NULL, _IONBF, 0);//<---The magic line
scanf("%i", &userBookSelection);
printf("Printing userBookSelection: %i", userBookSelection);
return EXIT_SUCCESS;
}
I'd appreciate any additional wisdom on what's going on, what setvbuf() is doing and how scanf() works more fundamentally.