c——内置类型字面值

来源:互联网 发布:mac. wps 编辑:程序博客网 时间:2024/04/28 07:46

布尔型

  • true
  • false
void bool_literals(){    bool b1 = true;    bool b2 = false;}
注:bool不是c内置类型,bool,true,false不是c关键字,需#include<stdbool.h>
#ifndef __STDBOOL_H#define __STDBOOL_H/* Don't define bool, true, and false in C++, except as a GNU extension. */#ifndef __cplusplus#define bool _Bool#define true 1#define false 0#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)/* Define _Bool, bool, false, true as a GNU extension. */#define _Bool bool#define bool  bool#define false false#define true  true#endif#define __bool_true_false_are_defined 1#endif /* __STDBOOL_H */
注:_Bool是c内置类型,是c关键字,无需#include<stdbool.h>,bool,true,false只是宏而已
字面值类型
void bool_type_check(){    printf("sizeof(true) = %ld, sizeof(false) = %ld\n", sizeof(true), sizeof(false));    printf("true = %d, false = %d\n", true, false);}
output:
sizeof(true) = 4, sizeof(false) = 4true = 1, false = 0
结论:
  • true和false为int字面值

字符型

  • 字符:'a'
  • 转义字符:'\n',’\"‘,'\123','\x6b'
void char_literals(){    char c1 = '\a';    char c2 = '\n';    char c3 = '\"';    char c4 = '\123';    char c5 = '\x6b';        wchar_t wc1 = L'a';    wchar_t wc2 = L'\n';    wchar_t wc3 = L'\"';    wchar_t wc4 = L'\123';    wchar_t wc5 = L'\x6b';}
注:前缀L表示wchar_t类型(宽字符)
字面值类型
void type_check(char){    cout << "char" << endl;}void type_check(signed char){    cout << "signed char" << endl;}void type_check(unsigned char){    cout << "unsigned char" << endl;}void type_check(wchar_t){    cout << "wchar_t" << endl;}void char_type_check(){    type_check('a');    type_check(L'a');}
output:
charwchar_t
结论:
  • char,signed char,unsigned char是不同类型
  • 字符字面值为char类型

整型

格式表示:
  • 十进制:123,456
  • 八进制:0123,0456
  • 十六进制:0x7abc,0X8def
取值范围:
  • 8 bits signed:[-128, 127]([-2^7,  2^7 - 1]);8 bits unsigned:[0, 255]([0, 2^8 - 1])
  • 16 bits signed:[-32768, 32767]([-2^15 ~ 2^15 - 1]);16 bits unsigned:[0, 65535]([0, 2^16 - 1])
  • 32 bits signed:[-2147483648, 2147483647]([-2^31, 2^31 - 1]);32 bits unsigned:[0, 4294967295]([0, 2^32 - 1])
  • 64 bits signed:[-9223372036854775808, 9223372036854775807]([-2^63, 2^63 - 1]);64 bits unsigned:[0, 18446744073709551615]([0, 2^64 - 1])
字面值后缀:
  • u或U:unsigned
  • l或L:long
  • ll或LL:long long,不能用Ll或lL表示
注:后缀可以组合使用,例如UL或LU——unsigned long,l和1易混淆,用L表示
void int_literals(){    int i1 = 123;    int i2 = 0456;    int i3 = 0x7abc;    int i4 = 0X8def;    unsigned int ui = 123U;    long l = 123L;    unsigned long ul = 123LU;    long long ll = 123LL;    unsigned long long ull = 123ULL;}
字面值类型
void type_check(signed int){    cout << "signed int" << endl;}void type_check(unsigned int){    cout << "unsigned int" << endl;}void type_check(signed long){    cout << "signed long" << endl;}void type_check(unsigned long){    cout << "unsigned long" << endl;}void type_check(signed long long){    cout << "signed long long" << endl;}void type_check(unsigned long long){    cout << "unsigned long long" << endl;}void int_type_check(){    cout << "-----sizeof-----" << endl;    cout << "sizeof(int) = " << sizeof(int) << endl;    cout << "sizeof(long) = " << sizeof(long) << endl;    cout << "sizeof(long long) = " << sizeof(long long) << endl;        cout << "-----int-----" << endl;    type_check(5);    type_check(-2147483648);//32位int取值下限    type_check(2147483647);//32位int取值上限    type_check(-2147483649);//32位int取值下限-1    type_check(2147483648);//32位int取值上限+1        cout << "-----long-----" << endl;    type_check(-9223372036854775808);//64位long取值下限    type_check(9223372036854775807);//64位long取值上限    type_check(-9223372036854775809);//64位long取值下限-1    type_check(9223372036854775808);//64位long取值上限+1}
output:
-----sizeof-----sizeof(int) = 4sizeof(long) = 8sizeof(long long) = 8-----int-----signed intsigned longsigned intsigned longsigned long-----long-----unsigned long longsigned longunsigned long longunsigned long long
结论:
  • 正整数类型匹配按取值范围以int->long->long long顺序匹配,如果还不能匹配,最后考虑匹配unsigned long long,因此unsigned long long是最后匹配项
  • 负号(-)本质是运算负号,取补码运算,因此负整数的类型是其对应正整数的类型

浮点型

格式表示:
  • 十进制(必须包含小数点):123.456,.123,123.
  • 科学计数法(e或E前后必须有值):123e3,4.56E6,.789e9
字面值后缀:
  • f或F:float
  • l或L:long double
注:l和1易混淆,用L表示
void float_literals(){    float f1 = 123.456f;    float f2 = .123e3f;        double d1 = 123.456;    double d2 = .123e3;        long double ld1 = 123.456L;    long double ld2 = .123e3L;}
字面值类型
void type_check(float){    cout << "float" << endl;}void type_check(double){    cout << "double" << endl;}void type_check(long double){    cout << "long double" << endl;}void float_type_check(){    type_check(123.456);    type_check(.123e3);}
output:
doubledouble
结论:
  • 浮点字面值为double类型
0 0
原创粉丝点击