Why n++ not work when it is an argument for sizeof() function? [duplicate]












-1















This question already has an answer here:




  • Why does sizeof(x++) not increment x?

    9 answers




int main() {
int n = 1;
sizeof(n++);
printf("%d",n);
return 0;
}


It's part of my code. The output is 1. But why is n not increased by 1?

I tried that for other functions but for others output was 2.










share|improve this question















marked as duplicate by phuclv, melpomene c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 13:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    sizeof n++ equates to "sizeof type(n++)" (if this existed). the size of of a type does not change no matter what value it has: sizeof 42 == sizeof -3 == sizeof isalpha('x') == sizeof (int).
    – pmg
    Nov 15 '18 at 13:17


















-1















This question already has an answer here:




  • Why does sizeof(x++) not increment x?

    9 answers




int main() {
int n = 1;
sizeof(n++);
printf("%d",n);
return 0;
}


It's part of my code. The output is 1. But why is n not increased by 1?

I tried that for other functions but for others output was 2.










share|improve this question















marked as duplicate by phuclv, melpomene c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 13:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    sizeof n++ equates to "sizeof type(n++)" (if this existed). the size of of a type does not change no matter what value it has: sizeof 42 == sizeof -3 == sizeof isalpha('x') == sizeof (int).
    – pmg
    Nov 15 '18 at 13:17
















-1












-1








-1








This question already has an answer here:




  • Why does sizeof(x++) not increment x?

    9 answers




int main() {
int n = 1;
sizeof(n++);
printf("%d",n);
return 0;
}


It's part of my code. The output is 1. But why is n not increased by 1?

I tried that for other functions but for others output was 2.










share|improve this question
















This question already has an answer here:




  • Why does sizeof(x++) not increment x?

    9 answers




int main() {
int n = 1;
sizeof(n++);
printf("%d",n);
return 0;
}


It's part of my code. The output is 1. But why is n not increased by 1?

I tried that for other functions but for others output was 2.





This question already has an answer here:




  • Why does sizeof(x++) not increment x?

    9 answers








c sizeof






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:24









Sourav Ghosh

109k14129187




109k14129187










asked Nov 15 '18 at 13:06









Morteza MirzaiMorteza Mirzai

345




345




marked as duplicate by phuclv, melpomene c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 13:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by phuclv, melpomene c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 13:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    sizeof n++ equates to "sizeof type(n++)" (if this existed). the size of of a type does not change no matter what value it has: sizeof 42 == sizeof -3 == sizeof isalpha('x') == sizeof (int).
    – pmg
    Nov 15 '18 at 13:17
















  • 1




    sizeof n++ equates to "sizeof type(n++)" (if this existed). the size of of a type does not change no matter what value it has: sizeof 42 == sizeof -3 == sizeof isalpha('x') == sizeof (int).
    – pmg
    Nov 15 '18 at 13:17










1




1




sizeof n++ equates to "sizeof type(n++)" (if this existed). the size of of a type does not change no matter what value it has: sizeof 42 == sizeof -3 == sizeof isalpha('x') == sizeof (int).
– pmg
Nov 15 '18 at 13:17






sizeof n++ equates to "sizeof type(n++)" (if this existed). the size of of a type does not change no matter what value it has: sizeof 42 == sizeof -3 == sizeof isalpha('x') == sizeof (int).
– pmg
Nov 15 '18 at 13:17














3 Answers
3






active

oldest

votes


















3














This is because, sizeof is not a function, it is a compile time operator, ans unless the operand is a VLA, the operand is not evaluated at runtime.



Quoting C11, chapter §6.5.3.4




[...] If the type of the operand is a variable length array
type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
integer constant.




To add a little on why it does not need to evaluate the operand, from the semantics of the operand,




[....] The size is determined from the type of
the operand. [...]




and, the type of the operand is fixed and known at compile time (unless a VLA), so it does not need to evaluate the operand to perform it's job anyway. In your case, since n is of type int



 sizeof (n++);


is just the same as



 sizeof (int);





