Join our subscribers list to get the latest news, updates and special offers directly in your inbox
Overview
Sizeof BitFields in Structure
Creating Custom Sizeof Operator
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.
sizeof
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.
Let's look at some examples of the sizeof operator in C.
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)
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; }
Size of arr[]: 20 byte(s) Number of elements in arr[]: 5
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; }
Size of struct Point: 8 byte(s) Size of struct Point variable p: 8 byte(s)
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; }
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.
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; }
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.
malloc()
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.
i + 1 and
free()
As discussed above, The sizeof operator has several applications in C programming, including:
calloc()
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.
EmbeddedWala
EmbeddedWala Jun 14, 2023 0 24.8K
EmbeddedWala Apr 27, 2023 0 23.5K
EmbeddedWala Apr 26, 2023 0 20.2K
EmbeddedWala Feb 15, 2024 0 19.1K
EmbeddedWala Aug 30, 2022 0 18.3K
EmbeddedWala Jun 19, 2022 0 5.1K
This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies Find out more here