Re: why?
- From: "Peter Nilsson" <airia@xxxxxxxxxxx>
- Date: 29 May 2005 16:50:00 -0700
Martin Ambuhl wrote:
> xcm wrote:
> > #include<stdio.h>
> > main()
> > { printf("%d %d\n",sizeof('a'),sizeof('A'));}
> >
> > why does it printf 2 2 in tc.
>
> Since sizeof() yields an unsigned type (size_t), the "%d" specifier
> is incorrect.
Not if size_t is unsigned short (unlikely), and though it's not
explicit (or at least normative) within the standard, the committee
has advised the intent is that corresponding signed and unsigned
types may be passed in this way, so long as the value being passed
is within the range of both types.
You are unlikely to find an implementation where...
sizeof(int) > INT_MAX
The real problem with the code is that size_t may be an unsigned long.
C90 suffers the problem that there is no length modifier specifically
for size_t. [C99 introduced z, as in %zu.]
> The reason that sizeof('x') is 2 in your implementation is
> that 'a' is an int, and sizeof(int) is 2 in your implementation.
> You would get a different result in a very similar language, C++.
Although the implicit int declaration in the code above would likely
fail to compile in that similar language. :-)
--
Peter
.
- References:
- Re: why?
- From: Martin Ambuhl
- Re: why?
- Prev by Date: Re: how to define a function pointer variable witout typdef?
- Next by Date: Re: Where is Dan Pop
- Previous by thread: Re: why?
- Next by thread: sizeof...
- Index(es):
Relevant Pages
|
|