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!!
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
Out of interest, what are the errors given when you use %c?
I can see three scenarios, - 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.
- 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.
- 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().
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.
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.
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
Jos, I thought you had gotten a faster keyboard! Maybe it needs some lubrication... ;-)
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
>>>
|
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()
|
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...
|
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);
|
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
| |
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
|
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}";
|
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.
|
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.
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |