查看: 257  |  回复: 0
C++ 写个Hello, world!
楼主
发表于 2023年10月1日 11:12

VS2022中,输入:

#include <iostream>
using namespace std;
int main()
{
	cout << "Hello, world!" << endl;
	return 0;
}

也可以用 "\n" 代替以上代码里的 endl。输出多行可以:

#include <iostream>
using namespace std;
int main()
{
	cout << "...............\n"
		<< "Hello, world!\n"
		<< "Welcome to c++\n"
		<< "...............\n";
	system("pause"); //如果想暂停得到 请按任意键继续. . .
	return 0;
}

当然也可以:

#include <iostream>
//using namespace std; //不用这行,改用std::格式
int main()
{
	std::cout << "Hello, world!" << std::endl;
	return 0;
}


1楼
发表于 2023年10月1日 11:21

据说cout 流速度较慢,如果速度过慢可以用 <stdio.h> 库中的 printf() 格式化输出函数,不需要 using namespace std;。

#include <stdio.h>
int main()
{
    printf("Hello World!\n");
    return 0;
}


您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2024 MaNongKu.com