美文网首页
变量/常量/字符串

变量/常量/字符串

作者: Sheik | 来源:发表于2021-07-24 08:41 被阅读0次

环境:ide:Mac+clion

视频链接:
https://www.bilibili.com/video/BV1Hb411Y7E5?p=5
变量创建:
变量类型 名字= 变量赋值;

int a = 10;
printf("printf 打印当前变量值: %d \n", a);
cout<< "a 的值是:"<<a<<endl;

整体注释:

#if 0
 //被注释的代码
#endif

常量,不可更改:

#define  WIDTH 110 //宏常量
const int CONSTINT = 111;
cout<< "不可更改的常量 WIDTH的值是:"<<WIDTH<<endl;
cout<< "不可更改的常量 CONSTINT的值是:"<<CONSTINT<<endl;

标识符:
1.不能是关键字
2.字母/数字/下划线组成
3.首不能是数字
4.区分大小写

整型,下面占用的内存空间不同(不同的机器占用空间也不同):
1.short 2
2.int 4
3.long 8

  1. long long 8
    short num1 = 10;
    int num2 = 10;
    long num3 = 10;
    long num4 = 10;
    cout<< " sizeof(num1)的值是:"<<sizeof(num1)<<endl;//2
    cout<< "sizeof(num2)的值是:"<<sizeof(num2)<<endl;//4
    cout<< "sizeof(num3)的值是:"<<sizeof(num3)<<endl;//8
    cout<< "sizeof(num4)的值是:"<<sizeof(num4)<<endl;//8

浮点型/实型:
1.float 4
2.double 8

    //科学计数法:
    float sFloat = 3e2; //  3* 10^2 3乘以10的2次方
    cout<< "sFloat的值是:"<<sFloat<<endl;
    float sFloat2 = 3e-3; //  3* 0.1^3 3乘以0.1的3次方
    cout<< "sFloat2的值是:"<<sFloat2<<endl;

字符:

    char ch = 'a';
    cout<< ch<<endl;
    cout<< (int)ch<<endl;//97
    char ch2 = 'A';//65
    cout<< (int)ch2<<endl;

转义字符:

\\  代表 \
\n  换行
\t  制表符
\r  回车
\b  退格
\f 换页
 cout<< "a \nb"<<endl; // 换行
 cout<< "\\" <<endl;//输出反斜杠
 cout<< "a \tb"<<endl;//水平制表符
 cout<< "a \t\tb"<<endl;//水平制表符 ,有一种对其的效果 \t 和前面的数字一共占用8个空格

//字符串

    char ch[] = "abcd"; //c风格的字符串
    string str = "abcdef";
    cout<<ch<<endl;
    cout<<str<<endl;
    cout<<str.length()<<endl;
    cout<<str.substr(0,4)<<endl;
//bool 类型 true/false  1/0  占用1个字节
    bool flag = true;
    cout<<flag<<endl; 

相关文章

网友评论

      本文标题:变量/常量/字符串

      本文链接:https://www.haomeiwen.com/subject/xfoumltx.html