Visual Studio Community 2015 - _CRT_SECURE_NO_WARNINGS

Visual Studio Community 2015 - _CRT_SECURE_NO_WARNINGS

1. _CRT_SECURE_NO_WARNINGS

1>------ Build started: Project: yongqiang, Configuration: Debug Win32 ------
1>  foreverstrong.c
1>d:\visual_studio_workspace\yongqiang\yongqiang\foreverstrong.c(51): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>  c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\stdio.h(1270): note: see declaration of 'scanf'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

error C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	

2. _CRT_SECURE_NO_WARNINGS

2.1 文件开头添加代码

#define _CRT_SECURE_NO_WARNINGS

注意,必须是文件的第一行,要在没有 include 任何头文件之前。

2.2 项目添加预处理定义

项目 -> 属性 -> 配置属性 -> C/C++ -> 预处理器 -> 预处理定义

_CRT_SECURE_NO_WARNINGS
a. 与前面的要用分号分开,后面加分号。
b. 换行添加。

在这里插入图片描述

在这里插入图片描述

/*
============================================================================
Name        : hello_world.c
Author      : Yongqiang Cheng
Version     : Version 1.0.0
Copyright   : Copyright (c) 2019 Yongqiang Cheng
Description : Hello World in C, Ansi-style
============================================================================
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int compute_word_length(const char *input_string)
{
	const char *string_pointer = input_string;
	int string_length = 0;
	int idx = 0;
	int word_length = 0;

	if (NULL == input_string)
	{
		return 0;
	}

	string_length = strlen(input_string);

	if (0 == string_length)
	{
		return 0;
	}

	for (idx = string_length - 1; string_pointer[idx] != ' '; idx--)
	{
		word_length++;
		if (string_length == word_length)
		{
			break;
		}
	}

	return word_length;
}

int main()
{
	char input_string[5000] = { 0 };
	int word_length = 0;
	int idx = 0;

	while (EOF != scanf("%c", &(input_string[idx])))
	{
		idx++;
	}

	if ('\n' == input_string[strlen(input_string) - 1])
	{
		input_string[strlen(input_string) - 1] = '\0';
	}

	word_length = compute_word_length(input_string);

	printf("%d\n", word_length);

	return 0;
}

在这里插入图片描述
退出循环:

qiang + Enter
Ctrl + Z + Enter
Ctrl + Z + Enter
发布了523 篇原创文章 · 获赞 1850 · 访问量 112万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/105015304