Download presentation
Presentation is loading. Please wait.
Published byGrant Paul Modified over 9 years ago
1
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); } #include is a compiler directive to include (concatenate) another file main is the function where execution starts printf is a library function, in the stdio library
2
Slides created by: Professor Ian G. Harris C Syntax Basics Comments: single line //, multiline /* … */ Semicolon used to terminate all statements # indicates a compiler directive (include, define, etc.) Brackets { … } group lines of code for execution Blankspace between lines is not important in C Still need to separate tokens int a, b, c; a = b + c; int a,b,c;a=b+c; correct incorrect (inta)correct
3
Slides created by: Professor Ian G. Harris Variables All variables’ types must be declared Memory space requirements known statically Type checking can be performed at compile-time Numerical Types: int, float, double Other types: char, void Type qualifiers: short, long, unsigned, signed, const Specify alternate sizes, constrain use modes
4
Slides created by: Professor Ian G. Harris Types for PICC Compiler Type Size Arith type Type modifiers unsigned and signed bit1integer char8integer short16integer int16integer short long24integer long32integer float24real double24 or 32real
5
Slides created by: Professor Ian G. Harris Local vs. Global Scope of a variable is limited to the function where it is declared (malloc later) Local variables “disappear” when its function returns Global variables are declared outside of a function Globals do not disappear when a function returns int x; main () { int y; foo(); } foo() { int y;} Global scope Local to main Local to foo
6
Slides created by: Professor Ian G. Harris Volatile Variables The value of a volatile variable may change at any time, not just at an explicit assignment Compiler optimizations are not applied to volatile variables When can variables change without an explicit assignment? 1. Memory-mapped peripheral registers 2. Global variables modified by an interrupt service routine 3. Global variables accessed by multiple tasks within a multi- threaded application
7
Slides created by: Professor Ian G. Harris Volatile Example. while (*periph != 1); // wait until data transfer. // is complete. periph is the mapped address of the peripheral status info *periph is assigned by peripheral directly Compiled code will move memory contents to a register Memory will only be moved once because *periph does not change
8
Slides created by: Professor Ian G. Harris Types of Statements Assignment – = Relational -, ==, != Control Flow – if, for, while, do, switch Arithmetic Operations - +, -, *, /, % Logical Operations - &&, ||, ! Bitwise Logical Operations - &, |, ^
9
Slides created by: Professor Ian G. Harris Decimal, Binary Hexadecimal All numbers are internally represented in binary Decimal is the default representation in C Binary is often useful in embedded programming – Individual bits are important to see TRISA = 0b110011; – Notated with “0b” prefix Hexadecimal is succinct and matches binary 4x1 – Notated with “0x” prefix Ex. 39 = 0b00100111 = 0x27
10
Slides created by: Professor Ian G. Harris Logical Operators These operators accept binary input and produce binary output &&, ||, ! Non-binary inputs are cast to binary values int a=1, b=0; c=56; d=-1, res; res = a && b;// res = 0 res = a || b;// res = 1 res = c && d;// res = 1
11
Slides created by: Professor Ian G. Harris Bitwise Logical Operators These operators accept multi-bit inputs and return multi-bit results &, |, ^, > Operation is performed on each corresponding pair of bits char a = 0b00001111, b = 0b00111100; res = a & b;// res = 0b00001100 res = a | b;// res = 0b00111111 res = a ^ b;// res = 0b00110011 res = a << 4; // res = 0b11110000 res = b >> 4; // res = 0b00000011
12
Slides created by: Professor Ian G. Harris Bitlevel Manipulation Often need to control individual bits in registers Constants may be defined to refer to individual bits TRISA vs. TRISA0 PORTA vs. RA0 Sometimes more convenient to assign many bits at once PORTA = 0b000111; PORTA = 7; TRISA = 0b111000;
13
Slides created by: Professor Ian G. Harris Function Calls Functions enable simple code reuse Control moves to function, returns on completion Functions return only 1 value main() { int x; x = foo( 3, 4); printf(“%i\n”, x); } int foo(int x, int y) { return (x+y*3); }
14
Slides created by: Professor Ian G. Harris Function Prototypes main() { int x; x = foo(); } void foo() { printf (“Hi!\n”); } Function prototypes declare the return value type and the types of the arguments Allows the compiler to do type checking Default return type is int if no prototype is provided void foo();
15
Slides created by: Professor Ian G. Harris Function Call Overhead main() { int x; x = foo(2); printf(“%i\n”, x); } int foo(int x) { int y=3; return (x+y*3); } Program counter value needs to be restored after call Local variables are stored on the stack Function calls place arguments and return address on the stack 20: 21: 22: 30: 31: 103: 3local var 102: 2argument 101: 21return addr 100: 2local var
16
Slides created by: Professor Ian G. Harris Header Files Files included at the top of a code file Traditionally named with.h suffix Include information to be shared between files Function prototypes extern s of global variables Global #define s Needed to refer to libraries
17
Slides created by: Professor Ian G. Harris Type Casts Tell the compiler to treat a variable as a different type Allows typing rules to be violated May change the space and functional requirements int x=5, y=3; float z; z = x / y; z = (float) x / (float) y; If x and y are integers then x/y = 1 If x and y are floats then x/y = 1.66 Need a floating point divider
18
Slides created by: Professor Ian G. Harris Strings Strings are arrays of chars A char is the ASCII value of the corresponding character Double quotes used for strings, single quotes for chars Strings need a '\0' terminator char achar, astr[13]; achar = 'a'; strncpy(astr, “Hello world!”, 13);
19
Slides created by: Professor Ian G. Harris Pointers c is a char. Size of c is the size of a char & is the reference operator &c is pointer to c, its address in memory Size of &c is the size of an address *d is a char * is the dereference operator d is the pointer to *d char c; c = 'a'; char *d; *d = 'a';
20
Slides created by: Professor Ian G. Harris Pointer Arithmetic Arithmetic can be performed on pointers Useful to explore known blocks of memory like arrays Arrays are just pointers to contiguous memory blocks int varr[10]; printf (“%i”, *varr++); printf (“%i”, *varr); // First elt of array // Second elt of array
21
Slides created by: Professor Ian G. Harris Call by Reference A pointer can be passed as an argument to a function The called function can directly manipulate the variable main () { int x=0; foo(&x); printf (“%i\n”, x); } foo (int *var) { *var = 10; } Normally a called function cannot access the caller's scope
22
Slides created by: Professor Ian G. Harris Structures A collection of variables under a single name Each grouped variable has its own name A record with several fields Pre-object oriented method of organization typedef struct { char name[20]; int age; } person; person harris; Size of structure is the sum of its component fields
23
Slides created by: Professor Ian G. Harris Accessing Structure Data person p1, *p2; printf (“%i”, p1.age); printf (“%i”, *p2.age); printf (“%i”, p2->age); . operator returns the value of the member -> operator dereferences structure pointer first Structures are often passed by reference, so - > is useful
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.