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 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
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.
Yes, I agree it does sound a bit fishy... That's why I'd like to see some code.
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |