C4996 C/C++ Origin: Application & API Last updated 15 Oct 2026

MSVC: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead

Visual C++ treats classic C library functions such as strcpy, sprintf, fopen and scanf as deprecated or unsafe and, with SDL checks on, turns the warning into an error.

Exact message you may see C4996: 'strcpy': This function or variable may be unsafeConsider using strcpy_s insteadwarning C4996: 'fopen': This function or variable may be unsafe
Seen on: C/C++ MSVC Windows

Meaning

Microsoft marks functions without bounds checking as unsafe. The message suggests the _s variant, which is not portable. The better fix is a bounded standard alternative (snprintf, std::string, std::copy). You can silence it with _CRT_SECURE_NO_WARNINGS, but you then keep the buffer-overflow risk.

Common causes

  • Using strcpy, strcat, sprintf, scanf, fopen, getenv in a project with SDL checks enabled
  • Visual Studio project defaults (/sdl) turning warnings into errors
  • std::copy/std::equal with raw pointers (checked iterators warning C4996)
  • Deprecated POSIX names such as strdup, read, open

⚡ Quick fix

  1. Replace with safer code: snprintf, std::string, std::vector, or fopen_s
  2. Define _CRT_SECURE_NO_WARNINGS before the includes if you accept the risk
  3. Disable SDL checks only for legacy code: Project → C/C++ → General → SDL checks = No

Detailed fix by platform

Visual Studio

  1. Project Properties → C/C++ → Preprocessor → add _CRT_SECURE_NO_WARNINGS (or put #define _CRT_SECURE_NO_WARNINGS at the very top of the file)
  2. Use #pragma warning(disable: 4996) locally only

CMake

  1. target_compile_definitions(app PRIVATE _CRT_SECURE_NO_WARNINGS) (MSVC only)

Code examples

Portable replacement

cpp
#include <cstdio>
#include <string>

int main() {
    char buf[16];
    // strcpy(buf, "hello");                      // C4996 on MSVC
    std::snprintf(buf, sizeof buf, "%s", "hello"); // bounded and portable
    std::string s = "hello";                       // best: no buffer at all
}

How to diagnose

  1. Function — Which function is flagged?
  2. Settings — SDL checks or warnings-as-errors on?
  3. Portability — Must the code also build on Linux/macOS?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

C2065 — MSVC: error C2065: 'x': undeclared identifier (C/C++)C2065comparison of integers of different signs — C/C++: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' [-Wsign-compare] (C/C++)comparison of integer…control reaches end of non-void function — C/C++: warning: control reaches end of non-void function [-Wreturn-type] (C/C++)control reaches end o…format expects argument of type — C: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=] (C/C++)format expects argume…implicit declaration of function — C: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration] (C/C++)implicit declaration …LNK1104 — MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running) (C/C++)LNK1104stack smashing detected — C/C++: *** stack smashing detected ***: terminated — buffer overflow on the stack (C/C++)stack smashing detect…C4996

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.