5. There are two int variables: a and b, don’t use “if”, “? :”, “switch”orother judgement statements, find out the biggest one of the two numbers.
答案:( ( a + b ) + abs( a - b ) ) / 2 6. 如何打印出當(dāng)前源文件的文件名以及源文件的當(dāng)前行號(hào)?
答案:
cout << __FILE__ ;
cout<<__LINE__ ;
__FILE__和__LINE__是系統(tǒng)預(yù)定義宏,這種宏并不是在某個(gè)文件中定義的,而是由編譯器定義的。
7. main主函數(shù)執(zhí)行完畢后,是否可能會(huì)再執(zhí)行一段代碼,給出說(shuō)明?
答案:可以,可以用_onexit 注冊(cè)一個(gè)函數(shù),它會(huì)在main 之后執(zhí)行intfn1(void), fn2(void), fn3(void), fn4 (void);
void main( void )
{
String str("zhanglin");
_onexit( fn1 );
_onexit( fn2 );
_onexit(fn3 );
_onexit( fn4 );
printf( "This is executed first.\n" );
}
int fn1()
{
printf( "next.\n" );
return 0;
}
int fn2()
{
printf( "executed " );
return 0;
}
int fn3()
{
printf( "is " );
return 0;
}
int fn4()
{
printf( "This ");
return 0;
}
The _onexit function is passed the address of afunction (func) to be called when the program terminates normally. Successivecalls to _onexit create a register of functions that are executed in LIFO(last-in-first-out) order. The functions passed to _onexit cannot takeparameters.
8. 如何判斷一段程序是由C 編譯程序還是由C++編譯程序編譯的?
答案:
#ifdef __cplusplus
cout<<"c++";
#else
cout<<"c";
#endif
|
|
||
|
|