Hi I'm having trouble with this simple C code since i'm very new to it.
When I try to get empty value from or get if the user only hit ENTER without typing anything. Tried few variations and still not working
The piece of code is the following:
char user_input[17] = {'\0'};
printf("Type something: ");
prompt = scanf("%s", user_input);
if (prompt <= 0) {
printf("You didn't type anythingt!\n");
return 1;
}
Also:
char user_input[17] = {'\0'};
printf("Type something: ");
scanf("%s", user_input);
if (user_input[0] == '\0') {
printf("You didn't type anything!\n");
return 1;
}
There is many variations on this forum none of them worked for me... What am I missing?
You can prevent the user from just pressing
[enter]
withscanf
, but you must both:scanf
return, andstdin
on[enter]
alone.You must also manually prevent writing beyond the end of your string by limiting the characters
scanf
attempts to put in your array. Otherwise,scanf
will happily try and write as many characters as are entered. (which makes line-oriented input likefgets
orgetline
preferable)However, if you take that all into consideration, you can do what it looks like you are attempting with
scanf
:Use/Output
getline Solution
As mentioned above, line-oriented input is the preferred manner for reading strings. Two of the primary tools available are
fgets
andgetline
. Both have strengths/weaknesses. Two of the primary strengths ofgetline
is that it (1) will allocate the line buffer for you, and (2) it returns the actual number of characters read into the buffer.A weakness of
getline
is it will read all characters it is given up to the point of memory exhaustion. So if you are limiting your input to17
(16 chars + null-terminator), it is up to you to enforce the limit. A weakness offgets
is that you have no way of knowing how many characters were actually read. All you know isfgets
read somewhere between1
andmax length
generally prompting the need to callstrlen
(or iterate over the string until the null-terminator is found.)Both
fgets
andgetline
will include the trailing'\n'
in the buffer (as it exists in the file, or as produced when you press[enter]
when reading fromstdin
). It is never a good idea to leave stray newlines hanging off the ends of your strings, so it is generally a good idea to strip the newline after it is read.note:
getline
will allocate memory for the buffer if the buffer is initially set toNULL
..and..getline
will reallocate the buffer it is given if it is insufficient to hold the input. (the current size of the allocation is maintained in'n'
). Sincegetline
allocates/reallocates for you, you are responsible for freeing the memory when it is no longer in use.Taking all that into consideration, the following is an equivalent
getline
implementation that prevents the user from leaving an empty string by simply pressing[enter]
at the prompt:Use/Output
The problem is that your
scanf()
call will not return until it encounters a string that is not whitespace only. You should usefgets()
instead, as it prevents overflow as well.Example: