Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof C integral types

Reply
Thread Tools

sizeof C integral types

 
 
ark
Guest
Posts: n/a
 
      01-14-2004
Risking to invoke flames from one Tom St Denis of Ottawa

Is there any guarantee that, say,
sizeof(int) == sizeof(unsigned int)
sizeof(long) > sizeof(char) ?

Thanks,
Ark


 
Reply With Quote
 
 
 
 
Russell Hanneken
Guest
Posts: n/a
 
      01-14-2004
ark wrote:
>
> Is there any guarantee that, say,
> sizeof(int) == sizeof(unsigned int)
> sizeof(long) > sizeof(char) ?


I don't believe the standard guarantees that either is true.

--
Russell Hanneken

Remove the 'g' from my address to send me mail.

 
Reply With Quote
 
 
 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      01-14-2004
ark wrote:

> Is there any guarantee that, say,
> sizeof(int) == sizeof(unsigned int)


Yes.

> sizeof(long) > sizeof(char) ?


No. BTW, sizeof(char) == 1 by definition.

However, sizeof(long) == 1, which I think implies sizeof(int) == 1,
would break several very common idioms, e.g.

int ch;
while ((ch = getchar()) != EOF) { ... }

because EOF is supposed to be a value which is different from all
'unsigned char' values. That is only possible when 'int' is wider
than 'unsigned char'.

Personally I've never seen a program which worred about this
possibility, though I suppose such programs exist. It might
be different with freestanding implementations (implementations
which do not use the C library, so getchar() is no problem).

--
Hallvard
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      01-14-2004
"ark" <> writes:

> Is there any guarantee that, say,
> sizeof(int) == sizeof(unsigned int)


Yes.

> sizeof(long) > sizeof(char) ?


No.
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      01-14-2004
Russell Hanneken <> writes:

> ark wrote:
>> Is there any guarantee that, say,
>> sizeof(int) == sizeof(unsigned int)
>> sizeof(long) > sizeof(char) ?

>
> I don't believe the standard guarantees that either is true.


The former, but not the latter, is guaranteed.
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-14-2004
ark wrote:

> Risking to invoke flames from one Tom St Denis of Ottawa
>
> Is there any guarantee that, say,
> sizeof(int) == sizeof(unsigned int)
> sizeof(long) > sizeof(char)?


Are you aware of the type definitions in <stdint.h>?

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      01-14-2004
Hallvard B Furuseth <h.b.furuseth(nospam)@usit.uio(nospam).no> writes:

> However, sizeof(long) == 1, which I think implies sizeof(int) == 1,


Actually there's no such implication. The range of int is a
subrange of the range of long, but there's no such guarantee on
the size in bytes of these types.

However, it would be a strange system for which sizeof(long) <
sizeof(int). I don't know of any.
--
"...Almost makes you wonder why Heisenberg didn't include postinc/dec operators
in the uncertainty principle. Which of course makes the above equivalent to
Schrodinger's pointer..."
--Anthony McDonald
 
Reply With Quote
 
ark
Guest
Posts: n/a
 
      01-14-2004

"Hallvard B Furuseth" <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote in
message news:...
>
> However, sizeof(long) == 1, which I think implies sizeof(int) == 1,
> would break several very common idioms, e.g.
>
> int ch;
> while ((ch = getchar()) != EOF) { ... }
>
> because EOF is supposed to be a value which is different from all
> 'unsigned char' values. That is only possible when 'int' is wider
> than 'unsigned char'.
>
> Personally I've never seen a program which worred about this
> possibility, though I suppose such programs exist. It might
> be different with freestanding implementations (implementations
> which do not use the C library, so getchar() is no problem).
>
> --
> Hallvard


I believe that a 16-bit DSP with a 16-bit byte would have
sizeof(int)==sizeof(short) (and ==1).
- Ark


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-15-2004
"E. Robert Tisdale" wrote:
> ark wrote:
>
> > Risking to invoke flames from one Tom St Denis of Ottawa
> >
> > Is there any guarantee that, say,
> > sizeof(int) == sizeof(unsigned int)
> > sizeof(long) > sizeof(char)?

>
> Are you aware of the type definitions in <stdint.h>?


Only implied misinformation from Trollsdale this time. <stdint.h>
is a C99 artifact, and the type defined therein are only defined
when the implementation has suitable types. So you could do
something like:

#if defined(sometype)
#define mytype sometype
#else
#define mytype whatever
#endif

with suitable guards for a C99 system.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      01-15-2004
In article <>,
"E. Robert Tisdale" <> wrote:

> ark wrote:
>
> > Risking to invoke flames from one Tom St Denis of Ottawa
> >
> > Is there any guarantee that, say,
> > sizeof(int) == sizeof(unsigned int)
> > sizeof(long) > sizeof(char)?

>
> Are you aware of the type definitions in <stdint.h>?


Quite possibly he is aware of them and knows that they have nothing to
do with the question asked.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Can "all bits zero" be a trap representation for integral types? Army1987 C Programming 6 07-07-2007 12:01 PM
integral types SasQ C++ 13 03-30-2007 10:10 AM
size and nomenclature of integral types Shailesh C++ 4 04-04-2004 06:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57