Also read - Bit stuffing program in c
sizeof() Operator in C
Sizeof is a quite frequently used operator in the C. It is a compile-time unary operator that can be used to determine the sizes of the data types or the expressions specified in the char-size storage units.
Syntax
sizeof(Expression);
Example
int main()
{
printf("%lu\n", sizeof(char));
return 0;
}
Output
1
Explanation: To compute the size of any data type we simply use the sizeof() operator as explained above.
Char Data Type
A “char’ data type is one of the primitive data types that is used to store single characters such as alphabets, special characters, or even numbers(considered as characters). It occupies a single byte of memory while storage. It may be of two types; signed or unsigned.
Signed char and unsigned char both are used to store a single character in the variable. The variable stores the ASCII value of the characters. For example if ‘B’ is stored, actually, it will hold 66. For signed char, we do not write the signed keyword but for unsigned, we need to mention the keywords as:
unsigned char g =’n’
Both the Signed and Unsigned chars are 8 bits. So for signed char, can store values from -128 to +127, and the unsigned char will store 0 to 255.
Also see, Floyd's Triangle in C
Size of Char in 32-bit Compiler
It typically occupies 8 bits or 1 byte of memory. In a 32-bit compiler, where the basic block size in a 32-bit compiler is 4 bytes (32 bits), char occupies only 1 byte (8 bits).
sizeof(char) is always 1. The 'block size' by which many people get confused is just the native word size of the machine - usually, the size that will result in the most efficient operation. Your computer can still address each byte individually - that's what an operator's size tells you about.
When you do sizeof(double), it returns 8 to tell you that a double is 8 bytes on your machine. Sometimes the structure is longer than it should be because the compiler is adding padding to the structure to keep everything aligned.
Char is of size 1 since that's the smallest access size your computer can handle - for most machines, an 8-bit value. The sizeof() operator gives the size of all other quantities in units of how many char objects would be the same size as whatever you asked about.
Size of Char in a Structure
One or multiple empty bytes (addresses) are inserted (or are left empty) between memory addresses allocated for other structure members during memory allocation to align the data in memory. This concept is called structure padding.
A computer processor's architecture is such that it can read 1 word (4 bytes in a 32-bit processor) from memory at a time. To use this advantage of the processor, data are always aligned as 4 bytes package, which leads to inserting empty addresses between other members’ addresses. Because of this structure padding concept in C, the structure's size is not always the same as what we think.
For example, please consider the below structure that has 5 members.
struct student
{
int i;
int j;
char c;
char d;
float dec;
};
As per C concepts, int and float data types occupy 4 bytes each, and the char data type occupies 1 byte for the 32-bit processor.
As there are two integers, two char variables, and a float variable, only 14 bytes should be allocated for the above structure. But this is not the case as the compiler adds some extra padding to the structure for proper memory alignment and hence memory used by the structure is greater than it should have been.
How Char is Stored in Computer Memory?
If we want to store a char variable in a computer, its corresponding ASCII value will be stored in the memory after getting converted to a binary value. For example, if a char ‘C’ is to be stored in the computer, the corresponding ASCII value (67 in this case) will be stored in the computer.
To store a character variable, the computer will allocate 1 byte (8-bit) memory.
67 will be converted into binary form which is (1000011) 2. Because the computer knows only the binary number system.
Then 1000011 will be stored in 8-bit memory.
Also see, Tribonacci Series and Short int in C Programming
Frequently Asked Questions
Is there any difference between the size of the signed or unsigned char variable?
The size of both unsigned and signed char is always 1 byte, irrespective of what compiler we use. Unsigned characters have values from 0 to 255 while others have -128 to 127.
Can I perform arithmetic operations on "char" variables?
Since a "char" is internally represented as an ASCII value, we can perform arithmetic operations on "char" variables. The operations like addition, subtraction, division, and multiplication are performed in it.
Can I reduce the size of the structure to only “char” members?
By arranging the members of a structure in the decreasing order of their size, with the help of this method, we can minimize the padding inserted in a structure by the compiler.
Conclusion
In this article, we learned about the data type size of char in a 32-bit GCC compiler. We also addressed the most prevalent confusion regarding the padding in a compiler while using structures.
To learn more, you can go through the following articles.
Enhance your skills in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and more with our Coding Ninjas Studio Guided Path. If you want to sharpen your coding skills for the test, check out the mock test series and enter the contests on Coding Ninjas Studio!
Check out The Interview Guide for Product Based Companies and some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc., on Coding Ninjas Studio.