#include<stdio.h> int i = 2; voidfunc() { if (i != 0) { // true staticint m = 0; int n = 0; n++; // n = 1 m++; // m = 1 printf("m = %d, n = %d\n", m, n); i--; // i = 1 func(); // new n = 1; // m = 2; // i = 0 // false, loop ends here. } else { return; } } // So the results are: // m = 1, n = 1 // m = 2, n = 1 intmain(int argc, char* argv[]) { func(); return0; }
4. What is the result of the following program? Why does this result occur?
1 2 3 4 5 6 7 8 9
intmain(int argc, char * argv[]) { char ch = 'A'; int i = 65; unsignedint f = 33554433; *(int *)&f >>= 24; *(int *)&f = *(int *)&f + '?'; printf("ch = %c i = %c f = %c\n", ch, i, *(int *)&f); return0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<stdio.h> intmain(int argc, char* argv[]) { char ch = 'A'; int i = 65; unsignedint f = 33554433; // f = 0000 0010 0000 0000 0000 0000 0000 0001 *(int*)&f >>= 24; // move right 24 bits, f = 0000 0000 0000 0000 0000 0000 0000 0010 *(int*)&f = *(int*)&f + '?'; // '?' == 63, so f + 63 = 0000 0000 0000 0000 0000 0000 0100 0001 = 65 printf("ch = %c i = %c f = %c\n", ch, i, *(int*)&f); // So: ch = A i = A f = A return0; }
5. What is the output of the code below, talk about your understanding.
#include<stdio.h> intmain(int argc, char* argv[]) { int a[2][2]; printf("&a = %p\t&a[0] = %p\t&a[0][0] = %p\n", &a, &a[0], &a[0][0]); // These three results are all the same thing, all of which are the starting address of the container a // &a = 0x16f0b7528 &a[0] = 0x16f0b7528 &a[0][0] = 0x16f0b7528 printf("&a+1 = %p\t&a[0]+1 = %p\t&a[0][0]+1= %p\n", &a + 1, &a[0] + 1, &a[0][0] + 1); // The size of array a is 4*4 = 16 bytes, the size of a[] is 2*4 = 8, and the size of an integer is 4. // So the output is: &a+1 = 0x16f0b7528 + 16 &a[0]+1 = 0x16f0b7528 + 8 &a[0][0]+1= 0x16f0b7528 + 4 // &a+1 = 0x16f0b7538 &a[0]+1 = 0x16f0b7530 &a[0][0]+1= 0x16f0b752c return0; }
6. What is the function of the following programs? What’s the problem, can you figure it out and fix it?
1 2 3 4 5 6 7 8 9 10
int* get_array() { intarray[1121]; for (int i = 0; i < sizeof(array) / sizeof(int); i++) { array[i] = i; } returnarray; } intmain(int argc, char *argv[]) { int *p = get_array(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<stdio.h> int* get_array() { intarray[1121]; printf("%lu\n",sizeof(array)); for (int i = 0; i < sizeof(array)/sizeof(int); i++) { array[i] = i; } returnarray; } // This is a pretty stupid mistake, if the address of the local variable is returned, the local array variable is located in the stack area. // After the function ends, the data in this address will lose its meaning; // what to do in this case, you can add static to the local variable array; intmain(int argc, char* argv[]) { int* p = get_array(); for (int i = 0; i < 1121; i++) { printf("%d ", *(p+i)); }
There is no regulation or standard for how many spaces to skip. Each output device will stipulate that \t will be positioned at a multiple of an integer unit on its own device.
8. The following program, according to the print results, what do you think?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
intadd(int *x, int y) { return *x = (*x^y) + ((*x&y)<<1); } int a; intmain(int argc, char *argv[]) { int b = 2020; if(add(&b, 1) || add(&a, 1)) { printf("XiyouLinuxGroup%d\n", b); printf("Waiting for y%du!\n", a); } if(add(&b, 1) && a++) { printf("XiyouLinuxGroup%d\n", b); printf("Waiting for y%du!\n", a); } return0; }
This involves a short circuit || .
9. In the next program, we can print out the address of a through the first step, if the result printed on your machine is 0x7ffd737c6db4; we use the scanf function in the second step to input this address value into the variable c Middle; the third step, randomly input a number, what is the final output, do you know the principle?
10. What process will a C language program go through from source code to executable file, can you briefly describe what each link does?
From source files to executable files generally need to go through several steps: preprocessing -> compile -> assembly -> link these four processes.
Preprocessing: Preprocessing is equivalent to converting the source code into a new c program according to the preprocessing command, but usually with an i extension.
Compile: Translate the resulting i file into assembly code, usually with an s extension.
Assembly: Translate assembly files into machine instructions and package them into o files for relocatable object programs
// This is actually a variant of selection sort, it is the same as the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<stdio.h> #include<string.h> intmain(int argc, char* argv[]) { char str[100]; int key; char t; fgets(str, 100, stdin); for (int i = 0; i < strlen(str) - 1; i++) { for (int j = i + 1; j < strlen(str); j++) { if (str[i] > str[j]) { t = str[i]; str[i] = str[j]; str[j] = t; } } } puts(str); return0; }
13. Use loop and recursion to find Fibonacci sequence, which one do you think is better? Give your opinion. If you are asked to find the 100th term of the Fibonacci sequence, do you think it can be solved by conventional methods? Please try to find the value of the first 100 items (tip operation with large numbers).
Please create a directory through the command, create several files with the suffix .Linux in the directory, and then query the basic attribute information of these files (such as file size, file creation time, etc.) through the command, and then use the command Check the number of files with “.Linux“ in the file name in the directory (excluding files in subdirectories), write the obtained number into a file, and finally delete the directory.