The sizeof operator : sizeof(++i)

K

Kislay

int main()
{
int i=10;
printf("\n Size of i = %d ",sizeof(++i));
printf("\n i = %d ",i);
system("pause");
return 0;
}

On executing the above code , the value of i obtained as 10 . What
happens to the increment ? Why does not not that take place ? Has it
got something to do with the fact that sizeof is a compile-time
operator ?
 
C

christian.bau

Kislay said:
int main()
{
int i=10;
printf("\n Size of i = %d ",sizeof(++i));
printf("\n i = %d ",i);
system("pause");
return 0;
}

On executing the above code , the value of i obtained as 10 . What
happens to the increment ? Why does not not that take place ? Has it
got something to do with the fact that sizeof is a compile-time
operator ?

sizeof doesn't evaluate its operand.
 
V

vipvipvipvip.ru

On executing the above code , the value of i obtained as 10 . What
happens to the increment ? Why does not not that take place ? Has it
got something to do with the fact that sizeof is a compile-time
operator ?

sizeof does not evaluate what it's given, that's why sizeof *p works
with uninitialized pointers, for example.
sizeof is not preprocessed, but evaluated/replaced by the compiler.
 
V

vipvipvipvip.ru

Also, take this for example
assume sizeof(char) < sizeof(long long)

char c;
printf("%zu \n", sizeof (c + 1LL));

Would print something >1, c is promoted to long long.
 
O

Old Wolf

sizeof is not preprocessed, but evaluated/replaced by the compiler.

As is every operator..
sizeof does not evaluate what it's given, that's why sizeof *p works
with uninitialized pointers, for example.

For some arguments to sizeof, it is allowed to evaluate
the argument. for example:

void func(int n, int array[n][n])
{
int i = 0;
printf("size is %zu\n", sizeof array[i++]);
printf("i is %d\n", i); // could be either 0 or 1
}
 
M

Mark McIntyre

int main()
{
int i=10;
printf("\n Size of i = %d ",sizeof(++i));

warning: incompatible implicit declaration of built-in function printf
printf("\n i = %d ",i);

warning: no newline after output - text may not appear onscreen
(and on Linux, it doesn't...)
On executing the above code , the value of i obtained as 10 .

I believe that sizeof() doesn't evaluate its operand (unless its a
VLA, I think), so the increment is never executed.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
P

Peter Pichler

Old said:
As is every operator..

Really? Compilers must have progressed quite a lot while I wasn't
looking. From now on, I will keep in mind that a+b is evaluated at
compile time.
 
K

Keith Thompson

Mark McIntyre said:
I believe that sizeof() doesn't evaluate its operand (unless its a
VLA, I think), so the increment is never executed.

Correct.

For the record, here's what the standard says (C99 6.5.3.4p2):

The sizeof operator yields the size (in bytes) of its operand,
which may be an expression or the parenthesized name of a
type. The size is determined from the type of the operand. The
result is an integer. 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.

There are some odd cases that this doesn't cover, where either the
standard says the operand isn't evaluated enen though it really needs
to be, or the operand is evaluated whne it really doesn't need to be.
All such cases involve indirect uses of VLAs (variable length arrays).
We discussed this recently in comp.std.c, subject "Evaluating the
operand of sizeof".

But if you're not using VLAs, none of this is relevant; the operand of
sizeof won't be evaluated if no VLAs are involved.
 
O

Old Wolf

Really? Compilers must have progressed quite a lot while I wasn't
looking. From now on, I will keep in mind that a+b is evaluated at
compile time.

All expression evaluation occurs at runtime, according
to the C standard.
 
?

=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F

All expression evaluation occurs at runtime, according to the C
standard.

enum { zero = 1 - 1 };
#if 1 > 2
#error
#elif 3 * 4
enum { one = !zero };
#endif
int main(int argc, char *argv[zero - one]) {}

All expressions in this simple invalid program are required to be
evaluated at compile time. I suppose you could say if the value is
determined at compile time, this isn't an evaluation. I haven't found the
definition of evaluation. However, the standard does say at least in the
description of #if directives that the expressions are evaluated, and
that must happen at compile time.
 
P

Peter Pichler

Old said:
All expression evaluation occurs at runtime, according
to the C standard.

Not all, as Harald described. But my point was that you claimed that
"every operator is evaluated/replaced by the compiler". At least that's
how I read your answer to vipvipvipvip..., whoever he/she/it is. The +
in my a+b is indeed "replaced" by a compiler (with some machine code),
but I do not think that's what either you or vipvipvipvip... meant.

Sorry, I used what is usually referred to as "irony".

irony [n]
having the properties of iron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top