Making the Transition to ANSI C
Total Page:16
File Type:pdf, Size:1020Kb
Making the Transition to ANSI C SunSoft, Inc. A Sun Microsystems, Inc. Business 2550 Garcia Avenue Mountain View, CA 94043 USA 415 960-1300 fax 415 969-9131 Part No.: 802-5776-10 Revision A, December 1996 Copyright 1996 Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, California 94043-1100 U.S.A. All rights reserved. This product or document is protected by copyright and distributed under licenses restricting its use, copying, distribution, and decompilation. No part of this product or document may be reproduced in any form by any means without prior written authorization of Sun and its licensors, if any. Portions of this product may be derived from the UNIX® system, licensed from Novell, Inc., and from the Berkeley 4.3 BSD system, licensed from the University of California. UNIX is a registered trademark in the United States and other countries and is exclusively licensed by X/Open Company Ltd. Third-party software, including font technology in this product, is protected by copyright and licensed from Sun’s suppliers. RESTRICTED RIGHTS: Use, duplication, or disclosure by the U.S. Government is subject to restrictions of FAR 52.227- 14(g)(2)(6/87) and FAR 52.227-19(6/87), or DFAR 252.227-7015(b)(6/95) and DFAR 227.7202-3(a). Sun, Sun Microsystems, the Sun logo, SunSoft, Solaris, OpenWindows, and Sun WorkShop are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the United States and other countries. Products bearing SPARC trademarks are based upon an architecture developed by Sun Microsystems, Inc. Intel is a registered trademark of Intel Corporation. PowerPC is a trademark of International Business Machines Corporation. The OPEN LOOK® and Sun™ Graphical User Interfaces were developed by Sun Microsystems, Inc. for its users and licensees. Sun acknowledges the pioneering efforts of Xerox in researching and developing the concept of visual or graphical user interfaces for the computer industry. Sun holds a non-exclusive license from Xerox to the Xerox Graphical User Interface, which license also covers Sun’s licensees who implement OPEN LOOK GUIs and otherwise comply with Sun’s written license agreements. THIS PUBLICATION IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. Please Recycle Contents Preface. ix 1. Making the Transition to ANSI C . 1 Basic Modes . 2 A Mixture of Old- and New-Style Functions . 2 Writing New Code. 3 Updating Existing Code . 3 Mixing Considerations . 4 Functions with Varying Arguments . 6 Promotions: Unsigned Versus Value Preserving . 10 Background. 10 Compilation Behavior . 10 First Example: The Use of a Cast . 11 Bit-fields . 12 Second Example: Same Result . 12 Integral Constants . 12 iii Third Example: Integral Constants . 13 Tokenization and Preprocessing . 14 ANSI C Translation Phases. 14 Old C Translation Phases . 15 Logical Source Lines . 16 Macro Replacement . 16 Using Strings . 17 Token Pasting . 18 const and volatile. 18 Types, Only for lvalues . 19 Type Qualifiers in Derived Types . 19 const Means readonly . 20 Examples of const Usage . 21 volatile Means Exact Semantics . 21 Examples of volatile Usage. 21 Multibyte Characters and Wide Characters . 22 Asian Languages Require Multibyte Characters . 22 Encoding Variations . 23 Wide Characters. 23 Conversion Functions . 24 C Language Features . 24 Standard Headers and Reserved Names. 26 Balancing Process. 26 Standard Headers . 27 iv Making the Transition to ANSI C Names Reserved for Implementation Use . 27 Names Reserved for Expansion . 28 Names Safe to Use . 29 Internationalization . 29 Locales. 30 The setlocale() Function . 30 Changed Functions . 31 New Functions . 32 Grouping and Evaluation in Expressions . 33 Definitions . 33 The K&R C Rearrangement License . 34 The ANSI C Rules . 35 The Parentheses . 35 The As If Rule. 35 Incomplete Types . 36 Types . 36 Completing Incomplete Types . 37 Declarations . 37 Expressions. 38 Justification. 38 Examples. 39 Compatible and Composite Types. 39 Multiple Declarations . 40 Separate Compilation Compatibility. 40 v Single Compilation Compatibility. 40 Compatible Pointer Types. 41 Compatible Array Types. 41 Compatible Function Types . 41 Special Cases . 42 Composite Types . 42 A. K&R Sun C / Sun ANSI C Differences . 43 Index . 53 vi Making the Transition to ANSI C Tables Table P-1 . Typographic Conventions . x Table 1-1 Trigraph Sequences . 14 Table 1-2 Multibyte Character Conversion Functions . 24 Table 1-3 Standard Headers . 27 Table 1-4 Names Reserved for Expansion. 28 Table A-1 K&R Sun C Incompatibilities with Sun ANSI C . 44 Table A-2 ANSI C Standard Keywords . 52 Table A-3 Sun C (K&R) Keywords . 52 vii viii Making the Transition to ANSI C Preface This manual describes: • Features of ANSI C, such as internationalization and prototyping • Differences between ANSI standard-conformant C and other versions of C • Techniques for writing new and upgrading existing C code to comply with the ANSI C language specification The information is presented as a series of articles, each covering a specific transition topic. These articles were originally written for an in-house AT&T newsletter by David Prosser, Distinguished Member of Technical Staff, AT&T Bell Laboratories. Comments by Vijay Tatkar and Walter Nielsen of Sun Microsystems, Inc. have also been incorporated. Appendix A, “K&R Sun C / Sun ANSI C Differences,” contains much of the information from these articles in tabular form. For more information on programming in ANSI C, refer to the following manuals: • C User’s Guide—describes the C language and how to use the ANSI C compiler. • Performance Profiling Tools— contains information on many helpful programming tools, such as prof(1), gprof(1), and various profiling tools. ix We recommend two texts for programmers new to the C language: Kernighan and Ritchie (hereinafter referred to as K&R), The C Language, Second Edition, 1988, Prentice-Hall; Harbison and Steele, C: A Reference Manual, Second Edition, 1987, Prentice-Hall. For implementation-specific details not covered in this book, refer to the Application Binary Interface for your machine. Typographic Conventions The following table describes the typographic conventions and symbols used in this book. Table P-1 Typographic Conventions Typeface or Symbol Meaning Examples AaBbCc123 Names of commands, files, and Edit your .login file. directories; on-screen computer Use ls -a to list all files. output system% You have mail. AaBbCc123 User input, contrasted with on- system% su screen computer output password: AaBbCc123 Command-line placeholder: To delete a file, type rm filename. replace with a real name or value AaBbCc123 Book titles, new words or terms, Read Chapter 6 in User’s Guide. or words to be emphasized These are called class options. You must be root to do this. Code samples are included in boxes and may display the following: % C shell prompt system% $ Bourne shell prompt system$ # Superuser prompt, either shell system# x Making the Transition to ANSI C Making the Transition to ANSI C 1 This chapter contains the following sections: Basic Modes page 2 A Mixture of Old- and New-Style Functions page 2 Functions with Varying Arguments page 6 Promotions: Unsigned Versus Value Preserving page 10 Tokenization and Preprocessing page 14 const and volatile page 18 Multibyte Characters and Wide Characters page 22 Standard Headers and Reserved Names page 26 Internationalization page 29 Grouping and Evaluation in Expressions page 33 Incomplete Types page 36 Compatible and Composite Types page 39 1 1 Basic Modes The ANSI C compiler allows both old-style and new-style C code. The following -X (note case) options.