|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 ? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Kislay wrote:
> 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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Oct 18, 1:22 pm, Kislay <kislaychan...@gmail.com> wrote:
> 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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Oct 18, 11:32 pm, vipvipvipvip...@gmail.com wrote:
> On Oct 18, 1:22 pm, Kislay <kislaychan...@gmail.com> wrote: > > > 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 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 } |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Thu, 18 Oct 2007 03:22:06 -0700, in comp.lang.c , Kislay
<kislaychandra@gmail.com> wrote: >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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Old Wolf wrote:
> On Oct 18, 11:32 pm, vipvipvipvip...@gmail.com wrote: > >>sizeof is not preprocessed, but evaluated/replaced by the compiler. > > 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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Mark McIntyre <markmcintyre@spamcop.net> writes:
[...] > 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. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On Oct 19, 11:42 am, Peter Pichler <use...@pichler.co.uk> wrote:
> Old Wolf wrote: > > On Oct 18, 11:32 pm, vipvipvipvip...@gmail.com wrote: > > >>sizeof is not preprocessed, but evaluated/replaced by the compiler. > > > 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. All expression evaluation occurs at runtime, according to the C standard. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
On Thu, 18 Oct 2007 18:51:05 -0700, Old Wolf wrote:
> On Oct 19, 11:42 am, Peter Pichler <use...@pichler.co.uk> wrote: >> Old Wolf wrote: >> > On Oct 18, 11:32 pm, vipvipvipvip...@gmail.com wrote: >> >> >>sizeof is not preprocessed, but evaluated/replaced by the compiler. >> >> > 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. > > 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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
Old Wolf wrote:
> On Oct 19, 11:42 am, Peter Pichler <use...@pichler.co.uk> wrote: > >>Old Wolf wrote: >> >>>On Oct 18, 11:32 pm, vipvipvipvip...@gmail.com wrote: >> >>>>sizeof is not preprocessed, but evaluated/replaced by the compiler. >> >>>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. > > 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 |
|
![]() |
| Outils de la discussion | |
|
|