PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > The sizeof operator : sizeof(++i)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
The sizeof operator : sizeof(++i)

Réponse
 
LinkBack Outils de la discussion
Vieux 18/10/2007, 11h22   #1
Kislay
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut The sizeof operator : sizeof(++i)

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 ?

  Réponse avec citation
Vieux 18/10/2007, 11h29   #2
christian.bau
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The sizeof operator : sizeof(++i)

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.

  Réponse avec citation
Vieux 18/10/2007, 11h32   #3
vipvipvipvip.ru@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The sizeof operator : sizeof(++i)

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.

  Réponse avec citation
Vieux 18/10/2007, 11h36   #4
vipvipvipvip.ru@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The sizeof operator : sizeof(++i)

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.

  Réponse avec citation
Vieux 18/10/2007, 22h51   #5
Old Wolf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The sizeof operator : sizeof(++i)

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
}

  Réponse avec citation
Vieux 18/10/2007, 23h38   #6
Mark McIntyre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The sizeof operator : sizeof(++i)

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
  Réponse avec citation
Vieux 18/10/2007, 23h42   #7
Peter Pichler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The sizeof operator : sizeof(++i)

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.
  Réponse avec citation
Vieux 19/10/2007, 00h59   #8
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: The sizeof operator : sizeof(++i)

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"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h54.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21374 seconds with 16 queries