int scanf ( const char * format, ... );
int sscanf ( const char * str, const char * format, ...);
Getting input from keyboard (stdin)
// NOTE: this uses scanf #include <stdio.h> int main() { int i; printf( "Enter an integer: " ); if( scanf( "%d", &i ) <=0 ) { printf ("ERROR"); } else { printf ("The integer value is: %d.",i); } return 0; }
Using a const char*
// NOTE: this uses sscanf #include <stdio.h> int main() { const char* str = "12345"; int i; if(sscanf(str, "%d", &i) <=0) { printf ("ERROR"); } else { printf ("The integer value is: %d.",i); } return 0; }
The string should start with whitespace, a plus or minus sign, or a digit. The function will continue reading from the string until it reaches a non-numerical character.
On success, the function returns the number of items successfully read.
If an input failure occurs before any data could be successfully read, EOF or -1 is returned.
Be very careful that the format specifier ("%d" in this example) matches the type of the passed variable (int in this example).
