sizeof():
It is an operator which helps in getting the size of the object.
Syntax:
sizeof(object);
Example:
sizeof(int), sizeof(struct struct_name);
It looks like a function where the object is the parameter and the return type is an int that is the size of the object but it is an operator.
It is a compile time operator as it needs type of the object to get its size and this type information is available at compile time and after compilation phase , the resulting object does not have the type information.
Type information can be fetched even at run time but it results in bigger object code and lesser performance.
Volatile:
Volatile is a keyword attached to variable which indicates to the compiler that the value of the variable can be changed any point during the lifetime of the variable that is its value is un deterministic. Its value can be changed even by code which is beyond the scope of current code.
It’s an indication to the compiler not to optimize the data.
Syntax:
volatile data_type name
Example:
- The variable which holds the data for the port must be declared as volatile as data may come at any point in time. If it is not declared as volatile and the port does not receive data for a while, the compiler may use the previous value kept in the register to speed(optimize) the performance. To avoid such optimization, the port variable must be volatile.
- The variable used in ISR (interrupt service routine) must be volatile as interrupt can come at any point in time and if the compiler may optimize which results in outdated values for such variables.
Const volatile:
Any variable that is preceded by const volatile means its value cannot be changed based on the current scope of the program but can be modifiable from outside.
It is used in situations like a read-only hardware register, or an output of another thread. Volatile means it may be changed by something external to the current thread and Const means that we do not write to it (in that program that is using the const declaration).
Example:
“extern const volatile int real_time_clock;”
Where real_time_clock may be modifiable by hardware, but cannot be assigned to, incremented, or decremented.
Categories: C Language
Leave a Reply