1. 9
  1.  

  2. 2

    How does a C compiler know the size of its data types? Integer data types are defined in the limits.h file.

    Minor quibble. I realize we’re simplifying things, but that is almost backwards. limits.h tells your program what size the compiler thinks things are. You cannot, for instance, change that file and expect the compiler to update itself. You’ll just get terribly wrong programs.

    1. 2

      Right, and there was a comment to that effect. Of course, changing the system (/usr/include) header files and expecting things to work is crazy, but that might not be obvious to someone new to C.

    2. 1

      Very weird and unnecessary complicated way. What about

      long x; int y; x = y; y = x; //assume their sizes are different
      

      Not to mention values can be in registers (and they may not be of the same size).

      1. 1

        I think that’s covered. I usually try to get people to diagram memory in boxes, so hopefully they’ll see the boxes are different sizes and comprehend the dangers of truncation.

        I wouldn’t worry about registers. They’re not relevant to understanding how a C program operates. The abstract machine in the C standard is a little vague, so modeling your program as a concrete machine with a stack and a heap is a useful clarification, but I would always study a program’s behavior in the context of a pure memory architecture.