Three questions for C language interviews and three questions for C language interviews
1. There are one hundred integers, with a negative number. Find the part with the largest sum of the three consecutive numbers.
Example:
Input: 2,-8, 3,-2, 4,-10
Output: 5 ({3,-2, 4 })
#include
int find(int arr[], int len); int main(void) { int a[100], b[98], i = 0; for(i=0; i<100; i++) scanf("%d", &a[i]); for(i=0; i<98; i++) { b[i] = a[i] + a[i+1] + a[i+2]; } i = find(b, 98); printf("%d({%d, %d, %d})\n", b[i], a[i], a[i+1], a[i+2]);} int find(int arr[], int len){ int i = 0, j = 0, tem = 0; for(i=0; i
arr[i+1] && arr[i] > arr[j]) j = i; } i++; if (arr[i] > arr[j]) j = i; return j;}
2. Use <<>>,|,& to implement an unsigned short variable (two bytes) to exchange the high and low bits !!
Example: 0x1234 after switching 0x3412
Function prototype: void func (unsigned short *)
#include
void func(unsigned short *a);int main(void) { unsigned short a = 0x11ff; func(&a); printf ("%x\n", a);} void func(unsigned short *a){ unsigned short b = *a; *a = *a << 8; b = b >> 8; *a += b;}
3. embedded systems often require programmers to access a specific memory location. In a project, the value of an integer variable with an absolute address of 0x67a9 must be set to 0xaa55.
The compiler is a pure ANSI compiler. Write code to complete this task.