Run the following code and what is the output? Explain why.
int i; int main(int argc,char *argv[]){ i--; if(i>sizeof(i)){ printf(">\n"); }else{ printf("<\n"); } return 0; }
>
The default value of uninitialized global variables is 0, while the value of i after i -- is - 1, which is binary
1111 1111 1111 1111 1111 1111 1111 1111
The result of sizeof is 4 and the binary representation is
0000 0000 0000 0000 0000 0000 0000 0100
Obviously, i is compared with sizeof (i) as an unsigned integer in the comparison, so the result is >
Execute the following code snippet to 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; }
11
The macro performs a simple text replacement. The replacement result of A*B in the question is actually 2 + 2 * 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; }
26 27
%zu output size_t-type, size_t is defined as unsigned int in the library .
strlen calculates the number of elements of the string (excluding the "\ 0" at the end)
sizeof calculates the memory size, and the ending "\ 0" also accounts for one byte, so the result is 27.
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); }
x = 5 num = 5
x = 5 num = 10
The output of x is the same, but the output of num is different.
The static modifier is used to define the static local variable. Its life cycle is the running time of the whole application, and it will be initialized only once. Every time you call fun(), you will add 5 to the original num, and x will add 5 from 0 every time
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; }
First, the function of 1u is to indicate that 1 is of unsigned int type, < < is shifted left,
If char a=1, the binary of a is 0000 0001. After a < < 3 is executed, the value of a is 8, its binary number is 0000 1000, and the low order is supplemented by 0.
&Yes and operation. When the corresponding binary bits are all 1, the result of this bit is 1, otherwise it is 0.
For example, a=3, B = 7, a & B = 2 (0011 & 1110 = 0010)
Similarly, mask > > = 1 is to move one bit to the right, and the high bit is supplemented by 0.
"?:" is a binocular operator,? If the previous return value is not zero, return? The value in the middle of and: otherwise, the value after: is returned
Please explain the execution results of the following procedures
int main(int argc, char *argv[]) { char *str = "Xiyou Linux Group"; printf("%c\n", *str + 1); return 0; }
If the address of X is the first address of the string, * str='X ', and' X'+1='Y' (addition and subtraction of ASCII code)
What are the 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; }
XiyouLinuxGroup
There is a loss of precision when converting integers into floating-point numbers. Float can only ensure the validity of at least 6 digits, while double can only ensure the validity of at least 15 digits. So a= b. When a is cast to float, a==b.
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; }
Home computers are generally small end machines, that is, the low end is in the front. For example, in the question: 0x6f796958 In the computer, "58" comes first and "69" comes last. First output the character corresponding to the hexadecimal number "58", and then output the character corresponding to the hexadecimal number "69".
Analyze the output of the following programs 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; }
2 0
2 0
int a[2][3] = {{5, 7}, {5, 2}};//Equivalent to: {5,7,0}, {5,2,0} int b[2][3] = {5, 7, 5, 2};//Initialize in sequence, equivalent to {{5,7,5}, {2,0,0} int c[2][2] = {{5, 7}, {5, 2}}; int d[2][2] = {5, 7, 5};//Same as b
Execute the following program segment, what is the output result and explain the reason.
int main(int argc, char *argv[]) { int a = 1; printf("%d\n", *(char *)&a); }
1
*(char *) & A is to change the pointer of a to a char pointer, and the actual value of a does not change after the value operation.
What is the output result of the following program section? Can array a 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? Explain why
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; }
1) Cannot be modified
2) Not working properly
const decorated array in which no element can be changed.
Array b cannot be assigned by pointer
What steps does a c source file go through to an executable file?
1. Pretreatment
In the preprocessing stage, the compiler is mainly used for loading header files, macro replacement and conditional compilation. Statements with "#" are generally handled.
2. Compile
In the compilation process, the compiler mainly performs syntax check and lexical analysis. We can use - S Option, which translates the preprocessed results into assembly code.
3. Compilation
In the assembly process, the compiler converts the assembly code into machine code.
4. Link
Linking is the process of linking object files, startup code and library files into executable files, which can be loaded or copied to memory for execution.
Can you see the function of this function? Try to understand the principle and optimize
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; } } } }
Bubble sort, compare two adjacent numbers in the array in turn, and exchange two numbers in ascending or descending order.
Optimization:
void sort(int arr[], int size) { int flag = 1; int count = 0; int i,j,temp; for(i=0;i<size-1&&flag;i++) { for(j=0;j<size-1;j++) { flag = 0; count++; if(arr[j]>arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; flag = 1; } } } }
pwd (displays the current directory)
ls: list the directory and file name, -a all the files, and list them together with the hidden files (the files starting with.)
MKDIR (create new directory):
mkdir [-mp] directory name
cp is copying files and directories.
[ root@www ~]#CP [- adfilprsu] source destination