I'm currently trying writing a program using C (very new to C - only been learning it for 2 weeks), and I wanted to get a string of input from the user by stdin, in which the string has a char, followed by 2 floats (each has space in between). Example would be: "y 2.1 1.1". My question is how can I obtain and store the 3 inputs, while making sure the first is a char, and the following two inputs are floats?
-
1http://man7.org/linux/man-pages/man3/strtol.3.html Try `strtol` – lost_in_the_source Mar 26 '16 at 12:16
-
It isn't possible for a user to enter something which isn't a char so any test if the first character is in fact a character will of course always return true. Do you mean how to check if it is in the character range `a-z`, or `A-Z`? – John Coleman Mar 26 '16 at 12:30
-
@Boku Why don't you use strtok()? – Sumeet Mar 26 '16 at 13:16
-
@SumeetSingh The main reason is probably I'm not really familiar with the strtok function. I've gotten it to work, but like I stated in the thread I could not get rid of the white spaces at the very end, and also I don't really know how to process the tokens - i.e. how to check what type they are and if they satisfy my requirements of first token being char, second and third being integers. – Boku Mar 26 '16 at 13:56
-
@Boku I have posted the solution, feel free for any queries. – Sumeet Mar 26 '16 at 13:57
-
@Boku The best explanation of strtok is given here http://www.tutorialspoint.com/ansi_c/c_strtok.htm – Sumeet Mar 26 '16 at 14:02
-
Similar to http://stackoverflow.com/q/36231014/2410359 – chux - Reinstate Monica Mar 26 '16 at 15:51
2 Answers
Stick with sscanf()
, but don't forget to check its return value (look here). What really happens for input "y 1u 1" is that sscanf
will read and store the char
, which is valid, then it will read and store the int
1, which is valid, and then stop, because "u" does not match the format string.
Below is example code (using scanf()
rather then fgets()
and sscanf()
).
char in1;
int in2,in3;
int retval;
/*
char array[100] = {'\0'};
fgets(array, 100, stdin);
retval = sscanf(array, "%c %d %d", &in1, &in2, &in3);
*/
retval = scanf("%c %d %d", &in1, &in2, &in3);
printf("Scanned %d items\n", retval);
printf("Here they come: ");
if(retval > 0) {
printf("%c ", in1);
}
if(retval > 1) {
printf("%d ", in2);
}
if(retval > 2) {
printf("%d", in3);
}
putchar('\n');

- 1,193
- 8
- 11
-
1Then use `strtok()` to split the string into tokens and use `strtol()` where appropriate with end checking. See [this](http://www.cplusplus.com/reference/cstdlib/strtol/#example) for inspiration. – Honza Remeš Mar 26 '16 at 12:57
How can I obtain and store the 3 inputs, while making sure the first is a char, and the following two inputs are ints?
problem with this code is that there are extra spaces at the very end, and I don't know how to get rid of it.
A simple way to use sscanf()
and check if there is extra anything after the scanned variable is to use "%n"
to record the location of the scan at that point.
char in1;
int in2, in3;
int n = 0;
sscanf(array,"%c %d %d%n", &in1, &in2, &in3, &n);
if (n > 0 && (array[n] == '\n' || array[n] == '\0')) {
Success(in1, in2, in3);
}
It is always important to check the results of sscanf()
. One way is to check its return value which should be 3 here. Unfortunately that does not tell us if anything exist after in3
. By setting n == 0
and then testing n > 0
, code knows that scanning proceeded all the way successfully to "%n"
. Code can also test what character the scanning stopped at.

- 1
- 1

- 143,097
- 13
- 135
- 256
-
@Boku "this method fails" --> would be nice if you provided details. Please provide 3: the value of `array[]`, `n` and the result of `strlen(array)` – chux - Reinstate Monica Mar 27 '16 at 17:21
-
@Boku Please provide the remaining 2: the value of `array[]` and the result of `strlen(array)`. Suspect `array[5] == '\r'` – chux - Reinstate Monica Mar 27 '16 at 17:50
-
@Boku "array[5]= ascii code 13" matches my "Suspect array[5] == '\r'" Given your post "But the problem with this code is that there are extra spaces at the very end, and I don't know how to get rid of it." Try `"%c %d %d %n"` (added space before %n). This will pass any white-space after the number, be it `'\n'`, `'\r'`, `' '`. – chux - Reinstate Monica Mar 27 '16 at 19:07