1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX.

Slides:



Advertisements
Similar presentations
Adv. UNIX: lib/121 Advanced UNIX v Objectives of these slides: –look at some of the less familiar functions in the ANSI C Standard Library
Advertisements

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
System Files and Process Environment Password file Group file System identification Time Process environment.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
CPSC 441 Tutorial TA: Fang Wang Introduction to C.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Characters and Strings File Processing Exercise C Programming:Part 3.
Generic Functions1 Generic Functions: A generic function is one that can work on any underlying C data type. Generic functions allow us to reuse programs.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Module-E- Libraries1/49 Module E- Standard Libraries Input and Validation Formatted Output Library Functions.
Interacting with Unix. Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process.
1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
Week 9 - Wednesday.  What did we talk about last time?  structs.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Advanced Programming in the UNIX Environment Hop Lee.
Generic Programming in C
‘C’ Programming Structures and Commands
Sorting Tutorial Using C On Linux.
A bit of C programming Lecture 3 Uli Raich.
C Characters and Strings
Functions, Part 2 of 2 Topics Functions That Return a Value
2016.
An Introduction to C Programming
Standard Libraries in C
C Short Overview Lembit Jürimägi.
C Programming:Part 3 Characters and Strings File Processing Exercise.
Programming in C Input / Output.
Pointers.
CSC215 Lecture Input and Output.
Pointers.
Miscellaneous functions
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Text and Binary File Processing
Pointers.
File Input and Output.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Programming in C Input / Output.
Homework Continue with K&R Chapter 5 Skipping sections for now
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
Module 12 Input and Output
Module 14 Miscellaneous Topics
File I/O & UNIX System Interface
Characters and Strings Functions
C Characters and Strings
Assignment Operators Topics Increment and Decrement Operators
Pointers.
Functions that return a value
Presentation transcript:

1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX Shells / Processes / Shell Scripts

2 Line Input and Output, K&R 7.7 The standard C library equivalents to getline and putline: fgets and fputs Function fgets like getline() from a file char *fgets(char *line, int maxline, FILE *fp); Reads the next input line (including '\n' at the end) from file fp into the char array line –at most maxline-1 chars will be read –then a terminal '\0' will be added. Returns ptr to line or NULL (means EOF)

3 Line Input and Output /* sample code to understand fgets – similar to getline */ char *fgets(char *s, int n, FILE *iop) { register int c; register char *cs; cs = s; while (--n >0 && (c = getc(iop)) != EOF)) if ((*cs++ =c) == '\n') break; *cs = '\0'' return (c == EOF && cs == s) ? NULL : s; }

4 Line Input and Output Function fputs writes line to fp int fputs(char *line, FILE *fp); It returns EOF if error occurs (disk fills up?) otherwise it returns zero Can use perror to print out exact error cause (to stderr, not user screen)

5 Miscellaneous Functions, K&R 7.8 Look at pg where they are covered String Operations (string.h) –Used many of these already –Examples: size_t strlen(const char *s) int strcmp(const char *s, const char *t) char *strcpy(char *s, const char *t) char *strncpy(char *s, const char *t, size_t n)

6 Miscellaneous functions Character Processing Functions (ctype.h) –Used at least one of these already –Examples: int isalnum(int c) int isalpha (int c) int isdigit (int c) int isxdigit (int c) int isspace (int c) int tolower (int c) int toupper (int c)

7 Miscellaneous functions Ungetc (stdio.h) –Had something like this in homework, ungetch() –Only have guarantee that you can push one char back, but that is usually enough in practice –Example: int c = getc (stdin) /* expecting an alpha character */ if (!isalpha(c)) ungetc (c, stdin) /* if not alpha - return it */

8 Miscellaneous functions Command Execution (stdlib.h) –The function system() See pg 253 int system(const char *s); –The string s contains a UNIX system command system(“pwd”); system(“ls >fname”); system(“prog”); –Return value depends on the command executed –Learn that from man page for the command

