在做我的作业时,我注意到一些非常奇怪的东西,我无法弄清楚为什么.
int x = 5;
cout << pow(x, 2);
Run Code Online (Sandbox Code Playgroud)
结果是25.那没关系.但如果我写这样的程序:
int x = 5;
int y = pow(x, 2);
cout << y;
Run Code Online (Sandbox Code Playgroud)
结果是24!
当x为2,3,4,6,7,8没有问题,但是5,10,11,13等结果比它应该低1.
if()也是一样的.
for (int x = 1; x <= 20 ; x++) {
if (x * x == pow(x, 2))
cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)
它打印出数字1,2,3,4,6,8,12,16.
我无法弄清楚为什么这段代码不起作用.(我知道如何修复它,但我对这个特定的解决方案很感兴趣)它需要一个字符串并且put在其他字符串的末尾.x = "abc",y = "def"执行foo之后,x = "abcdef".执行我的代码后,我才得到"abc".
#include <stdio.h>
#define MAX 10
void foo(char *x, char *y){
while(*x++);
while(*y){
*x++ = *y++;
}
*x = '\0';
}
int main(void){
char x[MAX+1], y[MAX+1];
scanf("%s %s", x, y);
foo(x, y);
printf("%s", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做,代码工作得很好.
while(*x) x++;
Run Code Online (Sandbox Code Playgroud)