学习c++(从入门到入土第一天)

编写hello world

#include <iostream>
using namespace std;
int main()
{
	cout<<"hello world";
}

注释

#include <iostream>
using namespace std;
//单行注释

/*
多行注释
*/ 

int main()
{
	cout<<"hello world";
}

变量

#include<iostream>
using namespace std;
int main()
{
	//变量命名方式   数据类型	变量名	=	值; 
	int a = 100;
	cout<<a<<endl; 
}

常量

int:整型,4字节,-2^31-2^31-1

short:短整型,2字节,-2^15-2^15-1

long:长整型,windows4字节,linux32位是4字节,64位是8字节,-2^31-2^31-1

long long:长长整型,八个字节,-2^63-2^63-1

#include <iostream>
using namespace std;
#define d 150
//宏常量   #define 常量名 常量值;   通常在文件上方定义,不需要使用=进行赋值

int main() {
    //const 数据类型 常量名 = 常量值;常量的值不可以被修改
    const int a=100;
    cout << a << endl;
    cout << d << endl;
    return 0;
}

关键字

c++中具有特殊意义的符号。

标识符命名规则

#include <iostream>
using namespace std;
//标识符命名规则:
/*
1、标识符只能由字母、数字、下划线组成
2、标识符不能是关键字
3、标识符首字母不能是数字
4、标识符区分大小写
 */
int main() {
    int a = 100;
    int A = 1000;
    cout << "a=" << a << endl;
    cout << "A=" << A << endl;
    return 0;
}

数据类型

整型

#include <iostream>
using namespace std;
//数据类型  整型
int main() {
    int a =100;
    cout << a << endl;
    return 0;
}

sizeof关键字

#include <iostream>
using namespace std;
int main() {
    //sizeof关键字可以求出不同类型数据所占空间大小
    int a=10;
    cout<< sizeof(a)<<endl;
    cout<< sizeof(char)<<endl;
    cout << "Hello, World!" << endl;
    return 0;
}

浮点型

#include <iostream>
using namespace std;
int main() {
    //float 表示7位有效数字。double表示15-16位有效数字
    //float单精度占用4字节,double是双精度占用8字节
    float f = 3.14;
    double d = 10.6;
    cout << d << endl;
    cout << f << endl;
    return 0;
}

字符型

#include <iostream>
using namespace std;
int main() {
    //字符型,关键字为 char  定义  char ch = 'a';   必须使用单引号,且单引号内只能有一个字符
    char ch='a';
    cout << ch << endl;
    return 0;
}

转义字符

#include <iostream>
using namespace std;
int main() {
    //转义字符  包含 \n  \t  \\
    //  \n表示换行符,\t表示制表符,跳到下一个tab的位置,用于对齐,\\表示输出一个\
    cout << "hello \nworld\n";
    cout << "hello \t world"<<endl;
    cout << "hi\t c++!" << endl;
    cout << "\\" << endl;
    return 0;
}

字符串型

#include <iostream>
#include <string>
using namespace std;
int main() {
    //字符串型  需要添加string头文件, string 字符串名 = "要输入的字符串";   字符串要用双引号
    string str = "hello world!";
    cout << str << endl;
    return 0;
}

布尔型

#include <iostream>
using namespace std;

int main() {
    //bool类型数据只有true 和 false,表示真和假,0表示假,1表示真
    bool b = false;
    bool b1 = true;
    cout << b << endl;
    cout << b1 << endl;
    return 0;
}

数据输入

#include <iostream>
using namespace std;
int main() {
    //输入  cin >> a;
    int a;
    cin >> a ;
    cout << a << endl;
    char c;
    cin >> c;
    cout << c << endl;
    string str;
    cin >> str;
    cout << str << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

123。.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值