intmain(int argc, char *argv[]) { unsignedchar a = 255; char ch = 128; a -= ch; printf("a = %d ch = %d\n", a, ch); }
解读:
#include<stdio.h> intmain(int argc, char *argv[]) { unsignedchar a = 255; char ch = 128; // char overflows to -128 a -= ch; printf("a = %d ch = %d\n", a, ch); // a = 127, ch = -128 }
2. 下面代码的运行输出结果是什么,并说说你的理解。
intmain(int argc, char *argv[]) { char *str = "Xi You Linux Group 20"; printf("%d\n", printf(str)); return0; }
解读:
#include<stdio.h> intmain(int argc, char *argv[]) { char *str = "Xi You Linux Group 20"; printf("%d\n", printf(str)); // printf(str) returns the length of the string, 21 // So the result is "Xi You Linux Group 2021" return0; }
3. 这段代码的输出结果是什么?为什么会出现这样的结果?
int i = 2; voidfunc() { if(i != 0) { staticint m = 0; int n = 0; n++; m++; printf("m = %d, n = %d\n", m, n); i--; func(); } else { return; } } intmain(int argc, char *argv[]) { func(); return0; }
解读:
#include<stdio.h> int i = 2; voidfunc() { if(i != 0) { staticint m = 0; int n = 0; n++; m++; // m = 1, n = 1 printf("m = %d, n = %d\n", m, n); i--; func(); // loop once, then m = 2, n = 1, i = 0 and exit loop } else { return; } } intmain(int argc, char *argv[]) { func(); return0; }
4. 下面程序会出现什么结果?为什么会出现这样的结果?
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; }
解读:
#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; // f = 0000 0000 0000 0000 0000 0000 0000 0010 *(int *)&f = *(int *)&f + '?'; // *(int *)&f = 2 + 63 = 65 // ch = 'A', i = 'A', f = 'A' printf("ch = %c i = %c f = %c\n", ch, i, *(int *)&f); return0; }
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(); }
解读:
#include<stdio.h> int* get_array() { intarray[1121]; for (int i = 0; i < sizeof(array) / sizeof(int); i++) { array[i] = i; } // How dare you return this!? returnarray; } intmain(int argc, char *argv[]) { // get_array() returns the unusable address of a local variable, // the content of whom was discarded, but you can declare // a static variable in get_array() and return it to avoid so int *p = get_array(); }
#include<stdio.h> intmain(int argc, char *argv[]) { char str[] = "XiyouLinuxGroup"; char *p = str; char x[] = "XiyouLinuxGroup\t\106F\bamily"; // XiyouLinuxGroup Family // sizeof(str) = 16, containing '\0' at the end // sizeof(p) = 8, 'cuz p is a char pointer, // only storing the address of variable str // sizeof(x) = 24, containing '\0' at the end // strlen(x) = 24, 'cuz of '\b', 'F' is covered by 'a' printf("%zu %zu %zu %zu\n", sizeof(str), sizeof(p), sizeof(x), strlen(x)); return0; }
8. 如下程序,根据打印结果,你有什么思考?
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; }
解读:
#include<stdio.h> intadd(int *x, int y) { return *x = (*x^y) + ((*x&y)<<1); } int a; intmain(int argc, char *argv[]) { int b = 2020; // add(&b, 1) returns true, so add(&a, 1) is not executed if(add(&b, 1) || add(&a, 1)) { printf("XiyouLinuxGroup%d\n", b); printf("Waiting for y%du!\n", a); } // add(&b, 1) returns true, while a++ executed with ruturning 0, // so this conditional statement will not be executed if(add(&b, 1) && a++) { printf("XiyouLinuxGroup%d\n", b); printf("Waiting for y%du!\n", a); } return0; }
// It's totally about the basic concept of pointers #include<stdio.h> voidfunc() { int a = 2020; unsignedlong c; // We get the address of variable a printf("%p\n", &a); printf("我们想要修改的地址:"); // We want to modify the value on address entered scanf("%lx", &c); printf("请随便输入一个数字:"); // And then we modified it. All done. scanf("%d", (int *)c); printf("a = %d\n", a); }
#include<stdio.h> intmain() { // It outputs a anoymous int array as a char array // using little-endian storing methods puts((char *)(intconst[]){ // o y i X n i L u r G x u 0X6F796958, 0X6E694C75, 0X72477875, // 2 p u o (\0)2 1 0 0X3270756F, 0X313230, 0X00000A}); }