share|improve this answer































    3














    sizeof is not a function, it's an operator.



    sizeof(n++) is equivalent to sizeof n++, which (since n is an int) is equivalent to sizeof (int).



    sizeof only looks at the type of its operand; it doesn't evaluate expressions.



    (Unless variable-length arrays are involved, but we're going to ignore that for now.)






    share|improve this answer





























      1














      Because sizeof is not a function, but an operator. It's argument has no side effects.






      share|improve this answer

















      • 1




        Most of the time the operand of sizeof has no side effects. However, in int n = 3; sizeof(int [n++]); printf("%dn", n);, n is changed, and “4” is printed.
        – Eric Postpischil
        Nov 15 '18 at 15:46


















      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      This is because, sizeof is not a function, it is a compile time operator, ans unless the operand is a VLA, the operand is not evaluated at runtime.



      Quoting C11, chapter §6.5.3.4




      [...] If the type of the operand is a variable length array
      type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
      integer constant.




      To add a little on why it does not need to evaluate the operand, from the semantics of the operand,




      [....] The size is determined from the type of
      the operand. [...]




      and, the type of the operand is fixed and known at compile time (unless a VLA), so it does not need to evaluate the operand to perform it's job anyway. In your case, since n is of type int



       sizeof (n++);


      is just the same as



       sizeof (int);





      share|improve this answer




























        3














        This is because, sizeof is not a function, it is a compile time operator, ans unless the operand is a VLA, the operand is not evaluated at runtime.



        Quoting C11, chapter §6.5.3.4




        [...] If the type of the operand is a variable length array
        type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
        integer constant.




        To add a little on why it does not need to evaluate the operand, from the semantics of the operand,




        [....] The size is determined from the type of
        the operand. [...]




        and, the type of the operand is fixed and known at compile time (unless a VLA), so it does not need to evaluate the operand to perform it's job anyway. In your case, since n is of type int



         sizeof (n++);


        is just the same as



         sizeof (int);





        share|improve this answer


























          3












          3








          3






          This is because, sizeof is not a function, it is a compile time operator, ans unless the operand is a VLA, the operand is not evaluated at runtime.



          Quoting C11, chapter §6.5.3.4




          [...] If the type of the operand is a variable length array
          type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
          integer constant.




          To add a little on why it does not need to evaluate the operand, from the semantics of the operand,




          [....] The size is determined from the type of
          the operand. [...]




          and, the type of the operand is fixed and known at compile time (unless a VLA), so it does not need to evaluate the operand to perform it's job anyway. In your case, since n is of type int



           sizeof (n++);


          is just the same as



           sizeof (int);





          share|improve this answer














          This is because, sizeof is not a function, it is a compile time operator, ans unless the operand is a VLA, the operand is not evaluated at runtime.



          Quoting C11, chapter §6.5.3.4




          [...] If the type of the operand is a variable length array
          type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
          integer constant.




          To add a little on why it does not need to evaluate the operand, from the semantics of the operand,




          [....] The size is determined from the type of
          the operand. [...]




          and, the type of the operand is fixed and known at compile time (unless a VLA), so it does not need to evaluate the operand to perform it's job anyway. In your case, since n is of type int



           sizeof (n++);


          is just the same as



           sizeof (int);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 13:31

























          answered Nov 15 '18 at 13:08









          Sourav GhoshSourav Ghosh

          109k14129187




          109k14129187

























              3














              sizeof is not a function, it's an operator.



              sizeof(n++) is equivalent to sizeof n++, which (since n is an int) is equivalent to sizeof (int).



              sizeof only looks at the type of its operand; it doesn't evaluate expressions.



              (Unless variable-length arrays are involved, but we're going to ignore that for now.)






              share|improve this answer


























                3














                sizeof is not a function, it's an operator.



                sizeof(n++) is equivalent to sizeof n++, which (since n is an int) is equivalent to sizeof (int).



                sizeof only looks at the type of its operand; it doesn't evaluate expressions.



                (Unless variable-length arrays are involved, but we're going to ignore that for now.)






                share|improve this answer
























                  3












                  3








                  3






                  sizeof is not a function, it's an operator.



                  sizeof(n++) is equivalent to sizeof n++, which (since n is an int) is equivalent to sizeof (int).



                  sizeof only looks at the type of its operand; it doesn't evaluate expressions.



                  (Unless variable-length arrays are involved, but we're going to ignore that for now.)






                  share|improve this answer












                  sizeof is not a function, it's an operator.



                  sizeof(n++) is equivalent to sizeof n++, which (since n is an int) is equivalent to sizeof (int).



                  sizeof only looks at the type of its operand; it doesn't evaluate expressions.



                  (Unless variable-length arrays are involved, but we're going to ignore that for now.)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 13:08









                  melpomenemelpomene

                  58.9k54489




                  58.9k54489























                      1














                      Because sizeof is not a function, but an operator. It's argument has no side effects.






                      share|improve this answer

















                      • 1




                        Most of the time the operand of sizeof has no side effects. However, in int n = 3; sizeof(int [n++]); printf("%dn", n);, n is changed, and “4” is printed.
                        – Eric Postpischil
                        Nov 15 '18 at 15:46
















                      1














                      Because sizeof is not a function, but an operator. It's argument has no side effects.






                      share|improve this answer

















                      • 1




                        Most of the time the operand of sizeof has no side effects. However, in int n = 3; sizeof(int [n++]); printf("%dn", n);, n is changed, and “4” is printed.
                        – Eric Postpischil
                        Nov 15 '18 at 15:46














                      1












                      1








                      1






                      Because sizeof is not a function, but an operator. It's argument has no side effects.






                      share|improve this answer












                      Because sizeof is not a function, but an operator. It's argument has no side effects.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '18 at 13:07









                      YoYoYonnYYoYoYonnY

                      782719




                      782719








                      • 1




                        Most of the time the operand of sizeof has no side effects. However, in int n = 3; sizeof(int [n++]); printf("%dn", n);, n is changed, and “4” is printed.
                        – Eric Postpischil
                        Nov 15 '18 at 15:46














                      • 1




                        Most of the time the operand of sizeof has no side effects. However, in int n = 3; sizeof(int [n++]); printf("%dn", n);, n is changed, and “4” is printed.
                        – Eric Postpischil
                        Nov 15 '18 at 15:46








                      1




                      1




                      Most of the time the operand of sizeof has no side effects. However, in int n = 3; sizeof(int [n++]); printf("%dn", n);, n is changed, and “4” is printed.
                      – Eric Postpischil
                      Nov 15 '18 at 15:46




                      Most of the time the operand of sizeof has no side effects. However, in int n = 3; sizeof(int [n++]); printf("%dn", n);, n is changed, and “4” is printed.
                      – Eric Postpischil
                      Nov 15 '18 at 15:46



                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)