What does this struct for ?

This is a discussion on What does this struct for ? within the C Programming forums, part of the General Programming Boards category; I forget where I got this snippet from. Anyway, here's the code Code: struct foo { char ch : 8; ...

  1. #1
    TmX
    TmX is offline
    Registered User TmX's Avatar
    Join Date
    Sep 2006
    Posts
    12

    What does this struct for ?

    I forget where I got this snippet from.
    Anyway, here's the code

    Code:
    struct foo {
    	char ch : 8;
    	int aaa : 2;
    	long bbb : 3;
    };
    I don't think it's for creating arrays ...

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    I don't know what it's supposed to do, but that stuct will contain 3 signed integers of size 8 bits, 2 bits, and 3 bits. The char, int, and long keywords have no effect in this case except to point out that it is a signed integer type; there is no difference between the three.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    58
    ch goes from -128 to 127
    aaa from -2 to 1
    bbb from -4 to 3

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    char does not go from -127 to 127 I think. It is a char and it is unsigned. ??

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by ozumsafa View Post
    char does not go from -127 to 127 I think. It is a char and it is unsigned. ??
    Actually, -128 to 127 is more or less the right range.

    A char is signed by default.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The integer arithmetic can be either two's complement, ones complement or sign-magnitude (though most modern machines use the first). The latter two have a symmetric range, the first has one extra on the minus side. Whether a char is signed or unsigned depends on the compiler (unlike other integral types which are signed by default), so to guarantee one or the other it's necessary to specify signed or unsigned explicitly.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In fact, declaring a signed char is the only place you have to use the keyword signed. (That's why it was introduced, in fact.) Everywhere else it is optional.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  3. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21