Very classic interesting C language questions

Source: Internet
Author: User

Http://stevenkobes.com/ctest.html

I found a set of interesting C language test questions on this website. If you are recruiting C language developers or are learning C language, it is worth doing it.

If not, do not read the following content for the time being. You 'd better finish it first.

OK. If the answer you provided is not completely correct, you can continue reading it. Otherwise, the following content is a piece of cake for you and is not worth reading.

Question 1:

# Include <setjmp. h>
Static jmp_buf buf;
Int main (void)
{
Volatile int B = 3;
If (setjmp (buf )! = 0)
{
Printf ("% d \ n", B );
Exit (0 );
}
B = 5;
Longjmp (buf, 1 );
}

The output result is A) 3 B) 5 C) 0 D) None

The answer is B, that is, output 5.

The key point is to understand setjmp and longjmp, (http://en.wikipedia.org/wiki/Setjmp.h) The first time you run to setjmp, jmp_buf is set and then 0 is returned. When longjmp is called, the non-zero value in longjmp is returned as the return value of setjmp (if the value parameter of longjmp is 0, 1 is returned after setjmp is restored, that is, when the setjmp storage point is restored, setjmp will not return 0 ).

The setjmp-longjmp combination is similar to the disk reading function in the game and is often used for exception recovery operations similar to C ++.

Question 2:

Struct node
{
Int a; int B; int c;
};
Struct node s = {3, 5, 6 };
Struct node * pt = & s;
Printf ("% d \ n", * (int *) pt );
The returned result is 3, which is relatively simple. pt is the pointer pointing to the structure s, converts pt to the int pointer, performs dereference, and retrieves an int value, that is the first number in the structure.

Modify the following code:

Copy codeCode: struct node
{
Char a; char B; short c; int d;
};
Struct node s = {3, 5, 6, 99 };
Struct node * pt = & s;
Printf ("% X \ n", * (int *) pt );

Note that the 32-bit C compiler generally regards char as 8bit, short as 16bit, and int as 32bit, so the node should be exactly aligned in the memory, that is, there is no gap between the members of abc. The final result should be 60503. If not, please tell me the specific compiling environment and hardware configuration.

Question 3:

Copy codeThe Code is as follows: int foo (int x, int n ){
Int val = 1;
If (n> 0)
{
If (n % 2 = 1) val * = x;
Val * = foo (x * x, n/2 );
}
Return val;
}

In fact, the simplest way to solve this problem is to make a deduction calculation on the paper. You can get the answer by running it one step at a time. There is no complicated C language concept.

Question 4:

Copy codeThe Code is as follows: int a [5] = {1, 2, 3, 4, 5 };
Int * ptr = (int *) (& a + 1 );
Printf ("% d \ n", * (a + 1), * (ptr-1 ));

This question is actually a pointer to an array. & a is an implicit pointer to an int [5] array. It is different from int * ptr, if you really want to define this pointer, it should be int (* ptoa) [5]. Therefore, every one-plus operation of ptoa is equivalent to a memory step that spans int a [5] (that is, five int lengths ), that is to say, & a + 1 actually points to the location a [5]. In fact, this location in the memory is invalid, however, the force conversion of ptr causes the memory step of the ptr-1 to be changed to 1 int length, so the ptr-1 actually points to a [4]. As for * (a + 1), the value is 2.

Question 5:

Copy codeThe Code is as follows: void foo (int [] [3]);
Int main (void)
{
Int a [3] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }};
Foo ();
Printf ("% d \ n", a [2] [1]);
Return 0;
}
Void foo (int B [] [3])
{
+ + B;
B [1] [1] = 9;
}

