Skip to content

C format specifiers

Absolutely! In C, format specifiers are used in functions like printf() and scanf() to tell the compiler what type of data you're working with.

complete list of commonly used format specifiers, along with what they do and an example for each:

Basic Format Specifiers

Specifier Data Type Description Example Code Output
%d int Signed decimal integer printf("%d", 42); 42
%i int Same as %d in printf; base-detect in scanf scanf("%i", &x); (0x10 = 16) Depends
%u unsigned int Unsigned decimal integer printf("%u", 42); 42
%f float/double Decimal floating-point (6 digits) printf("%f", 3.14); 3.140000
%lf double Reads a double in scanf() scanf("%lf", &d);
%c char Single character printf("%c", 'A'); A
%s char[] (string) String of characters printf("%s", "Hi"); Hi
%x int Unsigned hexadecimal (lowercase) printf("%x", 255); ff
%X int Unsigned hexadecimal (uppercase) printf("%X", 255); FF
%o int Octal (base 8) printf("%o", 10); 12
%p Pointer address Memory address printf("%p", ptr); 0x7ffee...
%% Literal % Prints a percent sign printf("%%"); %

🧠 Advanced / Less Common Specifiers

Specifier Data Type Description Example
%e / %E float/double Scientific notation printf("%e", 123.45);1.234500e+02
%g / %G float/double Shorter of %f or %e printf("%g", 123.45);123.45
%ld long int Long decimal integer printf("%ld", 123456789L);
%lld long long int Longer integer printf("%lld", 123456789LL);
%zu size_t Unsigned size type printf("%zu", sizeof(x));

💡 Examples Together

#include <stdio.h>

int main() {
    int i = 10;
    float f = 3.1415;
    char c = 'A';
    char str[] = "Hello";
    printf("int: %d\n", i);
    printf("float: %f\n", f);
    printf("char: %c\n", c);
    printf("string: %s\n", str);
    return 0;
}

Output:

int: 10
float: 3.141500
char: A
string: Hello

✨ Tips

  • Always match the format specifier with the correct data type — or you’ll get garbage output or runtime errors.
  • Use \n for new lines in printf().
  • Use scanf() carefully — wrong specifier can cause serious bugs.