Is it good to use char instead of int to save memory?

H

Herbert Rosenau

On 3/18/2010 7:40 AM, dspfun wrote: (...)
What are the pros and cons of using char instead of int when values
will not exceed 255?

If you need [0..255] you should use unsigned char, not plain
char. (If you need [-127..127] make it signed char.)

Are there any 1's complement machines out there which have C
implementations that are actively used?

No, of course not. And no signed-magnitude machines,
either. What's more, there *never* will be, no, not ever,
not while your children's children's children still use C.

I'm 1000% sure that you are unable to enumerate each and every CPU
ever builded, having at least one valid C compiler and written lots of
programs for it. i86 is widely used, yes - but at least it is only a
rare CPU with an ill design.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 
I

Ian Collins

No, because int is the natural wide of the processor. handling int
will always produce the most possible code because C is designed to
use int to get best.

Not always, int has to be at least 16 bits which can make extra work for
8 bit processors.
char on other hand is implementation defined either unsigned or
signed, so if you only defines char without explicite type specifier
signed or unsigned it will give you unwanted behavior when you changes
the implementation or simply only the version of it.

There are processors alive where access to a char costs significant
more runtime and or extra instructions because the processor is unable
to access a single char in memory and must truncate and expand single
chars to/from int in memory.

The converse is also true.
 
E

Eric Sosman

I'm 1000% sure that you are unable to enumerate each and every CPU
ever builded, having at least one valid C compiler and written lots of
programs for it. i86 is widely used, yes - but at least it is only a
rare CPU with an ill design.

Herbert, my post was sarcasm. In a written medium like
Usenet, the spoken inflections, pauses, emphases, and so on
that signal a speaker's "attitude" are absent. Sometimes the
reader, deprived of those signals, may not recognize the writer's
actual meaning (which may be at variance with a straightforward
literal reading). This seems to be especially true when the
writer and reader have different native languages.

In other words: I meant the opposite of what I wrote, and
what I wrote was intended as a sarcastic parody of short-sighted
attitudes that I feel are all too common around here.

(By the way, your use of "1000%" is itself ironic for USAnian
readers. Two words: Thomas Eagleton.)
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top