473,802 Members | 2,228 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1540
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...@gm ail.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*********@gm ail.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)size of (1<0));

--
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 30 '07 #4
On Oct 30, 11:12 am, Keith Thompson <ks...@mib.orgw rote:
(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)size of (1<0));
The only way is this

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

Oct 30 '07 #5
"somenath" <so*********@gm ail.coma écrit dans le message de news:
11************* *********@y27g2 00...legr oups.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.o rga écrit dans le message de news:
47************* *********@news. free.fr...
"somenath" <so*********@gm ail.coma écrit dans le message de news:
11************* *********@y27g2 00...legr oups.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)sizeo f (int));
#endif
I would recommend "%lu" and unsigned long for the second case.
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,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)bre ak;else default:continu e;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.orgw rote:
>(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)size of (1<0));

The only way is this

#if __STDC_VERSION_ _ >= 199901L
printf("%zu\n", sizeof (int));
#else
printf("%u\n", (unsigned)sizeo f (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_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 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

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

Similar topics

6
2040
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
5297
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 array(i.e)it will have the address of the first element.In the the program below am not able to increment the value stored in x,which is the address of the first element.Why am I not able to do that?Afterall 1 is also a hexadecimal number then...
12
2422
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
1842
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 operator. If it can be made as run time operator, Please clarify me how to make it as run time operator?. what configurations have to do , to get this behavior. Please clarify my doubt. Thanks,
17
4113
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
2057
by: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
8
2054
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
9699
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
9559
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10301
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10058
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9107
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7596
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
5620
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4268
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
3
2964
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.