C language _ brush questions

1. About the printf function

1. The printf function has a return value, and printf returns the number of output characters.

#include<stdio.h>
intmain()
{
	int i = 43;
	printf("%d\n", printf("%d",printf("%d",i)));//The return value of the printf function is the number of output characters//4321
	return 0;
}

2. The following question is from Niuke.com, and the knowledge point is the method of passing function parameters , but because the printf function is used, I will put it here and say it together.

Find the output of the following program

#include<stdio.h>

int f(int a, int b, int c)
{
	return 0;
}

intmain()
{
	return f(printf("a"), printf("b"), printf("c"));
	return 0;
}
The answer is abc, but I ran it in VS as cba, and I read the explanations of other netizens. It is said that the parameters of the function are passed through the stack, so the parameters are pushed onto the stack from right to left, and the output result is cba. It is said that the function is passed through the register, and the output result is abc. Such a topic is boring. Just know what it means.

2. Intelligence questions

1. There are 6 classmates playing dota in the dormitory, divided into two opposing sides, one is the Scourge Legion and the other is the Guard Legion. Now, please set the schedule and the matchup for each game (minimum 1 player and maximum 5 players per side), how many games must be played at least so that each student can compete with other students after the game is over (3)

Answer: This question was done right at the time, it was tried out. Looking at the netizens' answers, I feel very good.

Use 0, 1 to indicate which side each student is on, 0 for the Scourge and 1 for the Guards. 000 means that the student has been in the natural disaster army 3 times, 001 means that he has been in natural disaster twice and once in the guard. 000, 001, 010, 011, 100, 101, 110, 111, at least one of the eight binary numbers is different, indicating that they are in confrontation. This question is equivalent to several binary digits, which can represent 5 different numbers.

2. Draw cards from a deck of standard poker cards, and continue to draw (not take out) when black cards are drawn, until red cards are drawn, then stop. According to the probability, how many black cards can be drawn each time on average? (That is, the Luoshen skill of Zhen Ji in the Three Kingdoms kill, the expected value of the number of cards)

1
1.2
0.8
0.9

Answer: When I did it, it was 2, and I didn't see the drawn black card and continued to put it back into the deck. In fact, this question is very simple, but I also play Three Kingdoms Killing, and I think it is very interesting, so I put it here.

E=1/4+2/8+3/16+4/32+...+n/(2^n+1)

=1

3. The problem of memory occupied by structures in C language

Reprint: https://www.cnblogs.com/kl2blog/p/6908048.html

The memory occupied by the structure has been very confusing before. Which variable type is used to calculate the memory? Or how to calculate? Let's take a look at an example:

struct str1
{
	char a;
	int b;
	float c;
	double d;
};

How much memory does the str1 structure occupy? If you want to add directly with the variable type, the result is 17, but this is obviously not the case. The correct result for this program to run is 24. Why?

  Because in order for the CPU to access quickly and improve access efficiency, the starting address of the variable should have certain characteristics, which is called "alignment". For example, for a 4-byte int variable, its starting address should be on the 4-byte boundary, that is, the starting address can be divisible by 4.

  The rules for memory alignment are simple:

  1. The starting address is an integer multiple of the memory occupied by the variable type. If it is insufficient, the insufficient part will be filled with data to an integer multiple of the occupied memory.

  2. The total memory occupied by the structure is an integer multiple of the largest data type in the structure member variable.

  Next we analyze the above example:

  The char type variable occupies one byte, so its starting address is 0, while the int type occupies 4 bytes, and its starting address should be 4 (an integer multiple), then the memory addresses 1, 2, and 3 need to be is filled. Similarly, float occupies 4 bytes, and the two member variables a and b in the structure occupy memory addresses 0~7, and the address of c starts from 8, which conforms to rule 1, and occupies memory addresses 8~11. The double type occupies 8 bytes, so the starting address of d should start from 16, then the memory addresses 12, 13, 14, and 15 need to be filled. d starts at address 16 and occupies 8 bytes. The number of bytes occupied by the entire structure is 24, which conforms to Rule 2. The memory allocation is shown in the figure: the red area is the filled part



Here's another example to further illustrate:

struct str2
    {
        double a;
        int b;
        char c;
        double d;
    };

What is the memory space occupied by the str2 structure? is 24! How to analyze it?

First of all, the memory address occupied by a of double type is 0~7, the starting address of b of int type is 8, which conforms to Rule 1, the occupied address is 8~11, the c of char type occupies one byte, and the address is 12. Then the double type d, the starting address is 13? Obviously not, the address that satisfies rule 1 is 16, so the starting address of d is 16, occupying 16~23. The structure has a total of 24 bytes, which satisfies the second rule. If a member variable char e is added to the structure at the end, how much memory does this structure occupy? The starting address of e of the char type is 24, and the occupied address is 24, but the structure type has 25 bytes, which does not satisfy the second rule. What should I do? In order to satisfy the second rule, we will pad 25~31, so the entire structure occupies 32 bytes.

Let's take a look at an example from Nioke Online:

The following code prints (assuming running on a 64-bit computer):

struct st_t {
    int status;
    short *pdata;
    char errstr[32];
};
st_t st[16];
char *p=(char *)(st[2].esstr+32);
printf(“%d”,(p-(char *)(st)));

status occupies 4 bytes, and the bytes are filled with 4 bytes.

64-bit system, pointer occupies 8 bytes, pdata occupies 8 bytes

errster occupies 32 bytes

The entire structure occupies 48 bytes.

The final answer is 144.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326008818&siteId=291194637