ansi types

B

Bill Cunningham

I am using in my code an unsigned char when I could be using a char.
Just what does the standard say about all these types? On my machine a long
and long long are not the same. However char and unsigned char are the same
in length of bit streams that it. Double is 8 bytes and int is 4 just like
char. Does C treat these data types as different in any way other than
length? Could I not say,

int *i="stream\n"; rather than
char *p="stream\n";
And not lose lose anything of value other than having strange looking code?

Bill
 
B

Bill Cunningham

Richard Heathfield said:
Bill Cunningham said:
[snip]
Double is 8
bytes and int is 4 just like
char.

char can't be 4 bytes. It has to be 1 byte, by definition (where one
byte consists of exactly CHAR_BIT bits, defined in <limits.h>).


Oops. I meant int is 4 bytes. char is 1. Now is char an unsigned or
signed char by default? My machine also says size_t is 4 bytes. What the
standard says a return type is is I guess what it is. Example, int getc. But
I have discovered so many people using all these types in their code. Is a
long of type int? Then there's short of course. I am asking all of this for
portability and understand more about types to not use too much space or too
little.

Bill
 
B

Bill Cunningham

char, signed char, and unsigned char are three distinct types,
even though two of them (char and one of the other two) have the
same representation. Use whichever one makes sense in a particular
context; char for character data, unsigned char for byte data,
and signed char when you want a very narrow signed integer (the
latter is relatively rare).

What is narrow signed" ?

Bill
 
K

Keith Thompson

Richard Heathfield said:
Bill Cunningham said:



It depends on the implementation. But if you use char for text data,
unsigned char for bytes of arbitrary provenance, then it will never
matter to you.

char, signed char, and unsigned char are three distinct types,
even though two of them (char and one of the other two) have the
same representation. Use whichever one makes sense in a particular
context; char for character data, unsigned char for byte data,
and signed char when you want a very narrow signed integer (the
latter is relatively rare).
It's of type long int.


Which is of type short int.

To be clear, "long int" and "long" are two different names for the
same type; similarly, "short int" and "short" are two different names
for the same type. (Saying that short is of type short int might
obscure that point.)

short int, int, long int, and long long int are four distinct types,
even though some or all of them may have the same representation.

[snip]
 
P

Phil Carmody

Richard Heathfield said:
Bill Cunningham said:



It depends on the implementation. But if you use char for text data,
unsigned char for bytes of arbitrary provenance, then it will never
matter to you.

I thought that its signedness depended on the implementation, but
that it was a third type distinct from either signed or unsigned
char, simply coincident in properties with one of the other two.

Phil
 
K

Keith Thompson

Phil Carmody said:
I thought that its signedness depended on the implementation, but
that it was a third type distinct from either signed or unsigned
char, simply coincident in properties with one of the other two.

Correct.
 
P

Phil Carmody

Keith Thompson said:

Blimey - you're quick - I was just about to cancel my message as
I spotted it was completely redundant after yours!

Phil
 
K

Keith Thompson

Bill Cunningham said:
What is narrow signed" ?

There's no particular meaning for the phrase "narrow signed".
I merely meant a signed type that is narrow (i.e., covers a small
range and doesn't occupy much memory).
 
N

Nick Keighley

On 11 Aug 2009 at  0:59, Bill Cunningham wrote:

On your platform, and any platform your code is ever likely to run on,
it's signed.

but it's easy to write code that doesn't depend on this.
So why do otherwise?
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top