Hi have been doing research and I have found three most common different types of input inside a C console application:
- scanf("%[^\n]s", *char): Pick up a string until it finds a break line. It is advised not to use it due to overflow buffer problems.
- gets(*char): Pick up a string until it finds a break line. It is equivalent to the first. It is advised not to use it due to overflow buffer problems too.
- fgets(*char, LENGTH, *FILE): Pick up a string of LENGTH characters, from the file FILE. It is the safest of the three and the most recommended.
Having said this, I will show an example (MCVE) of the use of the three functions and what is picking up them:
#include <stdio.h>
#include <string.h>
int main(void)
{
char hello[7]; //Will store word 'hello'; Length is 6 = 5+(1)+1 = strlen("hello")+(BreakLineChar)+NullTermination - BreakLineChar will appear (or not) depending input type
int i;
// SCANF()
printf("Input HELLO: ");
scanf("%[^\n]s",hello);
fflush(stdin);
printf("Length: %d\n", strlen(hello));
for (i = 0; i < strlen(hello); i++)
{
printf("%i: %c\n", i, hello[i]);
}
printf("\n\n");
// GETS()
printf("Input HELLO: ");
gets(hello);
fflush(stdin);
printf("Length: %d\n", strlen(hello));
for (i = 0; i < strlen(hello); i++)
{
printf("%i: %c\n", i, hello[i]);
}
printf("\n\n");
// FGETS()
printf("Input HELLO: ");
fgets(hello,sizeof(hello),stdin);
fflush(stdin);
printf("Length: %d\n", strlen(hello));
for (i = 0; i < strlen(hello); i++)
{
printf("%i: %c\n", i, hello[i]);
}
printf("\n\n");
return 0;
}
This code, have next output:
Input HELLO: hello
Length: 5
0: h
1: e
2: l
3: l
4: o
Input HELLO: hello
Length: 5
0: h
1: e
2: l
3: l
4: o
Input HELLO: hello
Length: 6
0: h
1: e
2: l
3: l
4: o
5:
Process returned 0 (0x0) execution time : 5.757 s
Press any key to continue.
So, as we can see, three conclusions can be drawn from this:
- scanf: Pick up user's input until '\n' char (does not pick up the character '\n'). So string
hello
would be:
hello[0] = 'h'
hello[1] = 'e'
hello[2] = 'l'
hello[3] = 'l'
hello[4] = 'o'
hello[5] = '\0'
- gets: Equivalent to the point above. So string
hello
would be:
hello[0] = 'h'
hello[1] = 'e'
hello[2] = 'l'
hello[3] = 'l'
hello[4] = 'o'
hello[5] = '\0'
- fgets: Pick up user's input until '\n' char (pick up the character '\ n' too). So string
hello
would be:
hello[0] = 'h'
hello[1] = 'e'
hello[2] = 'l'
hello[3] = 'l'
hello[4] = 'o'
hello[5] = '\n'
hello[6] = '\0'
Are my conclusions correct? Any information to add?
Thank you.
scanf("%[^\n]s",hello);
This is wrong for two reasons. 1) to avoid a buffer overflow, always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer because this input format specifier always appends a NUL byte to the input. and 2) since the statement read all way to (But not including) the newline, the next character will NEVER be a 's'. Should always check the returned value to assure the operation was successful. Suggest:if( scanf("%6[^\n]", hello ) != 1 ) // handle error
– user3629249#define
insidescanf
clause. Regarding second reason, thank you, I will update my answer including this comprobation. Regards, – JuMoGarfflush(stdin);
Per the C standard, this is undefined behavior.fflush()
is only for output streams (regardless of what visual studio might allow) – user3629249stdin
buffer... which function should be used? – JuMoGar