473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1512
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: praba kar | last post by:
Dear All, I have doubt regarding sorting. I have a list that list have another list (eg) list = ,,] I want to sort only numeric value having array field. How I need to do for that.
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
12
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
3
by: venkat | last post by:
Hi, I am new to c programming language. I heard that , sizeof can be made as run time operator. Even though it is a compile time operator. I am not sure also whether sizeof can be made as run time...
17
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
4
by: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
8
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.