Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

prevent sscanf from parsing double as int

When the input string is e.g. "4.5" I want sscanf to not parse it as a integer. But now it does.

My code looks like this:
if (sscanf(str, "%d", &val) == 0) { return NULL; }
This does not work of course since sscanf parses "4.5" as 4 and does not return NULL

Thanks for help

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

sscanf("4.5", "%d", &val) does not parse "4.5" as 4. It parses the "4" as 4 and leaves the ".5" unparsed. You instead could do:

int val;
char c;
if (sscanf(str, "%d%c", &val, &c) != 1) { return NULL; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading