Thread: __u16 type

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    5

    __u16 type

    I am writing a program in C using usb.h where ManufacturerID of USB is in __u16 format. How to print it using printf.If I use %d it gives Segmentation Fault as output and if I use %c it gives some weird symbol.Does it have some other format specifier?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    What's the result of sizeof(__u16)? 2?

    You should probably typecast it to unsigned int, and pass that to printf. Or possibly to unsigned short.

    Since it's likely unsigned, use %u.

    What I suspect is happening is that the compiler writers didn't bother to make __u16 undergo standard integer promotion, which is needed by printf. So you have to cast it to a standard C type.
    Last edited by King Mir; 06-23-2011 at 10:53 PM.
    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
    Jun 2011
    Posts
    5
    " idVendor 0x%04x %s\n"
    This is what I saw here:
    Koders Code Search: lsusb.c - C - GPL
    u16 means 16 bit unsigned char.As you mentioned it should return sizeof() as 2

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It is far more likely that the %s also in the format string is trying to follow a junk pointer.

    Run the code in the debugger, and start examining stack traces and variables to figure out where things crash.

    Also, show us YOUR code as well, not just the example you're copying from.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    5
    the %s prints the name of manufacturer in that code.I just printed using %04x and got the right o/p.%u doesn't work here,gives the wrong ID.Anyway,got it solved thanks.%04x gives hexadecimal o/p

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If %d causes a segfault, then changing %x won't fix it. You haven't fixed that problem. You still have a bug somewhere.
    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.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jaypatel View Post
    I am writing a program in C using usb.h where ManufacturerID of USB is in __u16 format. How to print it using printf.If I use %d it gives Segmentation Fault as output and if I use %c it gives some weird symbol.Does it have some other format specifier?
    Is this not defined in their header? ... Have you looked? (Headers are text files, you can view them in notepad...)

    I'm guessing (and it is only a guess) that it's an unsigned short int ... In which case "%hd" should print it correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  2. error: array type has incomplete element type
    By gerger in forum C Programming
    Replies: 8
    Last Post: 10-05-2010, 07:40 AM
  3. what are __u32, __u16, __u8 ?
    By ashok449 in forum C Programming
    Replies: 8
    Last Post: 02-12-2010, 01:45 PM
  4. Replies: 17
    Last Post: 03-06-2008, 02:32 PM
  5. Converting type string to type const char*
    By rusty0412 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 05:59 PM

Tags for this Thread