-Short introduction to C

Do not ignore C compiler preprocessor

You maybe don't know or probably didn't think about this but you are using program preprocessing before compiling it. As I said before compiling I mean, that each time you are compiling your project, C compiler prepares program file to be ready for compilation. Preprocessor includes other files to main file, defines symbol constants and macros, prepares conditional compilation of code. All preprocessor tasks are marked with ampersand symbol “#”. Lets go through most of directives of preprocessor:

Typecasting in AVR-GCC

I am sure- typecasting is one of common practices in embedded C when converting one variable to another. As AVR microcontrollers are 8 bit systems and sometimes we operate with 16, 32 or even 64 bit length variables we have an ability to change like 16 bit integer value to 8 bit char and so on. This operations is so called typecasting. So it is important to understand this operation in order to use it properly and get desired result.

Enumeration of variables

This is similar to #define preprocessor where you can describe a set of constatnts. Using preprocessor we use:

#define zero 0

#define one 1

#define two 3

But there is an alternative of using enumerating using keyword enum:

enum (zero=0,one, two); //zero=0, one=1; two=2

By default enumeration assigns values from 0 and up.

Now you can use enumeration like in following example:

Pointers to structures in embedded C

Sometimes you might like to manipulate the members of structures in more generalized way. This can be done by using pointer reference to structure. Sometimes it is easier to pass pointer to structure than passing entire structure to function.

Structure pointer can be declared very easily:

struct structure_tag_name *variable;

For instance:

AVR GCC Structures

Basically Structures are nothing more than collection of variables so called members. Structures allows to reference all members by single name. Variables within a structure doesn't have to be the same type. General structure declaration:

struct structure_tag_name{
	type member1;
	type member2;
	type memberX


struct structure_tag_name{
	type member1;
	type member2;
	type memberX
} structure_variable_name;

Program Flow In Embedded C

Program Flow and control is a control method of your program. For example Loop constructions control repeated execution of repeated program segments where control is taken by control parameter. In this article we will go through if/else switch/case statements and loop sentences like while, do while, for.

While statement

As I mentioned three looping sentences available in C language one of them is While sentence. Lets take an example:

C language Operators and expressions

The main thing what microcontrollers does is operates with data. There are four main operations that microcontrollers does: adds, abstracts, multiplies and divides (+,-,*,/). division can be split in division “/” and modulus operation “%”. For instance i1/i2 is integer division.

Other part of operators is Relation operators. They are used for boolean conditions and expressions. Expressions with these operators return true or false values. Zero is taken as false and non zero value as true. Operators may be as follows: <, <=, > >=, ==, !=.

The priority of the first four operators is higher than that of the later two operators. These operators are used in relational expressions such as:

Constants in C language

Constant value is understandable as non changeable value like PI=3.141592... value in math. Usually you use constants in your programs, but don't realize that they are constants. For instance:

x=x+3; The number 3 is a constant which will be compiled directly in addition operation. Constants can be character or string. Like in function printf(“Hello World\n”); “Hello World” is a string constant which is placed in program memory and will never changes.

It is usually recommended to declare constants by using identifier with reserved word const:

const int No=44;

Variables in embedded C language

What are variables in C language. Variables are simple keywords which are defined by the values. Values can be changed. Variables are like a boxes with some size where values like apples can be put in. So variables can be various forms and sizes so called variable types.

Variable type is defined by a reserved word which indicates the type and size of variable identifier:

unsigned char my_char;

long int all_my_numbers;

int number;

Why do we need variables? The basic answer is that memory is limited and compiler needs to know much space to reserve for each variable. So the programmer needs to specify the variable type and its size by using one of reserved words from the table:

The very basics of C

C language is function based programming language. C program itself is a function. Usually parameters to C function are passed as arguments. Function consists of a name followed by the parentheses enclosing arguments or an empty pair of parentheses if there are not arguments required. If there are several arguments, they are separated by commas.

The mandatory part in C program is main function. This function must be included in every program because this is a first function which is run after execution of program.

Lets take an example:


Subscribe to RSS - -Short introduction to C