chimier-c++-day01

2021-08-03  本文已影响0人  __method__
#include <iostream>
using namespace std;

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

C++注释

本身不会被执行, 是给别人看的, 对程序的解释
单行注释 //
多行注释 /* */

变量

#include <iostream>
using namespace std;

int main(){
    //本身不会被执行, 是给别人看的, 对程序的解释
    /*
     * 多行注释
     * 多行注释
     */
    // 变量类型 变量名 = 变量值;
    // 声明并且初始化
    int age = 18;
    cout <<"age init = "<< age<<endl;
    // 修改变量
    age = 30;
    cout <<"age update = "<<age<<endl;
    // 先声明, 使用的时候再初始化
    int score;
    score = 100;
    cout <<"score init "<<score<<endl;

}

常量

作用: ⽤于记录程序中不可更改的数据
C++定义常量两种⽅式

1. #define 宏常量: #define  常量名 常量值  通常在⽂件上⽅定义,表⽰⼀个常量
2. const修饰的变量 const 数据类型 常量名  = 常量值 
通常在变量定义前加关键字const,修饰该变量为常量,不可修改
#include <iostream>
# define PI 3.14
# define WEEK 7
using namespace std;

int main(){
   cout << "pi = " << PI <<endl;
   cout << "a week have " << WEEK <<" days" << endl;
   const int  month = 12;
//   month = 17;// 不能修改
   cout << "a year have " << month <<" months" << endl;
}

关键字(保留字)

注意: 在定义变量或者常量时候,不要⽤关键字
作⽤:关键字是C++中预先保留的单词(标识符)


标识符

C++规定给标识符(变量、常量)命名时,有⼀套⾃⼰的规则

using namespace  std;
int main () {
    int num = 100;
//    int num = 100;  不能重名
    int Num = 100;
//    int 1Num = 100; 非法
    int Num1 = 100;
    int Num_ = 100;
    int _Num = 100;
}

数据类型

C++规定在创建⼀个变量或者常量时,必须要指定出相应的数据类型,否则⽆法给变量分配内存
1 字节(Byte) = 8 位(bit)

sizeof关键字

利⽤sizeof关键字可以统计数据类型所占内存⼤⼩
语法: sizeof( 数据类型/ 变量)

int main(){
    cout << "short = " << sizeof(short)<< endl;
    cout << "int = " << sizeof(int)<< endl;
    cout << "long = " << sizeof(long)<< endl;
    cout << "long long  = " << sizeof(long long)<< endl;
}


浮点型(小数/实型)

浮点型有两种

#include <iostream>

using namespace std;

int main(){
    //设置consle输出有效数字的范围
    cout.precision(10);
    float f = 3.141592653f; // 整数也是有效位范围
    cout << "f = " << f<< endl;
    double d = 3.141592653; //
    cout << "d = " << d<< endl;

}

字符型 (char型)

作用: 字符型变量⽤于显⽰单个字符

#include <iostream>

using namespace std;

int main(){
    char ch1 = 'A';
    cout << "ch1 = " << ch1 << endl;
    char ch2 = 'a';
    cout << "ch2 = " << ch2 << endl; // 97
    cout << "int ch2 = " << (int)ch2 << endl;
    cout << "sizeof = " << sizeof(char) << endl;// 1字节
    char ch3 = 98;
    cout << "ch3 = " << ch3 << endl;

}


ASCII 码⼤致由以下两部分组成:

字符串型

作⽤:⽤于表⽰⼀串字符
两种风格

 char str[] = "chimier";
    cout << str << endl;
    string  name = "eric";
    cout << name << endl;

布尔数据类型 boolean

布尔数据类型代表真或假的值
bool类型只有两个值:

#include <iostream>

using namespace std;

int main(){
    bool is_shangban = true;
    cout << is_shangban << endl;
    bool is_money = false;
    cout << is_money << endl;
    cout << sizeof(bool )<< endl;

}

控制台中输入

int age;
    cout << "please input your age" << endl;
    cin >> age;
    cout << "age = "<<  age <<endl;

    // 浮点型输入
    double height;
    cout << "please input your height" << endl;
    cin>>height; // 将console中的数据赋给变量age
    cout << "height = " << height << endl;
    // 字符型输入
    char ch;
    cout << "please input a char" << endl;
    cin>>ch;
    cout << "ch = " << ch << endl;

    // 字符串型输入
    string name;
    cout << "please input your name" << endl;
    cin>>name;
    cout << "name = " << name << endl;

    // 布尔型输入
    bool flag;
    cout << "please input a flag" << endl;
    cin>>flag;
    cout << "flag = " << flag << endl;
上一篇 下一篇

猜你喜欢

热点阅读