Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 C Programming Revision Malcolm Wilson

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

3 Keywords C has a small number of “keywords” http://tigcc.ticalc.org/doc/keywords.html

4 Standard I/O printf() scanf() Format specifiers %d, %f, %c, %s http://en.wikipedia.org/wiki/Printf http://en.wikipedia.org/wiki/Scanf

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

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

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

8 Control switch( myvar) case 1 : {code block break;} case 2: {code block break;} default {code block} http://msdn.microsoft.com/en- us/library/66k51h7a%28VS.80%29.aspx

9 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);

10 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) ;

11 Scope and storage class Used for AVR Static,will remain even after function has exited. Global Volatile, can be changed by unpredicable actions.

12 Preprocessor directives #include – “localfile” – /usr/include #define – #define WIDTH 80 – #define LENGTH ( WIDTH + 10 ) – #define u8 unsigned char

13 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.

14 Structures struct struct_name { structure_member;... } instance_1,instance_2 instance_n; OR struct struct_name instance_1,instance_2,instance3 After defining the structure. http://cprogramminglanguage.net/c-structure.aspx

15 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;

16 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.

17 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’;

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

19 sizeof() Used for malloc to allocate memory

20 Pointers and structures #include 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); }

21 argv and argc int main (int argc, char *argv[]) { } argc, argument count argv, argument vector

22 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}

23 Deep C Lvalues, Rvalues Inline functions Pointers to functions Pointers to pointers and multidimensional arrays.


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

Similar presentations


Ads by Google