[c language] West Post Linux interest group 2020 new interview questions

1, Run the following code, output the result and explain the reason.

int i;
int main(int argc, char *argv[])
{
    i--;
    if (i > sizeof(i))
    {
        printf(">\n");
    }
    else
    {
        printf("<\n");
    }
    return 0;
}

        The symbol ">" will be output here. Because i is a global variable, it will be initialized to 0. For sizeof(i), i is of type int, so it is 4,

i -- the last i is - 1, so - 1 < 4, the "<" symbol is output.

2, Execute the following code segment. Is the output the same as you expected? Talk about your understanding of macros.

#define A 2 + 2
#define B 3 + 3
#define C A * B
int main(int argc, char *argv[])
{
     printf("%d\n", C);
     return 0;
}

         Macros are simple substitutions, so C=2+2*3+3=11, not c (2 + 2) * (3 * 3)

3, Analyze the output of the following program

int main(int argc, char *argv[])
{
     char str[] = "Welcome to XiyouLinuxGroup";
     printf("%zu %zu\n", strlen(str), sizeof(str));
     return 0;
}

%zu is size_ Output type of T, size_t is defined in the standard c library.

/*  32 position */
typedef unsigned int    size_t;
typedef int             ssite_t;
 
/*  64 position */
typedef unsigned long   size_t;
typedef long            ssize_t;

strlen () is to get the length of the string, and sizeof is to get the number of bytes occupied by the variable.

Therefore, the results are 26 and 27. Sizeof obtains more bytes occupied by "\ 0" than strlen.

(int argc.char*argv []) is related to command line parameters. You can learn about it if you are interested.

4, What is the output result of executing this function in the program? Is the output result the same if the function is executed multiple times in the same program?

void fun()
{
 int x = 0;
 static int num = 0;
 for (int i = 0; i < 5; i++)
 {
 x++;
 num++;
 }
 printf("x = %d num = %d\n", x, num);
}

Num is a static local variable defined by static. Its life cycle lasts until the end of this program, and the scope is this function. After this function goes out, this variable cannot be called again, but if the function is called many times, Num will take the value of num when the function was called last time. Therefore, Num has been increasing, and x every call starts from 0.

5, Analyze the following procedure, speculate and verify its function

int main(int argc, char *argv[])
{
 int number;
 unsigned mask;
 mask = 1u << 31;
 scanf("%d", &number);
 while (mask)
 {
 printf("%d", (number & mask) ? 1 : 0);
 mask >>= 1;
 }
 return 0;
}

Function: convert decimal to binary

        The value of mask is to the power of 2 ^ 32. 1. On the sign bit, perform "&" bit sum operation between number and mask for a total of 32 times, and calculate the binary plum of number by one bit.

Plus number is 3, then the operation is

1:         10000000 00000000 00000000 00000000

              10000000 00000000   00000000   00000011 / / get the first value

2:         01000000 00000000 00000000 00000000

               10000000 00000000   00000000   00000011 / / get the value of the second bit

............

32:          00000000 00000000 00000000 00000001

                 10000000 00000000   00000000   00000011 / / get the value of the 32nd bit

6, Please explain what the running results of the following programs are

int main(int argc, char *argv[])
{
     char *str = "Xiyou Linux Group";
     printf("%c\n", *str + 1);
     return 0;
}

The result is: Y  

*(str+1) is: the pointer of the string indicates, * str is the string pointer, str is the address of the first character of the string, printf ("% c",*str) is the output of the first character, and * (str+1) is the output of the second character.

* str+1 is + 1 based on the * str value, but the character cannot be + 1, so it will be converted to ascii plus 1, and the ascii+1 of X is exactly Y

7, What are the running results of the following program segments? Do you know how to judge whether two floating-point numbers are the same?

int main(int argc, char *argv[])
{
    double a = 3.14;
    float b = a;
    if ((float)a == b)
    {
        printf("Xiyou");
    }
    if (a != b)
    {
        printf("LinuxGroup\n");
    }
    return 0;
}

Result: Linux group

Because the value after the decimal point calculated by the computer has precision, there will be no problem with constant assignment, and there will be errors in the comparison between the calculated and constant assignment, and there will be precision loss in the calculation. At this time, it is necessary to compare the size with a minimum value (remember to calculate the absolute value, because you may not be sure which is large and which is small).

For example:

#include<stdio.h>
#include<math.h>
int main(){
    double a=1.14;
    double b=0.9+0.24;

    if(fabs(a-b)<0.00000001){
        printf("True\n");
    }else{
        printf("False\n");
    }

    return 0;
}

8, Run the following code, explain the running results and talk about your understanding.

