C++ 检查C和C中的输出错误++;
我写了这个简单的程序。我知道C中的C++ 检查C和C中的输出错误++;,c++,c,printf,cout,C++,C,Printf,Cout,我写了这个简单的程序。我知道C中的printf()函数返回成功打印的字符总数,因此下面的C程序工作正常,因为任何非零值在C中都被计算为true #include <stdio.h> int main(void) { if (printf("C")) return 0; } 因为这和写一样 cout << "C++"; if(cout){ //do whatever } std::cout << "hello, world" <<
printf()
函数返回成功打印的字符总数,因此下面的C程序工作正常,因为任何非零值在C中都被计算为true
#include <stdio.h>
int main(void)
{
if (printf("C"))
return 0;
}
因为这和写一样
cout << "C++";
if(cout){
//do whatever
}
std::cout << "hello, world" << std::endl;
cout这个我不太清楚你想做什么,但是假设你想让你的程序在打印成功的情况下成功退出,你应该用C写这个:
#include <stdlib.h>
#include <stdio.h>
int
main()
{
if(printf("C") < 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
这样做的原因是,在c++11中调用操作符,到bool的转换不是隐式的
std::cout << "Hello" << " " << "C++" << " " << "World!" << std::endl;
#include <stdlib.h>
#include <stdio.h>
int
main()
{
if(printf("C") < 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
int
main()
{
if (!(std::cout << "C++"))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
std::cout << "You've got " << 99 << " points" << std::endl;
explicit operator bool() const;
#include <stdlib.h>
#include <stdio.h>
int
main()
{
if(printf("C") < 0 || fflush(stdout) < 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
int
main()
{
if (!(std::cout << "C++" << std::flush))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
std::cout << "hello, world\n" << std::flush;
std::cout << "hello, world" << std::endl;
#include <cstdlib>
#include <iostream>
int
main()
{
std::cout.exceptions(std::ifstream::failbit);
try
{
std::cout << "C++" << std::flush;
return EXIT_SUCCESS;
}
catch (const std::ios_base::failure &e)
{
return EXIT_FAILURE;
}
}