TI DSP 2013 Crash Course On C: February 17, 2013
TI DSP 2013 Crash Course On C: February 17, 2013
TI DSP 2013
Crash course on C
C Language
First slide
C Language
Program structure
#include <stdio.h> // the headers contain declarations for commonly used functions
#include <math.h> // if they are not used, the compiler cannot use the external libraries
typedef struct { // try to pack into a structure the variables
int x, y; // associated to one type of object
} Point; // the name of the structure appears at the end of the definition
unsigned int g_frame_counter = 0; // always put prefix g_ on global variables
// but in general, try to avoid using globals; use locals and pass structures as parameters
// use constants or defines to parametrize your programs
#define PI 3.14
#define ERROR_INVALID_POINTER (-1)
int func_1(int a, char *b) // function definition,
// in general the function returns a status code (0 == success)
{
if (b == NULL) // check input parameters
return ERROR_INVALID_POINTER; // this is an error
...
return 0; // exit code 0 is the standard way to say everything went well
}
void main(void) // execution starts here
{
int i = 0; // always initialize variables
// call a function
func_1(6, NULL);
}
TI DSP 2013 Crash course on C
C Language
Workflow
I
main.c
defs.h
Compiler
Object file
Linker
Executable
Libraries
C Language
Variables
Variables in C are always declared at the beginning of a block:
SystemStateType *g_system_state = NULL; // global pointer variable
// variables should always be initialized
void my_function(int a, unsigned char * b) // variables as function parameters
{
unsigned int error_code = 0xFFFF; // local function var; always declare and init
static int s_count = 0; // local static variable
char name[200] = { 0 }; // declare an array of chars (letters)
if ( a < 0 )
{
int i = 0; // local definition inside block
// note that a block consists of some code between a pair of { and }.
}
s_count++;
strcpy(name, "abc"); // what is this? why not name = "abc"?
return i;
}
What problems do you see in the above code (there are 2)?
C Language
Variables
Variables in C are always declared at the beginning of a block:
SystemStateType *g_system_state = NULL; // global pointer variable
// variables should always be initialized
void my_function(int a, unsigned char * b) // variables as function parameters
{
unsigned int error_code = 0xFFFF; // local function var; always declare and init
static int s_count = 0; // local static variable
char name[200] = { 0 }; // declare an array of chars (letters)
if ( a < 0 )
{
int i = 0; // local definition inside block
// note that a block consists of some code between a pair of { and }.
}
s_count++;
strcpy(name, "abc"); // what is this? why not name = "abc"?
return i;
}
What problems do you see in the above code (there are 2)?
C Language
Variables
Variables in C are always declared at the beginning of a block:
SystemStateType *g_system_state = NULL; // global pointer variable
// variables should always be initialized
void my_function(int a, unsigned char * b) // variables as function parameters
{
unsigned int error_code = 0xFFFF; // local function var; always declare and init
static int s_count = 0; // local static variable
char name[200] = { 0 }; // declare an array of chars (letters)
if ( a < 0 )
{
int i = 0; // local definition inside block
// note that a block consists of some code between a pair of { and }.
}
s_count++;
strcpy(name, "abc"); // what is this? why not name = "abc"?
return i;
}
What problems do you see in the above code (there are 2)?
C Language
int a;
unsigned int b;
unsigned char c;
char d;
float e;
double f;
//
//
//
//
//
//
4 bytes
4 bytes
1 byte
1 byte
4 bytes
8 bytes
//
//
//
//
//
//
C Language
Structures
Structures should be used to put together variables which are
relevant to a type of object.
typedef struct {
int width, height;
char format;
unsigned char * pixels;
} ImageType;
They can be initialized in one place and then passed via pointers:
// main program
// declare and init all
// fields to zero
ImageData img = { 0 }; // local variable
unsigned int histogram[256] = { 0 };
img.width = 720;
img.height = 480;
img.format = GRAYSCALE_FORMAT_8BIT;
img.pixels = GetFrameData(); // external
compute_hist( & img, histogram );
// local function
void compute_hist(ImageType * img,
unsigned int * hist) {
int x, y;
memset(hist, 0,
256 * sizeof(unsigned int));
// img->field is equivalent
// to (*img).field
for (y = 0; y < img->height; y++)
for (x = 0; x < img->width; x++)
hist[ img->pixels[ k++ ] ]++;
}
C Language
Macros
Macros are used to define constants and simple functions:
#define IMAGE_WIDTH 720
#define MAX(a,b) ( ( (a) < (b) ) ? (b) : (a) )
// good example
if (counter == MAX_COUNT)
for (i = 0; i < WIDTH; i++)
...
C Language
Branching If statement
An if statement looks like this:
if ( count < 10 && is_running != 0 )
{
printf("System is running\n");
}
else
{
printf("Error: system is stopped because: %s\n",
(count >= 10) ? "count is too big" : "is_running was set to zero");
}
Make sure you dont mix logical operators (|| and &&) with
bitwise operators (& and |).
C Language
switch( state )
{
case RUNNING:
show_logo(&frame); break;
case TRANSITION:
show_animation(&frame, counter++);
break;
default:
printf("System is stopped.\n");
break;
}
10
C Language
Loops For
11
C Language
Loops For
11
C Language
Loops For
11
C Language
Loops For
11
C Language
Loops While
int i = 1024;
do {
// code which uses variable i
i /= 2;
} while( i > 0 );
12
C Language
Loops While
int i = 1024;
do {
// code which uses variable i
i /= 2;
} while( i > 0 );
12
C Language
Loops While
int i = 1024;
do {
// code which uses variable i
i /= 2;
} while( i > 0 );
12
C Language
Loops While
int i = 1024;
do {
// code which uses variable i
i /= 2;
} while( i > 0 );
What about the left side, how many cycles? Unknown. The i
variable is not initialized.
12
C Language
Bit operations
Integers can be written in base 10 or base 16. Any integer can be
shifted to left or right by N bits:
int a = 65536, b = 0x10000; // a == b
a >> N; // shift a to the right by N bit positions. this is equivalent to a / 2^N;
a << N; // shift to the left by N positions, equivalent to a * 2^N;
a << 2 == a * 4 == 0x40000; a >> 8 == a / 256 == 0x100 = 256;
13
C Language
Pointers 1 (basics)
A pointer is a variable which contains a memory address. Pointers
have data types. A pointer can be initialized with the memory
address of another variable:
int a = 5, *p = NULL;
p = & a;
In order to get (or assign) the value stored at the address pointed
by a pointer, you use the * operator:
*p = 10; /* write to the address */
a = *p + 9; /* read from the address */
14
C Language
Pointers 2 (arrays)
Arrays in C are in fact just pointers to memory which is statically
allocated. These lines are equivalent:
unsigned int vector[100]; // allocate memory for 100 integers;
// the memory is allocated on the stack
unsigned int *p; // declare a pointer to a memory region containing integers;
// (the pointer does not point to anything yet)
// p is initialized to the address of the first element in the array
p = vector; // the same as: p = & vector[0];
// to access the 10th element, we can use the indexing operator [ ] or pointer arithmetic
vector[10] == *(vector + 10);
15
C Language
Memory allocation
Memory can be dynamically allocated on the heap:
unsigned int * bigarray = NULL;
int length = 1000;
bigarray = (unsigned int *)malloc( length * sizeof(unsigned int) );
Always check the result of the allocation. It may fail and using a
null pointer will cause a crash.
if (bigarray == NULL) printf("Error, not enough memory!\n");
16
C Language
Question 1
17
C Language
Question 1
17
C Language
Question 2
I
18
C Language
Question 2
I
18
C Language
Question 3
19
C Language
Question 3
19
C Language
Question 4
20
C Language
Question 4
20
C Language
Question 5
21
C Language
Question 5
21
C Language
Question 5
21
C Language
Question 6
I
22
C Language
Question 6
I
22
C Language
Question 6
I
22
C Language
Question 6
I
22
C Language
Question 6
I
22
C Language
Question 6
I
22
C Language
Question 6
I
22
C Language
Question 7
Q: What are the problems with the code below? What will be
printed?
int f(int *a)
{
*a++;
}
void main(void)
{
int a = 10;
f(a);
printf("a = %d\n", a);
}
23
C Language
Question 7
Q: What are the problems with the code below? What will be
printed?
int f(int *a)
{
*a++;
}
void main(void)
{
int a = 10;
f(a);
printf("a = %d\n", a);
}
23
C Language
Question 8
I
24
C Language
Question 8
I
24
C Language
Question 9
25
C Language
Question 9
25
C Language
Question 10
I
26
C Language
Question 10
I
26
C Language
Question 11
I
27
C Language
Question 11
I
27
C Language
References
The best book on C is:
28