SizeOf Operator Related Codes

The sizeof operator in C is a unary operator that returns the size, in bytes, of the operand. The operand can be a variable, a data type, or an expression. In this blog the sizeof operator is discussed in detail, including its syntax, examples, and applications.

Syntax

The syntax of the sizeof operator is as follows:

sizeof(operand)

The operand can be a variable, a data type, or an expression. The sizeof operator returns the size of the operand in bytes.

Examples

Let's look at some examples of the sizeof operator in C.

Example 1: Size of data types

The sizeof operator can be used to determine the size of data types in C. The following example shows the sizes of some commonly used data types:

#include <stdio.h> 
int main() { printf("Size of char: %ld byte(s)\n", sizeof(char)); printf("Size of short: %ld byte(s)\n", sizeof(short)); printf("Size of int: %ld byte(s)\n", sizeof(int)); printf("Size of long: %ld byte(s)\n", sizeof(long)); printf("Size of float: %ld byte(s)\n", sizeof(float)); printf("Size of double: %ld byte(s)\n", sizeof(double)); printf("Size of long double: %ld byte(s)\n", sizeof(long double)); printf("Size of void: %ld byte(s)\n", sizeof(void)); return 0; }

Output:

Size of char: 1 byte(s)
Size of short: 2 byte(s)
Size of int: 4 byte(s)
Size of long: 8 byte(s)
Size of float: 4 byte(s)
Size of double: 8 byte(s)
Size of long double: 16 byte(s)
Size of void: 1 byte(s)

Example 2: Size of arrays

The sizeof operator can also be used to determine the size of arrays in C. The following example shows the size of an array of integers:

#include <stdio.h>

int main() 
{
   int arr[] = {1, 2, 3, 4, 5};
   int n = sizeof(arr) / sizeof(arr[0]);
   printf("Size of arr[]: %ld byte(s)\n", sizeof(arr));
   printf("Number of elements in arr[]: %d\n", n);
   return 0;
}

Output:

Size of arr[]: 20 byte(s)
Number of elements in arr[]: 5

Example 3: Size of structures

The sizeof operator can also be used to determine the size of structures in C. The following example shows the size of a structure that contains two integers:

#include 

struct Point 
{
   int x;
   int y;
};

int main() 
{
   struct Point p;
   printf("Size of struct Point: %ld byte(s)\n", sizeof(struct Point));
   printf("Size of struct Point variable p: %ld byte(s)\n", sizeof(p));
   return 0;
}

Output:

Size of struct Point: 8 byte(s)
Size of struct Point variable p: 8 byte(s)

Example 4: Size of pointer variables

The sizeof operator can also be used to determine the size of pointer variables in C. The following example shows the size of a pointer variable that points to an integer:

#include 

int main() 
{
   int *p;
   printf("Size of pointer variable p: %ld byte(s)\n", sizeof(p));
   return 0;
}

Output:

Size of pointer variable p: 8 byte(s)

Note: The size of a pointer variable is usually the same as the size of a memory address on the system.

Example 5: Size of for memory allocation

The sizeof operator can also be used for memory allocation as shown below:

#include 
#include 

int main() 
{
   int *p;
   int size = 5;
   p = (int *)malloc(size * sizeof(int));
   
   if (p == NULL) 
   {
      printf("Memory allocation failed!\n");
      return 1;
   }
   
   for (int i = 0; i < size; i++) 
   {
      p[i] = i + 1;
   }
   
   printf("The values of the array are: ");
   for (int i = 0; i < size; i++) 
   {
      printf("%d ", p[i]);
   }
   
   free(p);
   return 0;
}

Output:

The values of the array are: 1 2 3 4 5 

In this example, the malloc() function is used to allocate memory for an integer array of size 5. The sizeof operator is used to determine the size of each integer element in bytes, which is then multiplied by the number of elements in the array to calculate the total size of the array in bytes. The malloc() function returns a pointer to the first byte of the allocated memory block.

After allocating memory for the array, it checks if the memory allocation was successful. If the memory allocation failed, it prints an error message and exit the program.

Next, it initialize the values of the array by iterating over each element and assigning it a value of i + 1 and prints the values of the array and free the allocated memory using the free() function to avoid memory leaks.

Applications

As discussed above, The sizeof operator has several applications in C programming, including:

  • Memory allocation: The sizeof operator is commonly used to allocate memory for variables, arrays, and structures using functions like malloc() and calloc().
  • Array manipulation: The sizeof operator is used to determine the size of an array, which is often used to iterate over the array or perform other operations on the array.
  • Structure manipulation: The sizeof operator is used to determine the size of a structure, which is often used to allocate memory for a structure or to perform operations on the structure.
  • Performance optimization: The sizeof operator can be used to optimize performance by reducing memory usage and improving cache locality. For example, when iterating over an array, using the sizeof operator to determine the size of the array can help avoid accessing out-of-bounds memory, which can reduce cache misses and improve performance.

In conclusion, the sizeof operator is a powerful tool in C programming that allows the user to determine the size of variables, data types, arrays, structures, and pointers. It has several applications in memory allocation, array and structure manipulation, and performance optimization, making it an essential operator to understand for any C programmer.