You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言使用scanf后printf输出异常,出现格式类型不匹配警告求助

问题解决:C语言printf输出地址而非输入值的错误

错误原因

你的问题出在最后一行printf的参数上:

  • scanf("%i", &num1);是正确的,因为scanf需要通过变量的内存地址(指针)来修改变量的值,所以必须加&取地址符。
  • 但printf("You have entered: %i!", &num1);里的&num1是错误的:%i格式符要求对应一个int类型的参数,而&num1的类型是int*(指向int的指针,也就是内存地址),这就导致了类型不匹配的警告;同时printf输出的是这个内存地址的数值,也就是你看到的长串数字。

修正后的代码

#include <stdio.h>

int main() {
    int num1;
    printf("Please enter a number: \n");
    scanf("%i", &num1);
    printf("You have entered: %i!", num1); // 移除&符号,直接传入变量本身
    return 0;
}

验证结果

输入4后,输出会变成:

Please enter a number: 
4
You have entered: 4!

内容的提问来源于stack exchange,提问作者truballin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 12:29:54