In fact, it is similar to the previous question. The step size of ++ B is actually three int values, that is, after ++ B operations, B points to the beginning of the array {, 6, B [1] is {7, 8, 9}, B [1] [1] is actually 8, which is a [2] [1] In the main function.

Question 6:

Copy codeThe Code is as follows: int a, B, c, d;
A = 3;
B = 5;
C = a, B;
D = (a, B );
Printf ("c = % d", c );
Printf ("d = % d \ n", d );

There are actually two knowledge points in C language. One is that the equal sign operator has a higher priority than the comma operator, the other is that the comma operator is equivalent to the expression in the first half and the last half of the comma, and then returns the value of the latter half of the expression. Therefore, c is equal to a (equal sign is calculated first), and d is equal to B (the comma expression returns B ).

Question 7:

Copy codeThe Code is as follows: int a [] [3] = {1, 2, 3, 4, 5, 6 };
Int (* ptr) [3] =;
Printf ("% d", (* ptr) [1], (* ptr) [2]);
++ Ptr;
Printf ("% d \ n", (* ptr) [1], (* ptr) [2]);

Still a question related to a 2-dimensional array. ptr is a pointer to an int [3] array. It first points to a [0], so (* ptr) [1], (* ptr) [2] is a [0] [1], a [0] [2]. then ++ ptr is equivalent to ptr pointing to a [1]. In this case, a [1] [1], a [1] [2] are obtained, so the result is 2, 3, 5, 6.

Question 8:

Copy codeCode: int * f1 (void)
{
Int x = 10; return & x;
}
Int * f2 (void)
{
Int * ptr; * ptr = 10; return ptr;
}
Int * f3 (void)
{
Int * ptr; ptr = malloc (sizeof * ptr); return ptr;
}

Here we take a test to return a pointer. In general, the function that returns the pointer must have memory application operations such as malloc. When the pointer type is passed in, it is used to modify the content pointed to by the pointer. If you want to modify the pointer itself, you need to input the pointer.

Question 9:

Copy codeThe Code is as follows: int I = 3; int j;
J = sizeof (++ I );
Printf ("I = % d j = % d \ n", I, j );

The content of this question is actually sizeof, I mentioned in this Article http://sunxiunan.com /? P = 1637 sizeof if the expression is calculated, the expression will not be calculated, that is, no matter the addition or subtraction, sizeof is the size of the I calculation. On 32-bit machines, this j should be 4.

I expanded the code to see if you can think of the result:

Copy codeThe Code is as follows: short m; int n; double dn;
Int j = sizeof (m + n );
Int k = sizeof (n + n );
Int l = sizeof (m );
Int l2 = sizeof (m * m );
Int l3 = sizeof (m + dn );
Int l4 = sizeof (m + m );

Question 10:

Copy codeThe Code is as follows: void f1 (int *, int );
Void (* p [2]) (int *, int );
Int main (void)
{
Int a = 3;
Int B = 5;
P [0] = f1;
P [1] = f1;
P [0] (& a, B );
Printf ("% d", a, B );
P [1] (& a, B );
Printf ("% d \ n", a, B );
Return 0;
}
Void f1 (int * p, int q)
{
Int tmp = * p; * p = q; q = tmp;
}

The array p of the function pointer is barely a knowledge point. Another knowledge point is mentioned in question 8. parameters such as int q will not be modified. * P can modify the content pointed to by p.

Question 11th:

Copy codeThe Code is as follows: void e (int );
Int main (void)
{
Int a = 3;
E ();
Putchar ('\ n ');
Return 0;
}
Void e (int n)
{
If (n> 0)
{
E (-n );
Printf ("% d", n );
E (-n );
}
}

Debug this question to understand it completely. The main knowledge point is recursive call. In addition, the return value of the pre-and post-auto-subtraction operation is a problem.

Question 12th:

Copy codeThe Code is as follows: typedef int (* test) (float *, float *);
Test tmp;

It is also a frequently-encountered problem. To resolve complicated pointer definitions, in fact, K & R (5.12) also describes how to interpret them. Unfamiliar users can try to practice bsearch, qsort, and signal functions in the standard library.

Question 13th:

Copy codeThe Code is as follows: char p;
Char buf [10] = {1, 2, 3, 4, 5, 6, 9, 8 };
P = (buf + 1) [5];
Printf ("% d \ n", p );

I am in http://sunxiunan.com /? P = 1637 also mentioned related knowledge points, that is, p actually points to * (buf + 1 + 5). The more strange thing to write is p = 5 [buf + 1]; the same result is returned.

Question 14th:

Similar to the 13th question, the array is also a little strange. (p + = sizeof (int) [-1]; equivalent to * (p + sizeof (int) + (-1 )).

Question 15th:

Copy codeThe Code is as follows: int ripple (int n ,...)
{
Int I, j, k;
Va_list p;
K = 0;
J = 1;
Va_start (p, n );
For (; j <n; ++ j)
{
I = va_arg (p, int );
For (; I & = I-1)
++ K;
}
Return k;
}
Int main (void)
{
Printf ("% d \ n", ripple (3, 5, 7 ));
Return 0;
}

This question is also two knowledge points. One is Variable Parameter Function Definition and how to implement it. va_arg will extract 5, 7 in sequence. Another knowledge point is I & = I-1, in fact is to calculate the number of 1 in the I binary form, each calculation will remove the minimum effective bit 1. For example, 7 is represented as 111 in binary format. The result of I & = I-1 is 110,100,000 (that is, 0) in sequence ). This book introduces many similar techniques in hacker's Delights.

Question 16th:

Copy codeThe Code is as follows: int counter (int I)
{
Static int count = 0;
Count = count + I;
Return count;
}
Int main (void)
{
Int I, j;
For (I = 0; I <= 5; I ++) j = counter (I );
Printf ("% d \ n", j );
Return 0;
}

As long as you understand the true meaning of static local variables, this question is a single disc, a single disc ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.