getchar() not working according to the code [message #1853151] |
Mon, 20 June 2022 16:37 |
C Lehar Messages: 2 Registered: June 2022 |
Junior Member |
|
|
I am new to C language. I'm learning scanf() and getchar() statements but always have trouble while exceuting them. getchar() is not taking input as per the flow of the code, instead first the character input is taken and then rest of the code follows. For example: The code is:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(void) {
char c = 'A';
printf("%c\n",c);
printf("%d\n",c);
printf("%0x\n",c);
printf("Input a character\n");
c = getchar();
printf("You entered %c\n",c);
return EXIT_SUCCESS;
}
But the output first takes a character input and the rest of it executed, If my char input is h, the output is :
h
A
65
41
Input a character
You entered h.
I was having similar problems with scanf(), but using fflush(stdout), after scanf() statement worked. Here fflush is not working and I am not able to figure out. Please help.
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02318 seconds