Why n++ not work when it is an argument for sizeof() function? [duplicate]
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.
c sizeof
marked as duplicate by phuclv, melpomene
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.
add a comment |
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.
c sizeof
marked as duplicate by phuclv, melpomene
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
add a comment |
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.
c sizeof
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
c sizeof
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
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
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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);
add a comment |
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.)
add a comment |
Because sizeof
is not a function, but an operator. It's argument has no side effects.
1
Most of the time the operand ofsizeof
has no side effects. However, inint n = 3; sizeof(int [n++]); printf("%dn", n);
,n
is changed, and “4” is printed.
– Eric Postpischil
Nov 15 '18 at 15:46
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
add a comment |
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);
add a comment |
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);
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);
edited Nov 15 '18 at 13:31
answered Nov 15 '18 at 13:08
Sourav GhoshSourav Ghosh
109k14129187
109k14129187
add a comment |
add a comment |
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.)
add a comment |
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.)
add a comment |
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.)
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.)
answered Nov 15 '18 at 13:08
melpomenemelpomene
58.9k54489
58.9k54489
add a comment |
add a comment |
Because sizeof
is not a function, but an operator. It's argument has no side effects.
1
Most of the time the operand ofsizeof
has no side effects. However, inint n = 3; sizeof(int [n++]); printf("%dn", n);
,n
is changed, and “4” is printed.
– Eric Postpischil
Nov 15 '18 at 15:46
add a comment |
Because sizeof
is not a function, but an operator. It's argument has no side effects.
1
Most of the time the operand ofsizeof
has no side effects. However, inint n = 3; sizeof(int [n++]); printf("%dn", n);
,n
is changed, and “4” is printed.
– Eric Postpischil
Nov 15 '18 at 15:46
add a comment |
Because sizeof
is not a function, but an operator. It's argument has no side effects.
Because sizeof
is not a function, but an operator. It's argument has no side effects.
answered Nov 15 '18 at 13:07
YoYoYonnYYoYoYonnY
782719
782719
1
Most of the time the operand ofsizeof
has no side effects. However, inint n = 3; sizeof(int [n++]); printf("%dn", n);
,n
is changed, and “4” is printed.
– Eric Postpischil
Nov 15 '18 at 15:46
add a comment |
1
Most of the time the operand ofsizeof
has no side effects. However, inint 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
add a comment |
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