[ILUG] sprintf formatting question

Phil philb at vodafone.ie
Wed Aug 20 13:56:09 IST 2008


Hi,

I thought this one was obvious but seemingly not. I have a value, in 
excess of 2^32 that I want to print out using perl (ancient RedHat 8.0 
on 32 bit). The output should be padded to 10 characters.

First test works fine:
my $foo = 3188931580; # > 2147483648
print sprintf("%10u\n", $foo);

Second test, fails due to overflow
my $foo = 5188931580; # > 4294967295
print sprintf("%10u\n", $foo);

(actually prints out 4294967295)


My understanding was that a prefix of "L" or "ll" would indicate to 
sprintf that the value is a "long long" so that any of the following 
should work:

print sprintf("%10lld\n", $foo);	# Prints -1
print sprintf("%10llu\n", $foo);	# Print 4294967295
print sprintf("%10Ld\n", $foo);		# Prints -1
print sprintf("%10Lu\n", $foo);		# Prints 4294967295

Have I misunderstood the sprintf syntax or is this a limitation of perl 
on 32 bit?

Thanks,
  -Phil







More information about the ILUG mailing list