Chapter 21: The Standard Library Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 21 The Standard Library.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Data types and variables
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Chapter 2: Introduction to C++.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
21. THE STANDARD LIBRARY. General Rules The C standard library is divided into 15 parts (24 in C99), with each part described by a header. The names of.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Characters and Strings
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
ECE 103 Engineering Programming Chapter 15 C Standard Library Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Strings (Continued) Chapter 13
Chapter 13 - The Preprocessor
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
14. THE PREPROCESSOR.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
2.1 Parts of a C++ Program.
Exam 4 review Copyright © 2008 W. W. Norton & Company.
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
The Standard Library Chapter 21
Chapter 2: Introduction to C++.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Chapter 21: The Standard Library Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 21 The Standard Library

Chapter 21: The Standard Library Using the Library The C89 standard library is divided into 15 parts, with each part described by a header. C99 has an additional nine headers. † † † † † † † † C99 only Copyright © 2008 W. W. Norton & Company. All rights reserved. 2

Chapter 21: The Standard Library Using the Library Most compilers come with a more extensive library that has additional (nonstandard) headers. Nonstandard headers often provide: –Functions that are specific to a particular computer or operating system –Functions that allow more control over the screen and keyboard –Support for graphics or a window-based user interface Copyright © 2008 W. W. Norton & Company. All rights reserved. 3

Chapter 21: The Standard Library Using the Library The standard headers consist primarily of function prototypes, type definitions, and macro definitions. When a file includes several standard headers, the order of #include directives doesn’t matter. It’s also legal to include a standard header more than once. Copyright © 2008 W. W. Norton & Company. All rights reserved. 4

Chapter 21: The Standard Library Restrictions on Names Used in the Library Any file that includes a standard header must obey two rules: –The names of macros defined in that header can’t be used for any other purpose. –Library names with file scope ( typedef names, in particular) can’t be redefined at the file level. Copyright © 2008 W. W. Norton & Company. All rights reserved. 5

Chapter 21: The Standard Library Restrictions on Names Used in the Library Other restrictions are less obvious: –Identifiers that begin with an underscore followed by an upper-case letter or a second underscore are reserved for use within the library. –Identifiers that begin with an underscore are reserved for use as identifiers and tags with file scope. –Every identifier with external linkage in the standard library is reserved for use as an identifier with external linkage. In particular, the names of all standard library functions are reserved. Copyright © 2008 W. W. Norton & Company. All rights reserved. 6

Chapter 21: The Standard Library Restrictions on Names Used in the Library These rules apply to every file in a program, regardless of which headers the file includes. Moreover, they apply not just to names that are currently used in the library, but also to names that are set aside for future use. For example, C reserves identifiers that begin with str followed by a lower-case letter. Copyright © 2008 W. W. Norton & Company. All rights reserved. 7

Chapter 21: The Standard Library Functions Hidden by Macros The C standard allows headers to define macros that have the same names as library functions, but requires that a true function be available as well. It’s not unusual for a library header to declare a function and define a macro with the same name. Copyright © 2008 W. W. Norton & Company. All rights reserved. 8

Chapter 21: The Standard Library Functions Hidden by Macros getchar is a library function declared in the header: int getchar(void); usually defines getchar as a macro as well: #define getchar() getc(stdin) By default, a call of getchar will be treated as a macro invocation. Copyright © 2008 W. W. Norton & Company. All rights reserved. 9

Chapter 21: The Standard Library Functions Hidden by Macros A macro is usually preferable to a true function, because it will probably improve the speed of a program. Occasionally, a genuine function is needed, perhaps to minimize the size of the executable code. Copyright © 2008 W. W. Norton & Company. All rights reserved. 10

Chapter 21: The Standard Library Functions Hidden by Macros A macro definition can be removed (thus gaining access to the true function) by using #undef : #include #undef getchar #undef has no effect when given a name that’s not defined as a macro. Copyright © 2008 W. W. Norton & Company. All rights reserved. 11

Chapter 21: The Standard Library Functions Hidden by Macros Individual uses of a macro can be disabled by putting parentheses around its name: ch = (getchar)(); /* instead of ch = getchar(); */ The preprocessor can’t spot a parameterized macro unless its name is followed by a left parenthesis. However, the compiler can still recognize getchar as a function. Copyright © 2008 W. W. Norton & Company. All rights reserved. 12

Chapter 21: The Standard Library C89 Library Overview Diagnostics Contains only the assert macro, which can be used to insert self-checks into a program. If any check fails, the program terminates. Character Handling Provides functions for classifying characters and for converting letters from lower to upper case or vice versa. Copyright © 2008 W. W. Norton & Company. All rights reserved. 13

Chapter 21: The Standard Library C89 Library Overview Errors Provides errno (“error number”), an lvalue that can be tested after a call of certain library functions to see if an error occurred. Characteristics of Floating Types Provides macros that describe the characteristics of floating types, including their range and accuracy. Copyright © 2008 W. W. Norton & Company. All rights reserved. 14

