1.
1 # Define Pi 3.14
2 # Define Area (r) pI * r * R
3 Main ()
4 {
5 Int R1 = 5 , R2 = 2 ;
6 Double S = 0 ;
7 S = Area (r1-r2 );
8 Printf ( " The area is % F " , S );
9 }
10
11 # Define Pi 3.14
12 # Define Area (r) pI * r * R
13 Main ()
14 {
15 Int R1 = 5 , R2 = 2 ;
16 Double S = 0 ;
17 S = Area (r1-r2 );
18 Printf ( " The area is % F " , S );
19 }
Analysis: this topic mainly examines the understanding and application of macro definitions. For the compiler, macros are directly replaced without any other action. In this question, the area (r) pI * r * R, and the R value is r1-r2, so the result after pre-compilation is:
PI * r1-r2 * r1-r2, calculated as 3.700000
The other is floating-point calculation. The output result is of Double Precision type, and the float type has 6 digits. Therefore, the result is 3.700000. If the float type is of long double type, the output result is :? It is strange that, in my test on Ubuntu, no matter the type is changed to float, double, long double, the output results are the same. Why? To be verified. In addition, printf ("float: % d, double: % d, long double: % d \ n", sizeof (float), sizeof (double ), sizeof (Long Double); the output result is 4, 8, 12. the results should be different.
2.FunctionInt
Compare (int A, int B ),Function pointer defined as this functionP:
INT (* p) (int A, int B );
P = compare;
This is simple and will not be explained;
3.
1 # Include <stdio. h>
2 Void Sub ( Char * S, Int Num)
3 {
4 Int I, j = num;
5 Char T;
6 While (J --> 1 )
7 {
8 For (I = 0 ; I <j; I ++)
9 {
10 If (S [I] <s [I + 1 ])
11 {
12 T = s [I];
13 S [I] = s [I + 1 ];
14 S [I + 1 ] = T;
15 }
16 }
17 }
18 }
19 Main ()
20 {
21 Char * S = " Ceaeded " ;
22 Sub (S, 7 );
23 Printf ( " % S \ n " , S );
24
25 Return ;
26 }
This question is very confusing, and the basic knowledge is not solid, so it is easy to get wrong results. At first glance, it is easy to think of as sorting.ProgramTo get "eeddeca". But the actual running program will get the segment Falt error. This is because S is a pointer pointing to a character constant, which cannot be modified. If it is defined as char s [] = "ceaeded", it can run normally. Why?
4. Exchange the values of two variables , Do not use the third variable , That is A = 3, B = 5, Exchange After B = 3, A = 5.
The answer is classic, but I cannot do it.
1UnsignedCharA =3, B =5;
2A = A + B;
3B = A-B;
4A = A-B;
5
6Or
7A = a ^ B;
8B = a ^ B;
9A = a ^ B;
5. Considerations for memory allocation:
1 # Define N 100
2 Void Getmemory1 ( Char * P)
3 {
4 P = ( Char *) Malloc ( Sizeof ( Char ) * N );
5 Strcpy (p, " Have a good day! " );
6 }
7
8 Char * Getmemory2 ( Void )
9 {
10 Char P [] = " Have a good day! " ;
11 Return P;
12 }
13
14 Void Main ( Void )
15 {
16 Char * Str1 = NULL, * str2 = NULL;
17 Getmemory1 (str1 );
18 Getmemory2 (str2 );
19 Printf ( " \ Nstr1: % s " , Str1 );
20 Printf ( " \ Nstr2: % s " , Str2 );
21 }
Result: str1: NULL, str2: garbled.
Why is str1 null? Is the memory allocated by malloc automatically released after the function is executed? No. In the main function, str1 is a local variable and cannot pass its pointer to getmemory1 ().
The C language parameter passing follows the value passing rule, and the pointer variable is the same.The getmemory1 () parameter is the str1 variable, not the address of str1.Getmemory1 ()Operations on P have no effect on str1.
Str2 returns garbled characters because P is a local variable. After the function is executed, P points to an unknown place, so garbled characters are displayed.
For more information, see <deep anatomy in C Language> Level 1 pointer parameter;