Cs288 Intensive Programming in Linux Instructor: C F Yurkoski Christopher.f.yurkowski@njit.edu Section web site: https://web.njit.edu/~yurkowsk/cs288.html Class 8 27-10-16
today homework revu scanf Pointers Bit operations FILE operations Macros Typedefs malloc() and free() Linked lists C lib Recursion In class exercises 6-9-15 cfy
homework Do exercise 1-19 from the text. Redo problem 4 from class 2 as a C program Write a program which reads stdin and converts any local case characters to upper 6-9-15 cfy
1-19 write a function called reverse(s) which reverses the character string s. Use it to write a program which reverses its input. solution at: https://web.njit.edu/~yurkowsk/x/reverse.c 6-9-15 cfy
6-9-15 cfy
6-9-15 cfy
6-9-15 cfy
1-21 write a program that replaces blanks with the minimum number of tabs and blanks. soluton at: https://web.njit.edu/~yurkowsk/x/entab.c 6-9-15 cfy
27-10-16 cfy
Problem3 https://web.njit.edu/~yurkowsk/x/prob3A.c 27-10-16 cfy
toupper.c https://web.njit.edu/~yurkowsk/x/toupper.c #include <stdio.h> main(){ char c; while((c=getchar())!=EOF){ if(c>='a' && c <= 'z') c+='A'-'a'; putchar(c); } 27-10-16 cfy
scanf Format similar to printf %d, %s, %x etc. int scanf(const char *restrict format, ... ); %d, %s, %x etc. 27-10-16 cfy
example #include <stdio.h> main(){ char action[6]; int word; while(scanf("%s %d",action,&word)!=EOF){ fprintf(stderr,"%s %d\n",action,word); if(!strcmp(action,“DO")) fprintf(stderr, “do something%x\n",word); else “reverse action %x\n",word); } 27-10-16 cfy
>: echo “DO 666" |. /a. out add 10 add a >: echo “UNDO 7" |. /a >: echo “DO 666" | ./a.out add 10 add a >: echo “UNDO 7" | ./a.out delete 7 27-10-16 cfy
pointers char c='a', d='b', *cp; cp= &d; *cp=c; printf("%c %c\n",c,d); 27-10-16 cfy
Referencing structures typedef struct string string; struct string{ struct string *forward; int data; }a[SZ]; a[i].forward=&a[i+1]; 27-10-16 cfy
Using pointers w/ structures struct list{ struct list *l; int payload; }array[SIZE]; struct list *lp; lp=array; lp->payload=data[10]; 27-10-16 cfy
BIT OPERATIONS int x, y, z; x&=7; z=y|x; z=y<<2; if(y^x); x=~y; 27-10-16 cfy
FILE I/O fopen() and friends (fwrite(), fread(), fseek() etc. 27-10-16 cfy
FILE *f; f=fopen("/tmp/yurkoski","w"); fprintf(f,“msg"); 27-10-16 cfy
Code macros e.g.: #define sum(a,b) (a+b) #define macro-name(args) (expression) e.g.: #define sum(a,b) (a+b) 6-9-15 cfy
#include <stdio.h> #include <stdlib.h> #define cube(a) (a*a*a) /* no semicolon*/ main(int argc, char *argv[]) { int a; if(argc != 2){ fprintf(stderr, "usage: %s n\n",argv[0]); exit(-1); } a=atoi(argv[1]); printf("the cube of %d is %d\n", a, cube(a)); 6-9-15
Revu typedefs Used to define you own types syntax: typedef {structure-def}typedef-name; Usage: typedef-name variable-name;
typedef struct { int fill; char name[8]; }frame; frame *p; p=(frame *)malloc(SIZE*sizeof(frame)); p++; p->fill=fill;
#include <stdlib.h> #define SIZE 10 typedef struct { int fill; char name[8]; }frame; main() { frame *p, *q, fill; char cmd[10]; q=p=malloc(SIZE*sizeof(frame)); 6-9-15
Linked list Singly linked lists Doubly linked lists Circularly linked lists Multiply linked lists 6-9-15 cfy
Singly linked lists 6-9-15 cfy
Doubly linked lists 6-9-15 cfy
C library atoi() itoa() atof() 27-10-16 cfy
Recursion 27-10-16 cfy
In class problem 1 Write a C program which accepts as a command line argument an integer and reads another integer from standard input and bit wise or the two together and writes the result (converted to an ascii string) to a file in the in /tmp whose name is your student id number. Put your source code in moodle.
example if the file /tmp/x contains a 5, and your id is 666 cc yourprogm.c –o yourprogm cat /tmp/x | yourprogm 6 should write the ascii string 30 to the file 666 in the current directory.
In class problem 2 Write a program which reads a series of integers for standard input until an EOF is encountered. Store them in a linked list in sorted order. Upon encountering the EOF, output the items on the list.