Ten, write a program that contains a logical expression.
1#include <stdio.h>2 3 voidMainvoid)4 {5 intx;6 7x =1;8printf"%d", x>=0&& x<=2);9 Tenx =5; Oneprintf"%d", x>=0&& x<=2); Aprintf"%d", x<-3|| X>3); - -x =0; theprintf"%d", x<-3|| X>3); -printf"%d", !x); - -x =5; +printf"%d", !x); -printf"%d",3&&'A'); +printf"%d", (x=2) ||0); Aprintf"x =%d\n", x); at}
Results:
1 0 1 0 1 0 1 1 x=2
"&&" for Logic and, "| |" For logic or, "!" is not logical.
(1) When the relationship expression evaluates to True (FALSE), the value of the relational expression is 1 (0), such as (5>3) +7 with a value of 8 (1+7).
(2) 0<=x<=2 forever, that is true, because first judge 0<=x, the result is 0 or 1, and 0<=2 (1<=2) judgment result is "true".
Xi. Write a program that contains a special logical expression.
1#include <stdio.h>2 3 voidMainvoid)4 {5 intA, B;6 7A =1;8printf"%d",0&& (a=2));//did not execute a=29printf"a=%d", a);Tenprintf"%d",5&& (a=2));//executed the a=2 Oneprintf"a=%d", a); A -b=1; -printf"%d",5|| (b=2));//did not execute b=2 theprintf"b=%d", b); -printf"%d",0|| (b=2));//executed the b=2 -printf"b=%d", b); -}
Results:
0 a=1 1 a=2 1 b=1 1 b=2
12, enter a character, if it is a numeric character, then converted to the corresponding number.
1#include <stdio.h>2 3 voidMainvoid)4 {5 Charch;6 intA =-1;7 8printf"Input Ch:");9CH =GetChar ();Ten if(Ch >='0'&& CH <='9') OneA = CH-'0'; Aprintf"ch=%c, a=%d\n", CH, a); -}
Results:
1.Input Ch:2
ch=2, a=2
2.Input ch:x
Ch=x, A=-1
Note: "ch-' 0 '" is the "ch" character corresponding to the decimal minus 48 (' 0 ' corresponding to the decimal 48)
ASCII number of lowercase letters (decimal): 97~122 (A~z)
ASCII number of uppercase letters (decimal): 65~90 (A~z)
ASCII number of numeric characters (decimal): 48~57 (0~9)
C Language Growth learning problem (iii)