how to define an 8 bit integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DanielJohnson

    how to define an 8 bit integer

    I have seen many legacy code use uint8_t or uint16_t to override the
    default compiler setting of an integer's length.

    I am using gcc on Linux and a sizeof(int) gives me 4. I want the
    ability to define an 8, 16 or 32 bit integer.

    I tried using uint8_t but the gcc doesn't like it. Do I need to modify
    any setting or declare typdefes.

    Please guide me. Your answer is greatly appreciated.

    Thanks,
  • James Kuyper

    #2
    Re: how to define an 8 bit integer

    DanielJohnson wrote:
    I have seen many legacy code use uint8_t or uint16_t to override the
    default compiler setting of an integer's length.
    >
    I am using gcc on Linux and a sizeof(int) gives me 4. I want the
    ability to define an 8, 16 or 32 bit integer.
    >
    I tried using uint8_t but the gcc doesn't like it. Do I need to modify
    any setting or declare typdefes.
    By default, gcc compiles for for a non-conforming version of C that is
    sort of like C90 with many features that are similar, but not always
    identical, to features that were added to C99. uint8_t was added in C99.
    Use -std=c99 to turn on support for C99. Add -pedantic to come a little
    closer to being fully conforming to C99.

    If for any reason you can't use C99, use the following:

    typedef unsigned char uint8;

    If a compiler supports any unsigned 8-bit integer type, unsigned char
    will be such a type. If the compiler has no 8-bit integer type,
    'unsigned char' is going to be the best approximation possible for that
    compiler.

    Comment

    • Antoninus Twink

      #3
      Re: how to define an 8 bit integer

      On 25 Oct 2008 at 19:56, DanielJohnson wrote:
      I am using gcc on Linux and a sizeof(int) gives me 4. I want the
      ability to define an 8, 16 or 32 bit integer.
      >
      I tried using uint8_t but the gcc doesn't like it. Do I need to modify
      any setting or declare typdefes.
      All these arcane portability issues have been thought of, solved, and
      painfully debugged by the creators of things like the GNU autotools, so
      why reinvent the wheel?

      Look at the autoconf macros AC_TYPE_INT8_T, AC_TYPE_INT16_T ,
      AC_TYPE_INT32_T and AC_TYPE_INT64_T .

      Comment

      • Malcolm McLean

        #4
        Re: how to define an 8 bit integer


        "DanielJohn son" <diffuser78@gma il.comwrote in message
        I am using gcc on Linux and a sizeof(int) gives me 4. I want the
        ability to define an 8, 16 or 32 bit integer.
        >
        Why?

        --
        Free games and programming goodies.


        Comment

        • Keith Thompson

          #5
          Re: how to define an 8 bit integer

          James Kuyper <jameskuyper@ve rizon.netwrites :
          DanielJohnson wrote:
          >I have seen many legacy code use uint8_t or uint16_t to override the
          >default compiler setting of an integer's length.
          >I am using gcc on Linux and a sizeof(int) gives me 4. I want the
          >ability to define an 8, 16 or 32 bit integer.
          >I tried using uint8_t but the gcc doesn't like it. Do I need to
          >modify
          >any setting or declare typdefes.
          >
          By default, gcc compiles for for a non-conforming version of C that is
          sort of like C90 with many features that are similar, but not always
          identical, to features that were added to C99. uint8_t was added in
          C99. Use -std=c99 to turn on support for C99. Add -pedantic to come a
          little closer to being fully conforming to C99.
          I think the problem is that he doesn't have "#include <stdint.h>".
          Add that to the top of the file, and uint8_t becomes visible --
          assuming the implementation provides <stdint.h>.

          On my system, this works whether you use gcc's partial C99 mode or not
          -- which is valid behavior, since it's a standard header in C99 and a
          permitted extension in C90.

          Incidentally, using uint8_t or uint16_t doesn't override anything.
          The predefined types are still there, and their sizes don't change for
          a given implementation. uint8_t and uint16_t, if they exist, are
          nothing more than typedefs for existing predefined types (typically
          unsigned char and unsigned short, respectively).

          An implementation note: the <stdint.hhead er isn't provided by gcc,
          it's provided by the library. On my system, the library is glibc,
          which does provide <stdint.h>. On another system, a different library
          might not provide this header. I suspect that <stdint.hwill be
          available on *most* modern implementations , but it's not guaranteed
          unless the implementation claims conformance to C99.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Martin Ambuhl

            #6
            Re: how to define an 8 bit integer

            DanielJohnson wrote:
            I have seen many legacy code use uint8_t or uint16_t to override the
            default compiler setting of an integer's length.
            #include <stdint.h>

            Comment

            • Bartc

              #7
              Re: how to define an 8 bit integer


              "Malcolm McLean" <regniztar@btin ternet.comwrote in message
              news:pP6dnV_QAa uPH57UnZ2dnUVZ8 oadnZ2d@bt.com. ..
              >
              "DanielJohn son" <diffuser78@gma il.comwrote in message
              >I am using gcc on Linux and a sizeof(int) gives me 4. I want the
              >ability to define an 8, 16 or 32 bit integer.
              >>
              Why?
              If he has lots of them (as in an array), then it might be useful to only
              require a quarter or an eighth of the memory for example.

              If he has to talk to some software/hardware that uses specific integer
              widths then again it would be handy.

              --
              Bartc

              Comment

              • Malcolm McLean

                #8
                Re: how to define an 8 bit integer

                "Bartc" <bc@freeuk.comw rote in message news:
                >
                "Malcolm McLean" <regniztar@btin ternet.comwrote in message
                news:pP6dnV_QAa uPH57UnZ2dnUVZ8 oadnZ2d@bt.com. ..
                >>
                >"DanielJohnson " <diffuser78@gma il.comwrote in message
                >>I am using gcc on Linux and a sizeof(int) gives me 4. I want the
                >>ability to define an 8, 16 or 32 bit integer.
                >>>
                >Why?
                >
                If he has lots of them (as in an array), then it might be useful to only
                require a quarter or an eighth of the memory for example.
                >
                If he has to talk to some software/hardware that uses specific integer
                widths then again it would be handy.
                >
                These are possible reasons.
                However let the OP answer for himself.

                --
                Free games and programming goodies.


                Comment

                • Phil Carmody

                  #9
                  Re: how to define an 8 bit integer

                  "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                  "DanielJohn son" <diffuser78@gma il.comwrote in message
                  >I am using gcc on Linux and a sizeof(int) gives me 4. I want the
                  >ability to define an 8, 16 or 32 bit integer.
                  >
                  Why?
                  Why do you need to know his reasons? Is it that you don't
                  believe him? Do you treat all posters with equal mistrust,
                  and do you expect others to treat you with the same mistrust?

                  He wants the ability to do the above. If he #includes stdint.h,
                  he'll have his wants most easily satisfied, no matter what
                  his reasons were. If he grabs a decent book in C, then he'll
                  probably have his wants satisfied far more quickly in the
                  future.

                  Phil
                  --
                  The fact that a believer is happier than a sceptic is no more to the
                  point than the fact that a drunken man is happier than a sober one.
                  The happiness of credulity is a cheap and dangerous quality.
                  -- George Bernard Shaw (1856-1950), Preface to Androcles and the Lion

                  Comment

                  • Malcolm McLean

                    #10
                    Re: how to define an 8 bit integer


                    "Phil Carmody" <thefatphil_dem unged@yahoo.co. ukwrote in message news:
                    "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                    >"DanielJohnson " <diffuser78@gma il.comwrote in message
                    >>I am using gcc on Linux and a sizeof(int) gives me 4. I want the
                    >>ability to define an 8, 16 or 32 bit integer.
                    >>
                    >Why?
                    >
                    Why do you need to know his reasons? Is it that you don't
                    believe him? Do you treat all posters with equal mistrust,
                    and do you expect others to treat you with the same mistrust?
                    >
                    Because the number of people who think they need integers of a certain width
                    is much greater than the number who actually do.
                    As Bartc pointed out, there can be good reasons for wanting a guaranteed
                    8-bit type, but they are rare.

                    --
                    Free games and programming goodies.


                    Comment

                    • Ian Collins

                      #11
                      Re: how to define an 8 bit integer

                      Malcolm McLean wrote:
                      >
                      "Phil Carmody" <thefatphil_dem unged@yahoo.co. ukwrote in message news:
                      >"Malcolm McLean" <regniztar@btin ternet.comwrite s:
                      >>"DanielJohnso n" <diffuser78@gma il.comwrote in message
                      >>>I am using gcc on Linux and a sizeof(int) gives me 4. I want the
                      >>>ability to define an 8, 16 or 32 bit integer.
                      >>>
                      >>Why?
                      >>
                      >Why do you need to know his reasons? Is it that you don't
                      >believe him? Do you treat all posters with equal mistrust,
                      >and do you expect others to treat you with the same mistrust?
                      >>
                      Because the number of people who think they need integers of a certain
                      width is much greater than the number who actually do.
                      As Bartc pointed out, there can be good reasons for wanting a guaranteed
                      8-bit type, but they are rare.
                      Not in my world (that of a driver writer) or that of most embedded
                      programmers. Considering a large proportion of C programmers are
                      embedded programmers, the need for fixed width types is much greater
                      than you think.

                      --
                      Ian Collins

                      Comment

                      • CBFalconer

                        #12
                        Re: how to define an 8 bit integer

                        DanielJohnson wrote:
                        >
                        I have seen many legacy code use uint8_t or uint16_t to override
                        the default compiler setting of an integer's length.
                        >
                        I am using gcc on Linux and a sizeof(int) gives me 4. I want the
                        ability to define an 8, 16 or 32 bit integer.
                        >
                        I tried using uint8_t but the gcc doesn't like it. Do I need to
                        modify any setting or declare typdefes.
                        >
                        Please guide me. Your answer is greatly appreciated.
                        uint8_t etc. are not guaranteed available. The guaranteed integer
                        types are char, short, int, long. C99 adds long long. These can
                        all be signed or unsigned.

                        Code using uint8_t is inherently non-portable. bytes can be larger
                        than 8 bits. See <limits.hfor the sizes available on your
                        system, expressed by MAX and MINs for the types.

                        --
                        [mail]: Chuck F (cbfalconer at maineline dot net)
                        [page]: <http://cbfalconer.home .att.net>
                        Try the download section.

                        Comment

                        • Wolfgang Draxinger

                          #13
                          Re: how to define an 8 bit integer

                          DanielJohnson wrote:
                          I tried using uint8_t but the gcc doesn't like it. Do I need to
                          modify any setting or declare typdefes.
                          Using GCC, adding

                          #include <stdint.h>

                          should do the trick. Like already told by the other posters, it
                          may not be avaliable, so add some checks to your source, and
                          provide alternative ways to define those types.

                          In the end it will boild down to some typedefs from primitive
                          types, which have been exactly matched to target architecture
                          and compiler. That's how stdint.h works.

                          Wolfgang Draxinger
                          --
                          E-Mail address works, Jabber: hexarith@jabber .org, ICQ: 134682867

                          Comment

                          • Tor Rustad

                            #14
                            Re: how to define an 8 bit integer

                            DanielJohnson wrote:
                            I have seen many legacy code use uint8_t or uint16_t to override the
                            default compiler setting of an integer's length.
                            >
                            I am using gcc on Linux and a sizeof(int) gives me 4. I want the
                            ability to define an 8, 16 or 32 bit integer.
                            >
                            I tried using uint8_t but the gcc doesn't like it. Do I need to modify
                            any setting or declare typdefes.
                            >
                            Please guide me. Your answer is greatly appreciated.
                            I haven't starting using C99 features yet, but I see that all here talk
                            about <stdint.hfor some weird reason. :)

                            For hosted systems, <inttypes.his the header file you are looking for.
                            In addition to including <stdint.h>, <inttypes.had d macros and useful
                            conversion functions.


                            $ cat use_uint8_t.c
                            #include <inttypes.h>
                            #include <stdio.h>

                            int main(void)
                            {
                            uint8_t byte;
                            uint16_t word;

                            printf("%d\n", (int)sizeof (byte));
                            printf("%d\n", (int)sizeof (word));

                            return 0;
                            }
                            $ gcc -std=c99 use_uint8_t.c
                            $ ./a.out
                            1
                            2
                            $ gcc -v
                            Using built-in specs.
                            Target: i486-linux-gnu
                            Configured with: ../src/configure -v
                            --enable-languages=c,c++ ,fortran,objc,o bj-c++,treelang --prefix=/usr
                            --enable-shared --with-system-zlib --libexecdir=/usr/lib
                            --without-included-gettext --enable-threads=posix --enable-nls
                            --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
                            --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc
                            --enable-mpfr --enable-targets=all --enable-checking=releas e
                            --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
                            Thread model: posix
                            gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)

                            --
                            Tor <echo bwzcab@wvtqvm.v w | tr i-za-h a-z>

                            Comment

                            • Keith Thompson

                              #15
                              Re: how to define an 8 bit integer

                              Tor Rustad <bwzcab@wvtqvm. vwwrites:
                              [...]
                              I haven't starting using C99 features yet, but I see that all here
                              talk about <stdint.hfor some weird reason. :)
                              >
                              For hosted systems, <inttypes.his the header file you are looking
                              for. In addition to including <stdint.h>, <inttypes.had d macros and
                              useful conversion functions.
                              [...]

                              And if you don't happen to need those macros and conversion functions,
                              even on a hosted system, why not use <stdint.h>?

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              Nokia
                              "We must do something. This is something. Therefore, we must do this."
                              -- Antony Jay and Jonathan Lynn, "Yes Minister"

                              Comment

                              Working...