Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printf long long int in C with GCC?

Tags:

c

gcc

printf

c99

How do I printf long long int and also unsigned long long int in C99 using GCC?

I have searched the other posts which suggest to use %lld but it gives these warnings:

warning#1: unknown conversion type character 'l' in format [-Wformat]|
warning#2: too many arguments for format [-Wformat-extra-args]|

For the following attempt:

#include <stdio.h>  int main() {    long long int x = 0;    unsigned long long int y = 0;    printf("%lld\n", x);    printf("%llu\n", y); } 
like image 288
user963241 Avatar asked Nov 27 '12 18:11

user963241


People also ask

How do I print long long int in printf?

Printing short, long, long long, and unsigned Types To print an unsigned int number, use the %u notation. To print a long value, use the %ld format specifier. You can use the l prefix for x and o, too. So you would use %lx to print a long integer in hexadecimal format and %lo to print in octal format.

How do I print long int?

You must use %ld to print a long int , and %lld to print a long long int . Note that only long long int is guaranteed to be large enough to store the result of that calculation (or, indeed, the input values you're using).

Can we use %d for long int?

You should scanf for a %ld if that is what you are expecting. But since a long is larger than your typical int , there is no issue with this.


1 Answers

If you are on windows and using mingw, gcc uses the win32 runtime, where printf needs %I64d for a 64 bit integer. (and %I64u for an unsinged 64 bit integer)

For most other platforms you'd use %lld for printing a long long. (and %llu if it's unsigned). This is standarized in C99.

gcc doesn't come with a full C runtime, it defers to the platform it's running on - so the general case is that you need to consult the documentation for your particular platform - independent of gcc.

like image 144
nos Avatar answered Oct 06 '22 07:10

nos