CS1030 - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

CS1030

Description:

1978: first book on programming in C ... (blue text = the ones I most commonly used) ... certain library functions assign specific positive values on error ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 28
Provided by: drmarkh
Category:
Tags: cs1030

less

Transcript and Presenter's Notes

Title: CS1030


1
C Standard Library
  • Includes derivatives of
  • Standard C Library
  • Standard Template Library
  • Later in this course
  • and a lot of history

2
Standard C Library
  • The Development of the C Language Dennis
    Ritchie
  • Summary of C (and Unix) History
  • 1968 _at_ ATT-Bell Labs/GE - Multics
  • 1970 1st Unix kernel running on DEC PDP-7
    PDP-11
  • 1971 early C Language created derived from BCPL
    and B
  • 1973 essentials of modern C complete Unix
    kernel rewritten in C
  • 1978 first book on programming in C
  • early 1980s Unix C distributed by ATT becomes
    widely popular
  • 1983 ANSI committee on C formed
  • 1989 C adopted as ANSI standard
  • ANSI Standard C Library
  • 1995 Library Standard extended to support
    Unicode (multinational characters)
  • 1999 additional support (complex number
    libraries, bool data type,)
  • C is quirky, flawed, and an enormous success
  • - Ritchie

3
Standard C Library
  • 24 individual header files you include
  • These get compiled along with your source code in
    your .cpp files, and contain
  • Function declarations
  • Data type definitions (more on these later,
    maybe)
  • Macros (defines e.g. M_PI)
  • Actual implementation is separated into a
    corresponding library files
  • Library files (.lib) are pulled in by the linker
  • Standard C Library is tiny compared to Javas
    standard class libraries
  • E.g. no support for graphics, networking)
  • Other libraries (e.g. MFC, QT) needed
  • Nearly always 3rd party (e.g. Microsoft,
    Trolltech)

4
ANSI Std C headers (1-6)(blue text the ones I
most commonly used)
  • ltassert.hgt Contains the assert macro, used to
    assist with detecting logical errors and other
    types of bug in debugging versions of a program.
  • ltcomplex.hgt A set of functions for manipulating
    complex numbers. (New with C99)
  • ltctype.hgt This header file contains functions
    used to classify characters by their types or to
    convert between upper and lower case in a way
    that is independent of the used character set
    (typically ASCII, although implementations
    utilizing EBCDIC are also known).
  • lterrno.hgt For testing error codes reported by
    library functions.
  • ltfenv.hgt For controlling floating-point
    environment. (New with C99)
  • ltfloat.hgt Contains defined constants specifying
    the implementation-specific properties of the
    floating-point library, such as the minimum
    difference between two different floating-point
    numbers (_EPSILON), the maximum number of digits
    of accuracy (_DIG) and the range of numbers which
    can be represented (_MIN, _MAX).

5
ANSI Std C headers (7-12)
  • ltinttypes.hgt For precise conversion between
    integer types. (New with C99)
  • ltiso646.hgt For programming in ISO 646 variant
    character sets. (New with NA1)
  • ltlimits.hgt Contains defined constants specifying
    the implementation-specific properties of the
    integer types, such as the range of numbers which
    can be represented (_MIN, _MAX).
  • ltlocale.hgt For setlocale() and related
    constants. This is used to choose an appropriate
    locale.
  • ltmath.hgt For computing common mathematical
    functions
  • ltsetjmp.hgt For executing nonlocal goto statements

6
ANSI Std C headers (13-18)
  • ltsignal.hgt For controlling various exceptional
    conditions
  • ltstdarg.hgt For accessing a varying number of
    arguments passed to functions.
  • ltstdbool.hgt For a boolean data type. (New with
    C99)
  • ltstdint.hgt For defining various integer types.
    (New with C99)
  • ltstddef.hgt For defining several useful types and
    macros.
  • ltstdio.hgt Provides the core input and output
    capabilities of the C language. This file
    includes the venerable printf function.

7
ANSI Std C headers (19-24)
  • ltstdlib.hgt For performing a variety of
    operations, including conversion, pseudo-random
    numbers, memory allocation, process control,
    environment, signalling, searching, and sorting.
  • ltstring.hgt For manipulating several kinds of
    strings.
  • lttgmath.hgt For type-generic mathematical
    functions. (New with C99)
  • lttime.hgt For converting between various time and
    date formats.
  • ltwchar.hgt For manipulating wide streams and
    several kinds of strings using wide characters -
    key to supporting a range of languages. (New with
    NA1)
  • ltwctype.hgt For classifying wide characters. (New
    with NA1)

