#include<stdio.h>
#include<string.h>
#define DENSITY 62.4
int main()
{
float weight, volume;
int size, letters;
char name[40];
printf("Hi!What's your first name?\n");
scanf("%s", name);
printf("%s,what's your weught in pounds?\n", name);
scanf("%f", &weight);
size = sizeof(name);
letters = strlen(name);
volume = weight / DENSITY;
printf("Well,%s,your volume is %2.2f cubic feet.\n", name, volume);
printf("and we have %d bytes to store it.\n", size);
return 0;
}
在VS 2019上 scanf 我改成了 scanf_s
vs2019检查严格,注意以下几点:
1.变量在使用前必须初始化
2.scanf改为scanf_s
代码修改如下:
#include<stdio.h>
#include<string.h>
#define DENSITY (float)62.4
int main()
{
float weight = 0, volume = 0;
int size = 0, letters = 0;
char name[40] = {0};
printf("Hi!What's your first name?\n");
scanf_s("%s",name,39);
printf("%s,what's your weught in pounds?\n", name);
scanf_s("%f", &weight);
size = sizeof(name);
letters = strlen(name);
volume = weight / DENSITY;
printf("Well,%s,your volume is %2.2f cubic feet.\n", name, volume);
printf("and we have %d bytes to store it.\n", size);
return 0;
}
你可以用#pragma warning(disable:4996)
来防止报错。
vs2019认为scanf安全性低,容易内存溢出,所以要加字符串长度。