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 | | | | re: doubt regarding sizeof operator
somenath wrote: Quote:
>
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. | | | | re: doubt regarding sizeof operator
On 10 30 , 4 44 , somenath <somenath...@gmail.comwrote: Quote:
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-- | | | | re: doubt regarding sizeof operator
somenath <somenathpal@gmail.comwrites: Quote:
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.
[...] Quote:
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) 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" | | | | re: doubt regarding sizeof operator
On Oct 30, 11:12 am, Keith Thompson <ks...@mib.orgwrote: Quote:
(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 | | | | re: doubt regarding sizeof operator
"somenath" <somenathpal@gmail.coma écrit dans le message de news: 1193733892.491643.315010@y27g2000pre.googlegroups. com... Quote:
>
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: | | | | re: doubt regarding sizeof operator
"Charlie Gordon" <news@chqrlie.orga écrit dans le message de news:
47272d17$0$5451$426a74cc@news.free.fr... Quote:
"somenath" <somenathpal@gmail.coma écrit dans le message de news: 1193733892.491643.315010@y27g2000pre.googlegroups. com... Quote:
>>
>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. | | | | re: doubt regarding sizeof operator vipvipvipvipvip.ru@gmail.com writes: Quote:
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;}}} | | | | re: doubt regarding sizeof operator vipvipvipvipvip.ru@gmail.com writes: Quote:
On Oct 30, 11:12 am, Keith Thompson <ks...@mib.orgwrote: Quote:
>(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) 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" | | | | re: doubt regarding sizeof operator
somenath wrote: Quote:
>
#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)" Quote:
>
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 | | | | re: doubt regarding sizeof operator
On Tue, 30 Oct 2007 09:03:33 +0000, qiooeer wrote: Quote:
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. | | | | re: doubt regarding sizeof operator
On Oct 30, 11:15 pm, "Charlie Gordon" <n...@chqrlie.orgwrote: Quote:
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. | | | | re: doubt regarding sizeof operator
<vipvipvipvipvip.ru@gmail.coma écrit dans le message de news: 1193781055.450457.290450@o80g2000hse.googlegroups. com... Quote:
On Oct 30, 11:15 pm, "Charlie Gordon" <n...@chqrlie.orgwrote: Quote:
>"Keith Thompson" <kst-u@mib.orga écrit dans le message de news:
ln8x5k1j9e.fsf@nuthaus.mib.org... Quote: Quote:
>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: Quote: Quote: Quote:
>>"%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. | | | | re: doubt regarding sizeof operator
"Charlie Gordon" <news@chqrlie.orgwrites: Quote:
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 | | | | re: doubt regarding sizeof operator
Keith Thompson <kst-u@mib.orgwrote: Quote:
"%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 | | | | re: doubt regarding sizeof operator
"Ben Pfaff" <blp@cs.stanford.edua écrit dans le message de news: 87zly0usev.fsf@blp.benpfaff.org... Quote:
"Charlie Gordon" <news@chqrlie.orgwrites:
> Quote:
>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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|