Variable declaration followed by colon?

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

    Variable declaration followed by colon?

    What does this represent?

    unsigned int n : 6;

    ???

    Saverio
  • Richard Heathfield

    #2
    Re: Variable declaration followed by colon?

    saverio.post@gm ail.com said:
    What does this represent?
    >
    unsigned int n : 6;
    See "The C Programming Language", 2nd edition, by Kernighan and Ritchie,
    pages 149-150.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Yunzhong

      #3
      Re: Variable declaration followed by colon?

      What does this represent?
      unsigned int n : 6;
      Inside a structure or union, it represents an unsigned integral
      bitfield that occupies 6 bits;
      Outside a structure or union, it represents a syntax error.
      Have fun.

      Comment

      • Antoninus Twink

        #4
        Re: Variable declaration followed by colon?

        On 12 Apr 2008 at 15:17, Yunzhong wrote:
        What does this represent?
        unsigned int n : 6;
        >
        Inside a structure or union, it represents an unsigned integral
        bitfield that occupies 6 bits;
        Perhaps the OP will wonder why such a thing could be useful.

        A couple of common examples where bitfields are used are device drivers,
        because combinations of flags (i.e. bits) are often stored in a single
        byte in hardware; and binary data formats, which frequently contain
        fields that aren't aligned at byte boundaries. Whether you realize it or
        not, you're relying on bitfields every time you send an IP packet!
        Here's the IP header struct that's used in every packet:

        struct iphdr {
        unsigned int version:4;
        unsigned int ihl:4;
        u_int8_t tos;
        u_int16_t tot_len;
        u_int16_t id;
        u_int16_t frag_off;
        u_int8_t ttl;
        u_int8_t protocol;
        u_int16_t check;
        u_int32_t saddr;
        u_int32_t daddr;
        };

        Comment

        • Kaz Kylheku

          #5
          Re: Variable declaration followed by colon?

          On Apr 12, 7:36 am, saverio.p...@gm ail.com wrote:
          What does this represent?
          >
                  unsigned int n   : 6;
          Your pitiful inability to consult a reference manual.

          Comment

          • Antoninus Twink

            #6
            Re: Variable declaration followed by colon?

            On 15 Apr 2008 at 2:04, robertwessel2@y ahoo.com wrote:
            On Apr 14, 5:20 pm, ymunt...@gmail. com wrote:
            >Almost assuredly, OP is not using a fancy apple or sun machine, true.
            >(That code most certainly needs gcc or gcc-compatible compiler,
            >but that's something almost assuredly used by OP, indirectly)
            >
            The structure as presented would have *not* been correct on almost any
            x86 implementation (actually on none that I know of, and I frankly
            doubt there are any where it would have been).
            Of course it's quite likely that the OP is using an x86, but it's not
            the absolute certainty you're implying. I think the latest stats are
            that 20% of laptop sales are Apples, and I imagine that C programmers
            are disproportionat ely likely to be using interesting big-endian
            hardware for their work...

            Comment

            • robertwessel2@yahoo.com

              #7
              Re: Variable declaration followed by colon?

              On Apr 15, 2:49 am, Antoninus Twink <nos...@nospam. invalidwrote:
              On 15 Apr 2008 at  2:04, robertwess...@y ahoo.com wrote:
              >
              On Apr 14, 5:20 pm, ymunt...@gmail. com wrote:
              Almost assuredly, OP is not using a fancy apple or sun machine, true.
              (That code most certainly needs gcc or gcc-compatible compiler,
              but that's something almost assuredly used by OP, indirectly)
              >
              The structure as presented would have *not* been correct on almost any
              x86 implementation (actually on none that I know of, and I frankly
              doubt there are any where it would have been).
              >
              Of course it's quite likely that the OP is using an x86, but it's not
              the absolute certainty you're implying. I think the latest stats are
              that 20% of laptop sales are Apples,

              All of which are now x86. (Actually it's probable that Apple is still
              moving a handful of PPC based systems to people who care more about
              compatibility than price or performance).

              and I imagine that C programmers
              are disproportionat ely likely to be using interesting big-endian
              hardware for their work...

              All of which are now x86. (Actually it's probable that Apple is still
              moving a handful of PPC based systems to people who care more about
              compatibility than price or performance).



              True, but mostly as embedded gear. There are darn few primary
              computing* systems that aren't x86 out there (and even fewer shipping
              now), older PPC based Macs are probably the biggest population.


              *Desktops, laptops, servers, HPC systems - IOW, non-embedded stuff

              Comment

              • Anand Hariharan

                #8
                Re: Variable declaration followed by colon?

                On Tue, 15 Apr 2008 00:31:21 -0700, Kaz Kylheku wrote:
                On Apr 12, 7:36 am, saverio.p...@gm ail.com wrote:
                >What does this represent?
                >>
                >        unsigned int n   : 6;
                >
                Your pitiful inability to consult a reference manual.
                Given a "reference manual", where would you look it up?

                --
                ROT-13 email address to reply

                Comment

                • Richard

                  #9
                  Re: Variable declaration followed by colon?

                  Anand Hariharan <znvygb.nanaq.u nevunena@tznvy. pbzwrites:
                  On Tue, 15 Apr 2008 00:31:21 -0700, Kaz Kylheku wrote:
                  >
                  >On Apr 12, 7:36 am, saverio.p...@gm ail.com wrote:
                  >>What does this represent?
                  >>>
                  >>        unsigned int n   : 6;
                  >>
                  >Your pitiful inability to consult a reference manual.
                  >
                  Given a "reference manual", where would you look it up?
                  Exactly. His reply would almost make me think he is Nick Keighley nym
                  shifting. But I'm sure that's where the similarities end ..

                  Comment

                  • CBFalconer

                    #10
                    Re: Variable declaration followed by colon?

                    Anand Hariharan wrote:
                    Kaz Kylheku wrote:
                    >saverio.p...@g mail.com wrote:
                    >>
                    >>What does this represent?
                    >>>
                    >> unsigned int n : 6;
                    >>
                    >Your pitiful inability to consult a reference manual.
                    >
                    Given a "reference manual", where would you look it up?
                    Under "Declaratio ns".

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


                    ** Posted from http://www.teranews.com **

                    Comment

                    • Anand Hariharan

                      #11
                      Re: Variable declaration followed by colon?

                      On Thu, 17 Apr 2008 00:09:40 -0400, CBFalconer wrote:
                      Anand Hariharan wrote:
                      >Kaz Kylheku wrote:
                      >>saverio.p...@ gmail.com wrote:
                      >>>
                      >>>What does this represent?
                      >>>>
                      >>> unsigned int n : 6;
                      >>>
                      >>Your pitiful inability to consult a reference manual.
                      >>
                      >Given a "reference manual", where would you look it up?
                      >
                      Under "Declaratio ns".
                      >
                      Looking up K&R2 index entry for "declaratio n", I see page references 9, 40
                      and 210-218.

                      Pages 9 and 40 do not discuss bit fields. Pages 210-218 gets into grammar,
                      and does not get into bit fields until mid-way of page 212. Not the
                      easiest of pages in this book to find the answer I sought out to find.

                      There are a dozen odd more entries for declaration, but nothing that says
                      "declaratio n, with colon" or something similar.

                      The entry for "declaratio n, structure" takes one to either page 128 or
                      212. Bit fields are covered in the same chapter as page 128, but it does
                      not come until page 150.

                      The index itself begins with entries for symbols starting from '0' to ...
                      '\0' (the quotes are mine), but no entry for ':'.

                      This isn't a critique of K&R2's index, but just to point out that it is
                      not obvious where to find an answer to OP's question. Considering that one
                      could always mouth off a rebuttal based on "reference manual" and
                      "consult", I suppose I am wasting my time ....

                      --
                      ROT-13 email address to reply

                      Comment

                      • lawrence.jones@siemens.com

                        #12
                        Re: Variable declaration followed by colon?

                        Anand Hariharan <znvygb.nanaq.u nevunena@tznvy. pbzwrote:
                        On Tue, 15 Apr 2008 00:31:21 -0700, Kaz Kylheku wrote:
                        >
                        >On Apr 12, 7:36 am, saverio.p...@gm ail.com wrote:
                        >>What does this represent?
                        >>>
                        >> unsigned int n : 6;
                        >>
                        >Your pitiful inability to consult a reference manual.
                        >
                        Given a "reference manual", where would you look it up?
                        ": (colon punctuator)" is in the Standard's index and points to exactly
                        the right place.

                        -Larry Jones

                        I think your train of thought is a runaway. -- Calvin's Mom

                        Comment

                        • santosh

                          #13
                          Re: Variable declaration followed by colon?

                          saverio.post@gm ail.com wrote:
                          What does this represent?
                          >
                          unsigned int n : 6;
                          >
                          ???
                          As shown this is a syntax error. Within a structure or union
                          declaration, this declares 'n' to be a "bit field" of 6 bits width and
                          of type unsigned int.

                          See:

                          <http://c-faq.com/struct/bitfield0.html>

                          Comment

                          Working...