C programming review
Microcontroller Technology GONDONGWE CN
conradnyika@gmail.com
Introduction to C
• Every C program consists of one or more modules called functions. One of the
functions must be called main.
• Each function must contain:
1. A heading (the function name), including the return type.
2. A list of parameters to be passed to the argument.
3. A list of statements enclosed by curly braces {}.
e.g int addNum (int a, int b) {
int z;
z = a + b;
return (z);
}
Example Program
#include <stdio.h>
int main (){
printf (“Hello World!”);
return 0;
}
Data Types
• Each argument in C must be of a distinct data type.
• The main data types in C are:
• int – this is for integer type arguments. It reserves 2 bytes for the data.
• char – this is for character type arguments. It reserves 1 byte for the
data.
• float – this is for floating point type arguments. It reserves 4 bytes for
the data.
• double – this is for double precision floating point type numbers. It
reserves 8 bytes for the data.
Data Type Qualifiers
• The four type of qualifiers in C are short, long, signed, and unsigned.
• Memory allocations for the data types and qualifiers vary from
compiler to compiler.
• Signed data types reserve the MSB for the sign bit, whereas unsigned
data types utilize all bits to represent the data.
Identifiers
• Identifiers are names that are given to arguments such as variables,
functions, arrays, and constants in a C program.
• Identifiers can consist of letters and numbers, but must start with a
letter.
• Certain characters are not allowed to be used in an identifier (see the C
character set).
• Every identifier that represents a number or character in a C program
must have a valid data type associated with it.
Identifiers
• Examples of declarations of identifiers include:
int val;
char name;
float area;
double roots;
int addNum (int a, int x);
Variables And Arrays
• A variable is an identifier that is used to represent a single data item
e.g a numerical value or character constant.
• We assign values to variables with the = operator, after declaring the
variable.
Example
int x;
x = 5;
Variables And Arrays
• An array is an identifier that refers to a collection of data items that are all of the same data
type.
• The individual data types are represented by their corresponding array elements.
Example 1
float x[3];
x[0] = 5.5;
x[1] = 3.2;
x[2] = 5.8;
OR
float x [] = {5.5,3.2,5.8};
Example 2
char city [ ] = "Harare";
Variables And Arrays
• Variables can also be assigned values through expressions.
x=a+b;
• Here, a and b must be declared and assigned values before this
statement can be executed.
x = cos(n);
• The cos function belongs to the math library.
Control structures in C
• The following control structures are available in C.
o if – else
o while
o do – while
o for
o switch
o goto
o Conditional operator ( ? : )
The if-else statement
• This statement carries out the test and then makes one of two
possible actions.
Syntax Example
if (condition){
statements 1;
}
else{
statements 2;
}
The if-else if …-else structure
• This structure is an extension of the if – else structure. It can be used when there are more than 2
possible outcomes. There is no limit on the number of else if blocks you can use in this structure.
Syntax
if (condition){
statements 1;
}
else if{
statements ;
}
…
else{
statements}
Example Program
The while statement
• This type of structure allows for program looping, whereby a block of
statements is repeated for as long as a certain condition is true.
Syntax
while (condition) {
statements;
}
Example program
The do-while loop
• With the while loop, the program checks to see if the condition
expression is true before it enters the loop.
• With the do-while loop however, the program checks to see if the
condition expression is true after it enters the loop.
• This means that a do-while loop with execute at least once.
Syntax
do {
statements;
} while(condition);
Example program
The for statement
• The for statement can be used in place of the while statement when looping is required in a
program.
Syntax
for( index initialization; condition; index manipulation){
statements;
}
Example
for (i=0; i<=10; i++){
printf(“%d\n”,i);
}
• index initialization – this is the expression used to initialize the index variable.
• condition – this is the expression which must be true in order to allow entry into the loop.
• index manipulation – this is the expression which alters the index value after every successive loop
execution.
Example program
Nested for loops
• Like all other control statements, it is possible to have a for loop
within another for loop
Syntax
for( index initialization1; condition1; index manipulation1){
statements for i loop;
for( index initialization2; condition2; index manipulation2){
statements for j loop;
}
}
The break statement
• The break statement is used to terminate loops prematurely.
• After the program sees a break statement, it will exit the loop in which
the break statement exists, and continue with the program.
Syntax
break;
Example program
The continue statement
• The continue statement is used to bypass the remainder of the current
pass through a loop.
• It does not terminate the loop entirely, but rather only the current pass.
Syntax
continue;
Example program
The switch statement
• The switch statement causes a particular group of statements to be
executed from several available groups.
Syntax Example
switch (expression){
case <value 1>:
statements;
break;
case <value 2>:
statements;
break;
.
.
case <value n>:
statements;
break;
default:
statements;
break;
The switch statement – explanation
• expression is the field where you place the variable under test. Usually the data type is int or
char.
• <value1>-<value n> are all the particular values you want to look out for, which the variable
under test can assume.
• The switch statement will examine the current value of the variable under test on entry into
the switch structure.
• It will then search for this value in all the case labels.
• If it finds this value it will execute the statements just under the case label containing the
corresponding value until it sees the break statement.
• Once it sees the break statement it will exit the entire switch structure and continue with
normal program execution.
• If it does not find the corresponding value in any case label, then it will execute the default
label.
Example program
Another example
The goto statement
• The goto statement is used to alter the normal sequence of program
execution by transferring control to some other part of the program.
• It uses labels to redirect program flow.
Syntax
goto <label>;
• where the label would have been previously defined at some statement
in the program.
Example program
However!
• The goto statement can cause a lot of confusion and reduce program
readability, as programs will usually be written in a non-sequential manner.
• You are encouraged to rather use the structured selection features of C.
• For example:
• we can jump out of a loop at any time with a break statement,
• we can skip the current pass in a loop with the continue statement,
• we can use the while statement to give the user a chance to repeat the program,
• we can use the for statement for program looping.
• By using the structured features of C, we can thus write our programs in a
sequential and easy-to-understand manner.
The conditional operator
• Simple conditional operations can be carried out with the conditional
operator.
• Syntax
variable =expression 1 ? expression 2 : expression 3
• Example y = (x==0)? 50:100;
• Expression 1 is evaluated first. If it is true then the variable will take the
value of expression 2, otherwise the variable will take the value of
expression 3.
• Expression 2 and expression 3 must therefore result in values that are of
corresponding data type to the variable.
Example program
Functions
• A function is a self-contained program segment that carries out some
specific, well-defined task.
• Every C program consists of one or more functions.
• It is mandatory that one of the functions in a C program is to be called
main.
• The use of functions avoids the need for redundant (repeated)
programming of the same instructions.
Elements of a function
• A function consists of two main elements:
• The function declaration
• The body of the function
• The function declaration will contain all the information needed to
initialize the function.
• The body of the function will contain all the C statements necessary
for the function to carry out its unique task.
• Another element of a function is its prototype, which is a statement
defining the function and is written before the main program.
Skeletal structure of a function
• In general, a function will have the following skeletal structure:
return type function name (arguments){
body of function
return value
}
Example
Skeletal structure of a function
• The return type represents the data type of the item that is returned by the function.
• The function name represents the name given to the function.
• The arguments contain all the parameters needed to be passed to the function in order for it to
work correctly.
• The body of the function contains all the statements which carry out the function’s purpose.
• The return value is the value that is passed from the function back to the portion of the C program
which called it. A function can have multiple return statements.
Declaration examples
• Can you identify the components of the following function declarations?
char letter (int x, char z){
}
void showValue (char index){
}
double roots (float a, float b, float c){
}
int timer (int val){
}
Accessing a function
• Most functions will be accessed from the main function.
• In order to access a function we must make reference to its name, as
well as pass all the necessary arguments to it.
• The argument names do not have to be the same as those in the
function declaration statement (since we only pass the value), but they
must be of the same data types and appear in the correct order.
• If the function returns a value, we normally assign the function access
to a variable of the same data type as the return type of the function.
• A function which does not return anything can be accessed by simply
writing the function name and passing its argument values.
Example program
Passing arguments to a function
• When a single value is passed to an argument, the value is copied into
the function. This means that the original value is not changed in the
main function, even after a successful execution of the function.
• Consider the example to follow.
Example program
Program output