Can Datatype of a Value be determined by Examining its Bits in C?

Question: Can datatype of a value be determined by Examining its Bits in C

Answer: Let’s first discuss how values either characters, integers, floats, doubles, character strings, addresses etc. are stored in computer memory. Computer Memory is Organised as continuous sequence of Binary Bits which are 1s or 0s for On and Off respectively. Because these bits serve no purpose in storing larger values when if used individually so these are combined into groups, each of 8 bits, called bytes. Further, compiler, on Linux system, allocates different amount of storage for each specific type of value. For instance, the smallest group being a Byte, comprising of 8-bits, suitable for type char, 4-Bytes group for type int etc. sizeof operator in C can be used to determine amount of storage space for different types including pointer variables. For example:

 * size_of_diff_types.c -- Program uses sizeof operator to determine size 
 * of different types
#include <stdio.h>
int main(void)
    char *cp, ch = 'A';      /* a character pointer */
    int *ip, i = 23;         /* an integer pointer */
    float *fp, fl = 23.00;   /* a float pointer */
    double *dp, dbl = 23.00; /* a double pointer */
    printf("\nSize of variable:\n\tcharacter ch is %d bytes\n\tinteger i is"
           " %d bytes\n\tfloat fl is %d bytes\n\tdouble dbl is %d bytes\n", 
           sizeof(ch), sizeof(i), sizeof(fl), sizeof(dbl));
    printf("\nSize of All:\n\tCharacters is %d bytes\n\tIntegers is %d "
           "bytes\n\tFloats or Reals are %d bytes\n\tDoubles are %d bytes"
           "\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));
    printf("\nSize of A/An:\n\tCharacter Pointer is %d bytes\n\tInteger "
           "Pointer is %d bytes\n\tFloat Pointer is %d bytes\n\tDouble "
           "Pointer is %d bytes\n", 
            sizeof cp, sizeof ip, sizeof fp, sizeof dp);
    return 0;

When above program is run, output produced as follows:

Size of:
	character ch is 1 bytes
	integer i is 4 bytes
	float fl is 4 bytes
	double dbl is 8 bytes
Size of All:
	Characters is 1 bytes
	Integers is 4 bytes
	Floats or Real are 4 bytes
	Doubles are 8 bytes
Size of A/An:
	Character Pointer is 8 bytes
	Integer Pointer is 8 bytes
	Float Pointer is 8 bytes
	Double Pointer is 8 bytes

We observed different types take different amount of storage. However, notice that pointer of any type is allocated fixed 8 Bytes. Now, we try to recognise type of a value represented in binary, for example:

    /* spaces between bytes are put for clarity */
    11100111 01101100 11110000 00001111

With the above given sequence of bytes we dive into problem of correctly interpreting the value when its type isn’t known. Though, We can attempt this value as four characters or a single integer value or two short integers or some character string or address or some other value unless we know the type of value.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.