1 / 16

C Homework

C Homework. Write mypaste. c and mycomm.c mypaste is like paste & mycomm is like comm Both take two files as arguments Paste reads a line from each file and outputs them to stdout on the same line

ravi
Download Presentation

C Homework

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. C Homework • Write mypaste.c and mycomm.c • mypasteis like paste & mycomm is like comm • Both take two files as arguments • Paste reads a line from each file and outputs them to stdout on the same line • Commcompares the two lines with strcmpand outputs the one that comes first in alphabetical order (with the appropriate number of tabs)

  2. hello.c • #include <stdio.h>main(){printf("hello, world\n");} • New Concepts • C is a compiled • Interpreted languages: python, javascript, R, lisp • cc –o hello hello.c • ./hello

  3. Make • make clean • make all • Makefile

  4. Input/Output: mycat.c (My Cat) • #include <stdio.h>main(){int c; while((c = getchar()) != EOF)putchar(c);} • New Concepts • Stdio: getchar/putchar • Variable declarations

  5. mywc.c (My Word Count) • #include <stdio.h>#include <ctype.h>main(){int c, bytes=0, words=0, lines=0;intprev_state=0; while((c = getchar()) != EOF) { bytes++; if(c == '\n') lines++;intcurrent_state = isalnum(c) || ispunct(c); if(!prev_state && current_state) words++;prev_state = current_state; }printf("%d\t%d\t%d\n", lines, words, bytes);} • New Concepts • ++ • ‘\n’ • printf • isalnum, ispunct • man isalnum IN OUT words++ words++

  6. mywc2.c: ac & av • #include <stdio.h>#include <ctype.h>void wc_fd(FILE *fd, char *filename){ int c, bytes=0, words=0, lines=0;intprev_state=0; while((c = getc(fd)) != EOF) { bytes++; if(c == '\n') lines++;intcurrent_state = isalnum(c) || ispunct(c); if(!prev_state && current_state) words++;prev_state = current_state; }printf("%d\t%d\t%d\t%s\n", lines, words, bytes, filename); if(fd && (fd != stdin))fclose(fd);} • void wc(char *filename){ if(strcmp(filename, "-") == 0) {wc_fd(stdin, filename); return; } FILE *fd = fopen(filename, "r"); if(!fd) printf("NA\tNA\tNA\t%s\n", filename); else wc_fd(fd, filename);}int main(int ac, char **av){inti; for(i=0;i<ac;i++)fprintf(stderr, "av[%d] = %s\n", i, av[i]); if(ac <= 1) wc("-"); else for(i = 1; i<ac; i++)wc(av[i]);}

  7. Pointer Addition • #include <stdio.h>main(){ char *str = "hello, world\n"; for( ; *str; str++)printf("%s", str);}

  8. hello3 • #include <stdio.h>#define MAXLINE 1024int main(){ char line[MAXLINE];while(fgets(line, MAXLINE, stdin) != NULL) { char *str = line; for( ; *str; str++)if(str == line || (isblank(str[-1]) && !isblank(str[0])))printf("%s", str); }} • Subscripting • x[i] • (x+i)[0] • *(x+i)

  9. Concepts so far… • Interpreted v. Compiled • cc, gcc, make • stdio • Variable declarations • Strings: pointers to sequences of characters • Pointer addition • Subscripting by positive & negative numbers • x[i] • (x+i)[0] • *(x+i)

  10. Structures • struct box {int ptr:30; unsigned int type:2;};struct pair {struct box first, rest;} heap[HEAPSIZE];

  11. Boxes: Pointer + Type • 3 Types • SYMBOL • nil, +, *, quote • LIST • first + rest • NUMBER • int

  12. first(cons(left, right))  left rest(cons(left, right))  right

  13. Pname: Symbol  StringIntern: String  Symbol

  14. Read, Eval, Print

  15. Eval • Done: addition • To do: subtraction, multiplication, division

More Related