| Specifier | Data Type | printf Example | scanf Example |
|---|---|---|---|
| %d | int | printf("Value: %d", num); | scanf("%d", &num); |
| %i | int | printf("Value: %i", num); | scanf("%i", &num); |
| %u | unsigned int | printf("Value: %u", num); | scanf("%u", &num); |
| %f | float | printf("Value: %f", num); | scanf("%f", &num); |
| %lf | double | printf("Value: %f", num); | scanf("%lf", &num); |
| %c | char | printf("Char: %c", ch); | scanf("%c", &ch); |
| %s | string (char array) | printf("String: %s", str); | scanf("%s", str); |
| %x, %X | hex integer | printf("Hex: %x", num); | scanf("%x", &num); |
| %hhx | unsigned char (hex) | printf("Hex byte: %hhx", byte); | scanf("%hhx", &byte); |
| %o | octal integer | printf("Octal: %o", num); | scanf("%o", &num); |
| %p | pointer | printf("Address: %p", ptr); | scanf("%p", &ptr); |
| %e, %E | scientific notation | printf("Scientific: %e", num); | scanf("%e", &num); |
| %lu | unsigned long | printf("Value: %lu", num); | scanf("%lu", &num); |
| %ld | long int | printf("Value: %ld", num); | scanf("%ld", &num); |
| %hd | short int | printf("Value: %hd", num); | scanf("%hd", &num); |
| %zu | size_t | printf("Size: %zu", sizeof(int)); | scanf("%zu", &size); |
| Modifier | Description | Example |
|---|---|---|
| %Nd | Field width (N spaces) | printf("%5d", 10); // " 10" |
| %.Nf | Precision (N decimals) | printf("%.2f", 3.14159); // "3.14" |
| %Nd.Nf | Width and precision | printf("%8.2f", 10.5); // " 10.50" |
| %-Nd | Left-justified | printf("%-5d", 10); // "10 " |
| %0Nd | Zero-padded | printf("%05d", 10); // "00010" |
Missing & operator: Forgetting the address-of operator for variables (except arrays/strings with %s)
// Incorrect
int num;
scanf("%d", num); // Missing &
// Correct
scanf("%d", &num);
Buffer overflow with strings: Not limiting string input size
// Incorrect (vulnerable to overflow)
char str[5]; // Only space for 4 chars + null terminator
scanf("%s", str); // Will overflow if input is longer than 4 chars
// Correct (preventing overflow)
char str[5];
scanf("%4s", str); // Reads at most 4 characters
Wrong format specifiers: Using incorrect format specifier for data type
// Incorrect
float num;
scanf("%d", &num); // %d is for int, not float
// Correct
float num;
scanf("%f", &num); // %f for float
Hexadecimal reading issues: Using wrong size specifiers
// Incorrect for reading 1-byte hex values
unsigned int value;
scanf("%x", &value); // Could overwrite adjacent memory
// Correct for reading 1-byte hex values
unsigned char byte;
scanf("%hhx", &byte); // Properly reads just one byte
Newline issues: Scanf leaving newlines in the buffer
// Problem
int num;
char c;
scanf("%d", &num);
scanf("%c", &c); // Will read the leftover newline, not user input
// Solution
scanf("%d", &num);
scanf(" %c", &c); // Notice the space before %c to consume whitespace
Not checking return value: Failing to verify successful input
// Better practice
int num;
if (scanf("%d", &num) == 1) {
// Input successful
} else {
// Input failed
}
Comma issues with multiple inputs: Misunderstanding delimiter behavior
// Incorrect
int a, b;
scanf("%d, %d", &a, &b); // User must enter: "5, 10" with comma
// Correct (usually better)
int a, b;
scanf("%d %d", &a, &b); // User can enter: "5 10" with space
Using expressions: printf can include expressions in its arguments
int mask = 5;
printf("Result: %d", mask << 1); // Prints: "Result: 10"
Format specifier mismatch: Using the wrong specifier for a data type can cause unexpected output or crashes
// Incorrect
float pi = 3.14;
printf("Pi: %d", pi); // %d expects int, not float
// Correct
printf("Pi: %f", pi);
Size mismatch with hex values: Using incorrect specifier for byte-sized values
// When you want to print just one byte in hex
unsigned char byte = 0xAB;
// Incorrect (may print more than one byte)
printf("Byte: %x\n", byte); // May print more than just the byte
// Correct (prints exactly one byte)
printf("Byte: %hhx\n", byte); // Prints: "ab"
Precision control: For floating-point numbers, controlling decimal places
double pi = 3.14159265;
printf("Pi to 2 decimals: %.2f\n", pi); // Prints: "Pi to 2 decimals: 3.14"
printf("Pi to 6 decimals: %.6f\n", pi); // Prints: "Pi to 6 decimals: 3.141593"
Remember to include the appropriate header file for these functions:
#include <stdio.h>