Like Tree1Likes
  • 1 Post By King Mir

__u16 type

This is a discussion on __u16 type within the C Programming forums, part of the General Programming Boards category; I am writing a program in C using usb.h where ManufacturerID of USB is in __u16 format. How to print ...

  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,023
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,023
    If %d causes a segfault, then changing %x won't fix it. You haven't fixed that problem. You still have a bug somewhere.
    stahta01 likes this.
    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, 12:45 PM
  4. Replies: 17
    Last Post: 03-06-2008, 01: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


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