top of page
  • Writer's pictureMARY PRINCY R

C programming for Embedded systems 3


I expect on the readers of this blog to have a fundamental understanding of the C programming language.


Learning Objectives:

  • Storage classes commonly used in embedded C programming.

What are storage classes?

Storage Classes are used to describe a variable's or function's features. These features, which mainly include scope, visibility, and lifetime, allow us to identify the existence of a certain variable throughout the run-time of a program.


The 4 keywords usually used with variable declaration in embedded C programming to specify specific requirements with the storage of variables in memory are static, volatile, const, register.


The following syntax must be used to specify a variable's storage class:

storage_class variable_data_type variable_name;


Static storage class

If a variable is declared static within the body of a function, it maintains its value between the function calls. At the same time, if a variable is declared static within a module but outside the body of a function, then it will be accessible by all functions within that module. Static variables are stored globally and not on the stack.


If a "function" is declared static within a module, then it may be called by other functions within that module only. Usage of static functions can result in smaller and faster code.


example: static int a;


Volatile storage class

Volatile variables may change their value outside the normal program flow. This can happen in embedded systems as a result of a hardware action or by an interrupt service routine. Declaring all peripheral registers in embedded devices to be volatile is considered to be an excellent practice because volatile variables are never optimized.


example:

*((volatile unsigned char*))0x18) =0xFF;

This piece of code set all the bits of a register whose address is 0x18 to 1.


Const storage class

The compiler protects the const variable from accidental writing i.e. they are "read only".The const variable must be declared with an initialized value. In embedded systems, they are stored in ROM.


example: const unsigned int b=12;


Register storage class

A variable declared as a register tells the compiler to store that variable in a register. But the compiler may or may not agree to this request. Loop counters and array indices are two variables that might be used as register storage class variables. Also, it is illegal to use the address operator '&' with the name of register variable because the value of this variable might be stored in a register.


example:

register int count = 0;

for( ; count<100; count++)

{

}


Happy learning!!!!!


Link to the previous blog from this series C Programming for Embedded systems 2 (risefromashes.blog)

12 views0 comments

Recent Posts

See All
bottom of page