scanf ( " format string " , &var1, &var2, ..... ); |
Very important note:
|
|
Formatting character | Meaning |
---|---|
%d | Print the (next) value as a signed integer value |
%u | Print the (next) value as a unsigned integer value |
%ld | Print the (next) value as a long signed integer value |
%lu | Print the (next) value as a long unsigned integer value |
%f | Print the (next) value as a floating point value |
%lf | Print the (next) value as a double precision floating point value |
%c | Print the (next) value as a character (i.e., used the ASCII code) |
%s | Print the (next) value as a string (will be explained much later) |
int main( int argc, char* argv[] ) { int i; float x; printf( "Enter an integer value:"); scanf( "%d", &i ); printf( "i = %d\n", i); printf( "Enter a floating point value:"); scanf( "%f", &x ); printf( "x = %f\n", x); } |
Sample Output:
Enter an integer value:56 i = 56 Enter a floating point value:3.14 x = 3.140000 |
How to run the program:
|
|
int main( int argc, char* argv[] ) { int i; printf( "Enter an integer value: "); while ( scanf( "%d", &i ) != EOF ) { printf( "i = %d\n", i); printf( "Enter another integer value (type ^D to end): "); } printf( "\nDONE !\n" ); } |
How to run the program:
|