long long data type

D

dijaster

Recently I have had the need to run loops of at least 10^10 steps.
Since 32 bit integers apparently can only store values up to about
2*10^9, I have tried to use the "long long" data type. However, it has
not worked and I am unsure why. For example, I try the following code:

testlong.c:

#include <stdio.h>
#include <stdlib.h>

int main() {
long long a;
a = 10000000000LL; //10^10
printf("a = %i\n", a);
return EXIT_SUCCESS;
}

and compile it by:

gcc testlong.c -o testlong

then when I run the executable I obtain the following:

a = 1410065408

Does anyone know how to fix this problem? Perhaps gcc requires some
commandline argument to support long long ints?
Thanks for any suggestions.
 
R

Robert Bauck Hamar

dijaster said:
Recently I have had the need to run loops of at least 10^10 steps.
[...]. For example, I try the following code:

testlong.c:

#include <stdio.h>
#include <stdlib.h>

int main() {
long long a;
a = 10000000000LL; //10^10
printf("a = %i\n", a);

As the format string specifies, printf expects it's second argument to
be an int.
return EXIT_SUCCESS;
}

and compile it by:

gcc testlong.c -o testlong

then when I run the executable I obtain the following:

a = 1410065408

You need to specify the long long length modifier (ll) to printf:
printf("%lli\n", a);
 
K

Keith Thompson

dijaster said:
Thanks Robert. That seems to have solved the problem.

Note that there may be systems on which the compiler supports
"long long", but the runtime library's implementation of printf()
doesn't (or supports it in a non-standard way).

The "long long" type was added to the language in the 1999 version of
the standard (C99), which is not yet widely implemented. Many pre-C99
compilers support it as an extension, but as I recall consensus on the
name of the type was reached before it was reached on the format
strings for printf.
 
E

Erik de Castro Lopo

dijaster said:
Recently I have had the need to run loops of at least 10^10 steps.
Since 32 bit integers apparently can only store values up to about
2*10^9, I have tried to use the "long long" data type. However, it has
not worked and I am unsure why. For example, I try the following code:

testlong.c:

#include <stdio.h>
#include <stdlib.h>

int main() {
long long a;
a = 10000000000LL; //10^10
printf("a = %i\n", a);

Thats the wrong conversion character. Try:

printf("a = %llu\n", a);

Erik
 
L

Lawrence Kirby

Thats the wrong conversion character. Try:

printf("a = %llu\n", a);

%llu is for unsigned long long, for long long use %lld.

You CAN also use %lli. However %d forms are usually preferred over %i
because %d was the original, %i was only a later addition; it is possible
that some old compilers may not support %i, and %d is what C programmers
tend to be used to. d can be easier to read than i in some character
sets. Also remember that %d and %i do different things in scanf()
conversions, and %d is the closer match to the printf() behaviour.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top