我看到了一个示例程序,用于演示递归,看起来它应该不起作用,但确实如此.逻辑很清楚,但为什么即使没有返回递归的函数调用它也能工作?return即使没有请求,看起来命令也会突破堆栈.这是语言标准还是gcc的东西?我在Windows和Linux上使用gcc编译的C和C++看到了它.
#include <iostream>
#include <cstdlib>
using namespace std;
int isprime(int num, int i)
{
if (i == 1) {
return 1;
}
else {
if (num % i == 0)
return 0;
else
isprime(num, i-1); // should be returned
}
}
int main(int argc, char** argv)
{
int input = atoi(argv[1]);
cout << input << "\t" << isprime(input, input/2) << "\n";
}
Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <string>
using namespace std;
string
crash()
{
}
int
noCrash()
{
}
int
main()
{
crash(); // crashes
// noCrash(); // doesn't crash
return 0;
}
Run Code Online (Sandbox Code Playgroud)
函数crash(),与Mingw g ++ 4.6.2崩溃,函数noCrash()执行没有问题.为什么在没有return语句的情况下函数返回字符串崩溃?
我在c ++中有以下代码:
int fff ( int a , int b )
{
if (a>b )
return 0;
else a+b ;
}
Run Code Online (Sandbox Code Playgroud)
虽然我没有在'else'之后写'return'但它没有出错!在main()中我写的时候:
cout<<fff(1,2);
Run Code Online (Sandbox Code Playgroud)
它打印1?怎么会发生这种情况
可以解释一下吗?
该程序应该找到在Unix上输入的以".exe"结尾的命令行参数.由于某种原因,它不起作用.这是代码:
int main( int argc, char* argv[] )
{
for ( int i = 1; i < argc; i++)
if( findExe( argv[i] ) )
cout << argv[i] << endl;
return 0;
}
bool findExe( char* argument )
{
if ( strlen( argument ) >= 4 )
{
string testExe = ".exe";
string initialWord=argument; //converts c-string to string
string temp( initialWord,( initialWord.size() - 4 ),4 );//creates temp with last four characters from initialWord
if ( !temp.compare(testExe) )
return true;
}
else …Run Code Online (Sandbox Code Playgroud)