9 Miscellaneous functions –In program compiled as prog, determine values for a and b through argc and argv[ ] int a, b; char command[MAXCMD]; … sprintf(command, "prog %d %d > prog.out", a, b); system(command); –System call doesn't return data from the command, but by writing > prog.out it creates an output file –Calling program can then open/read “prog.out” file

10 Miscellaneous Functions Storage Management (stdlib.h) malloc() andfree() –Review: Why is this incorrect? for (p = head; p != NULL; p = p->next) free(p); –Review: Why must it be written like this? for (p = head; p != NULL; p = q) { q = p->next; free(p); }

11 Miscellaneous Functions Math functions (math.h) –Need to use #include in source code –And use math library flag in execution of gcc: gcc -lm source.c –See list in appendix, mostly familiar! –Examples: double pow (double x, double y) /* Not “x**y” */

12 Miscellaneous Functions Random number functions (stdlib.h) –Two functions: rand (void) srand (unsigned int seed) /* default value is 1 */

13 qsort Library function qsort prototype (stdlib.h): void qsort(void *base, size_t n, size_t size, int (*cmp) (const void *, const void *)); It sorts an array of data using quick sort algorithm basea pointer to the table (an array of unknown type data) n the number of elements in the table sizethe size of each element What’s the last argument? –A pointer to a compare function for specific data type NOT the same function as qsort in K&R, pg 120

14 bsearch Library function bsearch prototype (stdlib.h): void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp) (const void *, const void *)); It searches for element containing key in an array of data of unknown that is already sorted in ascending order Returns: –pointer to element (if found) or –NULL (if not found) Last argument is a pointer to the compare function Should use same compare function as was used for sorting

15 qsort and bsearch qsort and bsearch don’t understand the data type for the elements of the table that it sorts or searches How can we tell that? –key (for bsearch only) is a type void * –base (the pointer to the table for both) is a type void * –size (the size of each element for both) is provided –Last argument is a pointer to the correct compare function with a cast of the argument list variables to void *

16 Example: qsort and bsearch /* compare function for integers lowest to highest, we pass a pointer to this function to qsort/bsearch */ int intcompare(int *i, int *j) { return (*i - *j); }

17 Example: qsort and bsearch main () { int i, *ip, *kp, a [10] = {8, 2, 9, 6, 5, 1, 3, 7, 4, 0}; qsort ( (void *) a, sizeof a/sizeof (int), sizeof(int), (int (*) (void *, void *)) intcompare); for (i = 0; i < 10; i++) printf(“%d, ”, a[i]); /* prints “0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ” */

18 Example: qsort and bsearch kp = &a[3]; ip = (int *) bsearch ( (void *) kp, (void *) a, sizeof a/sizeof (int), sizeof(int), (int (*) (void *, void *)) intcompare); if (ip != NULL) /* ip will be &a[3] */ printf(“found %d\n”, *ip); /* prints “found 3” */ }

19 UNIX Time Representation UNIX time is kept as a signed int representing the number of seconds from Jan 1, 1970 UTC That date is referred to as the UNIX epoch –Earliest representable date: 12/13/1901 –Latest representable date: 01/18/2038 UNIX will have a Y2.038K problem!

20 UNIX Time Representation clock_t/* elapsed processor time */ time_t/* calendar time */ struct tm {/* calendar time components */ int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year;... };

21 UNIX Time Representation Time functions (time.h) clock_t clock(void) /* returns processor time */ time_t time (time_t *tp) /* returns current time */ double diff_time(time_t time2, time_t time1) /* returns time difference in seconds */ struct tm *gmtime(const time_t *tp) /* returns *tp as UTC struct tm */ struct tm *localtime(const time_t *tp) /* returns *tp as local time struct tm */ char *asctime(const struct tm *tp) /* returns a string representing *tp, e.g. Sun Jan 3 13:08: \n\0 */