470,613 Members | 1,765 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

doubt regarding sizeof operator







Hi All,

I am not able to understand the behavior of the bellow mentioned
program.

#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;
}
The output of the program is
Size = 0

But my understanding is sizeof returns the size of the type .In this
case should it not returns sizeof (int) ?

But if I rewrite the code as mentioned bellow the output is 4

#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof (1<0));
return 0;
}
Size = 4

Could you please help me to understand the problem?

Regards
Somenath

Oct 30 '07 #1
15 1348
somenath wrote:
>
Hi All,

I am not able to understand the behavior of the bellow mentioned
program.

#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;
}
The output of the program is

Size = 0

But my understanding is sizeof returns the size of the type .In this
case should it not returns sizeof (int) ?
The expression "sizeof 1<0" is false, hence the zero. sizeof binds
tighter than '<', so you have written (sizeof 1) < 0.

--
Ian Collins.
Oct 30 '07 #2
On 10 30 , 4 44 , somenath <somenath...@gmail.comwrote:
Hi All,

I am not able to understand the behavior of the bellow mentioned
program.

#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;}

The output of the program is

Size = 0

But my understanding is sizeof returns the size of the type .In this
case should it not returns sizeof (int) ?

But if I rewrite the code as mentioned bellow the output is 4

#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof (1<0));
return 0;

}

Size = 4

Could you please help me to understand the problem?

Regards
Somenath
printf("\n Size = %d \n",sizeof (1<0));//sizeof(bool)
printf("\n Size = %d \n",sizeof 1<0);// equal to ((sizeof (int))<0)
of course zero --false--
Oct 30 '07 #3
somenath <so*********@gmail.comwrites:
I am not able to understand the behavior of the bellow mentioned
program.

#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;
}
The output of the program is

Size = 0
Which is exactly what it should be, as Ian Collins explained.

[...]
But if I rewrite the code as mentioned bellow the output is 4

#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof (1<0));
return 0;
}

Size = 4
(1<0) yields a value of type int (with the value 0), so sizeof (1<0)
yields sizeof(int). Apparently that's 4 on your system. But you
print the value using a "%d" format, which is correct for type int,
but not for type size_t.

For a small value like this, a reasonable way to print it is to
convert it to int:

printf("\n Size = %d \n",(int)sizeof (1<0));

--
Keith Thompson (The_Other_Keith) 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 30 '07 #4
On Oct 30, 11:12 am, Keith Thompson <ks...@mib.orgwrote:
(1<0) yields a value of type int (with the value 0), so sizeof (1<0)
yields sizeof(int). Apparently that's 4 on your system. But you
print the value using a "%d" format, which is correct for type int,
but not for type size_t.

For a small value like this, a reasonable way to print it is to
convert it to int:

printf("\n Size = %d \n",(int)sizeof (1<0));
The only way is this

#if __STDC_VERSION__ >= 199901L
printf("%zu\n", sizeof (int));
#else
printf("%u\n", (unsigned)sizeof (int));
#endif

Oct 30 '07 #5
"somenath" <so*********@gmail.coma écrit dans le message de news:
11**********************@y27g2000pre.googlegroups. com...
>
I am not able to understand the behavior of the bellow mentioned
program.

#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;
}
This looks like a typo in a program that prints the size of floating point
numbers:
Oct 30 '07 #6
"Charlie Gordon" <ne**@chqrlie.orga écrit dans le message de news:
47**********************@news.free.fr...
"somenath" <so*********@gmail.coma écrit dans le message de news:
11**********************@y27g2000pre.googlegroups. com...
>>
I am not able to understand the behavior of the bellow mentioned
program.

#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;
}

This looks like a typo in a program that prints the size of floating point
numbers: (OOPS, my fingers slipped)
#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1.0);
return 0;
}

Note that sizeof 1.0, which is the same as sizeof(double), should be cast to
(int) for printf's %d format specifier. The format specifier for size_t is
%zu, but it is c99 specific.

--
Chqrlie.
Oct 30 '07 #7
vi****************@gmail.com writes:
The only way is this

#if __STDC_VERSION__ >= 199901L
printf("%zu\n", sizeof (int));
#else
printf("%u\n", (unsigned)sizeof (int));
#endif
I would recommend "%lu" and unsigned long for the second case.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Oct 30 '07 #8
vi****************@gmail.com writes:
On Oct 30, 11:12 am, Keith Thompson <ks...@mib.orgwrote:
>(1<0) yields a value of type int (with the value 0), so sizeof (1<0)
yields sizeof(int). Apparently that's 4 on your system. But you
print the value using a "%d" format, which is correct for type int,
but not for type size_t.

For a small value like this, a reasonable way to print it is to
convert it to int:

printf("\n Size = %d \n",(int)sizeof (1<0));

The only way is this

#if __STDC_VERSION__ >= 199901L
printf("%zu\n", sizeof (int));
#else
printf("%u\n", (unsigned)sizeof (int));
#endif
Why is that the only way?

It's safe to assume that sizeof(int) does not exceed INT_MAX (even
though it's not absolutely guaranteed by the standard), so for this
particular case, converting to int and using "%d" is not unreasonable.

