[英]Scanf_s reading wrong input
对于下面的代码(在C中)
int small_a, small_b;
printf("Please input two numbers\n");
scanf_s("%d %d", &small_a, &small_b);
printf("%d %d", &small_a, &small_b);
int test_2nd = small_a - small_b;
if (test_2nd < 0) {
printf("a is smaller %d", &small_a);
}
else {
printf("b is smaller %d", &small_b);
当我写4和2时,它输出的值是一个巨大的六位数(在这种情况下为5504620和5504608),我不知道它出了什么问题。 stdio.h
已作为头文件包含在内。
这里的问题在打印语句中。 在代码中
printf("%d %d", &small_a, &small_b);
您不需要( 想要 )取得( 打印 )地址。 删除&
。
也就是说,这实际上会调用未定义的行为 。 使用printf()
%d
需要一个int
类型的参数,而您实际上是在提供int *
,从而导致UB。
FWIW,要打印地址(指针),您需要使用%p
格式说明符并将参数强制转换为void *
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.