sizeof() in 'C'

pradeep_agrawal

pradeep_agrawal

@pradeep-agrawal-rhdX5z Oct 26, 2024

Below code was compiled and executed on a 32-bit machine where size if 'int' is '4':

#include “stdio.h”

int main() {
    int i = 0, j = 0;
    j = sizeof(++i);
    printf("i = %d\nj = %d\n", i, j);
    return 0;
}

Why hhe output is
i = 0
j = 4
instead of
i = 1
j = 4


I know the answer but thought to post it as a query before sharing the cause of this behavior.

-Pradeep

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • slashfear

    slashfear

    @slashfear-tSWzpz Apr 20, 2009

    Hey Pradeep,

    I don't find any strange behavior in the code you have given here,

    #include "stdio.h"
    
    int main() {
        int i = 0, j = 0;
        j = sizeof(++i);
        printf("i = %d\nj = %d\n", i, j);
        return 0;
    }
    
    

    But i can give you the explanation for the output, first you have declared and assigned two variable i = 0 and j = 0 , then you have used j = sizeof (++i)
    when you use the print f to print the value of i and j , the value of i will be 0 only as you have assigned the value of i=0 so it will always yield 0 and the value of j will be 4 as the size of integer is 4 bytes.

    I think the question your trying to ask is, we have used ++i and so the value of i should be incremented from 0 to 1. right???

    If that was your question here is the answer, see by using j = sizeof(++i) will not change the value of i (because we are assigning the value only to j and not i ) so we have to include something like this in the code, i = ++i , so it will yield the value of i as 1.

    and more over sizeof() is only used for returning the size of a variable.

    
    #include <stdio.h>
    
    int main() {
        int i = 0, j = 0;
        j = sizeof(++i);
        i = ++i;
        printf("i = %d\nj = %d\n", i, j);
        return 0;
    }
    

    Hope that's the answer your expecting buddy!!! ok let me know if I am wrong or can you make the question clear so that i can give it another try!! 😀


    Regards,

    Arvind (slashfear) 😉

  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Apr 20, 2009

    slashfearI think the question your trying to ask is, we have used ++i and so the value of i should be incremented from 0 to 1. right???

    If that was your question here is the answer, see by using j = sizeof(++i) will not change the value of i (because we are assigning the value only to j and not i ) so we have to include something like this in the code, i = ++i , so it will yield the value of i as 1.

    Yes, my question was as we have used "++i" so the value of 'i' should have incremented from 0 to 1. But it's not changing to '1' , whats the rational behind this?


    slashfearby using j = sizeof(++i) will not change the value of i (because we are assigning the value only to j and not i ) so we have to include something like this in the code, i = ++i , so it will yield the value of i as 1.

    Its not necessary to assign the incremented value to same variable by doing "i = ++i" as mentioned by you. The increment operator itself do this (i.e., when we say ++i it internally takes it as "i = i + 1"; the advantage of using "++i" over "i = i + 1" is that it takes less cycles for execution). Consider the below code where "++i" is used at various points:

    #include "stdio.h"
    
    void donothing(int i) {
    }
    
    int main() {
      int i = 0, j;
    
      printf("i: %d\n", i);
      ++i;
      printf("i: %d\n", i);
      j = ++i;
      printf("i: %d\n", i);
      donothing(++i);
      printf("i: %d\n", i);
    
      return 0;
    }
    

    The output of the code is:
    i: 0
    i: 1
    i: 2
    i: 3

    As the value of i gets incremented every time "++i" is used in any statement (irrespective of if i have explicitly assigned the incremented value to same variable). Why the value does not get incremented when i say "j = sizeof(++i);"?

    slashfearand more over sizeof() is only used for returning the size of a variable.

    Its not necessary that user has used sizeof to determine the size of a variable. See below code:

    #include "stdio.h"
    
    int main() {
      int i = 0;;
      double d = 1.0;
    
      printf("Size: %d\n", sizeof(i + d));
    
      return 0;
    }
    

    Here sizeof is used to determine size of the result of the expression.

    -Pradeep

  • vivek.m

    vivek.m

    @vivekm-aDMB2K Apr 20, 2009

    pradeep_agrawalBelow code was compiled and executed on a 32-bit machine where size if 'int' is '4':

    #include “stdio.h”
    
    int main() {
        int i = 0, j = 0;
        j = sizeof(++i);
        printf("i = %d\nj = %d\n", i, j);
        return 0;
    }
    
    Why hhe output is
    i = 0
    j = 4
    instead of
    i = 1
    j = 4


    I know the answer but thought to post it as a query before sharing the cause of this behavior.

    -Pradeep

    Hi,

    Nice question.

    I can think of only one possible reason why this is happening:
    sizeof() makes a temporary copy of the object that is passed as argument. So, it will make a temporary copy of 'i' and increment that temporary copy and then do some voodoo to find out the size of that incremented temporay copy?

    BTW any idea how will it do that voodoo? Suppose, if I ask you to find out the size of a type without using sizeof() ?

    I would like to know the answer to both.. Thanks.

  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Apr 21, 2009

    vivek.mI can think of only one possible reason why this is happening:
    sizeof() makes a temporary copy of the object that is passed as argument. So, it will make a temporary copy of 'i' and increment that temporary copy and then do some voodoo to find out the size of that incremented temporay copy?

    This is not the correct reason for the behavior. I know the answer, will post it when i don't get the correct answer in next few days (may be in next 2 days).

    vivek.mBTW any idea how will it do that voodoo? Suppose, if I ask you to find out the size of a type without using sizeof() ?

    Will give it a thought. You can start a new thread on CE for this.

    -Pradeep

  • slashfear

    slashfear

    @slashfear-tSWzpz Apr 21, 2009

    Hey Pradeep,

    Ya now i got the question buddy, Thanks for the example too.

    Ok now the answer for your question, before getting into the core about the behaviour of the code lets first see what is sizeof().

    [FONT=arial,verdana,helvetica,sans-serif][SIZE=-1]Consider the syntax of the following expressions:
    [/SIZE][/FONT][FONT=arial,verdana,helvetica,sans-serif][SIZE=-1][SIZE=+1]
    sizeof expression
    [/SIZE][/SIZE][/FONT][SIZE=+1] sizeof ( type-name )
    [/SIZE]
    [FONT=arial,verdana,helvetica,sans-serif] The sizeof operator produces a compile-time integer constant value. expression is inspected only to deduce its type; it is not fully evaluated. For example, sizeof(++i) is equivalent to sizeof(i).

    [/FONT][FONT=arial,verdana,helvetica,sans-serif][SIZE=-1]The result of the sizeof operation is the size, in bytes, of the operand. In the first case, the result of sizeof is the size determined by the type of the expression. In the second case, the result is the size of an object of the named type. The expression should be enclosed in parentheses if it contains operators, because the precedence of sizeof is higher than that of most operators.


    So this is the reasoning behind the strange behavior of the code. So that's why the value of i will not be incremented from 0 to 1. ;-)


    Let me know if you have different answer buddy!!!!

    [/SIZE][/FONT]

  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Apr 21, 2009

    Slashfear, this time you got the correct reason behind the behavior and provided a detailed explanation. I really appreciate that. Thanks.

    -Pradeep

  • slashfear

    slashfear

    @slashfear-tSWzpz Apr 22, 2009

    pradeep_agrawalSlashfear, this time you got the correct reason behind the behavior and provided a detailed explanation. I really appreciate that. Thanks.

    -Pradeep



    Thanks to you too Pradeep...........😉