8
ltassert.hgt
  • void assert(int expression)
  • Macro used for internal error detection. (Ignored
    if NDEBUG is defined where ltassert.hgt is
    included.) If expression equals zero, message
    printed on stderr and abort called to terminate
    execution.

9
ltctype.hgt (1)
  • int isalnum(int c)
  • isalpha(c) or isdigit(c)
  • int isalpha(int c)
  • isupper(c) or islower(c)
  • int iscntrl(int c)
  • is control character. In ASCII, control
    characters are 0x00 (NUL) to 0x1F (US), and 0x7F
    (DEL)
  • int isdigit(int c)
  • is decimal digit
  • int isgraph(int c)
  • is printing character other than space
  • int islower(int c)
  • is lower-case letter

10
ltctype.hgt (2)
  • int isprint(int c)
  • is printing character (including space). In
    ASCII, printing characters are 0x20 (' ') to 0x7E
    ('')
  • int ispunct(int c)
  • is printing character other than space, letter,
    digit
  • int isspace(int c)
  • is space, formfeed, newline, carriage return,
    tab, vertical tab
  • int isupper(int c)
  • is upper-case letter
  • int isxdigit(int c)
  • is hexadecimal digit
  • int tolower(int c)
  • return lower-case equivalent
  • int toupper(int c)
  • return upper-case equivalent

11
lterrno.hgt
  • errno
  • object to which certain library functions assign
    specific positive values on error
  • EDOM
  • code used for domain errors
  • ERANGE
  • code used for range errors
  • Notes
  • other implementation-defined error values are
    permitted
  • to determine the value (if any) assigned to errno
    by a library function, a program should assign
    zero to errno immediately prior to the function
    call

12
ltmath.hgt (1)
  • On domain error, implementation-defined value
    returned and errno set to EDOM. On range error,
    errno set to ERANGE and return value is HUGE_VAL
    with correct sign for overflow, or zero for
    underflow. Angles are in radians.
  • HUGE_VAL
  • magnitude returned (with correct sign) on
    overflow error
  • double exp(double x)
  • exponential of x
  • double log(double x)
  • natural logarithm of x
  • double log10(double x)
  • base-10 logarithm of x
  • double pow(double x, double y)
  • x raised to power y
  • double sqrt(double x)
  • square root of x

13
ltmath.hgt (2)
  • double ceil(double x)
  • smallest integer not less than x
  • double floor(double x)
  • largest integer not greater than x
  • double fabs(double x)
  • absolute value of x
  • double ldexp(double x, int n)
  • x times 2 to the power n
  • double frexp(double x, int exp)
  • if x non-zero, returns value, with absolute value
    in interval 1/2, 1), and assigns to exp integer
    such that product of return value and 2 raised to
    the power exp equals x if x zero, both return
    value and exp are zero
  • double modf(double x, double ip)
  • returns fractional part and assigns to ip
    integral part of x, both with same sign as x
  • double fmod(double x, double y)
  • if y non-zero, floating-point remainder of x/y,
    with same sign as x if y zero, result is
    implementation-defined

14
ltmath.hgt (3)
  • double sin(double x)
  • sine of x
  • double cos(double x)
  • cosine of x
  • double tan(double x)
  • tangent of x
  • double asin(double x)
  • arc-sine of x
  • double acos(double x)
  • arc-cosine of x
  • double atan(double x)
  • arc-tangent of x
  • double atan2(double y, double x)
  • arc-tangent of y/x
  • double sinh(double x)
  • hyperbolic sine of x
  • double cosh(double x)
  • hyperbolic cosine of x
  • double tanh(double x)

15
ltstddef.hgt
  • NULL
  • Null pointer constant.
  • offsetof(stype, m)
  • Offset (in bytes) of member m from start of
    structure type stype.
  • ptrdiff_t
  • Type for objects declared to store result of
    subtracting pointers.
  • size_t
  • Type for objects declared to store result of
    sizeof operator.

16
ltstdlib.hgt
  • ltstdlib.hgt
  • EXIT_FAILURE
  • Value for status argument to exit indicating
    failure.
  • EXIT_SUCCESS
  • Value for status argument to exit indicating
    success.
  • RAND_MAX
  • Maximum value returned by rand().
  • NULL
  • Null pointer constant.

