I installed the MingW C compiler and I am trying to compile and run a C program there.
I use Visual Stdio Code IDE.
OS is WINDOWS 10.
Compiler is MinGW.
My programs that do not use scanf() or gets() all run successfully but when I use scanf() function the program does not take input. I also used gets() function but that has the same problem. So when I press "run" button it does not give any output.
Here is my minimal reproducible example. Is this a compiler problem or is my code wrong?
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
//scanf("%d", &n);
}
OUTPUT for the above code is:
[Running] cd "d:\code\C\" && gcc #13sum.c -o #13sum && "d:\code\C\"#13sum
Enter a number:
[Done] exited with code=0 in 0.29 seconds
If I add a scanf():
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
}
OUTPUT for the above code is:
[Running] cd "d:\code\C\" && gcc #13sum.c -o #13sum && "d:\code\C\"#13sum
[Done] exited with code=1 in 31.526 seconds
After using fflush(stdout);
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
fflush(stdout);
scanf("%d", &n);
}
[Running] cd "d:\code\C\" && gcc #13sum.c -o #13sum && "d:\code\C\"#13sum
Enter a number:
[Done] exited with code=1 in 2.804 seconds
After using fflush(stdout); printf() statement is executed but now I am unable to enter the value. I am typing but it is not showing in the output .
Compiler is not any giving errors in any of the above case.
the problem is your compiler its working fine for me.
your code take input until you enter 0 and then print the sum.
here's my output:-
Enter a number: 45 Enter a number: 34 Enter a number: 0 Sum is = 79%
so, check if you installed your mingw C compiler properly here.
Related
I want to insert the input given to a program into its output, in the moment it is read.
For instance, given the program:
#include <stdio.h>
int main() {
double x, y;
printf("Soma de dois números:\n");
printf("Digite o primeiro número: ");
scanf("%lf", &x);
printf("Digite o segundo número: ");
scanf("%lf", &y);
double soma = x + y;
printf("%lf + %lf = %lf\n", x, y, soma);
return 0;
}
when it is run with input redirection, I want the input inserted into the output, like the following:
$ echo -e "10\n25\n" | ./a.out
sum of two numbers
first number: 12
second number: 25
12.000000 + 25.000000 = 37.000000
Is there any library or tool to help me with that?
Edited Context: I intend to use this for correcting program exercises in a programming course. I want to see/show the program input and output as if it were run in an interactive console with the input being typed interactively, although the input is redirect from a text file.
As I noted in a comment:
There isn't an easy way to do that. The problem is that when you type at the terminal, the terminal driver echoes the characters typed. When you have I/O redirection (from a pipe or a file), the terminal driver is not involved, and neither the disk driver nor the pipe driver echoes to standard output.
One option might be to use a pseudo-terminal for the input to your program — but I've not tried it so that might not work. It's also fairly fiddly to set up (though there might well be a utility available on Linux, in particular, to do the job).
Or you could test to see whether the input is a terminal, doing nothing special if it is a terminal, and echoing the result of the input if it is. POSIX function isatty() determines whether the given file descriptor is a terminal or not.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("Enter a number: ");
double d;
if (scanf("%lf", &d) != 1)
{
fprintf(stderr, "Failed to read a number!\n");
exit(EXIT_FAILURE);
}
if (!isatty(STDIN_FILENO))
printf("%lf\n", d);
printf("You entered: %lf\n", d);
return 0;
}
With that saved as source file tty47.c, I can run:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -fno-common tty47.c -o tty47
$ tty47
Enter a number: 2.718282
You entered: 2.718282
$ echo "3.141593" | tty47
Enter a number: 3.141593
You entered: 3.141593
$ tty47 <<<'1.618034'
Enter a number: 1.618034
You entered: 1.618034
$
The first run accepts the input I type at the terminal, and that is echoed by the terminal driver. The other two runs know that the input comes from a pipe and a file respectively, and they echo the input. Note that the echo is not guaranteed to be exactly what the user entered — I cheated by typing numbers with 6 decimal places. Use fewer places and you get trailing zeros; use more and the result is rounded. You'll have to decide whether that matters.
An alternative strategy would read the input using getchar() or equivalent and echo each character with putchar() or equivalent if the input is not a terminal, and then use sscanf() to parse the data. That's definitely fiddlier.
I have been trying to run the scanf() on my mac, but it never worked (took forever to run the program). Everything else like the printf works fine!
#include <stdio.h>
#include <stdlib.h>
int main(){
char name[20];
printf("What is your name? \n");
scanf("%19s", name);
return 0;
}
terminal
[Running] cd "/Users/yiminghuang/Documents/C/" && gcc scanf4.c -o scanf4 && "/Users/yiminghuang/Documents/C/"scanf4
[Done] exited with code=null in 8.611 seconds
[Running] cd "/Users/yiminghuang/Documents/C/" && gcc scanf4.c -o scanf4 && "/Users/yiminghuang/Documents/C/"scanf4
[Done] exited with code=null in 10.384 seconds
[Running] cd "/Users/yiminghuang/Documents/C/" && gcc scanf3.c -o scanf3 && "/Users/yiminghuang/Documents/C/"scanf3
[Done] exited with code=null in 15.999 seconds
I am new to C programming, so please excuse me if I fail to answer your question correctly!
Now, the program you wrote works perfectly fine but as soon as you enter a string separated by a space, it only takes the characters before the space.
To solve this problem, you can use fgets() function of C.
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main(){
char name[MAX];
printf("What is your name? \n");
fgets(name, MAX, stdin);
printf("String = %s", name);
return 0;
}
//i have used fgets() here for getting the input
//it is a better option than gets()
//since it bounds your array and prevents buffer overflow
I have also added a printf() to check if the string is stored correctly.
IMPORTANT: fgets() also consider the newline character i.e. '\n' as a part of your string. So better to keep that in mind.
Check this question to know more about fgets()
Regards!
So basically, I have this simple Hello World program and when executed, it outputs correctly.
#include <stdio.h>
int main(){
printf("Hello");
}
Output: Hello
But for some reason adding a scanf command will cause the program to not output anything. It will display that it's running but it won't display anything.
#include <stdio.h>
int num1;
int main(){
printf("Enter number: \n");
scanf("%d", &num1);
printf("%d", num1);
}
Output:
[Running] cd "d:\programming\" && gcc main.c -o main && "d:\programming\"main
I know my code is okay since I tried it on other IDEs and it worked correctly. I even copied code from the internet and tried to run in VSCode but it still didn't work. It still works perfectly fine yesterday but this problem just pops in nowhere.
I use Visual Studio Code 1.62.3 with C/C++, C/C++ Compile Run, and Code Runner extensions.
It's a buffering problem. When standard output (stdout, where printf writes) is connected to an actual terminal then it's line-buffered, which means output is actually written to the terminal when there's a newline.
However, VSCode probably uses its own terminal emulation and uses pipes to connect stdout to that terminal. That means the output will be fully buffered, and you need to explicitly flush it.
So modify the code as such:
printf("Enter number: \n");
fflush(stdout); // Actually write the output to standard output
scanf("%d", &num1);
printf("%d", num1);
That it works for the first example is because then the process terminates, and all output is flushed automatically.
I've written a simple C program:
#include <stdio.h>
int main(void){
char first[20];
char last[20];
printf("Enter first name: ");
scanf("%s", first);
printf("Enter last name: ");
scanf("%s", last);
printf("Hello %c %c. \n", first, last);
return 0;
}
Then I built the program with compiler from GCC using gcc.exe build active file command and debugged it with the GDB configuration. There is no output in the debug console unless I set 'externalConsole' = true in launch.json, however if I do so I will not be able to examine if my program works or not, because the console window closes immediately after I have entered the strings.
How should I solve this problem and make sure I can debug the program properly?
I'm following a tutorial in c programming, a very simple c file hello.c:
#include <stdio.h>
int main(void) {
int num;
printf("Hello world! Give me an integer:\n");
scanf("%d", &num);
printf("Thanks! I've always been fond of %d.\n", num);
return 0;
}
Source from: https://www.cs.princeton.edu/courses/archive/fall02/cos126/assignments/hello-osx.html
Then compile and run it in using gcc hello.c then ./a.out
There seem to be a pause before I see Hello world! Give me an integer: from the time of ./a.out and hit return.
Why is that so? And how to improve this startup?