×

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of size is of the unsigned fundamental sort, normally signified by size_t. sizeof() operator can be applied to any information type, including crude sorts, for example, whole number and drifting point types, pointer types, or compound datatypes, for example, Structure, union, and so on.

  • When the operand is a Data Type:

When sizeof() is utilized with the information types, for example, int, float, scorch… and so on, it returns how much memory is apportioned to that data type.

Example:

//simple C program to check the size of the data types using the sizeof() //operator
#include <conio.h>   //here, we are including the console input and output //library into our program
#include <stdio.h>   //here, we are including the standard input and output //library into our program
int main()
{
    clrscr();
    printf(“Printing the size of data types using the sizeof() operator:”);
    printf("The size of the char data type is %lu\n", sizeof(char));    //here, we //are printing the size of char data type using the sizeof() operator
    printf("The size of the char data type is %lu\n", sizeof(int));      //here, we //are printing the size of integer data type using the sizeof() operator
    printf("The size of the char data type is %lu\n", sizeof(float));   //here, we //are printing the size of float data type using the sizeof() operator
    printf("The size of the char data type is %lu", sizeof(double));    //here, we //are printing the size of double data type using the sizeof() operator
    return 0;
}

Output:

Printing the size of data types using the sizeof() operator:
The size of the char data type is 1
The size of the char data type is 4
The size of the char data type is 4
The size of the char data type is 8
  • When the operand is an expression

When sizeof() is utilized with the expression, it returns the size of the expression.

Example:

//simple C program to check the size of the data types using the sizeof() //operator
#include <conio.h>   //here, we are including the console input and output //library into our program
#include <stdio.h>   //here, we are including the standard input and output //library into our program
int main()
{
    int n =0;     //here, we are declaring the integer variable with the value 0 
    double d = 22.15;    //here, we are declaring the double variable with the //value 22.15 
    printf(“Printing the size of the given expression using the sizeof() operator:”);
    printf("The size of the given expression is %lu\n", sizeof(n + d));    //here, we //are printing the size of char data type using the sizeof() operator
    return 0;
}

Output:

Printing the size of the given expression using the sizeof() operator:
The size of the given expression is 8

Why does sizeof(x++) not Increment x in C?

The sizeof() administrator considers the kind of the operand, which might be an articulation or the name of a sort (i.e, int, twofold, float, and so forth), and not the worth got on assessing the articulation. Subsequently, the operand inside the sizeof() administrator isn't assessed. It is assessed provided that the kind of the operand is variable length exhibited on the grounds that, all things considered, the size can be resolved solely after the articulation is assessed.

Case 1:

When the sizeof() method is passed a fixed size structure:

In this situation, the sizeof() administrator doesn't assess the boundary. Just the sort of boundary is checked, and its size is returned.

Example:

//simple C program to check the size of the data types using the sizeof() //operator
#include <conio.h>   //here, we are including the console input and output //library into our program
#include <stdio.h>   //here, we are including the standard input and output //library into our program
int main()
{
    int n = 10;       //here, we are declaring the integer variable with the data 3 
    printf("The size of the given variable is %d\n", sizeof(n++));      //here, we are //printing the value of the n after incrementing
    printf("The given data of the variable is n = %d", n);     //here, we are printing the //original value of the n
    return 0;
}

Output:

The size of the given variable is 11
The given data of the variable is n = 10

Explanation:

The result worth of x after augmentation is as yet 10 when contrasted with the average worth, which is 11. This is because the sizeof administrator doesn't have to assess the articulation to acquire the size as the information kind of the operand doesn't change, and thus the size continues as before.

Case 2:

When the sizeof() method is passed a variable size structure:

For this situation, the sizeof() administrator assesses the boundary to check whether there is any difference in size. In the event that it is found, first, the size is altered, then the last size is returned.

Example:

//simple C program to check the size of the data types using the sizeof() //operator
#include <conio.h>   //here, we are including the console input and output //library into our program
#include <stdio.h>   //here, we are including the standard input and output //library into our program
int main()
{
    int n = 10;       //here, we are declaring the integer variable with the data 3 
    printf("The size of the given variable is %d\n", sizeof(int[n++]));      //here, we are //printing the value of the n after incrementing
    printf("The given data of the variable is n = %d", n);     //here, we are printing the //original value of the n
    return 0;
}

Output:

The size of the given variable is 40
The given data of the variable is n = 10

Explanation:

In this model, the sizeof administrator requirements to assess the articulation to ascertain the size of the exhibit, which is displayed in the figure. Thus, for this situation, we get the worth of n after incrementation.

Example 1:

//simple C program to check the size of the data types using the sizeof() //operator
#include <conio.h>   //here, we are including the console input and output //library into our program
#include <stdio.h>   //here, we are including the standard input and output //library into our program
int main()
{
    int n = 10;       //here, we are declaring the integer variable with the data 3 
    printf("The size of the given variable is %d\n", sizeof(int[n++]));      //here, we are //printing the value of the n after incrementing
    printf("The given data of the variable is n = %d", n);     //here, we are printing the //original value of the n
    return 0;
}

Output:

The size of the given variable is 44
The given data of the variable is n = 10

Explanation:

Again in this example, the sizeof administrator requirements to assess the articulation to ascertain the size of the exhibit, which is displayed in the figure. In this model, the post-increase administrator is utilized, and the result in the main line is 44. In the subsequent line, the worth of x is printed after incrementation.


Related Topics

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.