Difference between uint8_t and unsigned char
I'm using mplabX 4.20, and xc8 compiler. I'm trying to understand which is the difference between uint8_t and unsigned char. Both of them have size from 0 till 255.
Both of can hold characters and numbers. But which is better to use, and for which case?
Example if i want to create a buffer for holding a string.
uint8_t buffer[20]="Hello World";
unsigned char buffer[20]="Hello World";
In most cases i need to hold characters. Which is the best practise for this action?
c
add a comment |
I'm using mplabX 4.20, and xc8 compiler. I'm trying to understand which is the difference between uint8_t and unsigned char. Both of them have size from 0 till 255.
Both of can hold characters and numbers. But which is better to use, and for which case?
Example if i want to create a buffer for holding a string.
uint8_t buffer[20]="Hello World";
unsigned char buffer[20]="Hello World";
In most cases i need to hold characters. Which is the best practise for this action?
c
1
uint8_tis not a native data type; it is merely atypedefofunsigned char. But have a look at this! it might actually be a duplicate stackoverflow.com/questions/16138237/…
– PhoenixBlue
Nov 13 '18 at 13:42
add a comment |
I'm using mplabX 4.20, and xc8 compiler. I'm trying to understand which is the difference between uint8_t and unsigned char. Both of them have size from 0 till 255.
Both of can hold characters and numbers. But which is better to use, and for which case?
Example if i want to create a buffer for holding a string.
uint8_t buffer[20]="Hello World";
unsigned char buffer[20]="Hello World";
In most cases i need to hold characters. Which is the best practise for this action?
c
I'm using mplabX 4.20, and xc8 compiler. I'm trying to understand which is the difference between uint8_t and unsigned char. Both of them have size from 0 till 255.
Both of can hold characters and numbers. But which is better to use, and for which case?
Example if i want to create a buffer for holding a string.
uint8_t buffer[20]="Hello World";
unsigned char buffer[20]="Hello World";
In most cases i need to hold characters. Which is the best practise for this action?
c
c
asked Nov 13 '18 at 13:38
dddddd
357
357
1
uint8_tis not a native data type; it is merely atypedefofunsigned char. But have a look at this! it might actually be a duplicate stackoverflow.com/questions/16138237/…
– PhoenixBlue
Nov 13 '18 at 13:42
add a comment |
1
uint8_tis not a native data type; it is merely atypedefofunsigned char. But have a look at this! it might actually be a duplicate stackoverflow.com/questions/16138237/…
– PhoenixBlue
Nov 13 '18 at 13:42
1
1
uint8_t is not a native data type; it is merely a typedef of unsigned char. But have a look at this! it might actually be a duplicate stackoverflow.com/questions/16138237/…– PhoenixBlue
Nov 13 '18 at 13:42
uint8_t is not a native data type; it is merely a typedef of unsigned char. But have a look at this! it might actually be a duplicate stackoverflow.com/questions/16138237/…– PhoenixBlue
Nov 13 '18 at 13:42
add a comment |
2 Answers
2
active
oldest
votes
I'm using mplabX 4.20, and xc8 compiler. I'm trying to understand
which is the difference between uint8_t and unsigned char. Both of
them have size from 0 till 255. Both of can hold characters and
numbers. But which is better to use, and for which case?
unsigned char is the unsigned integer type corresponding to signed char. Its representation does not use any padding bits. Both of these occupy the same amount of storage as type char, which is at least 8 bits, but may be more. The macro CHAR_BIT tells you how many it comprises in your implementation. Every conforming C implementation provides all of these types.
uint8_t, if available, is an unsigned integer data type exactly 8 bits wide and with no padding bits. On an implementation having CHAR_BIT defined as 8, this is the same type as unsigned char. On such systems you may use the two types interchangeably wherever the declarations provided by stdint.h are in scope. On other systems, uint8_t will not be declared at all.
Example if i want to create a buffer for holding a string.
If you want to declare a buffer for holding a string then as a matter of style, you should use type char, not either of the other two:
char buffer[20] = "Hello World";
Although either of the other two, or signed char, can also be used for string data (provided in the case of uint8_t that the type is defined at all), type char is the conventional one to use for character data. Witness, for example, that that's the type in terms of which all the string.h functions are declared.
You should use uint8_t where and only where you need an integer type with exactly its properties: unsigned, 8 value bits, no padding bits.
You should use unsigned char where you want the smallest unsigned integer type available, but you don't care whether it is exactly 8 bits wide, or where you want to emphasize that it is the same size as a char -- the smallest discrete unit of storage available.
You should use signed char where you want the smallest signed integer type available but don't care about the exact size or representation.
You should use int8_t where you want a signed integer type with exactly 7 value bits, one sign bit, and no padding bits, expressed in two's complement representation.
You should remain mindful that uint8_t and int8_t are not guaranteed to be available from every C implementation, and that where they are available, their use requires inclusion of stdint.h. Furthermore, this header and these types were not part of C90 at all, so you should not use them if compatibility with legacy C implementations is important to you.
add a comment |
difference between uint8_t and unsigned char
If you're on a some exotic system where CHAR_BIT > 8, then uint8_t isn't going to be defined at all.
Otherwise (if CHAR_BIT == 8) there is no difference between unsigned char and uint8_t.
i need to hold characters
Then use plain char.
Functions that operate in strings usually have [const]char * parameters, and you won't be able to pass your unsigned char arrays to them.
@EricPostpischil Fair enough, edited.
– HolyBlackCat
Nov 13 '18 at 15:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282274%2fdifference-between-uint8-t-and-unsigned-char%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm using mplabX 4.20, and xc8 compiler. I'm trying to understand
which is the difference between uint8_t and unsigned char. Both of
them have size from 0 till 255. Both of can hold characters and
numbers. But which is better to use, and for which case?
unsigned char is the unsigned integer type corresponding to signed char. Its representation does not use any padding bits. Both of these occupy the same amount of storage as type char, which is at least 8 bits, but may be more. The macro CHAR_BIT tells you how many it comprises in your implementation. Every conforming C implementation provides all of these types.
uint8_t, if available, is an unsigned integer data type exactly 8 bits wide and with no padding bits. On an implementation having CHAR_BIT defined as 8, this is the same type as unsigned char. On such systems you may use the two types interchangeably wherever the declarations provided by stdint.h are in scope. On other systems, uint8_t will not be declared at all.
Example if i want to create a buffer for holding a string.
If you want to declare a buffer for holding a string then as a matter of style, you should use type char, not either of the other two:
char buffer[20] = "Hello World";
Although either of the other two, or signed char, can also be used for string data (provided in the case of uint8_t that the type is defined at all), type char is the conventional one to use for character data. Witness, for example, that that's the type in terms of which all the string.h functions are declared.
You should use uint8_t where and only where you need an integer type with exactly its properties: unsigned, 8 value bits, no padding bits.
You should use unsigned char where you want the smallest unsigned integer type available, but you don't care whether it is exactly 8 bits wide, or where you want to emphasize that it is the same size as a char -- the smallest discrete unit of storage available.
You should use signed char where you want the smallest signed integer type available but don't care about the exact size or representation.
You should use int8_t where you want a signed integer type with exactly 7 value bits, one sign bit, and no padding bits, expressed in two's complement representation.
You should remain mindful that uint8_t and int8_t are not guaranteed to be available from every C implementation, and that where they are available, their use requires inclusion of stdint.h. Furthermore, this header and these types were not part of C90 at all, so you should not use them if compatibility with legacy C implementations is important to you.
add a comment |
I'm using mplabX 4.20, and xc8 compiler. I'm trying to understand
which is the difference between uint8_t and unsigned char. Both of
them have size from 0 till 255. Both of can hold characters and
numbers. But which is better to use, and for which case?
unsigned char is the unsigned integer type corresponding to signed char. Its representation does not use any padding bits. Both of these occupy the same amount of storage as type char, which is at least 8 bits, but may be more. The macro CHAR_BIT tells you how many it comprises in your implementation. Every conforming C implementation provides all of these types.
uint8_t, if available, is an unsigned integer data type exactly 8 bits wide and with no padding bits. On an implementation having CHAR_BIT defined as 8, this is the same type as unsigned char. On such systems you may use the two types interchangeably wherever the declarations provided by stdint.h are in scope. On other systems, uint8_t will not be declared at all.
Example if i want to create a buffer for holding a string.
If you want to declare a buffer for holding a string then as a matter of style, you should use type char, not either of the other two:
char buffer[20] = "Hello World";
Although either of the other two, or signed char, can also be used for string data (provided in the case of uint8_t that the type is defined at all), type char is the conventional one to use for character data. Witness, for example, that that's the type in terms of which all the string.h functions are declared.
You should use uint8_t where and only where you need an integer type with exactly its properties: unsigned, 8 value bits, no padding bits.
You should use unsigned char where you want the smallest unsigned integer type available, but you don't care whether it is exactly 8 bits wide, or where you want to emphasize that it is the same size as a char -- the smallest discrete unit of storage available.
You should use signed char where you want the smallest signed integer type available but don't care about the exact size or representation.
You should use int8_t where you want a signed integer type with exactly 7 value bits, one sign bit, and no padding bits, expressed in two's complement representation.
You should remain mindful that uint8_t and int8_t are not guaranteed to be available from every C implementation, and that where they are available, their use requires inclusion of stdint.h. Furthermore, this header and these types were not part of C90 at all, so you should not use them if compatibility with legacy C implementations is important to you.
add a comment |
I'm using mplabX 4.20, and xc8 compiler. I'm trying to understand
which is the difference between uint8_t and unsigned char. Both of
them have size from 0 till 255. Both of can hold characters and
numbers. But which is better to use, and for which case?
unsigned char is the unsigned integer type corresponding to signed char. Its representation does not use any padding bits. Both of these occupy the same amount of storage as type char, which is at least 8 bits, but may be more. The macro CHAR_BIT tells you how many it comprises in your implementation. Every conforming C implementation provides all of these types.
uint8_t, if available, is an unsigned integer data type exactly 8 bits wide and with no padding bits. On an implementation having CHAR_BIT defined as 8, this is the same type as unsigned char. On such systems you may use the two types interchangeably wherever the declarations provided by stdint.h are in scope. On other systems, uint8_t will not be declared at all.
Example if i want to create a buffer for holding a string.
If you want to declare a buffer for holding a string then as a matter of style, you should use type char, not either of the other two:
char buffer[20] = "Hello World";
Although either of the other two, or signed char, can also be used for string data (provided in the case of uint8_t that the type is defined at all), type char is the conventional one to use for character data. Witness, for example, that that's the type in terms of which all the string.h functions are declared.
You should use uint8_t where and only where you need an integer type with exactly its properties: unsigned, 8 value bits, no padding bits.
You should use unsigned char where you want the smallest unsigned integer type available, but you don't care whether it is exactly 8 bits wide, or where you want to emphasize that it is the same size as a char -- the smallest discrete unit of storage available.
You should use signed char where you want the smallest signed integer type available but don't care about the exact size or representation.
You should use int8_t where you want a signed integer type with exactly 7 value bits, one sign bit, and no padding bits, expressed in two's complement representation.
You should remain mindful that uint8_t and int8_t are not guaranteed to be available from every C implementation, and that where they are available, their use requires inclusion of stdint.h. Furthermore, this header and these types were not part of C90 at all, so you should not use them if compatibility with legacy C implementations is important to you.
I'm using mplabX 4.20, and xc8 compiler. I'm trying to understand
which is the difference between uint8_t and unsigned char. Both of
them have size from 0 till 255. Both of can hold characters and
numbers. But which is better to use, and for which case?
unsigned char is the unsigned integer type corresponding to signed char. Its representation does not use any padding bits. Both of these occupy the same amount of storage as type char, which is at least 8 bits, but may be more. The macro CHAR_BIT tells you how many it comprises in your implementation. Every conforming C implementation provides all of these types.
uint8_t, if available, is an unsigned integer data type exactly 8 bits wide and with no padding bits. On an implementation having CHAR_BIT defined as 8, this is the same type as unsigned char. On such systems you may use the two types interchangeably wherever the declarations provided by stdint.h are in scope. On other systems, uint8_t will not be declared at all.
Example if i want to create a buffer for holding a string.
If you want to declare a buffer for holding a string then as a matter of style, you should use type char, not either of the other two:
char buffer[20] = "Hello World";
Although either of the other two, or signed char, can also be used for string data (provided in the case of uint8_t that the type is defined at all), type char is the conventional one to use for character data. Witness, for example, that that's the type in terms of which all the string.h functions are declared.
You should use uint8_t where and only where you need an integer type with exactly its properties: unsigned, 8 value bits, no padding bits.
You should use unsigned char where you want the smallest unsigned integer type available, but you don't care whether it is exactly 8 bits wide, or where you want to emphasize that it is the same size as a char -- the smallest discrete unit of storage available.
You should use signed char where you want the smallest signed integer type available but don't care about the exact size or representation.
You should use int8_t where you want a signed integer type with exactly 7 value bits, one sign bit, and no padding bits, expressed in two's complement representation.
You should remain mindful that uint8_t and int8_t are not guaranteed to be available from every C implementation, and that where they are available, their use requires inclusion of stdint.h. Furthermore, this header and these types were not part of C90 at all, so you should not use them if compatibility with legacy C implementations is important to you.
answered Nov 13 '18 at 14:37
John BollingerJohn Bollinger
79.4k74075
79.4k74075
add a comment |
add a comment |
difference between uint8_t and unsigned char
If you're on a some exotic system where CHAR_BIT > 8, then uint8_t isn't going to be defined at all.
Otherwise (if CHAR_BIT == 8) there is no difference between unsigned char and uint8_t.
i need to hold characters
Then use plain char.
Functions that operate in strings usually have [const]char * parameters, and you won't be able to pass your unsigned char arrays to them.
@EricPostpischil Fair enough, edited.
– HolyBlackCat
Nov 13 '18 at 15:32
add a comment |
difference between uint8_t and unsigned char
If you're on a some exotic system where CHAR_BIT > 8, then uint8_t isn't going to be defined at all.
Otherwise (if CHAR_BIT == 8) there is no difference between unsigned char and uint8_t.
i need to hold characters
Then use plain char.
Functions that operate in strings usually have [const]char * parameters, and you won't be able to pass your unsigned char arrays to them.
@EricPostpischil Fair enough, edited.
– HolyBlackCat
Nov 13 '18 at 15:32
add a comment |
difference between uint8_t and unsigned char
If you're on a some exotic system where CHAR_BIT > 8, then uint8_t isn't going to be defined at all.
Otherwise (if CHAR_BIT == 8) there is no difference between unsigned char and uint8_t.
i need to hold characters
Then use plain char.
Functions that operate in strings usually have [const]char * parameters, and you won't be able to pass your unsigned char arrays to them.
difference between uint8_t and unsigned char
If you're on a some exotic system where CHAR_BIT > 8, then uint8_t isn't going to be defined at all.
Otherwise (if CHAR_BIT == 8) there is no difference between unsigned char and uint8_t.
i need to hold characters
Then use plain char.
Functions that operate in strings usually have [const]char * parameters, and you won't be able to pass your unsigned char arrays to them.
edited Nov 13 '18 at 15:32
answered Nov 13 '18 at 14:00
HolyBlackCatHolyBlackCat
15.9k33363
15.9k33363
@EricPostpischil Fair enough, edited.
– HolyBlackCat
Nov 13 '18 at 15:32
add a comment |
@EricPostpischil Fair enough, edited.
– HolyBlackCat
Nov 13 '18 at 15:32
@EricPostpischil Fair enough, edited.
– HolyBlackCat
Nov 13 '18 at 15:32
@EricPostpischil Fair enough, edited.
– HolyBlackCat
Nov 13 '18 at 15:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282274%2fdifference-between-uint8-t-and-unsigned-char%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
uint8_tis not a native data type; it is merely atypedefofunsigned char. But have a look at this! it might actually be a duplicate stackoverflow.com/questions/16138237/…– PhoenixBlue
Nov 13 '18 at 13:42