C/C++ Programmer's Reference, Third Edition ISBN:0072227222 by Herbert Schildt McGraw-Hill/Osborne © 2003 (358 pages) This guide reviews the rules and syntax of the principle commands that comprise C and its object-oriented cousin, C++ The reference consists of 19 chapters that define operators and the standard input/output, string, character, and more Table of Contents C/C++ Programmer’s Reference, Third Edition Introduction Data Types, Variables, Chapter 1 and Constants Functions, Scopes, Chapter 2 - Namespaces, and Headers Chapter 3 - Operators The Preprocessor and Chapter 4 Comments Chapter 5 - Keyword Summary The Standard C I/O Chapter 6 - Functions The String and Character Functions The C Mathematical Chapter 8 Functions The C++ Mathematical Chapter 9 Functions Time, Date, and Chapter 10 Localization Functions The Dynamic Allocation Chapter 11 Functions Chapter 12 - Miscellaneous Functions The Wide-Character Chapter 13 Functions The Old-Style C++ I/O Chapter 14 System The ANSI/ISO Standard Chapter 15 C++ I/O Classes The C++ Standard Chapter 16 Template Library The C++ String, Chapter 17 - Exception, Complex, and Pair Classes Library Features Added Chapter 18 by C99 The NET Managed Chapter 19 Extensions to C++ Index Chapter 7 - Back Cover Legendary programming author Herbert Schildt distills and organizes the essential elements of C and C++ into a convenient and easy-to-use format that gives you quick, accurate answers to your C/C++ programming questions You’ll quickly locate clear, concise explanations of the C and C++ programming syntax, keywords, operators, preprocessor directives, and function and class libraries This ready resource even describes the extended keywords used for NET programming Whether you’re a beginner programmer or an experienced pro, this is one indispensable tool that you won’t want to be without Quickly find the syntax for keywords, operators, functions, classes, and preprocessor directives Fully covers the Standard Template Library (STL) and the I/O system Packed with programming tips to speed your work Solve problems in all C/C++ environments— including Visual C++ About the Author Herbert Schildt, the world’s leading programming author, is an authority on the C, C++, Java, and C# programming languages and a master Windows programmer He was a member of the ANSI/ISO committees that standardized C and C++ C/C++ Programmer’s Reference, Third Edition Herbert Schildt McGraw-Hill/Osborne New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto McGraw-Hill/Osborne 2600 Tenth Street Berkeley, California 94710 U.S.A To arrange bulk purchase discounts for sales promotions, premiums, or fund-raisers, please contact McGraw-Hill/Osborne at the above address For information on translations or book distributors outside the U.S.A., please see the International Contact Information page immediately following the index of this book C/C++ Programmer’s Reference, Third Edition Copyright © 2003 by The McGraw-Hill Companies All rights reserved Printed in the United States of America Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication 1234567890 DOC DOC 0198765432 ISBN 0-07-222722-2 Publisher: Brandon A Nordin Vice President & Associate Publisher: Scott Rogers Acquisitions Editor: Lisa McClain Project Editor: Laura Stone Acquisitions Coordinator: Athena Honore Technical Editor: Greg Guntle Copy Editor: Dennis Weaver Proofreaders: Linda Medoff, Paul Medoff Indexer: Sheryl Schildt Composition: Apollo Publishing Services, George Toma Charbak Illustrators: Michael Mueller, Lyssa Wald Series Design: Peter F Hancik This book was composed with Corel VENTURA™ Publisher Information has been obtained by McGraw-Hill/Osborne from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, McGraw-Hill/Osborne, or others, McGraw-Hill/Osborne does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information About the Author Herbert Schildt is a leading authority on C and C++ and was a member of the ANSI/ISO committees that standardized C and C++ His books have sold more than 3 million copies worldwide and have been translated into all major foreign languages He is the author of C: The Complete Reference, C++: The Complete Reference, C++: A Beginner’s Guide, C++ from the Ground Up, and many other best-sellers, including C#: The Complete Reference and Java 2: The Complete Reference Schildt holds a Master’s degree in computer science from the University of Illinois Introduction C and C++ are two of the world’s most important programming languages Indeed, to be a professional programmer today implies proficiency in these two languages They are the foundation upon which modern programming is built C was invented by Dennis Ritchie in the 1970s C is a middle-level language It combines the control structures of a high-level language with the ability to manipulate bits, bytes, and pointers (addresses) Thus, C gives the programmer nearly complete control over the machine C was first standardized late in 1989 when the American National Standards Institute (ANSI) standard for C was adopted This version of C is commonly referred to as C89 This standard was also adopted by ISO (International Standards Organization) C89 was amended slightly in 1995 C++ was created by Bjarne Stroustrup, beginning in 1979 The development and refinement of C++ was a major effort, spanning the 1980s and most of the 1990s Finally, in 1998 an ANSI/ISO standard for C++ was adopted In general terms, C++ is the object-oriented version of C C++ is built upon the foundation of C89, including its 1995 amendments In fact, the version of C defined by C89 is commonly referred to as the “C subset of C++.” Although C++ began as a set of object-oriented extensions to C, it soon expanded into being a programming language in its own right Today, C++ is nearly twice the size of the C language Needless to say, C++ is one of the most powerful computer languages ever devised In 1999, a new ANSI/ISO standard for C was adopted This version is called C99 It includes a number of refinements and several new features Some of these “new” features were borrowed from C++, but some are entirely new innovations Thus, several of the elements added by C99 are incompatible with C++ This means that with the advent of C99, Standard C is no longer a pure subset of C++ Fortunately, many of the incompatibilities relate to special-use features that are readily avoided Thus, it is still easy to write code that is compatible with both C and C++ At the time of this writing, no major compiler currently accepts all of the C99 additions The following table synopsizes the relationships between C89, C99, and C++ C89 The original ANSI/ISO standard for C C89 is what most programmers today think of as C C++ The object-oriented version of C The current ANSI/ISO standard for C++ is built upon C89 Thus, C89 forms a subset of C++ The latest standard for C Includes all of C89, but adds several C99 new features Some of the new features are not supported by the current standard for C++ The material in this book describes C89, C99, and C++ When a feature is unique to one of these, it will be so flagged Otherwise, you can assume that the feature applies to all three As you are undoubtedly aware, C and C++ are large topics It is, of course, not possible to cover every aspect of these important languages here Instead, this quick reference distills their most salient features into a convenient and easy to use form Chapter 1: Data Types, Variables, and Constants C and C++ offer the programmer a rich assortment of built-in data types Programmer-defined data types can be created to fit virtually any need Variables can be created for any valid data type Also, it is possible to specify constants of C/C++’s built-in types In this section, various features relating to data types, variables, and constants are discussed The Basic Types C89 defines the following elemental data types: Type Keyword Character char Integer int Floating point float Double floating double point Valueless void To these, C99 adds the following: Type Keyword Boolean (true/false) _Bool Complex _Complex Imaginary _Imaginary C++ defines the following basic types: Type Keyword Boolean (true/false) bool Character char Integer int Floating point float Double floating double point Valueless void Wide character wchar_t As you can see, all versions of C and C++ provide the following five basic types: char, int, float, double, and void Also notice that the keyword for the Boolean type is bool in C++ and _Bool in C99 No Boolean type is included in C89 Several of the basic types can be modified using one or more of these type modifiers: signed unsigned short long The type modifiers precede the type name that they modify The basic arithmetic types, including modifiers, allowed by C and C++ are shown in the following table along with their guaranteed minimum ranges Most compilers will exceed the minimums for one or more types Also, if your computer uses two’s complement arithmetic (as most do), then the smallest negative value that can be stored by a signed integer will be one more than the minimums shown For example, the range of an int for most computers is –32,768 to 32,767 Whether type char is signed or unsigned is implementation dependent Type Minimum Range char –127 to 127 or 0 to 255 unsigned char 0 to 255 signed char –127 to 127 replace( ) algorithm, 300–301 replace_copy( ) algorithm, 300–301 replace_copy_if( ) algorithm, 300–301 replace_if( ) algorithm, 300–301 resetiosflags( ) manipulator, 216, 240 restrict type qualifier, 13, 80–81, 98, 125 return statement, 19, 81 reverse( ) algorithm, 301 reverse_copy( ) algorithm, 301 reverse_iterator type, 266, 310 rewind( ), 115 right format flag, 215, 216, 239, 240 manipulator, 240 rint( ), 157 rotate( ) algorithm, 301 rotate_copy( ) algorithm, 301 round( ), 157 RTTI (runtime type identification), 49, 91 runtime_error class, 323 Index S scalbn( ), 157–158 scalbln( ), 158 scanf( ), 115–119 Scanset, 118 scientific format flag, 215, 216, 239–240 manipulator, 240 Scope resolution (::) operator, 25, 48, 74 Scopes, 24–25 _ _sealed, 341 search( ) algorithm, 302 search_n( ) algorithm, 302 SEEK_CUR, 106 seek_dir type, 229 seekdir type, 243, 256 SEEK_END, 106 seekg( ), 228–229, 256 seekp( ), 228–229, 256 SEEK_SET, 106 set container, 266, 282 member functions, table of, 282–284 header, 35, 265, 266 set_difference( ) algorithm, 302–303 set_intersection( ) algorithm, 303 set_symmetric_difference( ) algorithm, 303 set_terminate( ), 90, 322 set_unexpected( ), 323 set_union( ) algorithm, 305 setbase( ) manipulator, 216, 241 setbuf( ), 119 setf( ), 229–230, 239, 257 setfill( ) manipulator, 216, 241 setiosflags( ) manipulator, 216, 241 setjmp( ), 190–191, 194 header, 34, 190, 194 setlocale( ), 175–176 setmode( ), 230 setprecision( ) manipulator, 216, 241 setstate( ), 257 setvbuf( ), 119–120 setw( ) manipulator, 216, 241 Shift operators, 39, 40–41 short data type modifier, 2, 81 showbase format flag, 215, 239 manipulator, 241 showpoint format flag, 215, 216, 239 manipulator, 241 showpos format flag, 215, 239 manipulator, 241 SIGABRT, 193 SIG_DFL, 195 SIG_ERR, 195 SIG_IGN, 195 SIGFPE, 193 SIGILL, 193 SIGINT, 193 signal( ), 194–195 header, 34, 193, 194, 195 signbit( ), 139 signed data type modifier, 2, 81 SIGSEGV, 193 SIGTERM, 193 sin( ), 140, 158, 167 sinh( ), 159, 167 Size generic type, 287 sizeof compile-time operator, 45, 82 size_t data type, 97, 125, 183, 203 size_type type, 266, 310 skipws format flag, 215, 239 manipulator, 241 snprintf( ), 120 sort( ) algorithm, 305 sort_heap( ) algorithm, 306 sprintf( ), 120–121 sqrt( ), 159, 168 srand( ), 195 sscanf( ), 121 header, 35, 238 stable_partition( ) algorithm, 306 stable_sort( ) algorithm, 306 Stack, 20, 21 stack container, 266, 284 member functions, table of, 284–285 header, 35, 266 Standard Template Library (STL), C++, 35, 261–308 static storage class specifier, 9, 10, 11, 24, 82 static_cast, 45, 46, 82 std namespace, 25, 36, 213, 235 header, 34, 124, 199 header, 16, 34, 62, 337 _ _STDC_ _, 59 _ _STDC_HOSTED_ _, 59 _ _STDC_IEC_559_ _, 59 _ _STDC_IEC_559_COMPLEX_ _, 59 _ _STDC_ISO_10646_ _, 59 _ _STDC_VERSION_ _, 59 header, 34, 113, 119 stderr, 97 header, 35, 321, 323 stdin, 97 header, 34, 333–334 stdio format flag, 215, 216 header, 34, 97 header, 34, 179, 183 stdout, 97 Storage class specifiers, 10–11 str( ) stringstream, istringstream, and ostringstream member, 258 strstream member, 230 strcat( ), 131, 321 strchr( ), 131 strcmp( ), 131–132, 321 strcoll( ), 132 strcpy( ), 132 strcspn( ), 133 streambuf class, 214, 237 header, 35, 238 streamoff data type, 228, 242 streampos data type, 229, 232, 242 Streams, 97 C++ predefined, 214–215, 238 streamsize data type, 242 strerror( ), 133 strftime( ), 176–177 String(s) C++ implementation of, 309–321 constants, 15 conversion functions, wide-character, 208 functions, wide-character, 207–208 handling functions, 125 string class, 309 header, 35, 309 header, 34, 125 stringbuf class, 237 stringstream class, 237 stringstream( ), 258 strlen( ), 133 strncat( ), 133–134 strncmp( ), 134 strncpy( ), 134–135 strpbrk( ), 135 strrchr( ), 135 strspn( ), 136 strstr( ), 136 strstrea.h header file, 214 strstream( ), 230–231 strstream class, 214 strtod( ), 195–196 strtof( ), 196 strtok( ), 136–137 strtol( ), 196–197 strtold( ), 197 strtoll( ), 197 strtoul( ), 198 strtoull( ), 198 struct keyword, 7, 8, 10, 83 Structures, 7, 83 vs classes, 8 strxfrm( ), 138 swap( ) algorithm, 307 swap_ranges( ) algorithm, 307 switch statement, 63, 82–83 sync_with_stdio( ), 232, 259 system( ), 199 Index T T generic type, 266, 287, 324 Tags (in C), 10 tan( ), 159, 168 tanh( ), 160, 168 tellg( ), 232, 259 tellp( ), 232, 259 template keyword, 6, 19, 68, 84–88 terminate( ), 90, 323 terminate_handler type, 322 tgamma( ), 160 header, 34, 336 this pointer, 70, 76, 88 throw statement, 88–90 time( ), 178 _ _TIME_ _, 59 header, 34, 169 time_t data type, 169, 178 tm structure, 169, 174 tmpfile( ), 121 TMP_MAX, 121, 122 tmpnam( ), 122 tolower( ), 138 toupper( ), 138 towctrans( ), 204–205 traits_type, 311 transform( ) algorithm, 304–305, 307 true, 16, 62, 91, 337 True and false in C/C++, 37 trunc( ), 160 try statement, 88–90, 91 _ _try_cast, 341 Two’s complement, 2 type_info class, 49, 91 typedef, 14, 91 typeid operator, 49, 91 header, 35, 49 typename, 92 _ _typeof, 341 Types, basic data, 1–3 Index U ULLONG_MAX, 198 ULONG_MAX, 198 uncaught_exception( ), 323 #undef, 57 underflow_error exception, 323 unexpected( ), 322, 323 unexpected_handler type, 322 ungetc( ), 122–123 union keyword, 7, 10, 92 Unions, 7–9, 92 unique( ) algorithm, 308 unique_copy( ) algorithm, 308 unitbuf format flag, 215, 216, 239, 240 manipulator, 241 unmanaged pragma, 342 UnPred, 263, 287 unsetf( ), 232–233, 239, 260 unsigned data type modifier, 2, 3, 92 upper_bound( ) algorithm, 308 uppercase format flag, 215, 239 manipulator, 241 #using preprocessor directive, 342 using statement, 25, 36, 74, 92, 235 header, 35, 263, 327 Index V va_arg( ), 199–201 _ _VA_ARGS_ _, 60 va_copy( ), 199, 201 va_end( ), 199–201 va_list data type, 124, 199 _ _value, 342 value_compare type, 266 value_type type, 266, 311 Variables, 3 global, 24 local, 24 va_start( ), 199–201 vector container, 261, 266, 285 demonstration program, 264–265 member functions, table of, 285–287 header, 35, 266 vfprintf( ), 123 vfscanf( ), 123–124 virtual, 30, 93 Virtual functions, 32, 49, 93, 94 void, 1, 2, 19, 23, 93 volatile type qualifier, 12–13, 94 overriding, 46, 65 vprintf( ), 123 vscanf( ), 123–124 vsnprintf( ), 123 vsprintf( ), 123 vsscanf( ), 123–124 Index W wcerr stream, 238 header, 34, 203, 205, 207, 208, 209 WCHAR_MAX, 203 WCHAR_MIN, 203 wchar_t, 1, 94, 183, 203 wcin stream, 238 wclog, 238 wcout stream, 238 wcrtomb( ), 211 wcsrtomb( ), 211 wcstombs( ), 201 wctob( ), 211 wctomb( ), 201 wctrans( ), 204–205 wctrans_t type, 203 wctype( ), 204 header, 34, 203 wctype_t type, 203 WEOF, 203 what( ), 244, 322 while loop, 94–95 width( ), 233, 260 WinMain( ), 26 wint_t type, 203 write( ), 233, 260 ws manipulator, 216, 241 wstreampos type, 242 wstring class, 309 Index X XOR (^) bitwise operator, 39, 40 ... If access is public, all public and protected members of the base class become public and protected members of the derived class, respectively If access is private, all public and protected members of the base class become private members of the derived class...Chapter 6 - Functions The String and Character Functions The C Mathematical Chapter 8 Functions The C+ + Mathematical Chapter 9 Functions Time, Date, and Chapter 10 Localization Functions... the base class become private members of the derived class If access is protected, all public and protected members of the base class become protected members of the derived class In the following class hierarchy, derived inherits base as private