473,670 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ?

Oct 18 '07 #1
10 3010
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.

Oct 18 '07 #2
On Oct 18, 1:22 pm, Kislay <kislaychan...@ gmail.comwrote:
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.

Oct 18 '07 #3
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.

Oct 18 '07 #4
On Oct 18, 11:32 pm, vipvipvipvip... @gmail.com wrote:
On Oct 18, 1:22 pm, Kislay <kislaychan...@ gmail.comwrote:
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
}

Oct 18 '07 #5
On Thu, 18 Oct 2007 03:22:06 -0700, in comp.lang.c , Kislay
<ki***********@ gmail.comwrote:
>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
Oct 18 '07 #6
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.
Oct 18 '07 #7
Mark McIntyre <ma**********@s pamcop.netwrite s:
[...]
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_Keit h) ks***@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"
Oct 19 '07 #8
On Oct 19, 11:42 am, Peter Pichler <use...@pichler .co.ukwrote:
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.

Oct 19 '07 #9
On Thu, 18 Oct 2007 18:51:05 -0700, Old Wolf wrote:
On Oct 19, 11:42 am, Peter Pichler <use...@pichler .co.ukwrote:
>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.
Oct 19 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
8860
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
31
2362
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
7
1926
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
12
1864
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's assume that the interpreter does conform to the range values for, say, type int, but allocates storage for the variables based on their value. So, for two variables foo and bar int foo = 0; /* interpreter allocates two bytes */ int bar =...
12
2408
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
15
2233
by: Alex Vinokur | last post by:
Why does one need to use two kinds of sizeof operator: * sizeof unary-expression, * sizeof (type-name) ? Their behavior seem not to be different (see an example below). ------ C++ code ------ #include <iostream> using namespace std;
43
781
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
28
6735
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
36
2789
by: Frederick Gotham | last post by:
sizeof has the same precedence as a cast, and they both bind from right to left. The following won't compile for me with gcc: int main(void) { sizeof(double)5; return 0; }
4
6052
by: Divick | last post by:
Hi, does sizeof operator evaluate the size at compile time or run time ? In other words is sizeof(SomeType) evaluated at compile time or at runtime? Please provide the reasoning involved as well. TIA, Divick
0
8471
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8907
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8593
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.