How To Fix "return Value Ignored: 'scanf'" Code C6031 In Visual Studio
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs How to fix "return value ignored: 'scanf'" code C6031 in visual studio Ask Question Asked 5 years, 2 months ago Modified 1 year ago Viewed 38k times 4I am brand new to coding C (and coding in general) so I have been practicing with some random programs. This one is supposed to determine the cost of a transit ticket (Translink Vancouver prices) based on the user's age and the desired number of "zones" (how far they would like to go). I have compiled it successfully but for some reason which I can not figure out, the scanf functions are being ignored. How do I fix this? Please keep in mind I have only been coding for a few days. Thanks!
int main(void) { int zones; int age; double price = 0.00; printf("Welcome to TransLink cost calculator!\n\n"); printf("Please enter the desired number of zones (1, 2, or 3) you wish to travel: "); scanf("%d", &zones); if (zones < 1) { printf("Invalid entry\n"); price = 0.00; } else if (zones > 3) { printf("Invalid entry\n"); price = 0.00; } else if (zones == 1) { printf("Please enter your age: "); scanf("%d", &age); if (age < 0.00) { printf("Invalid Aage"); } else if (age < 5) { price = 1.95; } else if (age >= 5) { price = 3.00; } } else if (zones == 2) { printf("Please enter your age: "); scanf("%d", &age); if (age < 0) { printf("Invalid Aage"); } else if (age < 5) { price = 2.95; } else if (age >= 5) { price = 4.25; } } else if (zones == 3) { printf("Please enter your age: "); scanf("%d", &age); if (age < 0) { printf("Invalid Aage"); } else if (age < 5) { price = 3.95; } else if (age >= 5) { price = 4.75; } } printf("The price of your ticket is: $%.2f + tax\n", price); system("PAUSE"); return 0; } Share Improve this question Follow asked Sep 19, 2019 at 5:25 Caiden KellerCaiden Keller 411 gold badge1 silver badge2 bronze badges 5- 2 Please explain how you use the value returned by this call scanf("%d", &age);. Not the value scanned into age, the value returned by that call. The value which tells you whether scanning has succeeded. If you cannot, then you have the answer to your question. – Yunnosch Commented Sep 19, 2019 at 5:28
- Thanks for your response but I don't understand what you are trying to say. I am not sure why it is not working (hence my post here) so if you know how I can fix it and could tell me that would be great, thanks. – Caiden Keller Commented Sep 19, 2019 at 5:37
- btw. It's not the problem , but age is an int and your are comparing to a float constant: if (age < 0.00) { – Mike Commented Sep 19, 2019 at 5:41
- 1 scanf() returns the number of values processed. You should compare this to the number of variables you're trying to read, to make sure it was successful. e.g. if (scanf("%d", &age) != 1) { // print error message } – Barmar Commented Sep 19, 2019 at 6:03
- 1 Whatever you did to make visual studio complain about ignoring scanf()s return value. Please teach it to as many people as you can. – Yunnosch Commented Sep 19, 2019 at 6:14
4 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 6A bit too much here to put in a comment.
I use a version of Visual C but it never complains about the return value from scanf not being used. What it does is to complain that scanf is unsafe and deprecated, when it isn't.
MS thinks I should be using its own "safer" version scanf_s which is even tricker to use and IMO no safer at all – because it is not a like-for-like replacement but takes different arguments, and so it is easy to make mistakes in using it.
One consequent problem is the compiler issues a warning for every use of scanf (and some other functions) which obscures other warnings. I deal with it as advised by adding a #define before the first library header inclusion.
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h>There are other matters which MS warns about too, and I actually place three #defines at the start of each file:
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE #include <stdio.h>And now the relevant warnings are easy to see.
Share Improve this answer Follow answered Sep 19, 2019 at 7:42 Weather VaneWeather Vane 34.5k7 gold badges41 silver badges61 bronze badges 2- I fully agree with the advice to disable MSVC's advice to use their pseudo-Annex K functions; however, the scanf family are unsafe, for several reasons, most importantly that %s is just as dangerous as gets, and that numeric conversion has undefined behavior on numeric input overflow (yes, that means the C library is allowed to crash just because someone typed too many digits). – zwol Commented Jun 9, 2021 at 15:08
- @zwol yes, but C is unsafe anyway. You can create a segfault very easily by exceeding array bounds. My point was that MS's scanf_s isn't really any better than scanf - both can cause damage when used incorrectly. – Weather Vane Commented Jun 9, 2021 at 18:33
From documentation of scanf() (e.g. https://en.cppreference.com/w/c/io/fscanf)
Return value 1-3) Number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned.
You are ignoring that return value.
Replace
scanf("%d", &age);by
int NofScannedArguments=0; /* Number of arguments which were successfully filled by the most recent call to scanf() */ /* ... do above once, at the start of your function */ NofScannedArguments= scanf("%d", &age); /* check the return value to find out whether scanning was successful */ if(NofScannedArguments!=1) /* should be one number */ { exit(EXIT_FAILURE); /* failure, assumptions of program are not met */ }... to find out whether scanning succeeded. Not doing so is a bad idea and worth the warning you got.
In case you want to handle failures more gracefully, e.g. prompt the user again, use a loop and read http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html about the pitfalls you may encounter. I am not saying you should NOT use scanf, the article explains a lot about using scanf, while trying to convince you not to.
Share Improve this answer Follow edited Sep 19, 2019 at 6:21 answered Sep 19, 2019 at 6:05 YunnoschYunnosch 26.6k9 gold badges44 silver badges61 bronze badges 4- If you'd used a more reasonable variable name than the essay you chose… I often use rv (return value) — I accept that's too minimal, but retval is a possibility, with an underscore in the middle if desired. – Jonathan Leffler Commented Sep 19, 2019 at 6:15
- 1 @JonathanLeffler I think I know what you mean. But in this case OP needs all the super-explicit explanation they can get. Let me see whether I can get both needs covered. There. Deal? – Yunnosch Commented Sep 19, 2019 at 6:18
- IMO this (making a variable) is not really useful as it really does nothing. in competitive programming at least – J Muzhen Commented Jan 6, 2022 at 13:03
- @JMuzhen I think I know what you mean. But in this case OP needs all the super-explicit explanation they can get. – Yunnosch Commented Jan 6, 2022 at 17:38
I know this is an old question and asked several times in other threads.
To suppress individual C6031 warning, I cast the function w/ (void).
Example:
double x; (void)scanf("%lf", &x);this way, I can "mark" that I'm intentionally ignoring the return value of the function (=scanf). This is useful for the functions such as InitializeCriticalSectionAndSpinCount(), which always succeeds and returns non-zero value, worth ignoring safely.
This seems better to suppress C6031 entirely.
Share Improve this answer Follow edited Nov 8, 2023 at 12:25 answered Nov 8, 2023 at 1:12 beshiobeshio 8042 gold badges8 silver badges18 bronze badges Add a comment | -1Using C++ functions for input is SO much easier. Instead of scanf and printf one could use cin and cout as the following demonstrates:
#include <iostream> // for cin and cout use int main() { int zones; std::cout << "Enter zones" << std::endl; // endl is similar to \n std::cin >> zones; std::cout << "Your zones is " << zones << std::endl; } Share Improve this answer Follow edited Jun 9, 2021 at 15:00 answered Jun 9, 2021 at 14:40 KenKen 141 bronze badge 2- Firstly, the author was clearly wanting to know how to use scanf and printf; secondly you are clearly not experienced with the scanf and printf functions, as although they may be harder to use, they run much faster than std::cin and std:cout. – J Muzhen Commented Jan 6, 2022 at 13:00
- This is not what author asked – Afzal Ali Commented Nov 17, 2022 at 8:50
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Draft saved Draft discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- Your docs are your infrastructure
- Featured on Meta
- More network sites to see advertising test [updated with phase 2]
- We’re (finally!) going to the cloud!
- Call for testers for an early access release of a Stack Overflow extension...
Linked
0 Can anyone help me with my C code in this Visual Studio scanf error C6031 1 How do I prevent command line closing after pressing EnterRelated
13 C - error: ignoring return value of scanf? 0 Visual Studio 2012 C Access Code Error 0 scanf_s error visual studio 29 error C4996: 'scanf': This function or variable may be unsafe in c programming 3 Run time Error with scanf 3 Program is crashing because of scanf_s in Visual Studio Ultimate 2013 4 Removing C6054 error with scanf functions 1 Access violation in scanf_s 0 Exception in c scanf_s 1 Return value ignored [scanf]Hot Network Questions
- Student is almost always late, and expects me to re-explain everything he missed
- Solving Large size problems in industry: Tips and Tricks
- Find the Smallest Data Type for a Number
- What kind of solvent is effective for removing roofing tar from shoes?
- Is Holy Terra Earth?
- How to attribute authorship to personal non-academic friend who made significant contributions
- Groups with no proper non-trivial fully invariant subgroup
- Lebesgue equivalence class with no continuous representative
- Sorites paradox and emergence
- Being honest with rejection vs. preparing applicant for future possibility
- Special stairs of infinites and infinitesimals
- Why is bash startup executed under non-interactive ssh
- Is Isaiah's suffering servant the prophet Jeremiah?
- Is there a cryptographic method that can only decrypt for a certain range of seeds?
- Replace Leaking Y Junction Cleanout From Washer Drain Before Main Sewer Line
- Frogs on lily pads want to make a party
- Weapon Mastery and Weapon Cantrips
- Why is Ukraine's conscription age (still) so high (25)?
- In GR, what is Gravity? A force or curvature of spacetime?
- Angular orientation of exact solution of the Hydrogen Schrödinger Equation
- Should sudo ask for root password?
- Identifying a TNG episode where Dr. Pulaski instructs another doctor on a sling
- The British used to (still?) classify their guns by weight in pounds rather than caliber. Was that a constant across shell types?
- Is the A321 XLR really necessary to fly MAD-BOS?
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-cTừ khóa » C C6031
-
C6031 - Microsoft Docs
-
C6031 Return Value Ignored: 'scanf' - Visual Studio Feedback
-
False Positive C6031 Warning From Static Analysis
-
Prodesign Denmark Eyeglasses 4332 C.6031 Pure Titanium Black
-
C - How To Fix "return Value Ignored: 'scanf'" Code C6031 In Visual Studio
-
The Problem Of Ignoring Scanf Return Value In Vs2019 And Its Solution
-
Cellex-C Betaplex Fresh Complexion Mist C6031/C4237 180ml/6oz
-
Ammar Abdul Aziz | Publons
-
NEW PRODESIGN DENMARK 1270 C.6031 BLACK EYEGLASSES ...
-
Prodesign Denmark Eyeglasses 4366 C.6031 Pure Titanium Silver ...
-
NEW PRODESIGN DENMARK 1288 C.6031 BLACK EYEGLASSES ...
-
C6031 - Docs - Microsoft Technical Documentation
-
Disable Display Status Warning C6031 Return Value Ignored: "scanf"
-
Can Anyone Help Me With My C Code In This Visua...anycodings