What are the byte values of basic data types : char , int , long , long long, float, double.?(using sizeof() )?

Recommended Answers

All 2 Replies

My question is: Why not write a little code to find out for your choice of compiler and system?

Neither C nor C++ defines specifics sizes for those data types. There are certain minimum requirements. A char is required to have at least 8 bits. Short integers require at least 16 bits, normal and long ints must be at least 32 bits and a long long integer must be at least 64 bits. There are no maximum sizes for any of those.

The sizeof operator returns the size of an object in "bytes", but the language specifications don't specify how big a byte is. The C11 standard, for example, describes "byte" as, "A byte is composed of a contiguous sequence of bits, the number of which is implementation defined." It's also described as "an addressable unit of data storage", meaning that each byte has it's own unique memory address. Whatever the "byte" size is, the sizeof operator returns the number of those units needed to store an object or an instance of a given type. And since a char is defined to be one "byte", that means that sizeof (char) is guaranteed to be 1.

It is required that the different integral type sizes be ordered. 1 == sizeof (char) <= sizeof (short) <= sizeof (int) <= sizeof (long) <= sizeof (long long) is guaranteed, and the sizes of the unsigned types are the same as the corrsponding signed types. But some implementations today have 64 bit long ints even though most 32 and 64-bit implemementations have used 32 bits for both int an long for decades. Also, some supercomputers have had little support for character processing, and only address whole words of memory. So you could have a fully conformant implementation of either C or C++ where all of those types have size "1 byte", on a word-addressed machine with 64 bit words.

For your homework, look at the values in the specific compiler you need to report on. For C, the headers are <limits.h> for the character and integer types; and <float.h> for the floating point types. The corresponding C++ headers are <climits> and <cfloat>. These contain macro constants defining the maximum and minimum values of each type (plus some other details. To find the number of bits of an integral type, take the base-2 logarithm of the unsigned maximum type and round up to a whole number. Divide that by the value of CHAR_BIT to get the size in C/C++ "bytes".

commented: You should get credit for doing this homework for them. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.