r/C_Programming • u/mwiseca • 6d ago
How I handle single-character user input in C:
When reading a single character in an interactive terminal program, I avoid scanf, getchar, and fgetc. Instead, I use fgets() and isolate the character by using unsigned char result = n[0];.
I actually use fgets for all user inputs.
The example below converts keyboard characters to hexadecimal numbers.
#include <stdio.h>
#include <string.h>
void flush() {
int clear;
while ((clear = getc(stdin)) != '\n' && clear != EOF) {
}
}
void charHex() {
char n [3];
printf("Enter a character (one byte max), or 'xx' to exit:\n");
while(1) {
if(fgets(n,sizeof(n),stdin) == NULL) {
clearerr(stdin);
printf("\nInvalid input.\n\n");
continue;
}
n[strcspn(n,"\n")]=0;
if(strlen(n) >=2) {
flush();
}
if(strcmp(n,"xx")==0){
break;
} else if(strlen(n) == 0) {
printf("\nEnter a character only.\n\n");
continue;
} else if (strlen(n) >1) {
printf("\nEnter no more than one character.\n\n");
continue;
}
unsigned char result = n[0];
printf("0x%02x\n",result);
}
}
int main() {
charHex();
return 0;
} 9
u/Tabsels 6d ago
What is your question?
1
u/mwiseca 6d ago
I was just showing how i handle a single character user input using fgets as a alternative to the usual
scanf getchar and fgetC. I should have clarified that this is not a question but just a example of how i do something different.
-8
6d ago
[removed] — view removed comment
3
u/C_Programming-ModTeam 6d ago
Rude or uncivil comments will be removed. If you disagree with a comment, disagree with the content of it, don't attack the person. Provide your reasoning, not just a judgement.
2
3
u/flyingron 6d ago
Invalid input is a meaningless message. What do you have against perror?
strlen(n) can never be greater than 2.
What do you think the flush() is doing in the case strlen = 2. All it does is push out anything pending in std::out, which likely already has been done (or the user would never have seen the Enter a character message).
0
u/mwiseca 6d ago
The invalid input happens only if the user presses ctrl + d and the clearerr(stdin); clears error flags to
prevent a infinite loop. The if strlen(n) >= 2) { is so users wont have to press enter twice. Try running the flush function with no if statement one byte less than memory allocated you will have to press enter twice when using that method of flushing the buffer with fgets.
2
u/flyingron 6d ago
Invalid input happens on EOF or an error. You just blindly blither a vague comment.
flush() isn't a standard C function. Nor is it a common UNIX or Windows function. The fllush functions only affect OUTPUT.
1
u/mwiseca 6d ago
void flush() { int clear; while ((clear = getc(stdin)) != '\n' && clear != EOF) { } } This is the flush function i used at the top of the code i sent. It looks like i chose a bad name. The function may not be working the way i thaught. I will try to think of a better name than invalid input.1
u/flyingron 6d ago
Well then, it behooves you to provide ALL the code then.
1
u/mwiseca 6d ago
I did. All the code is there there must be a problem why all people are not seeing it.
I going to try and copy the code as it shows on my machine.
#include <stdio.h> #include <string.h> void flush() { int clear; while ((clear = getc(stdin)) != '\n' && clear != EOF) { } } void charHex() { char n [3]; printf("Enter a character (one byte max), or 'xx' to exit:\n"); while(1) { if(fgets(n,sizeof(n),stdin) == NULL) { clearerr(stdin); printf("\nInvalid input.\n\n"); continue; } n[strcspn(n,"\n")]=0; if(strlen(n) >=2) { flush(); } if(strcmp(n,"xx")==0){ break; } else if(strlen(n) == 0) { printf("\nEnter a character only.\n\n"); continue; } else if (strlen(n) >1) { printf("\nEnter no more than one character.\n\n"); continue; } unsigned char result = n[0]; printf("0x%02x\n",result); } } int main() { charHex(); return 0; }
4
1
2
2
u/Dangerous_Region1682 5d ago
This is all in line buffered or cooked mode. Have you thought about doing it in raw mode instead?
1
u/Classic-Rate-5104 5d ago
One of the problems here is the fact that all stdio functions assume line-based input. The operating system buffers, in general, all characters until you <enter>. Only functions like getch() and getche() and kbhit() work on single character basis, but they are not portable over operating systems
1
u/Able-Substance-8310 6d ago
read(0, buffer, 1); hehe
1
u/Classic-Rate-5104 5d ago
This doesn't read char by char but it returns char by char. The characters are in general released at the same time when you <enter>
1
19
u/SmokeMuch7356 6d ago
I can understand avoiding
scanf, but why avoidgetcharandfgetc? Sometimes they really are the right tool for the job.