uint8_t or char

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all:

uint8_t and char are the same thing, are they not?

Why people use two different terms for one variable type?

Thank you :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No they are not the same, uint8_t is a typdef of "unsigned char" while "char" has no specification of its signedness

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, I see. Thank you clawson.

It was in fact a type error, I meant to type unsigned char. But anyway you answered my question.

Cheers clawson :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, on AVR that's true, but the C spec goes a little further; uint8_t is guaranteed to be 8-bit, while char is the smallest addressable type. It's possible to have a standard conforming platform with a wider char type, but no uint8_t type (though uint_least8_t would still be equivalent to unsigned char).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
It was in fact a type error, I meant to type unsigned char. But anyway you answered my question

Yes but that would have made it a completely DIFFERENT question in fact! In essence "unsigned char" and "uint8_t" are identical as "uint8_t" is just a substitution for "unsigned char" anyway.

HOWEVER the use of the (u)intN_t types isn't so useful when dealing with u_chars as it's a pretty fair bet that they are 8 bit on most architectures (though a recent thread identified some esoteric exceptions). However where it becomes more useful is in the use of int, short, long etc.

If I ask you "what's the numerical range of an 'int' on an AVR?" would you immediately know the answer? How about if I asked you the same question about an ARM?

As it happens '(unsigned) int' on an AVR is 16 bits wide so the unsigned has a numerical range of 0 to 65535 and the signed has a range of -32768..+32767 but if you just saw:

int fred;

in some code this may not be immediately obvious to you. However if the programmer had used the C99 standard type:

int16_t fred;

there would be NO DOUBT WHATSOEVER about the numerical range of 'fred' it's most definitely going to be able to hold that -32768..+32767 and if you saw the same thing on a 32bit ARm or some weird and wonderful 64 bit CPU it would still be equally true.

So it not only makes the code more self documenting it also makes it more portable between architectures.

Anyway uint8_t is shorter to type than "unsigned char" ! ;)

By the way, over the years, until a standard was defined there have been many variants of this idea: UI8, I16, UI32 is one example or int8, uint16, int32 is another but as there's now a standard for this it's probably best to adhere to it (though I find myself using UI32 an awful lot in fact!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Anyway uint8_t is shorter to type than "unsigned char" ! ;)

Yabbut, Cliff, I can *spell* 'unsigned char' :mrgreen:

Neil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Unfortunately for me most of my chars are actually unsinged! (well they are until I start using them)

(actually most of the editors I use are color syntax highlighting so I know immediately when I've mis-typed it - but I don't often get UI8 wrong)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

barnacle wrote:

clawson wrote:

Anyway uint8_t is shorter to type than "unsigned char" !

Yabbut, Cliff, I can *spell* 'unsigned char'

And to think it was just the day before yesterday that you ranted over the over-wordy-ness of C# programs... :wink:

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Foolish consistency is the hob-goblin of little minds - Ralph Waldo Emmerson :mrgreen:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
By the way, over the years, until a standard was defined there have been many variants of this idea: UI8, I16, UI32 is one example or int8, uint16, int32 is another but as there's now a standard for this it's probably best to adhere to it (though I find myself using UI32 an awful lot in fact!)

May I second that; especially in libraries. Interface of avr-libc is excellent in this regard.

IMHO use of good old int as much as possible and integers of specific width only when required denotes good programming style.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use of compiler specific abbreviations for data size and type will cause grief in the long run. Especially for those data types that already have a "standard" keywords and definitions. Even if "your" compiler supports these abbreviated types, I would recommend using the standard C datatypes for consistancy. Also be aware that other compilers may not recognize a special typedef that your present complier recognizes.
Also, as I've state before, be specific if defining signed/unsigned types - Why not ? They are part of the C standard.

Also, instead of "char", how about "unsigned char", or "signed char". It really isn't that much trouble - is it ? The vast majority of your typing will be in the body of your code, not the variable definitions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To be clear though, uintX_t and intX_t are standard C datatypes (as long as you use the current standard).

Quote:
Also, instead of "char", how about "unsigned char", or "signed char".

Because there is in fact a difference between them and many standard functions specifically use char.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Removed spam.

Last Edited: Thu. Oct 29, 2015 - 06:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, visual. You can not be sure a char is equivalent to uint8_t. Or to int8_t, signed char or unsigned char. To one or two of those yes, but it is implementors choice as to whether the simple char is signed or unsigned.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seven years late but it's still good info. wink

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well.. That was in response to "vish" who posted some half-arsed mumbo about chars and 8-bit types. I never spotted the spam, as I was on my phone. And the phone "corrected" 'vish' to 'visual'. 

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]