1 / 7

CS113 Introduction to C

CS113 Introduction to C. Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.edu Lecture 11 : September 18. The C preprocessor: Conditional Inclusion. #if , #elif , #else , #endif Allow us to define conditional statements for what the compiler should compile

olive
Download Presentation

CS113 Introduction to C

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. CS113Introduction to C Instructor: Ioannis A. VetsikasE-mail: vetsikas@cs.cornell.edu Lecture 11 : September 18

  2. The C preprocessor:Conditional Inclusion • #if, #elif, #else, #endif • Allow us to define conditional statements for what the compiler should compile • #define name and #undef name • defined(name) returns 1 if name is defined with a #define statement and 0 otherwise • #ifdef and #ifndef are same respectively as #if defined and #if !defined #ifdef DEBUG printf(“Debug mode”); i++; #else printf(“Real mode”); #endif

  3. Streams • I/O done through streams; two kinds: text and binary • Text streams are sequences of lines, each of which is a sequence of characters terminated by a newline • Binary streams are sequences of characters corresponding to internal representation of data. • Streams created by opening files and referenced using stream pointers (FILE *) • Normally three standard streams are automatically open: • stdin (stream for standard input - from keyboard) • stdout (stream for standard output - to screen) • stderr (stream for standard error output - to screen)

  4. Functions for opening & closing • Open a file: • FILE *fopen(char *name, char *mode); • Opens file with name name using mode mode • Mode is taking values: “r” (open text for reading), “w” (open text for writing), etc… • For binary files add “b” at the end, e.g. “rb” (open binary for reading) • Usage: FILE *fp=fopen(“infile”, “r”); • Returns NULL if some problem occurs during the opening of the file (e.g. file is not found, or permissions are not set correctly etc.) • Close a file: • int flcose(FILE *fp);

  5. Some other functions • Defined in <stdio.h> • int fprintf(FILE *fp, char *format, …); • int fscanf(FILE *fp, char *format, …); • int fgetc(FILE *fp); • int fputc(int c, FILE *fp); • char *fgets(char *s, int n, FILE *fp); • int fputs(char *s, FILE *fp); • int feof(FILE *fp); • For more functions read appendix B of the K&R book

  6. Example (from Kelley&Pohl - modified) #include <stdio.h> #include <stdlib.h> void double_space(FILE *ifp, FILE *ofp) { int c; while ((c=getc(ifp)) != EOF) { putc(c, ofp); if (c==‘\n’) putc(‘\n’, ofp); /* duplicate Newline */ } int main(int argc, char **argv) { FILE *ifp = fopen(argv[1], “r”); FILE *ofp = fopen(argv[2], “w”); double_space(ifp, ofp); fclose(ifp); fclose(ofp); }

  7. Announcements • Wednesday: Mock quiz • Friday: Quiz (the real one) – Make sure you come • We are done with covering the K&R book • Read the appendices by yourselves and look especially at appendix B where the library functions are presented • Homework 4 is challenging and you should start as early as possible; I’ll announce extra office hours for the next week to allow you to come and talk to me about the homework • I will do some revision, answer questions and do miscellaneous programming (debugging) skills next

More Related