HomeLinuxSizeof() Operator in C Language

Sizeof() Operator in C Language


In C language, the info sort that’s assigned to a variable is immediately associated to the dimensions in bytes that it occupies within the reminiscence. For instance, the info sort char or unsigned char at all times occupies 1 byte, however the different primitive knowledge varieties corresponding to an integer of sort int occupy 4 bytes on a 32-bit system whereas they occupy 8 bytes on a 64-bit system.

The scale of variables and arrays in bytes is dependent upon the kind of knowledge and the system on which this system is operating.

This issue is vital as a result of the variables, matrices, buildings, or blocks which can be utilized by the varied features within the C language such because the dynamic reminiscence administration, information, sockets, and plenty of others handle the amount of their buffers in bytes. Subsequently, to have the ability to use them, it’s essential to acquire this worth by way of an operation that calculates the dimensions of the buffer.

On this Linuxhint article, you’ll discover ways to use the unary sizeof() operator to find out the dimensions in bytes of a variable, array, or knowledge construction. You’ll see a theoretical description of the syntax and the way this operator works. Then, we’ll apply what we discovered in sensible examples with code snippets and pictures the place we’ll present you the most typical makes use of of this operator in numerous instances.

Syntax of the Sizeof() Operator in C Language

Description of the Sizeof() Operator in C Language

The sizeof() operator returns a size_t which comprises the variety of bytes that’s occupied by a selected knowledge sort, variable, array, construction, or reminiscence block. In every case, the return worth is the results of the sizeof() operator which is utilized to one among these.

Subsequent, let’s take a look at some examples that present the right way to use this operator to find out the dimensions of varied objects.

Methods to Decide the Dimension of a Variable in Bytes with the Sizeof() Operator

How can we decide the dimensions of a variable in bytes with the sizeof() operator in C language?

The scale of a variable in bytes is obtained as the results of the sizeof() operation on the variable that’s specified by its identifier within the operand. The next code reveals how this operator works with char and int. The results of every operation is saved within the variable bytes after which displayed on the command console utilizing the printf() operate.

#embody <stdio.h>

void primary ()

{
char var1;
int var2;
int bytes;
bytes = sizeof(var1);
printf(“Bytes in var1 %i”, bytes);

bytes = sizeof(var2);
printf(“Bytes in var2 %i”, bytes);

}

The next picture reveals the compilation and execution of this code the place you may see the end result which corresponds to the variety of bytes that every variable occupies in accordance with its knowledge sort:

Methods to Use the Sizeof() Operator within the C Language to Decide the Dimension of a Given Information Kind in Bytes

The scale of a given knowledge sort in bytes is obtained when the sizeof() operator is utilized to the kind declaration. For instance, the next operation obtains the dimensions of an information sort int:

Within the following code, the printf() operate shows the results of the sizeof() operator which operates on a number of knowledge varieties:

#embody <stdio.h>

void primary ()

{
printf(nBytes in char %i”,      sizeof(char));
printf(nBytes in int %i”,       sizeof(int));
printf(nBytes in float %i”,     sizeof(float));
printf(nBytes in double %in,  sizeof(double));
}

The next picture reveals the results of compiling and executing this code the place you may see the dimensions that’s returned by the sizeof() operator in bytes for every knowledge sort:

Methods to Use the Sizeof() Operator to Get the Dimension of Arrays with A number of Information Sorts

The sizeof() operator can even decide the dimensions in bytes that an array of components occupies. On this case, the operand of sizeof() is the identifier of the array whose measurement in bytes is to be decided. The next code determines and outputs the variety of bytes that’s occupied by the “ch” array with 10 components of sort char and the “fl” array with the identical variety of components of sort float:

#embody <stdio.h>

void primary ()

{
char ch[10];
float fl[10];
printf(nBytes in ch %i”,   sizeof(ch));
printf(nBytes in fl %in,   sizeof(fl));
}

The next picture reveals the compilation and execution of this code. As we will see, the “ch” array occupies 10 bytes whereas the “fl” array occupies 40:

Methods to Handle the Dynamic Reminiscence Primarily based on Information Kind with the Sizeof() Operator

Dynamic reminiscence administration consists of reserving and utilizing the managed reminiscence blocks at runtime.

When reserving and managing a block, we should take into account what sort of knowledge we wish to retailer in it, for the reason that measurement of the block relies upon immediately on it. The variety of bytes which can be wanted for a block of a given knowledge sort is obtained by multiplying the variety of components that we wish to reserve by the results of the sizeof() which is returned for the kind of knowledge that we wish to retailer.

Within the following code, the malloc() operate reserves 1024 knowledge of sort int in block1. The scale of the block in bytes is 1024 x sizeof(int):

#embody <stdlib.h>
#embody <stdio.h>

void primary ()

{
int block1 =  1024*sizeof(int);
malloc( 1024*sizeof(int))
printf(nBytes in block1 %in,  block1);
}

The next picture reveals the compilation and execution of this code which allocates a reminiscence block of 1024 integers after which shows the variety of bytes that’s occupied by the reserved portion:

Conclusion

On this Linuxhint article, we confirmed you the right way to use the unary sizeof() operator to find out the dimensions of an object. We defined its operation and syntax in a theoretical part. We additionally confirmed you the way vital it’s to know the byte measurement of objects and arrays with a view to use the totally different features on this language.

Additionally, we confirmed you with sensible examples with code snippets and footage on the right way to use the operator in the most typical instances the place we use the various kinds of operands and name the totally different features the place the parameter that’s decided with this operator is used as enter argument.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments