C语言面试5:const volatile, static, extern, register, typedef, #define, enum, big/little endia

Const,  #define:

  1. #define PI 3.14    :  the preprocessor replaces those macros before the compiler even sees it.
  2. const means it will be saved in read-only flash. C code cannot change it ( cannot change even use pointer because it's in read-only memory), but could be changed by hardware I/O【volatile const】
  3. const is better than #define because it has data type, so compiler could do the safety check

Volatile

  1. A volatile keyword is a qualifier that prevents the objects, from compiler optimization and tells the compiler that the value of the object can change at any time without any action being taken by the code
  2. It prevents the cache from a variable into a register and ensures that every access variable is fetched from the memory.
  3. The volatile keyword is mainly used where we directly deal with GPIO, interrupt, or flag Register. It is also used where a global variable or buffer is shared between the threads.

  4. const volatile:  If you want to access these read-only status register, you have to use const and volatile together with pointers

Storage class:  staic, register, extern, auto

Static

  1. Static is a keyword can be used with both variables and functions. 
  2. Static functions are not visible outside of the C file they are defined in.
  3. Static global variables : internal-linkage, are not visible outside of the C file they are defined in
  4. Static defined local variables do not lose their value between function calls. In other words they are global variables, but scoped to the local function they are defined in.

  5. Static defined local variables:  A static variable only initializes once, so a variable declared static within the body of a function maintains its prior value between function invocations.

  6. init as 0

Register:   

  1. Register variable stored in CPU register instead of memory and its properties is generally similar to the local variable. Accessing of the register is faster than the memory
  2. A register keyword only gives the indication to the compiler to store this variable in the register instead of RAM, But it totally depends on the compiler. The compiler decides where to put the variable in register or RAM.

  3. We can not use & and * operator with a register variable because access the address of the register variable is invalid.

Typedef:

typedef struct                                     // 用了typedef,这个struct的名字是可以忽略不写的

{

int iRollNumber;

char acName[30];

} sStudentInformation;

Enum:

  1. An enum in C is a user-defined data type and it consists set of named constant integer, start with 0 【所以使用方法是先将变量定义成enum类型,然后再去赋值】

enum Day

{

Mon,

Tue,

Wed,

Thu,

Fri,

Sat,

Sun

};

Big/Little Endiness: C Program To Check Little or Big Endian Machine

  1. Endian measn bytes order,  use c code to check your endiness
  2. Big endiness : most-significate byte save first ( in lower address)

写检测程序的时候注意:

int p=4;

p = 4;

  1. 全局变量必须在所有函数外定义,初始化。但是不能在函数外面再给他赋值了(正确的做法是在直接在定义的时候初始化,然后在函数里面进行操作)。【如上如,编译器就会报错变量p重定义,因为编译器会认为第二行仍然是在定义变量而不是再给变量赋值】
  2. 要想查endian,必须定义的是int,然后强制转化成char型的指针去读第一个byte。【写法 char *p = (char*) &a 】如果直接是强制转化成char型变量,编译器的做法是舍弃高位,只留下低8位,达不到效果【具体看汇编即可明白 深刻理解unsigned char, signed char. 有符号数,无符号数._hjjdebug的专栏-CSDN博客
  3. int默认是有符号int, int转char 既要考虑endian又要考虑符号/补码,过于麻烦,能展示思路就行。 int强制转char型问题理解_准备的永远在准备,迈出下一步,也许你能走的很远-CSDN博客_将int强制转换为char

Bitwise operation

XOR: exclusive OR, outputs true only when inputs differ。 所以想要取反/ toggle一个bit,只需要将它和 1 XOR 就可以

pass by value

  1. Pass by value means that a copy of the actual parameter’s value is made in memory
  2. Changes made to the passed variable do not affect the actual value.

pass by reference 

  1. The memory address of the stored data is passed
  2. Changes to the value have an effect on the original data

常见面试问题:

  1. Can we use const and volatile at the same time:
    1. Yes, const means read-only, could not be modified by the code after initialization, volatile tell's compiler do not optimize it using cache. Const Volatile is useful when accessing read-only I/O status register

Reference:

C Tutorial - Aticleworld

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值