Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any guaranteed minimum sizes for types in C?

Can you generally make any assumptions about the minimum size of a data type?

What I have read so far:

  • char: 1 Byte
  • short: 2 Byte
  • int: 2 Byte, typically 4 Byte
  • long: 4 Byte

float??? double???

Are the values in float.h and limits.h system dependent?

like image 841
Trollhorn Avatar asked Nov 15 '09 19:11

Trollhorn


People also ask

What is the size of char in C?

Char Size. The size of both unsigned and signed char is 1 byte always, irrespective of what compiler we use.

Is char always 1 byte?

"char" has always been a misspelling of "byte" in C. sizeof(char) has to be 1, but char doesn't have to be 1 byte in size. It's more correct to say that sizeof(foo) returns a result relative to sizeof(char).

What are the basic data type in C?

Types of Data Types in CFloating-point, integer, double, character. Union, structure, array, etc. The basic data types are also known as the primary data types in C programming.

What is range of long int in C?

0 to 65,535. long. 4. long int , signed long int. -2,147,483,648 to 2,147,483,647.


2 Answers

Nine years and still no direct answer about the minimum size for float, double, long double.


Any guaranteed minimum sizes for types in C?

For floating point type ...

From a practical point-of-view, float minimum size is 32-bits and double is 64- bits. C allows double and long double to share similar characteristics, so a long double could be as small as a double: Example1 or 80-bit or 128-bit or ...

I could imagine a C compliant 48-bit double may have existed – yet do not know of any.


Now, let us imagine our rich uncle dies and left us a fortune to pay for the development and cultural promotion for www.smallest_C_float.com.

C specifies:

  1. float finite range is at least [1E-37… 1E+37]. See FLT_MIN, FLT_MAX

  2. (1.0f + FLT_EPSILON) – 1.0f <= 1E-5.

  3. float supports positive and negative values.

    Let X: Digit 1-9 Let Y: Digit 0-9 Let E: value -37 to 36 Let S: + or - Let b: 0 or 1

Our float could minimally represent all the combinations, using base 10, of SX.YYYYY*10^E.

0.0 and ±1E+37 are also needed (3 more). We do not need -0.0, sub-normals, ±infinity nor not-a-numbers.

That is 2910^5*74 + 3 combinations or 133,200,003 which needs at least 27 bits to encode - somehow. Recall the goal is minimal size.

With a classic base 2 approach, we can assume an implied 1 and get S1.bbbb_bbbb_bbbb_bbbb_b2^e or 22^17*226 combinations or 26 bits.

If we try base 16, we then need about 21516^(4 or 5)*57 combinations or at least 26 to 30 bits.

Conclusion: A C float needs at least 26 bits of encoding.


A C’s double need not express a greater exponential range than float, it only has a different minimal precision requirement. 1E-9.

S1.bbbb_bbbb_bbbb_bbbb_ bbbb_ bbbb_ bbbb_bb2^e --> 22^30*226 combinations or 39 bits.


On our imagine-if-you-will computer, we could have a 13-bit char and so encode float, double, long double without padding. Thus we can realize a non-padded 26-bit float and 39-bit double, long double.


1: Microsoft Visual C++ for x86, which makes long double a synonym for double


[Edit] 2020

Additional double requirements may require 41 bits. May have to use 42-bit double and 28-bit float. Will need to review. Uncle will not be happy.

like image 72
chux - Reinstate Monica Avatar answered Oct 04 '22 19:10

chux - Reinstate Monica


Yes, the values in float.h and limits.h are system dependent. You should never make assumptions about the width of a type, but the standard does lay down some minimums. See §6.2.5 and §5.2.4.2.1 in the C99 standard.

For example, the standard only says that a char should be large enough to hold every character in the execution character set. It doesn't say how wide it is.

For the floating-point case, the standard hints at the order in which the widths of the types are given:

§6.2.5.10

There are three real floating types, designated as float, double, and long double. 32) The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double.

They implicitly defined which is wider than the other, but not specifically how wide they are. "Subset" itself is vague, because a long double can have the exact same range of a double and satisfy this clause.

This is pretty typical of how C goes, and a lot is left to each individual environment. You can't assume, you have to ask the compiler.

like image 31
Jed Smith Avatar answered Oct 04 '22 18:10

Jed Smith