Introduction to C – Part 2

4 Memory allocation

4.1 Memory organization: stack, heap, DATA and BSS

A stack is an area of memory used for temporary variables. A stack follows the “last in, first out” principle, LIFO. This means that during run time, a variable is put on the stack,, for instance it is declared and used in a function. When the variable has served its purpose, it is removed from the stack.

A heap on the other hand is used for dynamic memory allocation. Memory is allocated in an arbitrary order, and is not removed before the memory is deallocated using the function free or the program ends.

DATA is an area of memory used for global initialized variables. The BSS area contains uninitialized global variables, i.e. global variables that only have been declared.

Figure 4.1. A typical way to organize the memory. The stack grows downwards and the heap upwards in memory

1Example 4.1. Showing an example with variable declarations. The placements in memory are told in the comments.

24.2 malloc and free

To allocate memory while a program is running is known as dynamic memory allocation. The standard library function malloc returns a pointer to given amount of memory that can be used. The function free frees previously allocated space.

4.3 Dynamic arrays

By using malloc and free and declaring the array as a pointer you can make dynamical arrays. The general syntax is:

d_array = (type *) malloc (numberOfElements * sizeof(type));

where d_array is the array name. sizeof(type) returns the size in bytes. The array can be read from and written to in the same way as static arrays, using the notation d_array[i]. When the program is done you must remember to release the memory used, by using the function free. The syntax is:

free (d_array);5 Functions

5 Functions

5.1 Arguments and return value

A function takes arguments as input and returns a value. The format of a function is as follows:

type funcname(argument 1, argument2, …){statements}

type specifies the data type of the return value, funcname is the name of the function. The arguments also have a specified data type and a name. A function might not have any input arguments. The data types can be all the mentioned in Chapter 2 . type can also be set to void, which means no return value

Example 5.1. Example of a function that returns the sum of two integers

int sum(int a , int b){
return a+b;
}

5.2 Prototypes and definitions

A function is a group of statements that can be called and executed from some point in a program. The prototype is a declaration of the function which specifies the functions name, return value and arguments. This must be written before it is used in the code. The definition is the body of a function and specifies what the function does with the arguments.

Example 5.2. Prototype and definition of a function

6

5.3 The main function

The main function is where the program start its execution. The return value of main is an int. The function prototype looks like follow:

int main()

The main function can take arguments, argc, an argument count, and argv, an argument vector. The two arguments give the number and value of the command line arguments of the program. In this case the main function looks like follows:

int main(int argc, char *argv[])

The definition of the main function consists of the execution of the program with function calls, etc. The return value specifies how the program exited, and success is often represented with 0.

8

When programming microcontrollers the main function has no input and in general no return value. This is because the program stays in the main function in an infinite loop.

5.4 Static functions

Function prototypes are by default available in other files than the one where the prototype is. By adding the keyword static it is not available in all other files. The syntax of the prototype is:

void static do_something();

5.5 Inline functions

To inline a function means to request the compiler to write the function body where you are in the program instead of making a function call. The purpose of this is to save the time it would take to call the function from where it is defined. The compiler will not necessarily follow this request, and it might inline other functions as it sees fit. When inlining a function the keyword inline is used.

Example 5.3. An example of an inline function

8

6 Bitwise Operations

Sometimes a programmer needs to do operations at bit level. Bitwise operations can not be operated on one single bit, but must be performed on at least one byte at a time. In the following sections the bitwise operators will be presented.

6.1 & (AND)

& performs a logical AND operation to two binary representations of equal length. It returns 1 whenever there are 1 in both bits and 0 in the other cases.

Figure 6.1. A table showing the bitwise operation &

3

Example 6.1. An example showing the bitwise operation &

1001000
&1000100
=1000000

6.2 | (OR)

| performs a logical OR operation. It returns 1 if 1 is present and 0 if there are 0 in both bits.
Figure 6.2. A table showing the bitwise operation |

3

Example 6.2. An example showing the bitwise operation |

1001000
|1000100
=1001100

6.3 ~ (NOT)

The bitwise ~ operator returns the complement of a binary representation.
Example 6.3. An example showing the bitwise operation ~

~1001000 = 0110111

6.4 ^ (XOR)

The bitwise ^ operator returns 1 if both bits are equal and 0 if not.

Figure 6.3.  A table showing the bitwise operation ^

3

Example 6.4. An example showing the bitwise operation ^

1001000
^1000100
=0001100

6.5 << >> (bitshifting)

Bitshifting is moving bits either to the left or to the right. Because registers have a fixed number of bits this means that adding a bit on the one end means removing a bit on the other end. The added bits are zeros.

x << n

x >> n

The bitshift operators << (left) and >> (right) take two arguments; x that is a bitstring and n that is an integer that says how many bits to move to the right or to the left.

Example 6.5. An example showing a shift left by 3 bits

1001001 << 3 = 1001000

Left shift by K is the same as multiplying by 2^K and right shift by K is the same as dividing by 2^K

6.6 Short Hand

When setting a variable in C the sign “=” is used. Consider the following code snippet:

x = 1;
x + 2;

When adding 2 to x, this creates a temporary variable, but x stays the same, and if we printed the value of x the value would still be 1. If you want to change or update the value of x you must write:

x = x + 2;

or in short hand

x += 2;

The same syntax is used for other operators, for example bitshifting:

x = x<<2;

that in short hand equivalent to

x <<= 2;

7 Conditionals

7.1 Comparison operators

Comparison operators are boolean operators which means that when operated on some operands they return true or false. C did not support boolean expressions before C99 standard, and in that case the comparison operators will return 1 (true) or 0 (false).

Example 7.1. Example of a simple if-statement

if(a > 0){
a = -a;
}

Table 7.1. Comparison operators

4

Notice the double equal sign in contrast to the simple assignment sign, that sets a value to a variable.

7.2 Short-circuit conditionals: && and ||

The short-circuit conditionals are boolean operators. In C && means AND and || means OR. The following expressions will return true:

true && true == true
true && false == false
false && false == false
true || true == true
true || false == true
false || false == false

Looking at the third line: when checking the first false in the && statement, it is not necessary to check the other operand, and it will immediately return false.

7.3 if, else if, else

In conditional programming the result of a boolean computation will decide which computation or action to be performed. The general setup for an if, else if, else- statement is as shown in the following example:

Example 7.2. Function calculating bus ticket price using if, else if and else

5The computation or action to be done is where he boolean computation returns true. The if-statements can also be nested.

Energy Micro University is a program developed by Energy Micro to encourage learning and to help institutions develop their own programs for teaching microcontroller development. To download learning materials in pdf, please click here: Energy Micro University Program

For technical questions about this project, please use our support forum

One thought on “Introduction to C – Part 2

  1. Pingback: ARM Microcontrollers | Mpkb's Blog

Comments are closed.