ya修 2021-07-26 10:37 采纳率: 100%
浏览 522
已结题

为什么我的程序在Dev c++上能运行,在VS 2019上不能

#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

  • 写回答

2条回答 默认 最新

  • qfl_sdu 2021-07-26 10:47
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月3日
  • 已采纳回答 7月26日
  • 创建了问题 7月26日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装