Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Analysis of Several Difficulties in C Language Basis
Analysis of Several Difficulties in C Language Basis
First, the range of integer data

Different types of data occupy different storage unit lengths in memory. Take the compilation environment of VC++6.0 as an example, basic integer (int) data is allocated 4 bytes and short integer (short int) data is allocated 2 bytes. Let's take short int as an example. 1 byte is 8 binary bits, and each short integer data is allocated 2 bytes, namely 16 bits. For signed short integers (default), the leftmost bit is the sign bit, 0 means positive, and 1 means negative. The maximum quantity that can be stored is 0111111165438. The minimum number is1000000000000, which is converted into decimal -32768. Beyond this range, it is "overflow". For example:

short int a,b,c,d; a = 32767b = a+ 1; c =-32768; d = b- 1;

printf("b=%d\t ",b); printf("d=%d\n ",d); The output result is b=-32768 d=32767.

For unsigned short integers, the minimum value is 00000000000000, and the maximum value is111111. For example:

Unsigned short integer e=65535, f; f = e+ 1;

printf("f=%d\n ",f); The output result is f=0.

Second, the operator

Arithmetic operators are+,-,*,/,%,++,-. Among them, the * sign in the expression cannot be omitted, such as b2-4ac. The expression is written as b * b-4 * a * c ./ and% should be distinguished,/is the quotient, and% is the remainder. If a is divisible by b, it means that a%b==0. There are two ways to use++.One is i++ and I-. First, use the original value of I, and then add or subtract1with the value of I; The second ++i, -I first adds or subtracts 1 from the value of i, and then uses the new value of i. For example:

int I = 1 1; printf("%d\n ",i++ * 1/3); The output result is 3.

int I = 1 1; printf("%d\n ",++ I * 1/3); The output result is 4.

Self-addition and self-subtraction operators will be often used in later learning, so you should understand them well.

The logical operator is&; & amp,||,! . In a logical expression, not all logical operators are executed, but only when the next logical operator must be executed can the solution of the expression be found. A & amp& ampb only judges the value of B when A is true. In other words, if A is false, B will not execute it. Similarly, if a || b and A are true, then B does not need to execute. For example:

int m= 1,n= 1,a= 1,b = 1;

Printf ("expression =% d \ t ",(m = a>b) and amp & amp& amp(n = a & gtb));); printf("m=%d,n=%d\n ",m,n);

The output result is the expression =0 m=0, n= 1.

The priority order of common operators is:! , arithmetic operator (++,-higher than *,/,% higher than+,-), relational operator (>; ,>=,<,<= higher than = =,! =), & amp& amp|||, assignment operator, comma operator.

Third, compare the size of the numbers.

For example, write a program to output the maximum value of three integers. Most students who are new to C language can complete this program in computer practice, but their thinking is not very good. Adjust the algorithm ideas to facilitate future programming.

Master ()

{ int a,b,c,maxmax = a;

If (b & gtmax)max = b;; if(c & gt; max)max = c; printf("max=%d\n ",max); }

Similarly, write a program to output the maximum value of 10 integer.

Master ()

{ int a[ 10],max,I; for(I = 0; I < = 9; i++) scanf("%d ",& ampa[I]);

max = a[0]; for(I = 1; I < = 9; i++)if(a[I]& gt; max)max = a[I];

printf("max=%d ",max); }

Fourth, the switch statement

The processing selection structure includes if statement and switch statement. Switch statement is used to deal with multi-branch selection structure, and there are many points for attention when using it. Two points are emphasized here.

Constants after 1.case cannot be written as expressions.

For example, write a program to judge the grade of students' grades. 90 ~ 100 is a, 80 ~ 90 is b, 70 ~ 80 is c, 60 ~ 70 is d, and below 60 is e.