Chapter 21: The Standard Library C89 Library Overview Sizes of Integer Types Provides macros that describe the characteristics of integer types (including character types), including their maximum and minimum values. Localization Provides functions to help a program adapt its behavior to a country or other geographic region. Copyright © 2008 W. W. Norton & Company. All rights reserved. 15

Chapter 21: The Standard Library C89 Library Overview Mathematics Provides common mathematical functions. Nonlocal Jumps Provides the setjmp and longjmp functions. setjmp “marks” a place in a program; longjmp can then be used to return to that place later. Copyright © 2008 W. W. Norton & Company. All rights reserved. 16

Chapter 21: The Standard Library C89 Library Overview Signal Handling Provides functions that deal with exceptional conditions (signals). –The signal function installs a function to be called if a given signal should occur later. –The raise function causes a signal to occur. Variable Arguments Provides tools for writing functions that can have a variable number of arguments. Copyright © 2008 W. W. Norton & Company. All rights reserved. 17

Chapter 21: The Standard Library C89 Library Overview Common Definitions Provides definitions of frequently used types and macros. Input/Output Provides a large assortment of input/output functions, including operations on both sequential and random-access files. Copyright © 2008 W. W. Norton & Company. All rights reserved. 18

Chapter 21: The Standard Library C89 Library Overview General Utilities Provides functions that perform the following operations: –Converting strings to numbers –Generating pseudo-random numbers –Performing memory management tasks –Communicating with the operating system –Searching and sorting –Performing conversions between multibyte characters and wide characters Copyright © 2008 W. W. Norton & Company. All rights reserved. 19

Chapter 21: The Standard Library C89 Library Overview String Handling Provides functions that perform string operations, as well as functions that operate on arbitrary blocks of memory. Date and Time Provides functions for determining the time (and date), manipulating times, and formatting times for display. Copyright © 2008 W. W. Norton & Company. All rights reserved. 20

Chapter 21: The Standard Library C99 Library Changes Some of the biggest changes in C99 affect the standard library: –Additional headers. The C99 standard library has nine headers that don’t exist in C89. –Additional macros and functions. C99 adds macros and functions to several existing headers (especially ). –Enhanced versions of existing functions. Some existing functions, including printf and scanf, have additional capabilities in C99. Copyright © 2008 W. W. Norton & Company. All rights reserved. 21

Chapter 21: The Standard Library C99 Library Changes Complex Arithmetic Defines the complex and I macros. Provides functions for performing mathematical operations on complex numbers. Floating-Point Environment Provides access to floating-point status flags and control modes. Copyright © 2008 W. W. Norton & Company. All rights reserved. 22

Chapter 21: The Standard Library C99 Library Changes Format Conversion of Integer Types Defines macros that can be used in format strings for input/output of the integer types declared in. Provides functions for working with greatest- width integers. Alternative Spellings Defines macros representing the operators whose symbols contain the characters &, |, ~, !, and ^. Copyright © 2008 W. W. Norton & Company. All rights reserved. 23

Chapter 21: The Standard Library C99 Library Changes Boolean Type and Values Defines the bool, true, and false macros, as well as a macro that can be used to test whether these macros have been defined. Integer Types Declares integer types with specified widths and defines related macros. Defines parameterized macros that construct integer constants with specific types. Copyright © 2008 W. W. Norton & Company. All rights reserved. 24

Chapter 21: The Standard Library C99 Library Changes Type-Generic Math Provides “type-generic” macros that can detect argument types and substitute a call of a or function. Extended Multibyte and Wide-Character Utilities Provides functions for wide-character input/output and wide string manipulation. Copyright © 2008 W. W. Norton & Company. All rights reserved. 25

Chapter 21: The Standard Library C99 Library Changes Wide-Character Classification and Mapping Utilities The wide-character version of. Provides functions for classifying and changing the case of wide characters. Copyright © 2008 W. W. Norton & Company. All rights reserved. 26

Chapter 21: The Standard Library The Header: Common Definitions Types defined in : –ptrdiff_t. The type of the result when two pointers are subtracted. –size_t. The type returned by the sizeof operator. –wchar_t. A type large enough to represent all possible characters in all supported locales. Macros defined in : –NULL. Represents the null pointer. –offsetof. Computes the number of bytes between the beginning of a structure and one of its members. Copyright © 2008 W. W. Norton & Company. All rights reserved. 27

Chapter 21: The Standard Library The Header: Common Definitions An example structure: struct s { char a; int b[2]; float c; }; The value of offsetof(struct s, a) must be 0, but the offsets of b and c depend on the compiler. One possibility is that offsetof(struct s, b) is 1, and offsetof(struct s, c) is 9. If a compiler should leave a three-byte hole after a, the offsets of b and c would be 4 and 12. Copyright © 2008 W. W. Norton & Company. All rights reserved. 28

Chapter 21: The Standard Library The Header (C99): Boolean Type and Values Macros defined in : bool (defined to be _Bool ) true (defined to be 1 ) false (defined to be 0 ) __bool_true_false_are_defined (defined to be 1 ) A program could use a preprocessing directive to test the last of these before attempting to define its own version of bool, true, or false. Copyright © 2008 W. W. Norton & Company. All rights reserved. 29