0

Every scanf gets an error of C4996, use scanf_s instead.

I read every similar post about this issue and I know I can solve this issue with #pragma warning(disable : 4996), but what I want to ask is I was using vstudio before and it never happened to me before. I can compile without problem in CodeBlocks or DevC++ but why exactly is this happening and if I disable warnings, would that affect my code in any way?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Ischys
  • 1
  • 2
  • Does it not tell you what is deprecated? – Roope Sep 02 '18 at 09:55
  • Your probably not using the same compiler. CodeBlocks usually defaults to GCC. Visual Studio will default to MSVC. Never use Dev C++ for anything. – Aluan Haddad Sep 02 '18 at 09:58
  • Doesn't matter what I use it for,every scanf gets an error of C4996, use scanf_s instead. – Ischys Sep 02 '18 at 10:00
  • 1
    @Ischys That's kind of the most important part of your question. I edited it in. – melpomene Sep 02 '18 at 10:07
  • MSVC has a set of "safe" functions like `scanf_s` and its just forcing you to use their non standard functions instead of the standard ones – Spikatrix Sep 02 '18 at 10:08
  • Possible duplicate of [Difference between scanf and scanf_s](https://stackoverflow.com/questions/21434735/difference-between-scanf-and-scanf-s)? – George Sep 02 '18 at 10:11
  • @George no I learned what's the difference from posts I'm just talking about that why compiler forces me to use it ( it doesn't work with scanf_s ) and If I get rid of warnings will that cause a problem for my programs in any way? – Ischys Sep 02 '18 at 10:15
  • @Ischys Long and short of it is that `scanf_s` has a parameter for bounds checking, making it easier to avoid undefined behaviour. The flip side is that `scanf_s` is not standard, and therefore not portable. Personally, I don't think `scanf_s` has enough merit to warrant it being shoved down our throats. If you want the best of both worlds then you could wrap `scanf` and `scanf_s` in macros and only define `scanf_s` if the current compiler is some form of msvc. – George Sep 02 '18 at 10:16
  • @George so it's just better if Jr.Programmer like me disable it for good. – Ischys Sep 02 '18 at 10:20
  • If you need your code to be portable then i'd wrap it up in a macro so your program will still compile with other compilers. If you're an early beginner then disabling it for good might be a bad idea. Undefined behaviour bugs(the thing `scanf_s` aims to save you from) are hard enough for experienced programmers to find, they're just a source of infinite frustration for early beginners. But if you've got a program and you know it's 100% correct and works and it's all you'll use msvc for, then you should probably just save some time and disable it. – George Sep 02 '18 at 10:26

0 Answers0