C console output hidden while awaiting input - c

Hi I'm trying some C coding on Eclipse, and I had a problem:
I am trying to print some text, however, while awaiting input from the user, the text actually fails to appear until AFTER the user has input.
Here is an example of what I mean:
TEST
#include <stdio.h>
#include <stdlib.h>
int main(void){
char c[5];
printf("test\n"); //PRINTING 'test' BEFORE i have to enter code
fgets(c, 5, stdin);
printf("You entered: %s\n", c);
return 0;
}
OUTPUT:
dog (this is what i typed)
test
You entered: dog
Rather than appearing BEFORE I am prompted to enter code, the "test" printf only appears AFTER I have entered the code.

Probably this text to print is still waiting in the buffer (that's an optimization, to group data to write to make it more efficient). To make sure everything from buffer gets out to the console you should flush it like this
fflush(stdout);
or you can use a function that does not use buffering like (on linux)
write()

Related

My command prompt wont run the whole program

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.

C Scanf Command Won't Run

I am a new learner to the C language. I am trying to figure out how to use scanf. This is my code so far.
#include <stdio.h>
#include <string.h>
int main() {
char lastInitial;
printf("What is your last inital?");
scanf(" %c", &lastInitial);
}
When I run this code (I'm using VS Code), it shows that the file is running, but nothing shows up. When I go to the top to click the run button again, it says this code is already running. If I stop the run, delete the scanf line and run again, the file runs and displays "What is your last inital?" I am confused as to why adding scanf to the file stops the printf and doesn't allow any user input.
You might have run into a little intricacy with how printf works. When you run printf("What is your last inital?");, this text does not end with a newline (\n) character. As a result, some environments might not display it right away, delaying it until you printf a complete line or until the program ends. This is done for efficiency, since the internal steps to actually get output to your screen are a bit expensive.
When you remove the scanf, the program ends right after the printf; as the program is ending any text that's still waiting to be displayed gets sent to the screen by the built-in shutdown routines in the C standard library. However, when the scanf is included, the text gets buffered/delayed, and doesn't ever get sent to the screen sine the program is stalled waiting for user input. You can force the output to be sent immediately using a newline:
#include <stdio.h>
#include <string.h>
int main() {
char lastInitial;
printf("What is your last inital?\n");
scanf(" %c", &lastInitial);
}
Or if you don't want a newline, you can tell the C standard library to explicitly send all output text right away:
#include <stdio.h>
#include <string.h>
int main() {
char lastInitial;
printf("What is your last inital?");
fflush(stdout);
scanf(" %c", &lastInitial);
}
When you run the program, you should switch from "OUTPUT" to "TERMINAL".
You need to install the "Code Runner" extension.
Then go to File-->Preferences-->Settings in the search write code runner, and below will appear some settings of code runner, you need to find Run In Terminal, and turn it on.
THAT'S ALL :)

Wrong output when I run code in Eclipse Indigo CDT

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.

How to read and print the resulting array with fgets and printf("%s")?

I know it is a simple question but I am stuck.The code is:
#include <stdio.h>
#define MAX_SIZE 1025
#define NUM 64
int main(){
int mem_size;
char types[NUM];
char values[MAX_SIZE];
fgets(types,NUM,stdin);
printf("%s",types);
fgets(values,MAX_SIZE,stdin);
printf("%s",values);
scanf("%d",&mem_size);
printf("%d",mem_size);
return 0;
}
Although I want the results after I type and hit enter, the flow is: I need to enter all the fgets and scanf stuff and it correctly prints the desired results.
What is the problem? Please help.
OP: "Problem is it shows the results after I enter mem_size altogether simultaneously, not one by one"
Some systems to not "flush" the stdout output promptly even with a \n. The output that was seen came out just before the program ended, which forced the buffered stdout to the console.
Either add fflush(stdout) after each printf() or change your system's settings (varies with environment) to promptly send stdout to the console.
Ref:
printf not printing on console

Print a message on a different location without interrupting input prompt in C

In the case that I have a "MessageLine" to display messages and an input prompt Input:
Like this:
MessageLine: Type 123!
Input:
Then, typing a wrong input, would display "Wrong!" on the MessageLine:
MessageLine: Wrong!
Input: somewronginput
Then, the user not knowing what to do next, I want the first message to be shown again after 3 seconds, BUT the cursor is still at the input prompt, means the MessageLine would change it's message without affecting the input prompt.
Could it happen to have an independent sequence for my "MessageLine" or whatever solution that would be?
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <string.h>
void msgline(int msg_var){
char *msg[]={
"Type Numbers \"123\"",
"Wrong! Try Again",
"Correct! Press Any Key to Exit..."
};
gotoxy(25,1);
clreol();
printf("Message: %s",msg[msg_var]);// Message Box
}
void main()
{
char inp[256]={0},
answr[]="123";
clrscr();
do{
msgline(0);
printf("\n\nInput: ");
clreol();
scanf("%s",&inp);
if(!strcmp(inp,answr));
else{
memset(inp,0,sizeof(inp));
msgline(1); // Wrong input
delay(3000);
/* delay function also delays the loop
and the cursor is at the message's end of line */
}
}
while(strcmp(inp,answr));
msgline(2); // Correct input
getch();
}
This level of control is not part of the standard C definition of the output stream(s).
Depending on your platform, you might be able to use GNU ncurses for instance.

Resources