How to read numbers separated by space using scanf

Jainendra picture Jainendra · May 3, 2012 · Viewed 121.5k times · Source

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

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

Nandkumar Tekale picture Nandkumar Tekale · May 3, 2012

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);