How to fix “return value ignored: 'scanf'” code C6031 in visual studio

前端 未结 2 1214
慢半拍i
慢半拍i 2020-12-21 09:22

I 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 (Trans

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-21 10:10

    A 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 
    

    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 
    

    And now the relevant warnings are easy to see.

提交回复
热议问题