Does the size of various datatypes in C depend on the compiler? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Does the size of various datatypes in C depend on the compiler?

C programming language

6th Jun 2021, 1:11 PM
Samruddhi C R
6 Answers
+ 3
Calvin Thomas Well, besides the integral types, there are only float and double (and long double). They also have minimal size requirements, as well as the restriction that a double has to be able to represent all float values, and a long double in turn has to be at least as precise as a double. See the table at the end: https://en.cppreference.com/w/c/language/arithmetic_types But I think those are also only minimal guarantees, i.e. they may be wider: https://stackoverflow.com/questions/25524355/what-is-the-size-of-float-and-double-in-c-and-c Not taking into account any floating point norms like IEEE-754 which usual implementations will likely conform to (see also first link). In such a case, you would have more certainty of their properties.
6th Jun 2021, 4:08 PM
Shadow
Shadow - avatar
+ 7
Yes, it depends on the compiler, which will try to find the best combination for your platform (architecture + OS). That is why C and C++ have a sizeof() operator to determine the size of a data type. The standard itself does not make many assumptions about the size of integral data types, it only specifies the following: 1 == sizeof( char ) <= sizeof( short ) <= sizeof( int ) <= sizeof( long ) As well as a minimal range of values each type needs to be able to store. In particular, it is not even guaranteed char is actually 8 bit wide, which is why the macro CHAR_BIT exists. All of these loose rules make it possible to implement C on a variety of platforms. More details: https://stackoverflow.com/questions/13764892/what-determines-the-size-of-integer-in-c https://stackoverflow.com/questions/2331751/does-the-size-of-an-int-depend-on-the-compiler-and-or-processor https://stackoverflow.com/questions/35844586/can-i-assume-the-size-of-long-int-is-always-4-bytes
6th Jun 2021, 1:57 PM
Shadow
Shadow - avatar
+ 1
Shadow and Martin Taylor Thanks.
7th Jun 2021, 5:07 AM
Calvin Thomas
Calvin Thomas - avatar
0
Shadow Does this apply to all the primitive data types in C?
6th Jun 2021, 2:22 PM
Calvin Thomas
Calvin Thomas - avatar
0
Yes,it depends on compiler
8th Jun 2021, 8:47 AM
Bathini Swapna
Bathini Swapna - avatar