I have a program that reads input with fgets:
#include <stdio.h>
int main() {
char buf[128];
fgets(buf, 128, stdin);
}
I would like the user to enter any amount of integers. However i am not sure how to parse them. I did some research and found strtol(). So my question is how can i use strtol to parse ints and print them? here is what i tried:
int main() {
char buf[128];
char *a = buf;
char *b;
fgets(buf, 128, stdin);
while (1) {
long x = strtol(a, &b, 10);
printf("%ld ", x);
a = b;
if (b == buf+strlen(buf))
break;
}
Take into account that according to the description of the function in the C Standard
You can write something like shown in the demonstration program below
The program output is