0

In the book "The C Language" , the only restriction on the integral data types is that

16 bits <= size of short <= size of int <= size of long ; 32bits <= size of long

(Edit: FINE, i edited the constraint to remove the confusion with the std function)

and in another book (Programming in ANSI C - Balaguruswamy) , its given that the sizeof(short) in a 16 bit machine is 8 bits (half of the size of ints which are 16 bits). Isn't this contradicting with the above given constraint?

Bala Kumar
  • 355
  • 2
  • 4
  • 12
  • 5
    At least for standard C, `short` must be at least 16 bits. – Jerry Coffin Oct 02 '16 at 05:39
  • Exactly, so is the latter book wrong? – Bala Kumar Oct 02 '16 at 05:40
  • 1
    Unfortunately, some books have errors. Also, some companies produce compilers that they say compile C code, but really can't because the compiler has so many deviations from the C specification (DSPs in particular come to mind as platforms where this sin is popular). – Cornstalks Oct 02 '16 at 05:41
  • Any book saying a `short` is less than 16 bits is either wrong, or talking about some language other than standard C. – Jerry Coffin Oct 02 '16 at 05:43
  • 2
    @BalaK: It is either flat out wrong or *outdated*. Standard C does not allow 8-bit `short`. But pre-standard C might have. – AnT stands with Russia Oct 02 '16 at 05:43
  • Sizeof gives the size in bytes, not bits, so as stated, neither assertion is right. – n. m. could be an AI Oct 02 '16 at 05:46
  • 1
    @n.m 'sizeof' isnt referred here as a function but merely as a shorthand or notation for size of that data type. :P – Bala Kumar Oct 02 '16 at 05:47
  • So what's the title and author of the "other" book? And when was it published? My recollection (foggy and hazy though it may be) of programming DOS PC's BackInTheDay (tm), and prior to the first ANSI standard for C, was that int was 16 bits, short was 16 bits, long was 32 bits, and char was 8 bits across all the compilers I used back then (Microsoft, Datalight, Zortech, Borland). Your mileage and compiler might vary from that. – Bob Jarvis - Слава Україні Oct 02 '16 at 05:49
  • Actually, arranging integer types in terms of "amount of storage" they occupy (`sizeof`) is a C++ thing. C language never made such claim. C arranges these types in terms of their *ranges*. I.e. range of `short` must be a subrange of `int` and so on. Pedantically speaking, C does not even seem to require `sizeof(short) <= sizeof(int)`. It is known as on of those strange and very pedantic differences between C and C++. – AnT stands with Russia Oct 02 '16 at 05:51
  • Re-edited to add the book's title. I'm reading the latest edition released not more than a few months ago. – Bala Kumar Oct 02 '16 at 05:52
  • Time to throw the book away. If it gets that wrong, it probably has other more serious errors in it too. And you won't know when it is misleading you because you don't know enough C yet. (And if it is written by your course tutor, it might be time to ask for a refund and find someone else who can teach it). – Jonathan Leffler Oct 02 '16 at 05:59
  • @JohnZwinck The question is about C and not C++. Even though standards might not differ substantially on this matter, they are completely different languages with separate standards. SO uses a tag system. If a question asks about `c` the dup should be market with at least the same tag. In the future please close with the correct tag. Thank you. – 2501 Oct 02 '16 at 06:58
  • I have to remind everyone again the difference between width and size. Width is directly related to range but size could be padded arbitrarily larger than width. Also note that standard c uses neither to regulate char/short/int/long/long long because the representation is implementation defined, so is the relationship between range and width. Standard only specify the range those types can hold, without restricting how many bits they use or bytes to store them. – user3528438 Oct 02 '16 at 12:28

0 Answers0