ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

C Programming Tutorial• 6 min read

Data Types, Format Specifiers & Formatted I/O

int, float, double, char, printf, and avoiding scanf buffer overflows

C is statically typed. Input and output functions in <stdio.h> use format specifiers to interpret raw memory representations.

Common Format Specifiers in printf and scanf

%d or %i: Signed 32-bit integer.

%f: Floating point number (defaults to 6 decimal places).

%lf: Double precision floating point (for scanf).

%c: Single character; %s: Null-terminated string.

%p: Memory address pointer (hexadecimal format).

WARNING: scanf("%s", buffer) is vulnerable to buffer overflow attacks because it does not check destination length! Always use fgets() or length bounds like scanf("%31s", buffer).

Formatted I/O in C
#include <stdio.h>

int main(void) {
    int age = 25;
    double gpa = 3.85;
    char grade = 'A';

    printf("Age: %d | GPA: %.2f | Grade: %c | Address: %p\n", 
           age, gpa, grade, (void*)&age);
    return 0;
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which format specifier is used in `printf` to display the memory address of a pointer in hexadecimal?