C++字符型

183 阅读1分钟
#include<iostream>
using namespace std;

/*
字符型
作用:字符型变量用来显示单个字符
语法:char ch = 'a';
注意:1. 显示字符型变量时,用单引号将字符括起来,不要用双引号
2. 单引号内只能有一个字符,不可以是字符串

C和C++中字符型变量只占用1个字节
字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放到存储单元

*/

int main(){
    
    //1. 字符型变量创建方式
    char ch = 'a';
    cout << ch << endl;
    //2. 字符型变量所占内存大小
    cout << "char字符串变量所占内存:" << sizeof(char) << endl;
    //3. 字符型变量常见错误
    //char ch2 = 'b'  
    //char ch2 = 'adslfjl'

    //4. 字符型变量对应的ASCII编码, a == 97, A == 65
    cout << (int)ch << endl;

    cout << "hello world!" << endl;
    system("pause");
    return 0;
}