When I execute a C language string copy in visual studio, the strcpy part becomes an error.

Question: Question:

I'm sorry if there is a shortage etc. for the first time posting here. Can you tell me the reason for the error?

#include "pch.h"
#include <iostream>
#include <string.h>
#include <stdio.h>

int main(void)
    char str[10];
    **strcpy (str, "MARIO");**
    printf("%s\n", str);
    return 0;

thank you for your reply. I got the following error:

Error message

C4996 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
ConsoleApplication4 c:\users\takuy\source\repos\consoleapplication4\consoleapplication4.cpp 12

At first, I thought it was a typo, but since I entered it according to the reference book, it was different, and I couldn't understand what was wrong.

Answer: Answer:

That's because the reference books I'm using are old and now use functions that I shouldn't use.

Either replace strcpy () with strcpy_s () (but the parameters and return values ​​are different, so it's out of the reference book), or lower the compiler warning level and #define _CRT_SECURE_NO_WARNINGS to get an error or warning. You can take measures such as preventing it from appearing (however, if you get used to it, you will overlook or ignore errors and warnings).

If possible, choose a reference book whose first edition is recent or compatible with the new edition of Visual Studio XXXX.
As a beginner, you will continue to encounter various errors, but if you don't understand, you should try this area.

Try translating the error message to the web:

This function or variable may not be safe. Consider using strcpy_s instead.
Use _CRT_SECURE_NO_WARNINGS to disable deprecation. See the online help for more information.

Search by error code (C4996 this time):
Compiler Warning (Level 3) C4996
Insecure CRT library functions

This function or variable may not be safe. Consider using it instead of safe_version. _CRT_SECURE_No_Warn to disable deprecation. See the online help for more information.
Global is deprecated by Microsoft in favor of some CRT and C ++ Standard Library functions and more secure versions. In most cases, deprecated functions allow write access to the buffer, which can lead to unchecked reads or serious security issues. The compiler warns you that these functions are no longer in use and suggests preferred functions.
It is recommended to use a function or variable instead of safe_version to resolve this issue. You can't overwrite the buffer or change the code for portability. If you overread and see what happens in your code, you can turn off the warning.
To turn off warnings for these CRT functions, define: _CRT_SECURE_No_Warns. To turn off warnings about deprecated global variables, define: _CRT_SECURE_No_Warning_GLOBALS. For more information on these deprecated functions and global variables, see: CRT security features and secure libraries. C ++ standard library.

Other than that, it is not a direct measure, but please try to understand by searching and browsing the explanation of Microsoft's Docs site.

By the way, the outline of the transition of C language and its standard is summarized below.
C language-Wikipedia

The transition of the standard of the language itself and the difference in correspondence depending on the vendor / version of the compiler are combined, and depending on what the reference book is written on, it is better or impossible to use what you can do. It is changing as it becomes possible to do.

Even at this Stack Overflow site, the beginners of the ancestors are addicted to them and ask a lot of questions, so searching for them will also be a learning experience.

Scroll to Top