By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

sscanf print format problem - %hhd

P: 66
I am passing an unsigned 8-bit int into sscanf. If I try format specifier %c (which should fit) I get complicated errors during compliation. However, if I use %hhd instead I don't get any errors. Is my little quick fix a bad idea for any reason?

thanks!!
Nov 13 '08 #1
Share this Question
Share on Google+
10 Replies


Expert 10K+
P: 11,448
If the format specifier "%hhd" happens to work throw your C implementation away
because it's extremely non-compliant then.

kind regards,

Jos
Nov 14 '08 #2

10K+
P: 13,264
Out of interest, what are the errors given when you use %c?
Nov 14 '08 #3

100+
P: 424
I can see three scenarios,
  1. You're scanning a string for a character. You use %c in the format specifier and provide a short int.
    Then character will be silently cast to a short int. The short int you provided will be assigned the ASCII value of the character, as expected.

  2. You're scanning a string for a non-numeric character. You use %hhd in the format specifier and provide a short int.
    Probably %hhd is interpreted as %hd which means short int (since h is the "short" modifier, presumably more than one modifier is ignored although this is bad practice). In this case, scanf will not find any numeric character which can be interpreted as a short int in the string provided. Nothing will be assigned to the short int which will retain its previous value.

  3. You're scanning a string for a numeric character. You use %hhd in the format specifier and provide a short int.
    Assuming %hhd means %hd, scanf will assign the number to the short int.

In any case, if there are errors it shouldn't be directly because of scanf().
Nov 14 '08 #4

P: 66
Hi - The error I get if I try %c for my 8-bit unsigned int is:

error: duplicate case value
error: previously used here
error: duplicate case value

at specific lines, over and over again, and it won't compile.

I do not understand why I can't scan it properly. I have been stuck with this warning, not knowing how to fix it for months now.
Nov 14 '08 #5

100+
P: 424
Are you using switch statements? Note then that all cases should be unique - maybe the result of the scanf is causing two cases to be the same. Perhaps you could give us an example of a line where this error occurs.
Nov 14 '08 #6

Expert 10K+
P: 11,448
Hi - The error I get if I try %c for my 8-bit unsigned int is:

error: duplicate case value
error: previously used here
error: duplicate case value

at specific lines, over and over again, and it won't compile.

I do not understand why I can't scan it properly. I have been stuck with this warning, not knowing how to fix it for months now.
Those error messages indicate that you're using a switch statement somewhere.
Such a statement has nothing to do with a scanf() functions call. Can you show
us the code that uses the scanf() call as well as that switch statement?

kind regards,

Jos
Nov 14 '08 #7

100+
P: 424
Jos, I thought you had gotten a faster keyboard! Maybe it needs some lubrication... ;-)
Nov 14 '08 #8

Expert 10K+
P: 11,448
Jos, I thought you had gotten a faster keyboard! Maybe it needs some lubrication... ;-)
I know; I'm so speedy today ;-) especially when it comes to questions that divert
from their original goal. I wonder what source code comes up ...

kind regards,

Jos
Nov 14 '08 #9

100+
P: 687
Are you using switch statements? Note then that all cases should be unique - maybe the result of the scanf is causing two cases to be the same. Perhaps you could give us an example of a line where this error occurs.
Result of scanf can appear only in runtime and has nothing to do with compile error.
Nov 14 '08 #10

100+
P: 424
Yes, I agree it does sound a bit fishy... That's why I'd like to see some code.
Nov 14 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.