Sizeof operator in C https://www.skillvertex.com/blog Fri, 10 May 2024 07:15:58 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Sizeof operator in C https://www.skillvertex.com/blog 32 32 Sizeof Operator In C https://www.skillvertex.com/blog/sizeof-operator-in-c/ https://www.skillvertex.com/blog/sizeof-operator-in-c/#respond Fri, 10 May 2024 07:15:58 +0000 https://www.skillvertex.com/blog/?p=2234 Read more]]>

Table of Contents

Sizeof Operator In C

Sizeof is a common operator in C. It’s used to determine the size, in bytes, of a particular data type or variable. This operator provides information about the memory storage required for its operand. The result of sizeof is typically an unsigned integral value represented by the type size_t.

You can use sizeof with various data types, including basic types like integers and floats, pointer types, as well as more complex types like structures and unions. This operator helps you understand how much memory a specific data type or variable occupies in your computer’s memory.

Syntax

sizeof(Expression);

where ‘Expression‘ can be a data type or a variable of any type.

Return: It returns the size size of the given expression.

Time Complexity: O(1)
Auxiliary Space: O(1)

Usage of sizeof() operator 

When the sizeof operator is used with a data type as its operand, it returns the size, in bytes, of that data type.

  1. Data Type Operand: When you use sizeof a data type, like sizeof(int) or sizeof(char), it tells you how much memory is allocated for that specific data type in bytes. For example, sizeof(int) might return 4, indicating that an int takes up 4 bytes of memory on your system. This information is essential for memory management and understanding the storage requirements of your variables.

Example

// C Program To demonstrate
// sizeof operator
#include <stdio.h>
int main()
{
    printf("%lu\n", sizeof(char));
    printf("%lu\n", sizeof(int));
    printf("%lu\n", sizeof(float));
    printf("%lu", sizeof(double));
    return 0;
}

Output

1
4
4
8

2. Expression Operand: When you use sizeof with an expression, like sizeof(variable), it calculates and returns the size of the evaluated expression in bytes. For instance, sizeof(array) would give you the size in bytes of the entire array, and sizeof(structure) would provide the size of the entire structure in memory. This is helpful for determining the memory footprint of more complex data structures and expressions in your code.

// C Program To demonstrate
// operand as expression
#include <stdio.h>
int main()
{
    int a = 0;
    double d = 10.21;
    printf("%lu", sizeof(a + d));
    return 0;
}

Output

8

the sizeof operator returns the size of a data type in bytes. Typically, the size of an int is 4 bytes, and the size of a double is 8 bytes on many systems. So, if you have an int variable a and a double variable d, the size of int is 4 bytes, the size of double is 8 bytes, and the sizeof operator returns the size in bytes.

However, it’s important to note that the sizeof operator always returns a result of type size_t, which is an unsigned integral type used for sizes. The actual size of size_t may vary depending on the system (typically 4 or 8 bytes). So, the result of sizeof is not of the double type but rather of type size_t.

Type Of Operator

Sizeof() is indeed a compile-time operator in C and C++. It is evaluated by the compiler, not at runtime. When the compiler encounters sizeof(), it calculates the size of the operand (either a data type or an expression) and replaces sizeof() with the computed size during the compilation process.

// C Program to illustrate
// that the 'sizeof' operator
// is a 'compile time operator'
#include <stdio.h>
 
int main(void)
{
    int y;
    int x = 11;
 
    // value of x doesn't change
    y = sizeof(x++);
 
    // prints 4 and 11
    printf("%i %i", y, x);
 
    return (0);
}

Output

4 11

Need of Sizeof 

One common use of the sizeof operator is to determine the number of elements in an array automatically.

Example

// C Program
// demonstrate the method
// to find the number of elements
// in an array
#include <stdio.h>
int main()
{
    int arr[] = { 1, 2, 3, 4, 7, 98, 0, 12, 35, 99, 14 };
    printf("Number of elements:%lu ",
           sizeof(arr) / sizeof(arr[0]));
    return 0;
}

Output

Number of elements:11 

The sizeof operator is extensively used in dynamic memory allocation, especially when you want to allocate memory for a specific number of elements of a particular data type without hardcoding the size of that data type

Syntax

int* ptr = (int*)malloc(10 * sizeof(int));

FAQ- Sizeof Operator In C

Q1. What is the sizeof () operator in C?

Ans. sizeof is a widely used operator in C. It’s a compile-time unary operator used to calculate the size, in bytes, of its operand. The result of sizeof is typically of the unsigned integral type, often represented by size_t. This operator is essential for determining memory requirements and is valuable in various aspects of C programming.

Q2. Is size of () a function or operator?

Ans. In the C language, sizeof() is an operator, not a function, even though it resembles a function call. It is a unary operator used to determine the size, in bytes, of its operand, which can be a data type or an expression. The result of sizeof is of the unsigned integral type, often represented by size_t. It is essential for compile-time memory allocation and is a fundamental tool in C programming.

Q3. What is the use of size () operator?

Ans. It helps programmers avoid specifying machine-dependent data sizes in their programs, which promotes portability and flexibility. By using sizeof(), you can determine the total size of a data type or expression in terms of storage units that are the size of char, regardless of the specific machine’s architecture. This feature is valuable for writing code that can run on different platforms without modification, making it an essential tool for writing portable C programs.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/sizeof-operator-in-c/feed/ 0