Walt Mankowski on 6 Dec 2003 12:01:02 -0500


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

Re: [PLUG] printf and unsigned long


On Sat, Dec 06, 2003 at 11:41:52AM -0500, Jeff Abrahamson wrote:
> What am I doing wrong here?

2 things:

1.  Use %lu to get printf to print unsigned longs.

2.  You've got UL in both printf statements.  I assume you meant to
    put UL2 in the second one.

> 
>   /*  int.c  */
>   #include <stdio.h>
> 
>   int
>   main(int argc,
>        char *argv[])
>   {
> 	  signed long SL;
> 	  unsigned long UL, UL2;
> 
> 	  SL = -1;
> 	  UL = (unsigned long)SL;
> 	  printf("SL=%ld, UL=%ld\n", SL, UL);

          printf("SL=%ld, UL=%lu\n", SL, UL);
> 
> 	  UL2 = ~0;
> 	  printf("SL=%ld, UL=%ld\n", SL, UL);

          printf("SL=%ld, UL=%lu\n", SL, UL2);

> 
> 	  return 0;
>   }

Attachment: pgpNHD9y6xoZi.pgp
Description: PGP signature