You are on page 1of 8

C

1 Introduction
C is a high-level language that is often used in place of assembly to make writing programs
easier. The C language is standardized, so the same C program should work on every
microcontroller, as long as it is compiled (translated into assembly) correctly.
The main elements of a C program are the pre-processor command, followed by the main
function. Within the main function, subroutines may be called.

Credit: Bolton, Mechatronics: Electronic control systems in mechanical and electrical engineering

2 Features
2.1 Statements
These are the individual entries which make up a program. Each statement is terminated
with a semicolon. Individual statements can be grouped together into blocks with braces.
{
statement 1;
statement 2;
}

2.2 Functions
A function is used for a self-contained block of statements which perform a specific set of
actions. A function has a specific name by which it is referred, like a subroutine in assembly.
It’s written like name(arg). The brackets enclose any arguments, which are values passed
onto and inputted into the function. For example:

printf(”Mechatronics”);

1
Indicates that the string ’Mechatronics’ is passed to the function printf(), and is printed
onto the screen. printf is a keyword, and a pre-written function defined in the pre-processor
phase.

2.3 Return
A function may return a value to the calling routine. The type of value that is returned
is specified in front of the function name. For exaple, int main() indicates that the main
function will return an integer. The return type can be specified as void if the function does
not return a value. To actually return a value, the keyword return is used in a statement
such as return result.

2.4 Main function


Every C program must have a function called main(). This is the first function called, and
therefore the function that exercises control when the program is run. By convention, a
return value of 0 is used to indicate normal program interpretation.

2.5 Standard library functions


C packages are supplied with lbraries containing large numbers of predefined functions. These
can be called by naming them. For example, the printf() function can be called from the
stdio.h library.

2.6 Pre-processor
This is a porgram identified by pre-processor commands so that it is executed prior to
compilation. All such commands are identified by having # at the beginning of the line.
thus we might have # include < stdio.h > to include the stdio library.
You can use these for other purposes too, such as # define pi 3.14.

2.7 Comments
Like Assembly, C allows for notes to be made that are ignored by the compiler. They are
indicated by /* comments */.

2.8 Variables
A variable is a named memory location that can hold various values. These are usually
identified as carrying a specific type of data such as char (1 byte), int (4 bytes), float (4
bytes), or double (8 bytes).
Each number is formatted differently depending on its type. floats, for instance, include a
single sign bit, 8 exponent bits, and 32 mantissa (the actual digits) bits, and can be directly
expressed in scientific notation. The number = (sign -1:1)(2e xp)*1.(mantissa bits)

2
2.9 Keywords
In C, certain words are reserved for specific meanings. For example, int indicates when an
integer is being declared, and if indicates when a program can change direction.

2.10 Assignments
An assignment statement gives a value to a variable. a = 2 indicates that the variable is
now assigned the value 2. a = b means that variable a now has the same value as b.

2.11 Arithmetic operators


operators such as addition +, subtraction -, and modulus % all work in C.

2.12 Relationship operators


these return ’true’ or ’false’ values in response to comparisons such as ’is x greater than y’
(x¿y). These can be equals ==, not equal !=, less than or equal to <=, for example.

2.13 Logical operators


In C you have AND &&, OR || , and NOT !. The return is 1 or 0.

2.14 Bitwise operators


These treat their operands like a series of bits, and work instead of their numerical value,
and work like Assembly logic operators. Some examples include AND &, OR | , and NOT
∼.

2.15 Escape sequences


These are special sequences that escape from standard interpretation and are used to control
the output of the screen or other special treatments. For example \a sounds a beep, \n creates
a new line in the output, and \t creates a horizontal tab.

3
2.16 Arrays

Credit: Bolton, Mechatronics: Electronic control systems in mechanical and electrical engineering

An array is a collection of data storage collections each having the same data type, and can
be used to collect multiple values together.
float Temperature[7] = {10, 12, 15, 11, 10, 14, 12};
Indicates an array of size 7, with preassigned values. Temperature[0] = 10, and Temper-
ature[6] = 12. Arrays can be multidimensional, array[x][y].

2.17 Pointers
Each memory location has a unique address. A pointer is a special variable which can
store the address of another variable. Thus, if a variable p contains the address of another
variable x, then p is said to point to x. If x is at address 100, then p has value 100. A pointer
declaration takes the form
type *name
in *pnumber
with the asterisk indicating that it is a pointer. We can initialize or assign a pointer with
an address using the & sign
pointer = &variable

3 Simple examples
# include <stdio.h>
void main(void)
{
int a, b, c, d; /*a, b, c, d are integers*/
a = 4; /*a is assigned value of 4*/
b = 3; /*a is assigned value of 3*/
c = 5; /*a is assigned value of 5*/
d = a*b*c;/*d is assigned value of multiplication*/
printf(”a*b*c = %d\n”, d)
} In this program, %d indicates that d is to be presented in decimal form, and n indicates
a line break.

4
# include <stdio.h>
void main(void)
{
int *p, x; x = 12;
p = & x;
printf(”%d”, *p);

return 0;
}

4 Branches and loops


C has a few ways to form subroutines and branching programs.

4.1 If

Credit: Bolton, Mechatronics: Electronic control systems in mechanical and electrical engineering

and if statement will allow a subsequent statement or group of statements to be executed if


true.
if(x == y)
printf(”x is equal to y”);
If you want the program to do one thing or another thing, you can use and if/else state-
ment:
if(temp>50)
printf(”Warning”);
else

5
printf()”OK”);

4.2 For
The term loop refers to the execution of a set of statements repeatedly. One way to do this
is with a for loop. A for loop includes an initializing expression, a test expression, and an
increment expression.
main() {
for(count=0;count < 7; count ++)
printf(”%d”,count);
}

Credit: Bolton, Mechatronics: Electronic control systems in mechanical and electrical engineering

4.3 While
A while loop is continuously repeated as long as the expression is true.
int main() {
count = 1;
while(count < 7)
{
printf(”%d”, count);

6
count++;
}
return 0;
}

4.4 Switch

Credit: Bolton, Mechatronics: Electronic control systems in mechanical and electrical engineering

For multiple branching statements you can also use the switch statement. This takes an
expression, and will execute whichever statement matches the expression.
int main() {
int x;

7
printf(”Enter a number,0, 1, 2, or 3”);
scanf(”%d”, &x);

switch(x)
{
case 1:
printf(”One”);
break;
case 2:
printf(”Two”);
break;
case 3:
printf(”Three”);
break;
default:
printf(”Not 1, 2, or 3”);
}
return 0;
}