c programming revision malcolm wilson. variables types int, char, double, long. no type for string...

23
C Programming Revision Malcolm Wilson

Upload: horace-jordan

Post on 15-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

C Programming Revision

Malcolm Wilson

Page 2: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Variables

• Types int, char, double, long. NO type for string see later.

• unsigned above.• assignment

X=2 ;C=‘v’;

Page 3: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Keywords

• C has a small number of “keywords”

• http://tigcc.ticalc.org/doc/keywords.html

Page 4: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Standard I/O

• printf()

• scanf()

• Format specifiers %d, %f, %c, %s

http://en.wikipedia.org/wiki/Printfhttp://en.wikipedia.org/wiki/Scanf

Page 5: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Operations

• +, -, *, /, %, ^• Operation dependent on variable type• Try some

Page 6: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Boolean

• Any value other than zero is true.• Watch out for == “is equal to”.

Page 7: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Control

for(i=0, i<9, i++){ code block}

while (x<8){code block}

if (y==2){code block}

elseif(y==7){code block}

else{code block}

Page 8: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Control

switch( myvar)case 1 :

{code blockbreak;}

case 2:{code blockbreak;}

default{code block}

http://msdn.microsoft.com/en-us/library/66k51h7a%28VS.80%29.aspx

Page 9: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Control

Code block is surrounded by {} if more than one line. Don’t need {} if code one line long.

eg for(i=0; i<5; i++)printf(“ number is: %d /n”, i);

Page 10: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Functions and Prototypes

• C is composed of functions, and must have at least one function called main().

• Functions accept parameters and return values

• A “prototype” should be written which indicates what data types a function should accept and return.– Eg int mynumberfunction( int num1, int num2) ;

Page 11: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Scope and storage class

• Used for AVR• Static ,will remain even after function has

exited.• Global• Volatile , can be changed by unpredicable

actions.

Page 12: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Preprocessor directives

• #include– “localfile”– <standard_locations> /usr/include

• #define– #define WIDTH 80 – #define LENGTH ( WIDTH + 10 ) – #define u8 unsigned char

Page 13: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Arrays and strings

• int myarray[5];• int myarray[5]={1, 2, 3};• int myarray[]={1,2,3,4,5};• char mychararray=“malcolm”;• A string is a “null terminated” char array.

Page 14: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Structures

struct struct_name { structure_member; ... } instance_1,instance_2 instance_n;ORstruct struct_name

instance_1,instance_2 ,instance3 After defining the structure.http://cprogramminglanguage.net/c-structure.aspx

Page 15: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Structures

• Using typedef to avoid struct structurename all the time.• typedef struct{ unsigned int house_number; char street_name[50]; int zip_code; char country[50]; } address; address billing_addr; address shipping_addr;

Page 16: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Pointers

• Declared as using *• int *p says p in a pointer to an integer.• p points to the memory location where a

integer is stored.• Confusing , in the code. *p means the

contents of memory location p.• And &p is the memory address of p.

Page 17: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Pointers and arrays

• myarray is the same as &myarray[0] • So if an array is initialised as char

name[]=“malcolm”;• *(name+3) will be ‘c’;

Page 18: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Dynamic memory allocation

• Allocates memory on the “heap”• malloc(n)• calloc(s, nbytes) intialises memory• free();

Page 19: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

sizeof()

• Used for malloc to allocate memory

Page 20: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Pointers and structures#include <stdio.h>#include <stdlib.h>

main(){•printf("hello world \n");•struct mystruct{•int age;•char buffer[20];•}mydata;•mydata.age=45;•printf("age is %d \n", mydata.age);

•struct mystruct * d;•d=malloc(sizeof(struct mystruct));•d->age=53;•printf("pointed age is %d \n", d->age);}

Page 21: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

argv and argc

• int main (int argc, char *argv[]) { }

• argc , argument count• argv , argument vector

Page 22: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Boo Boo’s in C

• Forgetting the semicolon• Using = in a boolean expression instead of ==.• Completing a loop with no code being

executed.– while(test);

{ code block}

Page 23: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Deep C

• Lvalues, Rvalues• Inline functions• Pointers to functions• Pointers to pointers and multidimensional

arrays.