这是我的 C 源代码:
details that stackoverflow want to see or i can't edit
#include <stdio.h>
int main (void) {
int i, j;
int Next_multiple = (i + j) - (i % j);
i = 365;
j = 7;
printf("Solution for i == 365 j == 7 = %i \n", Next_multiple);
i = 12258;
j = 23;
printf("Solution for i == 12258 j == 23 = %i \n", Next_multiple);
i = 996;
j = 4;
printf("Solution for i== 996 j == 4 = %i \n", Next_multiple);
return 0;
}
这就是输出:
.exe(Visual Studio 工具中的 Windows cmd)
Solution for i == 365 j == 7 = 18039659
Solution for i == 12258 j == 23 = 18039659
Solution for i== 996 j == 4 = 18039659
最佳答案
- 当您使用未初始化的局部变量 i 和 j 时,您会出现未定义的行为。
然后显示 3 倍相同的计算变量(使用这些随机值)。该如何整理呢?
1 将此函数放在 main 函数之前。
int Next_multiple(int i, int j)
{
return ( i + j) - (i % j);
}
改变
printf("Solution for i == 12258 j == 23 = %i \n", Next_multiple);
至
printf("Solution for i == 12258 j == 23 = %i \n", Next_multiple(i, j));
并删除
int Next_multiple = (i + j) - (i % j);
<小时/>
#include <stdio.h>
int Next_multiple(int i, int j)
{
return ( i + j) - (i % j);
}
int main () {
int i, j;
i = 365;
j = 7;
printf("Solution for i == 365 j == 7 = %i \n", Next_multiple(i,j));
i = 12258;
j = 23;
printf("Solution for i == 12258 j == 23 = %i \n", Next_multiple(i,j));
i = 996;
j = 4;
printf("Solution for i== 996 j == 4 = %i \n", Next_multiple(i,j));
return 0;
}
关于c - Kochan Book : Programming in C i + j - i % j 中的一项练习遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45496593/