Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Data Types in C language?
3.
 
4.
 
5.
 
6.
 
7.
sizeof() Operator in C
7.1.
Syntax
7.2.
Char Data Type
8.
Size of Char in 32-bit Compiler
9.
Size of Char in a Structure
10.
How Char is Stored in Computer Memory?
11.
Frequently Asked Questions
11.1.
Is there any difference between the size of the signed or unsigned char variable?
11.2.
Can I perform arithmetic operations on "char" variables?
11.3.
Can I reduce the size of the structure to only “char” members?
12.
Conclusion
Last Updated: Mar 27, 2024
Easy

Size of Char in 32-Bit Compiler

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In a 32-bit compiler, the data type ‘char’ size is usually confused with structure padding. This data type is designed to store single characters, such as any alphabet, special character, or integer (treated as a character). It is the smallest data type in most modern computer architectures.

Size of Char in 32-Bit Compiler

In this article, we will learn about the size of char in 32-bit compiler.

What are Data Types in C language?

As the name suggests, data types refer to the data we use in a C program. Whenever we utilize a data type in a C program, we define its variables or functions. The data type is a collection of values with fixed values, meanings, and characteristics. Different data type requires different amounts of memory and has specific operations that can be performed over it. It specifies the type of data the variable can store, like integer, floating, character, double, etc. 

There are four types of data types:

Types

Description

Basic Data Types Arithmetic data types can be further classified into integer and floating data types. Examples are int, char, float, and double.
Void DataTypes The data type has no value and does not provide a result to its caller. But void comes under Primitive data types. The keyword used for such data type is “void.”
User-Defined Data Types Assigning the values to the integral constants is done by it, which makes it easy to maintain and read. 
Example:  enum
Derived types

The data types derived from the primitive datatypes are referred to as Derived Data Types.

For example, array, pointer, structure, union

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

 

 

 

 

 

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.

Live masterclass