Rohit Bajaj
asked on
printf not printing on console at proper time
HI,
I have the following code :
clean->Build All-> Run (Dont know if there is a shorter way to achieve the same)
Here is the output i get :
As you see above the Enter sentence : got printed after i typed in asdajHD...
Even though i used fflush(stdin)
I tried the above same code on Repl.it its working perfectly even without the fflush(stdin) statement.
what could be the reason for this. And how to correct it ?
Thanks
I have the following code :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct word {
char* charPtr;
int numOfCharInWord;
int positionInSentence;
}word;
typedef struct sentence {
word* wordPtr;
int lineNumber;
int numWordsInLine;
}sentence;
int main(void) {
char line[150];
printf("Enter sentence : ");
fflush(stdin);
scanf("%[^\n]s",line);
getchar();
printf(" The sentence you entered is : \n%s",line);
}
I am running the above code in Eclipse.clean->Build All-> Run (Dont know if there is a shorter way to achieve the same)
Here is the output i get :
As you see above the Enter sentence : got printed after i typed in asdajHD...
Even though i used fflush(stdin)
I tried the above same code on Repl.it its working perfectly even without the fflush(stdin) statement.
what could be the reason for this. And how to correct it ?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
that depends on the type of terminal. namely a windows terminal produces such weird behaviors in cases i have always been reluctant to bother debugging. pretty sure it is mostly due to weird bufferisation so flushing the adequate descriptor is likely to work.
ASKER
Yes I am working on windows probably that could be causing the issue.
But it's the console that appears inside eclipse.
Although using gcc externally fixes it.
Is there any alternative apart from doing fflush and making this same code work inside eclipse ?
Also I use gcc on wsl it's ubuntu 20.04
Is it possible to connect the eclipse c compiler to this gcc inside wsl or they are totally separate things ?
But it's the console that appears inside eclipse.
Although using gcc externally fixes it.
Is there any alternative apart from doing fflush and making this same code work inside eclipse ?
Also I use gcc on wsl it's ubuntu 20.04
Is it possible to connect the eclipse c compiler to this gcc inside wsl or they are totally separate things ?
if you fflush stdout instead of stdin like you are supposed to do anyway, whatever console /should/ work
afaik, you can hook eclipse to pretty much anything that can be run from a command line. i don't use eclipse, though, so sorry i cannot provide details.
afaik, you can hook eclipse to pretty much anything that can be run from a command line. i don't use eclipse, though, so sorry i cannot provide details.
The sentence you entered is :
asdfg
so your code works properly so its another dependency that is wrong