intmain(int argc, char *argv[]) { bool b = false; int a = b; printf("sizeof(b) = %d\n", (int)sizeof(b)); //1 printf("b = %d, a = %d\n", b, a); //0,0 b = 3; a = b; printf("b = %d, a = %d\n", b, a); //1,1 b = -5; a = b; printf("b = %d, a = %d\n", b, a); //1,1 a = 10; b = a; printf("a = %d, b = %d\n", a, b); //10,1 a = 0; b = a; printf("a = %d, b = %d\n", a, b); //0,0 return0; }
fengyun@ubuntu:~/share$ gcc test.c -o test test.c: In function ‘main’: test.c:5:5: error: unknown type name ‘bool’ 5 | bool b = false; | ^~~~ test.c:5:14: error: ‘false’ undeclared (first use in this function); did you mean ‘fclose’? 5 | bool b = false; | ^~~~~ | fclose test.c:5:14: note: each undeclared identifier is reported only once for each function it appears in
fengyun@ubuntu:~/share$ g++ test.c -o test fengyun@ubuntu:~/share$ ./test sizeof(b) = 1 b = 0, a = 0 b = 1, a = 1 b = 1, a = 1 a = 10, b = 1 a = 0, b = 0
布尔类型是C+ +中的基本数据类型
可以定义bool类型的全局变量
可以定义bool类型的常量
可以定义bool类型的指针
可以定义bool类型的数组
C++三目预算符
1 2 3 4 5 6 7 8 9
#include<stdio.h> intmain(int argc,char *argv[]) { int a = 1; int b = 2; (a < b ? a : b) = 3; printf("a = %d, b = %d\n",a, b); return0; }
fengyun@ubuntu:~/share$ gcc test.c -o test test.c: In function ‘main’: test.c:6:21: error: lvalue required as left operand of assignment 6 | (a < b ? a : b) = 3; | ^
fengyun@ubuntu:~/share$ g++ test.c -o test fengyun@ubuntu:~/share$