Jaguar Jaguar - 3 months ago 30
C Question

How to read numbers separated by space using scanf

I want to read numbers(integer type) separated by spaces using scanf() function.

I have read the following

C, reading multiple numbers from single input line (scanf?)


how to read scanf with spaces

It doesn't help me much.
How can I read numbers with space as delimiter. For e.g. I have following numbers as input

2 5 7 4 3 8 18
now I want to store these in different variables.
Please help.

Answer

I think by default values read by scanf with space/enter. Well you can provide space between '%d' if you are printing integers. Also same for other cases.

scanf("%d %d %d", &var1, &var2, &var3);

Similarly if you want to read comma separated values use :

scanf("%d,%d,%d", &var1, &var2, &var3);