常规逻辑量 常规逻辑量包括: 1.逻辑常量 1和0 2.逻辑变量 存储逻辑常量的变量。 3.结果是逻辑值的表达式 关系表达式、逻辑表达式等
常规逻辑量 常规逻辑量包括: 1.逻辑常量 1和0 2.逻辑变量 存储逻辑常量的变量。 3.结果是逻辑值的表达式 关系表达式、逻辑表达式等
扩展逻辑量 C语言为了提高编程的灵活性,大大地扩展了 逻辑量的范畴。 允许任意整型、实型、字符型、枚举型和指针 型的数据(或表达式)参与逻辑运算。 不妨将这些类型的数据(或表达式)称为扩展 逻辑量。 同时规定,只要扩展逻辑量的值为非日,就看 作“真”;只要扩展逻辑量的值为,就看 作“假
扩展逻辑量 C语言为了提高编程的灵活性,大大地扩展了 逻辑量的范畴。 允许任意整型、实型、字符型、枚举型和指针 型的数据(或表达式)参与逻辑运算。 不妨将这些类型的数据(或表达式)称为扩展 逻辑量。 同时规定,只要扩展逻辑量的值为非0,就看 作“真”;只要扩展逻辑量的值为0,就看 作“假
例1.输出逻辑表达式的值。 #include <stdio.h> int main(void) fint a=4; float b=5.0; printf("%d小n",la); printf("‰d小n",'a'+'b'||'c'); printf("%d\n",a&&'0')3 printf("%d\n",b&&'\0'); return 0;
例1.输出逻辑表达式的值。 #include <stdio.h> int main(void) {int a=4; float b=5.0; printf("%d\n",!a); printf("%d\n",'a'+'b'||'c'); printf("%d\n",a&&'0'); printf("%d\n",b&&'\0'); return 0; }
#include <stdio.h> 说明: int main(void) 1.该程序中的a、b为变量名, {int a=4; 而'a'、'b'、''、'g'、 float b=5.0; \日'均为字符常量。 printf("%d\n",!a); 2.字符常量中只有\0'是0, printf("%d\n",'a'+'b'll'c'); 因为它的ASCII码是0;其 printf("%d\n",a&&'0'); 它字符都是非0。 printf("%d\n",b&&'\0'); return 0; 3.'日'也是非0,因为它的 ASCII码是48
#include <stdio.h> int main(void) {int a=4; float b=5.0; printf("%d\n",!a); printf("%d\n",'a'+'b'||'c'); printf("%d\n",a&&'0'); printf("%d\n",b&&'\0'); return 0; } 说明: 1.该程序中的a、b为变量名, 而'a'、'b'、'c'、'0'、 '\0'均为字符常量。 2.字符常量中只有'\0'是0, 因为它的ASCII码是0;其 它字符都是非0。 3.'0'也是非0,因为它的 ASCII码是48
#include <stdio.h> 程序分析: int main(void) 1.变量a的值为非0,看作真, {int a=4; 故表达式!a的结果为日。 float b=5.0; 2.在表达式a'+b'|川'c'中, printf("%d\n",!a); 先求'a'+'b'(ASCII码相 printf("%d\n",'a'+'b'll'c')3 加),结果为195非0,看作 printf("%d\n",a&&'0'); 真;'c'也为非0,看作真; printf("%d\n",b&&'\'); 再求真真,结果为1。 return 0; 3.在表达式a&'0'中,变量a的 值为非0,看作真;'0'也为 非0,看作真;再求真&真, 结果为1。 4.在表达式b&&\日'中,变量b 的值为非0,看作真;八0 为日,看作假;再求真&&假, 结果为0
#include <stdio.h> int main(void) {int a=4; float b=5.0; printf("%d\n",!a); printf("%d\n",'a'+'b'||'c'); printf("%d\n",a&&'0'); printf("%d\n",b&&'\0'); return 0; } 程序分析: 1.变量a的值为非0,看作真, 故表达式!a的结果为0。 2.在表达式'a'+'b'||'c'中, 先求'a'+'b'(ASCII码相 加),结果为195非0,看作 真;'c'也为非0,看作真; 再求真||真,结果为1。 3.在表达式a&&'0'中,变量a的 值为非0,看作真;'0'也为 非0,看作真;再求真&&真, 结果为1。 4.在表达式b&&'\0'中,变量b 的值为非0,看作真;'\0' 为0,看作假;再求真&&假, 结果为0