EE 472 Embedded Systems Dr. Shwetak Patel Assistant Professor Computer Science & Engineering Electrical Engineering Agenda Announcements C programming intro + pointers Shwetak N. Patel - EE 472 2 Programming Languages C Compilation Process Source -> Preprocessor -> Compiler -> Assembler -> Linker -> Loader Shwetak N. Patel - EE 472 3 Shwetak N. Patel - EE 472 4 Page 1
Example Program in C Loops #include <stdio.h> // this imports a definition file (a header). int main() // note the return type of main: 0 means ok { int x=5; printf("local = %d", x); return 0; // return success for(int i = 0; i < 100; i++){ int i = 0; while(i < 100){ i++; Shwetak N. Patel - EE 472 5 Shwetak N. Patel - EE 472 6 Functions int processdata(int X){ return (X*X)/2; Shwetak N. Patel - EE 472 7 C types char short java 16 bits c 8 bits 16 bits 16 bits int 16,32 or 64 32 bits bits long 64 bits 32 or 64 bits float 32 bits 32 bits double 64 bits 64 bits boolean 1 bit 1 bit or int byte 8 bits use char long long N/A 64 bits long double N/A 80,96 or 128 bits Shwetak N. Patel - EE 472 8 Page 2
Unsigned types Boolean Operators with Integer types int i1; // range -2,147,483,648 to 2,147,483,647 signed int i2; // range -2,147,483,648 to 2,147,483,647 unsigned int i2; // range 0 to 4,294,967,295 967 295 singed: -2 n-1 to 2 n-1-1 unsigned: 0 to 2 n-1-1 1 && 1 == 1 1 && 0 == 0 1&&-3 ==1 7 25 == 1!34 == 0 Shwetak N. Patel - EE 472 9 Shwetak N. Patel - EE 472 10 Visibility and Lifetime Global vs Local int counter; main () { int foo = 2; // not visible outside of this function int glob = 2; // not visible outside of this function for (int i=3;...) { int global; // outside function void somefunction(void) { int local; // within function Shwetak N. Patel - EE 472 11 Shwetak N. Patel - EE 472 12 Page 3
Extern and Static Constants /* fileone.c: */ const float PI = 3.14; //have to declare and initialize int global = 2; // defines a global integer variable static int private = 5; // all functions in fileone can use // Not visible outside of file /* filetwo.c: */ extern int global; // declares that filetwo.c wants to use the // variable defined in fileone.c static int private = 7; // A new variable only visible within filetwo.c int main(){ float r = 10.0; float area = PI * r * r; void function(void) { int local = global * private; // local variable == 2*7 Shwetak N. Patel - EE 472 13 Shwetak N. Patel - EE 472 14 #define #define vs const #define PI 3.14 int main(){ float r = 10.0; float area = PI * r * r; #define handled by preprocessor Straight text replacement (think search & replace) #const handled by compiler Type checking Shwetak N. Patel - EE 472 15 Shwetak N. Patel - EE 472 16 Page 4
Other preprocessor directive Why use preprocessor #ifdef #endif #define LINUX 1 //#define WINDOWS 1... Debugging Multi platform code Others? #ifdef WINDOWS... windows -specific code... #endif #ifdef LINUX... linux -specific code... #endif Shwetak N. Patel - EE 472 17 Shwetak N. Patel - EE 472 18 Class Exercise Class Exercise Shwetak N. Patel - EE 472 19 Shwetak N. Patel - EE 472 20 Page 5
typedef typedef defines a custom type (synonym) Usage: typedef type synonym ex: typedef unsigned int myint; myint x = 100; type conversion char c = b ; int i = (int) c; unsigned short x = 100; int i2 = (int) x; Shwetak N. Patel - EE 472 21 Shwetak N. Patel - EE 472 22 Pointers and Memory Memory Warning: Sleepy eye syndrome Shwetak N. Patel - EE 472 23 Shwetak N. Patel - EE 472 24 Page 6
Getting the address of a variable Pointers &mydata0; //read address of mydata0 Where do we put the address? Pointer - just a variable that holds a memory address int* mydata0ptr = &mydata0; 1) mydata0ptr variable name 2) is a pointer - * specifies that 3) to an integer int specfies that 4) mydata0ptr is an pointer to an integer Shwetak N. Patel - EE 472 25 Shwetak N. Patel - EE 472 26 dereferencing How do we get the value of what the pointer points to? int mydata1 = *mydata0ptr Shwetak N. Patel - EE 472 27 Shwetak N. Patel - EE 472 28 Page 7
Class Exercise Shwetak N. Patel - EE 472 29 Shwetak N. Patel - EE 472 30 C Development Options C Development Options Note: This is for building apps for your PC to practice your C programming Mac OSX: Xcode (gcc compiler) Free download includes IDE (integrated development environment) Windows: Option 1: Visual Studio (includes C compiler and IDE) Option 2: Install Cygwin and use gcc from command line Option 3: Install Cygwin and use Eclipse Others: Netbeans, etc Linux: gcc included Use any IDE (eclipse, emacs, etc) Shwetak N. Patel - EE 472 31 Shwetak N. Patel - EE 472 32 Page 8
Eclipse and C in Windows Questions? Install Cygwin Make sure to install the gcc-core: C compiler and make: The GNU version packages under devl Set your path in windows to c:\cygwin\bin Install Eclipse UDE for C/C++ Developers Next time: Tutorial on make platform More C practice Structures and Representing Data Bitwise Operations Create new sample C application Shwetak N. Patel - EE 472 33 Shwetak N. Patel - EE 472 34 Page 9