Conventions:
1) in the following test questions, all the necessary header files have been correctly included
2) Data Type
Char 1 byte
Int two bytes 2 bytes (16-bit system, think integer is 2 bytes)
Long int, four bytes, 4 bytes
Float four bytes 4 byet
Double, eight bytes, 8 bytes
Long double 10 bytes 10 bytes
Pointer two bytes 2 byte (note that the address bus is only 16 bits)
Question 1st: measure the test taker's knowledge about the volatile keyword.
# Include <setjmp. h>
Static jmp_buf Buf;
Main ()
{
VolatileInt B;
B = 3;
If (setjmp (BUF )! = 0)
{
Printf ("% d", B );
Exit (0 );
}
B = 5;
Longjmp (BUF, 1 );
}
Excuse me, this sectionProgramThe output is
(A) 3
(B) 5
(C) 0
(D) None of the above
Question 2nd: Test type conversion
Main ()
{
Struct Node
{
Int;
Int B;
Int C;
};
Struct node S = {3, 5, 6 };
Struct node * PT = & S;
Printf ("% d", * (int *) pt );
}
The output of this program is:
(A) 3
(B) 5
(C) 6
(D) 7
3rd question: Examine recursive calls
Int Foo (int x, int N)
{
Int val;
Val = 1;
If (n> 0)
{
If (N % 2 = 1) val = Val * X;
Val = Val * Foo (x * X, n/2 );
}
Return val;
}
This sectionCodeWhat are the functions (Operations) for X and N )?
(A) x ^ n(N power of X)
(B) x * n (the product of x and N)
(C) n ^ x (x of N)Power)
(D) None of the above
Question 4th: Examine pointers. This question is only suitable for those who are very careful and have a deep understanding of pointers and arrays.
Main ()
{
Int A [5] = {1, 2, 3, 4, 5 };
Int * PTR = (int *) (& A + 1 );
Printf ("% d", * (a + 1), * (ptr-1 ));
}
The output of this program is:
(A) 2 2
(B) 2 1
(C) 2 5
(D) None of the above
Question 5th: multi-dimensional arrays and pointers
Void Foo (INT [] [3]);
Main ()
{
Int A [3] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }};
Foo ();
Printf ("% d", a [2] [1]);
}
Void Foo (int B [] [3])
{
+ + B;
B [1] [1] = 9;
}
The output of this program is:
(A) 8
(B) 9
(C) 7
(D) None of the above
6th question: comma-separated expressions
Main ()
{
Int A, B, C, D;
A = 3;
B = 5;
C = A, B;
D = (A, B );
Printf ("c = % d", C );
Printf ("d = % d", d );
}
The output of this program is:
(A) C = 3 D = 3
(B) C = 5 d = 3
(C) C = 3 D = 5
(D) C = 5 d = 5
Question 7th: pointer Array
Main ()
{
Int A [] [3] = {1, 2, 3, 4, 5, 6 };
INT (* PTR) [3] =;
Printf ("% d", (* PTR) [1], (* PTR) [2]);
++ PTR;
Printf ("% d", (* PTR) [1], (* PTR) [2]);
}
The output of this program is:
(A) 2 3 5 6
(B) 2 3 4 5
(C) 4 5 0 0
(D) None of the above
Question 8th: measure the test taker's knowledge about function pointers.
Int * F1 (void)
{
Int x = 10;
Return (& X );
}
Int * F2 (void)
{
Int * PTR;
* PTR = 10;
Return PTR;
}
Int * F3 (void)
{
Int * PTR;
PTR = (int *) malloc (sizeof (INT ));
Return PTR;
}
Which of the above three functions is most likely to cause pointer problems?
(A) Only F3
(B) Only F1 and F3
(C) Only F1 and F2
(D) F1, F2, F3
9th question: test self-added operations (++)
Main ()
{
Int I = 3;
Int J;
J = sizeof (++ I );
Printf ("I = % d J = % d", I, j );
}
The output of this program is:
(A) I = 4 J = 2
(B) I = 3 J = 2
(C) I = 3 J = 4
(D) I = 3 J = 6
Question 10th: test form parameters, actual parameters, pointers, and Arrays
Void F1 (int *, INT );
Void F2 (int *, INT );
Void (* P [2]) (int *, INT );
Main ()
{
Int;
Int B;
P [0] = F1;
P [1] = F2;
A = 3;
B = 5;
P [0] (& A, B );
Printf ("% d/T", a, B );
P [1] (& A, B );
Printf ("% d/T", a, B );
}
Void F1 (int * P, int q)
{
Int TMP;
TMP = * P;
* P = Q;
Q = TMP;
}
Void F2 (int * P, int q)
{
Int TMP;
TMP = * P;
* P = Q;
Q = TMP;
}
The output of this program is:
(A) 5 5 5 5
(B) 3 5 3 5
(C) 5 3 5 3
(D) 3 3 3 3
11th question: test the auto-increment operation (--)
Void E (INT );
Main ()
{
Int;
A = 3;
E ();
}
Void E (int n)
{
If (n> 0)
{
E (-- N );
Printf ("% d", N );
E (-- N );
}
}
The output of this program is:
(A) 0 1 2 0
(B) 0 1 2 1
(C) 1 2 0 1
(D) 0 2 1 1
12th question: test the typedef type definition and function pointer
Typedef int (* test) (float *, float *)
Test TMP;
The TMP type is
(A) pointer to the function. This function uses two pointers pointing to float as parameters (arguments)
Pointer to function of having two arguments that is pointer to float
(B) Integer
(C) pointer to a function. The function uses two pointers pointing to float as parameters (arguments) and the return value type of the function is integer.
Pointer to function having two argument that is pointer to float and return int
(D) None of the above
Question 13th: Differences and relationships between arrays and pointers
Main ()
{
Char P;
Char Buf [10] = {1, 2, 4, 5, 6, 9, 8 };
P = (BUF + 1) [5];
Printf ("% d", P );
}
The output of this program is:
(A) 5
(B) 6
(C) 9
(D) None of the above
Question 14th: Examine the pointer Array
Void F (char **);
Main ()
{
Char * argv [] = {"AB", "cd", "Ef", "GH", "IJ", "Kl "};
F (argv );
}
Void F (char ** P)
{
Char * t;
T = (p + = sizeof (INT) [-1];
Printf ("% s", t );
}
The output of this program is:
(A) AB
(B) CD
(C) EF
(D) GH
Question 15th: This question examines the variable length parameter of C. Just like printf () in the standard function library, this topic is generally not discussed in Chinese university classes, and it will not be excitable,
# Include <stdarg. h>
Int ripple (INT ,...);
Main ()
{
Int num;
Num = ripple (3, 5, 7 );
Printf ("% d", num );
}
Int ripple (int n ,...)
{
Int I, J;
Int K;
Va_list P;
K = 0;
J = 1;
Va_start (p, N );
For (; j <n; ++ J)
{
I = va_arg (p, INT );
For (; I; I & = i-1)
++ K;
}
Return K;
}
The output of this program is:
(A) 7
(B) 6
(C) 5
(D) 3
Question 16th: measure the test taker's knowledge about static variables.
Int counter (int I)
{
Static int COUNT = 0;
Count = count + I;
Return (count );
}
Main ()
{
Int I, J;
For (I = 0; I <= 5; I ++)
J = counter (I );
}
The value of J is as follows:
(A) 10
(B) 15
(C) 6
(D) 7
For more information, see answers.
Question 1st: (B)
Volatile literally means volatile. When this keyword is used to describe a variable, it means that after the variable is assigned a value (written), it will be read immediately. The written value may be different from the read value, so it is "volatile.
This is because this variable may be a register that is directly connected to an external device. After you write it, the register may also be changed by the write operation of the external device; or, this variable is interrupted by one program or another process
Changed.
Volatile will not be affected by Compiler Optimization. After longjump, its value is the variable value assumed later, and B's final value is 5, so 5 is printed.
Setjmp: set non-local jump/* setjmp. H */
Stores
Context information such as register values so that the lomgjmp
Function can return control to the statement following the one calling
Setjmp. returns 0 when it is initially called.
Lonjjmp: execute a non-local jump/* setjmp. H */
Transfers
Control to the statement where the call to setjmp (which initialized
Buf) was made. execution continues at this point as if longjmp cannot
Return the value 0.a nonvolatile automation variable might be changed by
Call to longjmp. When you use setjmp and longjmp, the only automatic
Variables guaranteed to remain valid are those declared volatile.
Note: test program without volatile qualifier (result may very)
For more information, see setjmp and longjmp in C.
2nd Question: ()
The addresses of the structure questions in the memory increase in sequence according to their defined locations. If a pointer to a struct is regarded as a pointer to its first member, the pointer does point to the first member.
3rd question: ()
This question is difficult.
Non-recursive version of this program
Int what (int x, int N)
{
Int val;
Int product;
Product = 1;
Val = X;
While (n> 0)
{
If (N % 2 = 1)
Product = product * val;/* if it is an odd power, x (VAL)
Take the last ride first ,;
An even power is returned.
Multiply by 1 */
Val = Val * val;
N = n/2;
}
Return product;
}
/* Use a binary compound multiplication policy */
AlgorithmDescription
(While n> 0)
{
If next most significant binary digit of N (Power) is one
Then multiply accumulated product by current Val,
Reduce N (Power) sequence by a factor of two using integer division.
Get next Val by multiply current value of itself
}
4th question: (c)
A is an integer array with 5 members.
& A is a pointer to an integer array.
So where & A + 1 points is equivalent to a [6]
Therefore, * (a + 1) is equivalent to a [1].
PTR is equivalent to a [6], and ptr-1 is equivalent to a [5]
Question 5th: (B)
The question itself gives enough tips
B [0] [0] = 4
B [1] [0] = 7
6th question: (c)
Test the comma expression. The priority of a comma expression is very low, which is lower than that of a value (=). The value of a comma expression is the value of the last element.
Another function of the comma expression is to split the parameter list of the function ..
E1, E2,..., en
In the preceding expression, the values of E1, E2,... en are calculated separately. The structure calculated by en is assigned to the entire comma expression.
C = A, B;/* yields c = */
D = (a, B);/* D = B */
7th question: ()
PTR is an array pointer, which has three int members.
8th question: (c)
Obviously there is a problem with F1. It returns a pointer to a local variable. The local variable is saved in the stack. After exiting the function, the local variable is destroyed and its pointer remains meaningless, because the stack space it points to may be overwritten by other variables
F2 also has a problem. PTR is a local variable and has not been initialized. Its value is unknown. * PTR does not know where it points. Directly assigning a value to * PTR may overwrite important system variables, this is a type of wild pointer.
Question 9th: (B)
The sizeof operator gives the space occupied by its operations. It can be determined during compilation. Therefore, its operations, even an expression, do not need to be calculated at runtime. (++ I) is not executed, so
The I value is 3.
10th question: ()
Apparently, select.
* P and q values are exchanged in F1. After F1 is executed, * p and q values are indeed exchanged, but Q changes do not affect B changes. * P is actually
Therefore, after F1 is executed, a = B = 5
This question covers a wide range of knowledge, including typedef custom types, function pointers, and pointer arrays.
Void (* P [2]) (int *, INT );
Array p that defines a function pointer. P has two pointer elements. an element is a function pointer. The function pointer points to a function with two parameters. The Void function is returned. The two parameters are pointer to an integer and integer.
P [
0] = F1; P [1] = F2 contain address of function. Function Name
Without parenthesis represent address of function value and address
Variable is passed to function only argument that is specified TED is
(Address is passed). Because of call by value F1, F2 can not effect B
11th question: ()
Test-operation and recursive call. You can analyze it carefully.
12th question: (c)
It is not recommended to look at C expert Programming
From the left to the left, when there is a bracket, stop and think of the things in the first bracket as a whole.
13th question: (c) when is the array a pointer. for some types of T, if an expression is a T [] (T array), the value of this expression is actually a pointer to the first element of the array. therefore, (BUF + 1) [5] is actually * (BUF + 6) or Buf [6]. Question 14th: (B)
The value of sizeof (INT) is 2, so P + = sizeof (INT) points to argv [2]. We have no doubt about this.
(P + = sizeof (INT) [-1] points to argv [1]. Can you understand it, because (p + = sizeof (INT )) [-1] is equivalent to (p + = 2) [-1], that is, (p + 2-1)
In question: (c)
The C compiler generally provides a series of macro processing variable parameters to block the differences caused by different hardware platforms, increase program portability. These macros include va_start, va_arg, and va_end.
When the ANSI standard form is used, the prototype declaration of a function with a variable number of parameters is:
Type fun( type para1, type para2 ,...)
This form requires at least one common form parameter, and the ellipsis behind it does not mean to be omitted, but is part of the function prototype. Type is the type of function return values and formal parameters. Different compilers have different implementations for this variable length parameter. gcc4.x contains built-in functions. For details about variable length parameters, refer to [url] callback.
Program Analysis
Va_list P;/* define a variable and save the pointer to the function parameter list */
Va_start (p, n);/* use the va_start macro to initialize the Variable P,
The 2nd parameter n of the va_start macro,
Is a fixed parameter,
It must be the last input stack parameter of the variable-length function we have defined.
That is, the 1st parameters in the parameter list during the call */
For (; j <n; ++ J)/* j traverses all variable parameters starting from 1 */
{
I = va_arg (p, INT);/* va_arg retrieves the current parameter,
The retrieved parameter is an integer (INT )*/
For (; I; I & = i-1)/* determine whether the retrieved I is 0 */
+ + K;/* If I is not 0, K auto-increment,
I and I-1 and logical operation until I is 0
This is a technique. The following describes its functions */
}
When we call the ripple function, the first parameter n in the parameter list passed to the ripple function is 3. va_start initialize P. The morale points to the first unnamed parameter (N is a parameter with a name), that is, is 5 (the first parameter ). each call to va_arg returns a parameter and Points P to the next parameter. va_arg uses a type name to determine the type of the returned parameter, and in the internal implementation of var_arg, the distance to the next parameter is determined.
(; I; I & = i-1) K ++/* calculate how many bits I have been set to 1 */
5. In binary format, it is (101) 2.
7 In binary format (111) 3
Therefore, K returns 5 (2 + 3), that is, C should be selected.
For example, it is easy to understand.
Make I = 9 = 1001
I-1 = 1000
(I-1) + 1 = I
1000
+ 1
1 001
Because I and the I-1's rightmost (DIGIT) must be different, if i1, The I-1 must be 0, and vice versa. I & I-1 this operation, in the binary complement digital system, will eliminate the rightmost 1 bit 16th question: (B)
The answer is (B)
It is said that Gauss will do this sort of proportional series during the first grade of elementary school. this question examines the knowledge of static variables. After each function is called, the value of static variables will not be lost, which is significantly different from the temporary local variables in the stack.
Therefore, after counter (0) is called for the first time, Count = 0
Count = 0 + 1 after the second counter (1) call;
The third counter (2) Count = 1 + 2;/* COUNT = count + I */
The fourth counter (3) Count = 3 + 3;
The fifth counter (4) Count = 6 + 4 call;
The sixth counter (5) Count = 10 + 5;
Ashok K. Pathak a member (research staff) at Bharat Electronics
Limited (CRL), Ghaziabad. He has been developing embedded application
For the past five years. Ashok holds a M. E in Computer Science and
Engineering. Ashok recently completed a book about '"advanced test in C
And embedded system programming ", published by BPB, nd.
This article is from 51cto. com technical blog