Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

size guarantee for integral/arithmetic types in C and C++

I know that the C++ standard explicitly guarantees the size of only char, signed char and unsigned char. Also it gives guarantees that, say, short is at least as big as char, int as big as short etc. But no explicit guarantees about absolute value of, say, sizeof(int). This was the info in my head and I lived happily with it. Some time ago, however, I came across a comment in SO (can't find it) that in C long is guaranteed to be at least 4 bytes, and that requirement is "inherited" by C++. Is that the case? If so, what other implicit guarantees do we have for the sizes of arithmetic types in C++? Please note that I am absolutely not interested in practical guarantees across different platforms in this question, just theoretical ones.

like image 247
Armen Tsirunyan Avatar asked Nov 03 '10 21:11

Armen Tsirunyan


People also ask

What are integral types in C?

The integral types are: char , signed char , unsigned char -8 bits. short int , signed short int , and unsigned short int -16 bits.

How many types of arithmetic are there in C++?

In simple terms, arithmetic operators are used to perform arithmetic operations on variables and data; they follow the same relationship between an operator and an operand. C++ Arithmetic operators are of 2 types: Unary Arithmetic Operator. Binary Arithmetic Operator.

What is an arithmetic type?

The arithmetic type specifiers are built up from the following keywords: void , char , int , float and double , together with the prefixes short , long , signed and unsigned . From these keywords you can build both integral and floating-point types.

What is meant by integral types?

A type is integral if it is a type of integer; it is impossible to define your own integer type, therefore the types of integer are sbyte, byte, short, ushort, int, uint, long, ulong.


2 Answers

18.2.2 guarantees that <climits> has the same contents as the C library header <limits.h>.

The ISO C90 standard is tricky to get hold of, which is a shame considering that C++ relies on it, but the section "Numerical limits" (numbered 2.2.4.2 in a random draft I tracked down on one occasion and have lying around) gives minimum values for the INT_MAX etc. constants in <limits.h>. For example ULONG_MAX must be at least 4294967295, from which we deduce that the width of long is at least 32 bits.

There are similar restrictions in the C99 standard, but of course those aren't the ones referenced by C++03.

This does not guarantee that long is at least 4 bytes, since in C and C++ "byte" is basically defined to mean "char", and it is not guaranteed that CHAR_BIT is 8 in C or C++. CHAR_BIT == 8 is guaranteed by both POSIX and Windows.

like image 56
Steve Jessop Avatar answered Oct 13 '22 02:10

Steve Jessop


Don't know about C++. In C you have


                                  Annex E
                              (informative)


                          Implementation limits

       [#1]  The contents of the header  are given below,
       in alphabetical order.  The minimum magnitudes  shown  shall
       be  replaced  by  implementation-defined magnitudes with the
       same sign.  The values shall  all  be  constant  expressions
       suitable  for  use  in  #if  preprocessing  directives.  The
       components are described further in 5.2.4.2.1.

               #define CHAR_BIT                         8
               #define CHAR_MAX    UCHAR_MAX or SCHAR_MAX
               #define CHAR_MIN            0 or SCHAR_MIN
               #define INT_MAX                     +32767
               #define INT_MIN                     -32767
               #define LONG_MAX               +2147483647
               #define LONG_MIN               -2147483647
               #define LLONG_MAX     +9223372036854775807
               #define LLONG_MIN     -9223372036854775807
               #define MB_LEN_MAX                       1
               #define SCHAR_MAX                     +127
               #define SCHAR_MIN                     -127
               #define SHRT_MAX                    +32767
               #define SHRT_MIN                    -32767
               #define UCHAR_MAX                      255
               #define USHRT_MAX                    65535
               #define UINT_MAX                     65535
               #define ULONG_MAX               4294967295
               #define ULLONG_MAX    18446744073709551615

So char <= short <= int <= long <= long long

and

CHAR_BIT * sizeof (char) >= 8
CHAR_BIT * sizeof (short) >= 16
CHAR_BIT * size of (int) >= 16
CHAR_BIT * sizeof (long) >= 32
CHAR_BIT * sizeof (long long) >= 64

like image 21
pmg Avatar answered Oct 13 '22 01:10

pmg