c++中hello,world的几种输出方式

时间:2025-03-08 19:21:39

1.利用puts()输出

  1. #include<cstdio> // puts
  2. int main()
  3. {
  4. puts("Hello, world!"); // 输出
  5. }
  1. #include <> // puts
  2. int main()
  3. {
  4. puts("Hello, world!"); // 输出
  5. }

2.利用printf输出

  1. #include <> // printf
  2. int main()
  3. {
  4. printf("Hello, world!\n"); // 输出
  5. }

3.利用cout输出

  1. #include <iostream> // cout
  2. int main()
  3. {
  4. std::cout << "Hello, world!\n"; // 输出
  5. }
  1. #include <iostream> // cout
  2. using std::cout; // 使用std名字空间的单个名字cout
  3. int main()
  4. {
  5. cout << "Hello, world!\n"; // 输出
  6. }
  1. #include <iostream> // cout, endl
  2. using namespace std; // 使用名字空间std(的任何名字)
  3. int main()
  4. {
  5. cout << "Hello, world!" << endl; // 输出
  6. }

4.总

  1. #include <cstdio> // puts, printf
  2. #include <iostream> // cout, endl
  3. using namespace std; // 使用名字空间std(的任何名字)
  4. int main()
  5. {
  6. puts("1. Hello, world!"); // 输出
  7. printf("2. Hello, world!\n"); // 输出
  8. cout << "3. Hello, world!\n"; // 输出
  9. }