17
ltstdlib.hgt
  • void abort()
  • Terminates program abnormally, by calling
    raise(SIGABRT).
  • void exit(int status)
  • Terminates program normally. Functions installed
    using atexit are called (in reverse order to that
    in which installed), open files are flushed, open
    streams are closed and control is returned to
    environment. status is returned to environment in
    implementation-dependent manner. Zero or
    EXIT_SUCCESS indicates successful termination and
    EXIT_FAILURE indicates unsuccessful termination.
    Implementations may define other values.
  • int atexit(void (fcm)(void))
  • Registers fcn to be called when program
    terminates normally (or when main returns).
    Returns non-zero on failure.
  • int system(const char s)
  • If s is not NULL, passes s to environment for
    execution, and returns status reported by command
    processor if s is NULL, non-zero returned if
    environment has a command processor.
  • char getenv(const char name)
  • Returns string associated with name name from
    implementation's environment, or NULL if no such
    string exists.

18
ltstdlib.hgt
  • void bsearch(const void key, const void base,
    size_t n, size_t size, int (cmp)(const void
    keyval, const void datum))
  • Searches ordered array base (of n objects each of
    size size) for item matching key according to
    comparison function cmp. cmp must return negative
    value if first argument is less than second, zero
    if equal and positive if greater. Items of base
    are assumed to be in ascending order (according
    to cmp). Returns a pointer to an item matching
    key, or NULL if none found.
  • void qsort(void base, size_t n, size_t size, int
    (cmp)(const void, const void))
  • Arranges into ascending order array base (of n
    objects each of size size) according to
    comparison function cmp. cmp must return negative
    value if first argument is less than second, zero
    if equal and positive if greater.
  • int rand(void)
  • Returns pseudo-random number in range 0 to
    RAND_MAX.
  • void srand(unsigned int seed)
  • Uses seed as seed for new sequence of
    pseudo-random numbers. Initial seed is 1.

19
ltstring.hgt
  • A whole bunch of functions that deal with
    handling C-style strings (arrays of characters)
  • Not the same as a C string
  • Remember, C had no classes
  • C programs should use the string class from the
    C Standard Library wherever possible.
  • Not the same as the C ltstringgt include

20
lttime.hgt
  • CLOCKS_PER_SEC
  • The number of clock_t units per second.
  • NULL
  • Null pointer constant.
  • clock_t
  • An arithmetic type elapsed processor representing
    time.
  • time_t
  • An arithmetic type representing calendar time.

21
ltstdio.hgt
  • ltstdio.hgt
  • BUFSIZ
  • Size of buffer used by setbuf.
  • EOF
  • Value used to indicate end-of-stream or to report
    an error.
  • FILENAME_MAX
  • Maximum length required for array of characters
    to hold a filename.
  • FOPEN_MAX
  • Maximum number of files which may be open
    simultaneously.
  • L_tmpnam
  • Number of characters required for temporary
    filename generated by tmpnam.
  • NULL
  • Null pointer constant.
  • SEEK_CUR
  • Value for origin argument to fseek specifying
    current file position.
  • SEEK_END
  • Value for origin argument to fseek specifying end
    of file.
  • SEEK_SET
  • Value for origin argument to fseek specifying
    beginning of file.

22
Standard book on the Standard C Library
23
Standard C Library within C Standard Libary
  • Includes small modifications that make it easier
    to work with C
  • Names of the Standard C Library header files
    changed from ltxxx.hgt to ltcxxxgt
  • C-style names are still available, although
    deprecated
  • include ltmath.hgtor
  • include ltcmathgt // actually, cmath includes
    math.h
  • All identifiers are part of the std namespace

24
C Standard Library
  • A collection of classes and functions
  • containers
  • lists similar to Java LinkedList
  • Vectors similar to Java ArrayList
  • strings and streams
  • interactive
  • file I/O
  • common functions for tasks such as finding the
    square root of a number (from the Standard C
    Librarys ltcmathgt rather than ltmath.hgt)
  • 69 header files
  • 19 of these are deprecated
  • Still tiny compared to Javas standard class
    libraries

25
Containers similar to Java Collections Framework
classes
  • ltbitsetgt
  • ltdequegt
  • ltlistgt
  • ltmapgt
  • ltqueuegt
  • ltsetgt
  • ltstackgt
  • ltvectorgt

26
Streams and Input/Output
  • ltfstreamgt
  • ltiosgt
  • ltiostreamgt // includes istream
  • ltiosfwdgt
  • ltiomanipgt
  • ltistreamgt // includes ostream
  • ltostreamgt
  • ltsstreamgt
  • ltstreambufgt

27
Strings
  • demo
Write a Comment
User Comments (0)
About PowerShow.com