Re: [Vala] type MIN and MAX



* Alessandro Pellizzari <alex amiran it> wrote:
Hi all,

today I stumbled upon a strange behaviour. I would like to know if it is
expected or not:

using GLib;
public class Main : Object {
 static int main (string[] args) {
  stdout.printf ("uint.min    => %i\n", uint.MIN);
  stdout.printf ("uint.max    => %i\n", uint.MAX);
  stdout.printf ("ulong.min   => %i\n", ulong.MIN);
  stdout.printf ("ulong.max   => %i\n", ulong.MAX);
  stdout.printf ("uint32.min  => %i\n", uint32.MIN);
  stdout.printf ("uint32.max  => %i\n", uint32.MAX);
  stdout.printf ("int64.min   => %i\n", int64.MIN);
  stdout.printf ("int64.max   => %i\n", int64.MAX);
  stdout.printf ("uint64.min  => %i\n", uint64.MIN);
  stdout.printf ("uint64.max  => %i\n", uint64.MAX);
 }
}

This code gives:

uint.min   => 0
uint.max   => -1
ulong.min  => 0
ulong.max  => -1
uint32.min => 0
uint32.max => -1
int64.min  => 0
int64.max  => -1
uint64.min => 0
uint64.max => -1

The other values are OK and showing expected boundaries.
I am using vala 0.1.7

That is because %i is just a regular `int'. You should use the following
code:

| using GLib;
| public class Main : Object {
|  static int main (string[] args) {
|   stdout.printf ("uint.min    => %u\n", uint.MIN);
|   stdout.printf ("uint.max    => %u\n", uint.MAX);
|   stdout.printf ("ulong.min   => %lu\n", ulong.MIN);
|   stdout.printf ("ulong.max   => %lu\n", ulong.MAX);
|   stdout.printf ("uint32.min  => %lu\n", uint32.MIN);
|   stdout.printf ("uint32.max  => %lu\n", uint32.MAX);
|   stdout.printf ("int64.min   => %lld\n", int64.MIN);
|   stdout.printf ("int64.max   => %lld\n", int64.MAX);
|   stdout.printf ("uint64.min  => %llu\n", uint64.MIN);
|   stdout.printf ("uint64.max  => %llu\n", uint64.MAX);
|  }
| }

Output:

| uint.min    => 0
| uint.max    => 4294967295
| ulong.min   => 0
| ulong.max   => 4294967295
| uint32.min  => 0
| uint32.max  => 4294967295
| int64.min   => -9223372036854775808
| int64.max   => 9223372036854775807
| uint64.min  => 0
| uint64.max  => 18446744073709551615

Be sure to read `man 3 printf'.

-- 
 Ed Schouten <ed fxq nl>
 WWW: http://g-rave.nl/

Attachment: pgpoXrrxKKXg1.pgp
Description: PGP signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]