doubt regarding sizeof operator

S

somenath

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
 
I

Ian Collins

somenath said:
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.
 
Q

qiooeer

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--
 
K

Keith Thompson

somenath said:
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));
 
V

vipvipvipvipvip.ru

(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
 
C

Charlie Gordon

somenath said:
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:
 
C

Charlie Gordon

Charlie Gordon said:
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.
 
B

Ben Pfaff

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.
 
K

Keith Thompson

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.
 
C

CBFalconer

somenath said:
#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))".
 
C

Charlie Gordon

Keith Thompson said:
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".

You question my ``#define true 1'' as not being particularly safer, and then
give the proper reason why it should be, and then you pretend there is even
a bit of a chance that sizeof(int) might not fit in an int or even in an
unsigned... This borders on pedantry.
"%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.

That's true, but it will not handle huge variables under Win64 where %zu
might not be supported either. Should we use %llu ?
 
V

vipvipvipvipvip.ru

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.
 
C

Charlie Gordon

(e-mail address removed)...

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:

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.
 
B

Ben Pfaff

Charlie Gordon said:
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.
 
K

Keith Thompson

Charlie Gordon said:
"Keith Thompson" <[email protected]> a écrit dans le message de
(e-mail address removed)... [...]
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".

You question my ``#define true 1'' as not being particularly safer, and then
give the proper reason why it should be, and then you pretend there is even
a bit of a chance that sizeof(int) might not fit in an int or even in an
unsigned... This borders on pedantry.

Borders? :cool:}
That's true, but it will not handle huge variables under Win64 where %zu
might not be supported either. Should we use %llu ?

"%llu" is not supported in C90.

In C90, size_t cannot be bigger than unsigned long, so "%lu" is safe.

In C99, "%zu" is supported.

But in a hybrid implementation (which most of them are these days),
it's reasonably likely that long long is supported but "%zu" is not.

Using "%lu" for non-C99 and "%zu" for C99 should cover most
implementations (and conceivably covers all existing implementations).
If you run across one where size_t is bigger than unsigned long *and*
the implementation doesn't claim C99 conformance, you can probably
customize a solution just for that system -- but even that's only
necessary if the particular value you're trying to print exceeds
ULONG_MAX.
 
R

Richard Bos

Keith Thompson said:
"%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
 
C

Charlie Gordon

Ben Pfaff said:
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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top