Beruflich Dokumente
Kultur Dokumente
TI DSP 2013
Crash course on C
First slide
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
Workflow
defs.h
Libraries
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;
}
I What problems do you see in the above code (there are 2)?
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;
}
I What problems do you see in the above code (there are 2)?
I return keyword appears in function declared as void.
Variable i can be used only in the if scope.
I What is a static variable?
TI DSP 2013 Crash course on C 5
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;
}
I What problems do you see in the above code (there are 2)?
I return keyword appears in function declared as void.
Variable i can be used only in the if scope.
I What is a static variable? Holds its value across function calls.
TI DSP 2013 Crash course on C 5
C Language
Structures
They can be initialized in one place and then passed via pointers:
// local function
// main program void compute_hist(ImageType * img,
// declare and init all unsigned int * hist) {
// fields to zero int x, y;
ImageData img = { 0 }; // local variable memset(hist, 0,
unsigned int histogram[256] = { 0 }; 256 * sizeof(unsigned int));
img.width = 720; // img->field is equivalent
img.height = 480; // to (*img).field
img.format = GRAYSCALE_FORMAT_8BIT; for (y = 0; y < img->height; y++)
img.pixels = GetFrameData(); // external for (x = 0; x < img->width; x++)
compute_hist( & img, histogram ); hist[ img->pixels[ k++ ] ]++;
}
Macros
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");
}
I Make sure you dont mix logical operators (|| and &&) with
bitwise operators (& and |).
I Try not to abuse the ?: operator. Make your code readable.
I Optimization tip: if statements are time consuming. Try to
avoid putting them in tight loops. Instead of
for (1) and (2) { if {1} else {2} } try to do
if {1} then for (1) else if {2} then for {2}
Loops For
Loops For
Loops For
Loops For
Loops While
Loops While
Loops While
Loops While
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;
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 */
Pointers 2 (arrays)
Memory allocation
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");
Question 1
Question 1
Question 2
Question 2
Question 3
Question 3
Question 4
Question 4
Question 5
Question 5
Question 5
Question 6
Question 6
Question 6
Question 6
Question 6
Question 6
Question 6
Question 7
I 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);
}
Question 7
I 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);
}
Question 8
Question 8
Question 9
Question 9
Question 10
Question 10
Question 11
Question 11
References