EmbeddedRelated.com
Forums

IAR and C data type char

Started by Rajagopalan May 6, 2012
All

I am not an active programmer but just deal with many hands on programmers.

I was shocked to find that the default for the char data type in IAR is unsigned. (Overridable of course!) In almost every other environment I have first hand knowledge (embedded and non embedded) - whether we are talking about gcc or other development systems, i had always found char by default is signed.

In other words a declaration such as :

{
char c ;
}

gave me an equivalent of:

{
signed char c ;
}

I realize both are perfectly legal. I also realize the whole rationale behind stdint.h etc.

Question is:

- are there other such major differences (default behavior) between the IAR view and others - gcc, MS Visual, Codewarrior ... etc.

cheers, srini

Beginning Microcontrollers with the MSP430

On 06/05/12 17:24, Rajagopalan wrote:
> All
>
> I am not an active programmer but just deal with many hands on
> programmers.
>
> I was shocked to find that the default for the char data type in IAR
> is unsigned. (Overridable of course!) In almost every other
> environment I have first hand knowledge (embedded and non embedded) -
> whether we are talking about gcc or other development systems, i had
> always found char by default is signed.
>
> In other words a declaration such as :
>
> { char c ; }
>
> gave me an equivalent of:
>
> { signed char c ; }
>
> I realize both are perfectly legal. I also realize the whole
> rationale behind stdint.h etc.
>
> Question is:
>
> - are there other such major differences (default behavior) between
> the IAR view and others - gcc, MS Visual, Codewarrior ... etc.
>
> cheers, srini

While it varies a bit, I think most embedded compilers use "unsigned" as
the default for char. Some processor's ABIs define specifically the
sign of "char", while others leave it up to the compiler. For several
small processors, "unsigned" chars can be more efficient (in particular,
the efficiency of comparing two 8-bit numbers can vary according to the
sign, and extending to integers can be different) - I know of none that
are more efficient at dealing with "signed" chars. And in embedded
programming, I suspect that the use of "unsigned" 8-bit data outweighs
the use of "signed" 8-bit data by a factor of at least 5.

If you ever use "char" to store numerical data, you should /always/
specify "signed char" or "unsigned char". Leave plain "char" for
characters in a string. You have nothing to lose, and lots to gain in
clarity and portability. If you care which type of "char" your compiler
uses by default, your code is almost certainly badly written.

And in the huge majority of cases when you think you might want a
"signed char" or "unsigned char", you would be better off using an
"uint8_t" or "int8_t" from . This again gives you clearer and
more portable code.

mvh.,

David