Do the sizes of data types in C such as char, short, and int have specified minimum size or relative in the C standard?
The size of a data type in C.
size of basic data types in C - Stack Overflow
Which is the size of an int in ESP32?
int is 32 bit, -2,147,483,648 to 2,147,483,647. Enough for almost 25 days worth of milliseconds (or almost 50 days if unsigned)
long is 64 bit (-9223372036854775808 to 9223372036854775807). I don't know the performance impact of 64 bit operations on the ESP32, but that'd give you almost 300 million years worth of millisecond ticks.
Although, if you just want a millisecond counter, I suggest using the "millis()" function: returns the number of milliseconds since it was powered on.
More on reddit.comWhy do we use the floating data types?
What differentiates the range for unsigned and signed types?
Videos
So, I ask this question because in my C class at college, my professor said that in the C standard, only relative size of char short and int are specified to keep the language flexible to any hardware architecture setup. sizeof char <= short <= int. ChatGPT seems to have confirmed this saying:
Relative Size: In C, data types such as
char,short,int, andlongare defined in terms of their relative sizes. The C standard specifies that:However, the absolute size (in terms of number of bits or bytes) of these types is not fixed by the C standard. For example, the size of anintorshortcan vary depending on the architecture or the compiler. This flexibility allows C to work across different systems.
sizeof(char)must always be 1 byte (this is guaranteed),
sizeof(short)must be at least as large assizeof(char),
sizeof(int)must be at least as large assizeof(short),
sizeof(long)must be at least as large assizeof(int).
Absolute Size: The actual size in terms of bits can vary by platform. For example, on many modern systems:
charis usually 8 bits,
shortis often 16 bits,
intcan be 16 or 32 bits (commonly 32 bits nowadays),
longcan be 32 or 64 bits (often 64 bits on modern systems).
Technical Point from Your Professor: Your professor is correct in saying that, technically, all of these types could be 9 bits, as long as the relative size constraints are followed. This would be unusual, but C is designed to be flexible and portable across different architectures. The key is that the relative sizes of types must adhere to the standard's requirements, but the absolute size of the types may vary.
However, on the Wikipedia page for C data types:
The actual size of the integer types varies by implementation. The standard requires only size relations between the data types and minimum sizes for each data type:
The relation requirements are that the
long longis not smaller than long, which is not smaller than int, which is not smaller than short. As char's size is always the minimum supported data type, no other data types (except bit-fields) can be smaller.
The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and
long longmust contain at least 64 bits.
Can someone tell what is correct and perhaps provide a definitive source? Maybe I am misunderstanding something or my professor is working with outdated information.
When I check the size of a variable such as an int I get 4 regardless of the value I've assiged:
int a=1234567;
sizeof(int)==4;
I cannot understand what is this size. I expect to have 7 bytes because each character has 1 byte. Can someone explain?
My question is if I were to run the same program on a 32-bit machine will I see different results
Maybe. Or maybe not.
Or in other words does the size of the basic data types depend on 1) processor 2) Operating System 3) anything else
- Yes, 2. yes, 3. yes, for example if you run a 32-bit app in 32-bit compatibility mode on a 64-bit OS, then it most likely will use a 32-bit word size (of course, it was compiled like that). Oh, and yes, it may depend on the compiler too.
"And your compiler flags..." (Thanks, Kay!)
If you care about the exact size of the variables use
#include <stdint.h>
And then use the fixed-width types defined there:
uint8_t
uint16_t
uint32_t
uint64_t
or their signed cousins
int8_t
int16_t
int32_t
int64_t
Do not rely on the sizes of native types in C. Different compilers have different rules.