[chbot] Pop quiz C

Volker Kuhlmann list0570 at paradise.net.nz
Thu Apr 27 06:40:44 BST 2017


On Wed 26 Apr 2017 16:31:06 NZST +1200, Michael Field wrote:

> Sorry Volker - till I hit the problem I agreed with with you, but
> Paul is indeed correct - it is undefined and the compiler and
> hardware can do whatever the heck they want, and they don't even
> have to agree about what they do when on a given platform.

Holy ... annoying C language. Is that officially undefined?

The default appears to be C90 and there is no long long, but on a 64bit
intel

#include <stdio.h>
int main(int argc, char *argv[])
{
  int a;
  long b;
  int n0 = 34;
  a = 0xFFFFFFFF;
  a = a << n0;
  printf("%08X\n",a);
  b = 0xFFFFFFFF << n0;
  printf("%016lX\n",b);
  return 0;  // required, avoids warning
}

gcc -o check check.c -Wall -pedantic

gcc -o check check.c -Wall -Wextra -pedantic -Wno-unused-parameter

both the above compiler runs give

FFFFFFFC
00000000FFFFFFFC

as you say. Adding -O or -O1 gives the expected result.

Volker

-- 
Volker Kuhlmann
http://volker.top.geek.nz/	Please do not CC list postings to me.



More information about the Chchrobotics mailing list