Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > plain int and signed int

Reply
Thread Tools

plain int and signed int

 
 
happy
Guest
Posts: n/a
 
      01-14-2010
As signed char and plain char can be different if plain char is
unsigned in a particular implementaion,
Can signed int and plain int be different or are they guaranteed to be
same?
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      01-14-2010
On 14 Jan, 09:03, happy <hppymit...@yahoo.com> wrote:

> As signed char and plain char can be different if plain char is
> unsigned in a particular implementaion,
> Can signed int and plain int be different or are they guaranteed to be
> same?


an int is a signed int
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-14-2010
happy <> writes:
> As signed char and plain char can be different if plain char is
> unsigned in a particular implementaion,
> Can signed int and plain int be different or are they guaranteed to be
> same?


Plain char and signed char are always distinct types. They may or
may not have the same representation.

"signed int" and "int" are simply two different names for the same
type, with one exception: the name "int", when it's the type of a bit
field, may refer either to signed int or to unsigned int. (Thanks to
Richard Heathfield for pointing that out; I would have forgotten it.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
happy
Guest
Posts: n/a
 
      01-14-2010
On Jan 14, 10:22*pm, Keith Thompson <ks...@mib.org> wrote:
> happy <hppymit...@yahoo.com> writes:
> > As signed char and plain char can be different if plain char is
> > unsigned in a particular implementaion,
> > Can signed int and plain int be different or are they guaranteed to be
> > same?

>
> Plain char and signed char are always distinct types. *They may or
> may not have the same representation.
>

Even if plain char is signed, then also are plain and signed char
distinct ?
I mean can they have different representations?
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      01-14-2010
happy <> writes:

> Even if plain char is signed, then also are plain and signed char
> distinct ?
> I mean can they have different representations?


If plain char is signed, then plain char and signed char have the
same representation.

If plain char is unsigned, then plain char and unsigned char have
the same representation.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-14-2010
On 1/14/2010 12:49 PM, happy wrote:
> On Jan 14, 10:22 pm, Keith Thompson<ks...@mib.org> wrote:
>> happy<hppymit...@yahoo.com> writes:
>>> As signed char and plain char can be different if plain char is
>>> unsigned in a particular implementaion,
>>> Can signed int and plain int be different or are they guaranteed to be
>>> same?

>>
>> Plain char and signed char are always distinct types. They may or
>> may not have the same representation.
>>

> Even if plain char is signed, then also are plain and signed char
> distinct ?
> I mean can they have different representations?


Signed char is a type. Unsigned char is another type,
whose representation is necessarily different from that of
signed char.

Plain char is a third type. It has the same representation
as one of the first two -- either signed char or unsigned char --
but is a distinct type nonetheless.

Many people find this weird, for a simple reason: It's weird.
But that's the way it is, mostly for historical reasons.

If it's any comfort, consider that on many systems there are
only two representations shared among the three distinct types
short, int, and long: Usually short and long are different, and
int has the same representation as one or the other. But short
and int are distinct types even on systems where they have the
same representation, while int and long are distinct even on
systems where *they* have the same representation.

--
Eric Sosman
lid
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-14-2010
happy <> writes:
> On Jan 14, 10:22Â*pm, Keith Thompson <ks...@mib.org> wrote:
>> happy <hppymit...@yahoo.com> writes:
>> > As signed char and plain char can be different if plain char is
>> > unsigned in a particular implementaion,
>> > Can signed int and plain int be different or are they guaranteed to be
>> > same?

>>
>> Plain char and signed char are always distinct types. Â*They may or
>> may not have the same representation.
>>

> Even if plain char is signed, then also are plain and signed char
> distinct ?


Yes.

> I mean can they have different representations?


No.

If plain char is signed, then plain char and signed char are two
disinct types with exactly the same range and representation.

Some examples:

char c = 'c';
signed char s = 's';
/* The actual values are not important */

c = s; /* ok, involves an implicit type conversion */
s = c; /* ok, as above */
/* Note that the implicit type conversions above don't do
any actual work.
*/

char *pc = &c;
signed char *ps = &s;
pc = ps; /* constraint violation, requires a diagnostic */
ps = pc; /* constraint violation, requires a diagnostic */

Since C permits implicit conversions between any two arithmetic
types, the fact that two such types are distinct *usually* isn't very
important. But pointers to two such distinct types are themselves
distinct pointer types, and there are no implicit conversions for
distinct pointer types (other than void* and some special cases
for null pointer constants)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
bartc
Guest
Posts: n/a
 
      01-15-2010

"Richard Heathfield" <> wrote in message
news:...
> Nick Keighley wrote:
>> On 14 Jan, 09:03, happy <hppymit...@yahoo.com> wrote:
>>
>>> As signed char and plain char can be different if plain char is
>>> unsigned in a particular implementaion,
>>> Can signed int and plain int be different or are they guaranteed to be
>>> same?

>>
>> an int is a signed int

>
> unless it's a bit-field, in which case you'd better specify which one you
> want, signed or unsigned. Otherwise the implementation gets to choose.


I didn't realise a field of one bit could be signed. Seems to work though.

--
Bartc

 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      01-15-2010
bartc <> wrote:
>
> I didn't realise a field of one bit could be signed. Seems to work though.


Just be aware that it might only be able to hold one value, which isn't
very useful in most cases.
--
Larry Jones

I don't think that question was very hypothetical at all. -- Calvin
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-16-2010
writes:
> bartc <> wrote:
>> I didn't realise a field of one bit could be signed. Seems to work though.

>
> Just be aware that it might only be able to hold one value, which isn't
> very useful in most cases.


I think that it could hold either 0 or -1 on a 2's-complement system.
On a 1's-complement or sign-and-magnitude system, it could only hold
+0 or -0.

Is that what you had in mind?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
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
no warning for assigning unsigned int to plain int V.Subramanian, India C Programming 5 10-12-2011 07:41 PM
comparison between signed int and unsigned int Alex C Programming 3 04-26-2006 05:20 AM
convert signed int to unsigned int Siemel Naran C++ 3 11-29-2004 08:22 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 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