Strncpy Error

Im receiving a error C4996 and says it is unsafe to use "strncpy", how can I fix this?
Google is your friend :+) Microsoft has documentation for all of it's error codes.

There is a safer version of that function

The following is a really good reference site:

https://en.cppreference.com/w/c/string/byte/strncpy
closed account (E0p9LyTq)
If you are using Visual Studio you can #define _CRT_SECURE_NO_WARNINGS before your #include files.

Not recommended. It is using a bomb to swat a fly. And doesn't fix the problems with the functions, you can still have buffer overruns.

OR

Use C11's strncpy_s() function that was created to prevent buffer overruns.

See TheIdeasMan's link for how to use.
At the design level, while the above answers tell you how to use strncpy safely, you might consider not using it at all. If you can, just use a C++ string type instead.
strncpy isn't unsafe as it's bounded by the buffer size of the input and/or the string length of the input.
Last edited on
That strncpy is unsafe and deprecated is Microsoft's own opinion, it still part of C and C++ standard.
The suggested alternative strncpy_s is only supported on Visual Studio and C11.

IMHO people who like safety shouldn't use C or C++ at all.
Topic archived. No new replies allowed.