Re: why?



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

.



Relevant Pages

  • Re: which should i learn c or c++ ?
    ... >I have a book to learn the C language, but also I have some ebooks to ... >So I have a real problem. ... They'll jump right into using STL, and from your knowledge of C you'll ... productive code before even delving into pointers (by using STL ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Mass Style modification?
    ... select all and go to Tools> Language. ... > check" in the language setting. ... > The real problem is that I have several complex multi-style document ... > templates that I work from and somehow, ...
    (microsoft.public.mac.office.word)
  • Re: Space Shuttle Frequencies (UK) ??
    ... as usual zpk, in his inimitable style, has rubbished the language. ... clearly lacks the ability to understand and express the very basic units ... his real problem is that he rushes in to "score points" but just ...
    (uk.radio.amateur)
  • Re: The limits of lisps synatactic gymnastics? (i sure hope not)
    ... > I don't particularly have a 'real problem' except 'clos classes are ... translate your prototype-based language, with whatever syntax you ... But since you control the compiler for your language its just up ... However, that said, unless you're already a CLOS master, I'd suggest ...
    (comp.lang.lisp)