gpt4 book ai didi

c - Kochan Book : Programming in C i + j - i % j 中的一项练习遇到问题

转载 作者:行者123 更新时间:2023-11-30 20:10:29 25 4
gpt4 key购买 nike

这是我的 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

最佳答案

  1. 当您使用未初始化的局部变量 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com