* Bamboozled by long long
@ 2002-07-19 11:17 arijitg
2002-07-19 12:16 ` John Love-Jensen
0 siblings, 1 reply; 5+ messages in thread
From: arijitg @ 2002-07-19 11:17 UTC (permalink / raw)
To: gcc-help
I have the following 2 lines in a program
unsigned long long k=1;
unsigned int i = 4;
printf("The values are i: %d, , k: %lX, i+k: %lX \n", i,k , i+k);
k = k + i;
printf("The values are i: %d, , k: %lX, i+k: %lX \n", i,k , i+k);
I get the foll. outputs:
The values are i: 4, , k: 0, i+k: 1
The values are i: 4, , k: 0, i+k: 5
Note that (i+k) is showing k's current value, yet k is always showing
0.
Any idea please?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Bamboozled by long long
2002-07-19 11:17 Bamboozled by long long arijitg
@ 2002-07-19 12:16 ` John Love-Jensen
2002-07-19 12:29 ` Gokhan Kisacikoglu
2002-07-19 14:22 ` Leo Przybylski
0 siblings, 2 replies; 5+ messages in thread
From: John Love-Jensen @ 2002-07-19 12:16 UTC (permalink / raw)
To: arijitg, gcc-help
Your data types are not matching your formats, a %lX expects a long, not a
long long.
Do this:
unsigned long long k=1;
unsigned int i = 4;
printf("The values are i: %d, , k: %lX, i+k: %lX \n", i, (long)k,
(long)(i+k));
k = k + i;
printf("The values are i: %d, , k: %lX, i+k: %lX \n", i, (long)k,
(long)(i+k));
That'll fix the problem. If you have an appropriately modified/enhanced
"standard" C library, you may be able to do this...
unsigned long long k=1;
unsigned int i = 4;
printf("The values are i: %d, , k: %llX, i+k: %llX \n", i,k , i+k);
k = k + i;
printf("The values are i: %d, , k: %llX, i+k: %llX \n", i,k , i+k);
...or this...
unsigned long long k=1;
unsigned int i = 4;
printf("The values are i: %d, , k: %LX, i+k: %LX \n", i,k , i+k);
k = k + i;
printf("The values are i: %d, , k: %LX, i+k: %LX \n", i,k , i+k);
...depending on the extension (or convention for long long) used.
--Eljay
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Bamboozled by long long
2002-07-19 12:16 ` John Love-Jensen
@ 2002-07-19 12:29 ` Gokhan Kisacikoglu
2002-07-19 14:22 ` Leo Przybylski
1 sibling, 0 replies; 5+ messages in thread
From: Gokhan Kisacikoglu @ 2002-07-19 12:29 UTC (permalink / raw)
To: John Love-Jensen; +Cc: arijitg, gcc-help
> unsigned long long k=1;
> unsigned int i = 4;
> printf("The values are i: %d, , k: %LX, i+k: %LX \n", i,k , i+k);
> k = k + i;
> printf("The values are i: %d, , k: %LX, i+k: %LX \n", i,k , i+k);
>
> ...depending on the extension (or convention for long long) used.
>
> --Eljay
This is not correct, this will only work with double precision float
numbers. "long long" is really "long long int" and only ll should be
used. This is from the man page (on my system):
ll For n, the argument has type pointer to long long int; for
d
and i, long long int; and for o, u, x, and X, unsigned long
long int.
L For b, B, e, E, f, g, and G, the argument has type long
double.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Bamboozled by long long
2002-07-19 12:16 ` John Love-Jensen
2002-07-19 12:29 ` Gokhan Kisacikoglu
@ 2002-07-19 14:22 ` Leo Przybylski
1 sibling, 0 replies; 5+ messages in thread
From: Leo Przybylski @ 2002-07-19 14:22 UTC (permalink / raw)
To: John Love-Jensen; +Cc: arijitg, gcc-help
Doesn't make the code very portable does it?
-Leo
On Fri, 2002-07-19 at 12:14, John Love-Jensen wrote:
> Your data types are not matching your formats, a %lX expects a long, not a
> long long.
>
> Do this:
>
> unsigned long long k=1;
> unsigned int i = 4;
> printf("The values are i: %d, , k: %lX, i+k: %lX \n", i, (long)k,
> (long)(i+k));
> k = k + i;
> printf("The values are i: %d, , k: %lX, i+k: %lX \n", i, (long)k,
> (long)(i+k));
>
> That'll fix the problem. If you have an appropriately modified/enhanced
> "standard" C library, you may be able to do this...
>
> unsigned long long k=1;
> unsigned int i = 4;
> printf("The values are i: %d, , k: %llX, i+k: %llX \n", i,k , i+k);
> k = k + i;
> printf("The values are i: %d, , k: %llX, i+k: %llX \n", i,k , i+k);
>
> ...or this...
>
> unsigned long long k=1;
> unsigned int i = 4;
> printf("The values are i: %d, , k: %LX, i+k: %LX \n", i,k , i+k);
> k = k + i;
> printf("The values are i: %d, , k: %LX, i+k: %LX \n", i,k , i+k);
>
> ...depending on the extension (or convention for long long) used.
>
> --Eljay
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Bamboozled by long long
@ 2002-07-19 13:11 arijitg
0 siblings, 0 replies; 5+ messages in thread
From: arijitg @ 2002-07-19 13:11 UTC (permalink / raw)
To: gcc-help
I tried running it on my system with both options.
llx works while LX does not.
My system is sunOS5.7 just in case.
thanks guys for the prompt and right response. :)
---------Included Message----------
>Date: Fri, 19 Jul 2002 12:31:23 -0700
>From: "Gokhan Kisacikoglu" <kisa@centropolisfx.com>
>Reply-To: <kisa@centropolisfx.com>
>To: "John Love-Jensen" <eljay@adobe.com>
>Cc: <arijitg@uci.edu>, <gcc-help@gcc.gnu.org>
>Subject: Re: Bamboozled by long long
>
>
>> unsigned long long k=1;
>> unsigned int i = 4;
>> printf("The values are i: %d, , k: %LX, i+k: %LX \n", i,k , i+k);
>> k = k + i;
>> printf("The values are i: %d, , k: %LX, i+k: %LX \n", i,k , i+k);
>>
>> ...depending on the extension (or convention for long long) used.
>>
>> --Eljay
>
>
>This is not correct, this will only work with double precision float
>numbers. "long long" is really "long long int" and only ll should be
>used. This is from the man page (on my system):
>
> ll For n, the argument has type pointer to long long int;
for
>d
> and i, long long int; and for o, u, x, and X, unsigned
long
> long int.
>
> L For b, B, e, E, f, g, and G, the argument has type long
> double.
>
>
---------End of Included Message----------
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2002-07-19 21:22 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-19 11:17 Bamboozled by long long arijitg
2002-07-19 12:16 ` John Love-Jensen
2002-07-19 12:29 ` Gokhan Kisacikoglu
2002-07-19 14:22 ` Leo Przybylski
2002-07-19 13:11 arijitg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).