C++ 为什么这个C++;函数返回值?
为什么这个代码会打印出n-100C++ 为什么这个C++;函数返回值?,c++,function,for-loop,C++,Function,For Loop,为什么这个代码会打印出n-100 int hello(int n) { for(int i = 0; i < n-100; i++) { } } int main() { int h = hello(12); cout << hello(12) << " " << h << endl; } inthell
int hello(int n)
{
for(int i = 0; i < n-100; i++)
{
}
}
int main()
{
int h = hello(12);
cout << hello(12) << " " << h << endl;
}
inthello(intn)
{
对于(int i=0;i 难道你只是看到了未定义行为的结果
始终使用-Wall-Werror
进行编译,以防止此类错误蔓延到代码中。您只是看到了未定义行为的结果
始终使用-Wall-Werror
进行编译,以防止此类错误蔓延到您的代码中。“为什么此代码打印出n-100?”它不是,我得到11
:)返回值应该存储在某个地方,比如在寄存器中。如果你不设置返回值,该寄存器可能包含其他内容,比如你最近使用的值。@Boperson啊,谢谢,这很有道理。“为什么这个代码打印出n-100?”没有,我得到11
:)返回值应该存储在某个地方,比如在寄存器中。如果不设置返回值,该寄存器可能包含其他内容,比如您最近使用的值。@Boperson啊,谢谢,这很有意义。
int hello1(int n)
{
for(int i = 0; i < 12; i++);
}
int hello2(int n)
{
(n - 100);
}