Search code examples
cprintfgnu99

C printf specifier %lld prints characters "ld"


I am developing using gcc (-std=gnu99) for an embedded toolchain (Myriota), and I have problems with printf.

when I try the following code:

long long int time = TimeGet();
printf("\nSeconds since epoch: %lld\r\n", time);

it prints:

Seconds since epoch: ld

Using "%" PRId64 prints the same "ld".

Any ideas? I'd appreciate if you could point me to the right place.

Edit variable type corrected long long int time


Solution

  • Most likely, your C library, specifically its implementation of printf, doesn't support C99.

    The type long long int and the %lld format were introduced by the 1999 ISO C standard (C99). Using gcc -std=c99 makes the compiler attempt to conform to C99, (or -std=cNN for later editions) but it can't make the runtime library do things that it doesn't implement. You have a mismatch between what the compiler supports and what the runtime library supports.

    In C90, calling printf with %lld in the format string had undefined behavior.

    Does %ld work for an argument of type long int? If the argument is in the range LONG_MIN to LONG_MAX, converting and using %ld might be a good workaround. If you need to print values less than LONG_MIN or greater than LONG_MAX, implementing a long long int to string conversion isn't horribly difficult. (And in some implementations, long int and long long int are both 64 bits, so just converting to long int and using %ld is sufficient.)