sizeof() operator in C ? With example | Need of sizeof() operator


sizeof() operator in C

The sizeof() operator is commonly used in C. It determines the size of the expression or the data type specified in the number of char-sized storage units. the sizeof operator is used to determine the size in bytes of a data type or an object. It is often used to allocate memory for variables, arrays, or data structures and to perform low-level memory management. The syntax of the sizeof operator is as follows:
c
sizeof(expression)
Here, expression can be a data type, a variable, or an object for which you want to determine the size. The sizeof operator returns the size in bytes as an unsigned integer value.

 Example, 

c
#include <stdio.h> int main() { int x; double y; size_t sizeOfX = sizeof(x); size_t sizeOfY = sizeof(y); printf("Size of int x: %zu bytes\n", sizeOfX); printf("Size of double y: %zu bytes\n", sizeOfY); return 0; }


Output:



Size of int x: 4 bytes Size of double y: 8 bytes

Need of sizeof() operator


Mainly, programs know the storage size of the primitive data types. Though the storage size of the data type is constant, it varies when implemented in different platforms.

1. Memory Allocation: One of the primary uses of sizeof() is for memory allocation. When you allocate memory for variables, arrays, or data structures using functions like malloc (dynamic memory allocation) or when declaring local variables, you need to specify the size in bytes. sizeof() allows you to determine the size of the data type or object, ensuring that you allocate the correct amount of memory
  1. c
    int *arr = (int *)malloc(sizeof(int) * 10); // Allocating memory for an array of 10 integers
2. Portability: C is often used for system-level programming, where different systems have different memory layouts and data type sizes. Using sizeof() ensures that your code is portable across different platforms since it provides the size of a data type or object for the specific system on which the code is compiled.


3. Array Size: When working with arrays, knowing their size is crucial for looping and accessing elements. sizeof() helps in calculating the size of arrays in bytes, making it easier to write code that is independent of the array's specific length.

c
int myArray[] = {1, 2, 3, 4, 5}; int arraySize = sizeof(myArray) / sizeof(myArray[0]);

4. Buffer Management: In C, you often work with buffers, such as character arrays or structures. Using sizeof() to determine the size of these buffers is essential for reading, writing, and ensuring you don't overrun the allocated memory.

5. Data Structure Layout: When working with complex data structures, you may need to understand how elements are laid out in memory. sizeof() helps you calculate the size of structures, which is important for tasks like serialization and deserialization.


The sizeof() operator behaves differently according to the type of the operand.


1. Operand is a data type

2. Operand is an expression

1. When operand is a data type:

c
#include <stdio.h> int main() { // Using a variable to indirectly determine the size of a data type int sampleVariable; size_t intSize = sizeof(sampleVariable); size_t charSize = sizeof(char); printf("Size of int data type: %zu bytes\n", intSize); printf("Size of char data type: %zu bytes\n", charSize); return 0; }


In this code, 
we are using sizeof() with variables (num and pi) to calculate the size of the objects they represent. When you run this code, you would get the size of int and double data types as output. The output may vary depending on the specific system and compiler, but you would see values like 4 bytes for int and 8 bytes for double on many systems.


2. When operand is an expression :

c
#include <stdio.h> int main() { int arr[5] = {1, 2, 3, 4, 5}; size_t arraySize = sizeof(arr); // Size of the whole array size_t elementSize = sizeof(arr[0]); // Size of an array element size_t arraySizeComputed = sizeof(arr) / sizeof(arr[0]); // Number of elements in the array printf("Size of the array: %zu bytes\n", arraySize); printf("Size of an array element: %zu bytes\n", elementSize); printf("Number of elements in the array: %zu\n", arraySizeComputed); return 0; }

In this code, we have an integer array arr. We use sizeof() with expressions to determine various sizes:

1. sizeof(arr) calculates the size of the entire array, including all of its elements.
2. sizeof(arr[0]) calculates the size of a single element within the array.
3. sizeof(arr) / sizeof(arr[0]) computes the number of elements in the array by dividing the size of the whole array by the size of a single element.

 Output :

c
Size of the array: 20 bytes Size of an array element: 4 bytes Number of elements in the array: 5

Post a Comment

0 Comments