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); printf("\n"); 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.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Computer Science Internship
- Buy C Books
- Practice Computer Science MCQs
- Apply for C Internship
- Practice BCA MCQs