int main(int argc, char *argv[])
{
     int a[6] = {0x6f796958, 0x694c2075, 0x2078756e, 0x756f7247, 0x30322070,0};
     printf("%d\n", printf("%s", (char *)a));
     return 0;
}

Running result: Xiyou Linux Group 2020

The output result of a is Xiyou Linux Group 20 and% d outputs the return value of printf. The return value of printf is 20. The printf in it means 20 characters, while Xiyou Linux Group 20 is exactly 20 characters.

In a[6], there are hexadecimal ascii codes and knowledge of large and small terminals. According to the output results, mine is the small terminal mode.

Attached test method:

int main(){
    int num = 0x12345678;
    char cs = *(char*)&num;

    // If the low address is the high byte, it is the large end
    if ( cs == 0x12 )    
        printf("Big end");
    else{
        printf("Small end");
    }
}

9, Analyze the output of the following procedures and explain why.

int main(int argc, char *argv[])
{
    int a[2][3] = {{5, 7}, {5, 2}};
    int b[2][3] = {5, 7, 5, 2};
    int c[2][2] = {{5, 7}, {5, 2}};
    int d[2][2] = {5, 7, 5};
    printf("%d %d\n", a[1][1], b[1][1]);
    printf("%d %d\n", c[1][1], d[1][1]);
    return 0;
}

result:       2 0
                2 0

          For a, it is a two-dimensional array, which is equivalent to two one-dimensional arrays. The third value of the first one-dimensional array is not assigned, so there is no third value. The second bit of the second array, i.e. a[1][1], has a value of 2 in {5,2}

        For b, it is also a two-dimensional array, but compared with a, there is no {} package, so the first one-dimensional array has a third value of 5, and the second two and three bits of the second one-dimensional array have no assignment, so there is no b[1][1], uninitialized, random number, and 0 is a coincidence.

        For c, it is a two-dimensional array. The one-dimensional array contains two values, so its values are assigned. It is output normally, and the value of c[1][1] is 2

        For d, it is a two-dimensional array. The one-dimensional array contains two values, but it is only assigned three values. The fourth is not assigned, so the fourth number. d[1][1] is a random number and 0 is a coincidence.

10, Execute the following program segment. What is the output result? Please analyze the reason according to relevant knowledge.         

int main(int argc, char *argv[])
{
     int a = 1;
     printf("%d\n", *(char *)&a);
}

  Output result: 1

        (char *) & A, forcibly convert the address of a into a pointer of character type, and then output the ascii code value of the value of the address, that is, 4 bytes of int type into a 1-byte value of char type. However, in the small end mode (I said earlier), the low address of 1 is stored in the low byte and converted into char type without change.

11, What is the output result of the following program section? Can the a array be modified if the const annotation on the third line is cancelled? If you cancel the comments on lines 6 and 8, can the program run normally? Try to explain the reason.

int main(int argc, char *argv[])
{
 /*const*/ char a[] = "XiyouLinux\0";
 char *b = "XiyouLinux\0";
 a[5] = '\0';
 // b[5] = '\0';
 printf("%s\n", a);
 // printf("%s\n",b);
 return 0;
}

Result: Xiyou

        It's wrong to change according to the title,   Because const is a constant, it is to prevent you from randomly changing and adding things. It cannot be changed. B is a string pointer, and the string pointer is similar to const char b []. The content pointed to by the string pointer cannot be modified. What is defined by the string pointer is stored in the static storage area. It is a constant and cannot be changed.

  XII     c source - > executable process, gcc

         I said it in my last blog, so I won't say it anymore.

For gcc: Simple and easy to understand gcc tutorial - beginner_ Big entertainer - CSDN blog_ gcc usage , I think it's very good. I can't explain it so clearly, so I directly post a link.

        13, Read the following function carefully. Can you see the function of this function? Try to understand the principle of the algorithm and try to optimize it.

void sort(int arr[], int size)
{
    int i, j, tmp;
    for (i = 0; i < size - 1; i++)
    {
        for (j = 0; j < size - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;
            }
        }
    }
}

  This is a simple bubble sort. If optimized, I can only directly sort quickly now. Le, I didn't expect to optimize bubble.

        For this bubble sort (it's much easier if you don't draw a picture), first move the large value backward as a whole, and then sort.

        For example: 1,3,2,6,5,4

First compare 1 with 3 and find that 1 < 3 to the next bit, compare 3 with 2 and move 3 backward by one bit............. (move the maximum number to the last bit)

  Then it comes out of the first internal circulation for the second comparison. (the second largest value moves to the penultimate position)

...... Finally, arrange the order from small to large

 

Tags: C

Posted on Thu, 02 Dec 2021 15:35:25 -0500 by miracle_potential