C Basics · beginner · ~10 min
Format and print common types using printf.
printf writes formatted text to stdout. The first argument is a format string with placeholders like %d (int), %f (double), %s (string), %c (char), %x (hex). The remaining arguments fill those placeholders in order.
Because printf is variadic and reads the format string at runtime, mismatched format/argument pairs are a classic source of bugs. Compile with -Wall and gcc will check format strings against arguments.
int age = 30;
double pi = 3.14159;
const char *name = "ada";
printf("name=%s age=%d pi=%.2f\n", name, age, pi);
// → name=ada age=30 pi=3.14
%d for a long — use %ld. %f is for double, not float directly (floats are promoted, but the conversion still says %f).%s — only valid if it points at a NUL-terminated C string.