const int a; | 常整型數 |
int const a; | 常整型數 |
const int *a; | 整型數是不可修改的,但指標可以 |
int * const a; | 整型數可以修改的,但指標不可以 |
int const * a const; | 整型數不可以修改的,但指標不可以 |
關鍵字volatile有什麼含意?
The volatile keyword was devised to prevent compiler optimizations that might render code incorrect in the presence of certain asynchronous events.
凡牽涉讀取該volatile變數的操作,保證會到該變數的實體位址讀取
Reference:
The first should set bit 3 of a. The second should clear bit 3 of a. In both cases, the remaining bits should be unmodified.
#define BIT3 (0x1<<3)
static int a;
void set_bit3(void)
{
a |= BIT3;
}
void clear_bit3(void)
{
a &= ~BIT3;
}
Absolute address 0x67a9 to the value 0xaa55
When interview use following code is better.
int *ptr;
ptr = (int *)0x67a9;
*ptr = 0xaa55;
A more obfuscated approach is:*(int * const)(0x67a9) = 0xaa55;
__interrupt關鍵字去定義了一個中斷服務副程式(ISR)
__interrupt double compute_area (double radius)
{
double area = PI * radius * radius;
printf(” Area = %f”, area);
return area;
}
這個函數有太多的錯誤了,以至讓人不知從何說起了:
1). ISR 不能返回一個值。
2). ISR 不能傳遞參數。
3). 在許多的處理器/編譯器中,浮點一般都是不可重入的。
#include<stdio.h>
int main()
{
char *ptr;
if ((ptr = (char *)malloc(0)) == NULL)
{
puts("Got a null pointer");
} else {
puts("Got a valid pointer");
}
}
The answer is “Got a valid pointer”int a = 5, b = 7, c;
c = a+++b;
equal toc = a++ + b;
explain “struct” and “union”
- struct → 所佔記憶體空間為 member 相加
- union → 所佔記憶體空間由最大size member決定
note: 所以union的member同一時間只會最多出現一個
#include <stdio.h>
int main()
{
unsigned long v1 = 0x00001111;
unsigned long v2 = 0x00001202;
unsigned long v;
v = v1 & (~v2);
printf("%x \n",v);
v = v1 | v2;
printf("%x \n",v);
}
answer 111
1313
#include <stdio.h>
int main()
{
char str[] = "Hello";
char *p = str;
int n = 10;
printf("%d %d %d",sizeof(str),sizeof(p),sizeof(n));
}
answer 6 8 4
#include <stdio.h>
int main()
{
int a[] = {6,7,8,9,10};
int *p = a;
*(p++)+=123;
*(++p)+=123;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}
answer 129 7 131 9 10
Write down a strcmp function
int scmp(const char *pStr1,const char *pStr2)
{
char c1, c2;
int v;
do {
c1 = *pStr1++;
c2 = *pStr2++;
/* the casts are necessary when pStr1 is shorter & char is signed */
v = (int)c1 - (int)c2;
} while ((v == 0) && (c1 != '\0'));
return v;
}
int main()
{
scmp("happy","happy1");
return 0;
}
List out differences between arrays and linked list
The difference between arrays and linked lists are:
- Arrays are linear data structures. Linked lists are linear and non-linear data structures.
- Linked lists are linear for accessing, and non-linear for storing in memory
- Array has homogenous values. And each element is independent of each other positions. Each node in the linked list is connected with its previous node which is a pointer to the node.
- Array elements can be modified easily by identifying the index value. It is a complex process for modifying the node in a linked list.
- Array elements can not be added, deleted once it is declared. The nodes in the linked list can be added and deleted from the list.
Write a C code
a) Get_Bit7_of_Input
b) Set_Bit5_of_Input
c) Clear_Bit_of_Input
#include <stdio.h>
int Get_Bit7_of_Input(int in) {
return (in & 64) >> 6; /* Shift to first postion to show */
}
void Set_Bit5_of_Input(int *in) {
(*in) |= 16;
}
void Clear_Bit_of_Input(int *in, int bit) {
(*in) &= ~(1 << (bit-1));
}
int main() {
int in1 = 65;
printf("bit 7 of %d is %d\n", in1, Get_Bit7_of_Input(in1));
int in2 = 0;
int in2_set = in2;
Set_Bit5_of_Input(&in2_set);
printf("set bit 5 of %d to %d\n", in2, in2_set);
int in3 = 9;
int in3_clear = in3;
int bit = 4;
Clear_Bit_of_Input(&in3_clear, bit);
printf("clear bit %d of %d to %d\n", bit, in3, in3_clear);
return 0;
}
1. check the input is a multiple of 3
#include <stdio.h>
static int reduce(int i) {
if (i >= 6) /* 3 * 2 = 6 */
return reduce((i >> 2) + (i & 0x03));
else
return i; // Done.
}
int a(int i){
if (i<0) i = -i;
i = (i >> 16) + (i & 0xFFFF);
i = (i >> 8) + (i & 0xFF);
i = (i >> 4) + (i & 0xF);
i = reduce(i);
return i == 0 || i == 3;
}
int main(){
printf("%d",a(5));
}
2. check the input is a multiple of 5
#include <stdio.h>
static int reduce(int i) {
if (i >= 10) /* 3 * 2 = 6 */
return reduce((i >> 4) + (i & 0x05));
else
return i; // Done.
}
int a(int i){
if (i<0) i = -i;
i = (i >> 16) + (i & 0xFFFF);
i = (i >> 8) + (i & 0xFF);
i = (i >> 4) + (i & 0xF);
i = reduce(i);
return i == 0 || i == 5;
}
int main(){
printf("%d",a(195));
}
Reference :
The difference between rvalues and lvalues
An lvalue is an expression that refers to such an object.
An rvalue is any expression that has a value, but cannot have a value assigned to it
Reference:
How DMA (Direct Memory Access) work
- Direct Memory Access video Java tutorials
What is IPC
Linux startup process
Linux startup process
一行版 九九乘法表
雙變數版
for(int i=1, j=1; i<=9; printf("%2d*%2d=%2d\n",i,j,i*j),(j==9?j=1,i++:j++));
單變數版
for(int j=0; j<9*9; printf("%2d*%2d=%2d\n",j/9+1, j%9+1, (j/9+1)*(j%9+1)),j++);
#include <stdio.h>
int main() {
int a = 0x12345678;
char *ptr = (char *)&a;
if (*ptr == 0x12)
printf("Big Endian\n");
else
printf("Little Endian\n");
return 0;
}
Reference:Reference:
- Embedded C Language Porblems
- A ‘C’ Test: The 0x10 Best Questions for Would-be Embedded Programmers
- Why is volatile needed in C?
- C程式 面試題目整理
- GIthub - headhsu2568/codejam/interview
- 面試題目整理
C和C++的差別:
C :具有高階語言流程控制與資料處理的便利性,以及低階語言直接(包含以位元)操作記憶體的精密性。
C++ :C++ 兼具了效率與彈性
Reference:
0 意見:
張貼留言