scanf()

Converts formatted text from stdin to binary data.

  #include <stdio.h>
  
  int scanf(const char *format, ...);    
Parameter

format

The format string.

Remarks

The scanf() function reads text and converts the text read to programmer specified types.

scanf() returns the number of items successfully read and returns EOF if a conversion type does not match its argument or and end-of-file is reached.

This facility may not be available on configurations of the EWL that run on platforms without file systems.

Listing: Example of scanf() usage

#include <stdio.h>

int main(void)

{

int i;

unsigned int j;

char c;

char s[40];

double x;

printf("Enter an integer surrounded by ! marks\n");

scanf("!%d!", &i);

printf("Enter three integers\n");

printf("in hexadecimal, octal, or decimal.\n");

// Note that 3 integers are read, but only the last two

// are assigned to i and j.

scanf("%*i %i %ui", &i, &j);

printf("Enter a character and a character string.\n");

scanf("%c %10s", &c, s);

printf("Enter a floating point value.\n");

scanf("%lf", &x);

return 0;

}

Output:

Enter an integer surrounded by ! marks

!94!

Enter three integers

in hexadecimal, octal, or decimal.

1A 6 24

Enter a character and a character string.

Enter a floating point value.

A

Sounds like 'works'!

3.4
Listing: Example of AltiVec scanf Extensions

#include <stdio.h>

int main(void)

{

vector signed char v8, vs8;

vector unsigned short v16;

vector signed long v32;

vector float vf32;

sscanf("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16", "%vd", &v8);

sscanf("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16", "%,vd", &vs8);

sscanf("abcdefgh", "%vhc", &v16);

sscanf("1, 4, 300, 400", "%,3lvd", &v32);

sscanf("1.10, 2.22, 3.333, 4.4444", "%,5vf", &vf32);

return 0;

}

The Result is:

v8 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16;

vs8 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16;

v16 = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'

v32 = 1, 4, 300, 400

vf32 = 1.1000, 2.2200, 3.3330, 4.4444