|
|
| Formatting character | Meaning |
|---|---|
| %d | Print the (next) value as a signed decimal integer value |
| %u | Print the (next) value as a unsigned decimal integer value |
| %ld | Print the (next) value as a long signed decimal integer value |
| %lu | Print the (next) value as a long unsigned decimal integer value |
| %f | Print the (next) value as a floating point value |
| %lf | Print the (next) value as a double precision floating point value |
| %o | Print the (next) value as a octal based number |
| %x | Print the (next) value as a hexadecimal based number |
| %p | Print the address (pointer) as a hexadecimal based number |
| %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) |
| %% | Print a % character |
Commonly used conversion characters: %d (int), %f (float), %lf (double) and %s (string)
Example:
#include <stdio.h>
int main( int argc, char* argv[] )
{
int x;
x = 65; // ASCII code for 'A'
printf( "x = 65: %d, %u, %c\n", x, x, x); // Same values !
x = -1;
printf( "x = -1: %d, %u, %c\n", x, x, x);
}
Output:
x = 65: 65, 65, A
x = -1: -1, 4294967295, ???
|
DEMO: /home/cs255001/demo/C/set1/printf.c
|
Note: I will explain why you need to use the & symbol later !
Example:
#include <stdio.h>
int main( int argc, char* argv[] )
{
int i;
float x;
printf( "Enter an integer value:");
scanf( "%d", &i ); // Must use %d to read int value
printf( "i = %d\n", i);
printf( "Enter a floating point value:");
scanf( "%f", &x ); // Must use %f to read float value
printf( "x = %f\n", x);
}
|
DEMO: /home/cs255001/demo/C/set1/scanf.c