473,671 Members | 2,446 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 11379
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

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

Similar topics

3
4371
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
2118
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 string import join x = print x print join()
3
2156
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 printf(format, *args): print str(format) % args, The only difference is that in the second one, str(format) replaces format. If args are not given and the format string contains a '%', the first will work but the second will not. Why is this so? It...
8
1911
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 args; va_start(args, format); printf(format, args);
4
1990
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
1262
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 from one of O'reilly's books. But there is no example and I couldn't get it to work by trials and errors. Does anyone have a working example? /Why Tea
4
2324
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 looked like the simple and good solution but no. class Example { string format = "{1} {2} {3}" string anotherFormat = "{3}{1}{2}";
3
3885
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
4504
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 %2u:%2u:%2u" which works, but it allows each numeric field to be either 1 or 2 digits in length. Also the man page for sscanf says that preceeding white space before a numeric is ignored.
0
8401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8824
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8603
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6236
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.