## 面試時間:2024/12 ### 1. Check the following staments (a)-(g) (a) ``` #include<stdio.h> main { int c; c=5; printf("%d\n", c); } ``` * Ans: ``` int main() { ... } ``` (b) ``` if(a>b) then c=0; ``` * Ans: ``` C 語言中沒有 then 關鍵字,應該刪除。 ``` (c) ``` if a>b {c=0;} ``` * Ans: ``` if 條件判斷後需要括號,應為 if (a > b)。 ``` (d) ``` if(a>b) c=0; ``` * Ans: ``` 單行 if 條件語法正確。 ``` (e) ``` c=0 b=0; ``` * Ans: ``` 漏掉 ; 分號。 ``` (f) ``` if(a>b) c=0 else b=0; ``` * Ans: ``` if(a > b) c = 0; else b = 0; ``` (g) ``` int square(int x); { return x*x; } ``` * Ans: ``` int square(int x) { return x * x; } ``` ### 2. What is in a[8] after the following code is executed? ``` for(i=0; i<10; i++) a[i]=9-i; for(i=0; i<10; i++) a[i]=a[a[i]]; ``` * Ans: ``` a[8] = 1 ``` ### 3. What does the following program print out? ``` #include<stdio.h> #include<stdlib.h> typedef struct ex* ptr; struct ex {ptr ff; int k;}; main() { ptr x, y ,t; x = malloc(sizeof *x); y = malloc(sizeof *y); x->ff=y; y->ff=x; x->k=1; y->k=1; for(t=x;t->k<100;t=t->ff) t->k=x->k+y->k; printf("%d\n",t->k); } ``` * Ans: ``` 128 ``` ### 4. What is the output of the following program segment?Note:Assume the following program are running on a 32-bit big-endian CPU (a) ``` unsigned char c='-'; switch(c){ case '-': printf("%c\n", c); break; case '+': printf("%c\n", c); break; case '*': printf("%c\n", c); break; case '/': printf("%c\n", c); break; default: printf("default\n"); break; } ``` * Ans: ``` - ``` (b) ``` #define test(a,b) ((a>b)?1:0) int i = 0; int j = 1; printf("%d\n", test(i,j)); ``` * Ans: ``` 0 ``` (c) ``` int i = 0; int *p = &i; *p = 1; printf("%d\n", i); ``` * Ans: ``` 1 ``` ### 5. (a) Explain what does "BIG ENDIAN" and "LITTLE ENDIAN" mean on different CPU types? (b) Assume the following program running on a 32-bit CPU, please descibe the results on the standard output for big-endian and little-endian CPU. ``` #include<stdio.h> void main (void) { unsigned long x; unsigned char *p; int i ; x=0x11223344UL; p=(unsigned char *) &x; for(i = 0; i<sizeof(unsigned long), i++){ printf("%x ", *p++); } printf("\n"); } ``` For big-endian CPU output? For little-endian CPU output? * Ans: big-endian ``` 11 22 33 44 ``` little-endian ``` 44 33 22 11 ```