Search code examples
cprintfformat-specifiers

Where are the format specifiers implemented/defined in the C language?


I have been working in C for a couple months now and it has never come to my mind to check where actually the format specifiers implemented/defined in the C language format specifiers like:

%f
%d
%p
%s
#include <stdio.h>

int main(){
    printf("%f",7.8);
}

I know these format specifiers are meant to be used with stdio.h but my question is where are they implemented/defined in C?


Solution

  • where are the format specifiers implemented

    They are implemented in the specific implementation source code of printf you are using. There are many implementations of prinf and implmentation of C standard library - https://en.wikipedia.org/wiki/C_standard_library#Implementations. You are using one of them.

    Some are easier to read - https://github.com/eblot/newlib/blob/master/newlib/libc/stdio/vfprintf.c#L1220 . Some are very hard to comprehend and have been optimized through decades - https://github.com/lattera/glibc/blob/master/stdio-common/vfprintf.c#L293 .

    defined in the c language?

    printf format specifiers are "defined" - standardized, specified - in the C language standard. The draft of the standard we are using online you can read about them here https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p1 . But cppreference https://en.cppreference.com/w/c/io/fprintf is friendlier and much better formatted to read than dry standard.