Put an l (lowercased letter L) directly before the specifier.
unsigned long n;
long m;
printf("%lu %ld", n, m);
Answer from postfuturist on Stack OverflowPut an l (lowercased letter L) directly before the specifier.
unsigned long n;
long m;
printf("%lu %ld", n, m);
I think you mean:
unsigned long n;
printf("%lu", n); // unsigned long
or
long n;
printf("%ld", n); // signed long
Printf long long int in C with GCC? - Stack Overflow
c - How to printf long long - Stack Overflow
Printing long value with sprintf
How to print a 64 bit long in 32 bit mode using printf?
Videos
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.
For portable code, the macros in inttypes.h may be used. They expand to the correct ones for the platform.
E.g. for 64 bit integer, the macro PRId64 can be used.
int64_t n = 7;
printf("n is %" PRId64 "\n", n);
%lld is the standard C99 way, but that doesn't work on the compiler that I'm using (mingw32-gcc v4.6.0). The way to do it on this compiler is: %I64d
So try this:
if(e%n==0)printf("%15I64d -> %1.16I64d\n",e, 4*pi);
and
scanf("%I64d", &n);
The only way I know of for doing this in a completely portable way is to use the defines in <inttypes.h>.
In your case, it would look like this:
scanf("%"SCNd64"", &n);
//...
if(e%n==0)printf("%15"PRId64" -> %1.16"PRId64"\n",e, 4*pi);
It really is very ugly... but at least it is portable.
- Your
scanf()statement needs to use%lldtoo. - Your loop does not have a terminating condition.
There are far too many parentheses and far too few spaces in the expression
pi += pow(-1.0, e) / (2.0*e + 1.0);- You add one on the first iteration of the loop, and thereafter zero to the value of 'pi'; this does not change the value much.
- You should use an explicit return type of
intformain(). - On the whole, it is best to specify
int main(void)when it ignores its arguments, though that is less of a categorical statement than the rest. - I dislike the explicit licence granted in C99 to omit the return from the end of
main()and don't use it myself; I writereturn 0;to be explicit.
I think the whole algorithm is dubious when written using long long; the data type probably should be more like long double (with %Lf for the scanf() format, and maybe %19.16Lf for the printf() formats.
Say i have 2 32 bit registers eax:edx that combined makes a 64 bit signed long. How can i print this using printf in base 10? Basically i need the 32 bit version of printf to interpret the sequence of bits as a 64 bit signed long.
I have tried to print the first half and the second half separately but the signed bit messes everything up. Any ideas? Don't need the exact solution.