+ All Categories
Home > Documents > Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello,...

Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello,...

Date post: 20-Jan-2016
Category:
Upload: grant-paul
View: 216 times
Download: 0 times
Share this document with a friend
23
Slides created by: Professor Ian G. Harris Hello World #include <stdio.h> main() { printf(“Hello, world.\n”); } lude is a compiler directive to include (concatenate) another is the function where execution starts tf is a library function, in the stdio library
Transcript
Page 1: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Hello World

#include <stdio.h>

main() {printf(“Hello, world.\n”);

}

#include is a compiler directive to include (concatenate) another filemain is the function where execution startsprintf is a library function, in the stdio library

Page 2: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 executionBlankspace 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; inta,b,c;a=b+c;

correct incorrect (inta)correct

Page 3: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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, doubleOther types: char, voidType qualifiers: short, long, unsigned, signed, const

•Specify alternate sizes, constrain use modes

Page 4: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Types for PICC Compiler

Type Size Arith type

Type modifiers unsigned and signed

bit 1 integer

char 8 integer

short 16 integer

int 16 integer

short long 24 integer

long 32 integer

float 24 real

double 24 or 32 real

Page 5: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 returnsGlobal 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

Page 6: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 assignmentCompiler 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

Page 7: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 registerMemory will only be moved once because *periph does not change

Page 8: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Types of Statements

Assignment – =Relational - <, >, ==, !=Control Flow – if, for, while, do, switchArithmetic Operations - +, -, *, /, %Logical Operations - &&, ||, !Bitwise Logical Operations - &, |, ^

Page 9: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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

Page 10: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 = 0res = a || b; // res = 1res = c && d; // res = 1

Page 11: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 = 0b00001100res = a | b; // res = 0b00111111res = a ^ b; // res = 0b00110011res = a << 4; // res = 0b11110000res = b >> 4; // res = 0b00000011

Page 12: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Bitlevel Manipulation

Often need to control individual bits in registersConstants may be defined to refer to individual bits

TRISA vs. TRISA0PORTA vs. RA0

Sometimes more convenient to assign many bits at oncePORTA = 0b000111;PORTA = 7;TRISA = 0b111000;

Page 13: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Function Calls

Functions enable simple code reuseControl moves to function, returns on completionFunctions 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); }

Page 14: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 argumentsAllows the compiler to do type checkingDefault return type is int if no prototype is provided

void foo();

Page 15: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 callLocal variables are stored on the stackFunction calls place arguments and return address on the stack

20:21:22:

30:31:

103: 3 local var

102: 2 argument

101: 21 return addr

100: 2 local var

Page 16: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Header Files

Files included at the top of a code fileTraditionally named with .h suffixInclude information to be shared between files

• Function prototypes• externs of global variables• Global #defines

Needed to refer to libraries

Page 17: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Type Casts

Tell the compiler to treat a variable as a different typeAllows typing rules to be violatedMay 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 = 1If x and y are floats then x/y = 1.66Need a floating point divider

Page 18: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Strings

Strings are arrays of charsA char is the ASCII value of the corresponding characterDouble quotes used for strings, single quotes for chars

Strings need a '\0' terminator

char achar, astr[13];achar = 'a';strncpy(astr, “Hello world!”, 13);

Page 19: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 memorySize of &c is the size of an address

*d is a char* is the dereference operatord is the pointer to *d

char c; c = 'a';

char *d; *d = 'a';

Page 20: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Pointer Arithmetic

Arithmetic can be performed on pointers Useful to explore known blocks of memory like arraysArrays are just pointers to contiguous memory blocks

int varr[10];printf (“%i”, *varr++);printf (“%i”, *varr);

// First elt of array// Second elt of array

Page 21: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Call by Reference

A pointer can be passed as an argument to a functionThe 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

Page 22: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides created by: Professor Ian G. Harris

Structures

A collection of variables under a single nameEach grouped variable has its own nameA record with several fieldsPre-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

Page 23: Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

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 firstStructures are often passed by reference, so - > is useful


Recommended