Difference between uint8_t and unsigned char












0















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?










share|improve this question


















  • 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


















0















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?










share|improve this question


















  • 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
















0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 13:38









dddddd

357




357








  • 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
















  • 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










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














2 Answers
2






active

oldest

votes


















2















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.






share|improve this answer































    1















    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.






    share|improve this answer


























    • @EricPostpischil Fair enough, edited.

      – HolyBlackCat
      Nov 13 '18 at 15:32













    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2















    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.






    share|improve this answer




























      2















      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.






      share|improve this answer


























        2












        2








        2








        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.






        share|improve this answer














        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 14:37









        John BollingerJohn Bollinger

        79.4k74075




        79.4k74075

























            1















            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.






            share|improve this answer


























            • @EricPostpischil Fair enough, edited.

              – HolyBlackCat
              Nov 13 '18 at 15:32


















            1















            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.






            share|improve this answer


























            • @EricPostpischil Fair enough, edited.

              – HolyBlackCat
              Nov 13 '18 at 15:32
















            1












            1








            1








            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.






            share|improve this answer
















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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





















            • @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




















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly