C Libraries. Bart Childs Complementary to the text(s)

Similar documents
Chapter 8 - Characters and Strings

C mini reference. 5 Binary numbers 12

C: How to Program. Week /May/28

Chapter 8 C Characters and Strings

Fundamentals of Programming. Lecture 11: C Characters and Strings

today cs3157-fall2002-sklar-lect05 1

Characters and Strings

Computer Programming

Scientific Programming in C V. Strings

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

Course organization. Course introduction ( Week 1)

SWEN-250 Personal SE. Introduction to C

Introduction to Programming Systems

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Contents. Preface. Introduction. Introduction to C Programming

Structured programming

Standard File Pointers

Chapter 8: Character & String. In this chapter, you ll learn about;

8. Characters, Strings and Files

CS3157: Advanced Programming. Outline

by Pearson Education, Inc. All Rights Reserved.

Strings and Library Functions

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CSE2301. Functions. Functions and Compiler Directives

Outline. Computer Programming. Structure of a function. Functions. Function prototype. Structure of a function. Functions

C Programming. Unit 9. Manipulating Strings File Processing.

Appendix A Developing a C Program on the UNIX system

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Programming in C. Part 1: Introduction

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

System Design and Programming II

Arrays, Strings, & Pointers

Introduction to string

C PROGRAMMING. Characters and Strings File Processing Exercise

Index. backslash character, 19 backup, off-site, 11. abs, 72 abstraction, 63, 83, 133, 141, 174, 181 acos, 72

UNIT-I Input/ Output functions and other library functions

211: Computer Architecture Summer 2016

Standard C Library Functions

C Characters and Strings

Introduction C CC. Advanced C

Library and function of C. Dr. Donald Davendra Ph.D. (Department of ComputingLibrary Science, andfei function VSB-TU of COstrava)

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Appendices E through H are PDF documents posted online at the book s Companion Website (located at

10/6/2015. C Input/Output. stdin, stdout, stderr. C Language II. CMSC 313 Sections 01, 02. C opens three input/output devices automatically:

Lecture 03 Bits, Bytes and Data Types

File IO and command line input CSE 2451

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Input/Output and the Operating Systems

Approximately a Test II CPSC 206

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Section 3: Library Functions

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Input / Output Functions

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Computers Programming Course 11. Iulian Năstac

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

UNIT IV-2. The I/O library functions can be classified into two broad categories:

CS167 Programming Assignment 1: Shell

This lists all known errors in The C Programming Language, Second Edition, by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1988).

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Advanced C Programming Topics

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, FALL 2012

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Programming in C Quick Start! Biostatistics 615 Lecture 4

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

C Basics And Concepts Input And Output

ONE DIMENSIONAL ARRAYS

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

System Software Experiment 1 Lecture 7

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

C programming basics T3-1 -

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

Goals of this Lecture

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Introduction to C Language

LESSON 4. The DATA TYPE char

CS240: Programming in C

MC8051: Speichertypen und Adressräume

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Chapter 10 Characters, Strings, and the string class

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Contents. A Review of C language. Visual C Visual C++ 6.0

Stream Model of I/O. Basic I/O in C

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Computer Science & Engineering 150A Problem Solving Using Computers

1 Pointer Concepts. 1.1 Pointer Examples

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Chapter 9 Strings. With this array declaration: char s[10];

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

C Programming Language Review and Dissection III

Transcription:

C Libraries Bart Childs Complementary to the text(s) 2006 C was designed to make extensive use of a number of libraries. A great reference for student purposes is appendix B of the K&R book. This list is ended to be a subset of those described there but to include those most likely to be used in an (not too) elementary course. The standard headers include: <assert.h> <float.h> <math.h> <stdarg.h> <stdlib.h> <ctype.h> <limits.h> <setjump.h> <stddef.h> <string.h> <errbi,g> <locale.h> <signal.h> <stdio.h> <time.h> Notice the order is quite unix like, alphabetical down the column. The order of the following sections will be somewhat like the earliest occurrence of the student needing them. The type size t is the unsigned egral type returned by the sizeof operator. That is subtle, sizeof is an operator therefore it is not in a standard library. The files stdin and stdout are the default files for eractive usage. They can be redirected.

1 <stdio.h> In K&R there are many subsections for this. In K&P there are smatterings here and there and then chapter 13 is full blown. Only a few of the many are included here. The first pair of input/output commands is the default ones and two extended pairs follow. Then some basic file commands are included. prf (const char format, list ) converts and writes to the output stream ( stdout in this case) under the control of the format. The return value is the number of characters written or is negative if an error has occurred. scanf (const char format, target s ) assigns converted values to the memory poed to by the target s, which are poers. scanf () returns when the format is exhausted. The return value is EOF if an end of file or error is encountered otherwise the number of conversions completed and assigned is returned. The char format argument in these most basic input/output commands seems to be a string constant in most cases. It can be a string variable and the user can create these in the program based on the data. The format for the input and output statements are quite similar but not identical. For example scanf("%lf", &dble); prf("%15.7lf", dble); can be used to input a value of type double from the standard input medium and then output it to the standard output medium. The output is specified to be 15 characters wide with 7 significant digits in the mantissa. The input is found in the input stream with appropriate delimiting, usually by whitespace. char scanfs prfs c character: char *. character: char. d decimal eger: * signed decimal eger lf floating: double * l is not needed. i eger: *. May be decimal, same as d octal, or hexadecimal. p void **. Input what is output. void *. Hexadecimal. 2

Finish tables of prf () and scanf () conversions and flags for format, similar to the above short one. The ssprf () and sscanf () functions are quite similar to the previous functions except that the input/output is to the first argument which is a string. The format and subsequent arguments work the same in all three of scanf () and prf () functions. sprf (char s, const char format, \ ldots {}) converts and writes to the output stream ( stdout in this case) under the control of the format. The return value is the number of characters written or is negative if an error has occurred. The output stream is the first argument, a string. sscanf (char s, const char format, \ ldots {}) assigns converted values to the memory poed to by the subsequent arguments which are poers. The input is from the first argument, a string. sscanf () returns when the format is exhausted. The return value is EOF if an end of file or error is encountered otherwise the number of conversions completed and assigned is returned. The fprf () and fscanf () functions are quite similar to the previous except the input file poer is included as the first argument. fprf (FILE stream, const char format, \ ldots {}) converts and writes to the output stream under the control of the format. The return value is the number of characters written or is negative if an error has occurred. fscanf (FILE stream, const char format, \ ldots {}) assigns converted values to the subsequent arguments (to the memory poed to by these poers). fscanf () returns when the format is exhausted. The return value is EOF if an end of file or error is encountered otherwise the number of conversions completed and assigned is returned. The concepts of C are surprising to many programmers who have had some experience with other languages. C is oriented to the concept of getting input a character at a time. We often think of a line of input at a time. The following functions enable the line at a time concepts. 3

char gets ( char s) Do not use this function, instead use the next one for safety and flexibilty. char fgets ( char s ), n, FILE stream) is similar to gets () with three notable exceptions. The first is that there are two more arguments. The argument n is a maximum number of characters that will be input and finally the input file is named by the third argument. If the number of characters input is less than n, then the newline character is preserved and a \0 is appended. The most basic of the file functions enable the user to associate input/output with specific files and remove that association when the program exits. FILE fopen(const char filename, const char mode) opens the filename given as the first argument and returns the file poer. This shall be captured by using this function as the right side of an assignment statement. The usual modes are "r", "w", and "rw" for read, write, and read/write. fclose (FILE stream) returns, etc. FILE freopen(const char filename, const char mode, FILE fp) closes the open file and then opens the stream again with the file name specified in the first argument. The return value is the FILE. Note that if the file is stdin, stdout, or stderr these file poers cannot be on the left side of an assignment statement because they are constants. Wow! The values of these constants are changed in the freopen () statement. feof (FILE fp ); returns a nonzero value if an end of file has been encountered for the file associated with fp. Another way of capturing the same situation is to assign the value of the return from the scanf ()s and testing it against EOF. ferror (FILE fp); returns a nonzero value if an error flag has been raised for the file associated with fp. If input is being done by use of fgets () one can use ferror () and feof () to determine more precise information than the simple returning of a NULL from fgets (). 4

2 <ctype.h> The character class tests are descriptive in their names if you know the vocabulary. These return true (non-zero) or false (zero). isalnum( c) alphanumeric isalpha ( c) alphabetic iscntrl ( c) control character isdigit ( c) decimal digit isgraph( c) pring character except space (ASCII) islower ( c) lower-case letter ispr ( c) pring character including space (ASCII) ispunct ( c) pring character except space, letter, or digit isspace ( c) space, formfeed,f newline, carriage return, tabs isupper ( c) upper-case letter isxdigit ( c) hexadecimal digit Case changes are performed by these two functions. Return the input if already appropriate or not a letter. toupper( c) change to an uppercase if it is lowercase tolower( c) change to a lowercase if it is uppercase 3 <string.h> Strings are not really a type in C. They are arrays of the type char and the functions involving them require a special characteristic. That is there is a null byte denoting the end of the string. If the string has n characters, then its declaration is like char string [n + 1] and the indexes of the elements are 0...n 1 for the data and n for the null byte. 5

The following is adapted from section B.3 of K&R. This does not include all the function headers described in that section. Variables s and t are of type char ; and variables st and ct are of type const char ;. The variable n is of type size t and c is an converted to char. These functions have been used in class or about to be used. char strcpy ( s, ct ) copy string ct to string s, including trailing \0 ; return s. char strncpy ( s, ct, n) copy at most n characters of ct to s; return s. Pad with \0 s if ct has fewer than n characters. char strcat ( s, ct ) concatenate string ct to end of string s; terminate with \0 ; return s. char strncat ( s, ct,n) concatenate at most n characters string ct to end of string s; terminate with \0 ; return s. strcmp( cs, ct ) compare string cs to string ct; return <0 if cs<ct, 0 if identical; or >0 if cs>ct; strncmp( cs, ct,n) compare at most n characters of string cs to string ct; return <0 if cs<ct, 0 if identical; or >0 if cs>ct; char strchr ( cs, c) return poer (not index) to first occurrence of c in cs; NULL if not present. char strrchr ( cs, c) return poer (not index) to last occurrence of c in cs; NULL if not present. char strstr ( cs, ct ) return poer to first occurrence of string ct in cs, NULL if not present. size t strlen ( cs) return length of cs. 6

4 <math.h> 5 <stdlib.h> void calloc ( size t n, size t el size ); void qsort ( void a ptr, size t n els, size t el size, compare( const void, const void )); The use of qsort () is powerful and must be done with a bit of care. 6 <assert.h> 7 <stdarg.h> 8 <setjmp.h> 9 <signal.h> 10 <time.h> 11 <limits.h> 12 <float.h> 13 Bibliography K&P Kelly and Pohl, C by Dissection, 4 th ed, Addison-Wesley, 2001. K&R Kernighhan, Brian and Ritchie, Dennis, The C Programming Language, 2 nd ed, Prentice Hall, 1988. The first edition was copyright in 1978. The second was published as the ANSI standard for C was about to appear. 7