×

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

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.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 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.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 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.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.