1 / 60

Introduction to Programming in C Part II

Introduction to Programming in C Part II. Recap. char ch; /* Byte to hold a character */ int a, b; /* Integer variables */ float theta; /* Floating point variables */ float value=1.2; /* Floating point variable & */ /* initialisation */

lilika
Télécharger la présentation

Introduction to Programming in C Part II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming in CPart II

  2. Recap char ch; /* Byte to hold a character */ int a, b; /* Integer variables */ float theta; /* Floating point variables */ float value=1.2; /* Floating point variable & */ /* initialisation */ ch = ‘c’; /* Single quotes for character */ /* constant */ value = 1.2; a = 1; theta = a; /* Automatic type conversion or */ /* “promotion” */ b = (int)value; /* Forced conversion – “cast” */ Variable declarations

  3. Variable Declarations int array[10]; /* array of integers */ /* Initialisation and dimensioning of integer array*/ int array[ ] ={1, 2, 5, 8, 10}; /* Initialisation of dimensioned integer array*/ int array[20] ={1, 2, 5, 8, 10}; float map[2][3]; /Two dimensional array of floats*/ /Two dimensional array of pointers to floats */ float *map[2][3]; Array declarations

  4. String declarations char *message1; /* Pointer to a character */ /* Pointer to char with initialisation of pointer*/ char *message2=“Welcome”; char message3[40]; /* Array of characters */ /* Initialisation and dimensioning */ /* of an array of characters*/ char message4[ ]=“Welcome”; /* Initialisation of dimensioned */ /* array of characters */ char message5[40]=“Welcome”;

  5. Function Declarations int add(int x, int y) { return (x + y); } Function body void add(int x, int y, int *ptr_sum) { *ptr_sum = x + y; } Function prototype int add(int x, int y); void add(int x, int y, int *ptr_sum);

  6. Iteration Constructs for(i=0, sum=0; i<10; i++) sum=sum+i; i=0; sum = list[i++]; sumsq = sum*sum; while i<10 {sum += list[i]; sumsq += list[i] * list[i]; i++; } int value; do { printf(“Enter positive value: “); scanf(“%d”, &value); }while (value < 0);

  7. Flow Control int value; if (value < min) min = value; else if(value > max) max = value; int value; scanf(“%d”, &value); switch (flag) { case 1: printf(“value of flag is 1\n”); break; case 2: case 3: printf(“value of flag is 2 or 3\n”); break; default: printf(“\a\a\a\a”); }

  8. Separate Compilation • Functions • For repeated operations in a single application program • Divide the application into easily understood parts • Libraries • For software used in multiple application programs • Many standard and proprietary libraries exist • You can make your own libraries

  9. Header File: name.h /* Macro definitions Function prototypes and * /* global variables*/ int add(int x, int y); int subtract(int x, int y); print_matrix(int A[ROWS][ ], int rows, int cols); “Body” File: name.c #include <stdio.h> #include “name.h” int add(int x, int y) { return (x + y); } int subtract(int x, int y) {return (x-y); } void print_matrix(int A[ROWS][ ], int rows, int cols) { for (i=0; …. for (j=0; … printf(“………

  10. Library name.c Compiler name.obj Application name.h Linker myprog.exe Compiler myprog.c myprog.obj Application program: myprog.c #include <stdio.h> #include “name.h” #define MYROWS 5 #defime MYCOLS 5 int main(void) { int i, j; int A[MYROWS][MYCOLS]; for(i=0; …. for(j=0 … scanf(“%d”, &A[i][j]); print_matrix(A, MYROWS, MYCOLS); }

  11. R1 33.0 Ohms R2 33.0 Ohms 1 3 2 R3 33.0 Ohms structure tag (name) 0 field Structured Data Types Concept • A way to collect together items of variant type • E.g. a string, two integers and a floating point value struct comp {char id[10]; int node1, node2; float value; }; Or struct comp {char type; int id; int node1, node2; float value; }; Comp. id Connect Value R1 1 2 33.0

  12. variable definitions struct {char id[10]; int node1, node2; float value; }R1, R2; { strcpy(R1.id, “R1”); R1.node1 = 1; R1.node2 = 2; R1.value = 33.0; strcpy(R2.id, “R2”); : : struct {char type; int id; int node1, node2; float value; }R1, R2; { R1.type = ‘R’; R1.id = 1; R1.node1 = 1; R1.node2 = 2; R1.value = 33.0; R2 = R1 R2.id = 2; : :

  13. User Defined Data Types Concept • To create meaningful names for data types such as: int, short int • To abbreviate data type names such as: “struct comp” typedef float ins; typedef unsigned int count; ins width; count No_comps;

  14. structure declaration type declaration struct complex {float real; float imag; }; struct complex v1; typedef struct {float real; float imag; } Complex; Complex v1; A structure definition CANNOT appear in a function argument list

  15. Structures as Function Arguments #include <math.h> struct cart {float x, y;}; struct polar {float r, theta;}; void cart_to_polar(struct cart val, struct polar *pol); int main(void) {struct cart va; struct polar vb; va.x = 10; va.y = 10; cart_to_polar(va, &vb); return 0; } void cart_to_polar(struct cart val, struct polar *pol) {pol->r = (float)sqrt(pow(val.x,2) + pow(val.y, 2) ); pol->theta = (float)atan(val.y / val.x); }

  16. Return a Structure Value #include <math.h> struct cart {float x, y;}; struct polar {float r, theta;}; struct polar cart_to_polar(struct cart val); int main(void) {struct cart va; struct polar vb; va.x = 10; va.y = 10; vb = cart_to_polar(va); return 0; } struct polar cart_to_polar(struct cart val) {struct polar temp; temp.r = (float)sqrt(pow(val.x,2) + pow(val.y, 2) ); temp.theta = (float)atan(val.y / val.x); return temp; }

  17. Returning Address of Structure #include <math.h> #include <stdlib.h> struct cart {float x, y;}; struct polar {float r, theta;}; struct polar *ptr_cart_to_polar(struct cart val); int main(void) {struct cart va; struct polar *ptr_vb; va.x = 10; va.y = 10; ptr_vb = cart_to_polar(va); free(ptr_vb); return 0; } struct polar *cart_to_polar(struct cart val) {struct polar *ptr_temp; ptr_temp=(struct polar *)malloc(sizeof(struct polar)); ptr_temp->r = (float)sqrt(pow(val.x,2)+pow(val.y,2)); ptr_temp->theta = (float)atan(val.y / val.x); return ptr_temp; }

  18. Accessing Structure Fields: Summary The -> Operator structure_address->structure_field Is shorthand for: (*structure_address).structure_field

  19. Unions Allows different data types to share the same memory union unknown {int i; float r; }data; Occupies space required by largest field specifier Integer filed accessed as: data.i Floating point field accessed as: data.r

  20. #define INT 0 #define FLOAT 1 struct item {int type; union {int i; float r; } }data; : data.type = INT; : : if (data.type == INT) scanf(“%d”,&data.i); if (data.type == FLOAT) scanf(%f”, &data.r); : Need a tag to identify how the union should be used:

  21. Enumerated Data Types Concept • To define symbolic names for values of a variable • Useful as tags for unions and options in switch statements 0 1 2 3 enum day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; enum day today; today = Sunday; while (1==1) { if( (today > Sunday && (today < Saturday)) printf(“Working day\n”); if(today == Saturday) today = Sunday; else today++; } : 4 5 6

  22. Can specify the values of each constant: enum day {Sunday=1, Monday=2, Tuesday=4, Wednesday=8, Thursday=16, Friday=32, Saturday=64}; typedef enum {INT, FLOAT}d_type; struct item {d_type type; union {int i; float r; } }data; : data.type = INT; : : : if (data.type == INT) scanf(“%d”,&data.i); if (data.type == FLOAT) scanf(%f”, &data.r); : switch(data.type) {case INT: scanf(“%d”,&data.i); break; case FLOAT: scanf(%f”, &data.r); break; }

  23. Memory Allocation Creating a 1-D dynamic array: #include <stdio.h> #include <stdlib.h> int i, n, *ptr_list; printf(“Number of entries in list is: “); scanf(“%d”, &n); ptr_list = (int *) calloc(n, sizeof(*ptr_list)); for(i=0; i<n; i++) { printf(“Enter value for list[%d]: “, i); scanf(“%d”, &ptr_list[i]); } Must remember to release the memory allocated

  24. int * int * int int * int int 2-D Dynamic Array Concept An array of pointers to pointers to integers int **

  25. Pointer to a pointer to an integer List of integer pointers Each one is a list of integers 2-D Dynamic Array Is an array of pointers to arrays: #include <stdio.h> #include <stdlib.h> int i, rows, cols, **ptr_ptr_list; printf(“Enter number of rows: “); scanf(“%d”, &rows); printf(“Enter number of cols: “); scanf(“%d”, &cols); ptr_ptr_list = (int **)calloc(rows, sizeof(*ptr_ptr_list)); for(i=0; i<rows; i++) { ptr_ptr_list[i]=(int *)calloc(cols, sizeof(**ptr_ptr_list)); }

  26. Improved Data Encapsulation Need to carry the array dimensions and, perhaps, the present index with the dynamic array Use a structured data type typedef struct {int rows, cols, **data;} dyn_2_array; dyn_2_array map; map.rows =10; map.cols = 10; map.data=(int **)calloc(map.rows,sizeof(*map.data)); for (i=0; i<map.rows; i++) map.data[i]=(int *)calloc(map.cols, sizeof(**map.data));

  27. Head of List next next next NULL tag tag tag data data data Linked Lists • Single Linked List • Sequential access • Basic operations • Create new list Find specified node • Add node (at head) Read from specified node • Next node Preceding Node • Insert at specified position Delete specified node

  28. Create Single Linked List typedef struct node {struct node *next; int id; void *data; }Node; Use an empty cell for the head of the list: Node *new_list(void) { Node *ptr_head; ptr_head = (Node *)malloc(sizeof(Node)); ptr_head->next = (Node *)NULL; ptr_head->id = -1; ptr_head->data = (void *)NULL; return ptr_head; }

  29. Head of List next next NULL tag tag data data Add to List • Create new node • Point next field of new node at current head of list • Point head of list at new node • Insert data Node *add_node(Node *ptr_head, void *data) { Node *ptr_temp; ptr_temp = (Node *)malloc(sizeof(Node)); ptr_temp->next = ptr_head; ptr_temp->id = ptr_head->id + 1; ptr_temp->data = data; return (ptr_temp); } 1 2 4 3 3 2 next 1 tag data 4

  30. Using “add_node” Node Ptr_Head; int *data; Ptr_Head = new_list(); data = (int *)calloc(5, sizeof(int)); data[0]=1; data[1]=1; data[2]=2; data[3]=3; data[4]=4; Ptr_Head = add_node(Ptr_Head, (void *)data);

  31. Find & Read Node Node *find_node(Node *ptr_head, int id) { while( (ptr_head != NULL) && (ptr_head->id != id) ) ptr_head=ptr_head->next; return (ptr_head); }` void *read_node(Node *ptr_head, int id) { Node *ptr_selected; ptr_selected = find_node(ptr_head, id); return (ptr_selected->data); }

  32. Preceding & Next Node Node *preceding_node(Node *ptr_head, int id) { while( (ptr_head->next != NULL) && (ptr_head->next->id != id) ) ptr_head=ptr_head->next; return (ptr_head); } Node *next_node(Node current) { return (current.next); }

  33. Insert at Position void insert_node( Node *ptr_head, int target_id, int id, void *data) { Node *ptr_selected, *ptr_temp; ptr_selected = find_node(ptr_head, id); ptr_selected = preceding_node(ptr_head, ptr_selected->id); ptr_temp = (Node *)malloc(sizeof(Node)); ptr_temp->next = ptr_selected->next; ptr_temp->id = target_id; ptr_temp->data = data; ptr_selected->next = ptr_temp; }

  34. 1 2 1 Head of List 2 next next next NULL tag tag tag data data data Delete Node` void delete_node(Node *ptr_head, int id) { Node *ptr_preceding, *ptr_selected; ptr_selected = find_node(ptr_head, id); ptr_preceding = ptr_preceding_node(ptr_head, ptr_selected->id); ptr_preceding->next = ptr_selected->next; free(ptr_selected->data); free(ptr_selected); }

  35. More Dynamic Data Structures • Double linked lists • Pointers forwards and backwards • Trees • Binary • Red-black • Circular buffers (not dynamic) • Fixed size read/write buffer • Start and end pointers rotate

  36. * A + * F + * C E B D Parse tree for: A*(((B+C)*(D*E))+F) Built by: put operator at root, first operand expressionon left and second on right Reading pre-order (root, left, right) => prefix notation *A+*+BC*DEF Reading in-order (left, root, right) => infix (as original) Reading post-order (left, right, root) => postfix ABC+DE**F+*

  37. expression expression expression left left left right right right expression expression expression expression left (NULL) left (NULL) left (NULL) left (NULL) right (NULL) right (NULL) right (NULL) right (NULL) Binary Tree

  38. Factorial Using Recursion long factorial(long n) { long fact; if (n > 1L) fact = n * factorial(n-1); else fact = 1L; return (fact); } int main(void) { long num=10; printf(%ld! = %ld\n”, num, factorial(num)); return 0; }

  39. Factorial Using Iteration long factorial(long n) { long result = 1L; while (N > 1L) result *= n--; return (result); } int main(void) { long num=10; printf(%ld! = %ld\n”, num, factorial(num)); return 0; }

  40. The User Manual • The user DOES NOT • Need the source file • Need to know how to compile a program • The user DOES • Need to know how to install the program • Need to know what resources are needed (file space, data space, screen resolution etc) • How to input data (what the prompts are and the data format) • How the results will be presented • How the program will respond to unexpected input and how to respond to the consequences of incorrect data input

  41. Advanced Programming Techniques

  42. Storage Class Storage classes: • auto: • Declaration is valid to the end of the present block • Default storage class • extern: • Variable or function is exported to the compiler only • The space allocating definition is elsewhere • Must be declared at the head of a block • Default for a variable defined at the file level • register: • Can be assigned to a register • Cannot compute the address

  43. static: A function or a variable is not exported to the linker. The definition is local to the file in which it occurs The stored value is valid the next time the function is called Makes them private to the object file • volatile: Prevents the compiler from allocating the variable to a register auto int value; extern int value; static int value; register int value; volatile int value; Global variables • Any variable declared outside the body of function • Any variable defined as extern • Should be avoided where ever possible

  44. Function Pointer to function Functions as Arguments int (*fp)(int i, int i); Function prototype assigned to the pointer • Pointer to a function int proto(int i, int i); fp = proto; Pointer declaration initialised with function address int (*fp)(int i, int i)=proto; • Function as an argument: Argument is a pointer to the function “operator” void process_list(int list, int (*my_operator)(int)); Argument is the function “operator” void process_list(int list, int (my_operator)(int));

  45. #include <stdio.h> #define SIZE 10 int (offset)(int list[ ], int index, int value); void process_list(int list[ ], int value, int (my_operator)(int *, int, int)); int main(void) {int i, list[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; printf("Initial list values are:\n"); for(i=0; i < SIZE; i+=5) printf("list[%d]=%d list[%d]=%d list[%d]=%d list[%d]=%d list[%d]=%d\n",i,list[i],i+1,list[i+1], i+2,list[i+2],i+3,list[i+3],i+4,list[i+4]); process_list(list, 5, offset); printf("\n\nOffset list values are:\n"); for(i=0; i < SIZE; i+=5) printf("list[%d]=%d list[%d]=%d list[%d]=%d list[%d]=%d list[%d]=%d\n",i,list[i],i+1,list[i+1], i+2,list[i+2],i+3,list[i+3],i+4,list[i+4]); return 0; }

  46. void process_list(int list[ ], int value, int (my_operator)(int *, int, int)) { int i; for(i=0; i < SIZE; i++) list[i] = my_operator(list,i,value); } int offset(int list[ ], int index, int value) { return (list[index] + value); } Execute function

  47. Macros Concept • Literal substitution prior to compilation • Accept any data type as “argument” • Execute faster – no function call overhead • USE UPPER CASE to distinguish from variables and functions #define PI 3.1415927 #define Begin { #define End } #define X 512 #define Y 512 #define AREA (X * Y) #define FOREVER while(1) #define FILE_NAME “y:\\Programming\\prog1/.c”

  48. Definition on compiler command line Typically: … -DMACRO to set “MACRO” true … -DMACRO=100 to define a substitution • Predefined macros __STDC__ Value is 1 for ANSI conformant compiler __TIME__ Time of compilation in “hh:mm:ss” format __DATE__ Date of compilation __FILE__ Name of file being compiled __LINE__ Line being compiled

  49. Parameterised Macros #define SQR(a) ((a)*(a)) #define CUBE(x) ((x)*(SQR(x))) NOT Functions a = CUBE(2.5); Expands to: a = ((2.5)*(SQR(2.5))); a = ((2.5)*((2.5)*(2.5))); However: #define SQR(a) a * a b = SQR(6 + 4); Expands to: b = 6 + 4 * 6 + 4;

  50. Important pre-processor commands: • Stringizing operator ‘#’ • Token passing operator ‘##’ • ‘#undef’ • ‘#pragma’ directives

More Related