c - printf 只打印 "1537"而不是整数的值

标签 c printf

我刚刚开始学习编程,所以这似乎是一个令人厌烦的问题。但是当我尝试打印一个整数的值时,它却打印出“1537”。下面是函数,大家可以自己细细品味。

void compare(void) {
int num1;
int num2;
int num3;
int num4;
int smallest;

printf("Please enter four integers:\n");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);

num1 = smallest;
if (num2 < smallest)
    num2 = smallest;
if (num3 < smallest)
    num3 = smallest;
if (num4 < smallest)
    num4 = smallest;

printf("%d is the smallest\n", smallest);

}

最佳答案

您需要将值分配给 smallest 而不是从它开始。

改成

smallest=num1;
if (num2 < smallest)
    smallest= num2;
if (num3 < smallest)
    smallest=num3;
if (num4 < smallest)
    smallest=num4;

~有时很难发现最小的错误~:)

关于c - printf 只打印 "1537"而不是整数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18399623/

相关文章:

将 Char * 缓冲区转换为十六进制以便在 c 中打印

c - 返回 ;这个语句返回什么?

c - scanf 和 printf 未按顺序执行

c - 将void(*)()指针用于其他功能

c - 文件和字符串

c - 生产者/消费者实现: Stuck in consumer loop

无法通过函数返回值打印字符串?

c - printf 是如何工作的?

在c中复制矩阵

c++ - 为什么 glGenVertexArrays 是为 C 程序定义的,而不是 Linux 上的 C++ 程序?