On 03/24/18 21:27, Mike Burns wrote:
On 2018-03-24 21.05.56 -0500, Edgar Pettijohn wrote:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int
main(void)
{
uint32_t n = arc4random();
printf("arc4random %d\n", n);
printf("abs %d\n", abs(n));
return 0;
}
Which is well and good. However, I get the following output.
laptop$ ./a.out
arc4random 247165650
abs 247165650
laptop$ ./a.out
arc4random 2012715611
abs 2012715611
laptop$ ./a.out
arc4random 222644175
abs 222644175
laptop$ ./a.out
arc4random -843593186
abs 843593186
// -----
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <math.h>
int
main(void)
{
uint32_t n = arc4random();
printf("arc4random %"PRIu32"\n", n);
printf("arc4random with %%d %d\n", n);
printf("abs %d\n", abs(n));
return 0;
}
//
output:
~% ./a.out
arc4random 1647540484
arc4random with %d 1647540484
abs 1647540484
~% ./a.out
arc4random 3569752639
arc4random with %d -725214657
abs 725214657
More docs:
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/inttypes.h.html
At first Basic C didn't seem like a good answer, but I went back to the
printf() manual and found the miraculous %u. Which seems to have fixed
it as well. Thanks for this alternate.