0% found this document useful (0 votes)
4 views5 pages

C Intro

CSS 145 Applied Signal Processing : An introduction to the C-programing language

Uploaded by

My Own
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

C Intro

CSS 145 Applied Signal Processing : An introduction to the C-programing language

Uploaded by

My Own
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Applied Signal Processing

An introduction to the C-programing language


A first draft version
Tomas McKelvey
2003

This document will serve the purpose of introducing the structure of the C-programming
language, including syntax and a presentation of a few of the basic language elements.

1 Data types
Variables in C can have different types. The language requires the data type of a variable
to be declared at the compile time. If the compiler knows the data type at compile time it
can produce more efficient code. The C-language supports several primitive data types.
The CCS and the 6711DSK supports (among other) the following type :

• char integer (8 bits)

• short integer (16 bits)

• int integer (32 bits)

• float floating point (32 bits)

• double floating point with double precision (64 bits)

2 Structure of a C-program
A C-program is based on a set of functions. The execution of the program starts by a call
to the function main. The functions which make up the program are listed in the source
file together with variable declarations and header files. The structure of the c-source file
is a result of an ideology where the compiler should be able to compile the code without
having to iterate to many times over the source code. Hence the following rules apply:

• All variables need to be explicit declared before used in a function. Declarations can
be made global as well as local. A global variable is reachable from all functions
while a local variable is only reachable within the function (actually block) it is
declared in. The type of the variable also need to be explicitly given.

1
• All functions need to be declared by a function prototype before being used in the
code. The reason for this is that when the compiler reaches the use of the function
it can explicitly check that the number of arguments and types of the arguments
are correctly given.

• So called header files with the extension “.h” are included before the functions are
defined. In the header files special types are defined, constants are defined and
function prototypes declared.

Consider the following C-program,

// This is a one line comment


/* This is multiple line comment
...
...
end of comment
*/
// Inclusion of header files
#include <stdio.h>
// Definitions of constants
#define ACONST 10
#define VECSIZE 4
// Declaration of a function prototype
float fmult(float a, float b);
// This is a declaration of global variables
float r1;
float vector[VECSIZE]={1,2,3,4}; // This declares a vector of size VECSIZE.

// Definition of the main function


int main(){
int a=1, b=2, c; // Declaration of local variables
c = a + b + ACONST;
r1 = fmult(0.23, 0.35);
printf("a + b + ACONST is %d \nr1 = %f \n", c , r1);
return(0); // Return 0
}
// Definition of the function fmult
float fmult(float a, float b){
float c;
int i;
c = a * b;
for (i=0; i<VECSIZE; i++){
c += vector[i]*vector[i];
};
return(c);
}

Comments in the code which the compiler should disregard can appear in two forms.

2
• The one line version. The compiler disregards all characters on which appear after
//.

• The multi line version. The compiler disregards all characters between the start of
comment /* and the end of comment */. Comments cannot be nested.

Library header files are included with #include <name.h > while header files local to
the project (within the same folder) are included with #include "name.h ". Numerical
constants can be defined using the format #define constantname value . Function
prototypes are declared like

type name (type1 var1, ...,typek vark );

Variable declarations have the form like

type name1, name2, ..., namek ;

which would reserve space for k variables of type type . Alternatively the variable can
also be initialized at the same time like

type name1 =value1, name2 =value2, ..., namek =valuek ;

Arrays of variables is declared in the following way

type name1 [size1 ], name2 [size2 ], ..., namek [sizek ];

The number within the square brackets determines the number of storage elements which
will be allocated in the memory. In C the reference to the elements of the array starts
with index 0 and run to index size-1. The first element in an array a will thus be referred
to as a[0]. Arrays can also be initialized at the declaration. The syntax for this is

type name [k] = {value1,value2,..., valuek};

which would reserve space for an array of size k initialized with the values given in the
list enclosed with curly brackets.
Functions are defined according to the following form
type name (type1 var1, ...,typek vark ){
local variable declarations
function body
return( expression );
}
When the function executes the return command the function will return to the calling
function and the value given in the expression will be returned to the calling function as
the value returned.

3
3 The basic language elements
The body or block of a C-program is composed of a sequence of statements. Each state-
ment is normally concluded by a semicolon ; unless part of the then clause in an if
statement. A statement can be of many forms like an assignment, an if statement or a
for loop statement. A block which in turn can contain variable declarations and state-
ments is also a statement. A block is enclosed by curly brackets and is called a compound
statement. A block can declare local variables which then are only accessible from within
the block. Statements are composed of identifiers, for example, numbers, names of vari-
ables or other reserved words. Identifiers are separated by so-called white spaces which
are one or several spaces, tabs or new line characters. The C-language disregard the
visual formatting of a program and one or many white spaces separates the identifiers
from each other.
Arithmetic expressions are formed as as expected using variables and constants and
the operators +,-,*,/,%. The modulus operator % gives the reminder of integer division.
e.g. 5 % 2 evaluates to 1 since 2 ∗ 2 + 1 is 5. Assignments has the following form

variable =expression ;

which will assign the value of expression to the variable variable . The following rela-
tional operators (relop ) exists:

• == equality

• != inequality

• <,>,<=,>= less than, greater than, less than or equal, greater than or equal

A relational expression looks like

expression relop expression

and results in a boolean value which is either 1 (true) or 0 (false). Boolean operators
operate on boolean expressions

• (&&) logical and

• (||) logical or

• (!) logical negation

and the result is again a boolean value. Important to know is that the boolean operators
have a higher precedence than the relational operators so it is often necessary to enclose
the relational expressions with parenthesis to get the right associations. For example a
test to check that two variables a,b are grater than 10, then a correct logical expression
would be be

(a>10) && (b>10)

Control structures control the flow of the program the most common of them is the
if statement.

4
if (expression )
statement
else
statement ;
Note that a semicolon only should appear at the last statement. Looping is accomplished
by the while, do and for statements. The while statement has the form

while (expression ) statement ;

As long as the expression evaluates to a non-zero value the statement will be repeated.
Normally the statement is a block statement. The do statement has the syntax

do statement while (expression );

Here the order is exchanged. First the statement is executed and then the expression
is evaluated. The looping continues until the expression becomes 0 (false). The for
statement is the most used one when dealing with arrays (or vectors). The syntax is

for (exp1 ; exp2 ; exp3 ) statement ;

The semantics are as follows. First expression exp1 is evaluated only once. Normally
the expression contains initialization of the loop variable. Then exp2 is evaluated. If
non-zero (true) statement is executed. Finally exp3 is evaluated. The looping continues
until the stop criterion in exp2 evaluates to false. The following example shows how to
add two arrays of size 4:
for (i=0; i<4 ;i++){
c[i] = a[i] + b[i];
};
Here we used the special increment C-construction i++ which is equivalent to i=i+1.
Also i-- exists which is equivalent to i=i-1 Finally we mention the special statement
i += j which is equivalent to i = 1 + j . Similar constructions exist for the other
arithmetic operators -,*,/,%.

You might also like