Timeline for How to prevent my code from the following conversion error?
Current License: CC BY-SA 4.0
Post Revisions
19 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 30, 2023 at 9:37 | review | Close votes | |||
| Jun 1, 2023 at 0:04 | |||||
| May 29, 2023 at 12:44 | comment | added | Eric Postpischil | @AndrewHenle: You have no business commenting on what I do or do not understanding. Refrain from making ad hominem comments. | |
| May 29, 2023 at 10:39 | comment | added | Andrew Henle |
@EricPostpischil Teaching "rules for dummies" to allow people to code without well understanding the programming language or libraries they are using is not a remedy for bad engineering or bugs How long have you been working in environments where things have to work? You don't seem to understand that developers who get hired to work on code like that come from the same pool of people who write the plethora of *scanf() questions here. Avoiding code like *scanf() that is bug-prone is a helluva lot more important than showing off you much you know about obscure, bug-prone corner cases.
|
|
| May 29, 2023 at 2:22 | comment | added | Eric Postpischil | @AndrewHenle: Teaching "rules for dummies" to allow people to code without well understanding the programming language or libraries they are using is not a remedy for bad engineering or bugs, nor does it justify making false statements. As an objective matter of fact, the sentence "There's no way to to use any of those functions without risking undefined behavior on an out-of-range value" is false. Teaching people false statements is not ethical, even if the motive is to do it for their own good or the good of some hypothetical farmer. Teach the hazards truthfully. | |
| May 28, 2023 at 21:54 | comment | added | tadman |
Using short int in 2023 is a bit backwards. If you want a particular size, may as well ask, like int16_t.
|
|
| May 28, 2023 at 18:03 | comment | added | Steve Summit |
@AndrewHenle You don't need scanf to introduce confusion between pigs and pig iron; it's been an issue in folk music since well before scanf even existed...
|
|
| May 28, 2023 at 18:00 | comment | added | Jonathan Leffler |
See also A Beginner's Guide Away From scanf() and How to read/parse input in C — The FAQ.
|
|
| May 28, 2023 at 17:55 | history | edited | Jonathan Leffler | CC BY-SA 4.0 |
Fix trivial typos
|
| May 28, 2023 at 15:07 | comment | added | Andrew Henle |
@EricPostpischil Probably 5-10% of the C questions here arise from one of the *scanf() functions. What happens when your just-out-of-college self-styled "l33t" coder gets the ticket to change that format, and three weeks after delivery to production, one of your biggest customers calls up all irate because he just had a livestock trailer unleash 100 full-grown pigs onto his factory floor and his account was directly debited for rush delivery of said pigs and you find out your ordering system messed up a rush order for 100 tons of pig iron because your noob screwed up the scanf() format?
|
|
| May 28, 2023 at 14:59 | comment | added | Eric Postpischil |
@AndrewHenle: No, you do not have to convert it. First you check that its format conforms to some pattern, then you convert it. The latter may be done using sscanf. The strto* functions cannot check that a string is in a specific format such as YYYY-MM-DD, for example.
|
|
| May 28, 2023 at 14:55 | comment | added | Andrew Henle |
@EricPostpischil That is excessive Really? For example, if the number of digits has been checked and is known to be within a sufficient limit So you pretty much have to convert and verify the input data before you can use any of the *scanf() functions? What's the point of using *scanf() when you can use one of the strto*() functions and both validate and convert - safely - a numeric string in one step?
|
|
| May 28, 2023 at 14:17 | comment | added | Eric Postpischil |
@AndrewHenle: That is excessive. For example, if the number of digits has been checked and is known to be within a sufficient limit, sscanf can be relied upon. Say a string has been matched to a date format, then sscanf can be used to parse the various numbers in it.
|
|
| May 28, 2023 at 13:55 | comment | added | Andrew Henle |
@SteveSummit I'm pretty sure it's downright undefined It is undefined behavior: "... if the result of the conversion cannot be represented in the object, the behavior is undefined." Never use any of the *scanf() functions to parse numeric data. There's no way to to use any of those functions without risking undefined behavior on an out-of-range value.
|
|
| May 28, 2023 at 13:23 | comment | added | Steve Summit |
But if you want guaranteed behavior if the user types a value bigger than the target type can hold, you need to use something other than scanf. Regardless of whether the target type is short, int, long, or long long, if the user types a too-big value, there's no guaranteed behavior. (In fact I'm pretty sure it's downright undefined.)
|
|
| May 28, 2023 at 13:22 | answer | added | Madagascar | timeline score: 7 | |
| May 28, 2023 at 13:21 | comment | added | Steve Summit |
If you're going to read a short int with scanf, you need %hd.
|
|
| May 28, 2023 at 13:19 | history | edited | Mildred Gallagher | CC BY-SA 4.0 |
added 26 characters in body
|
| S May 28, 2023 at 13:17 | review | Triage | |||
| May 30, 2023 at 9:31 | |||||
| S May 28, 2023 at 13:17 | history | asked | Mildred Gallagher | CC BY-SA 4.0 |