1 byte for character

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adityavasishth@gmail.com

    #1

    1 byte for character

    hi all,

    Characters are basically implemented via integers,ex : '\0' is 0.But
    integers requires 2 bytes and the characters require only 1
    byte.So,can anybody please tell me that how the charcters are
    implemented via integers.

    Thanks,
    Aditya

  • Yan Ivnitskiy

    #2
    Re: 1 byte for character

    adityavasishth@ gmail.com wrote:[color=blue]
    > hi all,
    >
    > Characters are basically implemented via integers,ex : '\0' is 0.But
    > integers requires 2 bytes and the characters require only 1
    > byte.So,can anybody please tell me that how the charcters are
    > implemented via integers.[/color]

    let's say you assign 1 to a 2-byte int (i think they're 4 bytes on most
    x86 machines)
    int a = 1;
    now, a will, in binary, be: 00000000 00000000 00000000 00000001
    if you assign that to a char, i.e.
    char a = 1;, you'll just have 00000001 in memory

    So you can assign the value to the actual bytes, but you can't assign
    value that go outside the range, i.e. -128 to 127
    [color=blue]
    >
    > Thanks,
    > Aditya
    >[/color]

    Comment

    • Andrey Tarasevich

      #3
      Re: 1 byte for character

      adityavasishth@ gmail.com wrote:[color=blue]
      > ...
      > Characters are basically implemented via integers,ex : '\0' is 0.But
      > integers requires 2 bytes and the characters require only 1
      > byte.So,can anybody please tell me that how the charcters are
      > implemented via integers.
      > ...[/color]

      The answer is very simple: characters are NOT implemented via integers.
      (Where did you get this idea?) For this reason your question can be
      dismissed as based on an invalid premise.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Michael Mair

        #4
        Re: 1 byte for character



        Andrey Tarasevich wrote:[color=blue]
        > adityavasishth@ gmail.com wrote:
        >[color=green]
        >>...
        >>Characters are basically implemented via integers,ex : '\0' is 0.But
        >>integers requires 2 bytes and the characters require only 1
        >>byte.So,can anybody please tell me that how the charcters are
        >>implemented via integers.
        >>...[/color][/color]

        I think you should clarify your question.
        Some things to help you along:
        - character constants, such as '0', '\0', ' ', 'a' are of type
        int (as they are only seen as a different representation of
        an integral number) but are guaranteed to "fit" into a char.
        - chars consist of CHAR_BIT bits.
        - ints consist of sizeof(int) chars and consequently of
        sizeof(int)*CHA R_BIT bits. sizeof(int)>=1. It can be 1,2,4
        or whatever makes sense on the respective platform and for
        the implementation.
        Now, do you want to know how to "extract" sizeof(int) different
        chars from an int or do you want to know how the mapping
        between numbers and characters works or was this information
        enough for you or ...?
        [color=blue]
        > The answer is very simple: characters are NOT implemented via integers.
        > (Where did you get this idea?) For this reason your question can be
        > dismissed as based on an invalid premise.[/color]

        Depends. If the processor word size and minimal addressable
        unit is >=16 Bit, the implementation could decide to give
        you a char type implemented in software. Whether that is also
        what the OS (if any) "does" ... *shrug*.


        Cheers
        Michael
        --
        E-Mail: Mine is a gmx dot de address.

        Comment

        • Keith Thompson

          #5
          Re: 1 byte for character

          Andrey Tarasevich <andreytarasevi ch@hotmail.com> writes:[color=blue]
          > adityavasishth@ gmail.com wrote:[color=green]
          >> ...
          >> Characters are basically implemented via integers,ex : '\0' is 0.But
          >> integers requires 2 bytes and the characters require only 1
          >> byte.So,can anybody please tell me that how the charcters are
          >> implemented via integers.
          >> ...[/color]
          >
          > The answer is very simple: characters are NOT implemented via integers.
          > (Where did you get this idea?) For this reason your question can be
          > dismissed as based on an invalid premise.[/color]

          Andrey is mistaken; characters are implemented as integers. (He may
          have meant that characters are not implemented as ints; read on.)

          In particular, the types "char", "signed char", and "unsigned char"
          are integer types. (Other integer types include short, int, long, and
          their unsigned variants.)

          Note the distinction between the term "integer", which covers a
          variety of types, and the term "int", which is one particular integer
          type.

          The type "int" can be 2 bytes on some systems. It can be 4 or more
          bytes on other systems. A byte (as C uses the term) is at least 8
          bits, but can be larger. If a byte is at least 16 bits, an int can be
          a single byte, though this is rare.

          So a character (type "char") is an integer, it's just a relatively
          small one.

          For more information, see any good C textbook. Kernighan & Ritchie's
          _The C Programming Language_, 2nd Edition, is excellent. I also
          recommend the C FAQ, <http://www.eskimo.com/~scs/C-faq/faq.html>.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Andrey Tarasevich

            #6
            Re: 1 byte for character

            Keith Thompson wrote:[color=blue][color=green][color=darkred]
            >>> ...
            >>> Characters are basically implemented via integers,ex : '\0' is 0.But
            >>> integers requires 2 bytes and the characters require only 1
            >>> byte.So,can anybody please tell me that how the charcters are
            >>> implemented via integers.
            >>> ...[/color]
            >>
            >> The answer is very simple: characters are NOT implemented via integers.
            >> (Where did you get this idea?) For this reason your question can be
            >> dismissed as based on an invalid premise.[/color]
            >
            > Andrey is mistaken; characters are implemented as integers. (He may
            > have meant that characters are not implemented as ints; read on.)[/color]

            You are right. Thans for the correction. I was subconsciously sticking
            to the terminology used by the OP. Apparently the OP also used the term
            "integers" to mean "ints" ("... integers requires 2 bytes ...").

            --
            Best regards,
            Andrey Tarasevich

            Comment

            • Zoran Cutura

              #7
              Re: 1 byte for character

              adityavasishth@ gmail.com wrote:[color=blue]
              > hi all,
              >
              > Characters are basically implemented via integers,ex : '\0' is 0.But
              > integers requires 2 bytes and the characters require only 1
              > byte.So,can anybody please tell me that how the charcters are
              > implemented via integers.[/color]

              Whoever told you that integers are 2 bytes lied. And whoever told you
              that characters are only 1 byte large did too.

              The type char although its name probably implies something else is a
              integer type. In C a char is by definition 1 byte large. This must not
              be confused with characters in the sense of written letters or symbols.
              Also note that 1 byte in C need not necessarily be 8 bits, but in many
              implementations actually is.

              A character on the other hand is something the implementation (your
              computer in conjunction with the used compiler and environment) is to
              interpret on one way or the other. How characters are interpreted is not
              actually defined bye the C-standard. There are only some constraints
              given to the implementation, so that a basic set of characters is made
              available by all implementations claiming conformance.

              One of the constraints given by the standard is that the representation
              of a character from that basic set must fit into a byte, which is it
              must fit in to a variable of type "char". But as you probably can
              imagine a implementation might give you some optional characters that
              would extend the basic set. The represetations of such characters need
              not fit into a byte/char. But if the implementation should be able to
              allow for literals in your programs for example it should also be able
              to store characters into integer variables that are not of type char but
              bigger. Therefor literal characters are of type int which is a integer
              type that is as big or bigger than short, which depending on the
              implementation might mean that it is 1 byte (if a byte would consist of
              at least 16 value bits) large but might also be 2, 3, 4, or any other.

              You might have recopgnized that a value that can be stored in 1 byte
              can always be stored in two bytes or more.

              I hope this doesn't confuse you even more, but if it does, don't
              hasitate to ask for more clearification, I'm sure some regulars in CLC
              will point you in the right direction.

              --
              Z (_Zoran_._Cutur a_@_daimler_chr ysler_.com) (remove underscores)
              "LISP is worth learning for the profound enlightenment experience
              you will have when you finally get it; that experience will make you
              a better programmer for the rest of your days." -- Eric S. Raymond

              Comment

              • Lawrence Kirby

                #8
                Re: 1 byte for character

                On Thu, 09 Dec 2004 19:19:26 +0000, Yan Ivnitskiy wrote:

                ....
                [color=blue]
                > let's say you assign 1 to a 2-byte int (i think they're 4 bytes on most
                > x86 machines)
                > int a = 1;
                > now, a will, in binary, be: 00000000 00000000 00000000 00000001
                > if you assign that to a char, i.e.
                > char a = 1;, you'll just have 00000001 in memory
                >
                > So you can assign the value to the actual bytes, but you can't assign
                > value that go outside the range, i.e. -128 to 127[/color]

                That is one possible range of values for the type char, but there are
                other. char can be wider than 8 bits, even when it is 8 bits it can have
                an unsigned representation with the range 0 to 255. If it is signed and 8
                bits the range could be -127 to 127.

                Lawrence

                Comment

                • pete

                  #9
                  Re: 1 byte for character

                  adityavasishth@ gmail.com wrote:[color=blue]
                  >
                  > hi all,
                  >
                  > Characters are basically implemented via integers,ex : '\0' is 0.But
                  > integers requires 2 bytes and the characters require only 1
                  > byte.So,can anybody please tell me that how the charcters are
                  > implemented via integers.[/color]

                  The type of the expression ('\0'), is int.
                  The value of '\0', is an integer value which the C standard
                  guarantees is greater than or equal to CHAR_MIN
                  and also less than or equal to CHAR_MAX.

                  --
                  pete

                  Comment

                  • Mike Wahler

                    #10
                    Re: 1 byte for character


                    "pete" <pfiland@mindsp ring.com> wrote in message
                    news:41BA6266.6 009@mindspring. com...[color=blue]
                    > adityavasishth@ gmail.com wrote:[color=green]
                    > >
                    > > hi all,
                    > >
                    > > Characters are basically implemented via integers,ex : '\0' is 0.But
                    > > integers requires 2 bytes and the characters require only 1
                    > > byte.So,can anybody please tell me that how the charcters are
                    > > implemented via integers.[/color]
                    >
                    > The type of the expression ('\0'), is int.
                    > The value of '\0', is an integer value which the C standard
                    > guarantees is greater than or equal to CHAR_MIN
                    > and also less than or equal to CHAR_MAX.[/color]

                    No, '\0' is never equal to CHAR_MAX. It's always at
                    least 127 less than CHAR_MAX.

                    -Mike


                    Comment

                    • Keith Thompson

                      #11
                      Re: 1 byte for character

                      "Mike Wahler" <mkwahler@mkwah ler.net> writes:[color=blue]
                      > "pete" <pfiland@mindsp ring.com> wrote in message
                      > news:41BA6266.6 009@mindspring. com...[color=green]
                      >> adityavasishth@ gmail.com wrote:[/color][/color]
                      [...][color=blue][color=green]
                      >> The type of the expression ('\0'), is int.
                      >> The value of '\0', is an integer value which the C standard
                      >> guarantees is greater than or equal to CHAR_MIN
                      >> and also less than or equal to CHAR_MAX.[/color]
                      >
                      > No, '\0' is never equal to CHAR_MAX. It's always at
                      > least 127 less than CHAR_MAX.[/color]

                      Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • Joe Wright

                        #12
                        Re: 1 byte for character

                        Keith Thompson wrote:[color=blue]
                        > "Mike Wahler" <mkwahler@mkwah ler.net> writes:
                        >[color=green]
                        >>"pete" <pfiland@mindsp ring.com> wrote in message
                        >>news:41BA6266 .6009@mindsprin g.com...
                        >>[color=darkred]
                        >>>adityavasish th@gmail.com wrote:[/color][/color]
                        >
                        > [...]
                        >[color=green][color=darkred]
                        >>>The type of the expression ('\0'), is int.
                        >>>The value of '\0', is an integer value which the C standard
                        >>>guarantees is greater than or equal to CHAR_MIN
                        >>>and also less than or equal to CHAR_MAX.[/color]
                        >>
                        >>No, '\0' is never equal to CHAR_MAX. It's always at
                        >>least 127 less than CHAR_MAX.[/color]
                        >
                        >
                        > Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.
                        >[/color]

                        Please expand on the case for '\0' == CHAR_MAX. Thanks.
                        --
                        Joe Wright mailto:joewwrig ht@comcast.net
                        "Everything should be made as simple as possible, but not simpler."
                        --- Albert Einstein ---

                        Comment

                        • Chris Torek

                          #13
                          Re: 1 byte for character

                          >Keith Thompson wrote:[color=blue][color=green]
                          >> Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.[/color][/color]

                          In article <X-udnajL5vjkXSHcR Vn-tQ@comcast.com>
                          Joe Wright <joewwright@com cast.net> wrote:[color=blue]
                          >Please expand on the case for '\0' == CHAR_MAX. Thanks.[/color]

                          He is saying that, mathematically:

                          0 <= 127

                          is just as correct as:

                          0 < 127

                          (which is true -- both statements are true).
                          --
                          In-Real-Life: Chris Torek, Wind River Systems
                          Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                          email: forget about it http://web.torek.net/torek/index.html
                          Reading email is like searching for food in the garbage, thanks to spammers.

                          Comment

                          • Keith Thompson

                            #14
                            Re: 1 byte for character

                            Joe Wright <joewwright@com cast.net> writes:[color=blue]
                            > Keith Thompson wrote:[color=green]
                            >> "Mike Wahler" <mkwahler@mkwah ler.net> writes:
                            >>[color=darkred]
                            >>>"pete" <pfiland@mindsp ring.com> wrote in message
                            >>>news:41BA626 6.6009@mindspri ng.com...
                            >>>
                            >>>>adityavasis hth@gmail.com wrote:[/color]
                            >> [...]
                            >>[color=darkred]
                            >>>>The type of the expression ('\0'), is int.
                            >>>>The value of '\0', is an integer value which the C standard
                            >>>>guarantee s is greater than or equal to CHAR_MIN
                            >>>>and also less than or equal to CHAR_MAX.
                            >>>
                            >>>No, '\0' is never equal to CHAR_MAX. It's always at
                            >>>least 127 less than CHAR_MAX.[/color]
                            >> Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.
                            >>[/color]
                            >
                            > Please expand on the case for '\0' == CHAR_MAX. Thanks.[/color]

                            Please expand on the case that '\0' is *not* less than or equal to
                            CHAR_MAX.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                            We must do something. This is something. Therefore, we must do this.

                            Comment

                            • Joe Wright

                              #15
                              Re: 1 byte for character

                              Keith Thompson wrote:[color=blue]
                              > Joe Wright <joewwright@com cast.net> writes:
                              >[color=green]
                              >>Keith Thompson wrote:
                              >>[color=darkred]
                              >>>"Mike Wahler" <mkwahler@mkwah ler.net> writes:
                              >>>
                              >>>
                              >>>>"pete" <pfiland@mindsp ring.com> wrote in message
                              >>>>news:41BA62 66.6009@mindspr ing.com...
                              >>>>
                              >>>>
                              >>>>>adityavasi shth@gmail.com wrote:
                              >>>
                              >>>[...]
                              >>>
                              >>>
                              >>>>>The type of the expression ('\0'), is int.
                              >>>>>The value of '\0', is an integer value which the C standard
                              >>>>>guarante es is greater than or equal to CHAR_MIN
                              >>>>>and also less than or equal to CHAR_MAX.
                              >>>>
                              >>>>No, '\0' is never equal to CHAR_MAX. It's always at
                              >>>>least 127 less than CHAR_MAX.
                              >>>
                              >>>Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.
                              >>>[/color]
                              >>
                              >>Please expand on the case for '\0' == CHAR_MAX. Thanks.[/color]
                              >
                              >
                              > Please expand on the case that '\0' is *not* less than or equal to
                              > CHAR_MAX.
                              >[/color]

                              I got it. Thanks.
                              --
                              Joe Wright mailto:joewwrig ht@comcast.net
                              "Everything should be made as simple as possible, but not simpler."
                              --- Albert Einstein ---

                              Comment

                              Working...