书写第一个C程序"Hello World"
从今天开始学习c++,版本使用visual studio 2019,希望可以坚持下去!
工程的创建
打开visual studio 2019后,选择创建新项目
创建新项目后右键源文件—>添加—>新建项
添加.cpp文件,修改名称后完成整个工程的创建
代码如下
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
system("pause");
return 0;
}
运行本地windows调试器
运行结果:
其中除了cout << “Hello World” << endl;是本次要打印的东西外,其他代码在所有程序均可使用。
p3注释
单行注释//
多行注释/* */
注释的内容,编译器是不会执行的
p4变量
作用:给一段指定的内存空间起名,方便操作这段内存
变量创建语法:数据类型+变量名 = 变量初始值;
例如 int a =10;
#include <iostream>
using namespace std;
int main()
{
int a = 10;
//在屏幕中输出Hello World
cout << "Hello World" << endl;
cout << "a=" << a << endl;
system("pause");
return 0;
}
p5常量
记录程序中不可修改的数据
1.#define 宏常量
2.const 数据类型 常量名 = 值
宏定义方法:
#include<iostream>
using namespace std;
#define day 7
int main()
{
int a = 10;
//在屏幕中输出Hello World
cout << "Hello World" << endl;
cout << "a=" << a << endl;
cout << "一周有" << day <<"天" << endl;
system("pause");
return 0;
}
const方法
#include<iostream>
using namespace std;
//#define day 7
int main()
{
int a = 10;
const int day = 7;
//在屏幕中输出Hello World
cout << "Hello World" << endl;
cout << "a=" << a << endl;
cout << "一周有" << day <<"天" << endl;
system("pause");
return 0;
}
p6关键字
定义变量或常量时,不要使用这些单词,否则会产生歧义
p7标识符命名规则
1,不能是关键字
2.只能由字母,数字,下划线组成
3.第一个字符不能是数字
4.区分大小写
p8数据类型
c++规定在创建一个变量或常量时,必须要指定出相应的数据类型,否则无法给变量分配内存
整型
p9sizeof关键字
利用sizeof可以看到数据类型所占内存大小
#include
using namespace std;
//#define day 7
//注释
int main()
{
int a = 10;
const int day = 7;
//在屏幕中输出Hello World
//cout << "Hello World" << endl;
//cout << "a=" << a << endl;
//cout << "一周有" << day <<"天" << endl;
cout << "int所占字节数为:" << sizeof(int) << endl;
system("pause");
return 0;
}
p10实型
1单精度float
2双精度double
float 数据类型后+f,表名这是浮点型,否则会默认为double型
p11字符型
语法:char ch = 'a';单引号内只能有一个字符
字符型变量只是将对应的ASCII编码放入存储单元
p12转义字符
p13字符串型
c语言风格字符串
char str1[] = "hello world";
c++型风格字符串
string str2 = "hello world";(用之前要包含头文件)
#include<string>
p14布尔数据类型
bool类型只有两个类型
ture 1
false 0
bool flag = true