30

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.

Chris Tang
  • 544
  • 7
  • 14
Jainendra
  • 23,305
  • 30
  • 116
  • 165

4 Answers4

39

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);
Nandkumar Tekale
  • 14,991
  • 7
  • 52
  • 85
  • 2
    scanf ignores whitespace with most `%` directives, so you generally don't want spaces in your format strings. So for the first case (whitespace delimited integers), you want `scanf("%d%d%d", &var, &var2, &var3);` The extra spaces don't actually hurt anything here (they have no effect), but in other cases you don't want them unless you need them. – Chris Dodd Feb 07 '13 at 21:23
  • Why do you have to use &var1 here (is var1 an int type variable?)? Isn't that just a 'label' for var1's memory adress? – YoTengoUnLCD Oct 04 '15 at 23:24
  • @YoTengoUnLCD yes exactly. The `scanf` function needs the address in order to overwrite the value at that address. This is true for `ints` and any other type you might use with `scanf`. There's nothing magic going on here, if you have any function that needs to overwrite a value, that function needs the address, not the value itself. – parker.sikand Jul 21 '16 at 16:48
6

scanf uses any whitespace as a delimiter, so if you just say scanf("%d", &var) it will skip any whitespace and then read an integer (digits up to the next non-digit) and nothing more.

Note that whitespace is any whitespace -- spaces, tabs, newlines, or carriage returns. Any of those are whitespace and any one or more of them will serve to delimit successive integers.

Chris Dodd
  • 101,438
  • 11
  • 111
  • 197
2

It should be as simple as using a list of receiving variables:

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

Alexander
  • 48,074
  • 8
  • 78
  • 121
  • what is fp here? In the above case the delimiter is \n, I want to use space as delimiter. – Jainendra May 03 '12 at 06:10
  • Sorry, I removed it since it is not pertinent in this example. You don't need to specify the space delimiter somewhere, it is implicit in your format string. –  May 03 '12 at 06:36
  • Never use `%i`.. unless you know what it does. – Antti Haapala Sep 15 '19 at 18:57
2
int main()
{
char string[200];
int g,a,i,G[20],A[20],met;

gets(string);
g=convert_input(G,string);

for(i=0;i<=g;i++)
    printf("\n%d>>%d",i,G[i]);
return 0;
}

int convert_input(int K[],char string[200])
{
int j=0,i=0,temp=0;
while(string[i]!='\0')
{
    temp=0;
    while(string[i]!=' ' && string[i]!='\0')
        temp=temp*10 + (string[i++]-'0') ;
    if(string[i]==' ')
        i++;
    K[j++]=temp;
}
return j-1;
}
niraj.nijju
  • 458
  • 1
  • 6
  • 13