If you want to be a bit safer, convert to unsigned long and use "%lu".

"%zu" is the correct format for C99 -- but in practice, there's a
significant risk that the compiler will claim to conform to C99, but
the runtime library won't support "%zu". "%lu" will work properly for
all sizes up to 2**32-1, and perhaps more.

--
Keith Thompson (The_Other_Keith) 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 30 '07 #9
somenath wrote:
>
#include<stdio.h>
int main(void) {
printf("\n Size = %d \n",sizeof 1<0);
return 0;
}
The output of the program is
Size = 0
Here "sizeof 1" returns size of an int, which is not < 0, so the
comparison function returns 0, which is then printed. Fully
parenthized the operation is "((sizeof 1) < 0)"
>
But my understanding is sizeof returns the size of the type .In
this case should it not returns sizeof (int) ?

But if I rewrite the code as mentioned bellow the output is 4

#include<stdio.h>
int main(void) {
printf("\n Size = %d \n",sizeof (1<0));
return 0;
}

Size = 4
Here "1 < 0" returns an int (which happens to be 0, and doesn't
matter). So sizeof (1 < 0) returns the sizeof an int, which
happens to be 4 on your system. Fully parenthized the operation is
"(sizeof (1 < 0))".

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 30 '07 #10
On Tue, 30 Oct 2007 09:03:33 +0000, qiooeer wrote:
printf("\n Size = %d \n",sizeof (1<0));//sizeof(bool)
(1<0) has type int.
%d is for signed int, not size_t.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 30 '07 #11
On Oct 30, 11:15 pm, "Charlie Gordon" <n...@chqrlie.orgwrote:
That's true, but it will not handle huge variables under Win64 where %zu
might not be supported either. Should we use %llu ?
You cannot have an object whose size SIZE_MAX.
%zu will work everywhere, anywhere as long as it is supported.
Else you cast to unsigned.

Oct 30 '07 #12
<vi****************@gmail.coma écrit dans le message de news:
11**********************@o80g2000hse.googlegroups. com...
On Oct 30, 11:15 pm, "Charlie Gordon" <n...@chqrlie.orgwrote:
>"Keith Thompson" <ks***@mib.orga écrit dans le message de news:
ln************@nuthaus.mib.org...
>That's true, but it will not handle huge variables under Win64 where %zu
might not be supported either. Should we use %llu ?
You cannot have an object whose size SIZE_MAX.
%zu will work everywhere, anywhere as long as it is supported.
Else you cast to unsigned.
Why did you snip the relevant part by Keith Thomson below:
>>"%zu" is the correct format for C99 -- but in practice, there's a
significant risk that the compiler will claim to conform to C99, but
the runtime library won't support "%zu". "%lu" will work properly for
all sizes up to 2**32-1, and perhaps more.
On the win64 architecture, size_t is 64 bits, while both unsigned and
unsigned long are 32 bits.
One can have objects larger than 4 GB on thosw systems for which %lu and
(unsigned long)sizeof(object) will fail to print the correct size. VC++ is
not c99 conforming, as %zu might not be supported. So the only way to print
a size on win64 is to cast to (unsigned long long) and use the %llu format.

--
Chqrlie.
Oct 30 '07 #13
"Charlie Gordon" <ne**@chqrlie.orgwrites:
One can have objects larger than 4 GB on thosw systems for which %lu and
(unsigned long)sizeof(object) will fail to print the correct size. VC++ is
not c99 conforming, as %zu might not be supported. So the only way to print
a size on win64 is to cast to (unsigned long long) and use the %llu format.
If VC++ is not C99 conforming, then it may not have unsigned long
long type at all, and %llu may also not be supported.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin
Oct 30 '07 #14
Keith Thompson <ks***@mib.orgwrote:
"%zu" is the correct format for C99 -- but in practice, there's a
significant risk that the compiler will claim to conform to C99, but
the runtime library won't support "%zu".
Possibly, but in that case, you have a broken implementation. %zu should
be among the least of your concerns. I'd advice upgrading to a correct,
well-built C99 implementation immediately. If that's not possible,
reverting to a correct, well-built C89 implementation would be
preferable over struggling with a chimaera.

Richard
Oct 31 '07 #15
"Ben Pfaff" <bl*@cs.stanford.edua écrit dans le message de news:
87************@blp.benpfaff.org...
"Charlie Gordon" <ne**@chqrlie.orgwrites:
>One can have objects larger than 4 GB on thosw systems for which %lu and
(unsigned long)sizeof(object) will fail to print the correct size. VC++
is
not c99 conforming, as %zu might not be supported. So the only way to
print
a size on win64 is to cast to (unsigned long long) and use the %llu
format.

If VC++ is not C99 conforming, then it may not have unsigned long
long type at all, and %llu may also not be supported.
Can anyone with VC++ experience what is the current status on c99
conformance and long long support ?

--
Chqrlie.
Nov 1 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by praba kar | last post: by
138 posts views Thread by ambika | last post: by
12 posts views Thread by sonu | last post: by
3 posts views Thread by venkat | last post: by
17 posts views Thread by venkat | last post: by
4 posts views Thread by Anarki | last post: by
8 posts views Thread by somenath | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.