sscanf print format problem - %hhd

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    sscanf print format problem - %hhd

    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!!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If the format specifier "%hhd" happens to work throw your C implementation away
    because it's extremely non-compliant then.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Out of interest, what are the errors given when you use %c?

      Comment

      • arnaudk
        Contributor
        • Sep 2007
        • 425

        #4
        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().

        Comment

        • dissectcode
          New Member
          • Jul 2008
          • 66

          #5
          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.

          Comment

          • arnaudk
            Contributor
            • Sep 2007
            • 425

            #6
            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.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by dissectcode
              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

              Comment

              • arnaudk
                Contributor
                • Sep 2007
                • 425

                #8
                Jos, I thought you had gotten a faster keyboard! Maybe it needs some lubrication... ;-)

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by arnaudk
                  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

                  Comment

                  • newb16
                    Contributor
                    • Jul 2008
                    • 687

                    #10
                    Originally posted by arnaudk
                    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.

                    Comment

                    • arnaudk
                      Contributor
                      • Sep 2007
                      • 425

                      #11
                      Yes, I agree it does sound a bit fishy... That's why I'd like to see some code.

                      Comment

                      Working...