Very classic C language interesting topic _c language

Source: Internet
Author: User
Tags function definition

Http://stevenkobes.com/ctest.html

Find a very interesting C language test in this website, if you recruit C language related developer, or are learning C language, it is worth doing.

If you do not, the following content for the time being do not look, it is best to finish it first.

OK, suppose you do not completely correct the answer, then you can continue to watch, otherwise, the back content for you is a piece of cake, not worth watching.

First question:

#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 is a) 3 B) 5 C) 0 D) is not

The answer is B, which 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, you set up jmp_buf, and then you return 0. When longjmp is invoked, the value of longjmp inside is returned as setjmp return value (if longjmp value parameter returns 1 after 0,SETJMP recovery, which means that setjmp will not return 0 when restoring to the setjmp storage point).

The use of 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 + +.

Second question:

struct node
{
int A; int b; int C;
};
struct node s = {3, 5, 6};
struct node *pt = &s;
printf ("%d\n", * (int*) PT);
The result is 3, which is relatively simple, the PT is a pointer to the structure s, then the PT is converted to an int pointer, the dereference is taken, and an int value is removed, which is the first number in the structure.

We will change the topic, the following code

Copy Code code as follows:

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 general 32-bit C compiler believes that Char is 8bit,short is 16bit,int 32bit, so node in memory should be exactly aligned, that is, there is no gap between the ABC members. The final result should be 60503, if not, you are welcome to tell me your specific compilation environment and hardware configuration.

Third question:

Copy Code code 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 is to do a deduction on paper, step by step, you can get the answer, there is no complex C language concept.

Question Fourth:

Copy Code code as follows:

int a[5] = {1, 2, 3, 4, 5};
int *ptr = (int*) (&a + 1);
printf ("%d%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 not the same as int* ptr, and if you really want to define this pointer, it should be int (*PTOA) [5]. So ptoa each addition is equal to the memory step across int a[5] (that is, 5 int length), which means that &a + 1 actually points to the a[5 position, which is actually illegal in the memory. However, the cast of PTR has resulted in the memory step of the back ptr-1 changed to 1 int length, so ptr-1 actually points to a[4]. As for * (a+1) There is nothing to say, the value is 2.

Question Fifth:

Copy Code code as follows:

void foo (int[][3]);
int main (void)
{
int a[3][3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Foo (a);
printf ("%d\n", a[2][1]);
return 0;
}
void foo (int b[][3])
{
++b;
B[1][1] = 9;
}


In fact, the same as the previous one, the ++b step is actually 3 int, that is, after ++b operation, B points to {4,5,6} The beginning of the array, and B[1] is {7,8,9}, b[1][1] is actually 8 this value is the main function in the a[2][1.

Question sixth:

Copy Code code 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);


This actually has two C language knowledge points, one is equal operator precedence is higher than the comma operator, the other is the comma operator is equivalent to the first half of the operation comma expression, and then return the second half of the expression value. So C equals a (the equal sign first), and D equals B (the comma expression returns B).

Question seventh:

Copy Code code as follows:

int a[][3] = {1, 2, 3, 4, 5, 6};
int (*PTR) [3] = A;
printf ("%d%d", (*PTR) [1], (*PTR) [2]);
++ptr;
printf ("%d%d\n", (*PTR) [1], (*PTR) [2]);


is still a 2-dimensional array of related topics, the PTR is a pointer to an int [3] array, pointing first to a[0], so (*PTR) [1], (*PTR) [2] is a[0][1], a[0][2]. Then ++ptr, which is equivalent to PTR pointing to a[1], is a[1. [1],a[1][2], so the result is 2, 3, 5, 6.

Question eighth:

Copy Code code as follows:

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;
}


The test here is to return a pointer to the problem, generally return the pointer function, there must be a malloc such as memory request operation, incoming pointer type, it is the pointer to the content to make changes. If you want to modify the pointer itself, pass the pointer over it.

Question Nineth:

Copy Code code as follows:

int i = 3; Int J;
j = sizeof (++i + ++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, then the expression is not to do calculations, that is, regardless of the Gaga reduction, sizeof is for I to calculate the size. On a 32-bit machine, this j should be 4.

I'll expand the code to see if you can think of the results:

Copy Code code 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 Tenth:

Copy Code code 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%d", A, b);
P[1] (&a, b);
printf ("%d%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 point of knowledge, and another point of knowledge is the eighth question, which does not modify its contents for parameters such as int Q. And *p can modify what p points to.

Question 11th:

Copy Code code as follows:

void e (int);
int main (void)
{
int a = 3;
E (a);
Putchar (' \ n ');
return 0;
}
void e (int n)
{
if (n > 0)
{
E (–n);
printf ("%d", n);
E (–n);
}
}


The problem is completely understood by the debug itself, the main knowledge point is recursive invocation, in addition to the return of the first post self-subtraction operation problem.

Question 12th:

Copy Code code as follows:

typedef int (*test) (float*, float*);
Test tmp;


is also often a kind of problem, to the complex pointer definition to do parsing, in fact k&r inside (5.12) also has introduced how to interpret. Unfamiliar friends can try to practice the bsearch,qsort and signal functions in the standard library.

Question 13th:

Copy Code code as follows:

char p;
Char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (buf + 1) [5];
printf ("%d\n", p);


This problem I also mentioned in http://sunxiunan.com/?p=1637 related knowledge points, that is, p actual point * (buf + 1 + 5), write more bizarre some is p=5[buf +1]; the same result.

Question 14th:

Similar to the 13 problem, it makes the array a bit weird, (p + + sizeof (int)) [-1]; equivalent to * (p + sizeof (int) + (-1)).

Question 15th:

Copy Code code 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 &= i–1)
++k;
}
return k;
}
int main (void)
{
printf ("%d\n", Ripple (3, 5, 7));
return 0;
}


This problem is also two points of knowledge, one is the variable parameter function definition and how to achieve, Va_arg will be 5, 7 in turn out. Another knowledge point is I &= i-1, which actually calculates the number of 1 in the I binary form, and every calculation cuts off 1 of the least significant digits. For example, the 72 binary representation is 111. The results of I &= i–1 are 110,100, 000 (i.e. 0). Many of the similar techniques are presented in the book Hacker's Delights.

Question 16th:

Copy Code code 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 real meaning of static local variables, this problem is a piece of cake dish dish dish ...

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.