C Programming
C Programming
Outline
Resources C versus C++ and Java Compilers and Environments Makefiles
Libraries Directories Dependencies
Outline
Structures Typedefs Bitwise and Logical
==, &&, ||, != &, |, xor?
Outline
Resources Differences between Matlab, C, C++, Java Setup and Compiling Syntax
Ask lots of questions. There will be time at end for specific topics I do not cover in my lecture
Resources
The C Programming Language -- ANSI C
By Brian W. C. Kernighan & Dennis M. Ritchie K and R
"Practical C programming"
By Steve Oualline
C vs. Matlab
Matlab uses script language = no compiling Matlab can dynamically allocate variables and memory
> x=5 > y = x + 2.5 > x = 2.5 > x = [0.5; 0.4; 1.2];
Methods
Functions are part of object type Can access object variables or be passed variables
Inheritance
Can inherit variables and methods from parent class
E.g. Animal class has variable color Therefore Dog class has variable color if subclass of Animal
Programming Environments
Unix/Linux typically come with gcc
Standard stuff Makefiles
The following instructions regarding compiling are for command line compilers like gcc, which come with Linux/Unix
Microsoft Developer Studio, etc, use auto-generated Makefiles from menu items
fname.o
links with the library libname.a directs gcc to look for libraries in directory, in addition to the standard system library path
Makefile
A makefile is used to specify parameters for one or more set of files
File is called Makefile From directory with Makefile
C:/home/> make C:/home/> gmake
Makefile
Variable Definitions
CFLAGS = -g -Wall SRCS = main.c file1.c file2.c CC = gcc main.o: main.c main.h gcc -g -Wall -c main.c CFLAGS = -g Wall
Dependency Rules
main.o is created if main.c or main.h has changes In order to compile main.o the second line is used as command Second line must have a tab
Comments
# A number sign denotes a comment
Makefile - Example
# top-level rule to compile the whole program. all: prog # program is made of several source files. prog: main.o file1.o gcc main.o file1.o -o prog # rule for file "main.o". main.o: main.c file1.h gcc -g -Wall -c main.c # rule for file "file1.o". file1.o: file1.c file1.h gcc -g -Wall -c file1.c # rule for cleaning files generated during compilations. clean: <- can have rule for other /bin/rm -f prog main.o file1.o file2.o commands besides compiling <- uses first rule if no target specified <- object can be dependent on more than one file <- object can be dependent on header files
CC = gcc LD = gcc CFLAGS = -g -Wall LDFLAGS = RM = /bin/rm -f OBJS = main.o file1.o PROG = UAVFlight LIBS = mathutil, camcal LIBDIRS = /home/utils/ # top-level rule, to compile everything. all: $(PROG) # rule to link the program $(PROG): $(OBJS) $(LD) $(LDFLAGS) $(OBJS) -o $(PROG) # rule for file "main.o". main.o: main.c file1.h $(CC) $(CFLAGS) -c main.c
# rule for file "file1.o". file1.o: file1.c file1.h $(CC) $(CFLAGS) -c file1.c l$(LIBS) L$(LIBDIRS) # rule for cleaning re-compilable files. clean: $(RM) $(PROG) $(OBJS)
Makefiles
More information online at
http://users.actcom.co.il/~choo/lupg/tutorials/writing-makefiles/writing-makefiles.html
Header Files
Header files allow your code to know about other functions provided by other code, e.g libraries, other .c files, or standard libraries
#include <math.h> #include myfile1.h <- brackets indicate standard library <- filename can be given if directory listed in Makefile <- Otherwise full path required
#include C:\home\code\myfile2.h
Comments
Use comments to leave notes to self and others about code functionality
// double slash marks a comment of a single line /* The slash then star marks the beginning of a multi-line comment while the star then slash marks the end of the comment */
Programming classes will teach you that one function should have 3-10 lines total
Otherwise decompose into smaller functions
void main () { InitializeRobot(world_map); while(robot==on) { DriveRobot(world_map); } ShutdownRobot(); }
Structures
Structures are a way of bundling data together, especially different types
Similar to C++ or Java classes, but not as powerful
Structures
Can use pointers to structures
Dereferenced differently
void main() { struct image_feature *object_ptr; object_ptr->x = 5.0; object_ptr->y = 6.0; object_ptr->size = 8;
Enum allows you to create a list of numbers and assign them variable names
enum e_data_pkt { GPS=1, IMU, SPEED, ACK, NAK}; typedef enum e_data_pkt pkt_type; current_data.pkt_type = IMU;
Syntax
All C programs begin with a main function
Windows based programs slightly different
void main() { printf(Hello World\n); }
Syntax
All lines end with a semicolon
void main() { int x,y; x = 3*y*y + 5*y + 25; } <= Equivalent => void main() { int x,y; x = 3*y*y + 5*y + 25; }
Variable types
Variables must be created and typed
Cannot dynamically create variables like in MATLAB
MATLAB: > x=5 > y = x + 2.5 > x = 2.5 C: void main() { int x; float z; x = 5; x = 6*x; z = 2.5*x; x = 6.0*x;
Symbols
Do not confuse logical symbols with others
== ! != && || = ~ & | ^ << >> logical equal logical not Not equal logical and logical or if (x==5) if (!flag) if (x!=5) if((x==5)&&(y==2)) if((x==5)||(y==2))
assignment x=5 bitwise not ~0xf0 bitwise and 0x3e & 0xf0 bitwise or 0x3e | 0xf0 bitwise xor (exclusive or) 0x3e ^ 0xf0 bitwise left shift 0x3e << 2 bitwise right shift 0x3e >> 2
<=
Assignment
Standard arithmetic operators (+,-) can be combined with assignment (=)
int x,y,z; x = 5; x =+ 6; // x now equals 11 (5 + 6) x =- 10; // space matters, x now equals 1 (11 10) x = -10; // x now equals -10 x++; //x now equals -9 (-10 + 1)
This is an issues if data is written to files or sent over a network Certain formats (e.g. jpeg) require specific formats
0 1 2 3
Big Endian Base Address+0 Base Address+1 Base Address+2 Base Address+3
3 2 1 0
Recommendations
Copy old Makefiles dont start from scratch Use #defines and enums
Avoid magic numbers that have no context
Variable and function names must be meaningful There is no such thing as too many comments
You might forget after several months what is supposed to happen or your teammates may need to make changes in your absence
Break code down into different functions and files that can be compiled and tested independently
Last Words
Use command line arguments as opposed to recompiling
Especially for parameters that are changed frequently
The computer will always do what you tell it to do. - Its just not always what you want it to do!
Programming is 10% coding, 90% debugging. -Dont expect your code to work the first time.