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.
C4996: 'strcpy': This function or variable may be unsafeConsider using strcpy_s insteadwarning C4996: 'fopen': This function or variable may be unsafe 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,getenvin a project with SDL checks enabled - Visual Studio project defaults (
/sdl) turning warnings into errors std::copy/std::equalwith raw pointers (checked iterators warning C4996)- Deprecated POSIX names such as
strdup,read,open
⚡ Quick fix
- Replace with safer code:
snprintf,std::string,std::vector, orfopen_s - Define
_CRT_SECURE_NO_WARNINGSbefore the includes if you accept the risk - Disable SDL checks only for legacy code: Project → C/C++ → General → SDL checks = No
Detailed fix by platform
Visual Studio
- Project Properties → C/C++ → Preprocessor → add
_CRT_SECURE_NO_WARNINGS(or put#define _CRT_SECURE_NO_WARNINGSat the very top of the file) - Use
#pragma warning(disable: 4996)locally only
CMake
target_compile_definitions(app PRIVATE _CRT_SECURE_NO_WARNINGS)(MSVC only)
Code examples
Portable replacement
#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
- Function — Which function is flagged?
- Settings — SDL checks or warnings-as-errors on?
- 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:
Similar errors
- C2065 MSVC: error C2065: 'x': undeclared identifier
- comparison of integers of different signs C/C++: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' [-Wsign-compare]
- control reaches end of non-void function C/C++: warning: control reaches end of non-void function [-Wreturn-type]
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 15 Oct 2026