473,511 Members | 16,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sscanf print format problem - %hhd

66 New Member
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
10 11331
JosAH
11,448 Recognized Expert MVP
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
r035198x
13,262 MVP
Out of interest, what are the errors given when you use %c?
Nov 14 '08 #3
arnaudk
424 Contributor
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
dissectcode
66 New Member
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
arnaudk
424 Contributor
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
JosAH
11,448 Recognized Expert MVP
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
arnaudk
424 Contributor
Jos, I thought you had gotten a faster keyboard! Maybe it needs some lubrication... ;-)
Nov 14 '08 #8
JosAH
11,448 Recognized Expert MVP
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
newb16
687 Contributor
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
arnaudk
424 Contributor
Yes, I agree it does sound a bit fishy... That's why I'd like to see some code.
Nov 14 '08 #11

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

Similar topics

3
4361
by: Rim | last post by:
Hi, >>> print '%x' % 54 36 >>> print '%b' % 54 Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: unsupported format character 'b' (0x62) at index 1 >>>
2
2109
by: beliavsky | last post by:
By default, Python prints many floating point numbers with 17 digits after the decimal place. I would like to make the DEFAULT 4 decimal places. Is this possible? For example, the code from...
3
2151
by: Lowell Kirsh | last post by:
In Peter Norvig's Infrequently Answered Questions he explains that the following 2 fnctions look almost identical but are not the same: def printf(format, *args): print format % args, def...
8
1897
by: Eric Lilja | last post by:
As the title, says: Why doesn't the following program print Hi Charles<newline> when run? #include <stdarg.h> #include <stdio.h> static void va_arg_example(const char *format, ...) { va_list...
4
1987
by: joriveek | last post by:
Hi, I want to print a string with defined width left trailed; How can I do this? Example: Print ("%05d%3s", &test1, test2); say test1 = 23 test2 = Hi
6
1253
by: Why Tea | last post by:
print format % values An optional minimum width of the conversion, specified using one or more digits or an asterisk (*), which means that the width is taken from the next item in values That's...
4
2319
by: carry | last post by:
Hi everybody, I need simple string formatting like string.Format() does, but just with strings what seems not to work. Can anyone tell me please why this doesn't work and how can it be done? This...
3
3874
by: PebblePicker | last post by:
hi all, is it possible to change print format to landscape using javascript/css or anything else which is suppoted in MOZILLA ? I could not find it anywhere.
6
4479
by: Richard | last post by:
I'm validating a date and time string which must be EXACTLY of the format yy-mm-dd hh:mm:ss and extracting the six numeric values using sscanf. I'm using the format string "%2u-%2u-%2u...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.