home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / books / c4yrself.db < prev    next >
Encoding:
Text File  |  1991-03-01  |  900.0 KB  |  19,668 lines

  1. %@1@%%@AB@%Microsoft QuickC Compiler - C FOR YOURSELF%@AE@%%@EH@%%@NL@%
  2.                                       %@NL@%
  3.                                       %@NL@%
  4.                                       %@NL@%
  5.                                       %@NL@%
  6.                                       %@NL@%
  7.                                       %@NL@%
  8.                                       %@NL@%
  9.  
  10. ────────────────────────────────────────────────────────────────────────────%@NL@%
  11.               %@AB@%Microsoft(R) QuickC(R) Compiler - C FOR YOURSELF%@AE@%%@NL@%
  12.                                       %@NL@%
  13.                                 %@AB@%VERSION 2.5%@AE@%%@NL@%
  14. ────────────────────────────────────────────────────────────────────────────%@NL@%
  15.                                       %@NL@%
  16.                                       %@NL@%
  17.                            MICROSOFT CORPORATION %@NL@%
  18.                                       %@NL@%
  19.                                       %@NL@%
  20. %@NL@%
  21. %@NL@%
  22. %@NL@%%@NL@%
  23. %@NL@%
  24.  
  25.  
  26. Information in this document is subject to change without notice and does
  27. not represent a commitment on the part of Microsoft Corporation. The
  28. software described in this document is furnished under a license agreement
  29. or nondisclosure agreement. The software may be used or copied only in
  30. accordance with the terms of the agreement. It is against the law to copy
  31. the software on any medium except as specifically allowed in the license or
  32. nondisclosure agreement. No part of this manual may be reproduced or
  33. transmitted in any form or by any means, electronic or mechanical, including
  34. photocopying and recording, for any purpose without the express written
  35. permission of Microsoft.  
  36. (C) Copyright Microsoft Corporation, 1988, 1990. All rights reserved.%@NL@%
  37. %@NL@%
  38.  
  39. Printed and bound in the United States of America.%@NL@%
  40. %@NL@%
  41.  
  42. Microsoft, MS, MS-DOS, CodeView, QuickC, and XENIX are 
  43. registered trademarks and Windows is a trademark of Microsoft Corporation.%@NL@%
  44. %@NL@%
  45. AT&T is a registered trademark of American Telephone 
  46. and Telegraph Company.%@NL@%
  47. %@NL@%
  48. IBM and PS/2 are registered trademarks of International 
  49. Business Machines Corporation.%@NL@%
  50. %@NL@%
  51. Hercules is a registered trademark and InColor is a trademark 
  52. of Hercules Computer Technology.%@NL@%
  53. %@NL@%
  54. Intel is a registered trademark of Intel Corporation.%@NL@%
  55. %@NL@%
  56. Olivetti is a registered trademark of Ing. C. Olivetti.%@NL@%
  57. %@NL@%
  58. VAX is a registered trademark of Digital Equipment Corporation.%@NL@%
  59. %@NL@%
  60. %@AI@%%@AE@%%@NL@%
  61. %@NL@%
  62. Document No. SY10423-0290
  63.  OEM D703-2Z
  64.  
  65. 10 9 8 7 6 5 4 3 2 1 %@NL@%
  66. %@NL@%
  67. %@NL@%
  68. %@NL@%
  69. %@NL@%
  70. %@NL@%
  71. %@NL@%
  72. %@1@%%@AB@%Table of Contents%@AE@%%@EH@%%@NL@%
  73. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@NL@%
  74. %@NL@%
  75.  
  76. %@NL@%
  77. %@AB@%Introduction%@AE@%%@BO:        5099@%%@NL@%
  78.      About This Book%@BO:        5274@%%@NL@%
  79.      Using the Example Programs%@BO:        5b81@%%@NL@%
  80.      Programming Style Used in This Manual%@BO:        6260@%%@NL@%
  81.      Key to Document Conventions%@BO:        692a@%%@NL@%
  82.      Other Books on C Programming%@BO:        7490@%%@NL@%
  83. %@NL@%
  84. %@NL@%
  85. %@AB@%PART I%@AE@%%@BO:        81fd@%  %@AB@%Learning C%@AE@%%@NL@%
  86. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@NL@%
  87. %@NL@%
  88. %@NL@%
  89. %@AB@%Chapter 1%@AE@%%@BO:        86b5@%  %@AB@%Anatomy of a C Program%@AE@%%@NL@%
  90. %@NL@%
  91.      A Typical C Program%@BO:        89da@%%@NL@%
  92.      Comments%@BO:        8bf6@%%@NL@%
  93.      Statements%@BO:        8eb7@%%@NL@%
  94.      Keywords and Names%@BO:        923e@%%@NL@%
  95.      Preprocessor Directives%@BO:        94b5@%%@NL@%
  96.      Functions%@BO:        9a77@%%@NL@%
  97.      Calling Functions%@BO:        a080@%%@NL@%
  98.      Declaring and Initializing Variables%@BO:        a7c9@%%@NL@%
  99.      External and Local Variables%@BO:        ac47@%%@NL@%
  100.      Function Prototypes%@BO:        b186@%%@NL@%
  101.      A Few Words about printf%@BO:        b5ef@%%@NL@%
  102. %@NL@%
  103. %@AB@%Chapter 2%@AE@%%@BO:        c3e4@%  %@AB@%Functions%@AE@%%@NL@%
  104. %@NL@%
  105.      Functions and Structured Programming%@BO:        c690@%%@NL@%
  106.      The main Function%@BO:        cd12@%%@NL@%
  107.      Placement and Visibility of Functions%@BO:        d49b@%%@NL@%
  108.      Function Definitions and Prototypes%@BO:        db70@%%@NL@%
  109.      Calling a Function%@BO:        e170@%%@NL@%
  110.      Passing Arguments to a Function%@BO:        ea85@%%@NL@%
  111.      Arguments Versus Parameters%@BO:        f4f6@%%@NL@%
  112.      Assigning Parameters%@BO:        f8fd@%%@NL@%
  113.      Passing by Value%@BO:       10322@%%@NL@%
  114.      Returning Values from Functions%@BO:       1128e@%%@NL@%
  115.      Using Return Values%@BO:       1211e@%%@NL@%
  116.      Declaring a Function's Return Type%@BO:       12a14@%%@NL@%
  117.      Function Prototypes%@BO:       1329c@%%@NL@%
  118.             Prototyping Functions without Parameters%@BO:       13e13@%%@NL@%
  119.             Prototyping Functions with Variable Parameters%@BO:       140ab@%%@NL@%
  120.      Old-Style Function Declarations and Definitions%@BO:       145b0@%%@NL@%
  121. %@NL@%
  122. %@AB@%Chapter 3%@AE@%%@BO:       15403@%  %@AB@%Flow Control%@AE@%%@NL@%
  123. %@NL@%
  124.      Loops: while, do, and for%@BO:       157a0@%%@NL@%
  125.             The while Statement%@BO:       1590a@%%@NL@%
  126.             The do Statement%@BO:       164be@%%@NL@%
  127.             The for Statement%@BO:       16e08@%%@NL@%
  128.      Decision-Making Statements: if, else, switch, break, continue,
  129.        and goto%@BO:       181f0@%%@NL@%
  130.             The if Statement%@BO:       183ac@%%@NL@%
  131.             The else Clause%@BO:       18f6a@%%@NL@%
  132.             The switch Statement%@BO:       1989f@%%@NL@%
  133.             The break Statement%@BO:       1b74e@%%@NL@%
  134.             The continue Statement%@BO:       1c4c1@%%@NL@%
  135.             The goto Statement%@BO:       1cb4f@%%@NL@%
  136. %@NL@%
  137. %@AB@%Chapter 4%@AE@%%@BO:       1d5ce@%  %@AB@%Data Types%@AE@%%@NL@%
  138. %@NL@%
  139.      Basic Data Types%@BO:       1d825@%%@NL@%
  140.             Specifying Basic Types%@BO:       1db1a@%%@NL@%
  141.             Specifying Variables%@BO:       1f426@%%@NL@%
  142.             Specifying Constants%@BO:       1fa02@%%@NL@%
  143.      Aggregate Data Types%@BO:       20f98@%%@NL@%
  144.             Arrays%@BO:       2145d@%%@NL@%
  145.             Structures%@BO:       2479f@%%@NL@%
  146.             Unions%@BO:       288e4@%%@NL@%
  147. %@NL@%
  148. %@AB@%Chapter 5%@AE@%%@BO:       29488@%  %@AB@%Advanced Data Types%@AE@%%@NL@%
  149. %@NL@%
  150.      Visibility%@BO:       2980f@%%@NL@%
  151.             Local Variables%@BO:       29ed3@%%@NL@%
  152.             External Variables%@BO:       2ade4@%%@NL@%
  153.             Visibility in Multiple Source Files%@BO:       2b898@%%@NL@%
  154.             Visibility of Functions%@BO:       2c75c@%%@NL@%
  155.      Lifetime%@BO:       2cddf@%%@NL@%
  156.             Extending the Lives of Local Variables%@BO:       2d4b4@%%@NL@%
  157.      Converting Data Types%@BO:       2df7f@%%@NL@%
  158.             Ranking of Data Types%@BO:       2e47d@%%@NL@%
  159.             Promotions and Demotions%@BO:       2e7b8@%%@NL@%
  160.             Automatic Type Conversions%@BO:       2ecf4@%%@NL@%
  161.             Manual Type Conversions through Casting%@BO:       302b3@%%@NL@%
  162.      Register Variables%@BO:       30c2e@%%@NL@%
  163.      Renaming Existing Types with typedef%@BO:       31945@%%@NL@%
  164.      The Enumeration Type%@BO:       31ecb@%%@NL@%
  165. %@NL@%
  166. %@AB@%Chapter 6%@AE@%%@BO:       32e30@%  %@AB@%Operators%@AE@%%@NL@%
  167. %@NL@%
  168.      Introducing C's Operators%@BO:       33370@%%@NL@%
  169.             Arithmetic Operators%@BO:       3358d@%%@NL@%
  170.             Relational Operators%@BO:       33a53@%%@NL@%
  171.             Assignment Operators%@BO:       3429e@%%@NL@%
  172.      C's Unique Operators%@BO:       34b0c@%%@NL@%
  173.             Increment and Decrement Operators%@BO:       34c2c@%%@NL@%
  174.             Bitwise Operators%@BO:       35648@%%@NL@%
  175.             Logical Operators%@BO:       362e1@%%@NL@%
  176.             Address Operators%@BO:       36dbb@%%@NL@%
  177.             Conditional Operator%@BO:       375c8@%%@NL@%
  178.             The sizeof Operator%@BO:       37974@%%@NL@%
  179.             Comma Operator%@BO:       37dcb@%%@NL@%
  180.             Base Operator%@BO:       384e1@%%@NL@%
  181.      Operator Precedence%@BO:       38659@%%@NL@%
  182. %@NL@%
  183. %@AB@%Chapter 7%@AE@%%@BO:       39f56@%  %@AB@%Preprocessor Directives%@AE@%%@NL@%
  184. %@NL@%
  185.      The #include Directive%@BO:       3a6f3@%%@NL@%
  186.             Specifying Include Files%@BO:       3b19f@%%@NL@%
  187.      The #define and #undef Directives%@BO:       3b7a3@%%@NL@%
  188.             Simple Text Replacement%@BO:       3b937@%%@NL@%
  189.             Function-Like Macros%@BO:       3bfdb@%%@NL@%
  190.             The #undef Directive%@BO:       3cc72@%%@NL@%
  191.      Conditional Directives%@BO:       3cf7c@%%@NL@%
  192.             The defined Operator%@BO:       3dd70@%%@NL@%
  193.      Pragmas%@BO:       3ec24@%%@NL@%
  194. %@NL@%
  195. %@AB@%Chapter 8%@AE@%%@BO:       3f2a2@%  %@AB@%Pointers%@AE@%%@NL@%
  196. %@NL@%
  197.      Using Pointers in C%@BO:       3f671@%%@NL@%
  198.      Pointers to Simple Variables%@BO:       3faa0@%%@NL@%
  199.             Declaring a Pointer Variable%@BO:       40199@%%@NL@%
  200.             How Pointers Are Stored%@BO:       413dd@%%@NL@%
  201.             Initializing a Pointer Variable%@BO:       4196c@%%@NL@%
  202.             Using a Pointer Variable%@BO:       42427@%%@NL@%
  203.             Summary of Pointer Basics%@BO:       42d9e@%%@NL@%
  204.      Pointers to Arrays%@BO:       430d5@%%@NL@%
  205.             Arrays and Pointer Arithmetic%@BO:       43c05@%%@NL@%
  206.             Comparing Pointers%@BO:       44c3c@%%@NL@%
  207.             PARRAY.C Revisited%@BO:       45138@%%@NL@%
  208.      Pointers and Strings%@BO:       45ade@%%@NL@%
  209.      Passing Pointers to Functions%@BO:       471d9@%%@NL@%
  210.             Passing Address Constants Versus Passing Pointer Variables%@BO:       48a6d@%%@NL@%
  211.      Arrays of Pointers%@BO:       49648@%%@NL@%
  212.      A Pause for Reflection%@BO:       4b45c@%%@NL@%
  213. %@NL@%
  214. %@AB@%Chapter 9%@AE@%%@BO:       4b6ea@%  %@AB@%Advanced Pointers%@AE@%%@NL@%
  215. %@NL@%
  216.      Pointers to Pointers%@BO:       4b8e2@%%@NL@%
  217.             Equivalence of Array and Pointer Notation%@BO:       4c4df@%%@NL@%
  218.             Getting Command-Line Arguments%@BO:       4d72b@%%@NL@%
  219.             Null Pointers%@BO:       4e1ed@%%@NL@%
  220.      Pointers to Structures%@BO:       4e98e@%%@NL@%
  221.      Pointers to Functions%@BO:       5010b@%%@NL@%
  222.             Passing Function Pointers as Arguments%@BO:       51379@%%@NL@%
  223.      A Parting Word on Pointers%@BO:       51b50@%%@NL@%
  224. %@NL@%
  225. %@AB@%Chapter 10%@AE@%%@BO:       51f42@%  %@AB@%Programming Pitfalls%@AE@%%@NL@%
  226. %@NL@%
  227.      Operator Problems%@BO:       52215@%%@NL@%
  228.             Confusing Assignment and Equality Operators%@BO:       52307@%%@NL@%
  229.             Confusing Operator Precedence%@BO:       52aab@%%@NL@%
  230.             Confusing Structure-Member Operators%@BO:       53400@%%@NL@%
  231.      Array Problems%@BO:       53843@%%@NL@%
  232.             Array Indexing Errors%@BO:       53941@%%@NL@%
  233.             Omitting an Array Subscript in Multidimensional Arrays%@BO:       53d4f@%%@NL@%
  234.             Overrunning Array Boundaries%@BO:       542c0@%%@NL@%
  235.      String Problems%@BO:       54694@%%@NL@%
  236.             Confusing Character Constants and Character Strings%@BO:       54791@%%@NL@%
  237.             Forgetting the Null Character That Terminates Strings%@BO:       54b82@%%@NL@%
  238.             Forgetting to Allocate Memory for a String%@BO:       551dd@%%@NL@%
  239.      Pointer Problems%@BO:       557b2@%%@NL@%
  240.             Using the Wrong Address Operator to Initialize a Pointer%@BO:       5591b@%%@NL@%
  241.             Declaring a Pointer with the Wrong Type%@BO:       5609e@%%@NL@%
  242.             Using Dangling Pointers%@BO:       56776@%%@NL@%
  243.      Library-Function Problems%@BO:       56f72@%%@NL@%
  244.             Failing to Check Return Values from Library Functions%@BO:       57106@%%@NL@%
  245.             Duplicating Library-Function Names%@BO:       57615@%%@NL@%
  246.             Forgetting to Include Header Files for Library Functions%@BO:       57cf3@%%@NL@%
  247.             Omitting the Address-Of Operator When Calling scanf%@BO:       58545@%%@NL@%
  248.      Macro Problems%@BO:       58b2d@%%@NL@%
  249.             Omitting Parentheses from Macro Arguments%@BO:       58ca3@%%@NL@%
  250.             Using Increment and Decrement Operators in Macro Arguments%@BO:       593cc@%%@NL@%
  251.      Miscellaneous Problems%@BO:       5a43c@%%@NL@%
  252.             Mismatching if and else Statements%@BO:       5a4fb@%%@NL@%
  253.             Misplacing Semicolons%@BO:       5ab17@%%@NL@%
  254.             Omitting Double Backslashes in DOS Path Specifications%@BO:       5b5dd@%%@NL@%
  255.             Omitting break Statements from a switch Statement%@BO:       5ba10@%%@NL@%
  256.             Mixing Signed and Unsigned Values%@BO:       5bfed@%%@NL@%
  257. %@NL@%
  258. %@NL@%
  259. %@AB@%PART II%@AE@%%@BO:       5c92a@%  %@AB@%Using C%@AE@%%@NL@%
  260. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@NL@%
  261. %@NL@%
  262. %@NL@%
  263. %@AB@%Chapter 11%@AE@%%@BO:       5cd0a@%  %@AB@%Input and Output%@AE@%%@NL@%
  264. %@NL@%
  265.      Input and Output Streams%@BO:       5d111@%%@NL@%
  266.      Screen and Keyboard I/O%@BO:       5d691@%%@NL@%
  267.             Manipulating and Printing Strings%@BO:       5d89d@%%@NL@%
  268.             Printing Numeric Values%@BO:       5fce6@%%@NL@%
  269.             Using scanf for Keyboard Input%@BO:       61d67@%%@NL@%
  270.      Standard Disk I/O%@BO:       63951@%%@NL@%
  271.             Creating and Writing to a Text File%@BO:       63da7@%%@NL@%
  272.             Reading a Text File in Binary Mode%@BO:       6575f@%%@NL@%
  273.             Binary and Text Files%@BO:       669bd@%%@NL@%
  274.             Text Format for Numeric Variables%@BO:       67d2f@%%@NL@%
  275.             Using Binary Format%@BO:       6a107@%%@NL@%
  276.      Low-Level Input and Output%@BO:       6c9fd@%%@NL@%
  277.             Low-Level Reading and Writing%@BO:       6cf9a@%%@NL@%
  278. %@NL@%
  279. %@AB@%Chapter 12%@AE@%%@BO:       6ea5f@%  %@AB@%Dynamic Memory Allocation%@AE@%%@NL@%
  280. %@NL@%
  281.      Why Allocate?%@BO:       6ed94@%%@NL@%
  282.      Memory Allocation Basics%@BO:       6f425@%%@NL@%
  283.             Preparing to Allocate Memory%@BO:       7028c@%%@NL@%
  284.             Specifying the Size of the Allocated Block%@BO:       70937@%%@NL@%
  285.             A Graphic Illustration%@BO:       70d10@%%@NL@%
  286.             Assigning the Address that malloc Returns%@BO:       71653@%%@NL@%
  287.             Checking the Return from malloc%@BO:       71ae1@%%@NL@%
  288.             Accessing an Allocated Memory Block%@BO:       722b1@%%@NL@%
  289.             Allocating Memory for Different Data Types%@BO:       72758@%%@NL@%
  290.             Deallocating Memory with the free Function%@BO:       73367@%%@NL@%
  291.      Specialized Memory-Allocating Functions%@BO:       738b2@%%@NL@%
  292.             The calloc Function%@BO:       73a39@%%@NL@%
  293.             The realloc Function%@BO:       73f87@%%@NL@%
  294.      Keeping Out of Trouble%@BO:       745d9@%%@NL@%
  295. %@NL@%
  296. %@AB@%Chapter 13%@AE@%%@BO:       74e41@%  %@AB@%Graphics%@AE@%%@NL@%
  297. %@NL@%
  298.      Graphics Mode%@BO:       750c7@%%@NL@%
  299.             Checking the Current Video Mode%@BO:       75988@%%@NL@%
  300.             Setting the Video Mode%@BO:       75f2c@%%@NL@%
  301.             Writing a Graphics Program%@BO:       76931@%%@NL@%
  302.             Using Color Graphics Modes%@BO:       79fdf@%%@NL@%
  303.             Using the Color Video Text Modes%@BO:       7e660@%%@NL@%
  304.      Text Coordinates%@BO:       7f578@%%@NL@%
  305.      Graphics Coordinates%@BO:       7f95a@%%@NL@%
  306.             The Physical Screen%@BO:       7fb3b@%%@NL@%
  307.             Viewport Coordinates%@BO:       80c43@%%@NL@%
  308.             Real Coordinates in a Window%@BO:       80e8f@%%@NL@%
  309. %@NL@%
  310. %@AB@%Chapter 14%@AE@%%@BO:       8470a@%  %@AB@%Presentation Graphics%@AE@%%@NL@%
  311. %@NL@%
  312.      Terminology%@BO:       84d2f@%%@NL@%
  313.      Presentation Graphics Program Structure%@BO:       86fcf@%%@NL@%
  314.      Five Example Chart Programs%@BO:       87d38@%%@NL@%
  315.      Palettes%@BO:       8afe9@%%@NL@%
  316.             Color Pool%@BO:       8b52d@%%@NL@%
  317.             Style Pool%@BO:       8bf55@%%@NL@%
  318.             Pattern Pool%@BO:       8c444@%%@NL@%
  319.             Character Pool%@BO:       8d543@%%@NL@%
  320.      Customizing Presentation Graphics%@BO:       8d6e6@%%@NL@%
  321.             Chart Environment%@BO:       8d8a7@%%@NL@%
  322.             titletype%@BO:       8e478@%%@NL@%
  323.             axistype%@BO:       8edea@%%@NL@%
  324.             windowtype%@BO:       91ee0@%%@NL@%
  325.             legendtype%@BO:       92eed@%%@NL@%
  326.             chartenv%@BO:       93e61@%%@NL@%
  327.      An Overview of the Presentation Graphics Functions%@BO:       951d7@%%@NL@%
  328. %@NL@%
  329. %@AB@%Chapter 15%@AE@%%@BO:       95d73@%  %@AB@%Fonts%@AE@%%@NL@%
  330. %@NL@%
  331.      QuickC Fonts%@BO:       960d5@%%@NL@%
  332.      Using QuickC's Font Library%@BO:       96dbe@%%@NL@%
  333.             Register Fonts%@BO:       97290@%%@NL@%
  334.             Set Current Font%@BO:       97732@%%@NL@%
  335.             Display Text%@BO:       98eaa@%%@NL@%
  336.      An Example Program%@BO:       9906f@%%@NL@%
  337.      A Few Hints%@BO:       99e94@%%@NL@%
  338. %@NL@%
  339. %@AB@%Chapter 16%@AE@%%@BO:       9a669@%  %@AB@%In-Line Assembly%@AE@%%@NL@%
  340. %@NL@%
  341.      Advantages of In-Line Assembly%@BO:       9aa4c@%%@NL@%
  342.      The _asm Keyword%@BO:       9af33@%%@NL@%
  343.      Using Assembly Language in _asm Blocks%@BO:       9b744@%%@NL@%
  344.             Instruction Set%@BO:       9b8c8@%%@NL@%
  345.             Expressions%@BO:       9bba5@%%@NL@%
  346.             Data Directives and Operators%@BO:       9bc91@%%@NL@%
  347.             EVEN and ALIGN Directives%@BO:       9bf0b@%%@NL@%
  348.             Macros%@BO:       9c0fc@%%@NL@%
  349.             Segment References%@BO:       9c2fe@%%@NL@%
  350.             Type and Variable Sizes%@BO:       9c41b@%%@NL@%
  351.      Using C in _asm Blocks%@BO:       9cf42@%%@NL@%
  352.             Using Operators%@BO:       9d42a@%%@NL@%
  353.             Using C Symbols%@BO:       9d950@%%@NL@%
  354.             Accessing C Data%@BO:       9de3d@%%@NL@%
  355.             Writing Functions%@BO:       9e5cd@%%@NL@%
  356.      Using and Preserving Registers%@BO:       9f2f2@%%@NL@%
  357.      Jumping to Labels%@BO:       9fbd3@%%@NL@%
  358.      Calling C Functions%@BO:       a0498@%%@NL@%
  359.      Defining _asm Blocks as C Macros%@BO:       a09db@%%@NL@%
  360.      Optimizing%@BO:       a1697@%%@NL@%
  361.      References and Books on Assembly Language%@BO:       a2124@%%@NL@%
  362. %@NL@%
  363. %@AB@%Appendix A%@AE@%%@BO:       a28b2@%  %@AB@%C Language Guide%@AE@%%@NL@%
  364. %@NL@%
  365.      General Syntax%@BO:       a2c28@%%@NL@%
  366.             User-Defined Names%@BO:       a2fdd@%%@NL@%
  367.             Keywords%@BO:       a343d@%%@NL@%
  368.      Functions%@BO:       a393a@%%@NL@%
  369.      Flow Control%@BO:       a3efa@%%@NL@%
  370.             The break Statement%@BO:       a418e@%%@NL@%
  371.             The continue Statement%@BO:       a44ad@%%@NL@%
  372.             The do Statement%@BO:       a4846@%%@NL@%
  373.             The for Statement%@BO:       a4c3b@%%@NL@%
  374.             The goto Statement%@BO:       a52f6@%%@NL@%
  375.             The if Statement%@BO:       a55e8@%%@NL@%
  376.             The return Statement%@BO:       a5a3c@%%@NL@%
  377.             The switch Statement%@BO:       a5bdd@%%@NL@%
  378.             The while Statement%@BO:       a646c@%%@NL@%
  379.      Data Types%@BO:       a67e8@%%@NL@%
  380.             Basic Data Types%@BO:       a68be@%%@NL@%
  381.             Aggregate Data Types%@BO:       a893e@%%@NL@%
  382.      Advanced Data Types%@BO:       a92e4@%%@NL@%
  383.             Visibility%@BO:       a93be@%%@NL@%
  384.             Lifetime%@BO:       a9557@%%@NL@%
  385.             Type Conversions%@BO:       a9699@%%@NL@%
  386.             User-Defined Types%@BO:       a9922@%%@NL@%
  387.             Enumerated Types%@BO:       a9ce9@%%@NL@%
  388.      Operators%@BO:       aa0ec@%%@NL@%
  389.      Preprocessor Directives%@BO:       ab6ec@%%@NL@%
  390.      Pointers%@BO:       ac78b@%%@NL@%
  391. %@NL@%
  392. %@AB@%Appendix B%@AE@%%@BO:       ace49@%  %@AB@%C Library Guide%@AE@%%@NL@%
  393. %@NL@%
  394.      Overview of the C Run-Time Library%@BO:       ad133@%%@NL@%
  395.      Buffer-Manipulation Routines%@BO:       aefe3@%%@NL@%
  396.      Character Classification and Conversion Routines%@BO:       b02f5@%%@NL@%
  397.      Data Conversion Routines%@BO:       b143e@%%@NL@%
  398.      Error Message Routines%@BO:       b259d@%%@NL@%
  399.      Graphics 1: Low-Level Graphics Routines%@BO:       b352c@%%@NL@%
  400.      Graphics 2: Presentation Graphics Routines%@BO:       bf8f5@%%@NL@%
  401.      Graphics 3: Font Display Routines%@BO:       c254d@%%@NL@%
  402.      Input and Output Routines%@BO:       c3528@%%@NL@%
  403.      Math Routines%@BO:       cca69@%%@NL@%
  404.      Memory-Allocation Routines%@BO:       d00e5@%%@NL@%
  405.      Process-Control Routines%@BO:       d1476@%%@NL@%
  406.      Searching and Sorting Routines%@BO:       d20b7@%%@NL@%
  407.      String-Manipulation Routines%@BO:       d343e@%%@NL@%
  408.      Time Routines%@BO:       d6b62@%%@NL@%
  409. %@NL@%
  410. %@AB@%Glossary%@AE@%%@BO:       d7f4f@%%@NL@%
  411. %@NL@%
  412. %@NL@%
  413. %@NL@%
  414. %@CR:C6A-Intro   @%%@1@%%@AB@%Introduction%@AE@%%@EH@%%@NL@%
  415. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  416. %@NL@%
  417. Ever since Microsoft introduced the QuickC(R) Compiler, version 1.0 in 1987,
  418. QuickC users have asked for more information on the C programming language.
  419. %@AI@%C for Yourself%@AE@% answers that need, particularly for those who have some
  420. programming experience but are new to the C language.  %@NL@%
  421. %@NL@%
  422. %@NL@%
  423. %@2@%%@CR:C6A00000001 @%%@AB@%About This Book%@AE@%%@EH@%%@NL@%
  424. %@NL@%
  425. %@AI@%C for Yourself%@AE@% assumes you have programmed before but are not familiar with
  426. C. Thus, it doesn't explain basic programming ideas such as why program
  427. loops are useful. It assumes that you already know about loops in general
  428. and now want to learn how to write them in the C language. The following
  429. list summarizes the book's contents:  %@NL@%
  430. %@NL@%
  431. %@NL@%
  432.   ■   Part 1, "Learning C," covers basic C language topics such as functions
  433.       and data types. The chapters in this section are designed to be read
  434.       in order, from beginning to end.%@NL@%
  435. %@NL@%
  436.   ■   Part 2, "Using C," covers practical programming topics such as
  437.       input/output and graphics. This section is organized topically, so you
  438.       don't have to read the chapters in any particular order.%@NL@%
  439. %@NL@%
  440.   ■   Appendix A, "C Language Guide," summarizes the QuickC implementation
  441.       of the C language. You can use this appendix as a quick reference once
  442.       you have read Part 1 and have some familiarity with C.%@NL@%
  443. %@NL@%
  444.   ■   Appendix B, "C Library Guide," summarizes the most commonly used
  445.       functions in the QuickC run-time library. This appendix is designed
  446.       mainly for browsing when you're not using QuickC. When you are in the
  447.       QuickC environment, use the Microsoft(R) QuickC Advisor (online help)
  448.       to get information about C language features and run-time library
  449.       functions.
  450. %@NL@%
  451. ────────────────────────────────────────────────────────────────────────────%@NL@%
  452. NOTE
  453.  
  454. %@AI@%The pages that follow use the term "OS/2" to refer to the OS/2 systems─
  455. %@AI@%Microsoft %@AI@%(R)%@AE@%%@AI@%%@AE@%%@AI@% Operating System/2 (MS %@AE@%%@AI@%(R)%@AE@%%@AI@%%@AE@%%@AI@% OS/2) and IBM%@AE@%%@AI@% %@AE@%%@AI@%(R)%@AE@%%@AI@%%@AE@%%@AI@% OS/2. Similarly,
  456. %@AI@%the term "DOS" refers to both the MS-DOS%@AE@%%@AI@% (R)%@AE@%%@AI@%%@AE@%%@AI@% and IBM%@AE@%%@AI@% %@AE@%%@AI@%(R) %@AE@%%@AI@%%@AE@%%@AI@%Personal Computer
  457. %@AI@%DOS operating systems. The name of a specific operating system is used when
  458. %@AI@%it is necessary to note features that are unique to that system.%@AE@%%@AI@%%@AE@%%@AE@%%@NL@%
  459. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  460. %@NL@%
  461. %@NL@%
  462. %@NL@%
  463. %@NL@%
  464. %@NL@%
  465. %@2@%%@CR:C6A00000002 @%%@AB@%Using the Example Programs%@AE@%%@EH@%%@NL@%
  466. %@NL@%
  467. The example programs in this book are available through online help. This
  468. feature allows you to load, run, and experiment with example programs as you
  469. read.  %@NL@%
  470. %@NL@%
  471. %@AU@% You can use online  help to load and run  example programs.%@AE@%  %@NL@%
  472. %@NL@%
  473. You must be using the QuickC environment to load an example. To load the
  474. program, select Contents from the Help menu, then select the title of this
  475. book. Find the desired program in Help, then copy it into the editor using
  476. QuickC's Copy and Paste functions.  %@NL@%
  477. %@NL@%
  478. After you copy a sample program into the QuickC Editor, you can treat it as
  479. you would any C source program. You can compile or edit the program, save it
  480. on disk, and so on.  %@NL@%
  481. %@NL@%
  482. QuickC online help includes all the significant examples in this book (it
  483. doesn't include code fragments and some very short programs). Every program
  484. that is in online help begins with a line in this general form:  %@NL@%
  485. %@NL@%
  486. %@AS@%  /* POINTER.C: Demonstrate pointer basics. */%@AE@%%@NL@%
  487. %@NL@%
  488. The line contains the program's name and a brief description of what it
  489. does. The program containing the above line is listed as POINTER.C in online
  490. help.  %@NL@%
  491. %@NL@%
  492. All the examples available in online help compile without errors at Warning
  493. Level 3, in which QuickC does the most stringent error-checking. At this
  494. Warning Level, some examples will generate the following harmless warnings:
  495. %@NL@%
  496. %@NL@%
  497. %@AS@%  C4103: 'main' : function definition used as prototype
  498. %@AS@%  C4035: 'main' : no return value
  499. %@AS@%  C4051: data conversion%@AE@%%@NL@%
  500. %@NL@%
  501. You can eliminate these warnings by compiling at a lower Warning Level.  %@NL@%
  502. %@NL@%
  503. %@NL@%
  504. %@2@%%@CR:C6A00000003 @%%@AB@%Programming Style Used in This Manual%@AE@%%@EH@%%@NL@%
  505. %@NL@%
  506. The C language allows considerable flexibility in formatting source code.
  507. The style used in this book is recommended for program readability, but you
  508. do not have to use it when writing your programs. Below is a list of style
  509. guidelines used in this book for example programs:  %@NL@%
  510. %@NL@%
  511. %@NL@%
  512.   ■   Each example program begins with a comment that names the program and
  513.       states what it does.%@NL@%
  514. %@NL@%
  515.   ■   Each statement or function is listed on its own line.%@NL@%
  516. %@NL@%
  517.   ■   Variable and function names are in lowercase. The names of symbolic
  518.       constants, such as TRUE or FALSE, are in uppercase.%@NL@%
  519. %@NL@%
  520.   ■   If a function doesn't take any arguments, an opening and a closing
  521.       parenthesis follow the function name with no extra space:
  522. %@NL@%
  523. %@AS@%      getch();%@AE@%%@NL@%
  524. %@NL@%
  525. %@NL@%
  526.   ■   If a function takes arguments, a space appears after the opening
  527.       parenthesis and before the closing parenthesis:
  528. %@NL@%
  529. %@AS@%      printf( "Number = %i", num_penguins );%@AE@%%@NL@%
  530. %@NL@%
  531. %@NL@%
  532.   ■   Binary operators such as addition and subtraction are preceded and
  533.       followed by a space:
  534. %@NL@%
  535. %@AS@%      3 + 5%@AE@%%@NL@%
  536. %@NL@%
  537. %@NL@%
  538.   ■   If parentheses are used to control operator precedence, no extra
  539.       spaces are included:
  540. %@NL@%
  541. %@AS@%      (3 + 5) * 2%@AE@%%@NL@%
  542. %@NL@%
  543. %@NL@%
  544.   ■   Opening and closing braces are aligned under the first character of
  545.       the controlling keyword. The block underneath is indented 3 spaces:
  546. %@NL@%
  547. %@AS@%      if( a == b )
  548. %@AS@%      {
  549. %@AS@%         c = 50;
  550. %@AS@%         printf( "%i\n", a );
  551. %@AS@%      }%@AE@%%@NL@%
  552. %@NL@%
  553. %@NL@%
  554. %@NL@%
  555. %@NL@%
  556. %@2@%%@CR:C6A00000004 @%%@AB@%Key to Document Conventions%@AE@%%@EH@%%@NL@%
  557. %@NL@%
  558. This book uses the following document conventions:  %@NL@%
  559. %@NL@%
  560. %@AB@%Example%@AE@%                           %@AB@%Description%@AE@%
  561. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  562. COPY TEST.OBJ C:                  Uppercase letters represent DOS commands
  563.                                   and file names.
  564.  
  565. %@AB@%printf%@AE@%                            Boldface letters indicate standard 
  566.                                   features of the C language: keywords, 
  567.                                   operators, and standard library 
  568.                                   functions.
  569.  
  570. %@AI@%expression%@AE@%                        Words in italics indicate placeholders 
  571.                                   for information you must supply, such as
  572.                                   a file name. Italics are also 
  573.                                   occasionally used for emphasis in the 
  574.                                   text.
  575.  
  576. %@AS@%main()%@AE@%                            This typeface is used for example 
  577. %@AS@%{%@AE@%                                 programs, program fragments, and the 
  578. %@AS@%}%@AE@%                                 names of user-defined functions and 
  579.                                   variables. It also indicates user input 
  580.                                   and screen output. %@AS@% %@AE@%
  581.  
  582. %@AB@%CL%@AE@% %@AI@%options%@AE@% «%@AI@%files...%@AE@%»             A horizontal ellipsis following an item 
  583.                                   indicates that more items having the 
  584.                                   same form may follow.
  585.  
  586. %@AS@%while( )%@AE@%%@AS@%%@AE@%                          A vertical ellipsis tells you that part 
  587. %@AS@%{%@AE@%                                 of the example program has been 
  588. %@AS@%   .%@AE@%                              intentionally omitted.
  589. %@AS@%   .%@AE@%                              
  590. %@AS@%   .%@AE@%                              
  591. %@AS@%}%@AE@%                                 
  592.  
  593. SHIFT                             Small capital letters denote names of 
  594.                                   keys on the keyboard. A plus sign ( + ) 
  595.                                   indicates a combination of keys. For 
  596.                                   example, SHIFT+F5 tells you to hold down
  597.                                   the SHIFT key while pressing the F5 key.
  598.  
  599. "array pointer"                   The first time a new term is defined, it
  600.                                   is enclosed in quotation marks. Since 
  601.                                   some knowledge of programming is assumed,
  602.                                   common terms such as memory or branch 
  603.                                   are not defined.
  604.  
  605. American National Standards       The first time an acronym appears, it is
  606. Institute (ANSI)                  spelled out.
  607.  
  608. %@NL@%
  609. %@2@%%@CR:C6A00000005 @%%@AB@%Other Books on C Programming%@AE@%%@EH@%%@NL@%
  610. %@NL@%
  611. This book provides an introduction to the C language and some practical
  612. programming topics. It does not attempt to teach you basic computer
  613. programming or advanced C programming techniques. The following books cover
  614. a variety of topics that you may find useful. They are listed only for your
  615. convenience. With the exception of its own publications, Microsoft does not
  616. endorse these books or recommend them over others on the same subject.  %@NL@%
  617. %@NL@%
  618. Feibel, Werner. %@AI@%Advanced QuickC%@AE@%,
  619.     2d ed. Berkeley, California: Osborne McGraw-Hill, 1989.%@NL@%
  620. %@NL@%
  621. %@STUB@%    An intermediate-level C programming guide using QuickC. It includes data
  622.     structures, parsing, simulations, and the DOS interface.%@NL@%
  623. %@NL@%
  624. Hancock, Les, and Morris Krieger. %@AI@%The C Primer%@AE@%,
  625.     2d ed. New York: McGraw-Hill, 1986.%@NL@%
  626. %@NL@%
  627. %@STUB@%    A beginner's guide to C programming.%@NL@%
  628. %@NL@%
  629. Hansen, Augie. %@AI@%Proficient C%@AE@%.
  630.     Redmond, Washington: Microsoft Press, 1987.%@NL@%
  631. %@NL@%
  632. %@STUB@%    An intermediate-level guide to C programming.%@NL@%
  633. %@NL@%
  634. Harbison, Samuel P., and Guy L. Steele. %@AI@%C: A Reference Manual%@AE@%,
  635.     2d ed. Englewood Cliffs, New Jersey: Prentice-Hall, 1987.%@NL@%
  636. %@NL@%
  637. %@STUB@%    A comprehensive guide to the C language and the standard library.%@NL@%
  638. %@NL@%
  639. Hergert, Douglas. %@AI@%The ABC's of QuickC%@AE@%.
  640.     Alameda, California: SYBEX, Inc., 1989.%@NL@%
  641. %@NL@%
  642. %@STUB@%    A beginner's guide to QuickC programming.%@NL@%
  643. %@NL@%
  644. Kernighan, Brian W., and Dennis M. Ritchie. %@AI@%The C Programming Language%@AE@%,
  645.      2d ed. Englewood Cliffs, New Jersey: Prentice Hall, 1988.%@NL@%
  646. %@NL@%
  647. %@STUB@%    The first edition of this book is the classic definition of the C
  648.     language. The second edition includes new information on the proposed
  649.     ANSI C standard.%@NL@%
  650. %@NL@%
  651. Lafore, Robert. %@AI@%Microsoft C Programming for the IBM%@AE@%.
  652.     Indianapolis, Indiana: Howard W. Sams & Company, 1987.%@NL@%
  653. %@NL@%
  654. %@STUB@%    The first half of the book teaches C. The second half concentrates on
  655.     specifics of the PC environment, such as BIOS calls, memory, and video
  656.     displays.%@NL@%
  657. %@NL@%
  658. Plum, Thomas. %@AI@%Learning to Program in C%@AE@%.
  659.     Hasbrouck Heights, New Jersey: Hayden Book Company, 1983.%@NL@%
  660. %@NL@%
  661. %@STUB@%    A widely used introductory college text on computer programming in C.%@NL@%
  662. %@NL@%
  663. Schustack, Steve. %@AI@%Variations in C%@AE@%.
  664.     Redmond, Washington: Microsoft Press, 1985.%@NL@%
  665. %@NL@%
  666. %@STUB@%    An intermediate-level guide to developing business applications in C.%@NL@%
  667. %@NL@%
  668. Waite, Mitchell, Stephen Prate, Bryan Costales, and Harry Henderson (The
  669. Waite Group). %@AI@%Microsoft QuickC Programming%@AE@%.
  670.     Redmond, Washington: Microsoft Press, 1988.%@NL@%
  671. %@NL@%
  672. %@STUB@%    Beginning- to intermediate-level C programming, with special emphasis on
  673.     the QuickC Compiler.%@NL@%
  674. %@NL@%
  675. Ward, Robert. %@AI@%Debugging C%@AE@%.
  676.     Indianapolis, Indiana: Que Corporation, 1986.%@NL@%
  677. %@NL@%
  678. %@STUB@%    An advanced guide to the theory and practice of debugging C programs.%@NL@%
  679. %@NL@%
  680. Wilton, Richard. %@AI@%Programmer's Guide to PC and PS/2 Video Systems%@AE@%.
  681.     Redmond, Washington: Microsoft Press, 1987.%@NL@%
  682. %@NL@%
  683. %@STUB@%    An advanced guide to all the PC and PS/2(R) video modes.%@NL@%
  684. %@NL@%
  685. %@NL@%
  686. %@NL@%
  687. %@NL@%
  688. %@NL@%
  689. %@NL@%
  690. %@CR:C6A-Part 01 @%%@1@%%@AB@%PART I  Learning C%@AE@%%@EH@%%@NL@%
  691. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  692. %@NL@%
  693. The C language has steadily increased in popularity since it was created in
  694. the early 1970s. C is currently the language of choice for many professional
  695. software developers and is becoming quite popular among nonprofessional
  696. programmers, as well.  %@NL@%
  697. %@NL@%
  698. %@AI@%C for Yourself%@AE@%  is divided into two parts. Part 1 is called "Learning C" and
  699. discusses the C language itself. This part assumes you know the fundamentals
  700. of computer programming but do not know C. Experienced C programmers may
  701. only want to skim these chapters. Part 2, "Using C," discusses practical
  702. programming capabilities, such as graphics, which are provided in the QuickC
  703. run-time library. It should be read after you have finished Part 1 and have
  704. some familiarity with the C language.  %@NL@%
  705. %@NL@%
  706. Part 1 begins with basic topics such as data types and functions, and it
  707. progresses to more advanced subjects such as pointers. This part of the book
  708. closes with a discussion of common C programming pitfalls.  %@NL@%
  709. %@NL@%
  710. %@NL@%
  711. %@NL@%
  712. %@NL@%
  713. %@NL@%
  714. %@NL@%
  715. %@CR:C6A00010001 @%%@1@%%@AB@%Chapter 1  Anatomy of a C Program%@AE@%%@EH@%%@NL@%
  716. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  717. %@NL@%
  718. As a knowledgeable programmer, you'll probably be tempted to immerse
  719. yourself in C immediately. But before taking that plunge, you should know
  720. the basic model for all C programs. This chapter sketches the anatomy of a C
  721. program without getting bogged down in formal definitions or exceptions to
  722. the rules. (You'll find plenty of rules in the chapters that follow.)  %@NL@%
  723. %@NL@%
  724. The discussion revolves around a short, reasonably typical C program named
  725. VOLUME.C. To get comfortable with the look of C programs, as well as the
  726. basic ideas that shape them, refer to VOLUME.C frequently as you read.  %@NL@%
  727. %@NL@%
  728. %@NL@%
  729. %@2@%%@CR:C6A00010002 @%%@AB@%A Typical C Program%@AE@%%@EH@%%@NL@%
  730. %@NL@%
  731. VOLUME.C is a simple program that calculates the volume of a sphere and
  732. prints the following result on the screen:  %@NL@%
  733. %@NL@%
  734. %@AS@%  Volume: 113.040001%@AE@%%@NL@%
  735. %@NL@%
  736. Like all of the sample programs in this book, you'll find VOLUME.C in
  737. QuickC's online help. The "Introduction" explains how to load sample
  738. programs. Figure 1.1 illustrates the VOLUME.C program.  %@NL@%
  739. %@NL@%
  740. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  741. %@NL@%
  742. %@NL@%
  743. %@2@%%@CR:C6A00010003 @%%@AB@%Comments%@AE@%%@EH@%%@NL@%
  744. %@NL@%
  745. The first line in VOLUME.C is a comment:  %@NL@%
  746. %@NL@%
  747. %@AS@%  /* VOLUME.C: Calculate sphere's volume. */%@AE@%%@NL@%
  748. %@NL@%
  749. %@AU@% Comments make a program more readable.%@AE@%  %@NL@%
  750. %@NL@%
  751. In C, a comment begins with a slash-asterisk (%@AB@%/*%@AE@%) and ends with an
  752. asterisk-slash (%@AB@%*/ %@AE@%). Because C is a compact language with very few
  753. keywords, comments play an important role in making your programs readable.
  754. %@NL@%
  755. %@NL@%
  756. You can't nest comments (put one comment inside another). The following line
  757. creates a syntax error:  %@NL@%
  758. %@NL@%
  759. %@AS@%  /* Error! /* You can't */ nest comments in C. */%@AE@%%@NL@%
  760. %@NL@%
  761. %@NL@%
  762. %@2@%%@CR:C6A00010004 @%%@AB@%Statements%@AE@%%@EH@%%@NL@%
  763. %@NL@%
  764. C statements always end with semicolons. Here is a statement from the
  765. VOLUME.C program:  %@NL@%
  766. %@NL@%
  767. %@AS@%  result = 4 * PI * result;%@AE@%%@NL@%
  768. %@NL@%
  769. %@AU@% Statement blocks are enclosed in braces.%@AE@%  %@NL@%
  770. %@NL@%
  771. You can also enclose a group of statements in braces, making a "statement
  772. block." Statement blocks contain related statements, such as the statements
  773. in the body of a function.  %@NL@%
  774. %@NL@%
  775. The C language ignores white space (tabs, blanks, and line breaks) in your
  776. source program, so you can arrange your program in almost any style.
  777. However, a few de facto rules help promote readability. A typical C program
  778. is written with one statement per line. Braces align vertically, and
  779. statements inside braces are indented. The "Introduction" describes the
  780. programming style used in this book.  %@NL@%
  781. %@NL@%
  782. %@NL@%
  783. %@2@%%@CR:C6A00010005 @%%@AB@%Keywords and Names%@AE@%%@EH@%%@NL@%
  784. %@NL@%
  785. C is a case-sensitive language (it distinguishes between uppercase and
  786. lowercase letters). All of C's keywords are spelled completely in lowercase;
  787. online help contains a complete list of C keywords.  %@NL@%
  788. %@NL@%
  789. You can declare names in any combination of either case, but many
  790. programmers prefer to use lowercase for variable and function names, saving
  791. uppercase for declaring symbolic constants. (A "symbolic constant" is a
  792. descriptive name that represents a constant value. In VOLUME.C, %@AS@% PI %@AE@% is a
  793. symbolic constant.)  %@NL@%
  794. %@NL@%
  795. %@NL@%
  796. %@2@%%@CR:C6A00010006 @%%@AB@%Preprocessor Directives%@AE@%%@EH@%%@NL@%
  797. %@NL@%
  798. %@AU@% A preprocessor  directive is a command to the QuickC compiler.%@AE@%  %@NL@%
  799. %@NL@%
  800. Not every line in a C program is an executable statement. Programs can also
  801. contain "preprocessor directives"─ commands for the QuickC compiler. A
  802. directive begins with a number sign (%@AB@%#%@AE@%) and does not end with a semicolon.  %@NL@%
  803. %@NL@%
  804. The second and third lines of VOLUME.C contain preprocessor directives. The
  805. %@AB@%#include%@AE@% directive in the second line tells QuickC to insert the file
  806. STDIO.H when it compiles VOLUME.C:  %@NL@%
  807. %@NL@%
  808. %@AS@%  #include <stdio.h>%@AE@%%@NL@%
  809. %@NL@%
  810. STDIO.H is one of the many "header files" supplied with QuickC. Header files
  811. contain declarations and definitions required by C library functions.
  812. ("Library functions" are supplied with QuickC rather than written by you.)
  813. In the VOLUME.C program, the %@AB@%printf%@AE@% library function requires information
  814. from the STDIO.H header file.  %@NL@%
  815. %@NL@%
  816. The %@AB@%#define%@AE@% directive in the third line defines a symbolic constant named %@AS@%
  817. %@AS@%PI%@AE@%:  %@NL@%
  818. %@NL@%
  819. %@AS@%  #define PI 3.14%@AE@%%@NL@%
  820. %@NL@%
  821. Wherever %@AS@% PI %@AE@% appears later in the source program, QuickC substitutes the
  822. text %@AS@% 3.14%@AE@%. The text can be any combination of letters, digits, or other
  823. characters. The effect is much like a search and replace operation in a word
  824. processor.  %@NL@%
  825. %@NL@%
  826. %@NL@%
  827. %@2@%%@CR:C6A00010007 @%%@AB@%Functions%@AE@%%@EH@%%@NL@%
  828. %@NL@%
  829. %@AU@% A function performs a specific task and can also return a value.%@AE@%  %@NL@%
  830. %@NL@%
  831. Functions are the building blocks of C. Every C program has at least one
  832. function, and every executable C statement appears inside some function or
  833. another. In plain English, a "function" is a group of statements that
  834. performs a specific task and often returns a value to the statement that
  835. calls it.  %@NL@%
  836. %@NL@%
  837. C functions serve the same purposes as QuickPascal procedures and functions
  838. or BASIC %@AB@%SUB%@AE@% and %@AB@%FUNCTION%@AE@% procedures. They allow you to write wellorganized
  839. programs that perform different tasks in separate parts.  %@NL@%
  840. %@NL@%
  841. %@AU@% The C language has no input/output statements.%@AE@%  %@NL@%
  842. %@NL@%
  843. C also uses functions to perform all input and output (I/O). Unlike other
  844. high-level languages, C has no I/O statements such as %@AB@%PRINT%@AE@% or %@AB@%READ%@AE@%.
  845. Instead, all I/O is done by calling C library functions such as %@AB@%printf%@AE@%.  %@NL@%
  846. %@NL@%
  847. %@AU@% Every C program has a function named%@AE@%%@AB@% main.%@AE@%  %@NL@%
  848. %@NL@%
  849. The VOLUME.C program contains two functions, named %@AB@%main%@AE@% and %@AS@%sphere%@AE@% (see
  850. Figure 1.1). The main execution section of every C program is itself a
  851. function named %@AB@%main,%@AE@% which marks where execution starts and ends. When you
  852. run VOLUME.C, execution starts at the beginning of the %@AB@%main%@AE@% function and
  853. stops at the end of %@AB@%main%@AE@%.  %@NL@%
  854. %@NL@%
  855. %@NL@%
  856. %@2@%%@CR:C6A00010008 @%%@AB@%Calling Functions%@AE@%%@EH@%%@NL@%
  857. %@NL@%
  858. Functions can be called (executed) from anywhere in a program, and they can
  859. receive values as well as return them. A value that you pass (send) to a
  860. function is called an "argument."  %@NL@%
  861. %@NL@%
  862. Calling a C function is a simple matter. You state the name of the function
  863. and supply in parentheses any arguments you want to pass to it. You must
  864. place a comma between arguments.  %@NL@%
  865. %@NL@%
  866. The VOLUME.C program contains two function calls, one to the %@AB@%printf%@AE@% library
  867. function and the other to the %@AS@% sphere %@AE@% function, which is defined in the
  868. program. The following statement calls the %@AB@%printf%@AE@% function:  %@NL@%
  869. %@NL@%
  870. %@AS@%  printf( "Volume: %f\n", volume );%@AE@%%@NL@%
  871. %@NL@%
  872. The statement passes two arguments to %@AB@%printf%@AE@%. The first, %@AS@%"Volume: %f\n"%@AE@%,
  873. supplies text and some formatting information. The second, %@AS@% volume%@AE@%, supplies
  874. a numeric value. See "A Few Words about %@AB@%printf%@AE@%," below, for more
  875. information.  %@NL@%
  876. %@NL@%
  877. In C, a function does not necessarily have to return a value. It can either
  878. return a value (like a QuickPascal function) or return nothing (like a
  879. QuickPascal  procedure).  %@NL@%
  880. %@NL@%
  881. When a function returns a value, the value is often assigned to a variable.
  882. The following statement from VOLUME.C calls the %@AS@% sphere %@AE@% function and
  883. assigns its return value to the variable %@AS@% volume%@AE@%:  %@NL@%
  884. %@NL@%
  885. %@AS@%  volume = sphere( radius );%@AE@%%@NL@%
  886. %@NL@%
  887. A function uses the %@AB@%return%@AE@% keyword to return a value. In VOLUME.C, the last
  888. statement in the %@AS@% sphere %@AE@% function returns the value of the variable %@AS@% result
  889. %@AS@%%@AE@% to the statement that calls %@AS@% sphere%@AE@%:  %@NL@%
  890. %@NL@%
  891. %@AS@%  return result;%@AE@%%@NL@%
  892. %@NL@%
  893. %@NL@%
  894. %@2@%%@CR:C6A00010009 @%%@AB@%Declaring and Initializing Variables%@AE@%%@EH@%%@NL@%
  895. %@NL@%
  896. You must "declare" every variable in a C program by stating its name and
  897. type. If you refer to an undeclared variable, QuickC displays an error
  898. message when you compile the program.  %@NL@%
  899. %@NL@%
  900. The following statement from VOLUME.C declares a %@AB@%float%@AE@% (floating-point) type
  901. variable named %@AS@% volume%@AE@%:  %@NL@%
  902. %@NL@%
  903. %@AS@%  float volume;%@AE@%%@NL@%
  904. %@NL@%
  905. After declaring a variable, you should "initialize" it─give it an initial
  906. value─before using it. Uninitialized variables might have any value, so they
  907. are dangerous to use. The VOLUME.C program initializes the variable %@AS@% volume %@AE@%
  908. by as- signing it the return value from a function call:  %@NL@%
  909. %@NL@%
  910. %@AS@%  volume = sphere( radius );%@AE@%%@NL@%
  911. %@NL@%
  912. You can also initialize a variable when it is declared, a convenient and
  913. common practice. The following statement from VOLUME.C declares the variable
  914. %@AS@% radius %@AE@% as an %@AB@%int%@AE@% (integer) variable and initializes it with the value 3:  %@NL@%
  915. %@NL@%
  916. %@AS@%  int radius = 3;%@AE@%%@NL@%
  917. %@NL@%
  918. %@NL@%
  919. %@2@%%@CR:C6A00010010 @%%@AB@%External and Local Variables%@AE@%%@EH@%%@NL@%
  920. %@NL@%
  921. The place where you declare a variable controls where it is visible. A
  922. variable declared outside any function is "external": you can refer to it
  923. anywhere within the program. (External variables are called "global" in some
  924. other languages.)  %@NL@%
  925. %@NL@%
  926. A variable declared inside the braces of a function is "local." You can
  927. refer to it inside the function but nowhere else. In VOLUME.C, the %@AS@% result %@AE@%
  928. variable is declared inside the %@AS@% sphere %@AE@% function:  %@NL@%
  929. %@NL@%
  930. %@AS@%  float sphere( int rad )
  931. %@AS@%  {
  932. %@AS@%     float result;
  933. %@AS@%     .
  934. %@AS@%     .
  935. %@AS@%     .
  936. %@AS@%  }%@AE@%%@NL@%
  937. %@NL@%
  938. %@AU@% Use external variables only when necessary.%@AE@%  %@NL@%
  939. %@NL@%
  940. Because it is local to the %@AS@% sphere %@AE@% function, the %@AS@% result %@AE@% variable cannot
  941. be used elsewhere in VOLUME.C. Making variables local whenever possible
  942. minimizes the risk that a variable's value will be changed accidentally in
  943. some other part of the program.  %@NL@%
  944. %@NL@%
  945. When a function receives arguments, the arguments become local variables
  946. within the function. The %@AS@% sphere %@AE@% function requires one argument, which it
  947. names %@AS@% rad%@AE@%. Within the function, %@AS@% rad %@AE@% is a local variable.  %@NL@%
  948. %@NL@%
  949. %@NL@%
  950. %@2@%%@CR:C6A00010011 @%%@AB@%Function Prototypes%@AE@%%@EH@%%@NL@%
  951. %@NL@%
  952. %@AU@% Function prototypes allow QuickC to check every function reference for
  953. %@AU@%accuracy.%@AE@%  %@NL@%
  954. %@NL@%
  955. A function can be declared in much the same way as a variable. Function
  956. declarations, often called "prototypes," allow QuickC to do "type checking."
  957. Given the information in the prototype, QuickC can check every subsequent
  958. use of the function to make sure you pass the right number and type of
  959. arguments and use the correct return type.  %@NL@%
  960. %@NL@%
  961. A function prototype gives the following information:  %@NL@%
  962. %@NL@%
  963. %@NL@%
  964.   ■   The function's name%@NL@%
  965. %@NL@%
  966.   ■   The type of value the function returns%@NL@%
  967. %@NL@%
  968.   ■   A list of arguments the function requires%@NL@%
  969. %@NL@%
  970. %@NL@%
  971. The VOLUME.C program contains one function prototype, for the %@AS@% sphere %@AE@%
  972. function:  %@NL@%
  973. %@NL@%
  974. %@AS@%  float sphere( int rad );%@AE@%%@NL@%
  975. %@NL@%
  976. The prototype states that the %@AS@% sphere %@AE@% function returns a %@AB@%float%@AE@%
  977. (floating-point) value and requires one %@AB@%int%@AE@% (integer) argument.  %@NL@%
  978. %@NL@%
  979. %@NL@%
  980. %@2@%%@CR:C6A00010012 @%%@AB@%A Few Words about printf%@AE@%%@EH@%%@NL@%
  981. %@NL@%
  982. The VOLUME.C program, like most examples in this book, uses the %@AB@%printf%@AE@%
  983. library function to display text. You won't need to know all of the details
  984. of %@AB@%printf%@AE@% to read the rest of this book, but the examples will be easier to
  985. follow if you know a few basic concepts.  %@NL@%
  986. %@NL@%
  987. The %@AB@%printf%@AE@% function works like the QuickBASIC %@AB@%PRINT USING%@AE@% statement or the
  988. QuickPascal %@AB@%Writeln%@AE@% procedure. It can display string and numeric data in
  989. various formats, and it normally prints to the screen.  %@NL@%
  990. %@NL@%
  991. You can print a simple message by passing %@AB@%printf%@AE@% a string (characters in
  992. double quotes) as an argument:  %@NL@%
  993. %@NL@%
  994. %@AS@%  printf( "Hi, Mom!" );%@AE@%%@NL@%
  995. %@NL@%
  996. The statement prints  %@NL@%
  997. %@NL@%
  998. %@AS@%  Hi, Mom!%@AE@%%@NL@%
  999. %@NL@%
  1000. The %@AB@%printf%@AE@% function doesn't automatically add a newline character at the end
  1001. of a line. The statements  %@NL@%
  1002. %@NL@%
  1003. %@AS@%  printf( "Le Nozze di Figaro" );
  1004. %@AS@%  printf( " by W. A. Mozart" );%@AE@%%@NL@%
  1005. %@NL@%
  1006. print the following message on one line:  %@NL@%
  1007. %@NL@%
  1008. %@AS@%  Le Nozze di Figaro by W. A. Mozart%@AE@%%@NL@%
  1009. %@NL@%
  1010. To start a new line, use the escape sequence %@AB@%\n%@AE@% as follows:  %@NL@%
  1011. %@NL@%
  1012. %@AS@%  printf( "Hi,\nMom!" );%@AE@%%@NL@%
  1013. %@NL@%
  1014. The statement prints two words on separate lines:  %@NL@%
  1015. %@NL@%
  1016. %@AS@%  Hi,
  1017. %@AS@%  Mom!%@AE@%%@NL@%
  1018. %@NL@%
  1019. The%@AB@% f%@AE@% in %@AB@%printf%@AE@% stands for formatting. To print the values of variables and
  1020. other items, you supply %@AB@%printf%@AE@% with format codes that tell %@AB@%printf%@AE@% the proper
  1021. format for each item. The codes are placed in the first argument, which is
  1022. enclosed in double quotes.  %@NL@%
  1023. %@NL@%
  1024. The following statement uses the %@AB@%%x%@AE@% code to print the integer 553 in
  1025. hexadecimal format. It passes two arguments to %@AB@%printf%@AE@%:  %@NL@%
  1026. %@NL@%
  1027. %@AS@%  printf( "%x", 553 );%@AE@%%@NL@%
  1028. %@NL@%
  1029. The first argument (%@AS@%"%x"%@AE@%) contains the format code and the second argument
  1030. (%@AS@%553%@AE@%) contains the item to be formatted. The line displays the following:  %@NL@%
  1031. %@NL@%
  1032. %@AS@%  229%@AE@%%@NL@%
  1033. %@NL@%
  1034. The %@AB@%printf%@AE@% function accepts several other format codes. For instance, the
  1035. VOLUME.C program uses %@AB@%%f%@AE@% to print a floating-point number. Some programs in
  1036. later chapters use %@AB@%%d%@AE@% to print integers or %@AB@%%ld%@AE@% to print long integers.  %@NL@%
  1037. %@NL@%
  1038. The first argument passed to %@AB@%printf%@AE@% can contain any combination of
  1039. characters and format codes. The other arguments contain the items that you
  1040. want %@AB@%printf%@AE@% to format. The statement  %@NL@%
  1041. %@NL@%
  1042. %@AS@%  printf( "%d times %d = %d\n", 2, 128, 2 * 128 );%@AE@%%@NL@%
  1043. %@NL@%
  1044. prints the line:  %@NL@%
  1045. %@NL@%
  1046. %@AS@%  2 times 128 = 256%@AE@%%@NL@%
  1047. %@NL@%
  1048. The %@AB@%printf%@AE@% function matches the format codes to the items you want to
  1049. format, in left-to-right order. In the code above, the first %@AB@%%d%@AE@% formats the
  1050. number 2, the second formats the 128, and the third formats the expression 2
  1051. * 128 (which evaluates to the number 256).  %@NL@%
  1052. %@NL@%
  1053. There's much more to say about %@AB@%printf%@AE@% and other I/O functions, but the rest
  1054. can wait until you reach Chapter 11, "Input and Output," which describes I/O
  1055. in detail.  %@NL@%
  1056. %@NL@%
  1057. Now that you've glimpsed the big picture, we can take a closer look at some
  1058. specifics of C programming, beginning with Chapter 2, "Functions."  %@NL@%
  1059. %@NL@%
  1060. %@NL@%
  1061. %@NL@%
  1062. %@NL@%
  1063. %@NL@%
  1064. %@NL@%
  1065. %@CR:C6A00020001 @%%@1@%%@AB@%Chapter 2  Functions%@AE@%%@EH@%%@NL@%
  1066. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  1067. %@NL@%
  1068. Chapter 1, "Anatomy of a C Program," introduced functions, the building
  1069. blocks of C programs. In this chapter, you'll learn how to use functions in
  1070. C programs.  %@NL@%
  1071. %@NL@%
  1072. We begin by discussing some function basics, including the role of the %@AB@%main%@AE@%
  1073. function. We then show you how to call functions, pass data to them, return
  1074. data from them, and declare them. The chapter concludes with a brief look at
  1075. old-style function declarations, which you may encounter in some programs.  %@NL@%
  1076. %@NL@%
  1077. %@NL@%
  1078. %@2@%%@CR:C6A00020002 @%%@AB@%Functions and Structured Programming%@AE@%%@EH@%%@NL@%
  1079. %@NL@%
  1080. As we mentioned in Chapter 1, a C function is a collection of statements,
  1081. enclosed in braces (%@AS@%{ }%@AE@%), which performs a particular task. It can receive
  1082. arguments (data) and also return a value.  %@NL@%
  1083. %@NL@%
  1084. Functions allow you to program with a "divide and conquer" strategy. Rather
  1085. than try to solve a large problem all at once, you break the problem into
  1086. several parts and attack each one separately. This approach, known as
  1087. "structured programming," allows you to write clear, reliable programs that
  1088. perform separate tasks in discrete, logically contained modules. In the C
  1089. language, these modules are called functions.  %@NL@%
  1090. %@NL@%
  1091. Functions offer several advantages. They can  %@NL@%
  1092. %@NL@%
  1093. %@NL@%
  1094.   ■   Make programs easier to write and read. All of the statements related
  1095.       to a task are located in one place.%@NL@%
  1096. %@NL@%
  1097.   ■   Prevent unexpected side effects by using private (local) variables
  1098.       that are not visible to other parts of the program.%@NL@%
  1099. %@NL@%
  1100.   ■   Eliminate unnecessary repetition of code for frequently performed
  1101.       tasks.%@NL@%
  1102. %@NL@%
  1103.   ■   Simplify debugging. Once the function works reliably, you can use it
  1104.       with confidence in many different situations.%@NL@%
  1105. %@NL@%
  1106. %@NL@%
  1107. If you know QuickPascal or QuickBASIC, you will see many similarities in the
  1108. C language. A C function serves the same basic purpose as a QuickPascal
  1109. function or procedure or a QuickBASIC %@AB@%FUNCTION%@AE@% or %@AB@%SUB%@AE@% procedure. In later
  1110. sections, we'll note some differences between C and these languages.  %@NL@%
  1111. %@NL@%
  1112. %@NL@%
  1113. %@2@%%@CR:C6A00020003 @%%@AB@%The main Function%@AE@%%@EH@%%@NL@%
  1114. %@NL@%
  1115. %@AU@% Every C program must  have one and only  one main function.%@AE@%  %@NL@%
  1116. %@NL@%
  1117. Every C program must have a function named %@AB@%main%@AE@%, which tells where program
  1118. execution begins and ends. Although %@AB@%main%@AE@% is not a C keyword, it has only one
  1119. use: naming the %@AB@%main%@AE@% function. A program must have only one %@AB@%main%@AE@% function,
  1120. and you shouldn't use the name anywhere else.  %@NL@%
  1121. %@NL@%
  1122. Below is the simplest possible C program:  %@NL@%
  1123. %@NL@%
  1124. %@AS@%  main()
  1125. %@AS@%  {
  1126. %@AS@%  }%@AE@%%@NL@%
  1127. %@NL@%
  1128. The braces (%@AS@%{ }%@AE@%) mark the %@AB@%main%@AE@% function's beginning and end, as they do in
  1129. every function. This program doesn't contain any executable statements; it
  1130. simply begins and ends.  %@NL@%
  1131. %@NL@%
  1132. Most functions have executable statements, of course, and these appear
  1133. within the function's braces. The following program contains a statement
  1134. which prints %@AS@% Hello, world! %@AE@% on the screen:  %@NL@%
  1135. %@NL@%
  1136. %@AS@%  main()
  1137. %@AS@%  {
  1138. %@AS@%     printf( "Hello, world!\n" );
  1139. %@AS@%  }%@AE@%%@NL@%
  1140. %@NL@%
  1141. The %@AB@%main%@AE@% function is called by the operating system when it runs your
  1142. program. While it's possible to call the %@AB@%main%@AE@% function in a program, you
  1143. should never do so, just as you wouldn't write a QuickBASIC program
  1144. containing the line  %@NL@%
  1145. %@NL@%
  1146. %@AS@%  10 GOSUB 10%@AE@%%@NL@%
  1147. %@NL@%
  1148. A program that calls %@AB@%main%@AE@% will start again and again in an endless loop that
  1149. eventually triggers a run-time error.  %@NL@%
  1150. %@NL@%
  1151. Like all functions, %@AB@%main%@AE@% can accept arguments and return a value. Through
  1152. this mechanism, your program can receive command-line arguments from DOS
  1153. when it begins execution and return a value to DOS when it ends. Chapter 9,
  1154. "Advanced Pointers," describes how to receive command-line arguments via
  1155. %@AB@%main%@AE@%.  %@NL@%
  1156. %@NL@%
  1157. %@NL@%
  1158. %@2@%%@CR:C6A00020004 @%%@AB@%Placement and Visibility of Functions%@AE@%%@EH@%%@NL@%
  1159. %@NL@%
  1160. %@AU@% A function is normally visible everywhere in the program.%@AE@%  %@NL@%
  1161. %@NL@%
  1162. Every C function is normally "visible" to all other functions in the same
  1163. program. That is, it can call and be called by any other function. C
  1164. functions can even call themselves, a process known as "recursion."  %@NL@%
  1165. %@NL@%
  1166. In the program below, the functions %@AS@% whiz %@AE@% and %@AS@% bang %@AE@% are visible to %@AB@%main%@AE@%
  1167. and to each other. The %@AB@%main %@AE@% function can call both %@AS@% whiz %@AE@% and %@AS@% bang%@AE@%. In
  1168. addition, %@AS@% whiz %@AE@% can call %@AS@% bang%@AE@%, and vice versa.  %@NL@%
  1169. %@NL@%
  1170. %@AS@%  main()
  1171. %@AS@%  {
  1172. %@AS@%  }
  1173. %@AS@%  
  1174. %@AS@%  whiz()
  1175. %@AS@%  {
  1176. %@AS@%  }
  1177. %@AS@%  
  1178. %@AS@%  bang()
  1179. %@AS@%  {
  1180. %@AS@%  }%@AE@%%@NL@%
  1181. %@NL@%
  1182. Functions can appear in any order and at almost any place in your program.
  1183. Since %@AB@%main%@AE@% starts and ends the program's execution, this function often
  1184. begins the program. But this is a readability convention, not a language
  1185. requirement.  %@NL@%
  1186. %@NL@%
  1187. %@AU@% C functions can't be nested.%@AE@%  %@NL@%
  1188. %@NL@%
  1189. One place where you can't put a function is inside another function. The C
  1190. language doesn't allow you to nest functions. Here C differs from
  1191. QuickPascal, in which one procedure can contain other "hidden" functions or
  1192. procedures. The following program causes a syntax error because the %@AS@% bang %@AE@%
  1193. function appears within the %@AS@% whiz %@AE@% function:  %@NL@%
  1194. %@NL@%
  1195. %@AS@%  main()
  1196. %@AS@%  {
  1197. %@AS@%  }
  1198. %@AS@%  
  1199. %@AS@%  whiz()
  1200. %@AS@%  {
  1201. %@AS@%     /* Error! Incorrect function placement */
  1202. %@AS@%     bang()
  1203. %@AS@%     {
  1204. %@AS@%     }
  1205. %@AS@%  }%@AE@%%@NL@%
  1206. %@NL@%
  1207. %@NL@%
  1208. %@2@%%@CR:C6A00020005 @%%@AB@%Function Definitions and Prototypes%@AE@%%@EH@%%@NL@%
  1209. %@NL@%
  1210. Now that you understand some function basics, we can look at functions in
  1211. more detail. A function, or more precisely, "function definition," contains
  1212. several parts. Figure 2.1 shows the parts of the %@AS@% sphere %@AE@% function
  1213. definition from the VOLUME.C example in Chapter 1, "Anatomy of a C Program."
  1214. %@NL@%
  1215. %@NL@%
  1216. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  1217. %@NL@%
  1218. The function "header" specifies the type of value a function returns and the
  1219. function's name. The header also contains an argument list, which specifies
  1220. the arguments the function requires. The rest of the function
  1221. definition─everything inside the braces─is the function "body."  %@NL@%
  1222. %@NL@%
  1223. The ANSI C standard, which QuickC follows, recommends that you supply a
  1224. function "prototype" (declaration) for every function definition in your
  1225. program. The prototype is identical to the function header except that it
  1226. ends with a semicolon. Figure 2.2 shows the %@AS@% sphere %@AE@% function prototype from
  1227. VOLUME.C.  %@NL@%
  1228. %@NL@%
  1229. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  1230. %@NL@%
  1231. The function prototype normally appears near the beginning of the program
  1232. and serves a purpose similar to a variable declaration. It provides advance
  1233. information about the function, which QuickC can use to check the accuracy
  1234. of subsequent calls to the function. We'll examine prototypes in detail in
  1235. "Function Prototypes," below.  %@NL@%
  1236. %@NL@%
  1237. %@NL@%
  1238. %@2@%%@CR:C6A00020006 @%%@AB@%Calling a Function%@AE@%%@EH@%%@NL@%
  1239. %@NL@%
  1240. You call (execute) a function by stating its name. In the simplest case─when
  1241. a function doesn't receive or return any data─the function call consists of
  1242. the function's name, followed by an empty pair of parentheses and a
  1243. semicolon. The BEEPER.C program, shown below, demonstrates this kind of
  1244. function call.  %@NL@%
  1245. %@NL@%
  1246. %@AS@%  /* BEEPER.C: Demonstrate simple function. */
  1247. %@AS@%  #include <stdio.h>
  1248. %@AS@%  
  1249. %@AS@%  void beep( void );
  1250. %@AS@%  
  1251. %@AS@%  main()
  1252. %@AS@%  {
  1253. %@AS@%     printf( "Time to beep\n" );
  1254. %@AS@%     beep();
  1255. %@AS@%     printf( "All done\n" );
  1256. %@AS@%  }
  1257. %@AS@%  
  1258. %@AS@%  void beep( void )
  1259. %@AS@%  {
  1260. %@AS@%     printf( "Beep!\a\n" );
  1261. %@AS@%  }%@AE@%%@NL@%
  1262. %@NL@%
  1263. When you run BEEPER.C, the program prints:  %@NL@%
  1264. %@NL@%
  1265. %@AS@%  Time to beep
  1266. %@AS@%  Beep!
  1267. %@AS@%  All done%@AE@%%@NL@%
  1268. %@NL@%
  1269. As you may recall from Chapter 1, "Anatomy of a C Program," the %@AB@%\n%@AE@% sequence
  1270. represents the newline character. The %@AB@%\a%@AE@% sequence is the "alert" character
  1271. (ASCII 7) which makes an audible beep.  %@NL@%
  1272. %@NL@%
  1273. In the %@AB@%main%@AE@% function of BEEPER.C, the statement  %@NL@%
  1274. %@NL@%
  1275. %@AS@%  beep();%@AE@%%@NL@%
  1276. %@NL@%
  1277. calls the %@AS@% beep %@AE@% function. Since %@AS@% beep %@AE@% takes no arguments, the parentheses
  1278. of the function call are empty.  %@NL@%
  1279. %@NL@%
  1280. The prototype and definition for the %@AS@% beep %@AE@% function use the%@AB@% void%@AS@% %@AE@%keyword
  1281. twice, first to indicate that the function returns no value, and second to
  1282. indicate that it receives no arguments. We'll return to these points later
  1283. in this chapter.%@AS@%  %@AE@%%@AE@%%@NL@%
  1284. %@NL@%
  1285. A function call transfers control to that function. The statements within
  1286. the function's braces execute in order until the function ends. Then
  1287. execution resumes where it left off.  %@NL@%
  1288. %@NL@%
  1289. A function can end in one of two ways. The %@AS@% beep %@AE@% function above ends by
  1290. "falling off" the closing brace of the function definition. A function can
  1291. also end by executing a %@AB@%return%@AE@% statement, which we discuss later in the
  1292. section "Returning Values from Functions."  %@NL@%
  1293. %@NL@%
  1294. Figure 2.3 illustrates the flow of control in BEEPER.C.  %@NL@%
  1295. %@NL@%
  1296. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  1297. %@NL@%
  1298. %@NL@%
  1299. %@2@%%@CR:C6A00020007 @%%@AB@%Passing Arguments to a Function%@AE@%%@EH@%%@NL@%
  1300. %@NL@%
  1301. If a function requires arguments, you list them in the parentheses of the
  1302. function call. In the BEEPER1.C program below, we revise the %@AS@% beep %@AE@% function
  1303. from BEEPER.C to take one argument.  %@NL@%
  1304. %@NL@%
  1305. %@AS@%  /* BEEPER1.C: Demonstrate passing arguments. */
  1306. %@AS@%  #include <stdio.h>
  1307. %@AS@%  
  1308. %@AS@%  void beep( int num_beep );
  1309. %@AS@%  
  1310. %@AS@%  main()
  1311. %@AS@%  {
  1312. %@AS@%     printf( "Time to beep\n" );
  1313. %@AS@%     beep( 5 );
  1314. %@AS@%     printf( "All done\n" );
  1315. %@AS@%  }
  1316. %@AS@%  
  1317. %@AS@%  void beep( int num_beep )
  1318. %@AS@%  {
  1319. %@AS@%     while( num_beep > 0  )
  1320. %@AS@%     {
  1321. %@AS@%        printf( "Beep!\a\n" );
  1322. %@AS@%        num_beep = num_beep - 1;
  1323. %@AS@%     }
  1324. %@AS@%  }%@AE@%%@NL@%
  1325. %@NL@%
  1326. The function definition states what kind of arguments the function expects.
  1327. In the %@AS@%beep %@AE@% function definition, the header,  %@NL@%
  1328. %@NL@%
  1329. %@AS@%  void beep( int num_beep )%@AE@%%@NL@%
  1330. %@NL@%
  1331. states that %@AS@% beep %@AE@% expects one %@AB@%int%@AE@% (integer) argument named %@AS@% num_beep%@AE@%
  1332. (number of beeps).  %@NL@%
  1333. %@NL@%
  1334. The statement that calls %@AS@% beep%@AE@%,  %@NL@%
  1335. %@NL@%
  1336. %@AS@%  beep( 5 );%@AE@%%@NL@%
  1337. %@NL@%
  1338. gives the value 5 in parentheses, passing that value as an argument. Figure
  1339. 2.4 shows argument passing in BEEPER1.C.  %@NL@%
  1340. %@NL@%
  1341. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  1342. %@NL@%
  1343. %@AU@% Function arguments are assigned to local variables inside the function.%@AE@%  %@NL@%
  1344. %@NL@%
  1345. When %@AS@% beep %@AE@% receives the value 5, the function automatically assigns the
  1346. value to %@AS@% num_beep%@AE@%, which the function can then treat as a local variable.
  1347. In this case, the function uses %@AS@% num_beep %@AE@% as a loop counter to repeat the
  1348. statement  %@NL@%
  1349. %@NL@%
  1350. %@AS@%  printf( "Beep!\a\n" );%@AE@%%@NL@%
  1351. %@NL@%
  1352. %@AS@%%@AE@%num_beep  times. (The C %@AB@%while%@AE@% loop is very similar to %@AB@%WHILE%@AE@% loops in
  1353. QuickBASIC or QuickPascal. You don't need to know the details of loops for
  1354. now; they're explained in Chapter 3, "Flow Control.")  %@NL@%
  1355. %@NL@%
  1356. If a function expects more than one argument, you separate the arguments
  1357. with commas. For instance, the statement  %@NL@%
  1358. %@NL@%
  1359. %@AS@%  printf( "%d times %d equals %d\n", 2, 16, 2 * 16 );%@AE@%%@NL@%
  1360. %@NL@%
  1361. passes four arguments to the %@AB@%printf%@AE@% function. The first argument is the
  1362. string  %@NL@%
  1363. %@NL@%
  1364. %@AS@%  "%d times %d equals %d\n"%@AE@%%@NL@%
  1365. %@NL@%
  1366. The second and third arguments are constants (%@AS@%2 %@AE@% and %@AS@% 16%@AE@%). The fourth
  1367. argument is an expression (%@AS@%2 * 16%@AE@%) that evaluates to a constant.  %@NL@%
  1368. %@NL@%
  1369. %@NL@%
  1370. %@2@%%@CR:C6A00020008 @%%@AB@%Arguments Versus Parameters%@AE@%%@EH@%%@NL@%
  1371. %@NL@%
  1372. In the C language, a value passed to a function is called either an
  1373. "argument" or a "parameter," depending on viewpoint. From the viewpoint of
  1374. the statement that calls the function, the value is an argument. In the view
  1375. of the function receiving it, the value is a parameter.  %@NL@%
  1376. %@NL@%
  1377. Thus, in BEEPER1.C, the following function call passes an argument to the %@AS@%
  1378. %@AS@%beep %@AE@% function:  %@NL@%
  1379. %@NL@%
  1380. %@AS@%  beep( 5 );%@AE@%%@NL@%
  1381. %@NL@%
  1382. Looking at the same value from the receiving end, the header of the %@AS@% beep %@AE@%
  1383. function declares a parameter named %@AS@% num_beep %@AE@% as follows:  %@NL@%
  1384. %@NL@%
  1385. %@AS@%  void beep( int num_beep );%@AE@%%@NL@%
  1386. %@NL@%
  1387. The argument and parameter refer to the same value─in this case, the value
  1388. 5. The naming distinction is just a matter of viewpoint, similar to the way
  1389. you call a letter outgoing mail if you're sending it, or incoming mail if
  1390. you're receiving it.  %@NL@%
  1391. %@NL@%
  1392. %@NL@%
  1393. %@2@%%@CR:C6A00020009 @%%@AB@%Assigning Parameters%@AE@%%@EH@%%@NL@%
  1394. %@NL@%
  1395. When you list a parameter in the function header, it becomes a local
  1396. variable within the function. This process is easy to follow when it
  1397. involves only one argument, as in the BEEPER1.C program above. The function
  1398. call passes one value, which the function assigns to one variable. The
  1399. variable can be treated like any other variable declared within the
  1400. function.  %@NL@%
  1401. %@NL@%
  1402. %@AU@% There is a one-to-one correspondence between arguments and parameters.%@AE@%  %@NL@%
  1403. %@NL@%
  1404. If a function takes more than one argument, the values are passed in order.
  1405. The first argument in the function call is assigned to the first variable,
  1406. the second argument is assigned to the second variable, and so on.  %@NL@%
  1407. %@NL@%
  1408. The SHOWME.C program below demonstrates this process. Its %@AS@% showme %@AE@% function
  1409. takes three arguments. The %@AB@%main%@AE@% function defines three integer variables and
  1410. passes their values to %@AS@% showme%@AE@%, which prints the values that it receives.
  1411. (You normally wouldn't write a function just to print one line, of course.
  1412. We'll add more to SHOWME.C in a later revision.)  %@NL@%
  1413. %@NL@%
  1414. %@AS@%  /* SHOWME.C: Demonstrate passing by value. */
  1415. %@AS@%  #include <stdio.h>
  1416. %@AS@%  
  1417. %@AS@%  void showme( int a, int b, int c );%@AE@%%@NL@%
  1418. %@NL@%
  1419. %@AS@%  main()
  1420. %@AS@%  {
  1421. %@AS@%     int x = 10, y = 20, z = 30;
  1422. %@AS@%     showme( z, y, x );
  1423. %@AS@%  }
  1424. %@AS@%  
  1425. %@AS@%  void showme( int a, int b, int c )
  1426. %@AS@%  {
  1427. %@AS@%     printf( "a=%d b=%d c=%d", a, b, c );
  1428. %@AS@%  }%@AE@%%@NL@%
  1429. %@NL@%
  1430. Here's the output from SHOWME.C:  %@NL@%
  1431. %@NL@%
  1432. %@AS@%  a=30 b=20 c=10%@AE@%%@NL@%
  1433. %@NL@%
  1434. The function call in SHOWME.C passes the values of %@AS@% z%@AE@%, %@AS@% y%@AE@%, and %@AS@% x %@AE@% in the
  1435. order listed:  %@NL@%
  1436. %@NL@%
  1437. %@AS@%  showme( z, y, x );%@AE@%%@NL@%
  1438. %@NL@%
  1439. %@AU@% Functions receive parameters in the order they are passed.%@AE@%  %@NL@%
  1440. %@NL@%
  1441. These values are assigned, in the same order, to the parameters listed in
  1442. the %@AS@% showme %@AE@% function header:  %@NL@%
  1443. %@NL@%
  1444. %@AS@%  void showme( int a, int b, int c )%@AE@%%@NL@%
  1445. %@NL@%
  1446. The position of the parameters, not their names, controls which arguments
  1447. the parameters receive. The first argument (%@AS@% z %@AE@%) listed in the function call
  1448. is assigned to the first parameter (%@AS@% a %@AE@%) in the function header, the second
  1449. argument (%@AS@% y %@AE@%) to the second parameter (%@AS@% b %@AE@%), and so on. Figure 2.5 shows
  1450. this process.  %@NL@%
  1451. %@NL@%
  1452. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  1453. %@NL@%
  1454. %@NL@%
  1455. %@2@%%@CR:C6A00020010 @%%@AB@%Passing by Value%@AE@%%@EH@%%@NL@%
  1456. %@NL@%
  1457. %@AU@% The C language passes copies of function arguments.%@AE@%  %@NL@%
  1458. %@NL@%
  1459. In C, all function arguments (except arrays) are passed "by value" rather
  1460. than "by reference." That is, a function receives a local copy of each
  1461. argument, not the argument itself. These copies are local variables within
  1462. the function. They are created and initialized automatically when the
  1463. function begins, and they disappear when it ends. Like all local variables,
  1464. their values can be changed without  affecting variables elsewhere in the
  1465. program.  %@NL@%
  1466. %@NL@%
  1467. We can clarify this point by adding a few statements to the SHOWME.C
  1468. program. The new program, SHOWMORE.C, will change the values of the local
  1469. variables in the %@AS@% showme %@AE@% function without changing the values of the
  1470. original variables.  %@NL@%
  1471. %@NL@%
  1472. %@AS@%  /* SHOWMORE.C: Demonstrate passing by value. */
  1473. %@AS@%  #include <stdio.h>
  1474. %@AS@%  
  1475. %@AS@%  void showme( int any, int old, int name );
  1476. %@AS@%  
  1477. %@AS@%  main()
  1478. %@AS@%  {
  1479. %@AS@%     int x = 10, y = 20, z = 30;
  1480. %@AS@%     showme( z, y, x );
  1481. %@AS@%     printf( "  z=%d   y=%d    x=%d\n", z, y, x );
  1482. %@AS@%  }
  1483. %@AS@%  
  1484. %@AS@%  void showme( int any, int old, int name )
  1485. %@AS@%  {
  1486. %@AS@%     printf( "any=%d old=%d name=%d\n", any, old, name );
  1487. %@AS@%     any = 55;
  1488. %@AS@%     old = 66;
  1489. %@AS@%     name = 77;
  1490. %@AS@%     printf( "any=%d old=%d name=%d\n", any, old, name );
  1491. %@AS@%  }%@AE@%%@NL@%
  1492. %@NL@%
  1493. Here is the output from SHOWMORE.C:  %@NL@%
  1494. %@NL@%
  1495. %@AS@%  any=30 old=20 name=10
  1496. %@AS@%  any=55 old=66 name=77
  1497. %@AS@%    z=30   y=20    x=10%@AE@%%@NL@%
  1498. %@NL@%
  1499. First, note that the %@AS@% showme %@AE@% function in SHOWMORE.C uses new names (%@AS@% any%@AE@%, %@AS@%
  1500. %@AS@%old%@AE@%, and %@AS@% name%@AE@%) when assigning the parameters it receives:  %@NL@%
  1501. %@NL@%
  1502. %@AS@%  void showme( int any, int old, int name )%@AE@%%@NL@%
  1503. %@NL@%
  1504. %@AU@% Function parameters can have any legal variable names.%@AE@%  %@NL@%
  1505. %@NL@%
  1506. Because these variables are local to the function, they can have any legal
  1507. names. (The rules for variable names are described in Chapter 4, "Data
  1508. Types.") The %@AS@% showme %@AE@% function prints the values of its parameters
  1509. immediately after assigning them:  %@NL@%
  1510. %@NL@%
  1511. %@AS@%  printf( "any=%d old=%d name=%d", any, old, name );%@AE@%%@NL@%
  1512. %@NL@%
  1513. Then the function assigns new values to the variables and prints them again:
  1514. %@NL@%
  1515. %@NL@%
  1516. %@AS@%  any = 55;
  1517. %@AS@%  old = 66;
  1518. %@AS@%  name = 77;
  1519. %@AS@%  printf( "any=%d old=%d name=%d", any, old, name );%@AE@%%@NL@%
  1520. %@NL@%
  1521. %@AU@% Local variables are private to the function containing them.%@AE@%  %@NL@%
  1522. %@NL@%
  1523. Changing the local variables in the %@AS@% showme %@AE@% function doesn't affect the
  1524. original variables in the %@AB@%main%@AE@% function. Remember, a variable defined inside
  1525. a function is only visible inside that function. After control returns to
  1526. %@AB@%main%@AE@%, SHOWMORE.C prints the values of the original variables:  %@NL@%
  1527. %@NL@%
  1528. %@AS@%  printf( "  z=%d   y=%d    x=%d\n", z, y, x );%@AE@%%@NL@%
  1529. %@NL@%
  1530. As the program output shows, the original values are unchanged:  %@NL@%
  1531. %@NL@%
  1532. %@AS@%  z=30   y=20    x=10%@AE@%%@NL@%
  1533. %@NL@%
  1534. We'll say more about the visibility of variables in Chapter 5, "Advanced
  1535. Data Types." For now, just remember that when you pass a value to a
  1536. function, the function makes a local copy of that value. The local copy can
  1537. be manipulated without changing the original.  %@NL@%
  1538. %@NL@%
  1539. ────────────────────────────────────────────────────────────────────────────%@NL@%
  1540. NOTE
  1541.  
  1542. %@AI@%In QuickPascal, you can pass either the value of an argument or the
  1543. %@AI@%argument's address. In C, function arguments are only passed by value.
  1544. %@AI@%However, that value can be an address. Chapter 8, "Pointers," explains how
  1545. %@AI@%to pass addresses to functions.%@AE@%%@NL@%
  1546. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  1547. %@NL@%
  1548. %@NL@%
  1549. %@2@%%@CR:C6A00020011 @%%@AB@%Returning Values from Functions%@AE@%%@EH@%%@NL@%
  1550. %@NL@%
  1551. %@AU@% The return keyword  ends a function and  can return one value.%@AE@%  %@NL@%
  1552. %@NL@%
  1553. Most C functions return a value. This is done with the %@AB@%return%@AE@% statement,
  1554. which also ends the function. The VOLUME.C program from Chapter 1, "Anatomy
  1555. of a C Program," (see Figure 2.1) contains such a statement. In that
  1556. program, the %@AS@% sphere %@AE@% function returns the value of the variable %@AS@% result %@AE@% as
  1557. follows:  %@NL@%
  1558. %@NL@%
  1559. %@AS@%  return result;%@AE@%%@NL@%
  1560. %@NL@%
  1561. The following statement in the %@AB@%main%@AE@% function of VOLUME.C calls the %@AS@% sphere %@AE@%
  1562. function and assigns its return value to the variable %@AS@% volume%@AE@%:  %@NL@%
  1563. %@NL@%
  1564. %@AS@%  volume = sphere( radius );%@AE@%%@NL@%
  1565. %@NL@%
  1566. Figure 2.6 shows the flow of control as the %@AS@% sphere %@AE@% function returns a
  1567. value in VOLUME.C.  %@NL@%
  1568. %@NL@%
  1569. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  1570. %@NL@%
  1571. A %@AB@%return%@AE@% statement can only return a single value. If a function needs to
  1572. return multiple values, the normal method is to use pointers─a major topic
  1573. that we'll discuss in Chapter 8, "Pointers."  %@NL@%
  1574. %@NL@%
  1575. ────────────────────────────────────────────────────────────────────────────%@NL@%
  1576. NOTE
  1577.  
  1578. %@AI@%In QuickPascal, a function returns a value and a procedure does not. The
  1579. %@AI@%same distinction applies to QuickBASIC %@AI@%FUNCTION %@AE@%%@AI@%and %@AE@%%@AI@%%@AB@%SUB %@AE@%%@AE@%%@AI@%procedures,
  1580. %@AI@%respectively. In the C language, a function can do both. It can return a
  1581. %@AI@%value or return nothing.%@AE@%%@AE@%%@NL@%
  1582. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  1583. %@NL@%
  1584. A function can contain more than one %@AB@%return%@AE@% statement, as shown below:  %@NL@%
  1585. %@NL@%
  1586. %@AS@%  if( error == 0 )
  1587. %@AS@%    return 0;
  1588. %@AS@%  else
  1589. %@AS@%    return 1;%@AE@%%@NL@%
  1590. %@NL@%
  1591. The code returns a different value in different cases. It returns the value
  1592. 0 when the variable %@AS@% error %@AE@% is 0 and the value 1 when %@AS@% error %@AE@% is nonzero.
  1593. (In C, the %@AB@%if%@AE@% and %@AB@%else%@AE@% statements work much like those in other languages.
  1594. Chapter 3, "Flow Control," explains these statements.)  %@NL@%
  1595. %@NL@%
  1596. %@AU@% A return statement can  appear anywhere and  need not return a value.%@AE@%  %@NL@%
  1597. %@NL@%
  1598. You can place the %@AB@%return%@AE@% keyword anywhere within a function, and the
  1599. statement need not necessarily return a value. In the following fragment,
  1600. the naked %@AB@%return%@AE@% statement simply ends the function if the value of %@AS@% count %@AE@%
  1601. exceeds 500:  %@NL@%
  1602. %@NL@%
  1603. %@AS@%  if( count > 500 )
  1604. %@AS@%     return;
  1605. %@AS@%  else
  1606. %@AS@%     /* execute more statements... */%@AE@%%@NL@%
  1607. %@NL@%
  1608. A %@AB@%return%@AE@% statement ends the function immediately, no matter where it
  1609. appears. In the function shown below, the statements following the %@AB@%return%@AE@%
  1610. never execute:  %@NL@%
  1611. %@NL@%
  1612. %@AS@%  void do_nothing( void )
  1613. %@AS@%  {
  1614. %@AS@%     return;
  1615. %@AS@%     /* The following statements do not execute */
  1616. %@AS@%     printf( "This function " );
  1617. %@AS@%     printf( "prints nothing.\n" );
  1618. %@AS@%  }%@AE@%%@NL@%
  1619. %@NL@%
  1620. If a function doesn't return a value, and you want the function to end by
  1621. falling off its closing brace, no %@AB@%return%@AE@% statement is needed. This method is
  1622. used to end the %@AS@% beep %@AE@% function in BEEPER.C, discussed earlier in this
  1623. chapter:  %@NL@%
  1624. %@NL@%
  1625. %@AS@%  void beep( void )
  1626. %@AS@%  {
  1627. %@AS@%     printf( "Beep!\a\n" );
  1628. %@AS@%  }%@AE@%%@NL@%
  1629. %@NL@%
  1630. You could add a %@AB@%return%@AE@% to the end of this function, but it's not necessary.
  1631. The function ends automatically.  %@NL@%
  1632. %@NL@%
  1633. %@NL@%
  1634. %@2@%%@CR:C6A00020012 @%%@AB@%Using Return Values%@AE@%%@EH@%%@NL@%
  1635. %@NL@%
  1636. %@AU@% Function return values are  often assigned to variables.%@AE@%  %@NL@%
  1637. %@NL@%
  1638. A function's return value can be used in the same way you would use any
  1639. value of its type. In the VOLUME.C program from Chapter 1, "Anatomy of a C
  1640. Program," the statement that calls %@AS@% sphere %@AE@% assigns the function's return
  1641. value to the variable %@AS@% volume%@AE@%:  %@NL@%
  1642. %@NL@%
  1643. %@AS@%  volume = sphere( radius );%@AE@%%@NL@%
  1644. %@NL@%
  1645. If there's no need to save the return value, you can use it directly. You
  1646. may have noticed that the variable %@AS@% volume %@AE@% isn't really needed in the
  1647. VOLUME.C program, which simply prints the variable's value and ends. Most
  1648. programmers would make the program more compact by replacing the two
  1649. statements  %@NL@%
  1650. %@NL@%
  1651. %@AS@%  volume = sphere( radius );
  1652. %@AS@%  printf( "Volume: %f\n", volume );%@AE@%%@NL@%
  1653. %@NL@%
  1654. with this one:  %@NL@%
  1655. %@NL@%
  1656. %@AS@%  printf( "Volume: %f\n", sphere( radius ) );%@AE@%%@NL@%
  1657. %@NL@%
  1658. The second version puts the %@AS@% sphere %@AE@% function call right in the %@AB@%printf%@AE@%
  1659. statement, eliminating the superfluous variable. Instead of assigning the
  1660. return value to a variable and passing that variable's value to %@AB@%printf%@AE@%, the
  1661. statement uses the value directly. (The %@AS@% sphere %@AE@% function is called first.
  1662. Then the return value from %@AS@% sphere %@AE@% is passed as an argument to the %@AB@%printf%@AE@%
  1663. function.)  %@NL@%
  1664. %@NL@%
  1665. While this change streamlines the program, it also makes the code a little
  1666. harder to follow. If you don't read carefully, you might overlook the fact
  1667. that the %@AB@%printf%@AE@% function call contains another function call.  %@NL@%
  1668. %@NL@%
  1669. %@AU@% Unused return values  are discarded.%@AE@%  %@NL@%
  1670. %@NL@%
  1671. Occasionally, you may have no use for a function's return value. The %@AB@%printf%@AE@%
  1672. function, for example, returns the number of characters it displayed, but
  1673. few programs need this information. If you don't use a return value, it's
  1674. discarded.  %@NL@%
  1675. %@NL@%
  1676. You should never ignore the error codes that library functions return to
  1677. show whether the function succeeded. See Chapter 10, "Programming Pitfalls,"
  1678. for more information about library function return values.  %@NL@%
  1679. %@NL@%
  1680. %@NL@%
  1681. %@2@%%@CR:C6A00020013 @%%@AB@%Declaring a Function's Return Type%@AE@%%@EH@%%@NL@%
  1682. %@NL@%
  1683. Thus far, we have explained how a function can return a value─and how the
  1684. calling statement can use that value─without paying much attention to what
  1685. type of value the function returns. (The C language supports various data
  1686. types, such as %@AB@%int%@AE@% for integer values, and %@AB@%float%@AE@% for floating-point values.
  1687. Chapter 4 describes data types in detail.)  %@NL@%
  1688. %@NL@%
  1689. The return type is important because it controls what the function returns.
  1690. If a function returns an integer when you expect a floating-point value,
  1691. your program may not work correctly.  %@NL@%
  1692. %@NL@%
  1693. %@AU@% A function's prototype and definition control what  type of value it
  1694. %@AU@%returns.%@AE@%  %@NL@%
  1695. %@NL@%
  1696. The function's return type is specified in its prototype and definition.
  1697. Below are the prototype and definition of the %@AS@% sphere %@AE@% function from the
  1698. VOLUME.C program in Chapter 1, "Anatomy of a C Program." They specify that
  1699. the function returns a %@AB@%float%@AE@% value.  %@NL@%
  1700. %@NL@%
  1701. %@AS@%  float sphere( int rad );  /* function prototype */
  1702. %@AS@%  
  1703. %@AS@%  float sphere( int rad )   /* function header */%@AE@%%@NL@%
  1704. %@NL@%
  1705. The type name (here, %@AB@%float%@AE@%) in front of the function name shows what type of
  1706. value the function returns. If the %@AS@% sphere %@AE@% function returned an %@AB@%int%@AE@% value,
  1707. its prototype and header would look like this:  %@NL@%
  1708. %@NL@%
  1709. %@AS@%  int sphere( int rad ); /* function prototype */
  1710. %@AS@%  
  1711. %@AS@%  int sphere( int rad )  /* function header */%@AE@%%@NL@%
  1712. %@NL@%
  1713. %@AU@% Use the void type name  to show a function  returns no value.%@AE@%  %@NL@%
  1714. %@NL@%
  1715. You should declare the return type for every function─even for functions
  1716. that don't return a value. These functions are declared with the %@AB@%void%@AE@% type
  1717. name. In the SHOWME.C program, shown above, the prototype of the %@AS@% showme %@AE@%
  1718. function follows this pattern:  %@NL@%
  1719. %@NL@%
  1720. %@AS@%  void showme( int a, int b, int c );%@AE@%%@NL@%
  1721. %@NL@%
  1722. The %@AB@%void%@AE@% that precedes the function name indicates that %@AS@% showme %@AE@% returns
  1723. nothing.  %@NL@%
  1724. %@NL@%
  1725. %@NL@%
  1726. %@2@%%@CR:C6A00020014 @%%@AB@%Function Prototypes%@AE@%%@EH@%%@NL@%
  1727. %@NL@%
  1728. Function prototyping is the major innovation of the ANSI standard for C. As
  1729. we mentioned earlier, a function prototype gives the same information as the
  1730. function's header: the name of the function, the type of value the function
  1731. returns, and the number and type of parameters the function requires.  %@NL@%
  1732. %@NL@%
  1733. %@AU@%   Function prototypes allow QuickC to check function references for
  1734. %@AU@%accuracy.%@AE@%  %@NL@%
  1735. %@NL@%
  1736. Function prototypes normally appear near the start of the program, before
  1737. the first function definition. Given the information in the prototype,
  1738. QuickC can perform "type checking." It checks each reference to the
  1739. function─its definition, as well as every function call─to make sure that
  1740. the reference uses the right number and type of arguments and the correct
  1741. return value. Without type checking, it's easy to create bugs by passing the
  1742. wrong type of value to a function or assuming the wrong return type.  %@NL@%
  1743. %@NL@%
  1744. C programs normally include one prototype for each function they define,
  1745. except the %@AB@%main%@AE@% function. Most programmers don't bother to prototype %@AB@%main%@AE@%
  1746. unless the program receives command-line arguments or returns a value to DOS
  1747. when it ends. (Command-line arguments are discussed in Chapter 9, "Advanced
  1748. Pointers.")  %@NL@%
  1749. %@NL@%
  1750. Here is the function prototype for the %@AS@% sphere %@AE@% function in VOLUME.C:  %@NL@%
  1751. %@NL@%
  1752. %@AS@%  float sphere( int rad );%@AE@%%@NL@%
  1753. %@NL@%
  1754. You can see that %@AS@% sphere %@AE@% expects a single %@AB@%int%@AE@%-type parameter and returns a
  1755. value of type %@AB@%float%@AE@%. On the other hand, the prototype for %@AS@% showme %@AE@% in
  1756. SHOWME.C indicates that %@AS@% showme %@AE@% expects three %@AB@%int%@AE@%-type parameters and
  1757. returns nothing:  %@NL@%
  1758. %@NL@%
  1759. %@AS@%  void showme( int a, int b, int c );%@AE@%%@NL@%
  1760. %@NL@%
  1761. It's common to use the same parameter names in both the function prototype
  1762. and the function header. In SHOWME.C, for instance, the %@AS@% showme %@AE@% function
  1763. prototype,  %@NL@%
  1764. %@NL@%
  1765. %@AS@%  void showme( int a, int b, int c );%@AE@%%@NL@%
  1766. %@NL@%
  1767. uses the names %@AS@% a%@AE@%, %@AS@% b%@AE@%, and %@AS@% c%@AE@%, as does the header for that function,  %@NL@%
  1768. %@NL@%
  1769. %@AS@%  void showme( int a, int b, int c )%@AE@%%@NL@%
  1770. %@NL@%
  1771. Using the same names in both parameter lists makes the program more
  1772. readable, but it's not a language requirement. The names in the prototype's
  1773. parameter list are merely cosmetic. You can use any names you choose, or
  1774. even omit the names completely. The prototype in SHOWME.C works just as well
  1775. when written  %@NL@%
  1776. %@NL@%
  1777. %@AS@%  void showme( int, int, int );%@AE@%%@NL@%
  1778. %@NL@%
  1779. as when you supply the names %@AS@% a%@AE@%, %@AS@% b%@AE@%, and %@AS@% c%@AE@%. Both versions fully specify the
  1780. number (three) and type (%@AB@%int%@AE@%) of the parameters the function expects.  %@NL@%
  1781. %@NL@%
  1782. %@NL@%
  1783. %@3@%%@CR:C6A00020015 @%%@AB@%Prototyping Functions without Parameters%@AE@%%@EH@%%@NL@%
  1784. %@NL@%
  1785. If a function doesn't expect any parameters, you might be tempted to leave
  1786. its parameter list blank. But it's better to put %@AB@%void%@AE@% in its parameter list,
  1787. as shown here:  %@NL@%
  1788. %@NL@%
  1789. %@AS@%  void beep( void );%@AE@%%@NL@%
  1790. %@NL@%
  1791. The %@AB@%void%@AE@% in parentheses specifies that the %@AS@% beep %@AE@% function requires no
  1792. parameters. If you leave the parentheses empty, the compiler draws no
  1793. conclusion about what parameters the function takes and won't be able to
  1794. detect an error if you mistakenly pass an argument to the function.  %@NL@%
  1795. %@NL@%
  1796. %@NL@%
  1797. %@3@%%@CR:C6A00020016 @%%@AB@%Prototyping Functions with Variable Parameters%@AE@%%@EH@%%@NL@%
  1798. %@NL@%
  1799. Some functions, such as the %@AB@%printf%@AE@% library function, can handle a variable
  1800. number of parameters. This capability can make functions more flexible. As
  1801. earlier examples have shown, the %@AB@%printf%@AE@% function can take one parameter or
  1802. several, depending on how many values you need to print.  %@NL@%
  1803. %@NL@%
  1804. To declare a function with a variable number of parameters, end the
  1805. parameter list with a comma and an ellipsis (%@AS@%, . . .%@AE@%). The following
  1806. prototype, for example, declares that the %@AS@% sample %@AE@% function expects at least
  1807. one %@AB@%int%@AE@%-type parameter and zero or more additional parameters:  %@NL@%
  1808. %@NL@%
  1809. %@AS@%  void sample( int a,... );%@AE@%%@NL@%
  1810. %@NL@%
  1811. The ellipsis stands for an unspecified number of parameters with types that
  1812. are also unspecified. The parameter list in the function header should
  1813. follow the same pattern.  %@NL@%
  1814. %@NL@%
  1815. Don't declare a variable number of parameters unless it's necessary. Giving
  1816. this sort of prototype for a function that takes a fixed number of
  1817. parameters defeats the prototype's main purpose. QuickC can't perform type
  1818. checking for parameters you leave out of a prototype.  %@NL@%
  1819. %@NL@%
  1820. %@NL@%
  1821. %@2@%%@CR:C6A00020017 @%%@AB@%Old-Style Function Declarations and Definitions%@AE@%%@EH@%%@NL@%
  1822. %@NL@%
  1823. This book explains how to declare and define functions under the ANSI
  1824. standard for C, which is now the norm. The original C language used slightly
  1825. different rules for function declarations and definitions. QuickC can
  1826. compile these "oldstyle" programs, but the ANSI standard recommends you use
  1827. the full function prototypes we just described.  %@NL@%
  1828. %@NL@%
  1829. Still, you may encounter old-style function declarations and definitions in
  1830. many existing C programs. So, you should be familiar with the style.  %@NL@%
  1831. %@NL@%
  1832. We'll use the VOLUME.C program from Chapter 1, "Anatomy of a C Program," to
  1833. demonstrate the old style. First, here's the ANSI-style program presented in
  1834. Chapter 1:  %@NL@%
  1835. %@NL@%
  1836. %@AS@%  /* VOLUME.C: Calculate sphere's volume. */
  1837. %@AS@%  #include <stdio.h>
  1838. %@AS@%  #define PI 3.14
  1839. %@AS@%  
  1840. %@AS@%  float sphere( int rad );
  1841. %@AS@%  
  1842. %@AS@%  main()
  1843. %@AS@%  {
  1844. %@AS@%     float volume;
  1845. %@AS@%     int radius = 3;
  1846. %@AS@%     volume = sphere( radius );
  1847. %@AS@%     printf( "Volume: %f\n", volume );
  1848. %@AS@%  }
  1849. %@AS@%  
  1850. %@AS@%  float sphere( int rad )
  1851. %@AS@%  {
  1852. %@AS@%     float result;
  1853. %@AS@%     result = rad * rad * rad;
  1854. %@AS@%     result = 4 * PI * result;
  1855. %@AS@%     result = result / 3;
  1856. %@AS@%     return result;
  1857. %@AS@%  }%@AE@%%@NL@%
  1858. %@NL@%
  1859. The same program written in the old style would look something like this:  %@NL@%
  1860. %@NL@%
  1861. %@AS@%  /* OLDSTYLE.C: Old-style function. */
  1862. %@AS@%  
  1863. %@AS@%  #include <stdio.h>
  1864. %@AS@%  #define PI 3.14
  1865. %@AS@%  
  1866. %@AS@%  float sphere();
  1867. %@AS@%  
  1868. %@AS@%  main()
  1869. %@AS@%  {
  1870. %@AS@%     float volume;
  1871. %@AS@%     int radius = 3;
  1872. %@AS@%     volume = sphere( radius );
  1873. %@AS@%     printf( "Volume: %f\n", volume );
  1874. %@AS@%  }
  1875. %@AS@%  
  1876. %@AS@%  float sphere( rad )
  1877. %@AS@%  int rad;
  1878. %@AS@%  {
  1879. %@AS@%     float result;
  1880. %@AS@%     result = rad * rad * rad;
  1881. %@AS@%     result = 4 * PI * result;
  1882. %@AS@%     result = result / 3;
  1883. %@AS@%     return result;
  1884. %@AS@%  }%@AE@%%@NL@%
  1885. %@NL@%
  1886. You'll notice two distinct differences. First, the old style doesn't allow a
  1887. parameter list in the function declaration. In the ANSI version, VOLUME.C,
  1888. the declaration of the %@AS@% sphere %@AE@% function specifies that the function takes a
  1889. single %@AB@%int%@AE@% parameter:  %@NL@%
  1890. %@NL@%
  1891. %@AS@%  float sphere( int rad );%@AE@%%@NL@%
  1892. %@NL@%
  1893. The corresponding declaration in OLDSTYLE.C omits the parameter list:  %@NL@%
  1894. %@NL@%
  1895. %@AS@%  float sphere();%@AE@%%@NL@%
  1896. %@NL@%
  1897. An old-style function declaration cannot provide any information about the
  1898. function's parameters.  %@NL@%
  1899. %@NL@%
  1900. The other change is in the way the function definition lists parameters. In
  1901. VOLUME.C, the function header lists the same information as the function
  1902. prototype, giving the type (%@AB@%int%@AE@%) and name (%@AS@% rad %@AE@%) of the function's
  1903. parameter:  %@NL@%
  1904. %@NL@%
  1905. %@AS@%  float sphere( int rad )
  1906. %@AS@%  {
  1907. %@AS@%     .
  1908. %@AS@%     .
  1909. %@AS@%     .
  1910. %@AS@%  }%@AE@%%@NL@%
  1911. %@NL@%
  1912. In OLDSTYLE.C, the function header gives the parameter's name (%@AS@% rad %@AE@%), but
  1913. not its type. The parameter's type is declared in a statement directly below
  1914. the function header (and before the left brace that begins the function
  1915. body):  %@NL@%
  1916. %@NL@%
  1917. %@AS@%  float sphere( rad )
  1918. %@AS@%  int rad;
  1919. %@AS@%  {
  1920. %@AS@%     .
  1921. %@AS@%     .
  1922. %@AS@%     .
  1923. %@AS@%  }%@AE@%%@NL@%
  1924. %@NL@%
  1925. The rest of OLDSTYLE.C is identical to VOLUME.C.  %@NL@%
  1926. %@NL@%
  1927. Now that you understand the basics of functions, we can turn our attention
  1928. to the C statements that a function can contain, beginning with flow-control
  1929. statements, the subject of the next chapter.  %@NL@%
  1930. %@NL@%
  1931. %@NL@%
  1932. %@NL@%
  1933. %@NL@%
  1934. %@NL@%
  1935. %@NL@%
  1936. %@CR:C6A00030001 @%%@1@%%@AB@%Chapter 3  Flow Control%@AE@%%@EH@%%@NL@%
  1937. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  1938. %@NL@%
  1939. Flow control─diverting execution by looping and branching─is one area where
  1940. C closely resembles other languages. If you know how to loop and branch in
  1941. QuickBASIC or QuickPascal, learning the C equivalents is mainly a matter of
  1942. adjusting to somewhat different syntax. Here, as elsewhere, C never uses two
  1943. keywords when one will do. For instance, C has no "then" keyword. Instead,
  1944. it uses simple punctuation.  %@NL@%
  1945. %@NL@%
  1946. This chapter has two parts. The first part examines the looping statements:
  1947. %@AB@%while%@AE@%, %@AB@%do%@AE@%, and %@AB@%for%@AE@%. The second part describes the decision-making
  1948. statements: %@AB@%if%@AE@%, %@AB@%else%@AE@%, %@AB@%switch%@AE@%, %@AB@%break%@AE@%, %@AB@%continue%@AE@%, and %@AB@%goto%@AE@%.  %@NL@%
  1949. %@NL@%
  1950. %@NL@%
  1951. %@2@%%@CR:C6A00030002 @%%@AB@%Loops: while, do, and for%@AE@%%@EH@%%@NL@%
  1952. %@NL@%
  1953. This section discusses the C statements that create loops: %@AB@%while%@AE@%, %@AB@%do%@AE@%, and
  1954. %@AB@%for%@AE@%. These loops repeat while a condition is true or for a set number of
  1955. times. We'll begin with the simplest loop, the %@AB@%while%@AE@% statement.  %@NL@%
  1956. %@NL@%
  1957. %@NL@%
  1958. %@3@%%@CR:C6A00030003 @%%@AB@%The while Statement%@AE@%%@EH@%%@NL@%
  1959. %@NL@%
  1960. %@AU@%    A while loop evaluates its test expression before executing the body of
  1961. %@AU@%the loop.%@AE@%  %@NL@%
  1962. %@NL@%
  1963. A %@AB@%while%@AE@% loop repeats as long as a given condition remains true. It consists
  1964. of the %@AB@%while%@AE@% keyword followed by a test expression in parentheses and a loop
  1965. body, as shown in Figure 3.1. The "test expression" can be any C expression
  1966. and is evaluated before the loop body is executed. The loop body is a single
  1967. statement or a statement block that executes once every time the loop is
  1968. iterated. The distinguishing feature of a %@AB@%while%@AE@% loop is that it evaluates
  1969. the test expression before it executes the loop body, unlike the %@AB@%do%@AE@% loop,
  1970. which we'll examine next.  %@NL@%
  1971. %@NL@%
  1972. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  1973. %@NL@%
  1974. We've incorporated the simple %@AB@%while%@AE@% loop from Figure 3.1 in the WHILE.C
  1975. program, shown below.  %@NL@%
  1976. %@NL@%
  1977. %@AS@%  /* WHILE.C: Demonstrate while loop. */
  1978. %@AS@%  
  1979. %@AS@%  #include <stdio.h>
  1980. %@AS@%  
  1981. %@AS@%  main()
  1982. %@AS@%  {
  1983. %@AS@%     int test = 10;
  1984. %@AS@%  
  1985. %@AS@%     while( test > 0 )
  1986. %@AS@%     {
  1987. %@AS@%        printf( "test = %d\n", test );
  1988. %@AS@%        test = test - 2;
  1989. %@AS@%     }
  1990. %@AS@%  }%@AE@%%@NL@%
  1991. %@NL@%
  1992. Here is the output from WHILE.C:  %@NL@%
  1993. %@NL@%
  1994. %@AS@%  test = 10
  1995. %@AS@%  test = 8
  1996. %@AS@%  test = 6
  1997. %@AS@%  test = 4
  1998. %@AS@%  test = 2%@AE@%%@NL@%
  1999. %@NL@%
  2000. In WHILE.C, if the variable %@AS@% test %@AE@% is positive when the loop begins, the
  2001. test expression evaluates as true and the loop executes. If %@AS@% test %@AE@% has a 0
  2002. or negative value when the loop starts, the test expression is false; the
  2003. loop body does not execute and the action falls through to the statement
  2004. that follows the loop.  %@NL@%
  2005. %@NL@%
  2006. (Chapter 6, "Operators," explains true and false values. For now, it's
  2007. enough to know that an expression is evaluated as false if it equals 0. Any
  2008. nonzero value is true.)  %@NL@%
  2009. %@NL@%
  2010. The loop body in WHILE.C happens to be a statement block enclosed in braces.
  2011. If the loop body is a single statement, as in the following code, no braces
  2012. are needed.  %@NL@%
  2013. %@NL@%
  2014. %@AS@%  main()
  2015. %@AS@%  {
  2016. %@AS@%     int test = 10;
  2017. %@AS@%     while( test > 0 )
  2018. %@AS@%        test = test - 2;
  2019. %@AS@%  }%@AE@%%@NL@%
  2020. %@NL@%
  2021. Occasionally, you'll see a %@AB@%while%@AE@% loop with a test expression such as  %@NL@%
  2022. %@NL@%
  2023. %@AS@%  while( 1 )%@AE@%%@NL@%
  2024. %@NL@%
  2025. or  %@NL@%
  2026. %@NL@%
  2027. %@AS@%  #define TRUE 1
  2028. %@AS@%     .
  2029. %@AS@%     .
  2030. %@AS@%     .
  2031. %@AS@%  while( TRUE )%@AE@%%@NL@%
  2032. %@NL@%
  2033. The test expressions above are always true, creating an indefinite loop that
  2034. never ends naturally. You can only terminate this kind of loop with some
  2035. overt action, usually by executing a %@AB@%break%@AE@% statement. (See "The break
  2036. Statement" later in this chapter.) You can use such a loop to repeat an
  2037. action for an indefinite time period─until a certain key is pressed, for
  2038. instance.  %@NL@%
  2039. %@NL@%
  2040. %@NL@%
  2041. %@3@%%@CR:C6A00030004 @%%@AB@%The do Statement%@AE@%%@EH@%%@NL@%
  2042. %@NL@%
  2043. A %@AB@%do%@AE@% loop is simply a %@AB@%while%@AE@% loop turned on its head. First comes the loop
  2044. body, then the test expression. Unlike a %@AB@%while%@AE@% loop, a %@AB@%do%@AE@% loop always
  2045. executes its loop body at least once.  %@NL@%
  2046. %@NL@%
  2047. %@AU@%   A do loop always executes at least once.%@AE@%  %@NL@%
  2048. %@NL@%
  2049. The difference is important. A %@AB@%while%@AE@% statement evaluates the test expression
  2050. before it executes the loop body. If the test expression in a %@AB@%while%@AE@%
  2051. statement is false, the loop body doesn't execute at all. A %@AB@%do%@AE@% statement, on
  2052. the other hand, evaluates the test expression after executing the loop body.
  2053. Thus, the body of a %@AB@%do%@AE@% statement always executes at least once, even if the
  2054. test expression is false when the loop begins.  %@NL@%
  2055. %@NL@%
  2056. Figure 3.2 contrasts the %@AB@%while%@AE@% loop from WHILE.C with a comparable %@AB@%do%@AE@% loop
  2057. to emphasize this difference. You'll notice that the %@AB@%do%@AE@% keyword comes right
  2058. before the loop body, which is followed by the %@AB@%while%@AE@% keyword and a test
  2059. expression─the same test expression that WHILE.C uses. Notice the semicolon
  2060. that ends the %@AB@%do%@AE@% loop. A %@AB@%do%@AE@% loop always ends with a semicolon; a %@AB@%while%@AE@% loop
  2061. never does.  %@NL@%
  2062. %@NL@%
  2063. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  2064. %@NL@%
  2065. The DO.C program below uses the %@AB@%do%@AE@% loop from Figure 3.2 to perform the same
  2066. action that WHILE.C does.  %@NL@%
  2067. %@NL@%
  2068. %@AS@%  /* DO.C: Demonstrate do loop. */
  2069. %@AS@%  
  2070. %@AS@%  #include <stdio.h>
  2071. %@AS@%  
  2072. %@AS@%  main()
  2073. %@AS@%  {
  2074. %@AS@%     int test = 10;
  2075. %@AS@%     do
  2076. %@AS@%     {
  2077. %@AS@%        printf( "test = %d\n", test );
  2078. %@AS@%        test = test - 2;
  2079. %@AS@%     }  while( test > 0 );
  2080. %@AS@%  }%@AE@%%@NL@%
  2081. %@NL@%
  2082. DO.C gives the same output as WHILE.C:  %@NL@%
  2083. %@NL@%
  2084. %@AS@%  test = 10
  2085. %@AS@%  test = 8
  2086. %@AS@%  test = 6
  2087. %@AS@%  test = 4
  2088. %@AS@%  test = 2%@AE@%%@NL@%
  2089. %@NL@%
  2090. The programs do not give the same output if you modify them so that the
  2091. value of %@AS@% test %@AE@% is 0 when the loop starts. In that case, the loop body in
  2092. DO.C executes once, but the loop body in WHILE.C doesn't execute at all. You
  2093. should only use a %@AB@%do%@AE@% loop when you always want the loop body to execute at
  2094. least once.  %@NL@%
  2095. %@NL@%
  2096. %@NL@%
  2097. %@3@%%@CR:C6A00030005 @%%@AB@%The for Statement%@AE@%%@EH@%%@NL@%
  2098. %@NL@%
  2099. As in QuickBASIC or QuickPascal, the %@AB@%for%@AE@% statement in C is often used to
  2100. repeat a statement a set number of times. Let's begin with a simple example.
  2101. The FORLOOP.C program, shown below, uses %@AB@%for%@AE@% to repeat a %@AB@%printf%@AE@% statement
  2102. five times.  %@NL@%
  2103. %@NL@%
  2104. %@AS@%  /* FORLOOP.C: Demonstrate for loop. */
  2105. %@AS@%  
  2106. %@AS@%  #include <stdio.h>
  2107. %@AS@%  
  2108. %@AS@%  main()
  2109. %@AS@%  {
  2110. %@AS@%     int test;
  2111. %@AS@%     for( test = 10; test > 0; test = test - 2 )
  2112. %@AS@%        printf( "test = %d\n", test );
  2113. %@AS@%  }%@AE@%%@NL@%
  2114. %@NL@%
  2115. FORLOOP.C gives the same output as WHILE.C and DO.C:  %@NL@%
  2116. %@NL@%
  2117. %@AS@%  test = 10
  2118. %@AS@%  test = 8
  2119. %@AS@%  test = 6
  2120. %@AS@%  test = 4
  2121. %@AS@%  test = 2%@AE@%%@NL@%
  2122. %@NL@%
  2123. Figure 3.3 shows the parts of the %@AB@%for%@AE@% loop in FORLOOP.C.  %@NL@%
  2124. %@NL@%
  2125. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  2126. %@NL@%
  2127. A %@AB@%for%@AE@% statement is more complex than a %@AB@%while%@AE@% or %@AB@%do%@AE@% statement. The part in
  2128. parentheses can contain three expressions separated by semicolons:  %@NL@%
  2129. %@NL@%
  2130. %@NL@%
  2131.   ■   An "initializing expression" that often initializes a loop counter%@NL@%
  2132. %@NL@%
  2133.   ■   A "test expression" that states how long the loop continues%@NL@%
  2134. %@NL@%
  2135.   ■   A "modifying expression" that often modifies a loop counter%@NL@%
  2136. %@NL@%
  2137. %@NL@%
  2138. Like the test expression in a %@AB@%while%@AE@% statement, the test expression in a %@AB@%for%@AE@%
  2139. statement causes the loop to continue as long as the test expression
  2140. evaluates as true.  %@NL@%
  2141. %@NL@%
  2142. All of the expressions in the parentheses of a %@AB@%for%@AE@% statement are optional.
  2143. If you omit the test expression (the second one), the statement repeats
  2144. indefinitely. In the following program, for instance, all of the expressions
  2145. in the parentheses of the %@AB@%for%@AE@% loop are empty:  %@NL@%
  2146. %@NL@%
  2147. %@AS@%  main()
  2148. %@AS@%  {
  2149. %@AS@%     for( ; ; )
  2150. %@AS@%        printf( "Hi, Mom!\n" );
  2151. %@AS@%  }%@AE@%%@NL@%
  2152. %@NL@%
  2153. The loop above repeats indefinitely because it has no test expression that
  2154. specifies when to end the loop. It has the same effect as the following
  2155. %@AB@%while%@AE@% loop, whose test expression is always true:  %@NL@%
  2156. %@NL@%
  2157. %@AS@%  main()
  2158. %@AS@%  {
  2159. %@AS@%     while( 1 )
  2160. %@AS@%        printf( "Hi, Mom!\n" );
  2161. %@AS@%  }%@AE@%%@NL@%
  2162. %@NL@%
  2163. You can use multiple expressions for either the initializing expression or
  2164. the modifying expression, as in FORLOOP1.C:  %@NL@%
  2165. %@NL@%
  2166. %@AS@%  /* FORLOOP1.C: Demonstrate multiple expressions. */
  2167. %@AS@%  
  2168. %@AS@%  #include <stdio.h>
  2169. %@AS@%  
  2170. %@AS@%  main()
  2171. %@AS@%  {
  2172. %@AS@%     int a, b;
  2173. %@AS@%     for( a = 256, b = 1; b < 512; a = a / 2, b = b * 2 )
  2174. %@AS@%        printf( "a = %d  b = %d\n", a, b );
  2175. %@AS@%  }%@AE@%%@NL@%
  2176. %@NL@%
  2177. The output from FORLOOP1.C appears below:  %@NL@%
  2178. %@NL@%
  2179. %@AS@%  a = 256  b = 1
  2180. %@AS@%  a = 128  b = 2
  2181. %@AS@%  a = 64  b = 4
  2182. %@AS@%  a = 32  b = 8
  2183. %@AS@%  a = 16  b = 16
  2184. %@AS@%  a = 8    b = 32
  2185. %@AS@%  a = 4    b = 64
  2186. %@AS@%  a = 2    b = 128
  2187. %@AS@%  a = 1    b = 256%@AE@%%@NL@%
  2188. %@NL@%
  2189. In the FORLOOP1.C program, the initializing expression of the %@AB@%for%@AE@% loop
  2190. initializes two variables (%@AS@% a %@AE@% and %@AS@% b %@AE@%) instead of one. The modifying
  2191. expression changes the values of the same two variables. Use commas to
  2192. separate multiple expressions in cases such as this.  %@NL@%
  2193. %@NL@%
  2194. Although %@AB@%for%@AE@% and %@AB@%while%@AE@% might seem quite different, they're interchangeable
  2195. in most cases. The FORLOOP2.C program demonstrates this principle. Both
  2196. loops, while constructed differently, produce the same effect─printing the
  2197. numbers from 0 through 9.  %@NL@%
  2198. %@NL@%
  2199. %@AS@%  /* FORLOOP2.C: Demonstrate similarity of for and while. */
  2200. %@AS@%  
  2201. %@AS@%  #include <stdio.h>
  2202. %@AS@%  
  2203. %@AS@%  main()
  2204. %@AS@%  {
  2205. %@AS@%     int count;
  2206. %@AS@%  
  2207. %@AS@%     for( count = 0; count < 10; count = count + 1 )
  2208. %@AS@%        printf( "count = %d\n", count );
  2209. %@AS@%  
  2210. %@AS@%     count = 0;
  2211. %@AS@%     while( count < 10 )
  2212. %@AS@%     {
  2213. %@AS@%        printf( "count = %d\n", count );
  2214. %@AS@%        count = count +1;
  2215. %@AS@%     }
  2216. %@AS@%  
  2217. %@AS@%  }%@AE@%%@NL@%
  2218. %@NL@%
  2219. The two loops in FORLOOP2.C function identically. The %@AB@%for%@AE@% loop prints the
  2220. numbers from 0 through 9:  %@NL@%
  2221. %@NL@%
  2222. %@AS@%  for( count = 0; count < 10; count = count + 1; )
  2223. %@AS@%     printf( "count = %d\n", count );%@AE@%%@NL@%
  2224. %@NL@%
  2225. as does the %@AB@%while%@AE@% loop:  %@NL@%
  2226. %@NL@%
  2227. %@AS@%  count = 0;
  2228. %@AS@%  while( count < 10 )
  2229. %@AS@%  {
  2230. %@AS@%     printf( "count = %d\n", count );
  2231. %@AS@%     count = count + 1;
  2232. %@AS@%  }%@AE@%%@NL@%
  2233. %@NL@%
  2234. Most programmers prefer %@AB@%for%@AE@% over %@AB@%while%@AE@% in a case like this, because %@AB@%for%@AE@%
  2235. groups all the loop-control statements in one place. The %@AB@%for%@AE@% statement is
  2236. also appropriate when you need to initialize one or more values at the
  2237. beginning of the loop. The %@AB@%while%@AE@% and %@AB@%do%@AE@% statements are more appropriate for
  2238. cases in which the value used in the test expression has already been
  2239. initialized.  %@NL@%
  2240. %@NL@%
  2241. %@NL@%
  2242. %@2@%%@CR:C6A00030006 @%%@AB@%Decision-Making Statements: if, else, switch, break, continue, and goto%@AE@%%@EH@%%@NL@%
  2243. %@NL@%
  2244. The C language provides six statements for decision making: %@AB@%if%@AE@%, %@AB@%else%@AE@%,
  2245. %@AB@%switch%@AE@%, %@AB@%break%@AE@%, %@AB@%continue%@AE@%, and %@AB@%goto%@AE@%. Like their counterparts in other
  2246. languages, these statements transfer control based on the outcome of a
  2247. logical test.  %@NL@%
  2248. %@NL@%
  2249. %@NL@%
  2250. %@3@%%@CR:C6A00030007 @%%@AB@%The if Statement%@AE@%%@EH@%%@NL@%
  2251. %@NL@%
  2252. %@AU@% The body of an if statement executes when its test expression is true.%@AE@%  %@NL@%
  2253. %@NL@%
  2254. An %@AB@%if%@AE@% statement consists of the %@AB@%if%@AE@% keyword followed by a test expression in
  2255. parentheses and a second statement. The second statement is executed if the
  2256. test expression is true, or skipped if the expression is false.  %@NL@%
  2257. %@NL@%
  2258. The IFF.C program contains a simple %@AB@%if%@AE@% test. It prints a prompt and waits
  2259. for you to press a key.  %@NL@%
  2260. %@NL@%
  2261. %@AS@%  /* IFF.C: Demonstrate if statement. */
  2262. %@AS@%  
  2263. %@AS@%  #include <stdio.h>
  2264. %@AS@%  #include <conio.h>
  2265. %@AS@%  
  2266. %@AS@%  main()
  2267. %@AS@%  {
  2268. %@AS@%     char ch;
  2269. %@AS@%     printf( "Press the b key to hear a bell.\n" );
  2270. %@AS@%     ch = getch();
  2271. %@AS@%     if( ch == 'b' )
  2272. %@AS@%        printf( "Beep!\a\n" );
  2273. %@AS@%  }%@AE@%%@NL@%
  2274. %@NL@%
  2275. In the IFF.C program, the statement  %@NL@%
  2276. %@NL@%
  2277. %@AS@%  ch = getch();%@AE@%%@NL@%
  2278. %@NL@%
  2279. calls the %@AB@%getch%@AE@% library function to get a keypress from the keyboard and
  2280. then assigns the result to the variable %@AS@% ch%@AE@%. If you press the b key, the
  2281. program prints  %@NL@%
  2282. %@NL@%
  2283. %@AS@%  Beep!%@AE@%%@NL@%
  2284. %@NL@%
  2285. and sounds a beep. (To simplify the code, IFF.C tests only for a lowercase b
  2286. character. A program would normally use a library function such as %@AB@%tolower%@AE@%
  2287. to test for both upper and lowercase.)  %@NL@%
  2288. %@NL@%
  2289. Figure 3.4 illustrates the parts of the %@AB@%if%@AE@% statement in the IFF.C program.  %@NL@%
  2290. %@NL@%
  2291. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  2292. %@NL@%
  2293. The test expression of the %@AB@%if%@AE@% statement  %@NL@%
  2294. %@NL@%
  2295. %@AS@%  ch == 'b'%@AE@%%@NL@%
  2296. %@NL@%
  2297. %@AU@% The equality operator (==) tests if values are equal.%@AE@%  %@NL@%
  2298. %@NL@%
  2299. is true when the variable %@AS@% ch %@AE@% equals the letter b. In C the equality
  2300. operator (%@AB@%==%@AE@%) tests if two values are equal. (Chapter 6 discusses
  2301. operators.)  %@NL@%
  2302. %@NL@%
  2303. The body of the %@AB@%if%@AE@% statement in IFF.C happens to be a single statement, but
  2304. the body can also be a statement block, as in the following fragment:  %@NL@%
  2305. %@NL@%
  2306. %@AS@%  if( ch == 'b' )
  2307. %@AS@%  {
  2308. %@AS@%     printf( "Beep!\a\n" );
  2309. %@AS@%     printf( "You pressed the 'b' key\n" );
  2310. %@AS@%  }%@AE@%%@NL@%
  2311. %@NL@%
  2312. You can also nest %@AB@%if%@AE@% statements, as shown below:  %@NL@%
  2313. %@NL@%
  2314. %@AS@%  if( ch == 'b' )
  2315. %@AS@%  {
  2316. %@AS@%     printf( "Beep!\a\n" );
  2317. %@AS@%     beep_count = beepcount + 1;
  2318. %@AS@%     if( beep_count > 10 )
  2319. %@AS@%     {
  2320. %@AS@%        printf( "More than 10 beeps...\n" );
  2321. %@AS@%        if( beep_count > 100 )
  2322. %@AS@%    printf( "Don't wear out the 'b' key!\n" );
  2323. %@AS@%     }
  2324. %@AS@%  }%@AE@%%@NL@%
  2325. %@NL@%
  2326. The code nests three %@AB@%if%@AE@% statements. The first %@AB@%if%@AE@% tests whether %@AS@% ch %@AE@% equals
  2327. the letter b; the second tests whether the variable %@AS@% beep_count %@AE@% is greater
  2328. than 10. The third tests whether %@AS@% beep_count %@AE@% exceeds 100.  %@NL@%
  2329. %@NL@%
  2330. %@NL@%
  2331. %@3@%%@CR:C6A00030008 @%%@AB@%The else Clause%@AE@%%@EH@%%@NL@%
  2332. %@NL@%
  2333. %@AU@% An else clause can follow an if statement.%@AE@%  %@NL@%
  2334. %@NL@%
  2335. The %@AB@%else%@AE@% keyword is used with %@AB@%if%@AE@% to form an either-or construct that
  2336. executes one statement when an expression is true and another when it's
  2337. false. The ELSE.C program demonstrates %@AB@%else%@AE@% by adding an %@AB@%else%@AE@% clause to the
  2338. code from IFF.C. It sounds a beep and prints %@AS@% Beep! %@AE@% if you type the letter
  2339. b, or it prints %@AS@% Bye bye %@AE@% if you type any other letter.  %@NL@%
  2340. %@NL@%
  2341. %@AS@%  /* ELSE.C: Demonstrate else clause. */
  2342. %@AS@%  
  2343. %@AS@%  #include <stdio.h>
  2344. %@AS@%  #include <conio.h>
  2345. %@AS@%  
  2346. %@AS@%  main()
  2347. %@AS@%  {
  2348. %@AS@%     char ch;
  2349. %@AS@%     printf( "Press the b key to hear a bell.\n" );
  2350. %@AS@%     ch = getch();
  2351. %@AS@%     if( ch == 'b' )
  2352. %@AS@%        printf( "Beep!\a\n" );
  2353. %@AS@%     else
  2354. %@AS@%        printf( "Bye bye\n" );
  2355. %@AS@%  }%@AE@%%@NL@%
  2356. %@NL@%
  2357. %@AU@% To create an else-if construct, place an if statement after an else.%@AE@%  %@NL@%
  2358. %@NL@%
  2359. The C language has no "elseif" keyword, but it's easy to create the same
  2360. effect, because the statement that follows %@AB@%else%@AE@% can be any C
  2361. statement─including another %@AB@%if%@AE@% statement. The ELSE1.C program uses %@AB@%if%@AE@% and
  2362. %@AB@%else%@AE@% to test three conditions. It sounds a beep when you type the letter b,
  2363. it prints %@AS@% Enter %@AE@% when you press the ENTER key, or it prints %@AS@% Bye bye %@AE@% when
  2364. you press any other key.  %@NL@%
  2365. %@NL@%
  2366. %@AS@%  /* ELSE1.C: Demonstrate else-if construct. */
  2367. %@AS@%  
  2368. %@AS@%  #include <stdio.h>
  2369. %@AS@%  #include <conio.h>
  2370. %@AS@%  
  2371. %@AS@%  main()
  2372. %@AS@%  {
  2373. %@AS@%     char ch;
  2374. %@AS@%     printf( "Press the b key to hear a bell.\n" );
  2375. %@AS@%     ch = getch();
  2376. %@AS@%     if( ch == 'b' )
  2377. %@AS@%        printf( "Beep!\a\n" );
  2378. %@AS@%     else
  2379. %@AS@%        if( ch == '\r' )
  2380. %@AS@%           printf( "Enter\n" );
  2381. %@AS@%     else
  2382. %@AS@%        printf( "Bye bye\n" );
  2383. %@AS@%  }%@AE@%%@NL@%
  2384. %@NL@%
  2385. The %@AB@%else%@AE@% keyword is tied to the closest preceding %@AB@%if%@AE@% that's not already
  2386. matched by an %@AB@%else%@AE@%. Keep this rule in mind when creating nested %@AB@%if%@AE@%-%@AB@%else%@AE@%
  2387. constructs. (See the section "Mismatching if and else Statements" in Chapter
  2388. 10, "Programming Pitfalls.")  %@NL@%
  2389. %@NL@%
  2390. %@NL@%
  2391. %@3@%%@CR:C6A00030009 @%%@AB@%The switch Statement%@AE@%%@EH@%%@NL@%
  2392. %@NL@%
  2393. %@AU@% The switch statement can perform multiple branches.%@AE@%  %@NL@%
  2394. %@NL@%
  2395. The %@AB@%switch%@AE@% statement offers an elegant option in situations that require
  2396. multiple branches. It tests a single expression that can have several
  2397. values, providing a different action for each value.  %@NL@%
  2398. %@NL@%
  2399. One disadvantage of %@AB@%if%@AE@% and %@AB@%else%@AE@% is that they only allow one branch per
  2400. keyword. The program either executes the statement that follows the %@AB@%if%@AE@% or
  2401. %@AB@%else%@AE@%, or it doesn't. To perform more complex tests, you have to pile on more
  2402. %@AB@%if%@AE@% and %@AB@%else%@AE@% statements, as in the ELSE1.C program above.  %@NL@%
  2403. %@NL@%
  2404. A program that handles keyboard input, for instance, may require several
  2405. different responses to various keypresses. The ELSE1.C program above used
  2406. combinations of %@AB@%if%@AE@% and %@AB@%else%@AE@% to process keyboard input. We've used a single
  2407. %@AB@%switch%@AE@% statement in the SWITCH.C program below to do the same job:  %@NL@%
  2408. %@NL@%
  2409. %@AS@%  /* SWITCH.C: Demonstrate switch statement. */
  2410. %@AS@%  
  2411. %@AS@%  #include <stdio.h>
  2412. %@AS@%  #include <conio.h>
  2413. %@AS@%  
  2414. %@AS@%  main()
  2415. %@AS@%  {
  2416. %@AS@%     char ch;
  2417. %@AS@%     printf( "Press the b key to hear a bell.\n" );
  2418. %@AS@%     ch = getch();
  2419. %@AS@%     switch( ch )
  2420. %@AS@%     {
  2421. %@AS@%        case 'b':
  2422. %@AS@%           printf( "Beep!\a\n" );
  2423. %@AS@%           break;
  2424. %@AS@%        case '\r':
  2425. %@AS@%           printf( "Enter\n" );
  2426. %@AS@%           break;
  2427. %@AS@%        default:
  2428. %@AS@%           printf( "Bye bye" );
  2429. %@AS@%           break;
  2430. %@AS@%     }
  2431. %@AS@%  }%@AE@%%@NL@%
  2432. %@NL@%
  2433. The SWITCH.C program produces the same output as ELSE1.C. Figure 3.5
  2434. illustrates the %@AB@%switch%@AE@% statement in SWITCH.C, comparing it with the %@AB@%if%@AE@%-%@AB@%else%@AE@%
  2435. construct in ELSE1.C.  %@NL@%
  2436. %@NL@%
  2437. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  2438. %@NL@%
  2439. As in other decision-making statements, the parentheses after the keyword
  2440. contain the expression to test. This can be any expression that yields a
  2441. constant value. The test expression in SWITCH.C evaluates the value of the
  2442. variable %@AS@% ch%@AE@%:  %@NL@%
  2443. %@NL@%
  2444. %@AS@%  switch( ch )%@AE@%%@NL@%
  2445. %@NL@%
  2446. %@AU@% The switch statement branches to one of several labeled alternatives.%@AE@%  %@NL@%
  2447. %@NL@%
  2448. The test expression is followed by a statement block enclosed in curly
  2449. braces. The block contains alternate sections of code that you want to
  2450. execute under various circumstances. The program's action branches to one of
  2451. the alternatives, depending on the value of the test expression.  %@NL@%
  2452. %@NL@%
  2453. Each alternative in the statement block starts with a "case label," which
  2454. consists of the %@AB@%case%@AE@% keyword, a constant or constant expression, and a
  2455. colon. (The only other C statement that uses labels is %@AB@%goto%@AE@%, which we'll
  2456. discuss later in this chapter.)  %@NL@%
  2457. %@NL@%
  2458. Below is the first case label in SWITCH.C:  %@NL@%
  2459. %@NL@%
  2460. %@AS@%  case 'b':%@AE@%%@NL@%
  2461. %@NL@%
  2462. This case label lists the character constant %@AS@% 'b'%@AE@%. If the variable %@AS@% ch %@AE@%
  2463. equals %@AS@% 'b'%@AE@%, the program's action branches to this label. If %@AS@% ch %@AE@% equals %@AS@%
  2464. %@AS@%'\r'%@AE@%, the program branches to the following label:  %@NL@%
  2465. %@NL@%
  2466. %@AS@%  case '\r':%@AE@%%@NL@%
  2467. %@NL@%
  2468. The basic effect of %@AB@%switch%@AE@% is similar to the %@AB@%SELECT CASE%@AE@% statement in
  2469. QuickBASIC. The program can branch to many different alternatives, but only
  2470. one at a time.  %@NL@%
  2471. %@NL@%
  2472. A %@AB@%switch%@AE@% statement can have as many %@AB@%case%@AE@% alternatives as you need. Each
  2473. alternative must be labeled with a constant value. (You can't use a variable
  2474. in the label.)  %@NL@%
  2475. %@NL@%
  2476. ────────────────────────────────────────────────────────────────────────────%@NL@%
  2477. NOTE
  2478.  
  2479. %@AI@%In previous versions of QuickC, the constant in a case label could only be a%@AI@%
  2480. %@AI@%%@AE@%%@AI@%%@AB@%char%@AE@%%@AE@%%@AI@% or %@AE@%%@AI@%%@AB@%int%@AE@%%@AE@%%@AI@% value. In QuickC 2.5, the constant can be any integral type,
  2481. %@AI@%including a %@AE@%%@AI@%%@AB@%long%@AE@%%@AE@%%@AI@% or %@AE@%%@AI@%%@AB@%unsigned%@AE@%%@AE@%%@AI@%%@AB@% %@AE@%%@AE@%%@AI@%%@AB@%long%@AE@%%@AE@%%@AI@% as well as a %@AE@%%@AI@%%@AB@%char%@AE@%%@AE@%%@AI@% or %@AE@%%@AI@%%@AB@%int%@AE@%%@AE@%%@AI@%.
  2482. %@AI@%hapter 4, "Data Types," describes the %@AE@%%@AI@%%@AB@%char%@AE@%%@AE@%%@AI@%, %@AE@%%@AI@%%@AB@%int%@AE@%%@AE@%%@AI@%, and %@AE@%%@AI@%%@AB@%long%@AE@%%@AE@%%@AI@% types.%@AE@%%@AE@%%@NL@%
  2483. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  2484. %@NL@%
  2485. %@AU@% The default keyword is used only in switch statements.%@AE@%  %@NL@%
  2486. %@NL@%
  2487. SWITCH.C also shows how to use the %@AB@%default%@AE@% keyword in a %@AB@%switch%@AE@% statement.
  2488. The statements after the %@AB@%default%@AE@% label execute if the value of the test
  2489. expression doesn't equal any of the values listed in other labels. In
  2490. SWITCH.C, the code following %@AB@%default%@AE@% executes when the variable %@AS@% ch %@AE@% equals
  2491. anything other than %@AS@% 'b' %@AE@% or %@AS@% '\r'%@AE@%.  %@NL@%
  2492. %@NL@%
  2493. Not every %@AB@%switch%@AE@% statement requires a %@AB@%default%@AE@% label. If no %@AB@%default%@AE@% is
  2494. present, and the test expression doesn't match any of the values listed in
  2495. the other %@AB@%case%@AE@% labels, no statements are executed.  %@NL@%
  2496. %@NL@%
  2497. %@AU@% Use the break keyword to exit a switch statement.%@AE@%  %@NL@%
  2498. %@NL@%
  2499. You normally place a %@AB@%break%@AE@% statement at the end of each alternative, as
  2500. shown in SWITCH.C. The %@AB@%break%@AE@% statement exits the %@AB@%switch%@AE@% statement block
  2501. immediately. If you don't put a %@AB@%break%@AE@% at the end of the alternative, the
  2502. action falls through to the next statement.  %@NL@%
  2503. %@NL@%
  2504. For instance, say that you remove all the %@AB@%break%@AE@% statements from SWITCH.C, as
  2505. shown below:  %@NL@%
  2506. %@NL@%
  2507. %@AS@%  switch( ch )
  2508. %@AS@%  {
  2509. %@AS@%     case 'b':
  2510. %@AS@%        printf( "Beep!\a\n" );
  2511. %@AS@%     case '\r':
  2512. %@AS@%        printf( "Enter\n" );
  2513. %@AS@%     default:
  2514. %@AS@%        printf( "Bye bye" );
  2515. %@AS@%  }%@AE@%%@NL@%
  2516. %@NL@%
  2517. If you run the revised program and type the letter b, the program executes
  2518. the first alternative, producing this output:  %@NL@%
  2519. %@NL@%
  2520. %@AS@%  Beep!%@AE@%%@NL@%
  2521. %@NL@%
  2522. then goes on to execute the statements that follow:  %@NL@%
  2523. %@NL@%
  2524. %@AS@%  Enter
  2525. %@AS@%  Bye bye%@AE@%%@NL@%
  2526. %@NL@%
  2527. Occasionally, you may want to fall through from one %@AB@%case%@AE@% alternative to
  2528. another. But you should be careful not to omit %@AB@%break%@AE@% statements
  2529. accidentally. (See the section "Omitting break Statements from a switch
  2530. Statement" in Chapter 10, "Programming Pitfalls.")  %@NL@%
  2531. %@NL@%
  2532. If you end each alternative with a %@AB@%break%@AE@%, as in SWITCH.C, the order of the
  2533. alternatives isn't critical. The program branches to the label containing
  2534. the correct value, no matter where that label appears in the %@AB@%switch%@AE@%
  2535. statement block. For instance, you can reverse the order of the alternatives
  2536. in SWITCH.C without changing the program's output. For readability's sake,
  2537. many programmers put %@AB@%default%@AE@% at the end of a %@AB@%switch %@AE@%statement and arrange
  2538. the other alternatives alphabetically or numerically.  %@NL@%
  2539. %@NL@%
  2540. Sometimes you'll want to execute the same code for more than one case. This
  2541. is done by grouping all the desired labels in front of one alternative. For
  2542. instance, if you revise the second alternative in SWITCH.C to read  %@NL@%
  2543. %@NL@%
  2544. %@AS@%  case '\r':
  2545. %@AS@%  case '\t':
  2546. %@AS@%  case ' ':
  2547. %@AS@%    printf( "What a boring choice!\n" );
  2548. %@AS@%    break;%@AE@%%@NL@%
  2549. %@NL@%
  2550. the program will print  %@NL@%
  2551. %@NL@%
  2552. %@AS@%  What a boring choice!%@AE@%%@NL@%
  2553. %@NL@%
  2554. when you press the ENTER key, the TAB key, or the SPACEBAR.  %@NL@%
  2555. %@NL@%
  2556. %@NL@%
  2557. %@3@%%@CR:C6A00030010 @%%@AB@%The break Statement%@AE@%%@EH@%%@NL@%
  2558. %@NL@%
  2559. The previous section explained how to use %@AB@%break%@AE@% to exit from a %@AB@%switch%@AE@%
  2560. statement. You can also use %@AB@%break%@AE@% to end a loop immediately. The BREAKER.C
  2561. program shows how to do this. The program prints a prompt, then displays
  2562. characters as they are typed until the TAB key is pressed.  %@NL@%
  2563. %@NL@%
  2564. %@AS@%  /* BREAKER.C: Demonstrate break statement. */
  2565. %@AS@%  
  2566. %@AS@%  #include <stdio.h>
  2567. %@AS@%  #include <conio.h>
  2568. %@AS@%  
  2569. %@AS@%  main()
  2570. %@AS@%  {
  2571. %@AS@%     char ch;
  2572. %@AS@%     printf( "Press any key. Press Tab to quit.\n" );
  2573. %@AS@%     while( 1 )
  2574. %@AS@%     {
  2575. %@AS@%        ch = getche();
  2576. %@AS@%        if( ch == '\t' )%@AE@%%@NL@%
  2577. %@NL@%
  2578. %@AS@%  {
  2579. %@AS@%    printf( "\a\nYou pressed Tab\n" );
  2580. %@AS@%    break;
  2581. %@AS@%        }
  2582. %@AS@%     }
  2583. %@AS@%  }%@AE@%%@NL@%
  2584. %@NL@%
  2585. The %@AB@%while%@AE@% statement in BREAKER.C creates an indefinite loop that calls the
  2586. %@AB@%getche%@AE@% function again and again, assigning the function's return value to
  2587. the variable %@AS@% ch%@AE@%. The %@AB@%if%@AE@% statement in the loop body compares %@AS@% ch %@AE@% to the tab
  2588. character. When TAB is pressed, BREAKER.C prints %@AS@% You pressed Tab %@AE@% and
  2589. executes the %@AB@%break%@AE@% statement, which terminates the %@AB@%while%@AE@% loop and ends the
  2590. program.  %@NL@%
  2591. %@NL@%
  2592. %@AU@% A break statement exits only one loop.%@AE@%  %@NL@%
  2593. %@NL@%
  2594. It's important to remember that the %@AB@%break%@AE@% statement only ends the loop in
  2595. which it appears. If two loops are nested, executing a %@AB@%break%@AE@% in the inner
  2596. loop exits that loop but not the outer loop. BREAKER1.C shows how %@AB@%break%@AE@%
  2597. works within nested loops. The program's inner loop checks for the TAB key
  2598. and the outer loop checks for the ENTER key.  %@NL@%
  2599. %@NL@%
  2600. %@AS@%  /* BREAKER1.C: Break only exits one loop. */
  2601. %@AS@%  
  2602. %@AS@%  #include <stdio.h>
  2603. %@AS@%  #include <conio.h>
  2604. %@AS@%  
  2605. %@AS@%  main()
  2606. %@AS@%  {
  2607. %@AS@%     char ch;
  2608. %@AS@%     printf( "Press any key. Press Enter to quit.\n" );
  2609. %@AS@%     do
  2610. %@AS@%     {
  2611. %@AS@%        while( ( ch = getche() ) != '\r' )
  2612. %@AS@%        {
  2613. %@AS@%           if( ch == '\t' )
  2614. %@AS@%           {
  2615. %@AS@%              printf( "\a\nYou pressed Tab\n" );
  2616. %@AS@%              break;
  2617. %@AS@%           }
  2618. %@AS@%        }
  2619. %@AS@%     } while( ch != '\r' );
  2620. %@AS@%     printf( "\nBye bye." );
  2621. %@AS@%  }%@AE@%%@NL@%
  2622. %@NL@%
  2623. The BREAKER1.C program includes a %@AB@%while%@AE@% loop nested within a %@AB@%do%@AE@% loop. Both
  2624. loops test the same condition─whether the variable %@AS@% ch %@AE@% equals the ENTER key
  2625. (%@AB@%\r%@AE@%). The %@AB@%while%@AE@% loop also calls the %@AB@%getche%@AE@% function, assigning the
  2626. function's return value to %@AS@% ch%@AE@%.  %@NL@%
  2627. %@NL@%
  2628. When TAB is pressed, the program prints %@AS@% You pressed Tab %@AE@% and executes a
  2629. %@AB@%break%@AE@% statement, which terminates the inner loop. The %@AB@%break%@AE@% does not end the
  2630. outer loop, however. The program continues until ENTER is pressed, providing
  2631. the condition that ends both loops.  %@NL@%
  2632. %@NL@%
  2633. Note that %@AB@%break%@AE@% can only be used to exit a loop or %@AB@%switch%@AE@% statement. While
  2634. you might be tempted to use %@AB@%break%@AE@% to jump out of complex %@AB@%if%@AE@% or %@AB@%else%@AE@%
  2635. statements, the %@AB@%break%@AE@% statement cannot be used for this purpose. It has no
  2636. effect on %@AB@%if%@AE@% and %@AB@%else%@AE@%.  %@NL@%
  2637. %@NL@%
  2638. %@NL@%
  2639. %@3@%%@CR:C6A00030011 @%%@AB@%The continue Statement%@AE@%%@EH@%%@NL@%
  2640. %@NL@%
  2641. %@AU@% The continue statement skips remaining statements in the loop body where it
  2642. %@AU@%appears.%@AE@%  %@NL@%
  2643. %@NL@%
  2644. The %@AB@%continue%@AE@% statement, like %@AB@%break%@AE@%, interrupts the normal flow of execution
  2645. in a loop body. But instead of ending the loop, %@AB@%continue%@AE@% skips all following
  2646. statements in the loop body and triggers the next iteration of the loop.
  2647. This effect can be useful within complex loops, in which you might wish to
  2648. skip to the next loop iteration from various locations.  %@NL@%
  2649. %@NL@%
  2650. The CONT.C program shows how %@AB@%continue%@AE@% works. It increments the %@AS@% count %@AE@%
  2651. variable, counting from 0 through 9, but stops printing the value of %@AS@% count %@AE@%
  2652. when that value exceeds 3.  %@NL@%
  2653. %@NL@%
  2654. %@AS@%  /* CONT.C: Demonstrate continue statement. */
  2655. %@AS@%  
  2656. %@AS@%  #include <stdio.h>
  2657. %@AS@%  
  2658. %@AS@%  main()
  2659. %@AS@%  {
  2660. %@AS@%     int count;
  2661. %@AS@%     for( count = 0; count < 10; count = count + 1 )
  2662. %@AS@%     {
  2663. %@AS@%        if( count > 3 )
  2664. %@AS@%           continue;
  2665. %@AS@%        printf( "count = %d\n", count );
  2666. %@AS@%     }
  2667. %@AS@%     printf( "Done!\n" );
  2668. %@AS@%  }%@AE@%%@NL@%
  2669. %@NL@%
  2670. Here's the output from CONT.C:  %@NL@%
  2671. %@NL@%
  2672. %@AS@%  count = 0
  2673. %@AS@%  count = 1
  2674. %@AS@%  count = 2
  2675. %@AS@%  count = 3
  2676. %@AS@%  Done!%@AE@%%@NL@%
  2677. %@NL@%
  2678. The %@AB@%continue%@AE@% statement occurs within the body of the %@AB@%for%@AE@% loop. When the
  2679. value of %@AS@% count %@AE@% exceeds 3, the %@AB@%continue%@AE@% skips the rest of the loop body─a
  2680. statement that calls %@AB@%printf%@AE@%─and causes the next iteration of the loop.  %@NL@%
  2681. %@NL@%
  2682. %@NL@%
  2683. %@3@%%@CR:C6A00030012 @%%@AB@%The goto Statement%@AE@%%@EH@%%@NL@%
  2684. %@NL@%
  2685. %@AU@% The goto statement jumps from one part of the function to another.%@AE@%  %@NL@%
  2686. %@NL@%
  2687. Similar to the %@AB@%GOTO%@AE@% statement in BASIC, %@AB@%goto%@AE@% in C performs an unconditional
  2688. jump from one part of a function to any other part. The target of the %@AB@%goto%@AE@%
  2689. statement is a label which you supply. The label must end with a colon, as
  2690. do %@AB@%case%@AE@% labels, which we discussed earlier.  %@NL@%
  2691. %@NL@%
  2692. Most C programmers avoid using the %@AB@%goto%@AE@% statement. It's a bit inconsistent
  2693. with the overall philosophy of C, which encourages structured, modular
  2694. programming. And, regardless of philosophy, it can be very difficult to read
  2695. and debug a program that is littered with haphazard unconditional jumps.  %@NL@%
  2696. %@NL@%
  2697. Nevertheless, %@AB@%goto%@AE@% has at least one sensible use. If a serious error occurs
  2698. deep within a nested series of loops or conditional statements, %@AB@%goto%@AE@% offers
  2699. the simplest escape. The following code has several levels of nesting, with
  2700. a %@AB@%goto%@AE@% statement at the innermost level. If the value of %@AS@% error_count %@AE@%
  2701. exceeds 15, the %@AB@%goto%@AE@% statement executes, transferring control to the label %@AS@%
  2702. %@AS@%bail_out%@AE@%.  %@NL@%
  2703. %@NL@%
  2704. %@AS@%  if( a == 1 )
  2705. %@AS@%  {
  2706. %@AS@%     while( b == 2 )
  2707. %@AS@%     {
  2708. %@AS@%        for( c = 0; c < 3; c = c + 1 )
  2709. %@AS@%        {
  2710. %@AS@%           if( d == 4 )
  2711. %@AS@%           {
  2712. %@AS@%              while( e == 6 )
  2713. %@AS@%              {
  2714. %@AS@%                 if( error_count > 15 )
  2715. %@AS@%                    goto bail_out;
  2716. %@AS@%              }
  2717. %@AS@%           }
  2718. %@AS@%        }
  2719. %@AS@%     }
  2720. %@AS@%  }
  2721. %@AS@%  bail_out:  /* The goto statement transfers control here. */%@AE@%%@NL@%
  2722. %@NL@%
  2723. To achieve the same effect without %@AB@%goto%@AE@%, you'd have to add extra conditional
  2724. tests to this code, making the code more complex and perhaps less efficient.
  2725. %@NL@%
  2726. %@NL@%
  2727. Names in %@AB@%goto%@AE@% labels are governed by the rules for variable names, which
  2728. we'll discuss in the next two chapters. For now, just remember that a %@AB@%goto%@AE@%
  2729. label is visible only in the function in which it appears. You can't execute
  2730. a %@AB@%goto%@AE@% statement to jump from one function to another function.  %@NL@%
  2731. %@NL@%
  2732. The next two chapters explain how to create and manipulate data─variables
  2733. and constants─in C programs. Chapter 4, "Data Types," describes the basics,
  2734. such as how to declare and initialize variables of different types. Chapter
  2735. 5, "Advanced Data Types," describes more advanced topics, such as the
  2736. visibility of variables.  %@NL@%
  2737. %@NL@%
  2738. %@NL@%
  2739. %@NL@%
  2740. %@NL@%
  2741. %@NL@%
  2742. %@NL@%
  2743. %@CR:C6A00040001 @%%@1@%%@AB@%Chapter 4  Data Types%@AE@%%@EH@%%@NL@%
  2744. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  2745. %@NL@%
  2746. This chapter explains the C data types and shows how to declare and use C
  2747. variables. The chapter begins by describing the basic data types from which
  2748. all other data types are derived. We then discuss more complex data types,
  2749. including arrays and structures. In Chapter 5, "Advanced Data Types," we'll
  2750. explore more advanced data-handling topics, such as variable visibility and
  2751. automatic type conversions.  %@NL@%
  2752. %@NL@%
  2753. %@NL@%
  2754. %@2@%%@CR:C6A00040002 @%%@AB@%Basic Data Types%@AE@%%@EH@%%@NL@%
  2755. %@NL@%
  2756. All data in C programs is either a constant or variable, and each has an
  2757. associated data type. The concept of types is common to all high-level
  2758. languages. For instance, an integer (whole) number has the %@AB@%INTEGER%@AE@% type in
  2759. QuickBASIC, the %@AB@%Integer%@AE@% type in QuickPascal, and the %@AB@%int%@AE@% type in C. This
  2760. section describes the basic data types in C and explains how to specify
  2761. variables and constants using these types.  %@NL@%
  2762. %@NL@%
  2763. All of the basic data types contain a single value. Types that contain more
  2764. than one value─arrays, structures, and unions─are called "aggregate types."
  2765. We'll discuss aggregate types later in this chapter.  %@NL@%
  2766. %@NL@%
  2767. %@NL@%
  2768. %@3@%%@CR:C6A00040003 @%%@AB@%Specifying Basic Types%@AE@%%@EH@%%@NL@%
  2769. %@NL@%
  2770. The C language has four basic data types, which are specified with the
  2771. keywords %@AB@%char%@AE@%, %@AB@%int%@AE@%, %@AB@%float%@AE@%, and %@AB@%double%@AE@%. The %@AB@%char%@AE@% (character) type is used for
  2772. text and the %@AB@%int%@AE@% type for integers. The %@AB@%float%@AE@% and %@AB@%double%@AE@% types express real
  2773. (floating-point) values.  %@NL@%
  2774. %@NL@%
  2775. The TYPES.C program creates variables of the four basic types and prints
  2776. their values:  %@NL@%
  2777. %@NL@%
  2778. %@AS@%  /* TYPES.C: Illustrate basic data types. */
  2779. %@AS@%  
  2780. %@AS@%  #include <stdio.h>
  2781. %@AS@%  
  2782. %@AS@%  main()
  2783. %@AS@%  {
  2784. %@AS@%     char char_val       = 'a';
  2785. %@AS@%     int int_val         = 543;
  2786. %@AS@%     float float_val     = 11.1;
  2787. %@AS@%     double double_val   = 66.123456789;
  2788. %@AS@%     printf( "char_val   = %c\n", char_val );
  2789. %@AS@%     printf( "int_val    = %d\n", int_val );
  2790. %@AS@%     printf( "float_val  = %f\n", float_val );
  2791. %@AS@%     printf( "double_val = %2.9f\n", double_val );
  2792. %@AS@%  }%@AE@%%@NL@%
  2793. %@NL@%
  2794. Here is the output from TYPES.C:  %@NL@%
  2795. %@NL@%
  2796. %@AS@%  char_val   = a
  2797. %@AS@%  int_val    = 543
  2798. %@AS@%  float_val  = 11.100000
  2799. %@AS@%  double_val = 66.123456789%@AE@%%@NL@%
  2800. %@NL@%
  2801. Each basic data type requires a different amount of memory, as illustrated
  2802. in Figure 4.1. In QuickC, a %@AB@%char%@AE@% contains one byte, an %@AB@%int%@AE@% has two bytes, a
  2803. %@AB@%float%@AE@% has four bytes, and a %@AB@%double%@AE@% type has eight bytes.  %@NL@%
  2804. %@NL@%
  2805. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  2806. %@NL@%
  2807. ────────────────────────────────────────────────────────────────────────────%@NL@%
  2808. NOTE
  2809.  
  2810. %@AI@%The C language is designed to run on many different computers, with machine
  2811. %@AI@%architectures that may be quite different. To accommodate these differences,
  2812. %@AI@%some C data types are "implementation dependent," meaning their sizes depend
  2813. %@AI@%on which computer you're using. For instance, the%@AI@% %@AE@%%@AI@%%@AB@%int%@AE@%%@AE@%%@AI@% (i%@AE@%%@AI@%nteger) type
  2814. %@AI@%contains two bytes on IBM PC computers and four bytes on VAX%@AE@%%@AI@%(R)
  2815. %@AI@%%@AE@%%@AI@%minicomputers. These differences are %@AE@%%@AI@%%@AE@%%@AI@%i%@AE@%%@AI@%mportant only if you're transporting a
  2816. %@AI@%program%@AE@%%@AI@%%@AE@%%@AI@% f%@AE@%%@AI@%rom one%@AE@%%@AI@%%@AE@%%@AI@% %@AE@%%@AI@%operating system to another. Since QuickC runs only under
  2817. %@AI@%one operating system (DOS), this book describes C data types in DOS.%@AE@%%@AI@%%@AE@%%@AI@%%@AE@%%@AE@%%@NL@%
  2818. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  2819. %@NL@%
  2820. %@NL@%
  2821. %@4@%%@AB@%Special Type Specifiers%@AE@%%@EH@%%@NL@%
  2822. %@NL@%
  2823. The C language has four special type specifiers─%@AB@%signed%@AE@%, %@AB@%unsigned%@AE@%, %@AB@%long%@AE@%, and
  2824. %@AB@%short%@AE@%. These act as "adjectives" to modify the range of values expressed by
  2825. a basic data type.  %@NL@%
  2826. %@NL@%
  2827. %@AU@% The char and int data types  are signed by default.%@AE@%  %@NL@%
  2828. %@NL@%
  2829. The %@AB@%signed%@AE@% keyword signifies that a value can be either negative or
  2830. nonnegative. If you don't specify, a %@AB@%char%@AE@% or %@AB@%int%@AE@% value is signed.  %@NL@%
  2831. %@NL@%
  2832. You can preface a %@AB@%char%@AE@% or %@AB@%int%@AE@% with %@AB@%unsigned%@AE@% to extend the range of
  2833. nonnegative values. An %@AB@%unsigned int%@AE@% can have a value in the range 0 through
  2834. 65,535, and an %@AB@%unsigned char%@AE@% can have a value of 0 through 255.  %@NL@%
  2835. %@NL@%
  2836. The %@AB@%long%@AE@% keyword is used to increase the size of an %@AB@%int%@AE@% or %@AB@%double%@AE@% type. A
  2837. %@AB@%long int%@AE@% value contains four bytes (twice as many as an %@AB@%int%@AE@%) and expresses
  2838. an integer in the range -2,147,483,648 through 2,147,483,647. A %@AB@%long double%@AE@%
  2839. value contains 10 bytes and can express a floating-point number with 19
  2840. digits of precision.  %@NL@%
  2841. %@NL@%
  2842. In QuickC, the %@AB@%short int%@AE@% type is identical to the %@AB@%int%@AE@% type. (This is not the
  2843. case in some operating systems other than DOS.)  %@NL@%
  2844. %@NL@%
  2845. Table 4.1 lists the basic data types and the range of values each can
  2846. express.  %@NL@%
  2847. %@NL@%
  2848. %@AB@%Table 4.1  %@AB@%Basic Data Types%@AE@%%@AE@%
  2849.  
  2850. %@TH:  38  1798 02 17 25 34 @%Type Name        Other Names              Range of Values%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%char%@AE@%             %@AB@%signed char%@AE@%              -128 to 127%@AB@%unsigned char%@AE@%    none                     0 to 255%@AB@%int%@AE@%              %@AB@%signed%@AE@%, %@AB@%signed int%@AE@%       -32,768 to 32,767%@AB@%unsigned%@AE@%         %@AB@%unsigned int%@AE@%             0 to 65,535%@AB@%unsigned short%@AE@%   %@AB@%unsigned short int %@AE@%      0 to 65,535%@AB@%short%@AE@%            %@AB@%short int%@AE@%, %@AB@%signed short%@AE@%  -32,768 to 32,767                 %@AB@%signed short int%@AE@% %@AB@%long%@AE@%             %@AB@%long int%@AE@%, %@AB@%signed long%@AE@%    -2,147,483,648 to%@AB@%%@AE@%                 %@AB@%signed long int%@AE@%          2,147,483,647%@AB@%unsigned long%@AE@%    %@AB@%unsigned long int%@AE@%        0 to 4,294,967,295                 %@AB@%%@AE@%                                          %@AB@%%@AE@%                                          %@AB@% %@AE@%                        %@AB@%float%@AE@%            none                     Approximately 1.2E-38 to 3.4E+38                                          (7-digit                                           precision)%@AB@%double%@AE@%           none                     Approximately 2.2E-308 to                                           1.8E+308 (15-digit                                           precision)%@AB@%long double%@AE@%      none                     Approximately 3.4E-4932 to                                           1.2E+4932 (19-digit precision)%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  38  1798 02 17 25 34 @%
  2851.  
  2852. Most programmers take advantage of type defaults. If a type qualifier
  2853. appears alone, the type %@AB@%int%@AE@% is implied. By itself, %@AB@%short%@AE@% is a synonym for
  2854. %@AB@%short int%@AE@%. Where %@AB@%long%@AE@% appears alone it is a synonym for %@AB@%long int%@AE@%, and where
  2855. %@AB@%unsigned%@AE@% appears alone it is a synonym for %@AB@%unsigned int%@AE@%.  %@NL@%
  2856. %@NL@%
  2857. %@NL@%
  2858. %@3@%%@CR:C6A00040004 @%%@AB@%Specifying Variables%@AE@%%@EH@%%@NL@%
  2859. %@NL@%
  2860. As we mentioned in Chapter 1, "Anatomy of a C Program," you must declare
  2861. every variable in a C program by stating the variable's name and type.
  2862. Variable names are governed by the following rules, which also apply to
  2863. other userdefined names such as function names:  %@NL@%
  2864. %@NL@%
  2865. %@NL@%
  2866.   ■   C is case-sensitive. For example, %@AS@% myvar%@AE@%, %@AS@% MyVar%@AE@%, and %@AS@% MYVAR %@AE@% are
  2867.       different names.%@NL@%
  2868. %@NL@%
  2869.   ■   The name can't be a keyword (see online help for a list of keywords).%@NL@%
  2870. %@NL@%
  2871.   ■   The first character must be a letter or underscore (%@AS@% %@AE@%%@AB@%_ %@AE@%). Many of
  2872.       QuickC's system-defined names, including some library-routine names,
  2873.       begin with underscores. To avoid conflicts with such names, don't
  2874.       create names that begin with underscores.%@NL@%
  2875. %@NL@%
  2876.   ■   Other characters can be letters, digits, or underscores.%@NL@%
  2877. %@NL@%
  2878.   ■   The first 31 characters of local variable names are significant. The
  2879.       name can contain more than 31 characters, but QuickC ignores
  2880.       everything beyond the thirty-first character. Global variable names
  2881.       are normally significant to 30 characters.%@NL@%
  2882. %@NL@%
  2883. %@NL@%
  2884. All C keywords are lowercase, and it's common to use lowercase for variable
  2885. names. Mixed case is becoming popular in some contexts, however. OS/2 and
  2886. Microsoft Windows(tm) use mixed case for most system-defined names.  %@NL@%
  2887. %@NL@%
  2888. %@NL@%
  2889. %@3@%%@CR:C6A00040005 @%%@AB@%Specifying Constants%@AE@%%@EH@%%@NL@%
  2890. %@NL@%
  2891. Constants─values that don't change during the life of the program─can be
  2892. numbers, characters, or strings. Your program can also define "symbolic
  2893. constants," which are names that represent constant values. This section
  2894. describes how to specify C constants.  %@NL@%
  2895. %@NL@%
  2896. %@NL@%
  2897. %@4@%%@AB@%Numeric Constants%@AE@%%@EH@%%@NL@%
  2898. %@NL@%
  2899. A numeric constant can have any basic data type, and can be specified in
  2900. decimal, hexadecimal, or octal notation. Table 4.2 shows how to specify
  2901. numeric constants.  %@NL@%
  2902. %@NL@%
  2903. %@AB@%Table 4.2  %@AB@%Constant Specifications%@AE@%%@AE@%
  2904.  
  2905. %@TH:  13   691 02 19 57 @%Constant           Type%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%255                decimal %@AB@%int%@AE@%0xFF               hexadecimal %@AB@%int%@AE@% (255)0377               octal %@AB@%int %@AE@%(255)255L               %@AB@%long int%@AE@%255U               %@AB@%unsigned int%@AE@%0xFFul             %@AB@%long unsigned %@AE@%hexadecimal %@AB@%int%@AE@% (255)15.75E2            floating point (1575)-.123              floating point.123               floating point3e0f               floating point%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  13   691 02 19 57 @%
  2906.  
  2907. A number without a suffix, such as 255, is treated as decimal. The %@AS@% 0x %@AE@%
  2908. prefix specifies a hexadecimal number, and the %@AS@% 0 %@AE@% (zero) prefix specifies
  2909. octal (base 8).  %@NL@%
  2910. %@NL@%
  2911. If a number doesn't have a decimal point, it is an integer. Integers are
  2912. signed by default; you can use the suffix %@AS@% U %@AE@% or %@AS@% u %@AE@% to specify an unsigned
  2913. constant. To specify a %@AB@%long%@AE@% integer, place the suffix %@AS@% L %@AE@% or %@AS@% l %@AE@% after the
  2914. number.  %@NL@%
  2915. %@NL@%
  2916. A floating-point constant contains either a decimal point or an exponent
  2917. preceded by %@AS@% e %@AE@% or %@AS@% E %@AE@%. It can optionally include the suffix %@AS@% F %@AE@% or %@AS@% f %@AE@% to
  2918. denote the %@AB@%float%@AE@% type or the suffix %@AS@% L %@AE@% or %@AS@% l%@AE@% to denote the %@AB@%long double%@AE@%
  2919. type.  %@NL@%
  2920. %@NL@%
  2921. %@NL@%
  2922. %@4@%%@AB@%Character and String Constants%@AE@%%@EH@%%@NL@%
  2923. %@NL@%
  2924. The C language uses different notation for character and string constants. A
  2925. single character enclosed in single quotes is a character constant:  %@NL@%
  2926. %@NL@%
  2927. %@AS@%  'a'%@AE@%%@NL@%
  2928. %@NL@%
  2929. A string constant is 0 or more characters enclosed in double quotes:  %@NL@%
  2930. %@NL@%
  2931. %@AS@%  "Hello"%@AE@%%@NL@%
  2932. %@NL@%
  2933. A string also ends with a null character (%@AB@%\0%@AE@%), as we'll see in the section
  2934. "Strings."  %@NL@%
  2935. %@NL@%
  2936. The difference between character and string constants is important when you
  2937. perform comparisons. The character constant %@AS@% 'a' %@AE@% contains 1 character, but
  2938. the string constant %@AS@% "a" %@AE@% contains 2 characters: the letter %@AS@% a %@AE@% plus a null
  2939. character. Because the two values have a different number of characters, any
  2940. comparison of them is invalid. (See "String Problems" in Chapter 10,
  2941. "Programming Pitfalls.")  %@NL@%
  2942. %@NL@%
  2943. You can specify special characters, such as the tab and backspace, with a
  2944. multi-character sequence that begins with a backslash ( \ ). These sequences
  2945. are sometimes called "escape sequences." Table 4.3 shows the special
  2946. character sequences.  %@NL@%
  2947. %@NL@%
  2948. %@AB@%Table 4.3  %@AB@%Special Characters%@AE@%%@AE@%
  2949.  
  2950. %@TH:  16   906 02 24 52 @%Sequence                Character%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%\a%@AE@%                      Alert (bell)%@AB@%\b %@AE@%                     Backspace%@AB@%\f%@AE@%                      Form feed%@AB@%\n%@AE@%                      Newline%@AB@%\r%@AE@%                      Carriage return%@AB@%\t%@AE@%                      Horizontal tab%@AB@%\v%@AE@%                      Vertical tab%@AB@%\'%@AE@%                      Single quote%@AB@%\"%@AE@%                      Double quote%@AB@%\\%@AE@%                      Backslash%@AB@%\%@AE@%%@AI@%ooo%@AE@%                    Octal notation%@AB@%\%@AE@%%@AI@%xhh%@AE@%                    Hexadecimal notation%@AB@%\0%@AE@%                      Null%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  16   906 02 24 52 @%
  2951.  
  2952. Some unusual characters don't have a predefined sequence. You can specify
  2953. these with a backslash ( %@AB@%\ %@AE@%) followed by the hexadecimal or octal number
  2954. representing the character's ASCII value. For instance, a telecommunications
  2955. program might need to specify ASCII 21, the NAK ("not acknowledged")
  2956. character. You can specify this character in either hexadecimal notation, %@AS@%
  2957. %@AS@%'\x15'%@AE@%, or octal notation, %@AS@%'\25'%@AE@%. Note that the hexadecimal number begins
  2958. with %@AS@% \x%@AE@%, while the octal number starts with a backslash alone.  %@NL@%
  2959. %@NL@%
  2960. %@NL@%
  2961. %@4@%%@AB@%Symbolic Constants%@AE@%%@EH@%%@NL@%
  2962. %@NL@%
  2963. A "symbolic constant" is a user-defined name that represents a constant.
  2964. Symbolic constants are usually typed in uppercase. For instance, the
  2965. directive  %@NL@%
  2966. %@NL@%
  2967. %@AS@%  #define PI 3.14%@AE@%%@NL@%
  2968. %@NL@%
  2969. declares a symbolic constant named %@AS@% PI%@AE@%. Wherever %@AS@% PI %@AE@% occurs in the program,
  2970. the compiler substitutes %@AS@% 3.14%@AE@%. Chapter 7, "Preprocessor Directives,"
  2971. discusses symbolic constants and the %@AB@%#define%@AE@% directive.  %@NL@%
  2972. %@NL@%
  2973. %@NL@%
  2974. %@2@%%@CR:C6A00040006 @%%@AB@%Aggregate Data Types%@AE@%%@EH@%%@NL@%
  2975. %@NL@%
  2976. This section describes aggregate data types, which contain organized
  2977. collections of data in a definite order. In C, the aggregate types are
  2978. arrays, structures, and unions.  %@NL@%
  2979. %@NL@%
  2980. An "array" is a collection of data items of the same type. Programs use
  2981. arrays in cases where a standard data format is repeated many times. For
  2982. example, you might use an array to store numbers representing the population
  2983. of Minnesota for all the years from 1950 to 2000. C-language arrays are very
  2984. similar to arrays in QuickPascal and QuickBASIC.  %@NL@%
  2985. %@NL@%
  2986. A "structure" is a collection of data items of different types. Programs use
  2987. structures in cases where a variety of data have a close association. For
  2988. example, you might use a structure to store information about a given
  2989. employee─name, months of employment, and hourly wage. Structures are similar
  2990. to QuickPascal records or QuickBASIC user-defined types.  %@NL@%
  2991. %@NL@%
  2992. A "union" allows you to use different data formats to access the same area
  2993. of memory. It can hold different kinds of information at different times.
  2994. Unions are similar to variant records in QuickPascal.  %@NL@%
  2995. %@NL@%
  2996. %@NL@%
  2997. %@3@%%@CR:C6A00040007 @%%@AB@%Arrays%@AE@%%@EH@%%@NL@%
  2998. %@NL@%
  2999. %@AU@% An array is a group of  data items of the same  type under one name.%@AE@%  %@NL@%
  3000. %@NL@%
  3001. The simplest aggregate data type is an array: a group of data items that
  3002. share the same type and a common name. You can make an array from any data
  3003. type, including basic types such as %@AB@%char%@AE@% and %@AB@%int%@AE@% and more complex types such
  3004. as structures. This section shows how to declare, initialize, and access
  3005. arrays, including arrays with more than one dimension. We'll begin with a
  3006. simple example that creates a one-dimensional array.  %@NL@%
  3007. %@NL@%
  3008. %@NL@%
  3009. %@4@%%@AB@%Creating a Simple Array%@AE@%%@EH@%%@NL@%
  3010. %@NL@%
  3011. The ARRAY.C program creates the array %@AS@% i_array%@AE@%, which contains three
  3012. integers.  %@NL@%
  3013. %@NL@%
  3014. %@AS@%  /* ARRAY.C: Demonstrate one-dimensional array. */
  3015. %@AS@%  
  3016. %@AS@%  #include <stdio.h>
  3017. %@AS@%  
  3018. %@AS@%  main()
  3019. %@AS@%  {
  3020. %@AS@%     int j;
  3021. %@AS@%     int i_array[3];
  3022. %@AS@%  
  3023. %@AS@%     i_array[0] = 176;
  3024. %@AS@%     i_array[1] = 4069;
  3025. %@AS@%     i_array[2] = 303;
  3026. %@AS@%  
  3027. %@AS@%     printf( "--- Values --------     --- Addresses -------\n\n" );
  3028. %@AS@%  
  3029. %@AS@%     for( j = 0; j < 3; j = j + 1 )
  3030. %@AS@%     {
  3031. %@AS@%        printf( "i_array[%d] = %d", j, i_array[j] );
  3032. %@AS@%        printf( "\t&i_array[%d] = %u\n", j, &i_array[j] );
  3033. %@AS@%     }
  3034. %@AS@%  
  3035. %@AS@%  }%@AE@%%@NL@%
  3036. %@NL@%
  3037. Here is the output from ARRAY.C:  %@NL@%
  3038. %@NL@%
  3039. %@AS@%  --- Values --------     --- Addresses -------
  3040. %@AS@%  
  3041. %@AS@%  i_array[0] = 176        &i_array[0] = 3506
  3042. %@AS@%  i_array[1] = 4069       &i_array[1] = 3508
  3043. %@AS@%  i_array[2] = 303        &i_array[2] = 3510%@AE@%%@NL@%
  3044. %@NL@%
  3045. As you can see, ARRAY.C prints the values in %@AS@% i_array %@AE@% and the memory
  3046. address where each array element is stored. You usually don't have to worry
  3047. about actual memory addresses in C, but it's useful to have some idea how
  3048. array elements are stored in memory. Depending on factors such as the amount
  3049. of memory in your system, you may see different addresses when you run
  3050. ARRAY.C.  %@NL@%
  3051. %@NL@%
  3052. (The second %@AB@%printf%@AE@% statement uses the "address-of" operator (%@AB@%&%@AE@%) to determine
  3053. the address of each array element. Chapter 6, "Operators," explains this
  3054. operator. For now, it's sufficient to recognize that the operator allows
  3055. ARRAY.C to print addresses.)  %@NL@%
  3056. %@NL@%
  3057. Figure 4.2 shows how %@AS@% i_array %@AE@% is stored in the addresses from the ARRAY.C
  3058. output.  %@NL@%
  3059. %@NL@%
  3060. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3061. %@NL@%
  3062. %@NL@%
  3063. %@4@%%@AB@%Declaring the Array%@AE@%%@EH@%%@NL@%
  3064. %@NL@%
  3065. You declare an array variable by stating its type and its name, as you would
  3066. a simple variable. You must also declare the size of the array, stating the
  3067. number of elements with an integer constant in square brackets. For example,
  3068. the line  %@NL@%
  3069. %@NL@%
  3070. %@AS@%  int i_array[3];%@AE@%%@NL@%
  3071. %@NL@%
  3072. from ARRAY.C declares a three-element integer (%@AB@%int%@AE@%) array named %@AS@% i_array%@AE@%.  %@NL@%
  3073. %@NL@%
  3074. Multidimensional arrays are declared the same way, except you must give the
  3075. size of each dimension. The following statement, for instance, declares a
  3076. twodimensional %@AB@%int%@AE@% array named %@AS@% two_dim%@AE@%:  %@NL@%
  3077. %@NL@%
  3078. %@AS@%  int two_dim[2][3];%@AE@%%@NL@%
  3079. %@NL@%
  3080. We'll return to multidimensional arrays a little later in this chapter.  %@NL@%
  3081. %@NL@%
  3082. %@NL@%
  3083. %@4@%%@AB@%Initializing the Array%@AE@%%@EH@%%@NL@%
  3084. %@NL@%
  3085. Arrays, like simple variables, should be initialized before use. ARRAY.C
  3086. initializes %@AS@% i_array %@AE@% with these statements:  %@NL@%
  3087. %@NL@%
  3088. %@AS@%  i_array[0] = 176;
  3089. %@AS@%  i_array[1] = 4069;
  3090. %@AS@%  i_array[2] = 303;%@AE@%%@NL@%
  3091. %@NL@%
  3092. %@AU@% An array can be initialized  when it is declared.%@AE@%  %@NL@%
  3093. %@NL@%
  3094. ARRAY.C declares an array in one statement and then initializes its elements
  3095. one by one. You can also initialize an array when you declare it. The
  3096. following statement does both jobs at once:  %@NL@%
  3097. %@NL@%
  3098. %@AS@%  int i_array[3] = { 176, 4069, 303 };%@AE@%%@NL@%
  3099. %@NL@%
  3100. Note the curly braces around the initializing values. The braces are
  3101. mandatory in this kind of initialization.  %@NL@%
  3102. %@NL@%
  3103. Under the ANSI C standard, which QuickC version 2.5 follows, you can
  3104. simultaneously declare and initialize an array within a function. Pre-ANSI
  3105. compilers, including QuickC version 1.0, don't allow this unless the %@AB@%static%@AE@%
  3106. keyword precedes the array declaration. Chapter 5, "Advanced Data Types,"
  3107. discusses %@AB@%static%@AE@%.  %@NL@%
  3108. %@NL@%
  3109. When you declare and initialize an array at the same time, the initializing
  3110. values are normally constants, as shown above. Occasionally, you may want to
  3111. initialize an array as you declare it using variables instead of constants.
  3112. QuickC version 2.5 allows this, but only within a function. The %@AS@% sample %@AE@%
  3113. array in the following example is initialized legally under QuickC version
  3114. 2.5 but illegally under QuickC version 1.0:  %@NL@%
  3115. %@NL@%
  3116. %@AS@%  func()
  3117. %@AS@%  {
  3118. %@AS@%     int val = 5;
  3119. %@AS@%     int sample[3] = { val, val, val };
  3120. %@AS@%  }%@AE@%%@NL@%
  3121. %@NL@%
  3122. If you initialize a local array in this way, you must include the size of
  3123. the array within the square brackets following the array name. If the
  3124. example initialized the %@AS@% sample %@AE@% array with the following line:  %@NL@%
  3125. %@NL@%
  3126. %@AS@%  int sample[ ] = {val, val, val};%@AE@%%@NL@%
  3127. %@NL@%
  3128. QuickC would issue an error because the size of the array (3) is not
  3129. specified.  %@NL@%
  3130. %@NL@%
  3131. %@NL@%
  3132. %@4@%%@AB@%Specifying Array Elements%@AE@%%@EH@%%@NL@%
  3133. %@NL@%
  3134. %@AU@% Array subscripts are enclosed in square brackets ( [ ] ).%@AE@%  %@NL@%
  3135. %@NL@%
  3136. You specify an array element by giving its position, using an integer value
  3137. called a "subscript." Square brackets ([ ]) enclose each subscript. In the
  3138. ARRAY.C program above we specify the first element of %@AS@% i_array %@AE@% as  %@NL@%
  3139. %@NL@%
  3140. %@AS@%  i_array[0]%@AE@%%@NL@%
  3141. %@NL@%
  3142. Notice that the first element of a C array has the subscript 0, not 1.
  3143. Unlike QuickPascal and QuickBASIC, the C language does not give you the
  3144. option to start at an index number other than 0.  %@NL@%
  3145. %@NL@%
  3146. Since array subscripts begin at 0, the subscript of the last array element
  3147. is 1 less than the number used to declare that dimension of the array. In
  3148. ARRAY.C, the last element of %@AS@% i_array %@AE@% is %@AS@% i_array[2] %@AE@%, not %@AS@% i_array[3]%@AE@%.  %@NL@%
  3149. %@NL@%
  3150. %@AU@% C doesn't check array subscripts.%@AE@%  %@NL@%
  3151. %@NL@%
  3152. Unlike QuickBASIC and QuickPascal, C doesn't check the validity of array
  3153. subscripts. If the ARRAY.C program included the expression  %@NL@%
  3154. %@NL@%
  3155. %@AS@%  i_array[55];%@AE@%%@NL@%
  3156. %@NL@%
  3157. it would refer to a nonexistent array element. (The expression refers to the
  3158. element 55, but %@AS@% i_array %@AE@% contains only three elements.) This would not
  3159. trigger a compiler error or run-time error, however. It's your job to
  3160. remember the size of the array and avoid references that go outside the
  3161. array's boundaries. This rule is also important when you're accessing arrays
  3162. with pointers (see Chapter 8, "Pointers").  %@NL@%
  3163. %@NL@%
  3164. %@NL@%
  3165. %@4@%%@AB@%Strings%@AE@%%@EH@%%@NL@%
  3166. %@NL@%
  3167. %@AU@% A string is an array  of characters.%@AE@%  %@NL@%
  3168. %@NL@%
  3169. You may have wondered why we didn't mention strings in our earlier
  3170. description of basic data types. The reason is that strings aren't a formal
  3171. data type. In the C language, a string is simply an array of characters
  3172. (%@AB@%char%@AE@% values).  %@NL@%
  3173. %@NL@%
  3174. The STRING.C program below creates the string %@AS@% c_array %@AE@% and displays its
  3175. contents in the same format as the previous example. The program prints the
  3176. value of each array element and its address.  %@NL@%
  3177. %@NL@%
  3178. %@AS@%  /* STRING.C: Demonstrate string arrays. */
  3179. %@AS@%  
  3180. %@AS@%  #include <stdio.h>
  3181. %@AS@%  
  3182. %@AS@%  main()
  3183. %@AS@%  {
  3184. %@AS@%     int j;
  3185. %@AS@%     char c_array[] = "Hello";
  3186. %@AS@%  
  3187. %@AS@%     printf( "--- Values --------     --- Addresses -------\n\n" );
  3188. %@AS@%  
  3189. %@AS@%     for( j = 0; j < 6; j = j + 1 )
  3190. %@AS@%     {
  3191. %@AS@%        printf( "c_array[%d]   = %x %c", j, c_array[j], c_array[j] );
  3192. %@AS@%        printf( "\t&c_array[%d]    = %u\n", j, &c_array[j] );
  3193. %@AS@%     }
  3194. %@AS@%  }%@AE@%%@NL@%
  3195. %@NL@%
  3196. Here is the output from STRING.C:  %@NL@%
  3197. %@NL@%
  3198. %@AS@%  --- Values --------     --- Addresses -------
  3199. %@AS@%  
  3200. %@AS@%  c_array[0]   = 48 H     &c_array[0]    = 3522
  3201. %@AS@%  c_array[1]   = 65 e     &c_array[1]    = 3523
  3202. %@AS@%  c_array[2]   = 6c l     &c_array[2]    = 3524
  3203. %@AS@%  c_array[3]   = 6c l     &c_array[3]    = 3525
  3204. %@AS@%  c_array[4]   = 6f o     &c_array[4]    = 3526
  3205. %@AS@%  c_array[5]   = 0        &c_array[5]    = 3527%@AE@%%@NL@%
  3206. %@NL@%
  3207. Figure 4.3 shows how %@AS@% c_array %@AE@% is stored in memory. Again, the addresses in
  3208. the output may differ depending on factors such as the amount of available
  3209. memory.  %@NL@%
  3210. %@NL@%
  3211. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3212. %@NL@%
  3213. %@AU@% A string ends with  a null character.%@AE@%  %@NL@%
  3214. %@NL@%
  3215. The figure illustrates another important feature of strings. Although %@AS@%
  3216. %@AS@%c_array %@AE@% has five printing characters (%@AS@% Hello %@AE@%), it actually contains six
  3217. characters─five letters plus a null character (%@AB@%\0%@AE@%) that marks the end of the
  3218. string. As noted earlier, the C language automatically adds a null character
  3219. to every string enclosed in double quotes.  %@NL@%
  3220. %@NL@%
  3221. STRING.C uses a shortcut when it initializes %@AS@% c_array%@AE@%. You may have noticed
  3222. that the array declaration  %@NL@%
  3223. %@NL@%
  3224. %@AS@%  char c_array[] = "Hello";%@AE@%%@NL@%
  3225. %@NL@%
  3226. doesn't declare the array's size (the square brackets are empty). When an
  3227. array is initialized at the same time it's declared, QuickC can figure out
  3228. how many elements the array has by counting the number of initializing
  3229. values to the right of the equal sign.  %@NL@%
  3230. %@NL@%
  3231. You can use this shortcut for any type of array, not just a %@AB@%char%@AE@% array. If
  3232. the array has more than one dimension, however, you can only omit the size
  3233. of the first dimension.  %@NL@%
  3234. %@NL@%
  3235. %@NL@%
  3236. %@4@%%@AB@%Multidimensional Arrays%@AE@%%@EH@%%@NL@%
  3237. %@NL@%
  3238. A "multidimensional" array contains two or more array dimensions. The
  3239. TWODIM.C program below creates a two-dimensional array named %@AS@% i_array.  %@AE@%%@NL@%
  3240. %@NL@%
  3241. %@AS@%  /* TWODIM.C: Demonstrate multidimensional arrays. */
  3242. %@AS@%  
  3243. %@AS@%  #include <stdio.h>
  3244. %@AS@%  
  3245. %@AS@%  main()
  3246. %@AS@%  {
  3247. %@AS@%     int j, k;
  3248. %@AS@%     int i_array[2][3] = { { 176, 4069, 303 }, { 6, 55, 777 } };
  3249. %@AS@%  
  3250. %@AS@%     printf( "--- Values --------     --- Addresses -------\n\n" );
  3251. %@AS@%  
  3252. %@AS@%     for( j = 0; j < 2; j = j + 1 )
  3253. %@AS@%     {
  3254. %@AS@%        for( k = 0; k < 3; k = k + 1 )
  3255. %@AS@%        {
  3256. %@AS@%           printf( "i_array[%d][%d] = %d", j, k, i_array[j][k] );
  3257. %@AS@%           printf( "\t&i_array[%d][%d] = %u\n", j, k, &i_array[j][k] );
  3258. %@AS@%        }
  3259. %@AS@%        printf( "\n" );
  3260. %@AS@%     }
  3261. %@AS@%  
  3262. %@AS@%  }%@AE@%%@NL@%
  3263. %@NL@%
  3264. Here's the output from TWODIM.C:  %@NL@%
  3265. %@NL@%
  3266. %@AS@%  --- Values --------     --- Addresses -------
  3267. %@AS@%  
  3268. %@AS@%  i_array[0][0] = 176     &i_array[0][0] = 3498
  3269. %@AS@%  i_array[0][1] = 4069    &i_array[0][1] = 3500
  3270. %@AS@%  i_array[0][2] = 303     &i_array[0][2] = 3502
  3271. %@AS@%  
  3272. %@AS@%  i_array[1][0] = 6       &i_array[1][0] = 3504
  3273. %@AS@%  i_array[1][1] = 55      &i_array[1][1] = 3506
  3274. %@AS@%  i_array[1][2] = 777     &i_array[1][2] = 3508%@AE@%%@NL@%
  3275. %@NL@%
  3276. Each subscript of a multidimensional array appears in its own set of square
  3277. brackets, as the TWODIM.C output shows. When you declare the array, the
  3278. first subscript states the size of the first dimension, the second states
  3279. the size of the second dimension, and so on. In TWODIM.C, the declaration of
  3280. %@AS@% i_array%@AE@%,  %@NL@%
  3281. %@NL@%
  3282. %@AS@%  int i_array[2][3]%@AE@%%@NL@%
  3283. %@NL@%
  3284. states that %@AS@% i_array %@AE@% contains two rows of values, each row containing three
  3285. integers. The statement that declares %@AS@% i_array %@AE@% also initializes the array,
  3286. listing the initializing values in curly braces to the right of the equal
  3287. sign:  %@NL@%
  3288. %@NL@%
  3289. %@AS@%  int i_array[2][3] = { { 176, 4069, 303 }, { 6, 5, 77 } };%@AE@%%@NL@%
  3290. %@NL@%
  3291. The braces clearly show that the array contains two groups of three values.
  3292. %@NL@%
  3293. %@NL@%
  3294. Two-dimensional arrays are often pictured in rows and columns, as in Figure
  3295. 4.4. Of course, since computer memory is linear, %@AS@% i_array %@AE@% is actually
  3296. stored with its rows end-for-end, as in Figure 4.5.  %@NL@%
  3297. %@NL@%
  3298. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3299. %@NL@%
  3300. You refer to a multidimensional array element the same way you would a
  3301. onedimensional array element, except that you use one subscript for each
  3302. dimension of the array. For instance, the statement  %@NL@%
  3303. %@NL@%
  3304. %@AS@%  printf( "%d\n", i_array[0][1] );%@AE@%%@NL@%
  3305. %@NL@%
  3306. specifies two subscripts. It prints the value stored in element 0, 1 of %@AS@%
  3307. %@AS@%i_array%@AE@%, which is %@AS@% 4069%@AE@%.  %@NL@%
  3308. %@NL@%
  3309. Figure 4.5 shows how to specify every element of %@AS@% i_array %@AE@% in TWODIM.C.  %@NL@%
  3310. %@NL@%
  3311. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3312. %@NL@%
  3313. %@NL@%
  3314. %@3@%%@CR:C6A00040008 @%%@AB@%Structures%@AE@%%@EH@%%@NL@%
  3315. %@NL@%
  3316. %@AU@% A structure is a group of related data items of different  types under one
  3317. %@AU@%name.%@AE@%  %@NL@%
  3318. %@NL@%
  3319. The second aggregate data type is the structure: a group of related data
  3320. items under one name. While array elements are all the same type, the
  3321. elements of a structure, known as its "members," can be of different types.
  3322. %@NL@%
  3323. %@NL@%
  3324. Structures are equivalent to records in QuickPascal or user-defined types in
  3325. QuickBASIC. As in those languages, the ability to group different types in
  3326. the same construct provides powerful, very flexible data-handling
  3327. capabilities.  %@NL@%
  3328. %@NL@%
  3329. %@NL@%
  3330. %@4@%%@AB@%Creating a Simple Structure%@AE@%%@EH@%%@NL@%
  3331. %@NL@%
  3332. We'll write a simple program to demonstrate the basics of structures.
  3333. Suppose you want to write a payroll program that records these facts about
  3334. an employee:  %@NL@%
  3335. %@NL@%
  3336. %@NL@%
  3337.   ■   Name%@NL@%
  3338. %@NL@%
  3339.   ■   Number of months of service%@NL@%
  3340. %@NL@%
  3341.   ■   Hourly wage%@NL@%
  3342. %@NL@%
  3343. %@NL@%
  3344. Each of these data items requires a different data type. The name can be
  3345. stored in a string (character array), while an integer will do for the
  3346. months of service. The hourly wage may contain a fraction; we'll store it in
  3347. a floating-point variable.  %@NL@%
  3348. %@NL@%
  3349. Although each of these variables has a different type, we can group all of
  3350. them in a single structure. The EMPLOYEE.C program below contains the
  3351. structure.  %@NL@%
  3352. %@NL@%
  3353. %@AS@%  /* EMPLOYEE.C: Demonstrate structures. */
  3354. %@AS@%  
  3355. %@AS@%  #include <stdio.h>
  3356. %@AS@%  #include <string.h>
  3357. %@AS@%  
  3358. %@AS@%  struct employee
  3359. %@AS@%  {
  3360. %@AS@%     char name[10];
  3361. %@AS@%     int months;
  3362. %@AS@%     float wage;
  3363. %@AS@%  };
  3364. %@AS@%  
  3365. %@AS@%  void display( struct employee show );
  3366. %@AS@%  
  3367. %@AS@%  main()
  3368. %@AS@%  {
  3369. %@AS@%     struct employee jones;
  3370. %@AS@%  
  3371. %@AS@%     strcpy( jones.name, "Jones, J" );
  3372. %@AS@%     jones.months = 77;
  3373. %@AS@%     jones.wage = 13.68;
  3374. %@AS@%  
  3375. %@AS@%     display( jones );
  3376. %@AS@%  }
  3377. %@AS@%  
  3378. %@AS@%  void display( struct employee show )
  3379. %@AS@%  {
  3380. %@AS@%     printf( "Name: %s\n", show.name );
  3381. %@AS@%     printf( "Months of service: %d\n", show.months );
  3382. %@AS@%     printf( "Hourly wage: %6.2f\n", show.wage );
  3383. %@AS@%  }%@AE@%%@NL@%
  3384. %@NL@%
  3385. Here is the output of the EMPLOYEE.C program:  %@NL@%
  3386. %@NL@%
  3387. %@AS@%  Name: Jones, J
  3388. %@AS@%  Months of service: 77
  3389. %@AS@%  Hourly wage:  13.68%@AE@%%@NL@%
  3390. %@NL@%
  3391. %@NL@%
  3392. %@4@%%@AB@%Declaring a Structure Type%@AE@%%@EH@%%@NL@%
  3393. %@NL@%
  3394. Since a structure can (and normally does) contain different data types,
  3395. creating it is a little more complicated than making an array or simple
  3396. variable. Before you can create a structure variable, you must declare a
  3397. structure type that tells the compiler how many members the structure
  3398. contains and what types they are.  %@NL@%
  3399. %@NL@%
  3400. A structure-type declaration starts with the keyword %@AB@%struct%@AE@%, which is
  3401. followed by a list of the structure's members enclosed in braces. Between
  3402. the %@AB@%struct%@AE@% and the list of members, you can also specify a "structure tag"─a
  3403. name that other parts of the program can use to refer to the type.  %@NL@%
  3404. %@NL@%
  3405. The structure declaration from EMPLOYEE.C,  %@NL@%
  3406. %@NL@%
  3407. %@AS@%  struct employee
  3408. %@AS@%  {
  3409. %@AS@%     char name[10];
  3410. %@AS@%     int months;
  3411. %@AS@%     float wage;
  3412. %@AS@%  };%@AE@%%@NL@%
  3413. %@NL@%
  3414. %@AU@% A structure declaration makes a template for variables  of the type it
  3415. %@AU@%defines.%@AE@%  %@NL@%
  3416. %@NL@%
  3417. creates a "template" for an %@AS@% employee %@AE@% structure that structure variables of
  3418. this type can use. It's as if you created a brand new data type, tagging it
  3419. %@AS@%employee%@AE@%. Figure 4.6 illustrates the %@AS@% employee %@AE@% structure type.  %@NL@%
  3420. %@NL@%
  3421. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3422. %@NL@%
  3423. %@NL@%
  3424. %@4@%%@AB@%Creating a Structure Variable%@AE@%%@EH@%%@NL@%
  3425. %@NL@%
  3426. Once you have declared a structure, you can create variables of that type
  3427. using the structure tag. Each variable can contain values of the types
  3428. defined in the structure type. In EMPLOYEE.C, the statement  %@NL@%
  3429. %@NL@%
  3430. %@AS@%  struct employee jones;%@AE@%%@NL@%
  3431. %@NL@%
  3432. declares a structure variable of the type %@AS@% employee %@AE@% named %@AS@% jones%@AE@%. The
  3433. %@AB@%struct%@AE@% states that the variable is a structure. The %@AS@% employee %@AE@% tag specifies
  3434. the variable's structure type, and %@AS@% jones %@AE@% is the variable's name.  %@NL@%
  3435. %@NL@%
  3436. You can also declare the variable in the same statement that declares the
  3437. structure type. The following code declares the %@AS@% employee %@AE@% structure type
  3438. and a variable of that type named %@AS@% jones%@AE@%:  %@NL@%
  3439. %@NL@%
  3440. %@AS@%  struct employee
  3441. %@AS@%  {
  3442. %@AS@%     char name[10];
  3443. %@AS@%     int months;
  3444. %@AS@%     float wage;
  3445. %@AS@%  } jones;%@AE@%%@NL@%
  3446. %@NL@%
  3447. The variable name (%@AS@% jones %@AE@%) appears at the end of the declaration.  %@NL@%
  3448. %@NL@%
  3449. %@AU@% Use the member-of operator (.) to specify structure members.%@AE@%  %@NL@%
  3450. %@NL@%
  3451. You specify structure members by name, using the "member-of" operator (%@AB@%.%@AE@%) to
  3452. separate the variable name and the member name. These are the names of the
  3453. members of the %@AS@% jones %@AE@% structure variable in EMPLOYEE.C:  %@NL@%
  3454. %@NL@%
  3455. %@AS@%  jones.name
  3456. %@AS@%  jones.months
  3457. %@AS@%  jones.wage%@AE@%%@NL@%
  3458. %@NL@%
  3459. Like other variables, structure variables should be initialized before use.
  3460. After %@AS@% jones %@AE@% is declared in EMPLOYEE.C, the statements  %@NL@%
  3461. %@NL@%
  3462. %@AS@%  strcpy( jones.name, "Jones, J" );
  3463. %@AS@%  jones.months = 77;
  3464. %@AS@%  jones.wage = 13.68;%@AE@%%@NL@%
  3465. %@NL@%
  3466. initialize the members of the %@AS@% jones %@AE@% variable. The first statement
  3467. initializes the %@AS@% jones.name %@AE@% member by calling the %@AB@%strcpy%@AE@% ("string copy")
  3468. library function; this function is described in Chapter 11, "Input and
  3469. Output."  %@NL@%
  3470. %@NL@%
  3471. Figure 4.7 shows how the %@AS@% jones %@AE@% structure is stored in memory. Again, since
  3472. computer memory is linear, the members of the structure are laid out
  3473. end-to-end.  %@NL@%
  3474. %@NL@%
  3475. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3476. %@NL@%
  3477. You can initialize a structure when you declare it. The following code would
  3478. perform both operations in EMPLOYEE.C:  %@NL@%
  3479. %@NL@%
  3480. %@AS@%  struct employee jones =
  3481. %@AS@%  {
  3482. %@AS@%     "Jones, J",
  3483. %@AS@%     77,
  3484. %@AS@%     13.68
  3485. %@AS@%  };%@AE@%%@NL@%
  3486. %@NL@%
  3487. This code declares the %@AS@% jones %@AE@% structure variable and lists the initializing
  3488. value for each of its members.  %@NL@%
  3489. %@NL@%
  3490. %@NL@%
  3491. %@4@%%@AB@%Using Structure Variables%@AE@%%@EH@%%@NL@%
  3492. %@NL@%
  3493. A structure member can be treated like any other variable of its type. You
  3494. can assign a value to it, change its value, and so on. For instance, the
  3495. statement  %@NL@%
  3496. %@NL@%
  3497. %@AS@%  jones.months = 83;%@AE@%%@NL@%
  3498. %@NL@%
  3499. would change the value of the %@AS@% jones.months %@AE@% member in EMPLOYEE.C.  %@NL@%
  3500. %@NL@%
  3501. %@AU@% Assigning one structure  to another copies  the entire structure.%@AE@%  %@NL@%
  3502. %@NL@%
  3503. You can also assign an entire structure to another structure of the same
  3504. type. This copies the entire contents of the first structure to the second.
  3505. You might do this to save time when creating a new structure whose contents
  3506. differ only slightly from those of an existing structure.  %@NL@%
  3507. %@NL@%
  3508. To illustrate, let's modify the EMPLOYEE.C program. Say you have a second
  3509. employee named Lavik whose wage rate and months of service are the same as
  3510. those of Jones and you want to create a second structure. You could begin by
  3511. declaring a second %@AS@% employee %@AE@% structure variable named %@AS@% lavik %@AE@% in this
  3512. fashion:  %@NL@%
  3513. %@NL@%
  3514. %@AS@%  struct employee lavik = jones;%@AE@%%@NL@%
  3515. %@NL@%
  3516. Now the members of the %@AS@% lavik %@AE@% structure contain the same data as the
  3517. members of the %@AS@% jones %@AE@% structure. The %@AS@% lavik.name %@AE@% member contains the
  3518. string %@AS@% Jones, J%@AE@%, the %@AS@% lavik.months %@AE@% member contains the value 77, and the %@AS@%
  3519. %@AS@%lavik.wage %@AE@% member contains the value 13.68. You could add the statement  %@NL@%
  3520. %@NL@%
  3521. %@AS@%  strcpy( lavik.name, "Lavik, B" );%@AE@%%@NL@%
  3522. %@NL@%
  3523. to place a new string in the %@AS@% lavik.name %@AE@% member.  %@NL@%
  3524. %@NL@%
  3525. %@AU@% Structure variables can be passed as function arguments.%@AE@%  %@NL@%
  3526. %@NL@%
  3527. When you pass a structure name to a function, the function creates a local
  3528. structure variable of that type. Like all local variables, the new variable
  3529. is private to the function that includes it.  %@NL@%
  3530. %@NL@%
  3531. For example, if you add the statements  %@NL@%
  3532. %@NL@%
  3533. %@AS@%  strcpy( show.name, "King, M" );
  3534. %@AS@%  printf( "%s\n", show.name );%@AE@%%@NL@%
  3535. %@NL@%
  3536. to the end of the %@AS@% display %@AE@% function in EMPLOYEE.C, then a new string is
  3537. copied into the %@AS@% show.name %@AE@% member of the function's structure variable. The
  3538. %@AB@%printf%@AE@% statement in the second line prints  %@NL@%
  3539. %@NL@%
  3540. %@AS@%  King, M%@AE@%%@NL@%
  3541. %@NL@%
  3542. Since this structure is local to the %@AS@% display %@AE@% function, the change doesn't
  3543. affect the structure defined in the %@AB@%main%@AE@% function. If you add the statement
  3544. %@NL@%
  3545. %@NL@%
  3546. %@AS@%  printf( "%s\n", jones.name );%@AE@%%@NL@%
  3547. %@NL@%
  3548. to the end of the %@AB@%main%@AE@% function, the program prints  %@NL@%
  3549. %@NL@%
  3550. %@AS@%  Jones, J%@AE@%%@NL@%
  3551. %@NL@%
  3552. The original structure is unchanged.  %@NL@%
  3553. %@NL@%
  3554. While you can pass a structure name to a function as we did above, it's more
  3555. common to pass the function a %@AI@%pointer%@AE@% to the structure. This not only
  3556. permits the function to access a structure defined elsewhere in the program,
  3557. but it conserves memory (since the function doesn't create a local copy of
  3558. the structure). Chapter 9, "Advanced Pointers," explains how to access
  3559. structures using pointers.  %@NL@%
  3560. %@NL@%
  3561. %@NL@%
  3562. %@4@%%@AB@%Arrays of Structures%@AE@%%@EH@%%@NL@%
  3563. %@NL@%
  3564. %@AU@% An array of structures  is a group of structures  of the same type.%@AE@%  %@NL@%
  3565. %@NL@%
  3566. Since it's rare for a company to have a single employee, a more practical
  3567. version of the EMPLOYEE.C program would have an array of structures─one
  3568. structure per employee. The concept may sound intimidating, but this is a
  3569. common use of structures.  %@NL@%
  3570. %@NL@%
  3571. The following statement declares a 50-element array named %@AS@% payroll%@AE@%, with
  3572. each element a structure of the type %@AS@% employee %@AE@%:  %@NL@%
  3573. %@NL@%
  3574. %@AS@%  struct employee payroll[50];%@AE@%%@NL@%
  3575. %@NL@%
  3576. To specify members in such an array, you combine array notation and
  3577. structure notation, giving the array name, a subscript, and a member name.
  3578. For instance, the name  %@NL@%
  3579. %@NL@%
  3580. %@AS@%  payroll[0].months%@AE@%%@NL@%
  3581. %@NL@%
  3582. specifies the %@AS@% months %@AE@% member of the first structure in the %@AS@% payroll %@AE@% array.
  3583. The first part of the name (%@AS@% payroll[0] %@AE@%) contains the array name and
  3584. subscript that identify the structure. The second part (%@AS@% months %@AE@%) identifies
  3585. the member within that structure.  %@NL@%
  3586. %@NL@%
  3587. Figure 4.8 depicts the first three elements of the %@AS@% payroll %@AE@% array.  %@NL@%
  3588. %@NL@%
  3589. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3590. %@NL@%
  3591. Once you grasp the basic idea, it's easy to imagine practical uses for an
  3592. array of structures. Many programs, from an address book to a library card
  3593. catalog, might use a structure to store different types of information about
  3594. an individual item, then store many such structures in an array.  %@NL@%
  3595. %@NL@%
  3596. %@NL@%
  3597. %@4@%%@AB@%Structures of Structures%@AE@%%@EH@%%@NL@%
  3598. %@NL@%
  3599. As noted earlier, a structure can contain members of any data type─including
  3600. other structures. So you can create a structure of structures: a structure
  3601. whose members are structures.  %@NL@%
  3602. %@NL@%
  3603. To illustrate, suppose you write a group of functions that draw various
  3604. kinds of graphic windows and message boxes. You could define a small
  3605. structure something like the following:  %@NL@%
  3606. %@NL@%
  3607. %@AS@%  struct title
  3608. %@AS@%  {
  3609. %@AS@%     char text[70];  /* Title text */
  3610. %@AS@%     int color;      /* Color of title text */
  3611. %@AS@%     short justify;  /* Left, center, or right */
  3612. %@AS@%  };%@AE@%%@NL@%
  3613. %@NL@%
  3614. to aid in drawing titles. The %@AS@% title %@AE@% structure's three members specify the
  3615. title's text, its color, and how its text is justified.  %@NL@%
  3616. %@NL@%
  3617. Once the %@AS@% title %@AE@% structure is defined, you can make it part of other, larger
  3618. structures that use titles. If you define a %@AS@% window %@AE@% structure type to draw
  3619. windows, for example, that structure could include a %@AS@% title %@AE@% along with
  3620. other structure members:  %@NL@%
  3621. %@NL@%
  3622. %@AS@%  struct window
  3623. %@AS@%  {
  3624. %@AS@%     struct title wintitle; /* Window title */
  3625. %@AS@%     /* Other structure members go here... */
  3626. %@AS@%  };%@AE@%%@NL@%
  3627. %@NL@%
  3628. In this structure type, the %@AS@% title %@AE@% member is named %@AS@% wintitle%@AE@%.  %@NL@%
  3629. %@NL@%
  3630. You specify members of such structures using member-of operators and  the
  3631. appropriate names. If you create a variable of the %@AS@% window %@AE@% type named %@AS@%
  3632. %@AS@%mywindow%@AE@%, the name  %@NL@%
  3633. %@NL@%
  3634. %@AS@%  mywindow.wintitle.color%@AE@%%@NL@%
  3635. %@NL@%
  3636. specifies the %@AS@% color %@AE@% member of the %@AS@% wintitle %@AE@% member of the %@AS@% mywindow %@AE@%
  3637. structure.  %@NL@%
  3638. %@NL@%
  3639. If you program using QuickC's Presentation Graphics library, you'll find it
  3640. useful to understand the notation we just explained. Our fictitious %@AS@% title %@AE@%
  3641. structure is a simplified version of the Presentation Graphics %@AB@%titletype%@AE@%
  3642. structure type (see Chapter 14, "Presentation Graphics").  %@NL@%
  3643. %@NL@%
  3644. %@NL@%
  3645. %@4@%%@AB@%Bit Fields%@AE@%%@EH@%%@NL@%
  3646. %@NL@%
  3647. A "bit field" is a specialized structure that provides a way to manipulate
  3648. individual bits or groups of bits. One use for this advanced feature is to
  3649. access hardware addresses such as the computer's video memory.  %@NL@%
  3650. %@NL@%
  3651. %@AU@% The members of a bit-field structure are groups of bits.%@AE@%  %@NL@%
  3652. %@NL@%
  3653. You declare and use a bit-field structure much as you would any other
  3654. structure. The difference is that every one of its members must be a bit or
  3655. group of bits. You can't include other data types in a bit field.  %@NL@%
  3656. %@NL@%
  3657. The following statement declares a bit-field structure type with the tag %@AS@%
  3658. %@AS@%SCREEN%@AE@%:  %@NL@%
  3659. %@NL@%
  3660. %@AS@%  struct SCREEN
  3661. %@AS@%  {
  3662. %@AS@%    unsigned character : 8;
  3663. %@AS@%    unsigned fgcolor   : 3;
  3664. %@AS@%    unsigned intensity : 1;
  3665. %@AS@%    unsigned bgcolor   : 3;
  3666. %@AS@%    unsigned blink     : 1;
  3667. %@AS@%  } screenbuf[25][80];%@AE@%%@NL@%
  3668. %@NL@%
  3669. The colons in the declaration tell QuickC these are bit fields rather than
  3670. normal structure members. The number following each colon tells how many
  3671. bits the field contains. In the %@AS@% SCREEN %@AE@% type the %@AS@% character %@AE@% member has 8
  3672. bits, %@AS@%intensity %@AE@% has 1 bit, and so on. Figure 4.9 illustrates the %@AS@% SCREEN %@AE@%
  3673. type.  %@NL@%
  3674. %@NL@%
  3675. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3676. %@NL@%
  3677. Figure 4.10 illustrates memory allocation for the %@AS@% SCREEN %@AE@% type. The members
  3678. of the %@AS@% SCREEN %@AE@% type mirror the arrangement of bits in screen memory.  %@NL@%
  3679. %@NL@%
  3680. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3681. %@NL@%
  3682. Take another look at the structure declaration. In addition to declaring a
  3683. structure type, the statement declares a two-dimensional array variable
  3684. %@AS@%screenbuf%@AE@%, of the same structure type. You could use this array as an
  3685. alternate video buffer. Many graphics programs use a similar arrangement to
  3686. switch between an alternate video buffer and the computer's video memory.  %@NL@%
  3687. %@NL@%
  3688. The five members of the %@AS@% SCREEN %@AE@% type happen to take up a full %@AB@%int%@AE@% (two
  3689. bytes, on DOS machines). A bit field need not fill up a byte or %@AB@%int%@AE@%; the bit
  3690. field can contain as many bits as you need up to the maximum number of bits
  3691. for the field's  %@NL@%
  3692. %@NL@%
  3693. base type. The base type for each field in the example is %@AB@%unsigned%@AE@% (%@AB@%unsigned
  3694. %@AB@%int%@AE@%), so each field can contain a maximum of 16 bits.  %@NL@%
  3695. %@NL@%
  3696. The members of a bit-field structure are accessed with the structure-member
  3697. operator─like other structure members. For instance, the name  %@NL@%
  3698. %@NL@%
  3699. %@AS@%  screenbuf[13][53].blink = 1;%@AE@%%@NL@%
  3700. %@NL@%
  3701. specifies the %@AS@% blink %@AE@% member of element 13, 53 of the %@AS@% screenbuf %@AE@% array.  %@NL@%
  3702. %@NL@%
  3703. The range of values you can assign to a bit-field member depends on the
  3704. member's size. Since the %@AS@% blink %@AE@% member of the %@AS@% SCREEN %@AE@% type contains one
  3705. bit, %@AS@% blink %@AE@% is limited to the value 0 or 1. The %@AS@% fgcolor %@AE@% member contains
  3706. three bits and can have any value from 0-7.  %@NL@%
  3707. %@NL@%
  3708. %@NL@%
  3709. %@3@%%@CR:C6A00040009 @%%@AB@%Unions%@AE@%%@EH@%%@NL@%
  3710. %@NL@%
  3711. %@AU@% A union is a group of  variables of different types  that share storage
  3712. %@AU@%space.%@AE@%  %@NL@%
  3713. %@NL@%
  3714. A union is a variable that can hold any one of several data types at
  3715. different times, using the same storage space. Unions are a rather advanced
  3716. feature. One use of them is to access DOS registers, which you may sometimes
  3717. need to access as bytes and at other times as words.  %@NL@%
  3718. %@NL@%
  3719. As with a structure, you must start by declaring a union type to tell the
  3720. compiler the number and types of the union's members. You include one of
  3721. each type that you expect to use.  %@NL@%
  3722. %@NL@%
  3723. The following code creates a union that can hold a %@AB@%char%@AE@%, %@AB@%int%@AE@%, or %@AB@%long%@AE@% value.
  3724. It declares a union type with the tag %@AS@% u_sample %@AE@% and declares a variable of
  3725. that type named %@AS@% example%@AE@%.  %@NL@%
  3726. %@NL@%
  3727. %@AS@%  union u_sample
  3728. %@AS@%  {
  3729. %@AS@%    char c_val;
  3730. %@AS@%    int i_val;
  3731. %@AS@%    long l_val;
  3732. %@AS@%  }  example;%@AE@%%@NL@%
  3733. %@NL@%
  3734. When you declare a union, QuickC allocates as much storage as the largest
  3735. data type in the union requires. Since the largest type in %@AS@% u_sample%@AE@%  is
  3736. %@AB@%long%@AE@%, this union contains four bytes.  %@NL@%
  3737. %@NL@%
  3738. The elements of a union are called members and use the same notation as
  3739. structure members. Thus, the members of the %@AS@% example %@AE@% union are named  %@NL@%
  3740. %@NL@%
  3741. %@AS@%  example.c_val
  3742. %@AS@%  example.i_val
  3743. %@AS@%  example.l_val%@AE@%%@NL@%
  3744. %@NL@%
  3745. The contents of a union depend on how you access it. For instance, the
  3746. statement  %@NL@%
  3747. %@NL@%
  3748. %@AS@%  example.c_val = '\0';%@AE@%%@NL@%
  3749. %@NL@%
  3750. stores a %@AB@%char%@AE@% value in the %@AS@% example %@AE@% union. Since a %@AB@%char%@AE@% value takes one
  3751. byte, the statement uses only one byte of the space in %@AS@% example%@AE@%. The
  3752. statement  %@NL@%
  3753. %@NL@%
  3754. %@AS@%  example.i_val = 77;%@AE@%%@NL@%
  3755. %@NL@%
  3756. uses two bytes of the union, because an %@AB@%int%@AE@% value requires two bytes of
  3757. storage. Likewise, the statement  %@NL@%
  3758. %@NL@%
  3759. %@AS@%  example.l_val = 75621;%@AE@%%@NL@%
  3760. %@NL@%
  3761. stores %@AB@%long%@AE@% value in %@AS@% example%@AE@%, taking up all four bytes of its storage
  3762. space. Figure 4.11 shows memory allocation for the three members in the %@AS@%
  3763. %@AS@%example %@AE@% union.  %@NL@%
  3764. %@NL@%
  3765. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  3766. %@NL@%
  3767. It's your job to keep track of what is stored in a union. If you store a
  3768. %@AB@%long%@AE@% value in %@AS@% example %@AE@% and mistakenly treat that value as a %@AB@%char%@AE@% value
  3769. later, the result may be nonsense. It's especially important not to confuse
  3770. integer and floating-point types, which are stored in different internal
  3771. formats.  %@NL@%
  3772. %@NL@%
  3773. Now that you're familiar with the data types that C offers, you are ready to
  3774. tackle more advanced data-handling concepts. The next chapter discusses
  3775. several of these.  %@NL@%
  3776. %@NL@%
  3777. %@NL@%
  3778. %@NL@%
  3779. %@NL@%
  3780. %@NL@%
  3781. %@NL@%
  3782. %@CR:C6A00050001 @%%@1@%%@AB@%Chapter 5  Advanced Data Types%@AE@%%@EH@%%@NL@%
  3783. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  3784. %@NL@%
  3785. In Chapter 4, "Data Types," we described the basic C data types and showed
  3786. how to declare and use different kinds of variables. This chapter examines
  3787. more advanced data topics, including the visibility and lifetime of
  3788. variables and the conversion of values from one data type to another.  %@NL@%
  3789. %@NL@%
  3790. If you know QuickPascal or QuickBASIC, some of these topics, such as
  3791. visibility, should be familiar. For example, a variable declared within a
  3792. function is visible (accessible) only in that function. One area in which C
  3793. differs notably from QuickPascal is type conversion. The C language gives
  3794. programmers the freedom to convert a value from one type to another type,
  3795. whereas QuickPascal does not.  %@NL@%
  3796. %@NL@%
  3797. %@NL@%
  3798. %@2@%%@CR:C6A00050002 @%%@AB@%Visibility%@AE@%%@EH@%%@NL@%
  3799. %@NL@%
  3800. Every variable in a C program has a definite "visibility" that determines
  3801. which parts of the program can "see," or access, the variable. Another term
  3802. for visibility is "scope."  %@NL@%
  3803. %@NL@%
  3804. As we mentioned in Chapter 1, "Anatomy of a C Program," there are two basic
  3805. kinds of visibility: local and external. A "local" variable─one declared
  3806. within a function─is visible only within that function. An "external"
  3807. variable─one declared outside all functions─is visible to all functions that
  3808. follow it in the program.  %@NL@%
  3809. %@NL@%
  3810. This section begins by describing local and external visibility, then goes
  3811. on to discuss visibility in multiple-file programs and the visibility of
  3812. functions.  %@NL@%
  3813. %@NL@%
  3814. ────────────────────────────────────────────────────────────────────────────%@NL@%
  3815. NOTE
  3816.  
  3817. %@AI@%While the examples in this section use simple %@AB@%int%@AE@%%@AI@% variables, visibility
  3818. %@AI@%rules apply equally to aggregate types such as arrays and structures.%@AE@%%@AE@%%@NL@%
  3819. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  3820. %@NL@%
  3821. %@AU@% Use external variables  only when necessary.%@AE@%  %@NL@%
  3822. %@NL@%
  3823. C programmers normally limit the visibility of each variable to those parts
  3824. of the program that need to access the variable. For instance, if a variable
  3825. is needed only within one function, it should always be local to that
  3826. function. By restricting a variable's visibility, you can prevent other
  3827. parts of the program from accidentally changing the variable's value. Such
  3828. haphazard side effects were common in older interpreted BASIC programs, in
  3829. which every variable had unlimited  visibility.  %@NL@%
  3830. %@NL@%
  3831. %@NL@%
  3832. %@3@%%@CR:C6A00050003 @%%@AB@%Local Variables%@AE@%%@EH@%%@NL@%
  3833. %@NL@%
  3834. %@AU@% Variables declared  within a function are  local to that function.%@AE@%  %@NL@%
  3835. %@NL@%
  3836. As we noted in Chapter 1, "Anatomy of a C Program," and Chapter 2,
  3837. "Functions," the place where you declare a variable controls where the
  3838. variable is visible. Declaring a variable within a function makes the
  3839. variable local to that function; the variable can be seen only within the
  3840. function.  %@NL@%
  3841. %@NL@%
  3842. The VISIBLE.C program below demonstrates local visibility. It contains a
  3843. function named %@AS@% be_bop %@AE@% that tries to print the value of the local variable %@AS@%
  3844. %@AS@%val%@AE@%.  %@NL@%
  3845. %@NL@%
  3846. %@AS@%  /* VISIBLE.C: Demonstrate local visibility. */
  3847. %@AS@%  
  3848. %@AS@%  #include <stdio.h>
  3849. %@AS@%  void be_bop( void );
  3850. %@AS@%  
  3851. %@AS@%  main()
  3852. %@AS@%  {
  3853. %@AS@%     int val = 10;
  3854. %@AS@%     be_bop();
  3855. %@AS@%  }
  3856. %@AS@%  
  3857. %@AS@%  void be_bop( void )
  3858. %@AS@%  {
  3859. %@AS@%     printf( "val = %d", val ); /* Error! */
  3860. %@AS@%  }%@AE@%%@NL@%
  3861. %@NL@%
  3862. Notice where the %@AS@% val %@AE@% variable is declared. The declaration  %@NL@%
  3863. %@NL@%
  3864. %@AS@%  int val = 10;%@AE@%%@NL@%
  3865. %@NL@%
  3866. occurs within the %@AB@%main%@AE@% function, so %@AS@% val %@AE@% is local to %@AB@%main%@AE@%. When you compile
  3867. VISIBLE.C, QuickC stops, providing this error message:  %@NL@%
  3868. %@NL@%
  3869. %@AS@%  C2065: 'val' : undefined%@AE@%%@NL@%
  3870. %@NL@%
  3871. What happened? The %@AB@%printf%@AE@% statement in the %@AS@% be_bop %@AE@% function  %@NL@%
  3872. %@NL@%
  3873. %@AS@%  printf( "val = %d", val ); /* Error! */%@AE@%%@NL@%
  3874. %@NL@%
  3875. can't "see" the variable %@AS@% val%@AE@%, which is declared locally within %@AB@%main%@AE@%.
  3876. Outside the %@AB@%main%@AE@% function, in which %@AS@% val %@AE@% is declared, the variable doesn't
  3877. exist.  %@NL@%
  3878. %@NL@%
  3879. You could eliminate the error message by declaring %@AS@% val %@AE@% externally, but
  3880. most programmers would avoid that solution. If a variable has external
  3881. visibility, any part of the program might change its value accidentally. A
  3882. better solution is to pass the value of %@AS@% val %@AE@% to %@AS@% be_bop %@AE@% as a function
  3883. argument, as shown in the program VISIBLE1.C below.  %@NL@%
  3884. %@NL@%
  3885. %@AS@%  /* VISIBLE1.C: Demonstrate local visibility. */
  3886. %@AS@%  
  3887. %@AS@%  #include <stdio.h>
  3888. %@AS@%  
  3889. %@AS@%  void be_bop( int param );
  3890. %@AS@%  
  3891. %@AS@%  main()
  3892. %@AS@%  {
  3893. %@AS@%     int val = 10;
  3894. %@AS@%     be_bop( val );
  3895. %@AS@%  }
  3896. %@AS@%  
  3897. %@AS@%  void be_bop( int param )
  3898. %@AS@%  {
  3899. %@AS@%     printf( "%d\n", param );
  3900. %@AS@%  }%@AE@%%@NL@%
  3901. %@NL@%
  3902. The VISIBLE1.C program is identical to VISIBLE.C except for two changes. The
  3903. %@AS@% be_bop %@AE@% function now can accept an argument, and the statement that calls
  3904. %@AS@%be_bop %@AE@% passes the value of %@AS@% val %@AE@% as an argument. These changes allow the %@AS@%
  3905. %@AS@%be_bop %@AE@% function to print the value of %@AS@% val %@AE@% without the drawback of making %@AS@%
  3906. %@AS@%val %@AE@% external.  %@NL@%
  3907. %@NL@%
  3908. Most local variables are declared at the beginning of the function and are
  3909. visible throughout the function. If you declare the variable later in the
  3910. function, it is visible only to statements that follow the declaration.  %@NL@%
  3911. %@NL@%
  3912. The reason for this rule is simple: QuickC, like all language compilers,
  3913. reads your program line by line, from beginning to end. Until the compiler
  3914. sees the variable's declaration, it must treat the variable as undefined.
  3915. This rule applies to all variables, including external variables, as we'll
  3916. see in the next section.  %@NL@%
  3917. %@NL@%
  3918. Although the practice isn't common, you can restrict a local variable's
  3919. visibility even further by declaring it in a statement block inside a
  3920. function. For instance, you might declare a variable within the body of the
  3921. loop or conditional statement. In fact, any pair of curly braces limits the
  3922. visibility of a variable declared within that pair.  %@NL@%
  3923. %@NL@%
  3924. %@NL@%
  3925. %@3@%%@CR:C6A00050004 @%%@AB@%External Variables%@AE@%%@EH@%%@NL@%
  3926. %@NL@%
  3927. If you declare a variable outside all functions, the variable has external
  3928. visibility; every function that follows the declaration can see the
  3929. variable. External variables are called "global" in some other languages.  %@NL@%
  3930. %@NL@%
  3931. Experienced C programmers use external variables only when necessary─for
  3932. instance, when two or more functions need the ability to change the same
  3933. variable or communicate with each other by changing a variable. Even in
  3934. those cases, however, you may be able to avoid the dangers of external
  3935. visibility by passing a pointer to the variable as a function argument. See
  3936. the section "Passing Pointers to Functions" in Chapter 8 ("Pointers") for
  3937. more information.  %@NL@%
  3938. %@NL@%
  3939. Most external variables are declared near the beginning of the program,
  3940. before any function definitions. In this way, you can make the variable
  3941. visible to every function in the program. You could do this in VISIBLE1.C by
  3942. placing the declaration of %@AS@% val%@AE@%,  %@NL@%
  3943. %@NL@%
  3944. %@AS@%  int val = 10;%@AE@%%@NL@%
  3945. %@NL@%
  3946. immediately before the %@AB@%main%@AE@% function.  %@NL@%
  3947. %@NL@%
  3948. If you declare the variable %@AS@% val %@AE@% later in the program, it is not visible to
  3949. functions that precede the declaration. The VISIBLE2.C program below
  3950. demonstrates this principle.  %@NL@%
  3951. %@NL@%
  3952. %@AS@%  /* VISIBLE2.C: Demonstrate external visibility. */
  3953. %@AS@%  
  3954. %@AS@%  #include <stdio.h>
  3955. %@AS@%  
  3956. %@AS@%  void be_bop( int param );
  3957. %@AS@%  
  3958. %@AS@%  main()
  3959. %@AS@%  {
  3960. %@AS@%     be_bop( val ); /* Error! */
  3961. %@AS@%  }
  3962. %@AS@%  
  3963. %@AS@%  int val = 10;
  3964. %@AS@%  
  3965. %@AS@%  void be_bop( int param )
  3966. %@AS@%  {
  3967. %@AS@%     printf( "val = %d\n", param );
  3968. %@AS@%  }%@AE@%%@NL@%
  3969. %@NL@%
  3970. The VISIBLE2.C program is identical to VISIBLE1.C except that %@AS@% val %@AE@% is
  3971. declared externally  %@NL@%
  3972. %@NL@%
  3973. %@AS@%  int val = 10;%@AE@%%@NL@%
  3974. %@NL@%
  3975. following the %@AB@%main%@AE@% function, rather than locally within %@AB@%main%@AE@%.  %@NL@%
  3976. %@NL@%
  3977. Because the declaration occurs outside all functions, the variable is
  3978. external. However, because the declaration follows the %@AB@%main%@AE@% function, the
  3979. variable is not visible within %@AB@%main%@AE@%. When the %@AB@%printf%@AE@% statement in the %@AB@%main%@AE@%
  3980. function refers to %@AS@% val%@AE@%, QuickC issues the error message:  %@NL@%
  3981. %@NL@%
  3982. %@AS@%  C2065: 'val' : undefined%@AE@%%@NL@%
  3983. %@NL@%
  3984. Remember, QuickC reads the program line by line, from start to finish. Since
  3985. the compiler knows nothing about %@AS@% val %@AE@% when it reaches the reference in
  3986. %@AB@%main%@AE@%, it must treat %@AS@% val %@AE@% as undefined. In this program, only the %@AS@% be_bop %@AE@%
  3987. function can refer to %@AS@% val%@AE@%.  %@NL@%
  3988. %@NL@%
  3989. %@NL@%
  3990. %@3@%%@CR:C6A00050005 @%%@AB@%Visibility in Multiple Source Files%@AE@%%@EH@%%@NL@%
  3991. %@NL@%
  3992. A "source file" is the file containing your program's text. Source files
  3993. normally have the .C file extension, to distinguish them from other files
  3994. such as executable (.EXE) files.  %@NL@%
  3995. %@NL@%
  3996. Simple programs have only one source file, but large programs are often
  3997. split into several source files. If you write a word-processing program, for
  3998. instance, you might place all the program's screen-output functions in one
  3999. file, all the file-handling functions in a second file, and so forth.  %@NL@%
  4000. %@NL@%
  4001. %@AU@% Use the extern keyword to make an external variable visible in more than
  4002. %@AU@%one source file.%@AE@%  %@NL@%
  4003. %@NL@%
  4004. Normally, an external variable is visible only in the source file in which
  4005. it is declared. In a multi-file program, however, a function in one file
  4006. might need to access a variable in a second file. To make the variable
  4007. visible in more than one source file, you must declare it with the %@AB@%extern%@AE@%
  4008. keyword.  %@NL@%
  4009. %@NL@%
  4010. Let's look at a short two-file program that shows how to use %@AB@%extern%@AE@%. The
  4011. first source file, FILE1.C, declares two external variables, %@AS@% chico %@AE@% and %@AS@%
  4012. %@AS@%harpo%@AE@%. The file contains one function (%@AB@%main%@AE@%) that calls a second function
  4013. named %@AS@% yonder%@AE@%.  %@NL@%
  4014. %@NL@%
  4015. %@AS@%  /* FILE1.C: Visibility in multiple source files. */
  4016. %@AS@%  
  4017. %@AS@%  int chico = 20, harpo = 30;
  4018. %@AS@%  extern void yonder( void );
  4019. %@AS@%  
  4020. %@AS@%  main()
  4021. %@AS@%  {
  4022. %@AS@%     yonder();
  4023. %@AS@%  }%@AE@%%@NL@%
  4024. %@NL@%
  4025. The second source file, FILE2.C, contains the %@AS@% yonder %@AE@% function that is
  4026. called in FILE1.C. This file also declares the variables %@AS@% chico %@AE@% and %@AS@% harpo,
  4027. %@AS@%%@AE@% but it prefaces their declarations with %@AB@%extern%@AE@% to show that the variables
  4028. are defined externally in some other file. Once this is done, any function
  4029. in FILE2.C can refer to %@AS@% chico %@AE@% and %@AS@% harpo %@AE@% as if they are defined in the
  4030. same file.  %@NL@%
  4031. %@NL@%
  4032. %@AS@%  /* FILE2.C: Visibility in multiple source files. */
  4033. %@AS@%  
  4034. %@AS@%  #include <stdio.h>
  4035. %@AS@%  
  4036. %@AS@%  void yonder( void )
  4037. %@AS@%  {
  4038. %@AS@%     extern int chico, harpo;
  4039. %@AS@%     printf( "chico = %d, harpo = %d\n", chico, harpo );
  4040. %@AS@%  }%@AE@%%@NL@%
  4041. %@NL@%
  4042. You can compile this program in one of two ways. In the QuickC environment,
  4043. choose Set Program List from the Make menu and add FILE1.C and FILE2.C to
  4044. the list. Then choose Build Program from the Make menu.  %@NL@%
  4045. %@NL@%
  4046. You can also enter this command from the DOS command line:  %@NL@%
  4047. %@NL@%
  4048. %@AS@%  qcl FILE1.C FILE2.C%@AE@%%@NL@%
  4049. %@NL@%
  4050. In either case, the executable file is named FILE1.EXE. The program's
  4051. output,  %@NL@%
  4052. %@NL@%
  4053. %@AS@%  chico = 20, harpo = 30%@AE@%%@NL@%
  4054. %@NL@%
  4055. shows that the %@AS@% yonder %@AE@% function in FILE2.C can access the variables defined
  4056. in FILE1.C.  %@NL@%
  4057. %@NL@%
  4058. Sometimes you may want an external variable to be visible only in the source
  4059. file where it's declared. The variable can be shared by functions in one
  4060. file, but it is hidden to all other files, thus minimizing the risk of
  4061. naming conflicts.  %@NL@%
  4062. %@NL@%
  4063. %@AU@% The static keyword  can limit a variable's visibility to one source file.%@AE@%  %@NL@%
  4064. %@NL@%
  4065. To limit a variable's visibility to one file, precede the variable's
  4066. declaration with the keyword %@AB@%static%@AE@%. For example, if FILE1.C declared the %@AS@%
  4067. %@AS@%harpo %@AE@% variable as %@AB@%static%@AE@% in this manner,  %@NL@%
  4068. %@NL@%
  4069. %@AS@%  static int harpo;%@AE@%%@NL@%
  4070. %@NL@%
  4071. it would prevent FILE2.C from accessing %@AS@% harpo %@AE@% at all, even though FILE2.C
  4072. declares (with %@AB@%extern%@AE@%) that %@AS@% harpo %@AE@% is defined somewhere else.  %@NL@%
  4073. %@NL@%
  4074. %@NL@%
  4075. %@3@%%@CR:C6A00050006 @%%@AB@%Visibility of Functions%@AE@%%@EH@%%@NL@%
  4076. %@NL@%
  4077. %@AU@% Functions are normally visible in multiple source files.%@AE@%  %@NL@%
  4078. %@NL@%
  4079. Unlike variables, functions are external by default. That is, they are
  4080. normally visible to every file in a multi-file program. You'll notice that
  4081. in FILE1.C we declared the %@AS@% yonder %@AE@% function with the %@AB@%extern%@AE@% keyword. We did
  4082. this merely to improve readability; the keyword shows clearly that the
  4083. function is defined in some other file. If we removed the %@AB@%extern%@AE@% from the
  4084. declaration of %@AS@% yonder %@AE@% in FILE1.C, the program would work just as well as
  4085. before.  %@NL@%
  4086. %@NL@%
  4087. At times you may want to restrict the visibility of a function in a
  4088. multi-file program, making it visible in some files but not in others. By
  4089. "hiding" a function from other parts of a program, you can reduce the danger
  4090. of naming conflicts. For instance, if you write a library of functions to
  4091. sell commercially, you probably would hide all of the library's local
  4092. function names, to prevent conflicts with function names your customers
  4093. might create.  %@NL@%
  4094. %@NL@%
  4095. %@AU@% The static keyword can  limit a function's visibility.%@AE@%  %@NL@%
  4096. %@NL@%
  4097. As with external variables, you limit a function's visibility using the
  4098. %@AB@%static%@AE@% keyword. A function declared as %@AB@%static%@AE@% is visible only in the source
  4099. file that declares it. If we add %@AB@%static%@AE@% to the header of the %@AS@% yonder %@AE@%
  4100. function, for example,  %@NL@%
  4101. %@NL@%
  4102. %@AS@%  static void yonder( void )%@AE@%%@NL@%
  4103. %@NL@%
  4104. the function could no longer be called from the FILE1.C file.  %@NL@%
  4105. %@NL@%
  4106. %@NL@%
  4107. %@2@%%@CR:C6A00050007 @%%@AB@%Lifetime%@AE@%%@EH@%%@NL@%
  4108. %@NL@%
  4109. In addition to visibility, every variable also has a certain "lifetime"─that
  4110. is, the period during the program's execution when the variable exists.  %@NL@%
  4111. %@NL@%
  4112. External variables exist for the life of the program. Memory is allocated
  4113. for them when the program begins and remains until the program ends.  %@NL@%
  4114. %@NL@%
  4115. %@AU@% An automatic variable  disappears when the function ends.%@AE@%  %@NL@%
  4116. %@NL@%
  4117. Local variables have shorter lifetimes. They come into being when the
  4118. function begins and disappear when the function ends. For this reason, a
  4119. local variable is said to be "automatic." The variable comes and goes
  4120. automatically, each time the function is called.  %@NL@%
  4121. %@NL@%
  4122. Automatic variables conserve memory in a couple of ways. First, since they
  4123. evaporate when the function ends, automatic variables don't consume memory
  4124. when not in use. Second, they are stored in the "stack" memory area, which
  4125. the program allocates at run time. So, automatic variables don't enlarge the
  4126. executable program.  %@NL@%
  4127. %@NL@%
  4128. The C language provides the %@AB@%auto%@AE@% keyword for declaring automatic variables.
  4129. However, this keyword is seldom used, since all local variables are
  4130. automatic unless you specify otherwise. In the following function, both %@AS@% val
  4131. %@AS@%%@AE@% and %@AS@% example %@AE@% are automatic variables:  %@NL@%
  4132. %@NL@%
  4133. %@AS@%  void sample( void )
  4134. %@AS@%  {
  4135. %@AS@%     int val;
  4136. %@AS@%     auto int example;
  4137. %@AS@%  }%@AE@%%@NL@%
  4138. %@NL@%
  4139. The %@AB@%auto%@AE@% preceding the declaration of %@AS@% example %@AE@% has no practical effect. The
  4140. variable %@AS@% example %@AE@% is automatic even if you remove the %@AB@%auto%@AE@% from its
  4141. declaration.  %@NL@%
  4142. %@NL@%
  4143. %@NL@%
  4144. %@3@%%@CR:C6A00050008 @%%@AB@%Extending the Lives of Local Variables%@AE@%%@EH@%%@NL@%
  4145. %@NL@%
  4146. Occasionally, you may want a local variable to retain its value between
  4147. function calls. The %@AB@%static%@AE@% keyword, introduced earlier as a means of
  4148. limiting the visibility of external variables, also performs this task.  %@NL@%
  4149. %@NL@%
  4150. %@AU@% A static local variable  retains its value through  subsequent function
  4151. %@AU@%calls.%@AE@%  %@NL@%
  4152. %@NL@%
  4153. If you precede a local variable declaration with %@AB@%static%@AE@%, the variable exists
  4154. for the life of the program─the same lifetime as an external variable. The
  4155. variable still has local visibility, however.  %@NL@%
  4156. %@NL@%
  4157. The STATIC.C program below shows how to create and use a %@AB@%static%@AE@% local
  4158. variable. In STATIC.C, the value of the %@AS@% methuselah %@AE@% variable persists
  4159. through all calls to the %@AS@% add_val %@AE@% function, which adds values to %@AS@%
  4160. %@AS@%methuselah %@AE@% and prints the variable's value.  %@NL@%
  4161. %@NL@%
  4162. %@AS@%  /* STATIC.C: Demonstrate static variables. */
  4163. %@AS@%  
  4164. %@AS@%  #include <stdio.h>
  4165. %@AS@%  
  4166. %@AS@%  void add_val( int value );
  4167. %@AS@%  
  4168. %@AS@%  main()
  4169. %@AS@%  {
  4170. %@AS@%     add_val( 1 );
  4171. %@AS@%     add_val( 5 );
  4172. %@AS@%     add_val( 20 );
  4173. %@AS@%  }%@AE@%%@NL@%
  4174. %@NL@%
  4175. %@AS@%  void add_val( int value )
  4176. %@AS@%  {
  4177. %@AS@%     static int methuselah;
  4178. %@AS@%     if( value == 1 )
  4179. %@AS@%        methuselah = 0;
  4180. %@AS@%     methuselah = methuselah + value;
  4181. %@AS@%     printf( "methuselah = %d\n", methuselah );
  4182. %@AS@%  }%@AE@%%@NL@%
  4183. %@NL@%
  4184. The %@AS@% add_val %@AE@% function in STATIC.C accepts one parameter and also declares a
  4185. %@AB@%static%@AE@% local variable named %@AS@% methuselah%@AE@%. Each time %@AS@% add_val %@AE@% is called, it
  4186. adds the passed value to %@AS@% methuselah%@AE@%.  %@NL@%
  4187. %@NL@%
  4188. The %@AB@%main%@AE@% function calls the %@AS@% add_val %@AE@% function three times, passing the
  4189. values 1, 5, and 20 to %@AS@% add_val %@AE@% as arguments. The program's output  %@NL@%
  4190. %@NL@%
  4191. %@AS@%  methuselah = 1
  4192. %@AS@%  methuselah = 6
  4193. %@AS@%  methuselah = 26%@AE@%%@NL@%
  4194. %@NL@%
  4195. shows that the value of %@AS@% methuselah %@AE@% persists through all three function
  4196. calls.  %@NL@%
  4197. %@NL@%
  4198. If we remove the %@AB@%static%@AE@% keyword from the declaration of %@AS@% methuselah%@AE@%, the
  4199. variable's value is not preserved between function calls. The value of %@AS@%
  4200. %@AS@%methuselah %@AE@% is unpredictable the second and third times that %@AS@% add_val %@AE@% is
  4201. called.  %@NL@%
  4202. %@NL@%
  4203. Notice that extending a local variable's lifetime with %@AB@%static%@AE@% doesn't affect
  4204. its visibility. The %@AS@% methuselah %@AE@% variable keeps its value between function
  4205. calls, but you can't refer to the variable outside the %@AS@% add_val %@AE@% function.  %@NL@%
  4206. %@NL@%
  4207. %@NL@%
  4208. %@2@%%@CR:C6A00050009 @%%@AB@%Converting Data Types%@AE@%%@EH@%%@NL@%
  4209. %@NL@%
  4210. It's usually best to avoid mixing data items of different types in the same
  4211. expression. You wouldn't normally add a character variable to a
  4212. floating-point variable, for instance. Some languages, such as QuickPascal,
  4213. generally treat type mixing as an error. However, the C language gives you
  4214. the freedom to mix data types when necessary.  %@NL@%
  4215. %@NL@%
  4216. For example, since the %@AB@%char%@AE@% and %@AB@%int%@AE@% types both can store whole numbers,
  4217. there may be times when you have a good reason to add a %@AB@%char%@AE@% value to an %@AB@%int%@AE@%
  4218. value. When you mix types, QuickC does not issue an error message. Instead,
  4219. the compiler converts both data items to the same type and then performs the
  4220. requested operation.  %@NL@%
  4221. %@NL@%
  4222. Type conversion can occur in one of two ways. The first way occurs
  4223. automatically when you combine different types in an expression. You can
  4224. also use special syntax to intentionally "cast" (convert) one type to
  4225. another. We'll discuss both methods in the following sections.  %@NL@%
  4226. %@NL@%
  4227. Knowing how C converts types will help you to find bugs that result from
  4228. unintended type clashes and to minimize errors when you deliberately mix
  4229. types.  %@NL@%
  4230. %@NL@%
  4231. %@NL@%
  4232. %@3@%%@CR:C6A00050010 @%%@AB@%Ranking of Data Types%@AE@%%@EH@%%@NL@%
  4233. %@NL@%
  4234. For purposes of conversion, the C language ranks data types in the order
  4235. shown in Figure 5.1.  %@NL@%
  4236. %@NL@%
  4237. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  4238. %@NL@%
  4239. The ranking illustrated in Figure 5.1 generally reflects the amount of
  4240. storage that each type requires. As you may remember from Chapter 4, "Data
  4241. Types," larger data types require more storage than smaller types. Thus, an
  4242. %@AB@%int%@AE@%, which requires two bytes of storage, outranks a %@AB@%char%@AE@%, which requires
  4243. one byte.  %@NL@%
  4244. %@NL@%
  4245. Within this ranking, an %@AB@%unsigned%@AE@% type outranks the corresponding %@AB@%signed%@AE@%
  4246. type. An %@AB@%unsigned char%@AE@% value is of higher rank than a %@AB@%signed char%@AE@%, and so
  4247. forth.  %@NL@%
  4248. %@NL@%
  4249. %@NL@%
  4250. %@3@%%@CR:C6A00050011 @%%@AB@%Promotions and Demotions%@AE@%%@EH@%%@NL@%
  4251. %@NL@%
  4252. %@AU@% A promotion is usually harmless.%@AE@%  %@NL@%
  4253. %@NL@%
  4254. A type conversion always involves two data items of different types.
  4255. Whenever possible, QuickC converts the lower-ranking (smaller) data item to
  4256. the higher-ranking (larger) type. This kind of conversion, called a
  4257. "promotion," is normally harmless. For example, since a two-byte %@AB@%int%@AE@% has
  4258. more than enough room to store a one-byte %@AB@%char%@AE@%, it's generally safe to
  4259. promote a %@AB@%char%@AE@% value to an %@AB@%int%@AE@%.  %@NL@%
  4260. %@NL@%
  4261. %@AU@% A demotion usually causes a loss of data.%@AE@%  %@NL@%
  4262. %@NL@%
  4263. Sometimes, the compiler is forced to convert a higher-ranking value to a
  4264. lower-ranking type. This kind of conversion, called a "demotion," usually
  4265. causes loss of data. For example, the %@AB@%int%@AE@% value 32,000 is much too large to
  4266. be stored in a %@AB@%char%@AE@% type, which can't hold a number larger than 255. If you
  4267. assign the value 32,000 to a %@AB@%char%@AE@% variable, some data must be lost.  %@NL@%
  4268. %@NL@%
  4269. A demotion of an integer type truncates the higher-ranking type, throwing
  4270. away the data from high-order bytes that can't fit in the smaller-ranking
  4271. value. Some demotions of floating-point types round off a value rather than
  4272. truncate it.  %@NL@%
  4273. %@NL@%
  4274. %@NL@%
  4275. %@3@%%@CR:C6A00050012 @%%@AB@%Automatic Type Conversions%@AE@%%@EH@%%@NL@%
  4276. %@NL@%
  4277. %@AU@% C does an automatic  type conversion when you  mix different data types.%@AE@%  %@NL@%
  4278. %@NL@%
  4279. When a program statement mixes two different data types, QuickC performs an
  4280. automatic type conversion. The following code, for instance, adds the %@AB@%char%@AE@%
  4281. variable %@AS@% a %@AE@% to the %@AB@%int%@AE@% variable %@AS@% b%@AE@%.  %@NL@%
  4282. %@NL@%
  4283. %@AS@%  char a = 5;
  4284. %@AS@%  int b = 32000;
  4285. %@AS@%  b = a + b;%@AE@%%@NL@%
  4286. %@NL@%
  4287. In the statement  %@NL@%
  4288. %@NL@%
  4289. %@AS@%  b = a + b;%@AE@%%@NL@%
  4290. %@NL@%
  4291. the addition operation to the right of the equal sign triggers an automatic
  4292. type conversion. QuickC promotes the %@AB@%char%@AE@% value to an %@AB@%int%@AE@% and then adds the
  4293. two %@AB@%int%@AE@% values.  %@NL@%
  4294. %@NL@%
  4295. If you're not sure whether QuickC is doing an automatic type conversion, set
  4296. Warning Level 2 or higher in the Compiler Flags dialog box. The compiler
  4297. generates the warning message  %@NL@%
  4298. %@NL@%
  4299. %@AS@%  C4051: data conversion%@AE@%%@NL@%
  4300. %@NL@%
  4301. whenever an automatic conversion occurs. This monitoring helps you readily
  4302. identify unwanted conversions.  %@NL@%
  4303. %@NL@%
  4304. If you carelessly mix different types, you may create subtle errors. The
  4305. CONVERT.C program below has a deliberate error that shows what can happen
  4306. when types are mixed. It adds four variables and assigns their sum to a
  4307. fifth variable, causing three promotions and one demotion.  %@NL@%
  4308. %@NL@%
  4309. %@AS@%  /* CONVERT.C: Demonstrate type conversions. */
  4310. %@AS@%  
  4311. %@AS@%  #include <stdio.h>
  4312. %@AS@%  
  4313. %@AS@%  main()
  4314. %@AS@%  {
  4315. %@AS@%     char c_val = 10;
  4316. %@AS@%     int i_val = 20;
  4317. %@AS@%     long l_val = 64000;
  4318. %@AS@%     float f_val = 3.1;
  4319. %@AS@%     int result;
  4320. %@AS@%  
  4321. %@AS@%     result = c_val + i_val + l_val + f_val;  /* Error! */
  4322. %@AS@%  
  4323. %@AS@%     printf( "%d\n", result );
  4324. %@AS@%  }%@AE@%%@NL@%
  4325. %@NL@%
  4326. The CONVERT.C program adds the numbers 10, 20, 64000, and 3.1. Instead of
  4327. the correct result, 64033.10, the program prints  %@NL@%
  4328. %@NL@%
  4329. %@AS@%  -1503%@AE@%%@NL@%
  4330. %@NL@%
  4331. Something definitely went wrong. The problem lies somewhere in the line  %@NL@%
  4332. %@NL@%
  4333. %@AS@%  result = c_val + i_val + l_val + f_val;%@AE@%%@NL@%
  4334. %@NL@%
  4335. which triggers four automatic type conversions. We'll examine the
  4336. conversions in order.  %@NL@%
  4337. %@NL@%
  4338. The first conversion occurs when the %@AB@%char%@AE@% variable %@AS@% c_val %@AE@% is added to the
  4339. %@AB@%int%@AE@% variable %@AS@% i_val%@AE@%:  %@NL@%
  4340. %@NL@%
  4341. %@AS@%  c_val + i_val%@AE@%%@NL@%
  4342. %@NL@%
  4343. Since the variables are different types, QuickC automatically converts the
  4344. lower-ranking %@AB@%char%@AE@% value to the higher-ranking %@AB@%int%@AE@% type before adding them.
  4345. This promotion doesn't create any problems, since there's more than enough
  4346. room to store the one-byte %@AB@%char%@AE@% value in the two-byte %@AB@%int%@AE@%. The sum of this
  4347. addition is 30, another %@AB@%int%@AE@% value.  %@NL@%
  4348. %@NL@%
  4349. The next operation adds that partial sum to the %@AB@%long%@AE@% value of %@AS@% l_val %@AE@% (to
  4350. make the expression easier to read, we'll show the sum from the previous
  4351. addition):  %@NL@%
  4352. %@NL@%
  4353. %@AS@%  30 + l_val%@AE@%%@NL@%
  4354. %@NL@%
  4355. This addition triggers another promotion. The compiler promotes the %@AB@%int%@AE@%
  4356. result of the first addition to a %@AB@%long%@AE@% value before adding it to %@AS@% l_val%@AE@%,
  4357. which is %@AB@%long%@AE@%. Since the four-byte %@AB@%long%@AE@% type has more than enough room to
  4358. store a two-byte %@AB@%int%@AE@%, this promotion is also harmless.  %@NL@%
  4359. %@NL@%
  4360. Now the partial sum equals 64030. The last addition from CONVERT.C  %@NL@%
  4361. %@NL@%
  4362. %@AS@%  64030 + f_val%@AE@%%@NL@%
  4363. %@NL@%
  4364. triggers another harmless conversion: the compiler converts the %@AB@%long%@AE@% result
  4365. of the previous addition to a %@AB@%float%@AE@% value before adding it to %@AS@% f_val%@AE@%. Even
  4366. though floating-point and integer values are stored in different internal
  4367. formats, no data is lost when the %@AB@%long%@AE@% is converted to a %@AB@%float%@AE@%.  %@NL@%
  4368. %@NL@%
  4369. The result of these additions and conversions is the %@AB@%float%@AE@% value 64033.10,
  4370. which is correct. So where does the mistake occur?  %@NL@%
  4371. %@NL@%
  4372. The problem arises when CONVERT.C assigns the final sum to the wrong type of
  4373. variable. You'll recall that the line containing these operations begins
  4374. with the assignment%@AS@% result =%@AE@%.  %@NL@%
  4375. %@NL@%
  4376. Earlier in the program, we declared the variable %@AS@% result %@AE@% as an %@AB@%int%@AE@%. The
  4377. twobyte %@AB@%int%@AE@% variable created to store the result of these additions is too
  4378. small to contain the four-byte %@AB@%float%@AE@% sum that was finally produced.  %@NL@%
  4379. %@NL@%
  4380. The assignment forces QuickC to demote the larger %@AB@%float%@AE@% value to the smaller
  4381. %@AB@%int%@AE@% type. It's impossible to store such a large floating-point value in the
  4382. two bytes of an %@AB@%int%@AE@%, so the final result is incorrect.  %@NL@%
  4383. %@NL@%
  4384. Figure 5.2 shows the progression of automatic type conversions that the
  4385. CONVERT.C program produces.  %@NL@%
  4386. %@NL@%
  4387. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  4388. %@NL@%
  4389. We can fix the conversion error by declaring the variable %@AS@% result %@AE@% as a
  4390. %@AB@%float%@AE@%, substituting  %@NL@%
  4391. %@NL@%
  4392. %@AS@%  float result;%@AE@%%@NL@%
  4393. %@NL@%
  4394. for the earlier declaration. We'll also need to change the format string in
  4395. the %@AB@%printf%@AE@% function call to print a %@AB@%float%@AE@% value, as shown below:  %@NL@%
  4396. %@NL@%
  4397. %@AS@%  printf( "%6.2f\n", result );%@AE@%%@NL@%
  4398. %@NL@%
  4399. Now CONVERT.C prints the expected value of 64033.10.  %@NL@%
  4400. %@NL@%
  4401. %@NL@%
  4402. %@3@%%@CR:C6A00050013 @%%@AB@%Manual Type Conversions through Casting%@AE@%%@EH@%%@NL@%
  4403. %@NL@%
  4404. %@AU@% A cast forces a value  to a particular type.%@AE@%  %@NL@%
  4405. %@NL@%
  4406. The C language also allows you to force a type conversion that would not
  4407. otherwise happen, a process known as "casting." Using casts, it is possible
  4408. to convert a data item to any C data type.  %@NL@%
  4409. %@NL@%
  4410. Sometimes you must use a cast to make the program work properly. When
  4411. calling the %@AB@%malloc%@AE@% library function, for instance, you should perform a cast
  4412. on the value that the function returns. (Chapter 12, "Dynamic Memory
  4413. Allocation," explains %@AB@%malloc%@AE@% and other memory-allocating functions.)  %@NL@%
  4414. %@NL@%
  4415. Casts can also make a program more readable. QuickC does most automatic type
  4416. conversions silently. So if you write a tricky bit of code that relies on
  4417. automatic conversions, you, or some other programmer, may not notice the
  4418. conversions later. To make such code more readable─and easier to debug─you
  4419. can add explicit type casts in places where silent conversions might go
  4420. unnoticed.  %@NL@%
  4421. %@NL@%
  4422. To cast a value to a different type, place the desired type name in
  4423. parentheses in front of the value. For instance, the statement  %@NL@%
  4424. %@NL@%
  4425. %@AS@%  f_val = (float)any_val;%@AE@%%@NL@%
  4426. %@NL@%
  4427. casts the value of the variable %@AS@% any_val %@AE@% to type %@AB@%float%@AE@% before assigning it
  4428. to %@AS@% f_val%@AE@%. Here the type name in parentheses,  %@NL@%
  4429. %@NL@%
  4430. %@AS@%  (float)%@AE@%%@NL@%
  4431. %@NL@%
  4432. performs the cast. No matter what type %@AS@% any_val %@AE@% has, the cast converts that
  4433. type to %@AB@%float%@AE@% before assigning it to %@AS@% f_val%@AE@%.  %@NL@%
  4434. %@NL@%
  4435. When you cast a variable, the cast affects the value the variable yields,
  4436. but not the variable itself. Suppose that %@AS@% any_val %@AE@% is an %@AB@%int%@AE@% variable with
  4437. the value 333. The above cast converts the value 333 to %@AB@%float%@AE@% format before
  4438. assigning it to %@AS@% f_val%@AE@%. But %@AS@% any_val %@AE@% remains an %@AB@%int%@AE@% variable after the
  4439. cast.  %@NL@%
  4440. %@NL@%
  4441. Remember, you can detect automatic type conversions by setting Warning Level
  4442. 2 or higher in QuickC and watching for the following warning:  %@NL@%
  4443. %@NL@%
  4444. %@AS@%  C4051: data conversion%@AE@%%@NL@%
  4445. %@NL@%
  4446. You can then add explicit casts to eliminate the warning where the
  4447. conversions are desirable. (See "Automatic Type Conversions" above.)  %@NL@%
  4448. %@NL@%
  4449. %@NL@%
  4450. %@2@%%@CR:C6A00050014 @%%@AB@%Register Variables%@AE@%%@EH@%%@NL@%
  4451. %@NL@%
  4452. %@AU@% Register variables are stored in processor registers instead of addressable
  4453. %@AU@%memory.%@AE@%  %@NL@%
  4454. %@NL@%
  4455. You can use the %@AB@%register%@AE@% keyword in variable declarations to request that a
  4456. variable be stored in a processor register. Because processor registers can
  4457. be accessed more quickly than addressable memory locations, this storage can
  4458. make a program run faster. Programmers use %@AB@%register%@AE@% to speed access to
  4459. heavily used variables, such as counter variables in loops.  %@NL@%
  4460. %@NL@%
  4461. The %@AB@%register%@AE@% specifier is much less important than it used to be, now that
  4462. most C compilers, including QuickC, can perform optimizations (improvements)
  4463. during compilation. If you compile with the Optimizations option turned on,
  4464. QuickC automatically stores variables in registers when needed. So you
  4465. probably won't need to use %@AB@%register%@AE@% except in special cases.  %@NL@%
  4466. %@NL@%
  4467. ────────────────────────────────────────────────────────────────────────────%@NL@%
  4468. IMPORTANT
  4469.  
  4470. %@AI@%If you compile with Optimizations on, an explicit %@AB@%register%@AE@%%@AI@% declaration can
  4471. %@AI@%override register storage that QuickC would do automatically. Declaring one
  4472. %@AI@%variable with %@AE@%%@AI@%%@AB@%register%@AE@%%@AE@%%@AI@% might prevent QuickC from storing some other variable
  4473. %@AI@%in a register. In the worst case, this can make a program run slower.%@AE@%%@AE@%%@NL@%
  4474. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  4475. %@NL@%
  4476. You can use %@AB@%register%@AE@% only with short integer types (%@AB@%char%@AE@%, %@AB@%int%@AE@%, and %@AB@%short
  4477. %@AB@%int%@AE@%). Other types─including aggregate types such as arrays─are too large to
  4478. fit in a register.  %@NL@%
  4479. %@NL@%
  4480. Only two registers are available for variable storage at any given time.
  4481. (They are DI and SI, for those who have programmed in assembly language.) If
  4482. you request more registers than are available, QuickC stores the extra
  4483. variables in addressable memory, as it does non-%@AB@%register%@AE@% variables.  %@NL@%
  4484. %@NL@%
  4485. The following declaration uses %@AB@%register%@AE@% to ask the compiler to store the %@AB@%int%@AE@%
  4486. variable %@AS@% val %@AE@% in a processor register:  %@NL@%
  4487. %@NL@%
  4488. %@AS@%  register int val;%@AE@%%@NL@%
  4489. %@NL@%
  4490. You can ask the compiler to store more than one variable in a register. For
  4491. instance, the statement  %@NL@%
  4492. %@NL@%
  4493. %@AS@%  register int val, count;%@AE@%%@NL@%
  4494. %@NL@%
  4495. declares %@AS@% val %@AE@% and %@AS@% count %@AE@% as %@AB@%register%@AE@% variables.  %@NL@%
  4496. %@NL@%
  4497. ────────────────────────────────────────────────────────────────────────────%@NL@%
  4498. NOTE
  4499.  
  4500. %@AI@%Since registers are not addressable, you can't use the address-of (%@AB@%&%@AE@%%@AI@%)
  4501. %@AI@%operator to get the address of a variable declared with %@AE@%%@AI@%%@AB@%register%@AE@%%@AE@%%@AI@%. This rule
  4502. %@AI@%applies whether or not QuickC is actually able to store the variable in a
  4503. %@AI@%register. Thus, if you need to access a variable through a pointer, don't
  4504. %@AI@%declare that variable with %@AE@%%@AI@%%@AB@%register%@AE@%%@AE@%%@AI@%. See the section "Initializing a Pointer
  4505. %@AI@%Variable" in Chapter 8, "Pointers."%@AE@%%@AE@%%@NL@%
  4506. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  4507. %@NL@%
  4508. %@NL@%
  4509. %@2@%%@CR:C6A00050015 @%%@AB@%Renaming Existing Types with typedef%@AE@%%@EH@%%@NL@%
  4510. %@NL@%
  4511. The %@AB@%typedef%@AE@% keyword creates a new name for an existing data type. This is a
  4512. convenience feature that you can use to make programs more readable. For
  4513. instance, the declaration  %@NL@%
  4514. %@NL@%
  4515. %@AS@%  typedef int integer;%@AE@%%@NL@%
  4516. %@NL@%
  4517. allows you to use %@AS@% integer %@AE@% as a synonym for %@AB@%int%@AE@%.  %@NL@%
  4518. %@NL@%
  4519. One more practical use of %@AB@%typedef%@AE@% is to substitute a short, descriptive name
  4520. for an aggregate type. For instance, the QuickC Presentation Graphics
  4521. library uses %@AB@%typedef%@AE@% to create descriptive names such as %@AB@%windowtype%@AE@% and
  4522. %@AB@%titletype%@AE@% for structures used in that library.  %@NL@%
  4523. %@NL@%
  4524. ────────────────────────────────────────────────────────────────────────────%@NL@%
  4525. NOTE
  4526.  
  4527. %@AI@%The %@AB@%typedef%@AE@%%@AI@% keyword doesn't create a new data type. It merely allows you to
  4528. %@AI@%use a different name for a type that already exists.%@AE@%%@AE@%%@NL@%
  4529. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  4530. %@NL@%
  4531. You can also use %@AB@%typedef%@AE@% to minimize portability problems. By using %@AB@%typedef%@AE@%
  4532. declarations for data types that are machine dependent, you need only change
  4533. the %@AB@%typedef%@AE@% declaration if you move the program to a different operating
  4534. system.  %@NL@%
  4535. %@NL@%
  4536. %@NL@%
  4537. %@2@%%@CR:C6A00050016 @%%@AB@%The Enumeration Type%@AE@%%@EH@%%@NL@%
  4538. %@NL@%
  4539. The "enumeration type" specifies a set of named integer constants, similar
  4540. to the enumerated type in QuickPascal. In the C language, enumeration types
  4541. are declared with the %@AB@%enum%@AE@% keyword.  %@NL@%
  4542. %@NL@%
  4543. %@AU@% Use enum to name a  set of integer constants.%@AE@%  %@NL@%
  4544. %@NL@%
  4545. The %@AB@%enum%@AE@% type is useful mainly for improving a program's readability. With
  4546. %@AB@%enum%@AE@%, you can use meaningful names for a set of constants whose purpose
  4547. might not otherwise be apparent.  %@NL@%
  4548. %@NL@%
  4549. Suppose you're writing a calendar program in which the constant 0 represents
  4550. Saturday, 1 represents Sunday, and so on. You might begin by declaring the
  4551. enumeration type %@AS@% day %@AE@% in the following manner:  %@NL@%
  4552. %@NL@%
  4553. %@AS@%  enum day
  4554. %@AS@%  {
  4555. %@AS@%     saturday, sunday, monday, tuesday,
  4556. %@AS@%     wednesday, thursday, friday
  4557. %@AS@%  };%@AE@%%@NL@%
  4558. %@NL@%
  4559. Notice this declaration's similarity to a structure declaration. As with
  4560. structures, the type declaration creates a template that you can use to
  4561. declare variables of this type. (See the section "Declaring a Structure
  4562. Type" in Chapter 4, "Data Types.")  %@NL@%
  4563. %@NL@%
  4564. Unless you specify otherwise, the first value in an enumeration type equals
  4565. 0 and others are numbered sequentially. In the %@AB@%enum%@AE@% type shown above, %@AS@%
  4566. %@AS@%saturday %@AE@% equals 0, %@AS@% sunday %@AE@% equals 1, and so forth.  %@NL@%
  4567. %@NL@%
  4568. The values in an enumeration type need not be sequential, however. If you
  4569. want some other order, you can declare explicit values for each member of
  4570. the type. The following declaration, for example, assigns the names %@AS@% zero%@AE@%, %@AS@%
  4571. %@AS@%freeze%@AE@%, and %@AS@%boil %@AE@% to the constants 0, 32, and 220, respectively.  %@NL@%
  4572. %@NL@%
  4573. %@AS@%  enum temps
  4574. %@AS@%  {
  4575. %@AS@%     zero = 0,
  4576. %@AS@%     freeze = 32,
  4577. %@AS@%     boil = 220
  4578. %@AS@%  };%@AE@%%@NL@%
  4579. %@NL@%
  4580. After declaring an enumeration type, you can create a variable of that type
  4581. and assign it a value from the type. The statement  %@NL@%
  4582. %@NL@%
  4583. %@AS@%  enum day today = wednesday;%@AE@%%@NL@%
  4584. %@NL@%
  4585. declares the variable %@AS@% today%@AE@%, assigning it the value %@AS@% wednesday %@AE@% from the %@AS@%
  4586. %@AS@%day %@AE@% enumeration type.  %@NL@%
  4587. %@NL@%
  4588. After you assign its value, you can use the variable %@AS@% today %@AE@% as you would an
  4589. %@AB@%int%@AE@% variable. Although the variable is considered to have the %@AB@%enum%@AE@% type, it
  4590. is an ordinary %@AB@%int%@AE@% for all practical purposes.  %@NL@%
  4591. %@NL@%
  4592. Enumeration types aren't used very often, partly because you can achieve a
  4593. similar effect using the %@AB@%#define%@AE@% directive. (Chapter 7, "Preprocessor
  4594. Directives," explains %@AB@%#define%@AE@% in detail.) For example, the code  %@NL@%
  4595. %@NL@%
  4596. %@AS@%  #define SATURDAY 0
  4597. %@AS@%  #define SUNDAY 1
  4598. %@AS@%  #define MONDAY 2
  4599. %@AS@%  #define TUESDAY 3
  4600. %@AS@%  #define WEDNESDAY 4
  4601. %@AS@%     .
  4602. %@AS@%     .
  4603. %@AS@%     .
  4604. %@AS@%  int today = WEDNESDAY;%@AE@%%@NL@%
  4605. %@NL@%
  4606. uses %@AB@%#define%@AE@% to create symbolic constants named %@AS@% SATURDAY%@AE@%, %@AS@% SUNDAY%@AE@%, %@AS@% MONDAY%@AE@%,
  4607. %@AS@% TUESDAY%@AE@%, and %@AS@% WEDNESDAY%@AE@%, assigning them the values 0 through 4. The last
  4608. line in the example creates the %@AB@%int%@AE@% variable %@AS@% today %@AE@% and assigns it the
  4609. value of %@AS@% WEDNESDAY%@AE@%. The result is identical to the statement shown earlier:
  4610. %@NL@%
  4611. %@NL@%
  4612. %@AS@%  enum day today = wednesday;%@AE@%%@NL@%
  4613. %@NL@%
  4614. One advantage of using %@AB@%enum%@AE@% over %@AB@%#define%@AE@% directives is that it groups
  4615. related names in one place and can be more compact than a long series of
  4616. directives.  %@NL@%
  4617. %@NL@%
  4618. This concludes our main discussion of data types. The next chapter,
  4619. "Operators," examines the C language's rich set of operators, which allow
  4620. you to manipulate data in many different ways.  %@NL@%
  4621. %@NL@%
  4622. %@NL@%
  4623. %@NL@%
  4624. %@NL@%
  4625. %@NL@%
  4626. %@NL@%
  4627. %@CR:C6A00060001 @%%@1@%%@AB@%Chapter 6  Operators%@AE@%%@EH@%%@NL@%
  4628. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  4629. %@NL@%
  4630. Compared with other languages, C is very compact, using fewer than 50
  4631. keywords. One reason C can get by with so few reserved words is its
  4632. abundance of powerful operators─well over 30.  %@NL@%
  4633. %@NL@%
  4634. Most C operators are easy to understand and remember. Even if you have never
  4635. seen a C program, you probably understand that the statement  %@NL@%
  4636. %@NL@%
  4637. %@AS@%  val = val * 5;%@AE@%%@NL@%
  4638. %@NL@%
  4639. multiplies the variable %@AS@% val %@AE@% by 5 and assigns the result to %@AS@% val%@AE@%.  %@NL@%
  4640. %@NL@%
  4641. Because the printable ASCII character set has only so many unique symbols, C
  4642. uses some ASCII symbols in more than one operator. For instance, the
  4643. asterisk (%@AB@%*%@AE@%) performs either a multiplication or pointer operation,
  4644. depending on context. Similarly, the ampersand (%@AB@%&%@AE@%) is part of three C
  4645. operators. Depending on context, the ampersand can obtain an address or
  4646. perform a logical or bitwise AND operation. Be careful not to confuse
  4647. operators that look similar but do different jobs.  %@NL@%
  4648. %@NL@%
  4649. This chapter describes the C operators, beginning with those that are common
  4650. to most languages, and then discussing those unique to C.  %@NL@%
  4651. %@NL@%
  4652. %@NL@%
  4653. %@2@%%@CR:C6A00060002 @%%@AB@%Introducing C's Operators%@AE@%%@EH@%%@NL@%
  4654. %@NL@%
  4655. We'll start by discussing C operators that look and behave similarly to
  4656. operators in other languages. These include the following groups:  %@NL@%
  4657. %@NL@%
  4658. %@NL@%
  4659.   ■   Arithmetic operators, which do operations such as addition and
  4660.       multiplication%@NL@%
  4661. %@NL@%
  4662.   ■   Relational operators, which compare two values and give a true or
  4663.       false result%@NL@%
  4664. %@NL@%
  4665.   ■   Assignment operators, which make one value equal to another%@NL@%
  4666. %@NL@%
  4667. %@NL@%
  4668. %@NL@%
  4669. %@3@%%@CR:C6A00060003 @%%@AB@%Arithmetic Operators%@AE@%%@EH@%%@NL@%
  4670. %@NL@%
  4671. The C language's arithmetic operators closely resemble those in other
  4672. languages. Table 6.1 lists C's arithmetic operators.  %@NL@%
  4673. %@NL@%
  4674. %@AB@%Table 6.1  %@AB@%Arithmetic Operators%@AE@%%@AE@%
  4675.  
  4676. %@TH:   8   511 02 26 50 @%Operator                  Description%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%*%@AE@%                         Multiplication%@AB@%/%@AE@%                         Division%@AB@%%%@AE@%                         Modulus%@AB@%+%@AE@%                         Addition%@AB@%- %@AE@%                        Subtraction%@AB@%%@AE@%%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   8   511 02 26 50 @%
  4677.  
  4678. The "modulus operator" (%@AB@%%%@AE@%) may be unfamiliar. It divides a value and gives
  4679. the remainder. For instance, the statement  %@NL@%
  4680. %@NL@%
  4681. %@AS@%  remainder = 20 % 3;%@AE@%%@NL@%
  4682. %@NL@%
  4683. assigns the value 2 to the variable %@AS@% remainder %@AE@% (20 divided by 3 equals 6,
  4684. with a remainder of 2). If the division doesn't produce a remainder, the
  4685. modulus operator yields the value 0.  %@NL@%
  4686. %@NL@%
  4687. %@NL@%
  4688. %@3@%%@CR:C6A00060004 @%%@AB@%Relational Operators%@AE@%%@EH@%%@NL@%
  4689. %@NL@%
  4690. "Relational operators" evaluate the relationship between two expressions,
  4691. giving a true result (the value 1) or a false result (the value 0). C has
  4692. six relational operators, which are listed in Table 6.2.  %@NL@%
  4693. %@NL@%
  4694. %@AB@%Table 6.2  %@AB@%Relational Operators%@AE@%%@AE@%
  4695.  
  4696. %@TH:   9   543 02 23 53 @%Operator               Description%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%<%@AE@%                      Less than%@AB@%<=%@AE@%                     Less than or equal%@AB@%>%@AE@%                      Greater than%@AB@%>=%@AE@%                     Greater than or equal%@AB@%==%@AE@%                     Equal%@AB@%!=%@AE@%                     Not equal%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   9   543 02 23 53 @%
  4697.  
  4698. The "equality operator" (%@AB@%==%@AE@%), shown above, tests whether two expressions are
  4699. equal.  %@NL@%
  4700. %@NL@%
  4701. Don't confuse the equality operator with the assignment operator (%@AB@%=%@AE@%)
  4702. discussed in the next section. The assignment operator sets one value equal
  4703. to another, as we'll see shortly. (Chapter 10, "Programming Pitfalls,"
  4704. discusses this common programming error.)  %@NL@%
  4705. %@NL@%
  4706. The C language gives the value 1 for true and 0 for false but recognizes any
  4707. nonzero value as true. The following code fragment demonstrates this
  4708. difference:  %@NL@%
  4709. %@NL@%
  4710. %@AS@%  printf( "C generates %d for true\n", 2 == 2 );
  4711. %@AS@%  printf( "C generates %d for false\n", 2 == 4 );
  4712. %@AS@%  if( -33 )
  4713. %@AS@%     printf( "C recognizes any nonzero value as true\n" );%@AE@%%@NL@%
  4714. %@NL@%
  4715. The output from this code,  %@NL@%
  4716. %@NL@%
  4717. %@AS@%  C generates 1 for true
  4718. %@AS@%  C generates 0 for false
  4719. %@AS@%  C recognizes any nonzero value as true%@AE@%%@NL@%
  4720. %@NL@%
  4721. shows that the true expression (%@AS@% 2 == 2 %@AE@%) gives the value 1 and the false
  4722. expression (%@AS@% 2 == 4 %@AE@%) gives the value 0. The last output line shows that C
  4723. recognizes the nonzero value -33 as a true value.  %@NL@%
  4724. %@NL@%
  4725. %@NL@%
  4726. %@3@%%@CR:C6A00060005 @%%@AB@%Assignment Operators%@AE@%%@EH@%%@NL@%
  4727. %@NL@%
  4728. The "assignment operator" (%@AB@%=%@AE@%) sets one value equal to another. The following
  4729. statement assigns the value of %@AS@% sample %@AE@% to %@AS@% val%@AE@%:  %@NL@%
  4730. %@NL@%
  4731. %@AS@%  val = sample;%@AE@%%@NL@%
  4732. %@NL@%
  4733. %@AU@% You can combine an assignment with a bitwise  or arithmetic operation.%@AE@%  %@NL@%
  4734. %@NL@%
  4735. In a convenient shorthand, C allows you to combine the assignment operator
  4736. with any arithmetic or bitwise operator (see the "Arithmetic Operators" and
  4737. "Bitwise Operators" sections). For example, the statement  %@NL@%
  4738. %@NL@%
  4739. %@AS@%  val = val + sample;%@AE@%%@NL@%
  4740. %@NL@%
  4741. can more conveniently be written  %@NL@%
  4742. %@NL@%
  4743. %@AS@%  val += sample;%@AE@%%@NL@%
  4744. %@NL@%
  4745. Both statements add %@AS@% val %@AE@% to %@AS@% sample %@AE@% and then assign the result to %@AS@% val%@AE@%.  %@NL@%
  4746. %@NL@%
  4747. Table 6.3 lists C's special assignment operators.  %@NL@%
  4748. %@NL@%
  4749. %@AB@%Table 6.3  %@AB@%Special Assignment Operators%@AE@%%@AE@%
  4750.  
  4751. %@TH:  13   863 02 12 14 50 @%Expression  Equivalent    Operation%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AS@%x *= y%@AE@%      %@AS@% x = x * y%@AE@%    Multiplication%@AS@%x /= y%@AE@%      %@AS@% x = x / y%@AE@%    Division%@AS@%x %= y%@AE@%      %@AS@% x = x % y%@AE@%    Modulus%@AS@%x += y%@AE@%      %@AS@% x = x + y%@AE@%    Addition%@AS@%x -= y%@AE@%      %@AS@% x = x - y%@AE@%    Subtraction%@AS@%x <<= y%@AE@%     %@AS@% x = x << y%@AE@%   Left shift%@AS@%x >>= y%@AE@%     %@AS@% x = x >> y%@AE@%   Right shift%@AS@%x &= y%@AE@%      %@AS@% x = x & y%@AE@%    AND%@AS@%x ^= y%@AE@%      %@AS@% x = x ^ y%@AE@%    Exclusive OR%@AS@%x |= y%@AE@%      %@AS@% x = x | y%@AE@%    Inclusive OR%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  13   863 02 12 14 50 @%
  4752.  
  4753. Note that the equal sign always follows the other operator. In the following
  4754. code,  %@NL@%
  4755. %@NL@%
  4756. %@AS@%  val ^= sample;
  4757. %@AS@%  val =^ sample;%@AE@%%@NL@%
  4758. %@NL@%
  4759. the first statement is meaningful, but the second is a syntax error.  %@NL@%
  4760. %@NL@%
  4761. %@NL@%
  4762. %@2@%%@CR:C6A00060006 @%%@AB@%C's Unique Operators%@AE@%%@EH@%%@NL@%
  4763. %@NL@%
  4764. The remaining sections in this chapter describe C operators that fall into
  4765. two categories: those that are unique to C and those that look or behave
  4766. differently in C than in other languages.  %@NL@%
  4767. %@NL@%
  4768. %@NL@%
  4769. %@3@%%@CR:C6A00060007 @%%@AB@%Increment and Decrement Operators%@AE@%%@EH@%%@NL@%
  4770. %@NL@%
  4771. The C language's unique "increment" (%@AB@%++%@AE@%) and "decrement" (%@AB@%- -%@AE@%) operators are
  4772. very useful. They increase or decrease an expression by a value of 1.  %@NL@%
  4773. %@NL@%
  4774. %@AB@%Table 6.4  %@AB@%Increment and Decrement Operators%@AE@%%@AE@%
  4775.  
  4776. %@TH:   5   370 02 22 54 @%Operator              Operation%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%++%@AE@%                    Increment expression by 1%@AB@%- -%@AE@%                   Decrement expression by 1%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   5   370 02 22 54 @%
  4777.  
  4778. Thus, the two statements  %@NL@%
  4779. %@NL@%
  4780. %@AS@%  val = val + 1;
  4781. %@AS@%  val++;%@AE@%%@NL@%
  4782. %@NL@%
  4783. are equivalent and so are these statements:  %@NL@%
  4784. %@NL@%
  4785. %@AS@%  val = val - 1;
  4786. %@AS@%  val--;%@AE@%%@NL@%
  4787. %@NL@%
  4788. %@AU@% You can use the ++ and - - operators before  or after an expression.%@AE@%  %@NL@%
  4789. %@NL@%
  4790. The %@AB@%++%@AE@% and %@AB@%- -%@AE@% operators can precede or follow an expression. Placed before
  4791. an expression, the operator changes the expression before the expression's
  4792. value is used. In this case, the operator is said to be a "prefix" operator.
  4793. Placed after an expression, the operator (known as a "postfix" operator)
  4794. changes the value of the expression after the expression's value is used.  %@NL@%
  4795. %@NL@%
  4796. In the DECRMENT.C program, shown below, the decrement operator is used both
  4797. as a prefix operator and a postfix operator.  %@NL@%
  4798. %@NL@%
  4799. %@AS@%  /* DECRMENT.C: Demonstrate prefix and postfix operators. */
  4800. %@AS@%  
  4801. %@AS@%  #include <stdio.h>
  4802. %@AS@%  main()
  4803. %@AS@%  {
  4804. %@AS@%     int val, sample = 3, proton = 3;
  4805. %@AS@%     val = sample--;
  4806. %@AS@%     printf( "val = %d  sample = %d\n", val, sample );
  4807. %@AS@%     val = --proton;
  4808. %@AS@%     printf( "val = %d  proton = %d\n", val, proton );
  4809. %@AS@%  }%@AE@%%@NL@%
  4810. %@NL@%
  4811. Here is the output from DECRMENT.C:  %@NL@%
  4812. %@NL@%
  4813. %@AS@%  val = 3  sample = 2
  4814. %@AS@%  val = 2  proton = 2%@AE@%%@NL@%
  4815. %@NL@%
  4816. In the first use of the decrement operator, the statement  %@NL@%
  4817. %@NL@%
  4818. %@AS@%  val = sample--;%@AE@%%@NL@%
  4819. %@NL@%
  4820. assigns the value of %@AS@% sample %@AE@% (3) to the variable %@AS@% val %@AE@% and then decrements %@AS@%
  4821. %@AS@%sample %@AE@% to the value 2. Contrast this with the statement  %@NL@%
  4822. %@NL@%
  4823. %@AS@%  val = --proton;%@AE@%%@NL@%
  4824. %@NL@%
  4825. which first decrements %@AS@% proton %@AE@% to the value 2 and then assigns that value
  4826. to %@AS@% val%@AE@%.  %@NL@%
  4827. %@NL@%
  4828. %@NL@%
  4829. %@3@%%@CR:C6A00060008 @%%@AB@%Bitwise Operators%@AE@%%@EH@%%@NL@%
  4830. %@NL@%
  4831. The "bitwise operators," listed in Table 6.5, manipulate bits in data of the
  4832. integer type. These operators are often used in programs that must interact
  4833. with hardware.  %@NL@%
  4834. %@NL@%
  4835. %@AB@%Table 6.5  %@AB@%Bitwise Operators%@AE@%%@AE@%
  4836.  
  4837. %@TH:   9   548 02 26 50 @%Operator                  Description%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%~%@AE@%                         Complement%@AB@%<<%@AE@%                        Left shift%@AB@%>>%@AE@%                        Right shift%@AB@%&%@AE@%                         AND%@AB@%^%@AE@%                         Exclusive OR%@AB@%|%@AE@%                         Inclusive OR%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   9   548 02 26 50 @%
  4838.  
  4839. The %@AB@%~%@AE@% operator, known as the "one's complement," acts on only one value
  4840. (rather than on two, as do most operators). This operator changes every 1
  4841. bit in its operand to a 0 bit and vice versa.  %@NL@%
  4842. %@NL@%
  4843. The %@AB@%%@AE@% and%@AB@% >>%@AE@% operators, known as the "shift operators," shift the left
  4844. operand by the value given in the right operand. These operators offer a
  4845. fast, convenient way to multiply or divide integers by a power of 2.  %@NL@%
  4846. %@NL@%
  4847. The %@AB@%&%@AE@% operator, known as the "bitwise AND," sets a bit to 1 if either of the
  4848. corresponding bits in its operands is 1, or to 0 if both corresponding bits
  4849. are 0. It is often used to "mask," or turn off, one or more bits in a value.
  4850. %@NL@%
  4851. %@NL@%
  4852. The %@AB@%^%@AE@% operator, known as the "bitwise exclusive OR," sets a bit to 1 if the
  4853. corresponding bits in its operands are different, or to 0 if they are the
  4854. same.  %@NL@%
  4855. %@NL@%
  4856. The  %@AB@%|%@AE@%  operator, known as the "bitwise inclusive OR," sets a bit to 1 if
  4857. either of the corresponding bits in its operands is 1, or to 0 if both
  4858. corresponding bits are 0. It is often used to turn on bits in a value.  %@NL@%
  4859. %@NL@%
  4860. Each of the bitwise operators is used in the BITWISE.C program, shown below.
  4861. %@NL@%
  4862. %@NL@%
  4863. %@AS@%  /* BITWISE.C: Demonstrate bitwise operators. */
  4864. %@AS@%  
  4865. %@AS@%  #include <stdio.h>
  4866. %@AS@%  
  4867. %@AS@%  main()
  4868. %@AS@%  {
  4869. %@AS@%     printf( "255 & 15 = %d\n", 255 & 15 );
  4870. %@AS@%     printf( "255 | 15 = %d\n", 255 | 15 );
  4871. %@AS@%     printf( "255 ^ 15 = %d\n", 255 ^ 15 );
  4872. %@AS@%     printf( "2 << 2   = %d\n", 2 << 2 );
  4873. %@AS@%     printf( "16 >> 2  = %d\n", 16 >> 2 );
  4874. %@AS@%     printf( "~2       = %d\n", ~2 );
  4875. %@AS@%  }%@AE@%%@NL@%
  4876. %@NL@%
  4877. The output from BITWISE.C,  %@NL@%
  4878. %@NL@%
  4879. %@AS@%  255 & 15 = 15
  4880. %@AS@%  255 | 15 = 255
  4881. %@AS@%  255 ^ 15 = 240
  4882. %@AS@%  2 << 2   = 8
  4883. %@AS@%  16 >> 2  = 4
  4884. %@AS@%  ~2  = -3%@AE@%%@NL@%
  4885. %@NL@%
  4886. shows the results of the program's various bitwise operations.  %@NL@%
  4887. %@NL@%
  4888. The fourth and fifth output lines show you how to use shift operators to
  4889. multiply and divide by powers of 2. The program multiplies 2 by 4 by
  4890. shifting the value 2 twice to the left:  %@NL@%
  4891. %@NL@%
  4892. %@AS@%  2 << 2   = 8%@AE@%%@NL@%
  4893. %@NL@%
  4894. Similarly, the program divides 16 by 4 by shifting the value 16 twice to the
  4895. right:  %@NL@%
  4896. %@NL@%
  4897. %@AS@%  16 >> 2  = 4%@AE@%%@NL@%
  4898. %@NL@%
  4899. %@NL@%
  4900. %@3@%%@CR:C6A00060009 @%%@AB@%Logical Operators%@AE@%%@EH@%%@NL@%
  4901. %@NL@%
  4902. C has three logical operators─AND, OR, and NOT─that allow you to test more
  4903. than one condition in a single expression. Table 6.6 lists C's logical
  4904. operators.  %@NL@%
  4905. %@NL@%
  4906. %@AB@%Table 6.6  %@AB@%Logical Operators%@AE@%%@AE@%
  4907.  
  4908. %@TH:   6   409 02 27 49 @%Operator                   Description%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%!%@AE@%                          Logical NOT%@AB@%&&%@AE@%                         Logical AND%@AB@%| |%@AE@%                        Logical OR%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   6   409 02 27 49 @%
  4909.  
  4910. The logical OR ( %@AB@%| |%@AE@% ) and AND (%@AB@%&&%@AE@%) operators are often used to combine
  4911. logical tests within a conditional statement. For example, the %@AB@%if%@AE@% statement
  4912. %@NL@%
  4913. %@NL@%
  4914. %@AS@%  if( val > 10 && sample < 10 )
  4915. %@AS@%     printf( "Oh joy!\n" );%@AE@%%@NL@%
  4916. %@NL@%
  4917. prints %@AS@% Oh joy! %@AE@% if both conditions in the test expression are true (if %@AS@% val
  4918. %@AS@%%@AE@% is greater than 10 and %@AS@% sample %@AE@% is less than 10). Here, the relational
  4919. operators (%@AB@%>%@AE@% and %@AB@%%@AE@%) have higher "precedence" than the logical AND operator
  4920. (%@AB@%&&%@AE@%), so the compiler evaluates them first. We discuss operator precedence
  4921. later in this chapter.  %@NL@%
  4922. %@NL@%
  4923. The logical NOT operator (%@AB@%!%@AE@%) reverses an expression's logical value. For
  4924. instance, if the variable %@AS@% val %@AE@% has the value 8, the expression %@AS@% (val == 8) %@AE@%
  4925. is true but the expression %@AS@% !(val == 8) %@AE@% is false.  %@NL@%
  4926. %@NL@%
  4927. The NOT.C program below shows a common use of this operator.  %@NL@%
  4928. %@NL@%
  4929. %@AS@%  /* NOT.C: Demonstrate logical NOT operator. */
  4930. %@AS@%  
  4931. %@AS@%  #include <stdio.h>
  4932. %@AS@%  
  4933. %@AS@%  main()
  4934. %@AS@%  {
  4935. %@AS@%    int val = 0;
  4936. %@AS@%    if( !val )
  4937. %@AS@%       printf( "val is zero" );
  4938. %@AS@%  }%@AE@%%@NL@%
  4939. %@NL@%
  4940. The expression %@AS@% if( !val ) %@AE@% is equivalent to the expression %@AS@% if( val == 0 )%@AE@%.
  4941. When used in this way, the logical NOT operator converts a 0 value to 1 and
  4942. any nonzero value to 0.  %@NL@%
  4943. %@NL@%
  4944. ────────────────────────────────────────────────────────────────────────────%@NL@%
  4945. NOTE
  4946.  
  4947. %@AI@%Don't confuse the logical OR and AND operators with the bitwise OR and AND
  4948. %@AI@%operators discussed in the previous section. The bitwise operators use the
  4949. %@AI@%same ASCII symbols, but have only one character. For instance, logical AND
  4950. %@AI@%is %@AB@%&&%@AE@%%@AI@%, whereas bitwise AND is %@AE@%%@AI@%%@AB@%&%@AE@%%@AE@%%@AI@%.%@AE@%%@AE@%%@NL@%
  4951. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  4952. %@NL@%
  4953. %@NL@%
  4954. %@3@%%@CR:C6A00060010 @%%@AB@%Address Operators%@AE@%%@EH@%%@NL@%
  4955. %@NL@%
  4956. The C language has two operators that work with memory addresses. Table 6.7
  4957. lists C's address operators.  %@NL@%
  4958. %@NL@%
  4959. %@AB@%Table 6.7  %@AB@%Address Operators%@AE@%%@AE@%
  4960.  
  4961. %@TH:   5   373 02 15 61 @%Operator       Operation%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%&%@AE@%              Yield address of the operand%@AB@%*%@AE@%              Yield value contained at the operand's address%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   5   373 02 15 61 @%
  4962.  
  4963. Both address operators are often used with pointers─variables that contain
  4964. the addresses of other variables. Chapter 8, "Pointers," and Chapter 9,
  4965. "Advanced Pointers," are devoted to explaining pointers, including the use
  4966. of these two operators with them. Since you must understand pointers in
  4967. order to understand these operators fully, we'll describe them briefly here
  4968. and elaborate on their use in Chapter 8.  %@NL@%
  4969. %@NL@%
  4970. The "address-of operator" (%@AB@%&%@AE@%) yields a constant equal to the machine address
  4971. of its operand. For instance, if the variable %@AS@% val %@AE@% contains the value 10,
  4972. and its storage is located at address 1508, the expression %@AS@% val %@AE@% yields the
  4973. value 10, while the expression %@AS@% &val %@AE@% yields the constant 1508.  %@NL@%
  4974. %@NL@%
  4975. Since the address-of operator yields a constant, you can't assign a value to
  4976. an expression that uses it. The statement  %@NL@%
  4977. %@NL@%
  4978. %@AS@%  &val = 20;%@AE@%%@NL@%
  4979. %@NL@%
  4980. is illegal for the same reason that the statement  %@NL@%
  4981. %@NL@%
  4982. %@AS@%  1508 = 20;%@AE@%%@NL@%
  4983. %@NL@%
  4984. won't pass muster.  %@NL@%
  4985. %@NL@%
  4986. The "indirection operator" (%@AB@%*%@AE@%) yields the value contained in the address
  4987. referenced by its operand. If you declare %@AS@% ptr %@AE@% as a pointer variable, the
  4988. expression  %@NL@%
  4989. %@NL@%
  4990. %@AS@%  *ptr%@AE@%%@NL@%
  4991. %@NL@%
  4992. yields the contents of the address to which %@AS@% ptr %@AE@% points.  %@NL@%
  4993. %@NL@%
  4994. %@NL@%
  4995. %@3@%%@CR:C6A00060011 @%%@AB@%Conditional Operator%@AE@%%@EH@%%@NL@%
  4996. %@NL@%
  4997. The "conditional operator" (%@AB@%? :%@AE@%) is made up of two symbols and requires
  4998. three expressions. It is similar to an %@AB@%if%@AE@%-%@AB@%else%@AE@% construct. If the first
  4999. expression evaluates as true, the first operand is assigned the value of the
  5000. second operand. If the first expression is false, the first operand is
  5001. assigned the value of the third operand.  %@NL@%
  5002. %@NL@%
  5003. The following statement gives the absolute value of the variable %@AS@% val%@AE@%. The
  5004. variable is assigned its original value if it is nonnegative or is negated
  5005. if its original value is negative:  %@NL@%
  5006. %@NL@%
  5007. %@AS@%  val = (val >= 0 ) ? val : -val;%@AE@%%@NL@%
  5008. %@NL@%
  5009. The statement is equivalent to the following %@AB@%if%@AE@%-%@AB@%else%@AE@% construct:  %@NL@%
  5010. %@NL@%
  5011. %@AS@%  if( val >= 0 )
  5012. %@AS@%     val = val;
  5013. %@AS@%  else
  5014. %@AS@%     val = -val;%@AE@%%@NL@%
  5015. %@NL@%
  5016. %@NL@%
  5017. %@3@%%@CR:C6A00060012 @%%@AB@%The sizeof Operator%@AE@%%@EH@%%@NL@%
  5018. %@NL@%
  5019. The "%@AB@%sizeof%@AE@% operator" yields the number of bytes contained in its operand,
  5020. which can be either a general data type or a specific variable. If you apply
  5021. %@AB@%sizeof%@AE@% to a type name in parentheses, as in the expression  %@NL@%
  5022. %@NL@%
  5023. %@AS@%  sizeof( int )%@AE@%%@NL@%
  5024. %@NL@%
  5025. the operator yields the size of that data type in bytes. This example yields
  5026. the value 2, indicating that an %@AB@%int%@AE@% contains two bytes on DOS machines. You
  5027. can use this feature to determine the sizes of types that are implementation
  5028. dependent when transporting a program from one machine to another.  %@NL@%
  5029. %@NL@%
  5030. If you place %@AB@%sizeof%@AE@% in front of a variable name, the operator returns the
  5031. number of bytes in the variable. For instance, if you create the string  %@NL@%
  5032. %@NL@%
  5033. %@AS@%  char my_string[] = "Hello";%@AE@%%@NL@%
  5034. %@NL@%
  5035. the expression  %@NL@%
  5036. %@NL@%
  5037. %@AS@%  sizeof my_string%@AE@%%@NL@%
  5038. %@NL@%
  5039. yields the value 6, showing that the string contains 5 printing characters
  5040. and a null character.  %@NL@%
  5041. %@NL@%
  5042. %@NL@%
  5043. %@3@%%@CR:C6A00060013 @%%@AB@%Comma Operator%@AE@%%@EH@%%@NL@%
  5044. %@NL@%
  5045. Preceding chapters have shown various ways to use the comma (%@AB@%,%@AE@%) in C
  5046. programming. For instance, commas can separate multiple function arguments
  5047. or variable declarations. In such cases the comma is not an operator in the
  5048. formal sense but merely punctuation, like the semicolon that ends a
  5049. statement. %@AS@%  %@AE@%%@NL@%
  5050. %@NL@%
  5051. %@AU@% The comma is used as punctuation and as an operator in C.%@AE@%  %@NL@%
  5052. %@NL@%
  5053. In C, the comma can also perform as an operator. The commas that separate
  5054. multiple expressions determine the order in which the expressions are
  5055. evaluated, and the type and value of the result that is returned. The comma
  5056. operator causes expressions to be evaluated from left to right. The value
  5057. and type of the result are the value and type of the rightmost operand.  %@NL@%
  5058. %@NL@%
  5059. For example, the statement  %@NL@%
  5060. %@NL@%
  5061. %@AS@%  val = sample, sample = temp;%@AE@%%@NL@%
  5062. %@NL@%
  5063. first assigns the value of %@AS@% sample %@AE@% to %@AS@% val%@AE@%, then assigns the value of %@AS@% temp
  5064. %@AS@%%@AE@% to %@AS@% sample%@AE@%.  %@NL@%
  5065. %@NL@%
  5066. The comma operator often appears in %@AB@%for%@AE@% statements, where it can separate
  5067. multiple initializing expressions or multiple modifying expressions. The
  5068. FORLOOP1.C program from Chapter 3, "Flow Control," demonstrates both uses.
  5069. Here is the %@AB@%for%@AE@% statement from that program:  %@NL@%
  5070. %@NL@%
  5071. %@AS@%  for( a = 256, b = 1; b < 512; a = a / 2, b = b * 2 )
  5072. %@AS@%     printf( "a = %d \tb = %d\n", a, b );%@AE@%%@NL@%
  5073. %@NL@%
  5074. The statement initializes two variables (%@AS@% a %@AE@% and %@AS@% b %@AE@%) and contains two
  5075. modifying expressions (%@AS@% a = a / 2 %@AE@% and %@AS@% b = b * 2 %@AE@%). Chapter 3 explains the
  5076. FORLOOP1.C program in detail.  %@NL@%
  5077. %@NL@%
  5078. %@NL@%
  5079. %@3@%%@CR:C6A00060014 @%%@AB@%Base Operator%@AE@%%@EH@%%@NL@%
  5080. %@NL@%
  5081. The base operator (%@AB@%:>%@AE@%) associates a base expression with a based pointer.
  5082. Based-object support is a highly advanced feature included in QuickC 2.5 for
  5083. compatibility with Microsoft C version 6.0; please refer to your C 6.0
  5084. documentation for information about based objects.  %@NL@%
  5085. %@NL@%
  5086. %@NL@%
  5087. %@2@%%@CR:C6A00060015 @%%@AB@%Operator Precedence%@AE@%%@EH@%%@NL@%
  5088. %@NL@%
  5089. Like all languages, C has precedence rules that control the order for
  5090. evaluating the elements in expressions containing more than one operator. If
  5091. you're familiar with precedence rules in other languages, you won't find any
  5092. surprises in C. Table 6.8 shows the "pecking order" established for C's
  5093. operators.  %@NL@%
  5094. %@NL@%
  5095. Three general rules control the order of evaluation:  %@NL@%
  5096. %@NL@%
  5097. %@NL@%
  5098.   1.  When two operators have unequal precedence, the operator with higher
  5099.       precedence is evaluated first.%@NL@%
  5100. %@NL@%
  5101.   2.  Operators with equal precedence are evaluated from left to right.%@NL@%
  5102. %@NL@%
  5103.   3.  You can change the normal order of precedence by enclosing an
  5104.       expression in parentheses. The enclosed expression is then evaluated
  5105.       first. (If parentheses are nested, inner parentheses have higher
  5106.       precedence than outer ones.)%@NL@%
  5107. %@NL@%
  5108. %@NL@%
  5109. We'll demonstrate operator precedence with a simple example. Since the
  5110. multiplication operator (*) has higher precedence than the addition operator
  5111. (+), the statement  %@NL@%
  5112. %@NL@%
  5113. %@AS@%%@AE@%val = 2 + 3 * 4  %@NL@%
  5114. %@NL@%
  5115. assigns to%@AS@% val %@AE@%the value of 14 (or 2 + 12) rather than 20 (or 5 * 4). Since
  5116. parentheses have higher precedence than any operator, they can change the
  5117. normal precedence order. If you enclose the addition operation in
  5118. parentheses, as follows  %@NL@%
  5119. %@NL@%
  5120. %@AS@%%@AE@%val = (2 + 3) * 4  %@NL@%
  5121. %@NL@%
  5122. the addition is done first. Now the statement assigns to%@AS@% val %@AE@% the value 20
  5123. (or 5 * 4).  %@NL@%
  5124. %@NL@%
  5125. Table 6.8 lists the C operators and their precedence values. The lines in
  5126. the table separate precedence levels. The highest precedence level is at the
  5127. top of the table.  %@NL@%
  5128. %@NL@%
  5129. %@AB@%Table 6.8  %@AB@%C Operators%@AE@%%@AE@%
  5130.  
  5131. %@TH: 114  4496 02 24 37 15 @%Symbol                  Name or Meaning                      Associativity%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%( )                     Function call                        Left to right[ ]                     Array element %@AB@%.%@AE@%                       Structure or union                         member %@AB@%->%@AE@%                      Pointer to structure                         member %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%- -%@AE@%                     Decrement                            Right to left%@AB@%++%@AE@%                      Increment %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%:>                      Base operator                        Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%!%@AE@%                       Logical NOT                          Right to left%@AB@%~ %@AE@%                      One's complement%@AB@% %@AE@%%@AB@%-%@AE@%                       Unary minus %@AB@%+%@AE@%                       Unary plus %@AB@%&%@AE@%                       Address %@AB@%*%@AE@%                       Indirection %@AB@%sizeof%@AE@%                  Size in bytes %@AB@%(%@AE@%type%@AB@%)%@AE@%                  Type cast [for example, (%@AB@%float%@AE@%) i] %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%*%@AE@%                       Multiply                             Left to right%@AB@%/%@AE@%                       Divide %@AB@%%%@AE@%                       Modulus (remainder) %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%+%@AE@%                       Add                                  Left to right%@AB@%-%@AE@%                       Subtract %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%<<%@AE@%                      Left shift                           Left to right%@AB@%>>%@AE@%                      Right shift %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%<%@AE@%                       Less than                            Left to right%@AB@%<=%@AE@%                      Less than or equal %@AB@%>%@AE@%                       Greater than %@AB@%>=%@AE@%                      Greater than or equal %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%==%@AE@%                      Equal                                Left to right%@AB@%!=%@AE@%                      Not equal %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%&%@AE@%                       Bitwise AND                          Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%^%@AE@%                       Bitwise exclusive OR                 Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%|%@AE@%                       Bitwise OR                           Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%&&%@AE@%                      Logical AND                          Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%||%@AE@%                      Logical OR                           Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%? : %@AE@%                    Conditional                          Right to left%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%=%@AE@%                       Assignment                           Right to left%@AB@%*=, /=, %=, +=, -=, %@AE@%    Compound assignment %@AB@%<<=, >>=,  &=, ^=, |=%@AE@%   %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%,%@AE@%                       Comma                                Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE: 114  4496 02 24 37 15 @%
  5132.  
  5133. %@NL@%
  5134. %@NL@%
  5135. %@NL@%
  5136. %@NL@%
  5137. %@NL@%
  5138. %@CR:C6A00070001 @%%@1@%%@AB@%Chapter 7  Preprocessor Directives%@AE@%%@EH@%%@NL@%
  5139. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  5140. %@NL@%
  5141. This chapter describes preprocessor directives─commands that control the
  5142. QuickC compiler. It explains how to insert the contents of one source file
  5143. into another file, how to do text substitutions throughout a file, and how
  5144. to compile different parts of a file in different situations.  %@NL@%
  5145. %@NL@%
  5146. A "preprocessor directive" is a command to the QuickC compiler. Although
  5147. they appear in the same source file as executable statements, preprocessor
  5148. directives aren't statements in the formal sense. Unlike executable
  5149. statements, they are not translated into machine code. Instead, they tell
  5150. the compiler itself to take some action while it translates your source
  5151. program. For instance, an %@AB@%#include%@AE@% directive tells QuickC to insert another
  5152. file into the source file.  %@NL@%
  5153. %@NL@%
  5154. The term "preprocessor" refers to the time when these commands are carried
  5155. out. Like most language compilers, QuickC translates your source program in
  5156. several phases, the first of which is called the "preprocessor phase."
  5157. QuickC first "preprocesses" all the directives in your source program, then
  5158. processes the program's executable statements.  %@NL@%
  5159. %@NL@%
  5160. All preprocessor directives begin with a number sign (%@AB@%#%@AE@%), which must be the
  5161. first nonblank character in the line on which it appears. Since directives
  5162. aren't statements, they don't end with semicolons. You can't put other
  5163. statements or directives on the same line with a preprocessor directive,
  5164. except for a comment, which must appear to the right of the directive.  %@NL@%
  5165. %@NL@%
  5166. Because the compiler reads your source file sequentially, line by line, the
  5167. position of directives is important. A preprocessor directive only affects
  5168. statements that follow it in the source file.  %@NL@%
  5169. %@NL@%
  5170. %@NL@%
  5171. %@2@%%@CR:C6A00070002 @%%@AB@%The #include Directive%@AE@%%@EH@%%@NL@%
  5172. %@NL@%
  5173. %@AU@% The #include directive inserts another file in the source file.%@AE@%  %@NL@%
  5174. %@NL@%
  5175. The %@AB@%#include%@AE@% directive inserts the contents of another file into your source
  5176. file. The inserted file is called an include file or header file.  %@NL@%
  5177. %@NL@%
  5178. When the compiler encounters an %@AB@%#include%@AE@%, it searches for the file named in
  5179. the directive. This directive makes QuickC look for the standard include
  5180. file STDIO.H:  %@NL@%
  5181. %@NL@%
  5182. %@AS@%  #include <stdio.h>%@AE@%%@NL@%
  5183. %@NL@%
  5184. If the designated file is found, the compiler inserts its contents at the
  5185. spot where the %@AB@%#include%@AE@% directive appears. Figure 7.1 illustrates a program
  5186. SAMPLE.C that includes the file STDIO.H.  %@NL@%
  5187. %@NL@%
  5188. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  5189. %@NL@%
  5190. When QuickC compiles the SAMPLE.C program shown in Figure 7.1, it inserts
  5191. the contents of file STDIO.H into SAMPLE.C at the spot marked by the
  5192. %@AB@%#include%@AE@% directive.  %@NL@%
  5193. %@NL@%
  5194. Most include files contain commonly used declarations and definitions.
  5195. Standard include files, supplied with QuickC, contain declarations and
  5196. definitions for QuickC library routines. You can also write include files of
  5197. your own.  %@NL@%
  5198. %@NL@%
  5199. Standard include files end with the .H file extension (STDIO.H is an
  5200. example). You can use any extension for include files you create, but most
  5201. programmers stick with the .H extension.  %@NL@%
  5202. %@NL@%
  5203. ────────────────────────────────────────────────────────────────────────────%@NL@%
  5204. NOTE
  5205.  
  5206. %@AI@%In some languages, it's common to put executable statements, as well as
  5207. %@AI@%declarations and definitions, in include files. This practice is legal but
  5208. %@AI@%not recommended in QuickC. Microsoft debugging tools such as the Microsoft
  5209. %@AI@%CodeView(R) debugger may not recognize executable statements in include
  5210. %@AI@%files.%@AE@%%@NL@%
  5211. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  5212. %@NL@%
  5213. The %@AB@%#include%@AE@% directive doesn't support wild cards, so you can't insert a
  5214. group of related files with a single directive. Each %@AB@%#include%@AE@% directive
  5215. inserts only one file.  %@NL@%
  5216. %@NL@%
  5217. Include files can be nested. For instance, the source program SAMPLE.C might
  5218. include a file named INOUT.H. The INOUT.H file, in turn, might contain a
  5219. second %@AB@%#include%@AE@% directive that includes a file named KEYBOARD.H. Figure 7.2
  5220. illustrates this process.  %@NL@%
  5221. %@NL@%
  5222. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  5223. %@NL@%
  5224. Although it's rarely necessary to nest include files more than two or three
  5225. levels, nesting may continue up to 10 levels.  %@NL@%
  5226. %@NL@%
  5227. %@NL@%
  5228. %@3@%%@CR:C6A00070003 @%%@AB@%Specifying Include Files%@AE@%%@EH@%%@NL@%
  5229. %@NL@%
  5230. There are two ways to tell QuickC where to search for an include file. You
  5231. may have noticed that the %@AB@%#include%@AE@% directive shown earlier encloses the file
  5232. name STDIO.H in angle brackets (%@AB@%<>%@AE@%). If you enclose the file name in angle
  5233. brackets, as in the directive  %@NL@%
  5234. %@NL@%
  5235. %@AS@%  #include <stdio.h>%@AE@%%@NL@%
  5236. %@NL@%
  5237. the compiler searches the "standard directories" for the file.  %@NL@%
  5238. %@NL@%
  5239. In QuickC, the standard directories are one or more directories that you
  5240. define by a DOS environment variable named INCLUDE. An advantage of
  5241. specifying the standard directories is that QuickC can automatically search
  5242. more than one directory.  %@NL@%
  5243. %@NL@%
  5244. Alternatively, you can enclose the file name in double quotes, in the
  5245. following manner,  %@NL@%
  5246. %@NL@%
  5247. %@AS@%  #include "myfile.h"%@AE@%%@NL@%
  5248. %@NL@%
  5249. to cause QuickC to start searching in the directory that contains the
  5250. current source file. If the target file isn't in that directory, the
  5251. compiler searches the standard directories.  %@NL@%
  5252. %@NL@%
  5253. ────────────────────────────────────────────────────────────────────────────%@NL@%
  5254. NOTE
  5255.  
  5256. %@AI@%You can specify additional directories on the DOS command line when you
  5257. %@AI@%invoke QuickC with the QCL command. See Chapter 1, "Creating Executable
  5258. %@AI@%Programs," in the %@AI@%Microsoft QuickC Tool Kit%@AE@%%@AI@%.%@AE@%%@AE@%%@NL@%
  5259. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  5260. %@NL@%
  5261. %@NL@%
  5262. %@2@%%@CR:C6A00070004 @%%@AB@%The #define and #undef Directives%@AE@%%@EH@%%@NL@%
  5263. %@NL@%
  5264. The %@AB@%#define%@AE@% directive performs a text substitution in the source file. This
  5265. directive has two main uses: simple text replacement and creation of
  5266. function-like macros. It is also used with the %@AB@%#undef%@AE@% directive to control
  5267. conditional compilation, as we'll discuss later.  %@NL@%
  5268. %@NL@%
  5269. %@NL@%
  5270. %@3@%%@CR:C6A00070005 @%%@AB@%Simple Text Replacement%@AE@%%@EH@%%@NL@%
  5271. %@NL@%
  5272. %@AU@% The #define directive works  like the search and replace  function of a
  5273. %@AU@%word processor.%@AE@%  %@NL@%
  5274. %@NL@%
  5275. At the simplest level, the %@AB@%#define%@AE@% directive works much like the "search and
  5276. replace" function of a word processor, replacing one piece of text in the
  5277. source file with another piece of text. The %@AB@%#define%@AE@% directive is commonly
  5278. used to create a symbolic constant─a meaningful name for a "magic number"
  5279. whose meaning might not otherwise be apparent. This improves the program's
  5280. readability.  %@NL@%
  5281. %@NL@%
  5282. For instance, in the VOLUME.C program in Chapter 1, "Anatomy of a C
  5283. Program," the directive  %@NL@%
  5284. %@NL@%
  5285. %@AS@%  #define PI 3.14%@AE@%%@NL@%
  5286. %@NL@%
  5287. defines a symbolic constant named %@AS@% PI%@AE@%. The directive causes QuickC to
  5288. replace every occurrence of the text %@AS@% PI %@AE@% in the VOLUME.C source program
  5289. with the text %@AS@% 3.14%@AE@%. For example, when the compiler encounters the program
  5290. line  %@NL@%
  5291. %@NL@%
  5292. %@AS@%  result = 4 * PI * result;%@AE@%%@NL@%
  5293. %@NL@%
  5294. it expands the line to read  %@NL@%
  5295. %@NL@%
  5296. %@AS@%  result = 4 * 3.14 * result;%@AE@%%@NL@%
  5297. %@NL@%
  5298. Besides making your program more readable, symbolic constants can streamline
  5299. its maintenance. For instance, say you later decide to use 3.14159265 rather
  5300. than 3.14 in VOLUME.C. All you need to change is one %@AB@%#define%@AE@% directive at
  5301. the beginning of the program.  %@NL@%
  5302. %@NL@%
  5303. The replacement text can be longer than the 3.14159265 we used above. A
  5304. replacement text can't be longer than 512 bytes in QuickC, but you'll
  5305. rarely, if ever, have to worry about this limit.  %@NL@%
  5306. %@NL@%
  5307. %@NL@%
  5308. %@3@%%@CR:C6A00070006 @%%@AB@%Function-Like Macros%@AE@%%@EH@%%@NL@%
  5309. %@NL@%
  5310. %@AU@% A function-like macro accepts arguments, like a function.%@AE@%  %@NL@%
  5311. %@NL@%
  5312. Some languages use the term "macro" when referring to replacement text. In
  5313. C, a macro can do more than simply replace text. It can also accept
  5314. arguments in much the same way that a function does. In this case the
  5315. replacement text is called a "function-like macro."  %@NL@%
  5316. %@NL@%
  5317. A well-designed macro can be every bit as useful as a function. In fact,
  5318. some C library routines are implemented as macros rather than C functions.  %@NL@%
  5319. %@NL@%
  5320. The MACRO.C program below has a macro that works like the %@AB@%abs%@AE@% library
  5321. function, returning the absolute value of any integer. The macro uses the
  5322. conditional operator (%@AB@%? :%@AE@%), which we explained in Chapter 6.  %@NL@%
  5323. %@NL@%
  5324. %@AS@%  /* MACRO.C: Demonstrate macros. */
  5325. %@AS@%  
  5326. %@AS@%  #include <stdio.h>
  5327. %@AS@%  #define ABS(value)  ( (value) >= 0 ? (value) : -(value) )
  5328. %@AS@%  
  5329. %@AS@%  main()
  5330. %@AS@%  {
  5331. %@AS@%     int val = -20;
  5332. %@AS@%     printf( "result = %d\n", ABS(val) );
  5333. %@AS@%  }%@AE@%%@NL@%
  5334. %@NL@%
  5335. The %@AS@% ABS %@AE@% macro behaves much like a function. You can "call" it by name,
  5336. passing it an argument you want to process. The macro is defined in the
  5337. following program line:  %@NL@%
  5338. %@NL@%
  5339. %@AS@%  #define ABS(value)  ( (value) >= 0 ? (value) : -(value) )%@AE@%%@NL@%
  5340. %@NL@%
  5341. The parameter %@AS@% value %@AE@% appears four times in the macro─once in the macro name
  5342. %@AS@% ABS %@AE@% and three times in the replacement text that follows the name.  %@NL@%
  5343. %@NL@%
  5344. %@AU@% Always enclose macro parameters in parentheses.%@AE@%  %@NL@%
  5345. %@NL@%
  5346. To avoid unwanted side effects, you should always enclose macro parameters
  5347. in parentheses. If the parameter passed to the %@AS@% ABS %@AE@% macro is an expression
  5348. containing operators, the lack of parentheses could cause
  5349. operator-precedence errors. See the section "Omitting Parentheses from Macro
  5350. Arguments" in Chapter 10, "Programming Pitfalls."  %@NL@%
  5351. %@NL@%
  5352. Function-like macros, like other %@AB@%#define%@AE@% directives, are expanded during the
  5353. preprocessor phase of compilation, before QuickC translates any executable
  5354. statements. When QuickC encounters the line  %@NL@%
  5355. %@NL@%
  5356. %@AS@%  printf( "result= %d\n", ABS(val) );%@AE@%%@NL@%
  5357. %@NL@%
  5358. it expands it to read:  %@NL@%
  5359. %@NL@%
  5360. %@AS@%  printf( "result= %d\n", ( (val) >= 0 ? (val) : -(val) ) );%@AE@%%@NL@%
  5361. %@NL@%
  5362. %@AU@% Macros can improve readability.%@AE@%  %@NL@%
  5363. %@NL@%
  5364. A macro can improve a program's readability by describing the nature of an
  5365. operation while hiding its complex details. Most people find the first of
  5366. the two statements above easier to understand than the expanded version.  %@NL@%
  5367. %@NL@%
  5368. %@AU@% Macros are faster than functions but can make  a program bigger.%@AE@%  %@NL@%
  5369. %@NL@%
  5370. Function-like macros are fast, too. Since a macro creates in-line code, it
  5371. doesn't have the overhead associated with a normal function call. On the
  5372. other hand, each use of a macro inserts the same code in your program,
  5373. whereas a function definition occurs only once. So while macros can be
  5374. faster than functions, they can also bloat the size of the executable file.
  5375. %@NL@%
  5376. %@NL@%
  5377. %@NL@%
  5378. %@3@%%@CR:C6A00070007 @%%@AB@%The #undef Directive%@AE@%%@EH@%%@NL@%
  5379. %@NL@%
  5380. The "%@AB@%#undef%@AE@% directive" is related to %@AB@%#define%@AE@%. As the name suggests, %@AB@%#undef%@AE@%
  5381. removes ("undefines") a name that was created with %@AB@%#define%@AE@%. For instance, if
  5382. you create the symbolic constant %@AS@% PI %@AE@% with the %@AB@%#define%@AE@% directive,  %@NL@%
  5383. %@NL@%
  5384. %@AS@%  #define PI  3.14%@AE@%%@NL@%
  5385. %@NL@%
  5386. you can then remove the name %@AS@% PI %@AE@% with the following %@AB@%#undef%@AE@% directive:  %@NL@%
  5387. %@NL@%
  5388. %@AS@%  #undef PI%@AE@%%@NL@%
  5389. %@NL@%
  5390. You can use %@AB@%#define%@AE@% and %@AB@%#undef%@AE@% to create a name that has meaning in only
  5391. part of a source program. The next two sections explain why you might want
  5392. to do this.  %@NL@%
  5393. %@NL@%
  5394. %@NL@%
  5395. %@2@%%@CR:C6A00070008 @%%@AB@%Conditional Directives%@AE@%%@EH@%%@NL@%
  5396. %@NL@%
  5397. %@AU@% Conditional directives are useful for making different versions of a
  5398. %@AU@%program.%@AE@%  %@NL@%
  5399. %@NL@%
  5400. Conditional directives can make QuickC skip part of a source file. They are
  5401. used primarily to create different versions of a program. While developing a
  5402. program, for instance, you might want to include debugging code at some
  5403. times but not others. Or, if you plan to move a program to some other
  5404. machine, you can compile machine-specific sections of code only for a
  5405. certain machine.  %@NL@%
  5406. %@NL@%
  5407. The C-language conditional directives are listed below.  %@NL@%
  5408. %@NL@%
  5409. %@AB@%#if%@AE@%%@AB@%%@AE@%                               %@AB@%#endif%@AE@%%@AB@%%@AE@%
  5410. %@AB@%#else%@AE@%%@AB@%%@AE@%                             %@AB@%#ifdef%@AE@%%@AB@%%@AE@%
  5411. %@AB@%#elif%@AE@%                             %@AB@%#ifndef%@AE@%
  5412.  
  5413. ────────────────────────────────────────────────────────────────────────────%@NL@%
  5414. NOTE 
  5415.  
  5416. %@AI@%The%@AB@% #ifdef%@AE@%%@AI@% and %@AE@%%@AI@%%@AB@%#ifndef %@AE@%%@AE@%%@AI@%directives are obsolete under the ANSI C standard;
  5417. %@AI@%see "The defined Operator" below.%@AE@%%@AE@%%@NL@%
  5418. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  5419. %@NL@%
  5420. %@AU@% The #if directive works  like the if statement.%@AE@%  %@NL@%
  5421. %@NL@%
  5422. The %@AB@%#if%@AE@% and %@AB@%#endif%@AE@% directives work like an %@AB@%if%@AE@% statement, allowing you to
  5423. compile a block of source code if a given condition is true. The %@AB@%#if%@AE@%
  5424. directive is followed by a constant expression, which the compiler tests at
  5425. compile time. If the expression is false, the compiler skips every line
  5426. between the %@AB@%#if%@AE@% and the next %@AB@%#endif%@AE@%.  %@NL@%
  5427. %@NL@%
  5428. The example below calls the %@AS@% display %@AE@% function only if the name %@AS@% DEBUG %@AE@% was
  5429. previously defined as 1 (with %@AB@%#define%@AE@%).  %@NL@%
  5430. %@NL@%
  5431. %@AS@%  #if DEBUG == 1
  5432. %@AS@%     display( debuginfo );
  5433. %@AS@%  #endif%@AE@%%@NL@%
  5434. %@NL@%
  5435. Here, the "conditional block" is a single line (the %@AS@% display %@AE@% function
  5436. call). A conditional block can contain any number of valid C program lines,
  5437. including preprocessor directives as well as executable statements.  %@NL@%
  5438. %@NL@%
  5439. The test expression for a conditional directive can be almost any expression
  5440. that evaluates to a constant, with a few minor exceptions (the expression
  5441. can't use the %@AB@%sizeof%@AE@% operator, type casts, or the %@AB@%float%@AE@% and %@AB@%enum%@AE@% types).  %@NL@%
  5442. %@NL@%
  5443. %@AU@% The #else directive works  like the else keyword.%@AE@%  %@NL@%
  5444. %@NL@%
  5445. The %@AB@%#else%@AE@% and %@AB@%#elif%@AE@% directives work like the %@AB@%else%@AE@% keyword and can perform
  5446. more complex conditional tests. For example, you could use code like that in
  5447. the following example to build different versions of a program for various
  5448. IBM PC computers, including different files for each computer.  %@NL@%
  5449. %@NL@%
  5450. %@AS@%  #if XT == 1
  5451. %@AS@%     #include "XT.H"
  5452. %@AS@%  #elif AT == 1
  5453. %@AS@%     #include "AT.H"
  5454. %@AS@%  #else
  5455. %@AS@%     #include "PS2.H"
  5456. %@AS@%  #endif%@AE@%%@NL@%
  5457. %@NL@%
  5458. The code includes the file XT.H if the name %@AS@% XT %@AE@% is defined as 1 and it
  5459. includes the file AT.H if the name %@AS@% AT %@AE@% is defined as 1. If both %@AS@% XT %@AE@% and %@AS@%
  5460. %@AS@%AT %@AE@% are undefined, the third conditional block executes, causing QuickC to
  5461. include the file PS2.H.  %@NL@%
  5462. %@NL@%
  5463. You can nest conditional directives in the same way as you would conditional
  5464. C language statements.  %@NL@%
  5465. %@NL@%
  5466. %@NL@%
  5467. %@3@%%@CR:C6A00070009 @%%@AB@%The defined Operator%@AE@%%@EH@%%@NL@%
  5468. %@NL@%
  5469. %@AU@% The defined operator  tests whether a name  has been defined.%@AE@%  %@NL@%
  5470. %@NL@%
  5471. The test expression of an %@AB@%#if%@AE@% or %@AB@%#elif%@AE@% directive can use the %@AB@%defined%@AE@%
  5472. operator to test whether a name has been defined. You can use this feature,
  5473. along with %@AB@%#define%@AE@% and %@AB@%#undef%@AE@%, to turn various parts of a program on and
  5474. off, compiling different parts under different conditions.  %@NL@%
  5475. %@NL@%
  5476. The %@AB@%defined%@AE@% operator is true if its argument has been defined and false
  5477. otherwise. A name is considered defined if it has been created with %@AB@%#define%@AE@%
  5478. (and not later removed with %@AB@%#undef%@AE@%).  %@NL@%
  5479. %@NL@%
  5480. The DEFINED.C program below prints %@AS@% Hi %@AE@% because the name %@AS@% DEBUG %@AE@% is defined
  5481. when the compiler encounters the %@AB@%#if defined%@AE@% directive.  %@NL@%
  5482. %@NL@%
  5483. %@AS@%  /* DEFINED.C: Demonstrate defined operator. */
  5484. %@AS@%  
  5485. %@AS@%  #define DEBUG 12345
  5486. %@AS@%  
  5487. %@AS@%  main()
  5488. %@AS@%  {
  5489. %@AS@%     #if defined( DEBUG )
  5490. %@AS@%        printf( "Hi\n" );
  5491. %@AS@%     #endif
  5492. %@AS@%  }%@AE@%%@NL@%
  5493. %@NL@%
  5494. The %@AB@%defined%@AE@% operator tests only whether a name is defined, not whether it
  5495. has a certain value. Thus, the DEFINED.C program will print %@AS@% Hi %@AE@% no matter
  5496. what value is assigned %@AS@% DEBUG%@AE@%. You could substitute the directive  %@NL@%
  5497. %@NL@%
  5498. %@AS@%  #define DEBUG 0%@AE@%%@NL@%
  5499. %@NL@%
  5500. to define %@AS@% DEBUG %@AE@% as 0, or the directive  %@NL@%
  5501. %@NL@%
  5502. %@AS@%  #define DEBUG%@AE@%%@NL@%
  5503. %@NL@%
  5504. to define %@AS@% DEBUG %@AE@% as having no value at all. Both directives define the name
  5505. %@AS@% DEBUG%@AE@%, so the program would print %@AS@% Hi %@AE@% in both cases.  %@NL@%
  5506. %@NL@%
  5507. You can use the logical NOT operator (%@AB@%!%@AE@%) to reverse the logic of an %@AB@%#if
  5508. %@AB@%defined%@AE@% directive. (Logical operators are explained in Chapter 6.) The code
  5509. %@NL@%
  5510. %@NL@%
  5511. %@AS@%  #if !defined( DEBUG )
  5512. %@AS@%     printf( "Hi\n");
  5513. %@AS@%  #endif%@AE@%%@NL@%
  5514. %@NL@%
  5515. prints %@AS@% Hi %@AE@% if %@AS@% DEBUG %@AE@% is not currently defined.  %@NL@%
  5516. %@NL@%
  5517. A plain %@AB@%#if%@AE@% directive treats undefined names a little differently than does
  5518. an %@AB@%#if defined%@AE@% directive. If a name is not currently defined, the %@AB@%#if%@AE@%
  5519. directive treats the name as having the value 0.  %@NL@%
  5520. %@NL@%
  5521. In the following code, the %@AB@%#if%@AE@% directive explicitly tests whether %@AS@% DEBUG %@AE@%
  5522. equals 0.  %@NL@%
  5523. %@NL@%
  5524. %@AS@%  #undef DEBUG
  5525. %@AS@%  #if DEBUG == 0
  5526. %@AS@%     printf( "Hi\n" );
  5527. %@AS@%  #endif%@AE@%%@NL@%
  5528. %@NL@%
  5529. The result is the same as that of the previous example.  %@NL@%
  5530. %@NL@%
  5531. ────────────────────────────────────────────────────────────────────────────%@NL@%
  5532. NOTE
  5533.  
  5534. %@AI@%The %@AB@%defined%@AE@%%@AI@% operator is new under the ANSI C standard. You may see older
  5535. %@AI@%programs that use the older directives %@AE@%%@AI@%%@AB@%#ifdef %@AE@%%@AE@%%@AI@%and %@AE@%%@AI@%%@AB@%#ifndef %@AE@%%@AE@%%@AI@%for the same
  5536. %@AI@%purpose. These directives are obsolete, but QuickC version 2.5 supports them
  5537. %@AI@%for the sake of compatibility. The #%@AE@%%@AI@%%@AB@%ifdef %@AE@%%@AE@%%@AI@%directive is followed by a name
  5538. %@AI@%(not in parentheses) and works the same as %@AE@%%@AI@%%@AB@%#if %@AE@%%@AE@%%@AI@%with %@AE@%%@AI@%%@AB@%defined%@AE@%%@AE@%%@AI@%. If the given
  5539. %@AI@%name has been defined, %@AE@%%@AI@%%@AB@%#ifdef %@AE@%%@AE@%%@AI@%is true. The %@AE@%%@AI@%%@AB@%#ifndef %@AE@%%@AE@%%@AI@%directive is the opposite
  5540. %@AI@%of %@AE@%%@AI@%%@AB@%#ifdef%@AE@%%@AE@%%@AI@%. It is true if the given name is not currently defined.%@AE@%%@AE@%%@NL@%
  5541. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  5542. %@NL@%
  5543. %@NL@%
  5544. %@2@%%@CR:C6A00070010 @%%@AB@%Pragmas%@AE@%%@EH@%%@NL@%
  5545. %@NL@%
  5546. %@AU@%  Pragmas are implementationspecific compiler commands.%@AE@%  %@NL@%
  5547. %@NL@%
  5548. Although portability is a hallmark of C, the language's creators recognized
  5549. that every C compiler will need to support some features unique to its host
  5550. machine. The "%@AB@%#pragma%@AE@% directive" offers a way for each C compiler to offer
  5551. machine-specific features while retaining overall compatibility with the C
  5552. language. Since pragmas are machine-specific by definition, they can be─and
  5553. usually are─different for every C compiler.  %@NL@%
  5554. %@NL@%
  5555. Pragmas have the same general syntax as preprocessor directives. The pragma
  5556. must begin with a number sign (%@AB@%#%@AE@%) and it can't share a line with other
  5557. directives or statements except a comment, which must appear to the right of
  5558. the pragma.  %@NL@%
  5559. %@NL@%
  5560. QuickC supports four pragmas: %@AB@%check_stack%@AE@%, %@AB@%check_pointer%@AE@%, %@AB@%message%@AE@%, and %@AB@%pack%@AE@%.
  5561. Each of these pragmas is described in online help.  %@NL@%
  5562. %@NL@%
  5563. Some pragmas take arguments, which come after the %@AB@%#pragma%@AE@% keyword. In the
  5564. following code, the %@AB@%message%@AE@% pragma displays different messages during
  5565. compilation depending on the outcome of an %@AB@%#if%@AE@% test:  %@NL@%
  5566. %@NL@%
  5567. %@AS@%  #if XT == 1
  5568. %@AS@%     #pragma message( "Building XT version" )
  5569. %@AS@%  #elif AT == 1
  5570. %@AS@%     #pragma message( "Building AT version" )
  5571. %@AS@%  #endif%@AE@%%@NL@%
  5572. %@NL@%
  5573. The message displayed by the %@AB@%message%@AE@% pragma is visible only if you compile
  5574. from the DOS command line with the QCL command.  %@NL@%
  5575. %@NL@%
  5576. %@NL@%
  5577. %@NL@%
  5578. %@NL@%
  5579. %@NL@%
  5580. %@NL@%
  5581. %@CR:C6A00080001 @%%@1@%%@AB@%Chapter 8  Pointers%@AE@%%@EH@%%@NL@%
  5582. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  5583. %@NL@%
  5584. The next two chapters explain pointers─a large and important topic in C.
  5585. This chapter explains fundamental techniques: how to use pointers with
  5586. various data types and pass them to functions. In Chapter 9, "Advanced
  5587. Pointers," we'll explore more advanced pointer techniques, such as multiple
  5588. indirection.  %@NL@%
  5589. %@NL@%
  5590. If you have never used pointers before, you may want to read this chapter
  5591. now and then turn to Chapter 9 after you have had some practice using
  5592. pointers in your own programs.  %@NL@%
  5593. %@NL@%
  5594. %@AU@% Don't panic!%@AE@%  %@NL@%
  5595. %@NL@%
  5596. There's a lot of new information in these two chapters. Don't be discouraged
  5597. if you don't grasp it all on a first reading. The idea behind a pointer is
  5598. simple, but some advanced pointer techniques are not so easy to follow at
  5599. first.  %@NL@%
  5600. %@NL@%
  5601. %@NL@%
  5602. %@2@%%@CR:C6A00080002 @%%@AB@%Using Pointers in C%@AE@%%@EH@%%@NL@%
  5603. %@NL@%
  5604. Almost every real-world C program uses pointers in some way or another. Much
  5605. of the usefulness of pointers stems from the fact that in C all function
  5606. arguments are passed by value. Because a function only receives local copies
  5607. of such arguments, it can't change the original values that the arguments
  5608. represent. Pointers make this possible.  %@NL@%
  5609. %@NL@%
  5610. Here are some common uses of pointers:  %@NL@%
  5611. %@NL@%
  5612. %@NL@%
  5613.   ■   Manipulating strings%@NL@%
  5614. %@NL@%
  5615.   ■   Passing command-line arguments to a program at run time%@NL@%
  5616. %@NL@%
  5617.   ■   Returning more than one value from a function%@NL@%
  5618. %@NL@%
  5619.   ■   Accessing variables that wouldn't otherwise be visible to a function%@NL@%
  5620. %@NL@%
  5621.   ■   Manipulating an array by moving pointers to its elements instead of
  5622.       using array subscripting%@NL@%
  5623. %@NL@%
  5624.   ■   Accessing the address of a memory area that your program allocates at
  5625.       run time%@NL@%
  5626. %@NL@%
  5627.   ■   Passing the address of one function to another function%@NL@%
  5628. %@NL@%
  5629. %@NL@%
  5630. %@NL@%
  5631. %@2@%%@CR:C6A00080003 @%%@AB@%Pointers to Simple Variables%@AE@%%@EH@%%@NL@%
  5632. %@NL@%
  5633. %@AU@% A pointer variable contains the address of a data object.%@AE@%  %@NL@%
  5634. %@NL@%
  5635. Although pointers have many different uses, it takes only a few words to say
  5636. what a pointer is. A "pointer" is a variable that contains the address of
  5637. some other data object─usually a variable. Because a pointer contains the
  5638. other variable's address, it is said to "point to" that variable.  %@NL@%
  5639. %@NL@%
  5640. This section uses the program POINTER.C to demonstrate the basic mechanics
  5641. of pointers─how to declare and initialize a pointer and use it to access a
  5642. simple variable:  %@NL@%
  5643. %@NL@%
  5644. %@AS@%  /* POINTER.C: Demonstrate pointer basics. */
  5645. %@AS@%  
  5646. %@AS@%  #include <stdio.h>
  5647. %@AS@%  
  5648. %@AS@%  main()
  5649. %@AS@%  {
  5650. %@AS@%     int val = 25;
  5651. %@AS@%     int *ptr;
  5652. %@AS@%     ptr = &val;
  5653. %@AS@%     printf( " val = %d\n", val );
  5654. %@AS@%     printf( "*ptr = %d\n\n", *ptr );
  5655. %@AS@%     printf( "&val = %u\n", &val );
  5656. %@AS@%     printf( " ptr = %d\n", ptr );
  5657. %@AS@%  }%@AE@%%@NL@%
  5658. %@NL@%
  5659. Here is the output from POINTER.C:  %@NL@%
  5660. %@NL@%
  5661. %@AS@%  val = 25
  5662. %@AS@%  *ptr = 25
  5663. %@AS@%  
  5664. %@AS@%  &val = 5308
  5665. %@AS@%   ptr = 5308%@AE@%%@NL@%
  5666. %@NL@%
  5667. (The third and fourth output lines show addresses. These may differ when you
  5668. run POINTER.C depending on factors such as available memory.)  %@NL@%
  5669. %@NL@%
  5670. POINTER.C creates a pointer variable named %@AS@% ptr %@AE@% and makes %@AS@% ptr %@AE@% point to an
  5671. %@AB@%int%@AE@% variable named %@AS@% val%@AE@%. Then it prints the two values to show that %@AS@% ptr %@AE@%
  5672. can access the value stored in %@AS@% val%@AE@%. The program goes on to print the
  5673. address where %@AS@% val %@AE@% is stored and the address contained in %@AS@% ptr%@AE@%, to show
  5674. they are the same.  %@NL@%
  5675. %@NL@%
  5676. %@NL@%
  5677. %@3@%%@CR:C6A00080004 @%%@AB@%Declaring a Pointer Variable%@AE@%%@EH@%%@NL@%
  5678. %@NL@%
  5679. Like any variable, a pointer variable must be declared before it is used,
  5680. and its value can change in the course of a program. A pointer variable can
  5681. have any legal variable name. Here is the pointer declaration from
  5682. POINTER.C:  %@NL@%
  5683. %@NL@%
  5684. %@AS@%  int *ptr;%@AE@%%@NL@%
  5685. %@NL@%
  5686. This declaration states the program has a pointer variable named %@AS@% ptr %@AE@% that
  5687. can point to a data object of the %@AB@%int%@AE@% type.  %@NL@%
  5688. %@NL@%
  5689. Notice the similarity to a simple variable declaration. As in other cases,
  5690. the declaration gives a type (%@AB@%int%@AE@%) and name (%@AS@% ptr %@AE@%) for the variable.  %@NL@%
  5691. %@NL@%
  5692. %@AU@% Use the indirection  operator (%@AE@% *) to declare  a pointer variable.  %@NL@%
  5693. %@NL@%
  5694. The indirection operator (%@AB@%*%@AE@%) in front of the name %@AS@% ptr %@AE@% shows this variable
  5695. is a pointer. This operator has two different uses in C. In declarations,
  5696. such as the one above, it simply means "this is a pointer." In other
  5697. contexts, as we'll elaborate throughout this chapter, it means
  5698. indirection─using the data object that a pointer points to.  %@NL@%
  5699. %@NL@%
  5700. %@AU@% A pointer declaration shows %@AE@%%@AB@%what%@AE@% type of data object a pointer references.
  5701. %@NL@%
  5702. %@NL@%
  5703. A pointer doesn't have a type in the same sense as other variables. When you
  5704. declare a simple variable, the type specifier shows what type of value the
  5705. variable stores. When you declare a pointer variable, the type specifier
  5706. shows what type of data object the pointer %@AI@%points to%@AE@%.  %@NL@%
  5707. %@NL@%
  5708. Thus, in POINTER.C the declaration of the variable %@AS@% val %@AE@% indicates %@AS@% val %@AE@%
  5709. stores a value of the type %@AB@%int%@AE@%,  %@NL@%
  5710. %@NL@%
  5711. %@AS@%  int val = 25;%@AE@%%@NL@%
  5712. %@NL@%
  5713. while the declaration of the variable %@AS@% ptr %@AE@% indicates it %@AI@%points to%@AE@% a data
  5714. object of the type %@AB@%int%@AE@%:  %@NL@%
  5715. %@NL@%
  5716. %@AS@%  int *ptr;%@AE@%%@NL@%
  5717. %@NL@%
  5718. To declare pointers to other types of variables, you can use whatever type
  5719. specifier is appropriate. These statements, for instance, declare pointers
  5720. to %@AB@%char%@AE@% and %@AB@%float%@AE@% variables:  %@NL@%
  5721. %@NL@%
  5722. %@AS@%  char *c_ptr, *ch;
  5723. %@AS@%  float *f_pointer;%@AE@%%@NL@%
  5724. %@NL@%
  5725. Note that if you declare more than one pointer variable in the same line,
  5726. each name must be preceded by the indirection operator. The first line in
  5727. the previous example declares two pointer variables: %@AS@% c_ptr %@AE@% and %@AS@% ch%@AE@%. Each
  5728. pointer can point to an object of the %@AB@%char%@AE@% type. If you omit the second
  5729. indirection operator from the first line,  %@NL@%
  5730. %@NL@%
  5731. %@AS@%  char *c_ptr, ch;%@AE@%%@NL@%
  5732. %@NL@%
  5733. the line declares a pointer variable named %@AS@% c_ptr %@AE@% and an ordinary %@AB@%char%@AE@%
  5734. variable named %@AS@% ch%@AE@%.  %@NL@%
  5735. %@NL@%
  5736. %@AU@% A pointer declared with type %@AE@%%@AB@%void%@AE@% can point to any type of data object.  %@NL@%
  5737. %@NL@%
  5738. In most cases a pointer points to a particular type of object, such as an
  5739. %@AB@%int%@AE@%. You can also declare a pointer with type %@AB@%void%@AE@%, which allows it to point
  5740. to any type of object.  %@NL@%
  5741. %@NL@%
  5742. One use of %@AB@%void%@AE@% pointers is to write a general-purpose function, such as a
  5743. sort, that can operate on data of more than one type. Each time you use a
  5744. %@AB@%void%@AE@% pointer, you must perform an explicit type cast to show what type of
  5745. object it points to on that occasion.  %@NL@%
  5746. %@NL@%
  5747. Figure 8.1 shows the relationship between %@AS@% val %@AE@% and %@AS@% ptr %@AE@% in POINTER.C,
  5748. immediately after %@AS@% ptr %@AE@% has been declared. The figure shows that the
  5749. variable %@AS@% val %@AE@% is stored at memory location 5308, as in the output shown
  5750. above. Again, the actual address may differ when you run POINTER.C.  %@NL@%
  5751. %@NL@%
  5752. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  5753. %@NL@%
  5754. Figure 8.1 uses question marks to show that the contents of %@AS@% ptr %@AE@% are
  5755. undefined at this stage in the program. Like any other variable that has
  5756. been declared but not initialized, the contents of %@AS@% ptr %@AE@% are unknown. You
  5757. must take special care not to use pointers that have not been initialized,
  5758. since an uninitialized pointer might point anywhere in memory─including
  5759. sensitive operating-system addresses.  %@NL@%
  5760. %@NL@%
  5761. ────────────────────────────────────────────────────────────────────────────%@NL@%
  5762. %@AU@%WARNING%@AE@%%@NL@%
  5763. %@NL@%
  5764. Because a pointer can potentially access any memory address, using an
  5765. uninitialized pointer can have drastic consequences.%@NL@%
  5766. ────────────────────────────────────────────────────────────────────────────%@NL@%
  5767. %@NL@%
  5768. %@NL@%
  5769. %@3@%%@CR:C6A00080005 @%%@AB@%How Pointers Are Stored%@AE@%%@EH@%%@NL@%
  5770. %@NL@%
  5771. Figure 8.1 also shows that while a pointer is a special kind of variable, it
  5772. is not a mysterious entity floating in limbo. A pointer is a true variable
  5773. whose contents are stored at a specific memory address.  %@NL@%
  5774. %@NL@%
  5775. In POINTER.C we don't care precisely where the pointer's contents are
  5776. stored─the compiler handles that detail for us, as it does so many others.
  5777. So Figure 8.1 does not include the address of the storage for %@AS@% ptr%@AE@%. It does
  5778. show, however, that the pointer is stored in two bytes, the same amount of
  5779. memory needed to store an %@AB@%int%@AE@% value.  %@NL@%
  5780. %@NL@%
  5781. ────────────────────────────────────────────────────────────────────────────%@NL@%
  5782. NOTE
  5783.  
  5784. %@AI@%The actual amount of memory needed to store a pointer variable depends on
  5785. %@AI@%the current "memory model." In the %@AI@%small%@AE@%%@AI@% memory model─the default for QuickC
  5786. %@AI@%version 2.5─a pointer is stored in two bytes. In some larger memory models,
  5787. %@AI@%a pointer is stored in four bytes. For purposes of discussion, this chapter
  5788. %@AI@%and the following chapter assume the small memory model. Appendix B,
  5789. %@AI@%"Working with QuickC Memory Models," in the Microsoft QuickC Tool Kit%@AE@%%@AI@%
  5790. %@AI@%discusses memory models.%@AE@%%@AE@%%@NL@%
  5791. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  5792. %@NL@%
  5793. %@NL@%
  5794. %@3@%%@CR:C6A00080006 @%%@AB@%Initializing a Pointer Variable%@AE@%%@EH@%%@NL@%
  5795. %@NL@%
  5796. The next step in the POINTER.C program is to initialize the pointer variable
  5797. %@AS@% ptr%@AE@%, making it point to some meaningful address in memory:  %@NL@%
  5798. %@NL@%
  5799. %@AS@%  ptr = &val;%@AE@%%@NL@%
  5800. %@NL@%
  5801. The "address-of operator" (%@AB@%&%@AE@%) gives the address of the name it precedes. So
  5802. in plain English the above statement says, "assign the address of %@AS@% val %@AE@% to %@AS@%
  5803. %@AS@%ptr%@AE@%."  %@NL@%
  5804. %@NL@%
  5805. After its initialization, the variable %@AS@% ptr %@AE@% points to %@AS@% val %@AE@% in the sense
  5806. that it contains the address where %@AS@% val %@AE@% is stored.  %@NL@%
  5807. %@NL@%
  5808. The output from POINTER.C shows that %@AS@% ptr %@AE@% contains the address of %@AS@% val%@AE@%.
  5809. First it prints the address of %@AS@% val %@AE@% using the address-of operator to
  5810. directly obtain the variable's address,  %@NL@%
  5811. %@NL@%
  5812. %@AS@%  &val = 5308%@AE@%%@NL@%
  5813. %@NL@%
  5814. then it prints the contents of %@AS@% ptr%@AE@%: %@AS@%  %@AE@%%@NL@%
  5815. %@NL@%
  5816. %@AS@%  ptr = 5308%@AE@%%@NL@%
  5817. %@NL@%
  5818. The two values are identical. Figure 8.2 shows the relationship of %@AS@% val %@AE@% and
  5819. %@AS@% ptr %@AE@% at this stage in the POINTER.C program.  %@NL@%
  5820. %@NL@%
  5821. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  5822. %@NL@%
  5823. Initialization is especially important for pointers because, as noted
  5824. earlier, they have the potential to point anywhere in memory. If you forget
  5825. to initialize it, or make it point to the wrong place, a pointer can wreak
  5826. havoc with your program or even the operating system itself.  %@NL@%
  5827. %@NL@%
  5828. %@AU@% The target of a pointer  must be present in  memory at run time.%@AE@%  %@NL@%
  5829. %@NL@%
  5830. The pointer in POINTER.C points to a simple %@AB@%int%@AE@% variable. As a general rule,
  5831. pointers can point to any data object that is present in memory at run time.
  5832. This category mainly includes objects for which the program allocates (sets
  5833. aside) memory. Memory can be allocated implicitly, by defining a variable or
  5834. function, or explicitly, by calling a memory-allocating library function
  5835. such as %@AB@%malloc%@AE@%.  %@NL@%
  5836. %@NL@%
  5837. A pointer can't point to program elements such as expressions or %@AB@%register%@AE@%
  5838. variables, which aren't present in addressable memory.  %@NL@%
  5839. %@NL@%
  5840. POINTER.C initializes the pointer %@AS@% ptr %@AE@% by assigning it an address constant
  5841. (the address of %@AS@% val%@AE@%, obtained with the address-of operator). You can also
  5842. assign the value of one pointer to another, as shown here:  %@NL@%
  5843. %@NL@%
  5844. %@AS@%  ptr = ptr1;%@AE@%%@NL@%
  5845. %@NL@%
  5846. If %@AS@% ptr %@AE@% and %@AS@% ptr1 %@AE@% are both pointers, this statement assigns the address
  5847. contained in %@AS@% ptr1 %@AE@% to %@AS@% ptr%@AE@%.  %@NL@%
  5848. %@NL@%
  5849. %@NL@%
  5850. %@3@%%@CR:C6A00080007 @%%@AB@%Using a Pointer Variable%@AE@%%@EH@%%@NL@%
  5851. %@NL@%
  5852. Once %@AS@% ptr %@AE@% points to %@AS@% val%@AE@%, we have two ways to access the %@AB@%int%@AE@% value stored
  5853. in %@AS@% val%@AE@%. The usual way is direct, using the name of %@AS@% val%@AE@%:  %@NL@%
  5854. %@NL@%
  5855. %@AS@%  printf( " val = %d\n", val );%@AE@%%@NL@%
  5856. %@NL@%
  5857. The second way to access %@AS@% val %@AE@% is indirect, using the pointer variable %@AS@% ptr %@AE@%
  5858. and the indirection operator:  %@NL@%
  5859. %@NL@%
  5860. %@AS@%  printf( "*ptr = %d\n\n", *ptr );%@AE@%%@NL@%
  5861. %@NL@%
  5862. Both of the preceding statements print the value of %@AS@% val%@AE@%, confirming that
  5863. you can access the contents of %@AS@% val %@AE@% indirectly as well as directly. Once %@AS@%
  5864. %@AS@%ptr %@AE@% points to %@AS@% val %@AE@% you can use %@AS@% *ptr %@AE@% anywhere that you would use %@AS@% val%@AE@%. %@AS@%  %@AE@%%@NL@%
  5865. %@NL@%
  5866. %@AU@% The indirection operator can obtain the value to which a pointer points.%@AE@%  %@NL@%
  5867. %@NL@%
  5868. Using the indirection operator to access the contents of %@AS@% val %@AE@% is the second
  5869. use of this operator (the first is in declaring pointer variables, as
  5870. explained earlier). When the asterisk appears in front of the name %@AS@% ptr%@AE@%, the
  5871. expression states that you want to use the %@AI@%value the pointer points to%@AE@%, not
  5872. the value of %@AS@% ptr %@AE@% itself.  %@NL@%
  5873. %@NL@%
  5874. The second %@AB@%printf%@AE@% statement in POINTER.C uses the expression %@AS@% *ptr %@AE@% to
  5875. access the value stored in %@AS@% val%@AE@%.  %@NL@%
  5876. %@NL@%
  5877. This use of a pointer is analogous to the %@AB@%PEEK%@AE@% function in QuickBASIC. You
  5878. can just as easily use %@AS@% ptr %@AE@% to change the data in %@AS@% val%@AE@%, an operation that
  5879. somewhat resembles a QuickBASIC %@AB@%POKE%@AE@% statement.  %@NL@%
  5880. %@NL@%
  5881. For instance, if you add the following statements to the end of POINTER.C,  %@NL@%
  5882. %@NL@%
  5883. %@AS@%  *ptr = 3301;
  5884. %@AS@%  printf( "%d\n", val );%@AE@%%@NL@%
  5885. %@NL@%
  5886. the program prints %@AS@% 3301%@AE@%.  %@NL@%
  5887. %@NL@%
  5888. Figure 8.3 shows the relationship between %@AS@% ptr %@AE@% and %@AS@% val %@AE@% after executing
  5889. the previous two statements.  %@NL@%
  5890. %@NL@%
  5891. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  5892. %@NL@%
  5893. As Figure 8.3 shows, the value stored in %@AS@% val %@AE@% has changed from 25 to 3301.
  5894. The contents of %@AS@% val %@AE@% were changed indirectly, through the pointer %@AS@% ptr%@AE@%.  %@NL@%
  5895. %@NL@%
  5896. %@NL@%
  5897. %@3@%%@CR:C6A00080008 @%%@AB@%Summary of Pointer Basics%@AE@%%@EH@%%@NL@%
  5898. %@NL@%
  5899. In the preceding sections, you have seen how to do these operations:  %@NL@%
  5900. %@NL@%
  5901. %@NL@%
  5902.   ■   Declare a pointer to a simple variable%@NL@%
  5903. %@NL@%
  5904.   ■   Initialize a pointer, making it point to a variable%@NL@%
  5905. %@NL@%
  5906.   ■   Use a pointer to get the value of a variable%@NL@%
  5907. %@NL@%
  5908.   ■   Use a pointer to change the contents of a variable%@NL@%
  5909. %@NL@%
  5910. %@NL@%
  5911. It's important for you to be comfortable with these basic ideas before
  5912. reading about the more advanced uses of pointers. If you're not sure you
  5913. understand these concepts, you may want to experiment with the POINTER.C
  5914. program to reinforce what you know. For instance, you might add some new
  5915. variables of different types and create new pointers to access them.  %@NL@%
  5916. %@NL@%
  5917. %@NL@%
  5918. %@2@%%@CR:C6A00080009 @%%@AB@%Pointers to Arrays%@AE@%%@EH@%%@NL@%
  5919. %@NL@%
  5920. Pointers and arrays are closely related in C─a major theme we'll elaborate
  5921. throughout the rest of this chapter and Chapter 9, "Advanced Pointers." This
  5922. section explains one of the simpler ways to use pointers with arrays.  %@NL@%
  5923. %@NL@%
  5924. A pointer to an array, or "array pointer," combines two powerful language
  5925. features─the pointer's ability to provide indirect access and the
  5926. convenience of accessing array elements through numerical subscripts.  %@NL@%
  5927. %@NL@%
  5928. %@AU@% An array pointer can point to any element in a given array.%@AE@%  %@NL@%
  5929. %@NL@%
  5930. A pointer to an array is not much different than a pointer to a simple
  5931. variable. In both cases, the pointer can point only to a single object at
  5932. any given time. An array pointer, however, can reference any individual
  5933. element within an array (but just one at a time).  %@NL@%
  5934. %@NL@%
  5935. The program PARRAY.C shows how to access the elements of an %@AB@%int%@AE@% array
  5936. through a pointer:  %@NL@%
  5937. %@NL@%
  5938. %@AS@%  /* PARRAY.C: Demonstrate pointer to array. */
  5939. %@AS@%  
  5940. %@AS@%  #include <stdio.h>
  5941. %@AS@%  
  5942. %@AS@%  int i_array[] = { 25, 300, 2, 12 };
  5943. %@AS@%  
  5944. %@AS@%  main()
  5945. %@AS@%  {
  5946. %@AS@%     int *ptr;
  5947. %@AS@%     int count;
  5948. %@AS@%     ptr = &i_array[0];%@AE@%%@NL@%
  5949. %@NL@%
  5950. %@AS@%  for( count = 0; count < 4 ; count++ )   {
  5951. %@AS@%        printf( "i_array[%d] = %d\n", count, *ptr );
  5952. %@AS@%        ptr++;
  5953. %@AS@%     }
  5954. %@AS@%  }%@AE@%%@NL@%
  5955. %@NL@%
  5956. Here is the output from PARRAY.C:  %@NL@%
  5957. %@NL@%
  5958. %@AS@%  i_array[0] = 25
  5959. %@AS@%  i_array[1] = 300
  5960. %@AS@%  i_array[2] = 2
  5961. %@AS@%  i_array[3] = 12%@AE@%%@NL@%
  5962. %@NL@%
  5963. The PARRAY.C program creates a four-element %@AB@%int%@AE@% array named %@AS@% i_array%@AE@%. Then
  5964. it declares a pointer named %@AS@% ptr %@AE@% and uses %@AS@% ptr %@AE@% in a %@AB@%for%@AE@% loop to access
  5965. each of the elements in %@AS@% i_array%@AE@%.  %@NL@%
  5966. %@NL@%
  5967. Notice the similarity between PARRAY.C and the previous example (POINTER.C).
  5968. The pointer is declared in the same way:  %@NL@%
  5969. %@NL@%
  5970. %@AS@%  int *ptr;%@AE@%%@NL@%
  5971. %@NL@%
  5972. As noted before, this declaration states that %@AS@% ptr %@AE@% can point to any object
  5973. of the %@AB@%int%@AE@% type, which includes an element in an %@AB@%int%@AE@% array as well as a
  5974. simple %@AB@%int%@AE@%. The initialization of %@AS@% ptr %@AE@% looks similar, too:  %@NL@%
  5975. %@NL@%
  5976. %@AS@%  ptr = &i_array[0];%@AE@%%@NL@%
  5977. %@NL@%
  5978. This statement assigns %@AS@% ptr %@AE@% the address of the first element of %@AS@% i_array%@AE@%,
  5979. which is %@AS@% i_array[0]%@AE@%. (There's a more compact way to initialize this
  5980. pointer, but we'll defer that discussion for a moment.) Figure 8.4 shows the
  5981. relationship between %@AS@% ptr %@AE@% and %@AS@% i_array %@AE@% immediately after %@AS@% ptr %@AE@% is
  5982. initialized.  %@NL@%
  5983. %@NL@%
  5984. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  5985. %@NL@%
  5986. %@NL@%
  5987. %@3@%%@CR:C6A00080010 @%%@AB@%Arrays and Pointer Arithmetic%@AE@%%@EH@%%@NL@%
  5988. %@NL@%
  5989. Once a pointer points to an array, it can access any of the array's
  5990. elements. By adding or subtracting from the pointer's value (using "pointer
  5991. arithmetic") you can access any element in the array, just as you can access
  5992. it with array subscripts.  %@NL@%
  5993. %@NL@%
  5994. So in PARRAY.C, just as in POINTER.C, we can use %@AS@% *ptr %@AE@% to access the %@AB@%int%@AE@%
  5995. value that %@AS@% ptr %@AE@% references. The only difference is now %@AS@% ptr %@AE@% points to an
  5996. array element instead of a simple variable.  %@NL@%
  5997. %@NL@%
  5998. When the %@AB@%for%@AE@% loop in PARRAY.C executes the first time, %@AS@% ptr %@AE@% points to the
  5999. first element of %@AS@% i_array%@AE@%, which is %@AS@% i_array[0]%@AE@%. The second statement in the
  6000. loop body,  %@NL@%
  6001. %@NL@%
  6002. %@AS@%  ptr++;%@AE@%%@NL@%
  6003. %@NL@%
  6004. increments the pointer. Now %@AS@% ptr %@AE@% points to the next element in %@AS@% i_array%@AE@%,
  6005. which is %@AS@% i_array[1]%@AE@%. Figure 8.5 shows the relationship of %@AS@% ptr %@AE@% and %@AS@%
  6006. %@AS@%i_array %@AE@% after the first iteration of the %@AB@%for%@AE@% loop in PARRAY.C.  %@NL@%
  6007. %@NL@%
  6008. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  6009. %@NL@%
  6010. Figures 8.4 and 8.5 illustrate another important fact about pointers.
  6011. Pointer arithmetic is automatically scaled to the size of the object that a
  6012. pointer references. As explained above, incrementing %@AS@% ptr %@AE@% with the
  6013. statement %@AS@%  %@AE@%%@NL@%
  6014. %@NL@%
  6015. %@AS@%  ptr++;%@AE@%%@NL@%
  6016. %@NL@%
  6017. %@AU@% Pointer arithmetic is scaled to the size of  elements in an array.%@AE@%  %@NL@%
  6018. %@NL@%
  6019. moves the pointer forward to the next %@AI@%element%@AE@% in %@AS@% i_array%@AE@%. Since each
  6020. element of an %@AB@%int%@AE@% array contains two bytes, this operation actually adds 2
  6021. to the address stored in %@AS@% ptr%@AE@%, but you don't have to worry about that
  6022. detail. The compiler knows the size of the elements in the array and adjusts
  6023. the pointer accordingly.  %@NL@%
  6024. %@NL@%
  6025. Incrementing a pointer adds 1 if it points to a %@AB@%char%@AE@% array, 4 if it points
  6026. to a %@AB@%float%@AE@% array, and so on.  %@NL@%
  6027. %@NL@%
  6028. You can also decrement an array pointer. If %@AS@% ptr %@AE@% points to %@AS@% i_array[2]%@AE@%,
  6029. this statement moves the pointer back one element, to %@AS@% i_array[1]%@AE@%:  %@NL@%
  6030. %@NL@%
  6031. %@AS@%  ptr--;%@AE@%%@NL@%
  6032. %@NL@%
  6033. Although the previous expressions increment and decrement %@AS@% ptr %@AE@% by 1, you
  6034. can add or subtract any integer value from a pointer. For instance, the
  6035. following statement moves %@AS@% ptr %@AE@% forward three elements in %@AS@% i_array%@AE@%:  %@NL@%
  6036. %@NL@%
  6037. %@AS@%  ptr += 3;%@AE@%%@NL@%
  6038. %@NL@%
  6039. Be careful not to overrun the bounds of an array when accessing its elements
  6040. with a pointer. As noted in Chapter 4, "Data Types," the C language doesn't
  6041. check array subscripts. This rule applies equally when you access an array
  6042. with a pointer, which can potentially reference any address in memory.  %@NL@%
  6043. %@NL@%
  6044. ────────────────────────────────────────────────────────────────────────────%@NL@%
  6045. %@AU@%WARNING%@AE@%%@NL@%
  6046. %@NL@%
  6047. The C language does not check array pointer references. If you increment or
  6048. decrement a pointer past the limits of an array, you can corrupt other parts
  6049. of your program or cause other unexpected results.%@NL@%
  6050. ────────────────────────────────────────────────────────────────────────────%@NL@%
  6051. %@NL@%
  6052. It's your job to make sure an increment or decrement doesn't move a pointer
  6053. outside the memory where an array is stored. For instance, if you decrement %@AS@%
  6054. %@AS@%ptr %@AE@% when it points to %@AS@% i_array[0]%@AE@%, it will point to whatever happens to be
  6055. stored in the %@AB@%int%@AE@%-size memory area %@AI@%below%@AE@% the element %@AS@% i_array[0]%@AE@%.  %@NL@%
  6056. %@NL@%
  6057. Most pointer arithmetic occurs in connection with arrays, where a numerical
  6058. index has obvious utility. It's not illegal to do pointer arithmetic on
  6059. nonarray pointers, but such operations normally serve no purpose. For
  6060. instance, if you increment a pointer to a simple variable, the pointer no
  6061. longer points to the variable and becomes useless.  %@NL@%
  6062. %@NL@%
  6063. %@NL@%
  6064. %@3@%%@CR:C6A00080011 @%%@AB@%Comparing Pointers%@AE@%%@EH@%%@NL@%
  6065. %@NL@%
  6066. The special nature of a pointer variable─the fact that it contains an
  6067. address─ precludes most operations that are legal for other variables.
  6068. There's no such thing as a fractional memory address, for example. So it
  6069. wouldn't make sense to divide a pointer, or add a floating-point number to
  6070. it. The most common pointer operations are assignment, incrementing, and
  6071. decrementing, as described earlier. You can also compare one pointer to
  6072. another.  %@NL@%
  6073. %@NL@%
  6074. If a program allocates memory for a stack, for instance, you might create
  6075. two pointers that point to different parts of the stack. One pointer can
  6076. show where the stack begins and the other where it ends. To see how much of
  6077. the stack is in use, you can subtract the pointers. (A "stack" is a memory
  6078. area used for temporary storage.)  %@NL@%
  6079. %@NL@%
  6080. %@AU@% You can compare pointer variables with relational operators or by
  6081. %@AU@%subtraction.%@AE@%  %@NL@%
  6082. %@NL@%
  6083. Pointer comparisons can be done with relational operators (such as %@AB@%%@AE@% ) or by
  6084. subtracting one pointer from another. Of course, pointer comparisons are
  6085. meaningful only for pointers that point to the same data object or related
  6086. objects of the same type.  %@NL@%
  6087. %@NL@%
  6088. %@NL@%
  6089. %@3@%%@CR:C6A00080012 @%%@AB@%PARRAY.C Revisited%@AE@%%@EH@%%@NL@%
  6090. %@NL@%
  6091. Before leaving the PARRAY.C program, we should note that most C programmers
  6092. would write it more compactly (PARRAY1.C):  %@NL@%
  6093. %@NL@%
  6094. %@AS@%  /* PARRAY1.C: Compact version of PARRAY.C. */
  6095. %@AS@%  
  6096. %@AS@%  #include <stdio.h>
  6097. %@AS@%  
  6098. %@AS@%  int i_array[] = { 25, 300, 2, 12 };
  6099. %@AS@%  
  6100. %@AS@%  main()
  6101. %@AS@%  {
  6102. %@AS@%     int count;
  6103. %@AS@%     int *ptr = i_array;
  6104. %@AS@%     for( count = 0; count < 4 ; count++ )
  6105. %@AS@%        printf( "i_array[%d] = %d\n", count, *ptr++ );
  6106. %@AS@%  }%@AE@%%@NL@%
  6107. %@NL@%
  6108. %@AU@% You can declare and initialize a pointer variable  in one statement.%@AE@%  %@NL@%
  6109. %@NL@%
  6110. The PARRAY1.C program uses several shorthand techniques you can expect to
  6111. see in C programs. Like other variables, pointers can be initialized at the
  6112. same time they are declared. The following statement in PARRAY1.C performs
  6113. both operations:  %@NL@%
  6114. %@NL@%
  6115. %@AS@%  int *ptr = i_array;%@AE@%%@NL@%
  6116. %@NL@%
  6117. The statement above is equivalent to these statements:  %@NL@%
  6118. %@NL@%
  6119. %@AS@%  int *ptr;
  6120. %@AS@%  ptr = i_array;%@AE@%%@NL@%
  6121. %@NL@%
  6122. You may have noticed another difference in the way %@AS@% ptr %@AE@% is initialized. The
  6123. PARRAY1.C program omits the address-of operator and array subscript that
  6124. PARRAY.C used to signify the address of the first element of %@AS@% i_array%@AE@%.
  6125. Instead of %@NL@%
  6126. %@NL@%
  6127. %@AS@%  &i_array[0]%@AE@%%@NL@%
  6128. %@NL@%
  6129. the program uses  %@NL@%
  6130. %@NL@%
  6131. %@AS@%  i_array%@AE@%%@NL@%
  6132. %@NL@%
  6133. %@AU@% An array name is a pointer.%@AE@%  %@NL@%
  6134. %@NL@%
  6135. In fact, the two expressions are equivalent. In the C language, the name of
  6136. an array is actually a pointer. Any array name that doesn't have a subscript
  6137. is interpreted as a pointer to the base address of the array. (The "base
  6138. address" is the address of the array's first element.) We'll explore this
  6139. equivalence further in the following sections and in Chapter 9, "Advanced
  6140. Pointers."  %@NL@%
  6141. %@NL@%
  6142. Finally, PARRAY1.C uses the expression %@AS@% *ptr++ %@AE@% to perform two jobs:
  6143. accessing the value %@AS@% ptr %@AE@% points to and incrementing %@AS@% ptr%@AE@%. Note the order in
  6144. which the two operators in this expression take effect. The indirection
  6145. operator takes effect first, accessing the value of the array element that %@AS@%
  6146. %@AS@%ptr %@AE@% currently points to. Then the increment operator (%@AB@%++%@AE@%) adds 1 to %@AS@% ptr%@AE@%,
  6147. making it point to the next element in %@AS@% i_array%@AE@%.  %@NL@%
  6148. %@NL@%
  6149. %@NL@%
  6150. %@2@%%@CR:C6A00080013 @%%@AB@%Pointers and Strings%@AE@%%@EH@%%@NL@%
  6151. %@NL@%
  6152. %@AU@% String pointers are handled like other array pointers.%@AE@%  %@NL@%
  6153. %@NL@%
  6154. Because a string is an array of characters, pointers to strings are handled
  6155. much like other array pointers. The program PSTRING.C is similar to the
  6156. examples that demonstrated array pointers (PARRAY.C and PARRAY1.C). It uses
  6157. a pointer to access a %@AB@%char%@AE@% array:  %@NL@%
  6158. %@NL@%
  6159. %@AS@%  /* PSTRING.C: Demonstrate pointer to a string. */
  6160. %@AS@%  
  6161. %@AS@%  #include <stdio.h>
  6162. %@AS@%  
  6163. %@AS@%  main()
  6164. %@AS@%  {
  6165. %@AS@%     int count;
  6166. %@AS@%     char name[] = "john";
  6167. %@AS@%     char *ptr = name;
  6168. %@AS@%     for( count = 0; count < 4; count++ )
  6169. %@AS@%     {
  6170. %@AS@%        printf( "name[%d]: %c\n", count, *ptr++ );
  6171. %@AS@%     }
  6172. %@AS@%  }%@AE@%%@NL@%
  6173. %@NL@%
  6174. The PSTRING.C program steps through the %@AS@% name %@AE@% array, printing each
  6175. character in turn:  %@NL@%
  6176. %@NL@%
  6177. %@AS@%  name[0]: j
  6178. %@AS@%  name[1]: o
  6179. %@AS@%  name[2]: h
  6180. %@AS@%  name[3]: n%@AE@%%@NL@%
  6181. %@NL@%
  6182. The notable difference between PARRAY.C and PSTRING.C is that PSTRING.C has
  6183. a %@AB@%char%@AE@% array instead of an %@AB@%int%@AE@% array. Again, incrementing an array pointer
  6184. moves the pointer to the next array element. So in PSTRING.C each iteration
  6185. of the %@AB@%for%@AE@% loop moves the pointer to the next %@AB@%char%@AE@% in the string.  %@NL@%
  6186. %@NL@%
  6187. The first time through the loop, %@AS@% ptr %@AE@% points to %@AS@% name[0]%@AE@%. The second time
  6188. it points to %@AS@% name[1]%@AE@%, and so on.  %@NL@%
  6189. %@NL@%
  6190. As mentioned in Chapter 4, "Data Types," one difference between strings and
  6191. noncharacter arrays is that strings end with a null character. The string in
  6192. PSTRING.C actually contains five characters: four letters and a null
  6193. character. We can exploit this fact to simplify the program, as we do below
  6194. in PSTRING.C.  %@NL@%
  6195. %@NL@%
  6196. %@AS@%  /* PSTRING1.C: Look for null at string's end.  */
  6197. %@AS@%  
  6198. %@AS@%  #include <stdio.h>
  6199. %@AS@%  
  6200. %@AS@%  main()
  6201. %@AS@%  {
  6202. %@AS@%     char name[] = "john";
  6203. %@AS@%     char *ptr = name;
  6204. %@AS@%     while( *ptr )
  6205. %@AS@%        printf( "*ptr = %c\n", *ptr++ );
  6206. %@AS@%  }%@AE@%%@NL@%
  6207. %@NL@%
  6208. Here is the output from PSTRING1.C:  %@NL@%
  6209. %@NL@%
  6210. %@AS@%  *ptr = j
  6211. %@AS@%  *ptr = o
  6212. %@AS@%  *ptr = h
  6213. %@AS@%  *ptr = n%@AE@%%@NL@%
  6214. %@NL@%
  6215. Like PSTRING.C, the PSTRING1.C program steps through the array one character
  6216. at a time. However, it replaces the %@AB@%for%@AE@% loop with a simpler %@AB@%while%@AE@% loop. The
  6217. test expression in the %@AB@%while%@AE@% loop,  %@NL@%
  6218. %@NL@%
  6219. %@AS@%  while( *ptr )%@AE@%%@NL@%
  6220. %@NL@%
  6221. is evaluated as true until %@AS@% ptr %@AE@% points to the null character that
  6222. terminates the string. It's a more compact way of writing this expression:  %@NL@%
  6223. %@NL@%
  6224. %@AS@%  while( *ptr != 0 )%@AE@%%@NL@%
  6225. %@NL@%
  6226. %@AU@% Any operation done with array subscripts can also be done with pointer
  6227. %@AU@%notation.%@AE@%  %@NL@%
  6228. %@NL@%
  6229. This is an ideal time to elaborate on the relationship between arrays and
  6230. pointers. Any operation you can do with conventional array notation
  6231. (subscripts) can also be done with pointers. This is possible because an
  6232. array name, as we noted earlier, is itself a pointer.  %@NL@%
  6233. %@NL@%
  6234. To illustrate, the PSTRING2.C program uses only array notation:  %@NL@%
  6235. %@NL@%
  6236. %@AS@%  /* PSTRING2.C: Demonstrate strings and array notation. */
  6237. %@AS@%  
  6238. %@AS@%  #include <stdio.h>
  6239. %@AS@%  #include <string.h>%@AE@%%@NL@%
  6240. %@NL@%
  6241. %@AS@%  main()
  6242. %@AS@%  {
  6243. %@AS@%     int count;
  6244. %@AS@%     char name[] = "john";
  6245. %@AS@%     for( count = 0; count < strlen( name ); count++ )
  6246. %@AS@%        printf( "name[%d]: %c\n", count, name[count] );
  6247. %@AS@%  }%@AE@%%@NL@%
  6248. %@NL@%
  6249. PSTRING2.C gives the same output as PSTRING.C. In this program, the
  6250. expression  %@NL@%
  6251. %@NL@%
  6252. %@AS@%  name[count]%@AE@%%@NL@%
  6253. %@NL@%
  6254. uses %@AS@% count %@AE@% as in an index to the %@AS@% name %@AE@% array.  %@NL@%
  6255. %@NL@%
  6256. PSTRING3.C is the same program written with pointer notation:  %@NL@%
  6257. %@NL@%
  6258. %@AS@%  /* PSTRING3.C: Strings and pointer notation.  */
  6259. %@AS@%  
  6260. %@AS@%  #include <stdio.h>
  6261. %@AS@%  #include <string.h>
  6262. %@AS@%  
  6263. %@AS@%  main()
  6264. %@AS@%  {
  6265. %@AS@%     int count;
  6266. %@AS@%     char name[] = "john";
  6267. %@AS@%     for( count = 0; count < strlen( name ); count++ )
  6268. %@AS@%        printf( "*(name+%d) = %c\n", count,*(name+count) );
  6269. %@AS@%  }%@AE@%%@NL@%
  6270. %@NL@%
  6271. Here is the output from PSTRING3.C:  %@NL@%
  6272. %@NL@%
  6273. %@AS@%  *(name+0) = j
  6274. %@AS@%  *(name+1) = o
  6275. %@AS@%  *(name+2) = h
  6276. %@AS@%  *(name+3) = n%@AE@%%@NL@%
  6277. %@NL@%
  6278. Notice how PSTRING3.C replaces the expression  %@NL@%
  6279. %@NL@%
  6280. %@AS@%  name[count]%@AE@%%@NL@%
  6281. %@NL@%
  6282. with the expression:  %@NL@%
  6283. %@NL@%
  6284. %@AS@%  *(name+count)%@AE@%%@NL@%
  6285. %@NL@%
  6286. Both expressions use the variable %@AS@% count %@AE@% as an offset from the base address
  6287. of the array. The parentheses in the second expression are important. They
  6288. are necessary because the indirection operator takes effect before the
  6289. addition operator. If you omit the parentheses, as in  %@NL@%
  6290. %@NL@%
  6291. %@AS@%  *name+count%@AE@%%@NL@%
  6292. %@NL@%
  6293. the expression has the same effect as  %@NL@%
  6294. %@NL@%
  6295. %@AS@%  (*name)+count%@AE@%%@NL@%
  6296. %@NL@%
  6297. which adds the value of %@AS@% count %@AE@% to the object %@AS@% name %@AE@% references.  %@NL@%
  6298. %@NL@%
  6299. In summary, the examples in this section show three alternative ways to
  6300. access a character inside a string. In the %@AB@%printf%@AE@% statements in the
  6301. examples, these expressions are equivalent:  %@NL@%
  6302. %@NL@%
  6303. %@AS@%  *ptr %@AE@%%@NL@%
  6304. %@NL@%
  6305. %@AS@%  name[count]%@AE@%%@NL@%
  6306. %@NL@%
  6307. %@AS@%  *(name+count)%@AE@%%@NL@%
  6308. %@NL@%
  6309. Many C programmers prefer pointer notation to array notation because
  6310. pointers are faster for some operations. In other cases─including the one
  6311. above─the choice is entirely one of taste. There's more to say about the
  6312. relationship between pointers and arrays. We'll return to this topic later
  6313. in this chapter and in Chapter 9, "Advanced Pointers."  %@NL@%
  6314. %@NL@%
  6315. %@NL@%
  6316. %@2@%%@CR:C6A00080014 @%%@AB@%Passing Pointers to Functions%@AE@%%@EH@%%@NL@%
  6317. %@NL@%
  6318. %@AU@% A function that receives  pointers can access  variables that are local  to
  6319. %@AU@%other functions.%@AE@%  %@NL@%
  6320. %@NL@%
  6321. One of the most common uses of pointers is to pass them as arguments to
  6322. functions. Functions that receive variables as parameters get local copies
  6323. of those variables, not the originals. In contrast, functions that receive
  6324. pointers to variables gain access to the original variables associated with
  6325. the pointers. This allows the functions to  %@NL@%
  6326. %@NL@%
  6327. %@NL@%
  6328.   ■   Return more than one value%@NL@%
  6329. %@NL@%
  6330.   ■   Read and change values in variables─including arrays and
  6331.       structures─that otherwise aren't visible to the function%@NL@%
  6332. %@NL@%
  6333. %@NL@%
  6334. The first item listed above relates to the %@AB@%return%@AE@% statement. As we noted in
  6335. Chapter 2, "Functions," a function can return only one value through %@AB@%return%@AE@%.
  6336. How-ever, it's not difficult to imagine a useful function─a sort, for
  6337. instance─that would return more than one value. Pointers offer an elegant
  6338. solution.  %@NL@%
  6339. %@NL@%
  6340. The second item involves visibility. Most variables in C programs are local
  6341. to the functions where they are defined, and a function normally can't
  6342. access local variables in other functions. There are times, however, when
  6343. you want a function to have access to a local variable defined elsewhere in
  6344. the program. By passing the function a pointer to the local variable, you
  6345. can give it access to the variable  itself.  %@NL@%
  6346. %@NL@%
  6347. The PFUNC.C program illustrates both ideas. It has a function that returns
  6348. more than one value and uses pointers to alter variables that aren't visible
  6349. within the function:  %@NL@%
  6350. %@NL@%
  6351. %@AS@%  /* PFUNC.C: Pass pointers to a function. */
  6352. %@AS@%  
  6353. %@AS@%  #include <stdio.h>
  6354. %@AS@%  
  6355. %@AS@%  void swap( int *ptr1, int *ptr2 );
  6356. %@AS@%  
  6357. %@AS@%  main()
  6358. %@AS@%  {
  6359. %@AS@%     int first = 1, second = 3;
  6360. %@AS@%     int *ptr = &second;
  6361. %@AS@%     printf( "first: %d  second: %d\n", first, *ptr );
  6362. %@AS@%     swap( &first, ptr );
  6363. %@AS@%     printf( "first: %d  second: %d\n", first, *ptr );
  6364. %@AS@%  }
  6365. %@AS@%  
  6366. %@AS@%  void swap( int *ptr1, int *ptr2 )
  6367. %@AS@%  {
  6368. %@AS@%     int temp;
  6369. %@AS@%     temp = *ptr1;
  6370. %@AS@%     *ptr1 = *ptr2;
  6371. %@AS@%     *ptr2 = temp;
  6372. %@AS@%  }%@AE@%%@NL@%
  6373. %@NL@%
  6374. Here is the output from PFUNC.C:  %@NL@%
  6375. %@NL@%
  6376. %@AS@%  first: 1  second: 3
  6377. %@AS@%  first: 3  second: 1%@AE@%%@NL@%
  6378. %@NL@%
  6379. %@AU@%  Pointers can eliminate the need for external variables.%@AE@%  %@NL@%
  6380. %@NL@%
  6381. The PFUNC.C program swaps the values of two %@AB@%int%@AE@% variables named %@AS@% first %@AE@% and %@AS@%
  6382. %@AS@%second%@AE@%, using a function named %@AS@% swap%@AE@%. Since the exchange involves two
  6383. values, the %@AS@% swap %@AE@% function can't use %@AB@%return%@AE@% to communicate its results.
  6384. More-over, the variables %@AS@% first %@AE@% and %@AS@% second %@AE@% are defined only in the %@AB@%main%@AE@%
  6385. function, and as good C programmers, we want to exchange their values
  6386. without making them externally visible.  %@NL@%
  6387. %@NL@%
  6388. The prototype for the %@AS@% swap %@AE@% function shows that %@AS@% swap %@AE@% expects to receive
  6389. two pointers to %@AB@%int%@AE@% variables:  %@NL@%
  6390. %@NL@%
  6391. %@AS@%  void swap( int *ptr1, int *ptr2 );%@AE@%%@NL@%
  6392. %@NL@%
  6393. Notice the use of %@AB@%void%@AE@% in the prototype and function definition. The %@AB@%void%@AE@%
  6394. specifier shows that the %@AS@% swap %@AE@% function doesn't return any value through a
  6395. %@AB@%return%@AE@% statement. Instead, %@AS@%swap %@AE@% returns its results indirectly, through the
  6396. action of pointers.  %@NL@%
  6397. %@NL@%
  6398. The variables we want to exchange are defined only in %@AB@%main%@AE@%:  %@NL@%
  6399. %@NL@%
  6400. %@AS@%  int first = 1, second = 3;%@AE@%%@NL@%
  6401. %@NL@%
  6402. No other function in the program can access these variables directly by
  6403. using the variable names %@AS@% first %@AE@% and %@AS@% second%@AE@%. We must pass these variables
  6404. as arguments; but since the C language passes arguments by value, we need to
  6405. pass pointers to the variables.  %@NL@%
  6406. %@NL@%
  6407. The %@AB@%main%@AE@% function calls %@AS@% swap %@AE@% with the following statement:  %@NL@%
  6408. %@NL@%
  6409. %@AS@%  swap( &first, ptr );%@AE@%%@NL@%
  6410. %@NL@%
  6411. This statement shows two different ways to pass a pointer to a function. The
  6412. first argument in the function call, %@AS@%  %@AE@%%@NL@%
  6413. %@NL@%
  6414. %@AS@%  &first %@AE@%%@NL@%
  6415. %@NL@%
  6416. passes the address of first as a constant, using the address-of operator.
  6417. The second argument,%@AS@%  %@AE@%%@NL@%
  6418. %@NL@%
  6419. %@AS@%  ptr%@AE@%%@NL@%
  6420. %@NL@%
  6421. passes the address of %@AS@% second %@AE@% with a pointer variable. Earlier in PFUNC.C
  6422. we declared %@AS@% ptr %@AE@% as a pointer to an %@AB@%int%@AE@% and assigned it the address of %@AS@%
  6423. %@AS@%second%@AE@%:  %@NL@%
  6424. %@NL@%
  6425. %@AS@%  int *ptr = &second;%@AE@%%@NL@%
  6426. %@NL@%
  6427. Both arguments pass the same kind of data─the address of a local variable─to
  6428. the function. We'll return to this idea after we see how the rest of PFUNC.C
  6429. works.  %@NL@%
  6430. %@NL@%
  6431. When the %@AS@% swap %@AE@% function executes, it creates two %@AB@%int%@AE@% pointers named %@AS@% ptr1 %@AE@%
  6432. and %@AS@% ptr2 %@AE@% and assigns the passed addresses to them:  %@NL@%
  6433. %@NL@%
  6434. %@AS@%  void swap( int *ptr1, int *ptr2 )%@AE@%%@NL@%
  6435. %@NL@%
  6436. Since there's a one-to-one correspondence between arguments and parameters,
  6437. the pointer %@AS@% ptr1 %@AE@% receives the address of %@AS@% first %@AE@% and %@AS@% ptr2 %@AE@% receives the
  6438. address of %@AS@% second%@AE@%. The %@AS@% swap %@AE@% function exchanges the values of %@AS@% first %@AE@% and %@AS@%
  6439. %@AS@%second%@AE@%, using the two pointers and a temporary %@AB@%int%@AE@% variable named %@AS@% temp%@AE@%:  %@NL@%
  6440. %@NL@%
  6441. %@AS@%  int temp;
  6442. %@AS@%  temp = *ptr1;
  6443. %@AS@%  *ptr1 = *ptr2;
  6444. %@AS@%  *ptr2 = temp;%@AE@%%@NL@%
  6445. %@NL@%
  6446. Within the %@AS@% swap %@AE@% function, PFUNC.C uses the indirection operator to access
  6447. the values that %@AS@% ptr1 %@AE@% and %@AS@% ptr2 %@AE@% reference. The expression %@AS@% *ptr1 %@AE@% accesses
  6448. the value stored in %@AS@% first%@AE@%. Likewise, the expression %@AS@% *ptr2 %@AE@% accesses the
  6449. value stored in %@AS@% second%@AE@%.  %@NL@%
  6450. %@NL@%
  6451. Through the addresses contained in the pointers, the %@AS@% swap %@AE@% function can
  6452. indirectly access variables that are local to the %@AB@%main%@AE@% function.  %@NL@%
  6453. %@NL@%
  6454. %@NL@%
  6455. %@3@%%@CR:C6A00080015 @%%@AB@%Passing Address Constants Versus Passing Pointer Variables%@AE@%%@EH@%%@NL@%
  6456. %@NL@%
  6457. Now that you know how the %@AS@% swap %@AE@% function works, we can elaborate on the two
  6458. methods that PFUNC.C uses to pass the address of %@AS@% first %@AE@% and %@AS@% second %@AE@% to %@AS@%
  6459. %@AS@%swap%@AE@%.  %@NL@%
  6460. %@NL@%
  6461. %@AU@% When you pass a pointer  to a function, the function actually receives an
  6462. %@AU@%address.%@AE@%  %@NL@%
  6463. %@NL@%
  6464. Earlier, we said the %@AS@% swap %@AE@% function expects to receive two pointers as
  6465. parameters. While it's common to say pointers in this context, it would be
  6466. more accurate to say the function expects addresses, since that's what it
  6467. actually receives.  %@NL@%
  6468. %@NL@%
  6469. To work correctly, %@AS@% swap %@AE@% only needs the addresses of two variables. Once it
  6470. has the addresses, it assigns them to its own local pointers and proceeds to
  6471. do its work─modifying the original variables at long distance, as it were.
  6472. The %@AS@% swap %@AE@% function doesn't care whether you pass the addresses as constants
  6473. or pointer variables, since it receives the same kind of value in either
  6474. case. The address is all the function needs to change the value of a
  6475. variable defined elsewhere.  %@NL@%
  6476. %@NL@%
  6477. The first argument in the function call to %@AS@% swap %@AE@% shows a straightforward
  6478. way to pass an address. Inside the %@AB@%main%@AE@% function of PFUNC.C, the expression %@AS@%
  6479. %@AS@%&first %@AE@% equals the address of %@AS@% first%@AE@%. When you pass this argument to %@AS@% swap%@AE@%,
  6480. the function clearly receives an address.  %@NL@%
  6481. %@NL@%
  6482. The second argument is an address, too. Since %@AB@%main%@AE@% assigns the address of %@AS@%
  6483. %@AS@%second %@AE@% to the pointer variable %@AS@% ptr%@AE@%, the expression %@AS@% ptr %@AE@% equals the
  6484. address of %@AS@%second%@AE@%. When you pass this argument to %@AS@% swap%@AE@%, the function also
  6485. receives an address. (Remember, the value contained in a pointer variable is
  6486. an address.)  %@NL@%
  6487. %@NL@%
  6488. Some beginning programmers get confused by functions that expect to receive
  6489. pointers, thinking they must always pass pointer %@AI@%variables%@AE@% to such
  6490. functions. As PFUNC.C shows, if the function expects an address you can
  6491. simply pass the address as a constant, using the address-of operator.  %@NL@%
  6492. %@NL@%
  6493. ────────────────────────────────────────────────────────────────────────────%@NL@%
  6494. NOTE
  6495.  
  6496. %@AI@%When a function expects to receive an address as a parameter, you can pass
  6497. %@AI@%either an address constant or a pointer variable, whichever is more
  6498. %@AI@%suitable.%@AE@%%@NL@%
  6499. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  6500. %@NL@%
  6501. Why, then, would you ever go to the trouble of passing a pointer variable to
  6502. this kind of function? In a real program, the function that calls %@AS@% swap %@AE@%
  6503. might well use pointers to process %@AS@% first %@AE@% and %@AS@% second %@AE@% for some other
  6504. purpose. In such a case you might prefer to use pointers in the function
  6505. call, too.  %@NL@%
  6506. %@NL@%
  6507. %@NL@%
  6508. %@2@%%@CR:C6A00080016 @%%@AB@%Arrays of Pointers%@AE@%%@EH@%%@NL@%
  6509. %@NL@%
  6510. Pointers, like other variables, can be stored in arrays. This feature allows
  6511. you to create a variety of useful data structures.  %@NL@%
  6512. %@NL@%
  6513. %@AU@% In an array of pointers, each array element is  a pointer variable.%@AE@%  %@NL@%
  6514. %@NL@%
  6515. If you find an array of pointers hard to picture, begin with the idea that
  6516. an array is a group of variables of the same type. An "array of pointers" is
  6517. also a group of variables, but instead of simple variables, it contains a
  6518. group of pointer variables.  %@NL@%
  6519. %@NL@%
  6520. Each element in an array of pointers, then, is a pointer that contains an
  6521. address. Like other array elements, each element can be accessed with a
  6522. numerical subscript.  %@NL@%
  6523. %@NL@%
  6524. Pointer arrays are often used to speed up sorts. The QCSORT.C program shows
  6525. the basic idea behind such a sort:  %@NL@%
  6526. %@NL@%
  6527. %@AS@%  /* QCSORT.C: Demonstrate sorting array of pointers. */
  6528. %@AS@%  
  6529. %@AS@%  #include <stdio.h>
  6530. %@AS@%  #define SIZE 4
  6531. %@AS@%  
  6532. %@AS@%  void sort( int size, double *p[] );
  6533. %@AS@%  void show( int size, double *p[], double dd[] );
  6534. %@AS@%  
  6535. %@AS@%  main()
  6536. %@AS@%  {
  6537. %@AS@%     int x;
  6538. %@AS@%     double d[] = { 3.333, 1.111, 2.222, 4.444 };
  6539. %@AS@%     double *d_ptr[SIZE];
  6540. %@AS@%     for( x = 0; x < SIZE; x++ )
  6541. %@AS@%        d_ptr[x] = &d[x];
  6542. %@AS@%     show( SIZE, d_ptr, d );
  6543. %@AS@%     sort( SIZE, d_ptr );
  6544. %@AS@%     show( SIZE, d_ptr, d );
  6545. %@AS@%  }
  6546. %@AS@%  
  6547. %@AS@%  void sort( int size, double *p[] )
  6548. %@AS@%  {
  6549. %@AS@%     int x, x1;
  6550. %@AS@%     double *temp;
  6551. %@AS@%     for( x = 0; x < size - 1; x++ )
  6552. %@AS@%        for( x1 = x + 1; x1 < size; x1++ )
  6553. %@AS@%        {
  6554. %@AS@%           if( *p[x] > *p[x1] )
  6555. %@AS@%           {
  6556. %@AS@%              temp = p[x1];
  6557. %@AS@%              p[x1] = p[x];
  6558. %@AS@%              p[x] = temp;
  6559. %@AS@%           }
  6560. %@AS@%        }
  6561. %@AS@%  }%@AE@%%@NL@%
  6562. %@NL@%
  6563. %@AS@%  void show( int size, double *p[], double dd[] )
  6564. %@AS@%  {
  6565. %@AS@%     int x;
  6566. %@AS@%     printf( "------------------------" );
  6567. %@AS@%     printf( "------------------------\n" );
  6568. %@AS@%     for( x = 0; x < size; x++ )
  6569. %@AS@%     {
  6570. %@AS@%        printf( "*d_ptr[%d] = %1.3f   ", x, *p[x]);
  6571. %@AS@%        printf( "d_ptr[%d] = %u ", x, p[x]);
  6572. %@AS@%        printf( "  d[%d] = %1.3f\n", x, dd[x] );
  6573. %@AS@%     }
  6574. %@AS@%  }%@AE@%%@NL@%
  6575. %@NL@%
  6576. Here is the output from QCSORT.C:  %@NL@%
  6577. %@NL@%
  6578. %@AS@%  ------------------------------------------------
  6579. %@AS@%  *d_ptr[0] = 3.333   d_ptr[0] = 66   d[0] = 3.333
  6580. %@AS@%  *d_ptr[1] = 1.111   d_ptr[1] = 74   d[1] = 1.111
  6581. %@AS@%  *d_ptr[2] = 2.222   d_ptr[2] = 82   d[2] = 2.222
  6582. %@AS@%  *d_ptr[3] = 4.444   d_ptr[3] = 90   d[3] = 4.444
  6583. %@AS@%  ------------------------------------------------
  6584. %@AS@%  *d_ptr[0] = 1.111   d_ptr[0] = 74   d[0] = 3.333
  6585. %@AS@%  *d_ptr[1] = 2.222   d_ptr[1] = 82   d[1] = 1.111
  6586. %@AS@%  *d_ptr[2] = 3.333   d_ptr[2] = 66   d[2] = 2.222
  6587. %@AS@%  *d_ptr[3] = 4.444   d_ptr[3] = 90   d[3] = 4.444%@AE@%%@NL@%
  6588. %@NL@%
  6589. Since the purpose of QCSORT.C is to demonstrate pointers, not sorting
  6590. methods, it uses a simple bubble sort. This method isn't efficient but has
  6591. the advantage of being short and easy to follow.  %@NL@%
  6592. %@NL@%
  6593. The QCSORT.C program creates a %@AB@%double%@AE@% array named %@AS@% d %@AE@% and an array of
  6594. pointers named %@AS@% d_ptr%@AE@%. Each array has four elements. To illustrate the sort,
  6595. the elements of %@AS@% d %@AE@% are initialized out of order.  %@NL@%
  6596. %@NL@%
  6597. The goal of QCSORT.C is to display a sorted list of the values in %@AS@% d%@AE@%. You
  6598. could do this by sorting the elements of %@AS@% d %@AE@% itself, but that solution is
  6599. not efficient. Every %@AB@%double%@AE@% value contains eight bytes, and sorting a large
  6600. number of %@AB@%double%@AE@% values requires that you move a lot of memory.  %@NL@%
  6601. %@NL@%
  6602. Instead of moving the %@AB@%double%@AE@% values themselves, QCSORT.C creates an array of
  6603. pointers that point to the elements of the %@AS@% d %@AE@% array, then sorts the
  6604. pointers. This saves time because a pointer is stored in only two bytes.
  6605. Figure 8.6 shows the relationship between the %@AS@% d %@AE@% and %@AS@% d_ptr %@AE@% arrays
  6606. immediately after both are initialized.  %@NL@%
  6607. %@NL@%
  6608. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  6609. %@NL@%
  6610. At the stage shown in Figure 8.6, the pointers in the %@AS@% d_ptr %@AE@% array have
  6611. been initialized to point to the %@AB@%double%@AE@% elements in the %@AS@% d %@AE@% array. (The
  6612. array element %@AS@% d_ptr[0] %@AE@% points to %@AS@% d[0]%@AE@%, %@AS@% d_ptr[1] %@AE@% points to %@AS@% d[1]%@AE@%, and so
  6613. on.) The function %@AS@% show %@AE@% displays three sets of data:  %@NL@%
  6614. %@NL@%
  6615. %@NL@%
  6616.   ■   The value each pointer references%@NL@%
  6617. %@NL@%
  6618.   ■   The address assigned to each pointer%@NL@%
  6619. %@NL@%
  6620.   ■   The value of each element in the %@AS@% d %@AE@% array%@NL@%
  6621. %@NL@%
  6622. %@NL@%
  6623. After calling the %@AS@% show %@AE@% function, QCSORT.C calls the %@AS@% sort %@AE@% function, which
  6624. sorts the pointers in %@AS@% d_ptr%@AE@%.  %@NL@%
  6625. %@NL@%
  6626. The declaration of %@AS@% sort %@AE@% contains something new. In the declaration  %@NL@%
  6627. %@NL@%
  6628. %@AS@%  void sort( int size, double *p[] );%@AE@%%@NL@%
  6629. %@NL@%
  6630. the expression %@AS@% *p[] %@AE@% shows that the %@AS@% sort %@AE@% function expects to receive a
  6631. %@AI@%pointer to an array of pointers%@AE@%. When the program calls %@AS@% sort%@AE@%, it passes the
  6632. size of the array to be sorted (first argument) and a pointer to the array
  6633. of pointers (second argument):  %@NL@%
  6634. %@NL@%
  6635. %@AS@%  sort( SIZE, d_ptr );%@AE@%%@NL@%
  6636. %@NL@%
  6637. Now the %@AS@% sort %@AE@% function has all the information it needs to sort the
  6638. pointers in the %@AS@% d_ptr %@AE@% array, making each pointer point to the correct
  6639. element in the %@AS@% d %@AE@% array.  %@NL@%
  6640. %@NL@%
  6641. After the sort is complete, QCSORT.C calls %@AS@% show %@AE@% again to display the
  6642. results of the sort. Now that the pointers have been sorted, they can be
  6643. used to display a sorted list of %@AB@%double%@AE@% values. Figure 8.7 shows the
  6644. relationship between the %@AS@% d %@AE@% and %@AS@% d_ptr %@AE@% arrays after the sort is complete.
  6645. %@NL@%
  6646. %@NL@%
  6647. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  6648. %@NL@%
  6649. Of course, the array in QCSORT.C is so small that the time savings from
  6650. using pointers is negligible. In a real program, however, which might sort
  6651. thousands of values instead of four, the difference between moving eight
  6652. bytes and two bytes can be dramatic. The advantage of sorting pointers is
  6653. even greater when sorting large data objects such as strings or structures.
  6654. %@NL@%
  6655. %@NL@%
  6656. %@AU@%  The elements in a  pointer array can point  to any type of data.%@AE@%  %@NL@%
  6657. %@NL@%
  6658. The QCSORT.C example section uses a fairly simple array of pointers. But you
  6659. can use such arrays to create quite complex data structures. The basic form
  6660. of the array is always the same─it is a group of pointer variables, each
  6661. pointer accessible through a subscript─but the pointers in an array can
  6662. point to any kind of data object. You can have an array of pointers to
  6663. structures, an array of pointers to strings, and so on. The only difference
  6664. is in what the pointers reference.  %@NL@%
  6665. %@NL@%
  6666. Don't confuse an array of pointers with a pointer to an array. A pointer to
  6667. an array (or "array pointer") is a single pointer variable that points to an
  6668. array element. The single pointer can access any element of the array, but
  6669. only one pointer is involved.  %@NL@%
  6670. %@NL@%
  6671. In contrast, an array of pointers is a group of related pointer variables
  6672. stored in an array. Each element in the array is a pointer, and you can
  6673. access individual pointers with the array name and subscript. Each pointer
  6674. in the array points, in turn, to some other object.  %@NL@%
  6675. %@NL@%
  6676. %@NL@%
  6677. %@2@%%@CR:C6A00080017 @%%@AB@%A Pause for Reflection%@AE@%%@EH@%%@NL@%
  6678. %@NL@%
  6679. If this is your first exposure to pointers, you may want to reflect on what
  6680. you have learned before reading the next chapter. This chapter has explained
  6681. the basic uses of pointers, and you can write a great many useful programs
  6682. using only these techniques. If you're not comfortable with all these ideas,
  6683. you may want to experiment with them before reading more about pointers.  %@NL@%
  6684. %@NL@%
  6685. The next chapter, "Advanced Pointers," examines further uses of pointers,
  6686. including multiple indirection and pointers to structures.  %@NL@%
  6687. %@NL@%
  6688. %@NL@%
  6689. %@NL@%
  6690. %@NL@%
  6691. %@NL@%
  6692. %@NL@%
  6693. %@CR:C6A00090001 @%%@1@%%@AB@%Chapter 9  Advanced Pointers%@AE@%%@EH@%%@NL@%
  6694. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  6695. %@NL@%
  6696. The preceding chapter, "Pointers," explained the basics of using pointers─
  6697. how to declare and initialize pointer variables and use them to access basic
  6698. data types. This chapter explores more advanced pointer techniques,
  6699. including multiple indirection, pointers to structures, and pointers to
  6700. functions.  %@NL@%
  6701. %@NL@%
  6702. %@NL@%
  6703. %@2@%%@CR:C6A00090002 @%%@AB@%Pointers to Pointers%@AE@%%@EH@%%@NL@%
  6704. %@NL@%
  6705. In Chapter 8, "Pointers," we stated a pointer can point to any kind of
  6706. variable. Since a pointer is a variable, you can make it the target of
  6707. another pointer, creating a pointer to a pointer. This concept is useful in
  6708. itself and is also important for understanding the equivalence of array
  6709. notation and pointer notation, which is explained in the next section.  %@NL@%
  6710. %@NL@%
  6711. The program PTRPTR.C demonstrates a pointer to a pointer insimple terms:  %@NL@%
  6712. %@NL@%
  6713. %@AS@%  /* PTRPTR.C: Demonstrate a pointer to a pointer. */
  6714. %@AS@%  
  6715. %@AS@%  #include <stdio.h>
  6716. %@AS@%  
  6717. %@AS@%  main()
  6718. %@AS@%  {
  6719. %@AS@%     int val = 501;
  6720. %@AS@%     int *ptr = &val;
  6721. %@AS@%     int **ptr_ptr = &ptr;
  6722. %@AS@%     printf( "val = %d\n", **ptr_ptr );
  6723. %@AS@%  }%@AE@%%@NL@%
  6724. %@NL@%
  6725. Here is the output from PTRPTR.C:  %@NL@%
  6726. %@NL@%
  6727. %@AS@%  val = 501%@AE@%%@NL@%
  6728. %@NL@%
  6729. The first two statements in PTRPTR.C should look familiar by now. They
  6730. create an %@AB@%int%@AE@% variable named %@AS@% val %@AE@% and an %@AB@%int%@AE@% pointer named %@AS@% ptr%@AE@%. The third
  6731. line, however, requires some explanation:  %@NL@%
  6732. %@NL@%
  6733. %@AS@%  int **ptr_ptr = &ptr;%@AE@%%@NL@%
  6734. %@NL@%
  6735. This statement uses double indirection to create a variable named %@AS@% ptr_ptr%@AE@%,
  6736. which is a pointer to a pointer. This pointer is assigned the address of the
  6737. first pointer, %@AS@% ptr%@AE@%. The pointer %@AS@% ptr %@AE@% references %@AS@% val%@AE@%, and the pointer %@AS@%
  6738. %@AS@%ptr_ptr %@AE@% references %@AS@% ptr%@AE@%. Figure 9.1 illustrates the relationship between %@AS@%
  6739. %@AS@%ptr %@AE@% and %@AS@% ptr_ptr%@AE@%.  %@NL@%
  6740. %@NL@%
  6741. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  6742. %@NL@%
  6743. Once we have initialized both pointers, we can use %@AS@% ptr_ptr %@AE@% to access %@AS@% val%@AE@%:
  6744. %@NL@%
  6745. %@NL@%
  6746. %@AS@%  **ptr_ptr%@AE@%%@NL@%
  6747. %@NL@%
  6748. %@AU@% The double indirection operator (%@AE@%%@AB@% %@AE@%%@AB@%** %@AE@%) is used with a pointer to a pointer.
  6749. %@NL@%
  6750. %@NL@%
  6751. The double indirection operator (%@AB@%**%@AE@%) in front of %@AS@% ptr_ptr %@AE@% tells two things
  6752. about %@AS@%ptr_ptr%@AE@%: that %@AS@% ptr_ptr %@AE@% is itself a pointer and it points to a second
  6753. pointer. Both asterisks are needed to access the contents of %@AS@% val%@AE@%. If you
  6754. use only one, as in  %@NL@%
  6755. %@NL@%
  6756. %@AS@%  *ptr_ptr%@AE@%%@NL@%
  6757. %@NL@%
  6758. then %@AS@% ptr_ptr %@AE@% accesses the contents of %@AS@% ptr%@AE@%, which is the address of %@AS@% val%@AE@%.
  6759. This statement, for instance, prints the address stored in %@AS@% ptr%@AE@%:  %@NL@%
  6760. %@NL@%
  6761. %@AS@%  printf( "ptr = %u", *ptr_ptr );%@AE@%%@NL@%
  6762. %@NL@%
  6763. Using pointers to pointers is known as "multiple indirection." One pointer
  6764. points to a second pointer, which in turn accesses a third data object. In
  6765. theory, there's no limit to how far you can take multiple indirection. You
  6766. can create pointers to pointers, pointers to pointers to pointers, and so
  6767. on. However, there's rarely any practical reason to carry indirection beyond
  6768. two levels (a pointer to a pointer).  %@NL@%
  6769. %@NL@%
  6770. %@NL@%
  6771. %@3@%%@CR:C6A00090003 @%%@AB@%Equivalence of Array and Pointer Notation%@AE@%%@EH@%%@NL@%
  6772. %@NL@%
  6773. In previous sections we noted, more or less in passing, two important facts
  6774. about arrays and pointers:  %@NL@%
  6775. %@NL@%
  6776. %@NL@%
  6777.   1.  An array name is actually a pointer.%@NL@%
  6778. %@NL@%
  6779.   2.  Array notation (subscripts) and pointer notation are interchangeable.%@NL@%
  6780. %@NL@%
  6781. %@NL@%
  6782. These ideas are significant enough to warrant an explicit demonstration.
  6783. Let's rewrite the QCSORT.C program using pointer notation:  %@NL@%
  6784. %@NL@%
  6785. %@AS@%  /* QCSORT1.C: Demonstrate sort with pointer notation. */
  6786. %@AS@%  
  6787. %@AS@%  #include <stdio.h>
  6788. %@AS@%  #define SIZE 4
  6789. %@AS@%  
  6790. %@AS@%  void sort( int size, double **p );
  6791. %@AS@%  void show( int size, double **p, double dd[] );
  6792. %@AS@%  
  6793. %@AS@%  main()
  6794. %@AS@%  {
  6795. %@AS@%     int x;
  6796. %@AS@%     double d[] = { 3.333, 1.111, 2.222, 4.444 };
  6797. %@AS@%     double *d_ptr[SIZE];
  6798. %@AS@%     for( x = 0; x < SIZE; x++ )
  6799. %@AS@%        d_ptr[x] = &d[x];
  6800. %@AS@%     show( SIZE, d_ptr, d );
  6801. %@AS@%     sort( SIZE, d_ptr );
  6802. %@AS@%     show( SIZE, d_ptr, d );
  6803. %@AS@%  }%@AE@%%@NL@%
  6804. %@NL@%
  6805. %@AS@%  void sort( int size, double **p )
  6806. %@AS@%  {
  6807. %@AS@%     int x, x1;
  6808. %@AS@%     double *temp;
  6809. %@AS@%     for( x = 0; x < size - 1; x++ )
  6810. %@AS@%        for( x1 = x + 1; x1 < size; x1++ )
  6811. %@AS@%        {
  6812. %@AS@%           if( **(p+x) > **(p+x1) )
  6813. %@AS@%           {
  6814. %@AS@%              temp = *(p+x1);
  6815. %@AS@%              *(p+x1) = *(p+x);
  6816. %@AS@%              *(p+x) = temp;
  6817. %@AS@%           }
  6818. %@AS@%        }
  6819. %@AS@%  }
  6820. %@AS@%  
  6821. %@AS@%  void show( int size, double **p, double dd[] )
  6822. %@AS@%  {
  6823. %@AS@%     int x;
  6824. %@AS@%     printf( "------------------------" );
  6825. %@AS@%     printf( "------------------------\n" );
  6826. %@AS@%     for( x = 0; x < size; x++ )
  6827. %@AS@%     {
  6828. %@AS@%        printf( "*d_ptr[%d] = %1.3f   ", x, **(p+x) );
  6829. %@AS@%        printf( "d_ptr[%d] = %u ", x, *(p+x) );
  6830. %@AS@%        printf( "  d[%d] = %1.3f\n", x, dd[x] );
  6831. %@AS@%     }
  6832. %@AS@%  }%@AE@%%@NL@%
  6833. %@NL@%
  6834. The QCSORT1.C program works like its predecessor, QCSORT.C. (It sorts an
  6835. array of pointers that point to elements in an %@AB@%int%@AE@% array.) The only
  6836. difference is QCSORT1.C uses pointer notation instead of array notation.  %@NL@%
  6837. %@NL@%
  6838. Let's look at how the change affects the %@AS@% sort %@AE@% function, beginning with its
  6839. prototype. In the previous program, QCSORT.C, the prototype  %@NL@%
  6840. %@NL@%
  6841. %@AS@%  void sort( int size, double *p[] );%@AE@%%@NL@%
  6842. %@NL@%
  6843. uses array notation to show we'll pass the name of an array of pointers to %@AS@%
  6844. %@AS@%sort%@AE@%. Since an array name is a pointer, we can rewrite the prototype using
  6845. pointer notation, as in QCSORT1.C:  %@NL@%
  6846. %@NL@%
  6847. %@AS@%  void sort( int size, double **p );%@AE@%%@NL@%
  6848. %@NL@%
  6849. The %@AS@% sort %@AE@% function definition is rewritten in the same way. Here is the
  6850. definition of %@AS@% sort %@AE@% in the original program (QCSORT.C):  %@NL@%
  6851. %@NL@%
  6852. %@AS@%  void sort( int size, double *p[] )
  6853. %@AS@%  {
  6854. %@AS@%     int x, x1;
  6855. %@AS@%     double *temp;
  6856. %@AS@%     for( x = 0; x < size - 1; x++ )
  6857. %@AS@%        for( x1 = x + 1; x1 < size; x1++ )
  6858. %@AS@%        {
  6859. %@AS@%           if( *p[x] > *p[x1] )
  6860. %@AS@%           {
  6861. %@AS@%           temp = p[x1];
  6862. %@AS@%           p[x1] = p[x];
  6863. %@AS@%           p[x] = temp;
  6864. %@AS@%           }
  6865. %@AS@%        }
  6866. %@AS@%  }%@AE@%%@NL@%
  6867. %@NL@%
  6868. The same function using pointers looks like this in QCSORT1.C:  %@NL@%
  6869. %@NL@%
  6870. %@AS@%  void sort( int size, double **p )
  6871. %@AS@%  {
  6872. %@AS@%     int x, x1;
  6873. %@AS@%     double *temp;
  6874. %@AS@%     for( x = 0; x < size - 1; x++ )
  6875. %@AS@%        for( x1 = x + 1; x1 < size; x1++ )
  6876. %@AS@%        {
  6877. %@AS@%           if( **(p+x) > **(p+x1) )
  6878. %@AS@%           {
  6879. %@AS@%              temp = *(p+x1);
  6880. %@AS@%              *(p+x1) = *(p+x);
  6881. %@AS@%              *(p+x) = temp;
  6882. %@AS@%           }
  6883. %@AS@%        }
  6884. %@AS@%  }%@AE@%%@NL@%
  6885. %@NL@%
  6886. Within the %@AS@% sort %@AE@% function, the variable %@AS@% p %@AE@% is a pointer to a pointer. When
  6887. we use a single asterisk, as in,  %@NL@%
  6888. %@NL@%
  6889. %@AS@%  *(p+x1)%@AE@%%@NL@%
  6890. %@NL@%
  6891. we access the contents of the %@AS@% x1 %@AE@% pointer, which is an address. When we
  6892. place a double asterisk in front of an address value, as in,  %@NL@%
  6893. %@NL@%
  6894. %@AS@%  **(p+x)%@AE@%%@NL@%
  6895. %@NL@%
  6896. we access the contents of this address.  %@NL@%
  6897. %@NL@%
  6898. Using pointer notation in place of array notation, QCSORT1.C achieves the
  6899. same result as QCSORT.C. In many cases─including this one─it doesn't really
  6900. matter which notation you use. If you're still more comfortable with array
  6901. notation, you may prefer to use it sometimes. Since many C programs use
  6902. pointers to manipulate arrays, however, it's worth taking the time to learn
  6903. pointer notation, too.  %@NL@%
  6904. %@NL@%
  6905. %@NL@%
  6906. %@3@%%@CR:C6A00090004 @%%@AB@%Getting Command-Line Arguments%@AE@%%@EH@%%@NL@%
  6907. %@NL@%
  6908. %@AU@%  Command-line arguments are passed to programs through argv, an array of
  6909. %@AU@%pointers.%@AE@%  %@NL@%
  6910. %@NL@%
  6911. Arrays of pointers have one very common use─accessing command-line
  6912. arguments. When a C program begins execution, DOS passes two arguments to
  6913. it. The first argument, normally called %@AS@% argc%@AE@%, is an %@AB@%int%@AE@% variable that
  6914. indicates the number of command-line arguments. The second, normally called %@AS@%
  6915. %@AS@%argv%@AE@%, is a pointer to an array of strings. Each string in the array contains
  6916. one of the command-line arguments.  %@NL@%
  6917. %@NL@%
  6918. Even if you don't plan to use %@AS@% argc %@AE@% and %@AS@% argv %@AE@% in your programs, you can
  6919. expect to see them often in other C programs, so it's useful to know how
  6920. they're used. The ARGV.C program uses %@AS@% argc %@AE@% and %@AS@% argv%@AE@%.  %@NL@%
  6921. %@NL@%
  6922. %@AS@%  /* ARGV.C: Demonstrate accessing command-line arguments. */
  6923. %@AS@%  
  6924. %@AS@%  #include <stdio.h>
  6925. %@AS@%  
  6926. %@AS@%  void show_args( char *argument );
  6927. %@AS@%  
  6928. %@AS@%  int main( int argc, char *argv[] )
  6929. %@AS@%  {
  6930. %@AS@%     int count;
  6931. %@AS@%     for( count=0; count < argc; count++ )
  6932. %@AS@%        show_args( argv[count] );
  6933. %@AS@%     return 0;
  6934. %@AS@%  }
  6935. %@AS@%  
  6936. %@AS@%  void show_args( char *argument )
  6937. %@AS@%  {
  6938. %@AS@%     printf( "%s\n", argument );
  6939. %@AS@%  }%@AE@%%@NL@%
  6940. %@NL@%
  6941. To make ARGV.C produce output, you must give it some command-line arguments.
  6942. (If you run ARGV.C in the QuickC environment, select Run/Debug from the
  6943. Options menu and type the command-line arguments at the Command Line
  6944. prompt.) The program prints each argument on the screen.  %@NL@%
  6945. %@NL@%
  6946. If you use this command line, for instance,  %@NL@%
  6947. %@NL@%
  6948. %@AS@%  argv harpo chico groucho zeppo%@AE@%%@NL@%
  6949. %@NL@%
  6950. then ARGV.C gives this output:  %@NL@%
  6951. %@NL@%
  6952. %@AS@%  C:\SOURCES\ARGV.EXE
  6953. %@AS@%  harpo
  6954. %@AS@%  chico
  6955. %@AS@%  groucho
  6956. %@AS@%  zeppo%@AE@%%@NL@%
  6957. %@NL@%
  6958. The first argument may have surprised you. In DOS versions 3.0 and higher,
  6959. the first string in the %@AS@% argv %@AE@% array ( %@AS@%argv[0]%@AE@% ) contains the drive
  6960. specification and full pathname to the program that is executing. The drive
  6961. and path you see will depend on how your system is configured. In the
  6962. example the ARGV.EXE program is located in the SOURCES directory of drive C.
  6963. %@NL@%
  6964. %@NL@%
  6965. Thus, the value of %@AS@% argc %@AE@% actually is one greater than the number of
  6966. command-line arguments, and the first argument typed on the command line is
  6967. the second string in the array (%@AS@% argv[1] %@AE@%). If you type the arguments shown
  6968. above, the value of %@AS@% argc %@AE@% is 5 and %@AS@% argv[1] %@AE@% contains the argument %@AS@% harpo%@AE@%.
  6969. %@NL@%
  6970. %@NL@%
  6971. %@NL@%
  6972. %@3@%%@CR:C6A00090005 @%%@AB@%Null Pointers%@AE@%%@EH@%%@NL@%
  6973. %@NL@%
  6974. We can use the ARGV.C program to illustrate another handy property of
  6975. pointers: null pointers. Consider this modification (ARGV1.C):  %@NL@%
  6976. %@NL@%
  6977. %@AS@%  /* ARGV1.C: Demonstrate null pointers. */
  6978. %@AS@%  
  6979. %@AS@%  #include <stdio.h>
  6980. %@AS@%  
  6981. %@AS@%  void show_args( char *argument );
  6982. %@AS@%  
  6983. %@AS@%  int main( int argc, char **argv )
  6984. %@AS@%  {
  6985. %@AS@%     while( *argv )
  6986. %@AS@%        show_args( *(argv++) );
  6987. %@AS@%     return 0;
  6988. %@AS@%  }
  6989. %@AS@%  
  6990. %@AS@%  void show_args( char *argument )
  6991. %@AS@%  {
  6992. %@AS@%     printf( "%s\n", argument );
  6993. %@AS@%  }%@AE@%%@NL@%
  6994. %@NL@%
  6995. The ARGV1.C program gives the same output as the previous program but it
  6996. uses a %@AB@%while%@AE@% loop instead of a %@AB@%for%@AE@% loop. The test expression in this loop,  %@NL@%
  6997. %@NL@%
  6998. %@AS@%  while( *argv )%@AE@%%@NL@%
  6999. %@NL@%
  7000. is equivalent to this test expression:  %@NL@%
  7001. %@NL@%
  7002. %@AS@%  while( *argv != 0 )%@AE@%%@NL@%
  7003. %@NL@%
  7004. The loop in ARGV1.C continues until it finds a "null pointer," a pointer
  7005. that contains 0. In this case, the null pointer means we have reached the
  7006. end of the array: no more strings are available.  %@NL@%
  7007. %@NL@%
  7008. %@AU@% Null pointers can be used to show success or failure and as markers in a
  7009. %@AU@%series.%@AE@%  %@NL@%
  7010. %@NL@%
  7011. Many C library functions use null pointers to signal the success or failure
  7012. of an operation that returns a pointer. For instance, the library function
  7013. %@AB@%malloc%@AE@% normally returns a pointer to the beginning address of the memory
  7014. area it allocates. If no memory is available, %@AB@%malloc%@AE@% returns a null pointer
  7015. to show the operation failed. Similarly, the %@AB@%fopen%@AE@% function usually returns
  7016. a pointer to a %@AB@%FILE%@AE@% structure, but returns a null pointer when it fails.  %@NL@%
  7017. %@NL@%
  7018. Null pointers can also be used to mark the end of a list of pointers, such
  7019. as the %@AS@% argv %@AE@% array or a linked list.  %@NL@%
  7020. %@NL@%
  7021. %@NL@%
  7022. %@2@%%@CR:C6A00090006 @%%@AB@%Pointers to Structures%@AE@%%@EH@%%@NL@%
  7023. %@NL@%
  7024. %@AU@% A structure pointer can access any member of a structure.%@AE@%  %@NL@%
  7025. %@NL@%
  7026. A pointer to a structure, or "structure pointer," is conceptually similar to
  7027. an array pointer. Just as an array pointer can point to any element in an
  7028. array, a structure pointer can reference any member in a structure. The
  7029. major difference is one of notation.  %@NL@%
  7030. %@NL@%
  7031. In case you're not yet an expert on structure notation, let's review it very
  7032. briefly. First recall that each element in an array has the same type, so
  7033. you refer to individual array elements with subscripts:  %@NL@%
  7034. %@NL@%
  7035. %@AS@%  i_array[3]%@AE@%%@NL@%
  7036. %@NL@%
  7037. Because members of a structure can have different types, you can't use
  7038. numerical subscripts to refer to them based on their order. Instead, each
  7039. structure member has a symbolic name. You refer to a member with a structure
  7040. name and member name, separating the two names with the member-of operator
  7041. (%@AB@%.%@AE@%):  %@NL@%
  7042. %@NL@%
  7043. %@AS@%  jones.name%@AE@%%@NL@%
  7044. %@NL@%
  7045. The notation for structure pointers follows the same pattern, with only two
  7046. differences. You must  %@NL@%
  7047. %@NL@%
  7048. %@NL@%
  7049.   1.  Replace the structure name with the name of the pointer%@NL@%
  7050. %@NL@%
  7051.   2.  Replace the member-of operator with a two-character operator called
  7052.       the "pointer-member" operator (%@AB@%->%@AE@%)%@NL@%
  7053. %@NL@%
  7054. %@NL@%
  7055. The pointer-member operator is formed by a dash and a right-angle bracket.
  7056. The following name uses the pointer-member operator:  %@NL@%
  7057. %@NL@%
  7058. %@AS@%  jones_ptr->name%@AE@%%@NL@%
  7059. %@NL@%
  7060. Here %@AS@% jones_ptr %@AE@% is the name of a pointer to a structure, and %@AS@% name %@AE@% is a
  7061. member of the structure that %@AS@% jones_ptr %@AE@% points to.  %@NL@%
  7062. %@NL@%
  7063. The EMPLOY1.C program is a revision of the EMPLOYEE.C program that
  7064. demonstrates structures in Chapter 4, "Data Types." This program illustrates
  7065. how to manipulate a structure through a pointer:  %@NL@%
  7066. %@NL@%
  7067. %@AS@%  /* EMPLOY1.C: Demonstrate structure pointers. */
  7068. %@AS@%  
  7069. %@AS@%  #include <stdio.h>
  7070. %@AS@%  
  7071. %@AS@%  struct employee
  7072. %@AS@%  {
  7073. %@AS@%     char name[10];
  7074. %@AS@%     int months;
  7075. %@AS@%     float wage;
  7076. %@AS@%  };
  7077. %@AS@%  
  7078. %@AS@%  void display( struct employee *e_ptr  );
  7079. %@AS@%  
  7080. %@AS@%  main()
  7081. %@AS@%  {
  7082. %@AS@%     struct employee jones =
  7083. %@AS@%     {
  7084. %@AS@%        "Jones, J",
  7085. %@AS@%        77,
  7086. %@AS@%        13.68
  7087. %@AS@%     };
  7088. %@AS@%  
  7089. %@AS@%     display( &jones );
  7090. %@AS@%  }
  7091. %@AS@%  
  7092. %@AS@%  void display( struct employee *e_ptr )
  7093. %@AS@%  {
  7094. %@AS@%     printf( "Name: %s\n", e_ptr->name );
  7095. %@AS@%     printf( "Months of service: %d\n", e_ptr->months );
  7096. %@AS@%     printf( "Hourly wage: %6.2f\n", e_ptr->wage );
  7097. %@AS@%  }%@AE@%%@NL@%
  7098. %@NL@%
  7099. %@AU@% Structure pointers allow  functions to access  structures that are local
  7100. %@AU@%to other functions.%@AE@%  %@NL@%
  7101. %@NL@%
  7102. The EMPLOY1.C program gives the same output as the earlier version. But
  7103. instead of passing the entire structure to the %@AS@% display %@AE@% function, this
  7104. program passes a structure pointer. This method conserves memory, since the %@AS@%
  7105. %@AS@%display %@AE@% function doesn't create a local copy of the structure. It also
  7106. allows %@AS@% display %@AE@% to change members in the original structure, which is local
  7107. to the %@AB@%main%@AE@% function.  %@NL@%
  7108. %@NL@%
  7109. The header of the %@AS@% display %@AE@% function shows that the function expects to
  7110. receive a structure pointer:  %@NL@%
  7111. %@NL@%
  7112. %@AS@%  void display( struct employee *e_ptr  )%@AE@%%@NL@%
  7113. %@NL@%
  7114. The expression in parentheses specifies what type of value the function
  7115. expects. This expression is a bit complex, so let's look at each part
  7116. individually. The expression %@AS@% *e_ptr %@AE@% indicates the function expects to
  7117. receive a pointer, which it names %@AS@% e_ptr%@AE@%. It is preceded by  %@NL@%
  7118. %@NL@%
  7119. %@AS@%  struct employee%@AE@%%@NL@%
  7120. %@NL@%
  7121. which states what type of pointer %@AS@% e_ptr %@AE@% is. The %@AB@%struct%@AE@% keyword indicates %@AS@%
  7122. %@AS@%e_ptr %@AE@% is a pointer to a structure, and the tag %@AS@% employee %@AE@% specifies the
  7123. structure type.  %@NL@%
  7124. %@NL@%
  7125. The next item of interest in EMPLOY1.C is the function call that passes the
  7126. structure pointer:  %@NL@%
  7127. %@NL@%
  7128. %@AS@%  display( &jones );%@AE@%%@NL@%
  7129. %@NL@%
  7130. This statement uses the address-of operator to pass the address of the %@AS@%
  7131. %@AS@%jones %@AE@% structure to the %@AS@% display %@AE@% function. The address-of operator is not
  7132. optional. Since we want the function to access the original structure─not a
  7133. local copy─we must pass the structure's address.  %@NL@%
  7134. %@NL@%
  7135. When the %@AS@% display %@AE@% function executes, it creates a pointer variable named %@AS@%
  7136. %@AS@%e_ptr %@AE@% and assigns to it the address passed in the function call. Now the %@AS@%
  7137. %@AS@%display %@AE@% function can refer to any member of the structure indirectly
  7138. through the pointer %@AS@% e_ptr%@AE@%. Within the %@AS@% display %@AE@% function, the statement  %@NL@%
  7139. %@NL@%
  7140. %@AS@%  printf( "%s\n", e_ptr->name );%@AE@%%@NL@%
  7141. %@NL@%
  7142. has the same effect that the statement  %@NL@%
  7143. %@NL@%
  7144. %@AS@%  printf( "%s\n", jones.name );%@AE@%%@NL@%
  7145. %@NL@%
  7146. has in the %@AB@%main%@AE@% function. Figure 9.2 illustrates the relationship between
  7147. the structure pointer and structure members in EMPLOY1.C.  %@NL@%
  7148. %@NL@%
  7149. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  7150. %@NL@%
  7151. Just to confirm that the %@AS@% display %@AE@% function can access the original
  7152. structure in EMPLOY1.C, try adding this statement to the end of the %@AS@% display
  7153. %@AS@%%@AE@% function:  %@NL@%
  7154. %@NL@%
  7155. %@AS@%  strcpy( e_ptr->name, "King, M" );%@AE@%%@NL@%
  7156. %@NL@%
  7157. and this statement to the end of the %@AB@%main%@AE@% function:  %@NL@%
  7158. %@NL@%
  7159. %@AS@%  printf( "%s\n", jones.name );%@AE@%%@NL@%
  7160. %@NL@%
  7161. These changes cause EMPLOY1.C to print:  %@NL@%
  7162. %@NL@%
  7163. %@AS@%  King, M%@AE@%%@NL@%
  7164. %@NL@%
  7165. Acting indirectly through a structure pointer, the %@AS@% display %@AE@% function was
  7166. able to change a structure defined elsewhere in the program.  %@NL@%
  7167. %@NL@%
  7168. %@NL@%
  7169. %@2@%%@CR:C6A00090007 @%%@AB@%Pointers to Functions%@AE@%%@EH@%%@NL@%
  7170. %@NL@%
  7171. At the beginning of the previous chapter we stated that a pointer can point
  7172. to any object present in memory at run time. Since functions themselves are
  7173. located in memory, you can assign the address of a function to a pointer,
  7174. creating a "function pointer."  %@NL@%
  7175. %@NL@%
  7176. %@AU@% A function pointer makes it possible to pass a function as a function
  7177. %@AU@%argument.%@AE@%  %@NL@%
  7178. %@NL@%
  7179. Function pointers provide a way─in fact, the only practical way─to pass a
  7180. function as an argument to another function. This permits the second
  7181. function to call the first function indirectly through the pointer.  %@NL@%
  7182. %@NL@%
  7183. While function pointers may sound rather obscure, they have some common
  7184. practical uses:  %@NL@%
  7185. %@NL@%
  7186. %@NL@%
  7187.   ■   Some QuickC run-time library functions, such as %@AB@%qsort%@AE@%, expect to
  7188.       receive a pointer to a user-defined function in your program. (Online
  7189.       help includes an example program that uses %@AB@%qsort%@AE@%.)%@NL@%
  7190. %@NL@%
  7191.   ■   Function pointers are used extensively in Windows and OS/2
  7192.       Presentation Manager programs. %@NL@%
  7193. %@NL@%
  7194.   ■   Using an array of function pointers, you can create a "dispatch
  7195.       table." A dispatch table is a list of related functions that can be
  7196.       called based on some choice made at run time. It is similar to an %@AB@%ON
  7197. %@AB@%      GOSUB%@AE@% statement in BASIC or a call table in assembly language.%@NL@%
  7198. %@NL@%
  7199. %@NL@%
  7200. The syntax for function pointers is a bit complex, so let's start with a
  7201. simple example. The FUNCPTR.C program creates a pointer to our old friend,
  7202. %@AB@%printf%@AE@%, and calls %@AB@%printf%@AE@% through the pointer:  %@NL@%
  7203. %@NL@%
  7204. %@AS@%  /* FUNCPTR.C: Demonstrate function pointers. */
  7205. %@AS@%  
  7206. %@AS@%  #include <stdio.h>
  7207. %@AS@%  
  7208. %@AS@%  main()
  7209. %@AS@%  {
  7210. %@AS@%     int (*func_ptr) ();
  7211. %@AS@%     func_ptr = printf;
  7212. %@AS@%     (*func_ptr) ( "Curiouser and curiouser...\n" );
  7213. %@AS@%  }%@AE@%%@NL@%
  7214. %@NL@%
  7215. Here is the output from FUNCPTR.C:  %@NL@%
  7216. %@NL@%
  7217. %@AS@%  Curiouser and curiouser...%@AE@%%@NL@%
  7218. %@NL@%
  7219. This line from FUNCPTR.C declares %@AS@% func_ptr %@AE@% as a pointer to a function:  %@NL@%
  7220. %@NL@%
  7221. %@AS@%  int (*func_ptr) ();%@AE@%%@NL@%
  7222. %@NL@%
  7223. The declaration of a function pointer must use the same type specifier as
  7224. the function it references. If the function returns a %@AB@%float%@AE@% value, the
  7225. pointer uses type %@AB@%float%@AE@%, and so on. Since the %@AB@%printf%@AE@% function returns an %@AB@%int%@AE@%
  7226. value showing how many characters it displays, the declaration of %@AS@% func_ptr %@AE@%
  7227. uses the type %@AB@%int%@AE@%.  %@NL@%
  7228. %@NL@%
  7229. %@AU@% A function-pointer declaration must have two pairs of parentheses.%@AE@%  %@NL@%
  7230. %@NL@%
  7231. Function-pointer declarations may look complex, but all the parentheses are
  7232. essential. The empty parentheses at the end of the declaration are needed to
  7233. show the pointer points to a function.  %@NL@%
  7234. %@NL@%
  7235. The parentheses enclosing the function name itself are mandatory, too.
  7236. Notice what happens if you omit them:  %@NL@%
  7237. %@NL@%
  7238. %@AS@%  void *func_ptr(); /* Error! Not a function pointer. */%@AE@%%@NL@%
  7239. %@NL@%
  7240. Instead of declaring a pointer to a function, this statement declares a
  7241. function that returns a pointer─not at all what we want in FUNCPTR.C.  %@NL@%
  7242. %@NL@%
  7243. The next program line initializes the function pointer, assigning it the
  7244. address of the %@AB@%printf%@AE@% function:  %@NL@%
  7245. %@NL@%
  7246. %@AS@%  func_ptr = printf;%@AE@%%@NL@%
  7247. %@NL@%
  7248. This line has two important features. First, notice the name %@AB@%printf%@AE@% isn't
  7249. followed by parentheses, as it would be when you call %@AB@%printf%@AE@% directly. We
  7250. want to obtain the address of %@AB@%printf%@AE@%, not call it.  %@NL@%
  7251. %@NL@%
  7252. Second, note that it's not necessary to place the address-of operator before
  7253. the name %@AB@%printf%@AE@%. Because %@AS@% func_ptr %@AE@% was declared as a function pointer, the
  7254. compiler knows it should use the address of %@AB@%printf%@AE@% here. If you like,
  7255. however, you can add the address-of operator to make the statement a little
  7256. more readable:  %@NL@%
  7257. %@NL@%
  7258. %@AS@%  func_ptr = &printf;%@AE@%%@NL@%
  7259. %@NL@%
  7260. The next line calls the %@AB@%printf%@AE@% function indirectly through the pointer %@AS@%
  7261. %@AS@%func_ptr%@AE@%:  %@NL@%
  7262. %@NL@%
  7263. %@AS@%  (*func_ptr) ( "Curiouser and curiouser...\n" );%@AE@%%@NL@%
  7264. %@NL@%
  7265. Note the similarity between this statement and a normal call to %@AB@%printf%@AE@%. It's
  7266. equivalent to this line:  %@NL@%
  7267. %@NL@%
  7268. %@AS@%  printf( "Curiouser and curiouser...\n" );%@AE@%%@NL@%
  7269. %@NL@%
  7270. To call %@AB@%printf%@AE@% indirectly through %@AS@% func_ptr%@AE@%, you supply the same arguments
  7271. as when you call %@AB@%printf%@AE@% directly.  %@NL@%
  7272. %@NL@%
  7273. %@NL@%
  7274. %@3@%%@CR:C6A00090008 @%%@AB@%Passing Function Pointers as Arguments%@AE@%%@EH@%%@NL@%
  7275. %@NL@%
  7276. %@AU@% Function pointers are usually passed as function arguments.%@AE@%  %@NL@%
  7277. %@NL@%
  7278. Like other pointers, function pointers can be passed as arguments to
  7279. functions. Normally, in fact, this is the only reason to use a function
  7280. pointer.  %@NL@%
  7281. %@NL@%
  7282. The FUNCPTR.C program in the previous section is easy to follow but not very
  7283. practical. In a real program, you wouldn't go to the trouble of creating a
  7284. function pointer just to call %@AB@%printf%@AE@% from the %@AB@%main%@AE@% function.  %@NL@%
  7285. %@NL@%
  7286. The FUNCPTR1.C program demonstrates how to pass a function pointer as an
  7287. argument. It has a function named %@AS@% gimme_func %@AE@% that expects to be passed a
  7288. function pointer:  %@NL@%
  7289. %@NL@%
  7290. %@AS@%  /* FUNCPTR1.C: Passing function pointers as arguments. */%@AE@%%@NL@%
  7291. %@NL@%
  7292. %@AS@%  #include <stdio.h>
  7293. %@AS@%  
  7294. %@AS@%  void gimme_func( void (*func_ptr) () );
  7295. %@AS@%  
  7296. %@AS@%  main()
  7297. %@AS@%  {
  7298. %@AS@%     gimme_func( puts );
  7299. %@AS@%     gimme_func( printf );
  7300. %@AS@%  }
  7301. %@AS@%  
  7302. %@AS@%  void gimme_func( void (*func_ptr) () )
  7303. %@AS@%  {
  7304. %@AS@%     (*func_ptr) ( "Ausgezeichnet!" );
  7305. %@AS@%  }%@AE@%%@NL@%
  7306. %@NL@%
  7307. Here is the output from FUNCPTR1.C:  %@NL@%
  7308. %@NL@%
  7309. %@AS@%  Ausgezeichnet!
  7310. %@AS@%  Ausgezeichnet!%@AE@%%@NL@%
  7311. %@NL@%
  7312. In the interests of brevity, the function %@AS@% gimme_func %@AE@% does a very simple
  7313. job. It expects to receive a pointer to a function that can display a string
  7314. and uses that pointer to print the string. The first call to %@AS@% gimme_func %@AE@%
  7315. passes a pointer to the library function %@AB@%puts%@AE@%, and the second passes a
  7316. pointer to %@AB@%printf%@AE@%.  %@NL@%
  7317. %@NL@%
  7318. Since the declaration of %@AS@% gimme_func %@AE@% states it takes a pointer to a
  7319. function, the address-of operator is optional in a call to %@AS@% gimme_func%@AE@%. The
  7320. following statements are equivalent:  %@NL@%
  7321. %@NL@%
  7322. %@AS@%  gimme_func( puts );
  7323. %@AS@%  gimme_func( &puts );%@AE@%%@NL@%
  7324. %@NL@%
  7325. %@NL@%
  7326. %@2@%%@CR:C6A00090009 @%%@AB@%A Parting Word on Pointers%@AE@%%@EH@%%@NL@%
  7327. %@NL@%
  7328. If you have read the previous two chapters from beginning to end, you may be
  7329. suffering from a mild─or perhaps not so mild─case of information overload.
  7330. Pointers have so many different uses that it's difficult to learn everything
  7331. about them at once.  %@NL@%
  7332. %@NL@%
  7333. Don't be discouraged if some uses of pointers still aren't clear to you. The
  7334. latter parts of this chapter cover some rather esoteric techniques, which
  7335. you probably won't use often. When needed, however, these techniques offer
  7336. some very powerful capabilities.  %@NL@%
  7337. %@NL@%
  7338. Like other programming concepts, pointers are best learned through practice,
  7339. so use them at every sensible opportunity. Remember, you don't need to know
  7340. everything about pointers in order to do %@AI@%something%@AE@% with them. The more you
  7341. use pointers in everyday programming, the sooner all the pieces of the
  7342. puzzle will fall into place.  %@NL@%
  7343. %@NL@%
  7344. %@NL@%
  7345. %@NL@%
  7346. %@NL@%
  7347. %@NL@%
  7348. %@NL@%
  7349. %@CR:C6A00100001 @%%@1@%%@AB@%Chapter 10  Programming Pitfalls%@AE@%%@EH@%%@NL@%
  7350. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  7351. %@NL@%
  7352. In C, as in every language, it's rare for any program to work perfectly the
  7353. first time. An important part of knowing a language is recognizing what %@AI@%not%@AE@%
  7354. to do and why certain problems occur.  %@NL@%
  7355. %@NL@%
  7356. This chapter describes common C programming pitfalls and how to avoid them.
  7357. It is organized under broad topics, such as "Pointer Problems," with a
  7358. category for miscellaneous problems at the end. The description of each
  7359. error gives a code example, explains why the error occurs, and offers a
  7360. solution.  %@NL@%
  7361. %@NL@%
  7362. %@NL@%
  7363. %@2@%%@CR:C6A00100002 @%%@AB@%Operator Problems%@AE@%%@EH@%%@NL@%
  7364. %@NL@%
  7365. The most common operator problems involve operators unique to C. Others
  7366. involve questions of precedence, which can cause problems in any language.  %@NL@%
  7367. %@NL@%
  7368. %@NL@%
  7369. %@3@%%@CR:C6A00100003 @%%@AB@%Confusing Assignment and Equality Operators%@AE@%%@EH@%%@NL@%
  7370. %@NL@%
  7371. A common error is to confuse the assignment operator (%@AB@%=%@AE@%) with the equality
  7372. operator (%@AB@%==%@AE@%). The mistake often occurs in decision-making statements:  %@NL@%
  7373. %@NL@%
  7374. %@AS@%  int val = 555;
  7375. %@AS@%  if( val = 20 ) /* Error! */
  7376. %@AS@%     printf( "val equals 20\n" );%@AE@%%@NL@%
  7377. %@NL@%
  7378. The above code prints %@AS@% val equals 20 %@AE@% even though it's clear %@AS@% val %@AE@% doesn't
  7379. equal 20 when the %@AB@%if%@AE@% statement begins. Instead of testing whether %@AS@% x %@AE@% equals
  7380. 20, the expression %@AS@% val = 20 %@AE@% %@AI@%assigns%@AE@% the value 20 to %@AS@% val%@AE@%.  %@NL@%
  7381. %@NL@%
  7382. Remember, the single equal sign (=) performs an assignment in C. This
  7383. particular assignment results in a nonzero value, so the %@AB@%if%@AE@% test is
  7384. evaluated as true, causing the %@AB@%printf%@AE@% statement to execute.  %@NL@%
  7385. %@NL@%
  7386. To correct the problem, use the double equal sign (%@AB@%==%@AE@%) to test equality:  %@NL@%
  7387. %@NL@%
  7388. %@AS@%  if( x == 20 )
  7389. %@AS@%     printf( "x equals 20\n" );%@AE@%%@NL@%
  7390. %@NL@%
  7391. Once you're in the habit of using the equality operator, you might make the
  7392. opposite mistake of using two equal signs where you should use only one:  %@NL@%
  7393. %@NL@%
  7394. %@AS@%  main()
  7395. %@AS@%  {
  7396. %@AS@%     int val;
  7397. %@AS@%     for( val == 0; val < 5; val++ )  /* Error! */
  7398. %@AS@%        printf( "val = %d\n", val );
  7399. %@AS@%  }%@AE@%%@NL@%
  7400. %@NL@%
  7401. Here the error appears in the initializing expression of the %@AB@%for%@AE@% statement.
  7402. It's the reverse of what happened in the first example. Instead of assigning
  7403. the value 0 to %@AS@% val%@AE@%, the expression %@AS@% val == 0 %@AE@% evaluates whether or not %@AS@% val
  7404. %@AS@%%@AE@% equals 0. The expression doesn't change the value of %@AS@% val %@AE@% at all. Since %@AS@%
  7405. %@AS@%val %@AE@% is an uninitialized variable, the %@AB@%for%@AE@% loop is unpredictable.  %@NL@%
  7406. %@NL@%
  7407. %@NL@%
  7408. %@3@%%@CR:C6A00100004 @%%@AB@%Confusing Operator Precedence%@AE@%%@EH@%%@NL@%
  7409. %@NL@%
  7410. Peculiar things can happen if you ignore operator precedence:  %@NL@%
  7411. %@NL@%
  7412. %@AS@%  main()
  7413. %@AS@%  {
  7414. %@AS@%     int ch;
  7415. %@AS@%     while( ch = getch() != '\r' )
  7416. %@AS@%        printf( "%d\n", ch );
  7417. %@AS@%  }%@AE@%%@NL@%
  7418. %@NL@%
  7419. Instead of assigning the result of the %@AB@%getch%@AE@% library-function call to %@AS@% ch%@AE@%,
  7420. the above code assigns the value 0 to %@AS@% ch %@AE@% when you press the ENTER key and
  7421. the value 1 when you press any other key. (The values 1 and 0 represent true
  7422. and false.)  %@NL@%
  7423. %@NL@%
  7424. The error occurs because the inequality operator (%@AB@%!=%@AE@%) has higher precedence
  7425. than the assignment operator (%@AB@%=%@AE@%). The expression  %@NL@%
  7426. %@NL@%
  7427. %@AS@%  ch = getch() != '\r'%@AE@%%@NL@%
  7428. %@NL@%
  7429. is the same as  %@NL@%
  7430. %@NL@%
  7431. %@AS@%  ch = (getch() != '\r')%@AE@%%@NL@%
  7432. %@NL@%
  7433. Both expressions compare the result of the %@AB@%getch%@AE@% call to the character
  7434. constant %@AB@%\r%@AE@%. The result of that comparison is then assigned to %@AS@% ch%@AE@%.  %@NL@%
  7435. %@NL@%
  7436. For the program to work correctly, these operations must happen in the
  7437. reverse order. The result of the function call must be assigned to the
  7438. variable %@AI@%before%@AE@% the variable is compared to the constant. We can solve the
  7439. problem by adding parentheses:  %@NL@%
  7440. %@NL@%
  7441. %@AS@%  main()
  7442. %@AS@%  {
  7443. %@AS@%     int ch;
  7444. %@AS@%     while( (ch = getch()) != '\r')
  7445. %@AS@%      printf( "%d\n", ch );
  7446. %@AS@%  }%@AE@%%@NL@%
  7447. %@NL@%
  7448. Parentheses have the highest precedence of any operator, so the expression  %@NL@%
  7449. %@NL@%
  7450. %@AS@%  (ch = getch()) != '\r'%@AE@%%@NL@%
  7451. %@NL@%
  7452. works correctly. It assigns the result of the %@AB@%getch%@AE@% call to %@AS@% ch %@AE@% before
  7453. comparing %@AS@% ch %@AE@% to the constant.  %@NL@%
  7454. %@NL@%
  7455. The list of precedence-related errors is almost endless. Fortunately, QuickC
  7456. makes it unnecessary to memorize precedence rules. To view a complete table
  7457. of operator precedences, see Appendix A, "C Language Guide," and online help
  7458. in the QuickC environment.  %@NL@%
  7459. %@NL@%
  7460. %@AU@% Use parentheses  to avoid operator  precedence problems.%@AE@%  %@NL@%
  7461. %@NL@%
  7462. When in doubt, use extra parentheses to make the order of operations
  7463. absolutely clear. Extra parentheses don't degrade performance, and they can
  7464. improve readability as well as minimize precedence problems.  %@NL@%
  7465. %@NL@%
  7466. %@NL@%
  7467. %@3@%%@CR:C6A00100005 @%%@AB@%Confusing Structure-Member Operators%@AE@%%@EH@%%@NL@%
  7468. %@NL@%
  7469. Two different operators are used to access the members of a structure. Use
  7470. the structure-member operator (%@AB@%.%@AE@%) to access a structure member directly, and
  7471. the pointer-member operator (%@AB@%->%@AE@%) to access a structure member indirectly
  7472. through a pointer.  %@NL@%
  7473. %@NL@%
  7474. For instance, you may create a pointer to a structure of the %@AS@% employee %@AE@%
  7475. type,  %@NL@%
  7476. %@NL@%
  7477. %@AS@%  struct employee *p_ptr;%@AE@%%@NL@%
  7478. %@NL@%
  7479. and initialize the pointer to point to the %@AS@% jones %@AE@% structure:  %@NL@%
  7480. %@NL@%
  7481. %@AS@%  p_ptr = &jones;%@AE@%%@NL@%
  7482. %@NL@%
  7483. If you use the structure-member operator to access a structure member
  7484. through the pointer,  %@NL@%
  7485. %@NL@%
  7486. %@AS@%  p_ptr.months = 78; /* Error! */%@AE@%%@NL@%
  7487. %@NL@%
  7488. QuickC issues this error message:  %@NL@%
  7489. %@NL@%
  7490. %@AS@%  C2040: '.' requires struct/union name%@AE@%%@NL@%
  7491. %@NL@%
  7492. Use the pointer-member operator to access a structure member through a
  7493. pointer:  %@NL@%
  7494. %@NL@%
  7495. %@AS@%  p_ptr->months = 78;%@AE@%%@NL@%
  7496. %@NL@%
  7497. %@NL@%
  7498. %@2@%%@CR:C6A00100006 @%%@AB@%Array Problems%@AE@%%@EH@%%@NL@%
  7499. %@NL@%
  7500. The most common errors associated with arrays involve indexing errors. The
  7501. problems described in this section all concern indexing errors of one form
  7502. or another.  %@NL@%
  7503. %@NL@%
  7504. %@NL@%
  7505. %@3@%%@CR:C6A00100007 @%%@AB@%Array Indexing Errors%@AE@%%@EH@%%@NL@%
  7506. %@NL@%
  7507. %@AU@% The first C array subscript is 0.%@AE@%  %@NL@%
  7508. %@NL@%
  7509. If you're used to a language that has different subscripting rules, it's
  7510. easy to forget that the first subscript of a C array is 0 and the last
  7511. subscript is 1 less than the number used to declare the array. Here's an
  7512. example:  %@NL@%
  7513. %@NL@%
  7514. %@AS@%  int i_array[4] = { 3, 55, 600, 12 };
  7515. %@AS@%  main()
  7516. %@AS@%  {
  7517. %@AS@%     int count;
  7518. %@AS@%     for( count = 1; count < 5; count++ )  /* Error! */
  7519. %@AS@%        printf( "i_array[%d] = %d\n", i_array[count] );
  7520. %@AS@%  }%@AE@%%@NL@%
  7521. %@NL@%
  7522. The %@AB@%for%@AE@% loop in the above program starts at %@AS@% i_array[1] %@AE@% and ends at %@AS@%
  7523. %@AS@%i_array[4]%@AE@%. It should begin with the first element, %@AS@% i_array[0] %@AE@% and end at
  7524. the last,%@AS@% i_array[3]%@AE@%. The following corrects the error.  %@NL@%
  7525. %@NL@%
  7526. %@AS@%  for( count = 0; count < 4; count++ )
  7527. %@AS@%     printf( "i_array[%d] = %d\n", i_array[count] );%@AE@%%@NL@%
  7528. %@NL@%
  7529. %@NL@%
  7530. %@3@%%@CR:C6A00100008 @%%@AB@%Omitting an Array Subscript in Multidimensional Arrays%@AE@%%@EH@%%@NL@%
  7531. %@NL@%
  7532. %@AU@% Enclose each subscript  in its own set of brackets.%@AE@%  %@NL@%
  7533. %@NL@%
  7534. Programmers who know QuickBASIC, QuickPascal, or FORTRAN may be tempted to
  7535. place more than one array subscript in the same pair of brackets. In C, each
  7536. subscript of a multidimensional array is enclosed in its own pair of
  7537. brackets:  %@NL@%
  7538. %@NL@%
  7539. %@AS@%  int i_array[2][2] = { { 12, 2 }, { 6, 55 } };
  7540. %@AS@%  main()
  7541. %@AS@%  {
  7542. %@AS@%     printf( "%d\n", i_array[ 0, 1 ] ); /* Error! */
  7543. %@AS@%  }%@AE@%%@NL@%
  7544. %@NL@%
  7545. In the preceding example, the expression  %@NL@%
  7546. %@NL@%
  7547. %@AS@%  i_array[ 0, 1 ]%@AE@%%@NL@%
  7548. %@NL@%
  7549. does not access element 0,1 of %@AS@% i_array %@AE@%. Here is the correct way to refer
  7550. to that array element:  %@NL@%
  7551. %@NL@%
  7552. %@AS@%  i_array[0][1]%@AE@%%@NL@%
  7553. %@NL@%
  7554. Interestingly, the deviant array reference doesn't cause a syntax error. As
  7555. mentioned in Chapter 6, "Operators," it's legal to separate multiple
  7556. expressions with a comma operator, and the final value of such a series is
  7557. the value of the rightmost expression in the group. Thus, the expression  %@NL@%
  7558. %@NL@%
  7559. %@AS@%  i_array[ 0, 1 ]%@AE@%%@NL@%
  7560. %@NL@%
  7561. is equivalent to this one:  %@NL@%
  7562. %@NL@%
  7563. %@AS@%  i_array[ 1 ];%@AE@%%@NL@%
  7564. %@NL@%
  7565. Both expressions give an address, not the value of an array element.  %@NL@%
  7566. %@NL@%
  7567. %@NL@%
  7568. %@3@%%@CR:C6A00100009 @%%@AB@%Overrunning Array Boundaries%@AE@%%@EH@%%@NL@%
  7569. %@NL@%
  7570. Since C doesn't check array subscripts for validity, you must keep track of
  7571. array boundaries on your own. For instance, if you initialize a
  7572. five-character array,  %@NL@%
  7573. %@NL@%
  7574. %@AS@%  char sample[] = "ABCD";%@AE@%%@NL@%
  7575. %@NL@%
  7576. and refer to a nonexistent array element,  %@NL@%
  7577. %@NL@%
  7578. %@AS@%  sample[9] = 'X';%@AE@%%@NL@%
  7579. %@NL@%
  7580. QuickC doesn't signal an error, although the second statement overwrites
  7581. memory outside the array. It stores a character in element 9 of an array
  7582. that contains only 5 elements.  %@NL@%
  7583. %@NL@%
  7584. The same problem can occur when accessing an array through a pointer:  %@NL@%
  7585. %@NL@%
  7586. %@AS@%  char sample[] = "ABCD";
  7587. %@AS@%  char *ptr = sample;
  7588. %@AS@%  *--ptr = 'X';  /* Error! */%@AE@%%@NL@%
  7589. %@NL@%
  7590. The code overwrites the byte in memory below the array. To avoid such
  7591. problems, confine all array operations within the range used to declare the
  7592. array.  %@NL@%
  7593. %@NL@%
  7594. %@NL@%
  7595. %@2@%%@CR:C6A00100010 @%%@AB@%String Problems%@AE@%%@EH@%%@NL@%
  7596. %@NL@%
  7597. Strings are handled a little differently in C than most languages─a fact
  7598. that can cause problems. The following errors are common to programs that
  7599. use strings.  %@NL@%
  7600. %@NL@%
  7601. %@NL@%
  7602. %@3@%%@CR:C6A00100011 @%%@AB@%Confusing Character Constants and Character Strings%@AE@%%@EH@%%@NL@%
  7603. %@NL@%
  7604. Remember the difference between a character constant, which has one byte,
  7605. and a character string, which is a series of characters ending with a null
  7606. character:  %@NL@%
  7607. %@NL@%
  7608. %@AS@%  char ch = 'Y';
  7609. %@AS@%  if( ch == "Y" )  /* Error! */
  7610. %@AS@%     printf( "The ayes have it..." );%@AE@%%@NL@%
  7611. %@NL@%
  7612. The example above mistakenly compares the %@AB@%char%@AE@% variable %@AS@% ch %@AE@% to a
  7613. twocharacter string (%@AS@% "Y" %@AE@%) instead of a single character constant (%@AS@% 'Y' %@AE@%).
  7614. Since the comparison is false, the %@AB@%printf%@AE@% statement never executes─no matter
  7615. what %@AS@% ch %@AE@% equals.  %@NL@%
  7616. %@NL@%
  7617. The %@AB@%if%@AE@% statement needs to use single quotes. This code correctly tests
  7618. whether %@AS@% ch %@AE@% equals the character %@AS@% 'Y'%@AE@%:  %@NL@%
  7619. %@NL@%
  7620. %@AS@%  char ch = 'Y';
  7621. %@AS@%  if( ch == 'Y' )
  7622. %@AS@%     printf( "The ayes have it..." );%@AE@%%@NL@%
  7623. %@NL@%
  7624. %@NL@%
  7625. %@3@%%@CR:C6A00100012 @%%@AB@%Forgetting the Null Character That Terminates Strings%@AE@%%@EH@%%@NL@%
  7626. %@NL@%
  7627. Remember that strings end with a null character in C. If you declare this
  7628. five-character array,  %@NL@%
  7629. %@NL@%
  7630. %@AS@%  char sample[5];%@AE@%%@NL@%
  7631. %@NL@%
  7632. the compiler allocates five bytes of memory for the array. If you try to
  7633. store the string %@AS@%"Hello" %@AE@% in the array like this,  %@NL@%
  7634. %@NL@%
  7635. %@AS@%  strcpy( sample, "Hello" );%@AE@%%@NL@%
  7636. %@NL@%
  7637. you'll overrun the array's bounds. The string %@AS@%"Hello" %@AE@% contains six
  7638. characters (five letters and a null character), so it's one byte too big to
  7639. fit in the %@AS@% sample %@AE@% array. The %@AB@%strcpy%@AE@% overwrites one byte of memory outside
  7640. the array's storage.  %@NL@%
  7641. %@NL@%
  7642. It's easy to make this error when allocating memory for a string, too:  %@NL@%
  7643. %@NL@%
  7644. %@AS@%  char str[] = "Hello";
  7645. %@AS@%  char *ptr;
  7646. %@AS@%  ptr = malloc( strlen( str ) ); /* Error! */
  7647. %@AS@%  if( ptr == NULL )
  7648. %@AS@%     exit( 1 );
  7649. %@AS@%  else
  7650. %@AS@%     strcpy( ptr, str );%@AE@%%@NL@%
  7651. %@NL@%
  7652. This time the error occurs in the call to the %@AB@%malloc%@AE@% function, which
  7653. allocates memory to a pointer prior to a string copy. The %@AB@%strlen%@AE@% function
  7654. returns the length of a string not including the null character that ends
  7655. the string. Since the amount of memory allocated is one byte too small, the
  7656. %@AB@%strcpy%@AE@% operation overwrites memory, just as in the previous example.  %@NL@%
  7657. %@NL@%
  7658. To avoid the problem, add 1 to the value returned by %@AB@%strlen%@AE@%:  %@NL@%
  7659. %@NL@%
  7660. %@AS@%  ptr = malloc( strlen( str ) + 1 );%@AE@%%@NL@%
  7661. %@NL@%
  7662. %@NL@%
  7663. %@3@%%@CR:C6A00100013 @%%@AB@%Forgetting to Allocate Memory for a String%@AE@%%@EH@%%@NL@%
  7664. %@NL@%
  7665. If you declare a string as a pointer, don't forget to allocate memory for
  7666. it. This example tries to create a %@AB@%char%@AE@% pointer named %@AS@% ptr %@AE@% and initialize
  7667. it with a string:  %@NL@%
  7668. %@NL@%
  7669. %@AS@%  main()
  7670. %@AS@%  {
  7671. %@AS@%     char *ptr;
  7672. %@AS@%     strcpy( ptr, "Ashby" );  /* Error! */
  7673. %@AS@%  }%@AE@%%@NL@%
  7674. %@NL@%
  7675. The pointer declaration %@AS@% char *ptr; %@AE@% creates a pointer variable but nothing
  7676. else. It allocates enough memory for the pointer to store an address but
  7677. doesn't allocate any memory to store the object to which %@AS@% ptr %@AE@% will point.
  7678. The %@AB@%strcpy%@AE@% operation in the next line overwrites memory by copying the
  7679. string into an area not used by the program.  %@NL@%
  7680. %@NL@%
  7681. One way to allocate memory is by declaring a %@AB@%char%@AE@% array large enough to hold
  7682. the string:  %@NL@%
  7683. %@NL@%
  7684. %@AS@%  main()
  7685. %@AS@%  {
  7686. %@AS@%     char c_array[10];
  7687. %@AS@%     strcpy( c_array, "Randleman" );
  7688. %@AS@%  }%@AE@%%@NL@%
  7689. %@NL@%
  7690. You can also call the %@AB@%malloc%@AE@% library function to allocate memory at run
  7691. time:  %@NL@%
  7692. %@NL@%
  7693. %@AS@%  #define BUFFER_SIZE 30
  7694. %@AS@%  #include <malloc.h>
  7695. %@AS@%  
  7696. %@AS@%  main()
  7697. %@AS@%  {
  7698. %@AS@%     char *ptr;
  7699. %@AS@%     if( ptr = (char *) malloc( BUFFER_SIZE ) )
  7700. %@AS@%     {
  7701. %@AS@%        strcpy( ptr, "Duvall" );
  7702. %@AS@%        printf( ptr );
  7703. %@AS@%        free( ptr );
  7704. %@AS@%     }
  7705. %@AS@%  }%@AE@%%@NL@%
  7706. %@NL@%
  7707. %@NL@%
  7708. %@2@%%@CR:C6A00100014 @%%@AB@%Pointer Problems%@AE@%%@EH@%%@NL@%
  7709. %@NL@%
  7710. Every experienced C programmer has a collection of favorite pointer-induced
  7711. bugs. Pointer errors can wreak havoc because pointers can change the
  7712. contents of any addressable memory location. If a pointer writes to an
  7713. unexpected address, the results can be disastrous.  %@NL@%
  7714. %@NL@%
  7715. %@NL@%
  7716. %@3@%%@CR:C6A00100015 @%%@AB@%Using the Wrong Address Operator to Initialize a Pointer%@AE@%%@EH@%%@NL@%
  7717. %@NL@%
  7718. If you're still learning about pointers, it's easy to forget which address
  7719. operator to use when initializing a pointer variable. For example, you might
  7720. want to create a pointer to a simple %@AB@%int%@AE@% variable:  %@NL@%
  7721. %@NL@%
  7722. %@AS@%  int val = 25;
  7723. %@AS@%  int *ptr;
  7724. %@AS@%  ptr = val; /* Error! */%@AE@%%@NL@%
  7725. %@NL@%
  7726. The code above doesn't initialize %@AS@% ptr %@AE@% correctly. Instead of assigning to %@AS@%
  7727. %@AS@%ptr %@AE@% the address of %@AS@% val%@AE@%, the statement  %@NL@%
  7728. %@NL@%
  7729. %@AS@%  ptr = val;%@AE@%%@NL@%
  7730. %@NL@%
  7731. tries to assign %@AS@% ptr %@AE@% the contents of %@AS@% val%@AE@%, causing an error message:  %@NL@%
  7732. %@NL@%
  7733. %@AS@%  warning C4047: '=' : different levels of indirection%@AE@%%@NL@%
  7734. %@NL@%
  7735. Because %@AS@% val %@AE@% is an %@AB@%int%@AE@% variable, its contents can't form a meaningful
  7736. address for %@AS@%ptr%@AE@%. You must use the address-of operator to initialize %@AS@% ptr%@AE@%:  %@NL@%
  7737. %@NL@%
  7738. %@AS@%  ptr = &val;%@AE@%%@NL@%
  7739. %@NL@%
  7740. Here's another pointer initialization error:  %@NL@%
  7741. %@NL@%
  7742. %@AS@%  int val = 25;
  7743. %@AS@%  int *ptr;
  7744. %@AS@%  *ptr = &val; /* Error! */%@AE@%%@NL@%
  7745. %@NL@%
  7746. The last line doesn't initialize %@AS@% ptr %@AE@% to point to the variable %@AS@% val%@AE@%. The
  7747. expression to the left of the equal sign, %@AS@% *ptr%@AE@%, stands for the object %@AS@% ptr %@AE@%
  7748. points to. Instead of assigning %@AS@% ptr %@AE@% the address of %@AS@% val%@AE@%, the line tries to
  7749. assign the address of %@AS@% val %@AE@% to the place where %@AS@% ptr %@AE@% points. Because %@AS@% ptr %@AE@%
  7750. has never been initialized, the assignment triggers a run-time error:  %@NL@%
  7751. %@NL@%
  7752. %@AS@%  run-time error R6001
  7753. %@AS@%  -null pointer assignment%@AE@%%@NL@%
  7754. %@NL@%
  7755. Here is the correct way to initialize this pointer:  %@NL@%
  7756. %@NL@%
  7757. %@AS@%  ptr = &val;%@AE@%%@NL@%
  7758. %@NL@%
  7759. %@NL@%
  7760. %@3@%%@CR:C6A00100016 @%%@AB@%Declaring a Pointer with the Wrong Type%@AE@%%@EH@%%@NL@%
  7761. %@NL@%
  7762. You should make sure the type used to declare a pointer matches the type of
  7763. data object it points to:  %@NL@%
  7764. %@NL@%
  7765. %@AS@%  main()
  7766. %@AS@%  {
  7767. %@AS@%     int *ptr;
  7768. %@AS@%     .
  7769. %@AS@%     .
  7770. %@AS@%     .
  7771. %@AS@%     float val = 3.333333;
  7772. %@AS@%     ptr = val;  /* Error! */
  7773. %@AS@%     printf( "val = %f\n", *ptr );
  7774. %@AS@%  }%@AE@%%@NL@%
  7775. %@NL@%
  7776. The program declares %@AS@% ptr %@AE@% as a pointer to an %@AB@%int%@AE@%. Later on, forgetting what
  7777. type we used when declaring %@AS@% ptr%@AE@%, we assign it the address of the
  7778. floating-point variable %@AS@% val%@AE@%.  %@NL@%
  7779. %@NL@%
  7780. %@AU@% Declaring a pointer with the wrong type can cause unwanted type
  7781. %@AU@%conversions.%@AE@%  %@NL@%
  7782. %@NL@%
  7783. Since C allows you to assign any address to a pointer, the assignment
  7784. doesn't cause an error. But accessing %@AS@% val %@AE@% through %@AS@% ptr %@AE@% creates problems.
  7785. Because %@AS@% ptr %@AE@% is declared as a pointer to an %@AB@%int%@AE@%, the compiler does a type
  7786. conversion on the %@AB@%float%@AE@% it points to, converting the %@AB@%float%@AE@% value to an %@AB@%int%@AE@%.
  7787. The output is garbage:  %@NL@%
  7788. %@NL@%
  7789. %@AS@%  val = 11242989923343410000000000000000000000000000000000000
  7790. %@AS@%  000000000000000000000000000000000000000000000000000.000000%@AE@%%@NL@%
  7791. %@NL@%
  7792. The following program cures the error by declaring %@AS@% ptr %@AE@% as a pointer to a
  7793. %@AB@%float%@AE@% data type:  %@NL@%
  7794. %@NL@%
  7795. %@AS@%  main()
  7796. %@AS@%  {
  7797. %@AS@%     float *ptr;
  7798. %@AS@%     float val = 3.333333;
  7799. %@AS@%     ptr = &val;
  7800. %@AS@%     printf( "%f\n", *ptr );
  7801. %@AS@%  }%@AE@%%@NL@%
  7802. %@NL@%
  7803. Now it gives the correct output:  %@NL@%
  7804. %@NL@%
  7805. %@AS@%  val = 3.333333%@AE@%%@NL@%
  7806. %@NL@%
  7807. %@NL@%
  7808. %@3@%%@CR:C6A00100017 @%%@AB@%Using Dangling Pointers%@AE@%%@EH@%%@NL@%
  7809. %@NL@%
  7810. A "dangling pointer" is one that points to a memory area no longer in use by
  7811. your program. Dangling pointers, like uninitialized pointers, can be very
  7812. dangerous to use.  %@NL@%
  7813. %@NL@%
  7814. For instance, say you allocate a block of memory with the %@AB@%malloc%@AE@% library
  7815. function:  %@NL@%
  7816. %@NL@%
  7817. %@AS@%  #define BUFSIZE 1000
  7818. %@AS@%  char *ptr;
  7819. %@AS@%  if( ptr = (char *) malloc( BUFSIZE ) )
  7820. %@AS@%     /* do something */ ;%@AE@%%@NL@%
  7821. %@NL@%
  7822. After the memory block has been allocated with %@AB@%malloc%@AE@%, the pointer %@AS@% ptr %@AE@%
  7823. points to a valid data object. Once you're done using allocated memory, you
  7824. normally return it to the heap:  %@NL@%
  7825. %@NL@%
  7826. %@AS@%  free( ptr );%@AE@%%@NL@%
  7827. %@NL@%
  7828. After you free the memory it points to, %@AS@% ptr %@AE@% is a dangling pointer. It
  7829. still points to a valid machine address, but that address is no longer in
  7830. use by the program. You shouldn't use the pointer at this stage, just as you
  7831. shouldn't use it before it has been initialized.  %@NL@%
  7832. %@NL@%
  7833. Dangling pointers can also be created by a function that returns a pointer
  7834. to a local variable:  %@NL@%
  7835. %@NL@%
  7836. %@AS@%  int *boo_boo( void )
  7837. %@AS@%  {
  7838. %@AS@%     int object;
  7839. %@AS@%     .
  7840. %@AS@%     .
  7841. %@AS@%     .
  7842. %@AS@%     return &object; /* Error! */
  7843. %@AS@%  }%@AE@%%@NL@%
  7844. %@NL@%
  7845. The %@AS@% boo_boo %@AE@% function returns the address of the local variable %@AS@% object%@AE@%,
  7846. forgetting the storage for %@AS@% object %@AE@% is no longer part of the program after
  7847. the function ends.  %@NL@%
  7848. %@NL@%
  7849. Here's a variant of the previous example involving a string pointer:  %@NL@%
  7850. %@NL@%
  7851. %@AS@%  char *boo_boo( void )
  7852. %@AS@%  {
  7853. %@AS@%     char *c_ptr;
  7854. %@AS@%     c_ptr = "Hello";
  7855. %@AS@%     .
  7856. %@AS@%     .
  7857. %@AS@%     .
  7858. %@AS@%     return c_ptr; /* Error! */
  7859. %@AS@%  }%@AE@%%@NL@%
  7860. %@NL@%
  7861. Since the string constant %@AS@% "Hello" %@AE@% is local to the function, it evaporates
  7862. when the function ends, leaving the pointer %@AS@% c_ptr %@AE@% dangling.  %@NL@%
  7863. %@NL@%
  7864. %@NL@%
  7865. %@2@%%@CR:C6A00100018 @%%@AB@%Library-Function Problems%@AE@%%@EH@%%@NL@%
  7866. %@NL@%
  7867. Once you've learned enough about C to write practical programs, you can
  7868. begin to explore the rich function library supplied with QuickC. This
  7869. section outlines a few common problems related to using library functions.
  7870. Again, you can use online help to get information about specific library
  7871. functions.  %@NL@%
  7872. %@NL@%
  7873. %@NL@%
  7874. %@3@%%@CR:C6A00100019 @%%@AB@%Failing to Check Return Values from Library Functions%@AE@%%@EH@%%@NL@%
  7875. %@NL@%
  7876. %@AU@% Always check library function return values.%@AE@%  %@NL@%
  7877. %@NL@%
  7878. Almost all library functions return some value─either the result of
  7879. processing or an error code showing success or failure. You should always
  7880. check libraryfunction return values, even if you're confident of the result.
  7881. %@NL@%
  7882. %@NL@%
  7883. This rule is critical when calling a library function such as %@AB@%malloc%@AE@%, which
  7884. allocates memory at run time:  %@NL@%
  7885. %@NL@%
  7886. %@AS@%  char *ptr;
  7887. %@AS@%  ptr = (char *) malloc( BUFSIZE );  /* Error! */%@AE@%%@NL@%
  7888. %@NL@%
  7889. If the call to %@AB@%malloc%@AE@% fails, the pointer %@AS@% ptr %@AE@% is assigned a null (0) value.
  7890. Using %@AS@% ptr %@AE@% under these circumstances can overwrite unexpected memory
  7891. addresses or cause a run-time error. The following code checks the return
  7892. value from %@AB@%malloc%@AE@%:  %@NL@%
  7893. %@NL@%
  7894. %@AS@%  #define NULL 0
  7895. %@AS@%  #define BUFSIZE 32768
  7896. %@AS@%     .
  7897. %@AS@%     .
  7898. %@AS@%     .
  7899. %@AS@%  char *ptr;
  7900. %@AS@%  if( (ptr = (char *) malloc( BUFSIZE ) ) != NULL )
  7901. %@AS@%  {
  7902. %@AS@%     printf( "Copacetic.\n" );
  7903. %@AS@%     /* Do something useful... */
  7904. %@AS@%  }
  7905. %@AS@%  else
  7906. %@AS@%     printf( "Not enough memory!\n" );%@AE@%%@NL@%
  7907. %@NL@%
  7908. %@NL@%
  7909. %@3@%%@CR:C6A00100020 @%%@AB@%Duplicating Library-Function Names%@AE@%%@EH@%%@NL@%
  7910. %@NL@%
  7911. There are so many functions in the QuickC run-time library that it's
  7912. sometimes difficult to avoid duplicating function names. For instance, if
  7913. you write a function that reads data from a buffer, the name %@AS@% read %@AE@% may
  7914. strike you as short and descriptive.  %@NL@%
  7915. %@NL@%
  7916. The only problem is that %@AB@%read%@AE@% is the name of a QuickC library function. A
  7917. program that defines its own %@AS@% read %@AE@% function may work correctly at first,
  7918. but if you later include the header file that declares the %@AB@%read%@AE@% library
  7919. function,  %@NL@%
  7920. %@NL@%
  7921. %@AS@%  #include <io.h>%@AE@%%@NL@%
  7922. %@NL@%
  7923. then redefinition errors occur. You can't use the same name for two
  7924. different functions. The solution here is to rename the user-defined
  7925. function.  %@NL@%
  7926. %@NL@%
  7927. %@AU@% Use online help to check  for function-name conflicts.%@AE@%  %@NL@%
  7928. %@NL@%
  7929. QuickC's online help lets you check for such name conflicts on the spot. Put
  7930. the cursor on the function name you wish to use, then press F1. If the name
  7931. is already used for a library function, online help displays information
  7932. about the function. If the name isn't in online help, it's not used in the
  7933. QuickC function library and is a safe choice.  %@NL@%
  7934. %@NL@%
  7935. Unless you're writing your own library functions, it's a good rule to avoid
  7936. declaring names that begin with an underscore ( _ ), since many of the
  7937. system-defined names in QuickC start with that character. (Non-ANSI library
  7938. functions begin with a single underscore. Predefined identifiers such as
  7939. %@AB@%TIME%@AE@% start with two underscores, and routines internal to the C run-time
  7940. library can begin with either one or two underscores.)  %@NL@%
  7941. %@NL@%
  7942. %@NL@%
  7943. %@3@%%@CR:C6A00100021 @%%@AB@%Forgetting to Include Header Files for Library Functions%@AE@%%@EH@%%@NL@%
  7944. %@NL@%
  7945. Because they contain needed function prototypes, it's important to include
  7946. the correct header files when using QuickC library functions:  %@NL@%
  7947. %@NL@%
  7948. %@AS@%  main()
  7949. %@AS@%  {
  7950. %@AS@%     double val = sqrt( (double) 10 );
  7951. %@AS@%     printf( "square root of 10 = %le\n", val );
  7952. %@AS@%  }%@AE@%%@NL@%
  7953. %@NL@%
  7954. The program above calls the library function %@AB@%sqrt%@AE@%, which calculates a square
  7955. root. Most of the program is correct. When passing the value 10 to %@AB@%sqrt%@AE@%, it
  7956. casts the argument as a %@AB@%double%@AE@%, the type %@AB@%sqrt%@AE@% expects. The return value from
  7957. %@AB@%sqrt%@AE@% is assigned to a %@AB@%double%@AE@% variable, too.  %@NL@%
  7958. %@NL@%
  7959. Unfortunately, the program still gives the wrong output. The square root of
  7960. 10 is not 171 (1.710000e+002 in exponential notation):  %@NL@%
  7961. %@NL@%
  7962. %@AS@%  square root of 10 = 1.710000e+002%@AE@%%@NL@%
  7963. %@NL@%
  7964. %@AU@% Function prototypes can prevent unexpected type conversions.%@AE@%  %@NL@%
  7965. %@NL@%
  7966. Because the program has no prototype for the %@AB@%sqrt%@AE@% function, %@AB@%sqrt%@AE@% has the %@AB@%int%@AE@%
  7967. return type by default. The value returned by %@AB@%sqrt%@AE@% undergoes an unexpected
  7968. type conversion─from type %@AB@%double%@AE@% to %@AB@%int%@AE@%─and becomes garbage.  %@NL@%
  7969. %@NL@%
  7970. This problem is easily solved. Simply include the standard header file that
  7971. contains the prototype for %@AB@%sqrt%@AE@%:  %@NL@%
  7972. %@NL@%
  7973. %@AS@%  #include <stdio.h>
  7974. %@AS@%  #include <math.h>
  7975. %@AS@%  main()
  7976. %@AS@%  {
  7977. %@AS@%     double val = sqrt( (double) 10 );
  7978. %@AS@%     printf( "square root of 10 = %le\n", val );
  7979. %@AS@%  }%@AE@%%@NL@%
  7980. %@NL@%
  7981. Now the program works correctly:  %@NL@%
  7982. %@NL@%
  7983. %@AS@%  square root of 10 = 3.162278e+000%@AE@%%@NL@%
  7984. %@NL@%
  7985. If you're not sure which header file a library function needs, take
  7986. advantage of QuickC's online help. (Put the cursor on the function name and
  7987. press F1.) If the function needs a header file, the file name appears in an
  7988. %@AB@%#include%@AE@% directive above the function prototype.  %@NL@%
  7989. %@NL@%
  7990. %@NL@%
  7991. %@3@%%@CR:C6A00100022 @%%@AB@%Omitting the Address-Of Operator When Calling scanf%@AE@%%@EH@%%@NL@%
  7992. %@NL@%
  7993. Don't forget to put the address-of operator in front of arguments when using
  7994. the %@AB@%scanf%@AE@% library function (the %@AB@%scanf%@AE@% function accesses keyboard input; see
  7995. Chapter 11, "Input and Output")%@AB@%:%@AE@%  %@NL@%
  7996. %@NL@%
  7997. %@AS@%  main()
  7998. %@AS@%  {
  7999. %@AS@%     int val;
  8000. %@AS@%     printf( "Type a number: " );
  8001. %@AS@%     scanf( "%d", val ); /* Error! */
  8002. %@AS@%     printf( "%d", val );
  8003. %@AS@%  }%@AE@%%@NL@%
  8004. %@NL@%
  8005. When the program calls %@AB@%scanf%@AE@%, it omits the address-of operator that should
  8006. precede the second argument:  %@NL@%
  8007. %@NL@%
  8008. %@AS@%  scanf( "%d", val );  /* Error! */%@AE@%%@NL@%
  8009. %@NL@%
  8010. The %@AB@%scanf%@AE@% function expects to be passed a pointer to a variable (in this
  8011. case, a pointer to %@AS@% val %@AE@%) so it can assign an input value to the variable.
  8012. But because the address-of operator is missing, the program passes the value
  8013. of %@AS@% val%@AE@%, not its address.  %@NL@%
  8014. %@NL@%
  8015. Instead of storing an input value in %@AS@% val %@AE@% as intended, %@AB@%scanf%@AE@% uses the
  8016. uninitialized value of %@AS@% val %@AE@% as a pointer and assigns the input value to an
  8017. unpredictable address. As a result, %@AS@% val %@AE@% remains uninitialized and the
  8018. program overwrites memory elsewhere─two very undesirable events.  %@NL@%
  8019. %@NL@%
  8020. Here is the correct way to call %@AB@%scanf%@AE@% in this program:  %@NL@%
  8021. %@NL@%
  8022. %@AS@%  scanf( "%d", &val );%@AE@%%@NL@%
  8023. %@NL@%
  8024. %@NL@%
  8025. %@2@%%@CR:C6A00100023 @%%@AB@%Macro Problems%@AE@%%@EH@%%@NL@%
  8026. %@NL@%
  8027. Function-like macros─macro definitions that take arguments─share many of the
  8028. advantages of functions. They can cause unwanted side effects, however, if
  8029. you fail to put parentheses around their arguments or carelessly supply an
  8030. argument that uses an increment or decrement operator.  %@NL@%
  8031. %@NL@%
  8032. %@NL@%
  8033. %@3@%%@CR:C6A00100024 @%%@AB@%Omitting Parentheses from Macro Arguments%@AE@%%@EH@%%@NL@%
  8034. %@NL@%
  8035. A macro definition that doesn't enclose its arguments in parentheses can
  8036. create precedence problems:  %@NL@%
  8037. %@NL@%
  8038. %@AS@%  #include <stdio.h>
  8039. %@AS@%  
  8040. %@AS@%  #define FOURX(arg)  ( arg * 4 )
  8041. %@AS@%  
  8042. %@AS@%  main()
  8043. %@AS@%  {
  8044. %@AS@%     int val;
  8045. %@AS@%     val = FOURX( 2 + 3 );
  8046. %@AS@%     printf( "val = %d\n", val );
  8047. %@AS@%  }%@AE@%%@NL@%
  8048. %@NL@%
  8049. The %@AS@% FOURX %@AE@% macro in the program multiplies its argument by 4. The macro
  8050. works fine if you pass it a single value, as in  %@NL@%
  8051. %@NL@%
  8052. %@AS@%  val = FOURX( 2 );%@AE@%%@NL@%
  8053. %@NL@%
  8054. but returns the wrong result if you pass it this expression:  %@NL@%
  8055. %@NL@%
  8056. %@AS@%  val = FOURX( 2 + 3 );%@AE@%%@NL@%
  8057. %@NL@%
  8058. QuickC expands the above line to this line:  %@NL@%
  8059. %@NL@%
  8060. %@AS@%  val = 2 + 3 * 4;%@AE@%%@NL@%
  8061. %@NL@%
  8062. %@AU@% Use parentheses to  avoid precedence  problems in macros.%@AE@%  %@NL@%
  8063. %@NL@%
  8064. Because the multiplication operator has higher precedence than the addition
  8065. operator, this line assigns %@AS@% val %@AE@% the value 14 (or 2 + 12) rather than the
  8066. correct value 20 (or 5 * 4).  %@NL@%
  8067. %@NL@%
  8068. You can avoid the problem by enclosing the macro argument in parentheses
  8069. each time it appears in the macro definition:  %@NL@%
  8070. %@NL@%
  8071. %@AS@%  #include <stdio.h>
  8072. %@AS@%  
  8073. %@AS@%  #define FOURX(arg)  ( (arg) * 4 )
  8074. %@AS@%  
  8075. %@AS@%  main()
  8076. %@AS@%  {
  8077. %@AS@%     int val;
  8078. %@AS@%     val = FOURX(2 + 3);
  8079. %@AS@%     printf( "val = %d\n", val );
  8080. %@AS@%  }%@AE@%%@NL@%
  8081. %@NL@%
  8082. Now the program expands this line  %@NL@%
  8083. %@NL@%
  8084. %@AS@%  val = FOURX(2 + 3);%@AE@%%@NL@%
  8085. %@NL@%
  8086. into this one:  %@NL@%
  8087. %@NL@%
  8088. %@AS@%  val = (2 + 3) * 4;%@AE@%%@NL@%
  8089. %@NL@%
  8090. The extra parentheses assure that the addition is performed before the
  8091. multiplication, giving the desired result.  %@NL@%
  8092. %@NL@%
  8093. %@NL@%
  8094. %@3@%%@CR:C6A00100025 @%%@AB@%Using Increment and Decrement Operators in Macro Arguments%@AE@%%@EH@%%@NL@%
  8095. %@NL@%
  8096. If a function-like macro evaluates an argument more than once, you should
  8097. avoid passing it an expression that contains an increment or decrement
  8098. operator:  %@NL@%
  8099. %@NL@%
  8100. %@AS@%  #include <stdio.h>
  8101. %@AS@%  #define ABS(value)  ( (value) >= 0 ? (value) : -(value) )
  8102. %@AS@%  
  8103. %@AS@%  main()
  8104. %@AS@%  {
  8105. %@AS@%     int array[4] = {3, -20, -555, 6};
  8106. %@AS@%     int *ptr = array;
  8107. %@AS@%     int val, count;
  8108. %@AS@%     for( count = 0; count < 4; count++ )
  8109. %@AS@%     {
  8110. %@AS@%        val = ABS(*ptr++); /* Error! */
  8111. %@AS@%        printf( "abs of array[%d] = %d\n", count, val );
  8112. %@AS@%     }
  8113. %@AS@%  }%@AE@%%@NL@%
  8114. %@NL@%
  8115. The program uses the %@AS@% ABS %@AE@% macro that was used to explain macros in Chapter
  8116. 7, "Preprocessor Directives." The macro returns the absolute value of the
  8117. argument you pass to it.  %@NL@%
  8118. %@NL@%
  8119. The goal in this program is to display the absolute value of every element
  8120. in %@AS@% array%@AE@%. It uses a %@AB@%for%@AE@% loop to step through the array and a pointer named %@AS@%
  8121. %@AS@%ptr %@AE@% to access each array element in turn. Instead of the output you would
  8122. expect,  %@NL@%
  8123. %@NL@%
  8124. %@AS@%  abs of array[0] = 3
  8125. %@AS@%  abs of array[1] = 20
  8126. %@AS@%  abs of array[2] = 555
  8127. %@AS@%  abs of array[3] = 6%@AE@%%@NL@%
  8128. %@NL@%
  8129. the program gives this output:  %@NL@%
  8130. %@NL@%
  8131. %@AS@%  abs of array[0] = -20
  8132. %@AS@%  abs of array[1] = -6
  8133. %@AS@%  abs of array[2] = 8307
  8134. %@AS@%  abs of array[3] = 24864%@AE@%%@NL@%
  8135. %@NL@%
  8136. (The last two array values may differ if you run the program. They are the
  8137. contents of memory not used by the program.)  %@NL@%
  8138. %@NL@%
  8139. The error occurs in this line,  %@NL@%
  8140. %@NL@%
  8141. %@AS@%  val = ABS(*ptr++); /* Error! */%@AE@%%@NL@%
  8142. %@NL@%
  8143. which QuickC expands as shown here:  %@NL@%
  8144. %@NL@%
  8145. %@AS@%  val = ( (*ptr++) >= 0 ? (*ptr++) : -(*ptr++) ); /* Error! */%@AE@%%@NL@%
  8146. %@NL@%
  8147. Because it uses the conditional operator, the %@AS@% ABS %@AE@% macro always evaluates
  8148. its argument at least twice. This isn't a problem when the argument is a
  8149. constant or simple variable. In the example, however, the argument is the
  8150. expression %@AS@% *ptr++%@AE@%. Each time the macro evaluates this expression, the
  8151. increment operator takes effect, causing %@AS@% ptr %@AE@% to point to the next element
  8152. of %@AS@% array%@AE@%.  %@NL@%
  8153. %@NL@%
  8154. The first time the program invokes the macro, %@AS@% ptr %@AE@% points to the first
  8155. array element, %@AS@% array[0]%@AE@%. Since this element contains a nonnegative value
  8156. (3) the macro evaluates the argument twice. The first evaluation takes the
  8157. value that %@AS@% ptr %@AE@% points to and then increments %@AS@% ptr%@AE@%. Now %@AS@% ptr %@AE@% points to the
  8158. second element, %@AS@% array[1]%@AE@%. The second evaluation takes the value of %@AS@%
  8159. %@AS@%array[1] %@AE@% and increments %@AS@% ptr %@AE@% again.  %@NL@%
  8160. %@NL@%
  8161. The first macro invocation not only returns an incorrect value (-20, the
  8162. value of %@AS@% array[1]%@AE@% ). It also leaves %@AS@% ptr %@AE@% pointing to the third array
  8163. element, making the results of later invocations unpredictable. (The pointer
  8164. eventually moves past the last element of %@AS@% array %@AE@% and points to unknown
  8165. data.)  %@NL@%
  8166. %@NL@%
  8167. To avoid the problem, don't use the increment or decrement operators in
  8168. arguments you pass to a macro. This revision removes the error by
  8169. incrementing %@AS@%ptr %@AE@% in the %@AB@%for%@AE@% statement instead of the macro invocation:  %@NL@%
  8170. %@NL@%
  8171. %@AS@%  #include <stdio.h>
  8172. %@AS@%  #define ABS(value)  ( (value) >= 0 ? (value) : -(value) )
  8173. %@AS@%  
  8174. %@AS@%  main()
  8175. %@AS@%  {
  8176. %@AS@%     int array[4] = {3, -20, -555, 6};
  8177. %@AS@%     int *ptr = array;
  8178. %@AS@%     int val, count;
  8179. %@AS@%     for( count = 0; count < 4; count++, ptr++ )
  8180. %@AS@%     {
  8181. %@AS@%        val = ABS(*ptr);
  8182. %@AS@%        printf( "abs of array[%d] = %d\n", count, val );
  8183. %@AS@%     }
  8184. %@AS@%  }%@AE@%%@NL@%
  8185. %@NL@%
  8186. This advice applies generally to QuickC library routines as well as macros
  8187. you write. Remember, some run-time library routines are implemented as
  8188. macros rather than C functions. If you're not sure whether a library routine
  8189. is actually a macro, look it up in online help.  %@NL@%
  8190. %@NL@%
  8191. %@NL@%
  8192. %@2@%%@CR:C6A00100026 @%%@AB@%Miscellaneous Problems%@AE@%%@EH@%%@NL@%
  8193. %@NL@%
  8194. This section describes C programming problems that don't fit into any
  8195. convenient category.  %@NL@%
  8196. %@NL@%
  8197. %@NL@%
  8198. %@3@%%@CR:C6A00100027 @%%@AB@%Mismatching if and else Statements%@AE@%%@EH@%%@NL@%
  8199. %@NL@%
  8200. In nested %@AB@%if%@AE@% statements, each %@AB@%else%@AE@% is associated with the closest preceding
  8201. %@AB@%if%@AE@% statement that does not have an %@AB@%else%@AE@%. Although indentation can make
  8202. nested constructs more readable, it has no syntactical effect:  %@NL@%
  8203. %@NL@%
  8204. %@AS@%  if( val > 5 )
  8205. %@AS@%     if( count == 10 )
  8206. %@AS@%        val = sample;
  8207. %@AS@%  else
  8208. %@AS@%     val = 0;%@AE@%%@NL@%
  8209. %@NL@%
  8210. The indentation suggests that the %@AB@%else%@AE@% associates with the first %@AB@%if%@AE@%. In
  8211. fact, the %@AB@%else%@AE@% is part of the second %@AB@%if%@AE@%, as shown more clearly here:  %@NL@%
  8212. %@NL@%
  8213. %@AS@%  if( val > 5 )
  8214. %@AS@%     if( count == 10 )
  8215. %@AS@%        val = sample;
  8216. %@AS@%     else
  8217. %@AS@%        val = 0;%@AE@%%@NL@%
  8218. %@NL@%
  8219. The %@AB@%else%@AE@% is part of the second %@AB@%if%@AE@% statement─the closest preceding %@AB@%if%@AE@% that
  8220. doesn't have a matching %@AB@%else%@AE@%. To tie the %@AB@%else%@AE@% to the first %@AB@%if%@AE@%, you must use
  8221. braces:  %@NL@%
  8222. %@NL@%
  8223. %@AS@%  if( val > 5 )
  8224. %@AS@%  {
  8225. %@AS@%     if( count == 10 )
  8226. %@AS@%        val = sample;
  8227. %@AS@%  }
  8228. %@AS@%  else
  8229. %@AS@%     val = 0;%@AE@%%@NL@%
  8230. %@NL@%
  8231. %@AU@% Indentation makes  programs easier to read, but  is ignored by the
  8232. %@AU@%compiler.%@AE@%  %@NL@%
  8233. %@NL@%
  8234. Now the %@AB@%else%@AE@% belongs with the outermost %@AB@%if%@AE@%. Remember, indentation is
  8235. meaningful only to humans. The compiler relies strictly on punctuation when
  8236. it translates the source file.  %@NL@%
  8237. %@NL@%
  8238. %@NL@%
  8239. %@3@%%@CR:C6A00100028 @%%@AB@%Misplacing Semicolons%@AE@%%@EH@%%@NL@%
  8240. %@NL@%
  8241. Misplaced semicolons can cause subtle bugs:  %@NL@%
  8242. %@NL@%
  8243. %@AS@%  #include <stdio.h>
  8244. %@AS@%  
  8245. %@AS@%  main()
  8246. %@AS@%  {
  8247. %@AS@%     int count;
  8248. %@AS@%     for( count = 0; count < 500; count++ ); /* Error! */
  8249. %@AS@%     {
  8250. %@AS@%        printf( "count = %d\n", count );
  8251. %@AS@%        printf( "And the beat goes on...\n" );
  8252. %@AS@%     }
  8253. %@AS@%  }%@AE@%%@NL@%
  8254. %@NL@%
  8255. You might expect the program to print the value of %@AS@% count %@AE@% 500 times, but
  8256. this is all it prints:  %@NL@%
  8257. %@NL@%
  8258. %@AS@%  count = 500
  8259. %@AS@%  And the beat goes on...%@AE@%%@NL@%
  8260. %@NL@%
  8261. The culprit is the extra semicolon immediately after the parentheses of the
  8262. %@AB@%for%@AE@% statement. Its effect is more evident if we reformat the statement:  %@NL@%
  8263. %@NL@%
  8264. %@AS@%  #include <stdio.h>
  8265. %@AS@%  
  8266. %@AS@%  main()
  8267. %@AS@%  {
  8268. %@AS@%     int count;
  8269. %@AS@%     for( count = 0; count < 500; count++ )
  8270. %@AS@%        ; /* Null statement */
  8271. %@AS@%     {
  8272. %@AS@%        printf( "count = %d\n", count );
  8273. %@AS@%        printf( "And the beat goes on...\n" );
  8274. %@AS@%     }
  8275. %@AS@%  }%@AE@%%@NL@%
  8276. %@NL@%
  8277. Instead of printing the value of %@AS@% count %@AE@% 500 times, the program executes the
  8278. null statement (%@AS@%;%@AE@%) 500 times. Null statements are perfectly legal in C, so
  8279. the compiler has no way to tell this is a mistake.  %@NL@%
  8280. %@NL@%
  8281. Since the null statement is interpreted as the loop body, the %@AB@%printf%@AE@%
  8282. statements inside curly braces are interpreted as a statement block and
  8283. executed once. Statement blocks usually appear as part of a loop, function
  8284. definition, or decision- making statement, but it's legal to enclose any
  8285. series of statements in braces.  %@NL@%
  8286. %@NL@%
  8287. The program works as intended if you remove the extra semicolon:  %@NL@%
  8288. %@NL@%
  8289. %@AS@%  #include <stdio.h>
  8290. %@AS@%  
  8291. %@AS@%  main()
  8292. %@AS@%  {
  8293. %@AS@%     int count;
  8294. %@AS@%     for( count = 0; count < 500; count++ )
  8295. %@AS@%     {
  8296. %@AS@%        printf( "count = %d\n", count );
  8297. %@AS@%        printf( "And the beat goes on...\n" );
  8298. %@AS@%     }
  8299. %@AS@%  }%@AE@%%@NL@%
  8300. %@NL@%
  8301. Here's another one. If you know QuickPascal, you might be tempted to put a
  8302. semicolon after the parentheses of a function definition:  %@NL@%
  8303. %@NL@%
  8304. %@AS@%  void func( void );
  8305. %@AS@%  
  8306. %@AS@%  void func( void ); /* Error! No semicolon here. */
  8307. %@AS@%  {
  8308. %@AS@%     printf( "C is not Pascal\n" );
  8309. %@AS@%  }%@AE@%%@NL@%
  8310. %@NL@%
  8311. The function header causes a syntax error. While a function declaration
  8312. requires a semicolon after its parentheses, a function definition does not.
  8313. This code corrects the error:  %@NL@%
  8314. %@NL@%
  8315. %@AS@%  void func( void );
  8316. %@AS@%  
  8317. %@AS@%  void func( void )
  8318. %@AS@%  {
  8319. %@AS@%     printf( "C is not Pascal\n" );
  8320. %@AS@%  }%@AE@%%@NL@%
  8321. %@NL@%
  8322. %@NL@%
  8323. %@3@%%@CR:C6A00100029 @%%@AB@%Omitting Double Backslashes in DOS Path Specifications%@AE@%%@EH@%%@NL@%
  8324. %@NL@%
  8325. Because C uses the backslash (%@AB@%\%@AE@%) as an escape character, it's easy to create
  8326. garbled path specifications:  %@NL@%
  8327. %@NL@%
  8328. %@AS@%  fp = fopen( "c:\temp\bodkin.txt", "w" );%@AE@%%@NL@%
  8329. %@NL@%
  8330. At first glance, the path specification in the string  %@NL@%
  8331. %@NL@%
  8332. %@AS@%  "c:\temp\bodkin.txt"%@AE@%%@NL@%
  8333. %@NL@%
  8334. looks good because that's how you would type it on the DOS command line. In
  8335. a quoted string, however, the backslash is interpreted as an escape
  8336. character. In this string the sequences %@AB@%\t%@AE@% and %@AB@%\b%@AE@% are interpreted as the tab
  8337. and backspace character, respectively, garbling the path and file name. Even
  8338. if the indicated file exists, this call to %@AB@%fopen%@AE@% is sure to fail.  %@NL@%
  8339. %@NL@%
  8340. In a quoted string the escape sequence for a backslash character is a double
  8341. backslash (%@AB@%\\%@AE@%). This statement solves the problem:  %@NL@%
  8342. %@NL@%
  8343. %@AS@%  fp = fopen( "c:\\temp\\bodkin.txt", "w" );%@AE@%%@NL@%
  8344. %@NL@%
  8345. %@NL@%
  8346. %@3@%%@CR:C6A00100030 @%%@AB@%Omitting break Statements from a switch Statement%@AE@%%@EH@%%@NL@%
  8347. %@NL@%
  8348. Don't forget to include %@AB@%break%@AE@% statements when using the %@AB@%switch%@AE@% statement:  %@NL@%
  8349. %@NL@%
  8350. %@AS@%  switch( ch )
  8351. %@AS@%  {
  8352. %@AS@%     case 'e':
  8353. %@AS@%        printf( "Bye bye\n" );
  8354. %@AS@%        break;
  8355. %@AS@%     case 'l':
  8356. %@AS@%        printf( "Loading the file\n" );
  8357. %@AS@%        load_file( fp );
  8358. %@AS@%        break;%@AE@%%@NL@%
  8359. %@NL@%
  8360. %@AS@%  case 's':
  8361. %@AS@%        printf( "Saving the file\n" );
  8362. %@AS@%        write_file( fp );  /* Error! Missing break. */
  8363. %@AS@%     case 'd':
  8364. %@AS@%        printf( "Deleting the file\n" );
  8365. %@AS@%        kill_file( fp );
  8366. %@AS@%        break;
  8367. %@AS@%     default:
  8368. %@AS@%        break;
  8369. %@AS@%  }%@AE@%%@NL@%
  8370. %@NL@%
  8371. In this code a %@AB@%break%@AE@% statement is missing from the statements following the
  8372. third case label (the statements that print %@AS@% Saving the file %@AE@%). After those
  8373. statements execute, execution falls through to the next case label, deleting
  8374. the newly saved file.  %@NL@%
  8375. %@NL@%
  8376. To avoid this problem, place a %@AB@%break%@AE@% at the end of every case item:  %@NL@%
  8377. %@NL@%
  8378. %@AS@%  case 's':
  8379. %@AS@%     printf( "Saving the file.\n" );
  8380. %@AS@%     write_file( fp );
  8381. %@AS@%     break;%@AE@%%@NL@%
  8382. %@NL@%
  8383. It's legal, of course, to write a program in which execution deliberately
  8384. falls through from one case label to the next. In such cases you may want to
  8385. add a comment to prevent confusion.  %@NL@%
  8386. %@NL@%
  8387. %@NL@%
  8388. %@3@%%@CR:C6A00100031 @%%@AB@%Mixing Signed and Unsigned Values%@AE@%%@EH@%%@NL@%
  8389. %@NL@%
  8390. If you explicitly compare two values of different types, the compiler
  8391. normally catches the error. Some type mismatches aren't easy to spot,
  8392. however, even for humans:  %@NL@%
  8393. %@NL@%
  8394. %@AS@%  #define CHARVAL '\xff'
  8395. %@AS@%  
  8396. %@AS@%  main()
  8397. %@AS@%  {
  8398. %@AS@%     unsigned char uc;
  8399. %@AS@%     uc = CHARVAL;
  8400. %@AS@%     if( uc == CHARVAL )
  8401. %@AS@%        printf( "Eureka!" );
  8402. %@AS@%     else
  8403. %@AS@%        printf( "Oops..." );
  8404. %@AS@%  }%@AE@%%@NL@%
  8405. %@NL@%
  8406. The program prints %@AS@% Oops... %@AE@% which probably wasn't expected. The comparison
  8407. between %@AS@% CHARVAL %@AE@% and %@AS@% uc %@AE@% is false even though both are clearly %@AB@%char%@AE@%
  8408. values.  %@NL@%
  8409. %@NL@%
  8410. The answer lies in the way the compiler converts %@AB@%signed%@AE@% and %@AB@%unsigned char%@AE@%
  8411. values into %@AB@%int%@AE@% values for internal use. The %@AB@%#define%@AE@% directive,  %@NL@%
  8412. %@NL@%
  8413. %@AS@%  #define CHARVAL '\xff'%@AE@%%@NL@%
  8414. %@NL@%
  8415. defines %@AS@% CHARVAL %@AE@% as the constant 0xff. Since no sign is specified, the
  8416. compiler treats the constant as a %@AB@%signed char%@AE@% value by default. When it
  8417. converts the %@AB@%char%@AE@% to an %@AB@%int%@AE@% for internal use, as it does all character
  8418. values, the compiler extends the value's sign. The result is an %@AB@%int%@AE@% with the
  8419. value 0xffff.  %@NL@%
  8420. %@NL@%
  8421. The variable %@AS@% uc %@AE@% undergoes the same internal conversion, with an important
  8422. difference. Since %@AS@% uc %@AE@% is explicitly declared as %@AB@%unsigned%@AE@%, its value is
  8423. converted to an %@AB@%int%@AE@% value of 0x00ff.  %@NL@%
  8424. %@NL@%
  8425. When the two %@AB@%int%@AE@% values are compared, the result is false (0xffff does not
  8426. equal 0x00ff). One solution is to explicitly cast %@AS@% CHARVAL %@AE@% to the desired
  8427. type:  %@NL@%
  8428. %@NL@%
  8429. %@AS@%  #define CHARVAL (unsigned char)'\xff'%@AE@%%@NL@%
  8430. %@NL@%
  8431. Now the compiler compares two %@AB@%unsigned char%@AE@% values, giving the desired
  8432. result. Another solution is to make %@AS@% CHARVAL %@AE@% an %@AB@%int%@AE@% instead of a %@AB@%char%@AE@%
  8433. constant:  %@NL@%
  8434. %@NL@%
  8435. %@AS@%  #define CHARVAL 0xff%@AE@%%@NL@%
  8436. %@NL@%
  8437. Both solutions give the desired result, although the second is slightly less
  8438. efficient. It creates word-size, rather than byte-size, machine-code
  8439. instructions.  %@NL@%
  8440. %@NL@%
  8441. %@NL@%
  8442. %@NL@%
  8443. %@NL@%
  8444. %@NL@%
  8445. %@NL@%
  8446. %@CR:C6A-Part 02 @%%@1@%%@AB@%PART II  Using C%@AE@%%@EH@%%@NL@%
  8447. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  8448. %@NL@%
  8449. Part 2 of %@AI@%C for Yourself%@AE@%  is called "Using C" and should be read after you
  8450. are familiar with basic C concepts. It covers practical topics that make it
  8451. possible for you to write real programs. The features discussed in these
  8452. chapters are provided in the QuickC run-time library, which, as you may
  8453. recall from Part 1, is not part of the C language itself.  %@NL@%
  8454. %@NL@%
  8455. While Part 1 was designed to be read sequentially, Part 2 is topical. So you
  8456. don't need to read its chapters in any particular order. If you are new to
  8457. C, however, it is recommended that you begin with Chapter 11, "Input and
  8458. Output," which describes how to read and write data, and process files.
  8459. Similarly, if you're not familiar with QuickC graphics, you should read
  8460. Chapters 13-15 in order.  %@NL@%
  8461. %@NL@%
  8462. %@NL@%
  8463. %@NL@%
  8464. %@NL@%
  8465. %@NL@%
  8466. %@NL@%
  8467. %@CR:C6A00110001 @%%@1@%%@AB@%Chapter 11  Input and Output%@AE@%%@EH@%%@NL@%
  8468. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  8469. %@NL@%
  8470. The first part of this book explored the fundamentals of the C language. In
  8471. the second part (starting with this chapter), the topics include more
  8472. complex and powerful functions: accessing disk files, creating
  8473. high-resolution graphics, creating graphs, manipulating fonts, and adding
  8474. assembly-language routines to your C programs.  %@NL@%
  8475. %@NL@%
  8476. Program examples in previous chapters used %@AB@%printf%@AE@% to print to the screen. In
  8477. this chapter, we'll cover %@AB@%printf%@AE@% in more detail, moving on to other I/O
  8478. functions such as %@AB@%fprintf%@AE@%, which prints to a file, instead of to the screen.
  8479. %@NL@%
  8480. %@NL@%
  8481. This chapter covers three broad topics: keyboard and screen input/output
  8482. (I/O), reading and writing standard disk files, and low-level disk access.
  8483. It also introduces several common string-handling functions.  %@NL@%
  8484. %@NL@%
  8485. %@NL@%
  8486. %@2@%%@CR:C6A00110002 @%%@AB@%Input and Output Streams%@AE@%%@EH@%%@NL@%
  8487. %@NL@%
  8488. Books about C often refer to "input streams" and "output streams." A stream
  8489. is a sequence of bytes flowing into the program (input) or flowing out
  8490. (output). The data might have originally come from the keyboard, a modem, a
  8491. disk file, or some other peripheral device. The outgoing data might be sent
  8492. out to the screen, a modem, or a disk file.  %@NL@%
  8493. %@NL@%
  8494. Thus, when you see a phrase such as "opening a stream," it means opening a
  8495. line of communication to the disk drive or to some other peripheral.  %@NL@%
  8496. %@NL@%
  8497. %@AU@% Peripherals and files are called "streams" in C.%@AE@%  %@NL@%
  8498. %@NL@%
  8499. The five streams always open and available for input or output are shown in
  8500. Table 11.1.  %@NL@%
  8501. %@NL@%
  8502. %@AB@%Table 11.1  %@AB@%Standard I/O Streams%@AE@%%@AE@%
  8503.  
  8504. %@TH:   8   537 02 16 60 @%Name            Stream%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%stdin%@AE@%           Standard input (keyboard)%@AB@%stdout%@AE@%          Standard output (screen)%@AB@%stderr%@AE@%          Standard error channel (screen)%@AB@%stdprn%@AE@%          Standard printer (parallel port)%@AB@%stdaux%@AE@%          Standard auxiliary device (serial port)%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   8   537 02 16 60 @%
  8505.  
  8506. %@NL@%
  8507. %@2@%%@CR:C6A00110003 @%%@AB@%Screen and Keyboard I/O%@AE@%%@EH@%%@NL@%
  8508. %@NL@%
  8509. Imagine an application program that doesn't ever send output to the screen
  8510. or accept input from the keyboard. It's possible to write such a program,
  8511. but it's unlikely you'd ever want to.  %@NL@%
  8512. %@NL@%
  8513. In most situations, you need to display various kinds of data on the screen
  8514. and to accept input from the keyboard. "Manipulating and Printing Strings"
  8515. introduces the functions commonly used to communicate back and forth.  %@NL@%
  8516. %@NL@%
  8517. %@NL@%
  8518. %@3@%%@CR:C6A00110004 @%%@AB@%Manipulating and Printing Strings%@AE@%%@EH@%%@NL@%
  8519. %@NL@%
  8520. %@AU@%  Always pass at least one  string to the printf function.%@AE@%  %@NL@%
  8521. %@NL@%
  8522. Previous chapters have used the %@AB@%printf%@AE@% function to display results on the
  8523. screen. By now you should be accustomed to how it works. There's one rule
  8524. you must always follow when using %@AB@%printf%@AE@%: pass it at least one format
  8525. string, which may be a literal string or a pointer to a string. The string
  8526. may or may not include format specifiers, which are defined below.  %@NL@%
  8527. %@NL@%
  8528. The %@AB@%printf%@AE@% function always prints to the %@AB@%stdout%@AE@% device. Unless the output
  8529. has been redirected, the standard output device is the screen.  %@NL@%
  8530. %@NL@%
  8531. The following program illustrates some typical ways to manipulate strings
  8532. and to print them:  %@NL@%
  8533. %@NL@%
  8534. %@AS@%  /* PRTSTR.C: Print strings. */
  8535. %@AS@%  
  8536. %@AS@%  #include <stdio.h>
  8537. %@AS@%  #include <string.h>
  8538. %@AS@%  
  8539. %@AS@%  main()
  8540. %@AS@%  {
  8541. %@AS@%     char aline[80], more[80];
  8542. %@AS@%     char *strptr;
  8543. %@AS@%  
  8544. %@AS@%     /* aline = "Another line."; */
  8545. %@AS@%     /* Note: This causes a compiler error */
  8546. %@AS@%  
  8547. %@AS@%     strcpy( aline, "Another line." );
  8548. %@AS@%     strcpy( more, aline );
  8549. %@AS@%     strptr = aline;
  8550. %@AS@%     strcat( aline, "dog" );
  8551. %@AS@%     printf( "A line of text." );
  8552. %@AS@%     printf( aline );
  8553. %@AS@%     printf( more );
  8554. %@AS@%     printf( strptr );
  8555. %@AS@%  }%@AE@%%@NL@%
  8556. %@NL@%
  8557. The declarations come first:  %@NL@%
  8558. %@NL@%
  8559. %@AS@%  char aline[80], more[80];
  8560. %@AS@%  char *strptr;%@AE@%%@NL@%
  8561. %@NL@%
  8562. The variables %@AS@% aline %@AE@% and %@AS@% more %@AE@% are arrays of characters. In this program,
  8563. they act as strings. Although these arrays have 80 characters each (numbered
  8564. 0-79), the maximum string length is 79 characters, because strings must end
  8565. with a null character. The variable %@AS@% strptr %@AE@% is a pointer to a string.  %@NL@%
  8566. %@NL@%
  8567. If you've previously programmed in BASIC, you might expect to use the equal
  8568. sign to assign a value to a string variable. The program won't compile if
  8569. you remove the comment symbols from the following line:  %@NL@%
  8570. %@NL@%
  8571. %@AS@%  /* aline = "Another line."; */%@AE@%%@NL@%
  8572. %@NL@%
  8573. Faced with this line, QuickC prints the error message:  %@NL@%
  8574. %@NL@%
  8575. %@AS@%  2106: '=' : Left operand must be lvalue%@AE@%%@NL@%
  8576. %@NL@%
  8577. (an "lvalue" is a value allowed on the left side of an equal sign).  %@NL@%
  8578. %@NL@%
  8579. %@AU@% Use the strcpy function─ not the equal sign─ to copy a string.%@AE@%  %@NL@%
  8580. %@NL@%
  8581. You can use the equal sign to assign a value to a numeric variable. When
  8582. you're using strings, however, you almost always use the library function
  8583. %@AB@%strcpy%@AE@%, which copies a string to a character array from either a string
  8584. constant or another array:  %@NL@%
  8585. %@NL@%
  8586. %@AS@%  strcpy( aline, "Another line." );
  8587. %@AS@%  strcpy( more, aline );%@AE@%%@NL@%
  8588. %@NL@%
  8589. The %@AB@%strcpy%@AE@% function makes an exact copy of a string. The first argument is
  8590. the address of the destination string. The second is the address of the
  8591. source string. The first %@AB@%strcpy%@AE@% above copies %@AS@%" Another line." %@AE@% to the %@AS@% aline
  8592. %@AS@%%@AE@% string. The second copies %@AS@% aline %@AE@% to %@AS@% more%@AE@%.  %@NL@%
  8593. %@NL@%
  8594. Note that the first argument must be the address of an array, but the second
  8595. is either a string constant (enclosed in quotation marks) or the address of
  8596. a character array.  %@NL@%
  8597. %@NL@%
  8598. The 80-character arrays have more than enough room for the 13 characters of
  8599. %@AS@%"Another line." %@AE@% and a null character. In your own programs, you should be
  8600. aware of the declared size of an array and avoid overrunning the bounds of
  8601. the array. See Chapter 10, "Programming Pitfalls," for more information
  8602. about this programming mistake.  %@NL@%
  8603. %@NL@%
  8604. It is possible to assign the address of a string to a pointer:  %@NL@%
  8605. %@NL@%
  8606. %@AS@%  strptr = aline;%@AE@%%@NL@%
  8607. %@NL@%
  8608. Notice that both %@AS@% strptr %@AE@% and %@AS@% aline %@AE@% point to the same string. There's one
  8609. object in memory, but it has two different names. If %@AS@% aline %@AE@% changes, the
  8610. same change occurs in the string referenced by %@AS@% strptr%@AE@%, because they're the
  8611. same string. Below, the word %@AS@%"dog" %@AE@% is added to the end of the string %@AS@%
  8612. %@AS@%aline%@AE@%:  %@NL@%
  8613. %@NL@%
  8614. %@AS@%  strcat( aline, "dog" );%@AE@%%@NL@%
  8615. %@NL@%
  8616. The %@AB@%strcat%@AE@% function concatenates one string to the end of a second string.
  8617. In the line above, both %@AS@% aline %@AE@% and the string referenced by %@AS@% strptr %@AE@% have
  8618. been changed from %@AS@% "Another line." %@AE@% to %@AS@% "Another line.dog".%@AE@%  %@NL@%
  8619. %@NL@%
  8620. Now four %@AB@%printf%@AE@% statements execute:  %@NL@%
  8621. %@NL@%
  8622. %@AS@%  printf( "A line of text." );
  8623. %@AS@%  printf( aline );
  8624. %@AS@%  printf( more );
  8625. %@AS@%  printf( strptr );%@AE@%%@NL@%
  8626. %@NL@%
  8627. The screen should look like this:  %@NL@%
  8628. %@NL@%
  8629. %@AS@%  A line of text.Another line.dogAnother line.Another line.dog%@AE@%%@NL@%
  8630. %@NL@%
  8631. To the first %@AB@%printf%@AE@% we passed a string constant. To the other three we
  8632. passed names of strings. Concatenating %@AS@% aline %@AE@% and %@AS@% "dog" %@AE@% also affected the
  8633. string referenced by %@AS@% strptr%@AE@%, because they both point to the same string in
  8634. memory. The contents of %@AS@% more %@AE@% weren't affected, however, because the %@AB@%strcpy%@AE@%
  8635. function makes a complete and unique copy of the source string at the memory
  8636. location referenced by %@AS@% more%@AE@%.  %@NL@%
  8637. %@NL@%
  8638. Unfortunately, the strings ran together. As we saw in Chapter 1, "Anatomy of
  8639. a C Program," %@AB@%printf%@AE@% is unlike QuickBASIC's %@AB@%PRINT%@AE@% command or Pascal's %@AB@%
  8640. %@AB@%Writeln%@AE@% procedure in one respect: it does not automatically move the cursor
  8641. to the beginning of the next line. You need to include the newline character
  8642. (%@AB@%\n%@AE@%), which is one of a series of available escape codes discussed in
  8643. Chapter 4, "Data Types." The program below includes a few examples of escape
  8644. codes, each of which begins with the backslash character:  %@NL@%
  8645. %@NL@%
  8646. %@AS@%  /* PRTESC.C: Print escape characters \",\n, and \t. */
  8647. %@AS@%  
  8648. %@AS@%  #include <stdio.h>
  8649. %@AS@%  #include <string.h>
  8650. %@AS@%  
  8651. %@AS@%  main()
  8652. %@AS@%  {
  8653. %@AS@%     char b[80];
  8654. %@AS@%     int i,j;
  8655. %@AS@%  
  8656. %@AS@%     strcpy( b, "and seven years ago\n" );
  8657. %@AS@%     printf( "\"Four score\n" );
  8658. %@AS@%     printf( b );
  8659. %@AS@%     printf( "\tone tab\n\t\ttwo tabs\n\t\t\tthree tabs\n" );
  8660. %@AS@%     i = sizeof( b );
  8661. %@AS@%     j = strlen( b );
  8662. %@AS@%     printf( "Size is %d\nLength is %d.\n", i, j );
  8663. %@AS@%  }%@AE@%%@NL@%
  8664. %@NL@%
  8665. If you compile and run the PRTESC.C program, the following text prints on
  8666. the screen:  %@NL@%
  8667. %@NL@%
  8668. %@AS@%  "Four score
  8669. %@AS@%  and seven years ago
  8670. %@AS@%   one tab
  8671. %@AS@%    two tabs
  8672. %@AS@%     three tabs
  8673. %@AS@%  Size is 80
  8674. %@AS@%  Length is 20.%@AE@%%@NL@%
  8675. %@NL@%
  8676. To print a newline character in a string, type a backslash and the letter n
  8677. (%@AB@%\n%@AE@%). For a quotation mark, use %@AB@%\"%@AE@%. For tabs, use %@AB@%\t%@AE@%. Escape sequences can
  8678. appear anywhere within a string:  %@NL@%
  8679. %@NL@%
  8680. %@AS@%  printf( "\tone tab\n\t\ttwo tabs\n\t\t\tthree tabs\n" );%@AE@%%@NL@%
  8681. %@NL@%
  8682. You'll find complete lists of escape characters in Appendix A, "C Language
  8683. Guide," and in online help.  %@NL@%
  8684. %@NL@%
  8685. %@NL@%
  8686. %@4@%%@AB@%Finding the Size%@AE@%%@EH@%%@NL@%
  8687. %@NL@%
  8688. The last call to %@AB@%printf%@AE@% in PRTESC.C provides two pieces of information: the
  8689. size of the character array and the length of the string inside the array.  %@NL@%
  8690. %@NL@%
  8691. The variable %@AS@% b %@AE@% was declared to be an 80-character array, but the string
  8692. inside %@AS@%b %@AE@% contains only 20 characters; it holds 19 letters plus one newline
  8693. character. Although typing %@AB@%\n%@AE@% takes two characters, it's stored in memory as
  8694. one character─ the ASCII value 10. As we'll see later in this chapter, the
  8695. newline character is sometimes expanded to two characters (a carriage return
  8696. and a linefeed) when it is written to disk. But while it's in memory, it's a
  8697. single character.  %@NL@%
  8698. %@NL@%
  8699. %@AU@% The sizeof operator examines array size; the %@AE@%%@AB@%strlen%@AE@% function returns the
  8700. length of a string.  %@NL@%
  8701. %@NL@%
  8702. There are two methods available to find the size of arrays and strings. The
  8703. %@AB@%sizeof %@AE@%operator returns the size (in bytes) of an identifier or type. The
  8704. string-handling function %@AB@%strlen%@AE@% counts the number of characters in a string,
  8705. up to but not including the null that marks the end of the string:  %@NL@%
  8706. %@NL@%
  8707. %@AS@%  i = sizeof( b );
  8708. %@AS@%  j = strlen( b );
  8709. %@AS@%  printf( "Size is %d\nLength is %d.\n", i, j );%@AE@%%@NL@%
  8710. %@NL@%
  8711. The final line of the program PRTESC.C prints out two integer values, which
  8712. follow the format string. When %@AB@%printf%@AE@% evaluates the format string, it
  8713. substitutes the two values for the %@AB@%%d%@AE@% specifiers:  %@NL@%
  8714. %@NL@%
  8715. %@AS@%  Size is 80
  8716. %@AS@%  Length is 20%@AE@%%@NL@%
  8717. %@NL@%
  8718. The %@AB@%sizeof%@AE@% operator is part of the C language. In this example, it evaluates
  8719. to the value 80, which is the size of the array. The %@AB@%strlen%@AE@% function is a
  8720. library function for measuring strings (up to, but not including the null at
  8721. the end). It returns a 20 because that's the length of the string.  %@NL@%
  8722. %@NL@%
  8723. %@NL@%
  8724. %@3@%%@CR:C6A00110005 @%%@AB@%Printing Numeric Values%@AE@%%@EH@%%@NL@%
  8725. %@NL@%
  8726. %@AU@% The printf format string  may hold one or more  format specifiers.%@AE@%  %@NL@%
  8727. %@NL@%
  8728. We've seen how %@AB@%printf%@AE@% requires at least one string (or a pointer to a
  8729. string). To print variables and values, place a comma and the name of the
  8730. variable or value after the format string. Then, within the format string,
  8731. include a format specification. See Table 11.2.  %@NL@%
  8732. %@NL@%
  8733. %@AB@%Table 11.2  %@AB@%Common Format Specifications%@AE@%%@AE@%
  8734.  
  8735. %@TH:  10   675 02 22 54 @%Specification         Format%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%%c%@AE@%                    Print a character%@AB@%%d%@AE@%                    Print a decimal integer%@AB@%%f%@AE@%                    Print a floating-point number%@AB@%%i%@AE@%                    Print a decimal integer (same as %@AB@%%d%@AE@%)%@AB@%%s%@AE@%                    Print a string%@AB@%%u%@AE@%                    Print an unsigned integer%@AB@%%x%@AE@%                    Print in hexadecimal format%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  10   675 02 22 54 @%
  8736.  
  8737. The percent sign (%@AB@%%%@AE@%) always marks the beginning of a format specification.
  8738. The letters %@AB@%c%@AE@%, %@AB@%d%@AE@%, %@AB@%f%@AE@%, %@AB@%i%@AE@%, %@AB@%s%@AE@%, %@AB@%u%@AE@%, and %@AB@%x%@AE@% are called the "type." Between the
  8739. percent sign and the type, you may include optional specifications for
  8740. flags, width, or precision values.  %@NL@%
  8741. %@NL@%
  8742. At the very least, you must include the type, as in the program below:  %@NL@%
  8743. %@NL@%
  8744. %@AS@%  /* NFORMAT.C: Print numbers and a string. */
  8745. %@AS@%  
  8746. %@AS@%  #include <stdio.h>
  8747. %@AS@%  #include <string.h>
  8748. %@AS@%  
  8749. %@AS@%  main()
  8750. %@AS@%  {
  8751. %@AS@%     int    a = -765,
  8752. %@AS@%            b = 1,
  8753. %@AS@%            c = 44000,
  8754. %@AS@%            d = 33;
  8755. %@AS@%     float  e = 1.33E8,
  8756. %@AS@%            f = -0.1234567,
  8757. %@AS@%            g = 12345.6789,
  8758. %@AS@%            h = 1.0;
  8759. %@AS@%     char   i[80];
  8760. %@AS@%  
  8761. %@AS@%     strcpy( i, "word 1, word 2, word 3, word 4, word 5" );
  8762. %@AS@%  
  8763. %@AS@%     printf( "Unformatted:\n%d %d %d %d\n", a, b, c, d );
  8764. %@AS@%     printf( "%f %f %f %f\n", e, f, g, h );
  8765. %@AS@%     printf( "%s\n", i );
  8766. %@AS@%  }%@AE@%%@NL@%
  8767. %@NL@%
  8768. The output looks like this:  %@NL@%
  8769. %@NL@%
  8770. %@AS@%  Unformatted:
  8771. %@AS@%  -765 1 -21536 33
  8772. %@AS@%  133000000.000000 -0.123457 12345.678711 1.000000
  8773. %@AS@%  word 1, word 2, word 3, word 4, word 5%@AE@%%@NL@%
  8774. %@NL@%
  8775. If you carefully compare NFORMAT.C with its output, you'll notice some
  8776. unexpected results. For example, the variable %@AS@% c%@AE@%, which was initialized to
  8777. 44000, has somehow changed to -21536.  %@NL@%
  8778. %@NL@%
  8779. The %@AB@%%d%@AE@% format specification applies to signed integers in the range -32768
  8780. to +32767. The value of %@AS@% c %@AE@% (44000) is outside that range, but still within
  8781. the realm of unsigned integers, which can hold values up to +65535. The
  8782. proper format specification would be %@AB@%%u%@AE@% (where %@AS@% u %@AE@% represents the unsigned
  8783. type).  %@NL@%
  8784. %@NL@%
  8785. Two of the floating-point values have changed, too. The %@AB@%%f%@AE@% specification
  8786. defaults to 6 digits of precision to the right of the decimal point. The
  8787. value of %@AS@% f %@AE@% (.1234567) is therefore rounded off to a precision of 6 digits:
  8788. .123457. Also, the limitations of floating-point accuracy transform the
  8789. value of %@AS@% g %@AE@% from 12345.6789 to 12345.678711. If you modify the program,
  8790. changing the %@AS@% float %@AE@% declarations to %@AS@% double%@AE@%, the second problem disappears.
  8791. The variable %@AS@% g %@AE@% prints correctly as 12345.67.  %@NL@%
  8792. %@NL@%
  8793. Between the%@AB@%%%@AE@% and the type character, you may include two numbers separated
  8794. by a period. The first number is called the "width;" the second is the
  8795. "precision." The width and precision affect integers, floating-point
  8796. numbers, and strings in different ways. For example, we could specify a
  8797. width of 2 and precision of 3 for each of the above variables:  %@NL@%
  8798. %@NL@%
  8799. %@AS@%  printf( "\nWidth 2, Precision 3:\n" );
  8800. %@AS@%  printf( "%2.3d %2.3d %2.3u %2.3d\n", a, b, c, d );
  8801. %@AS@%  printf( "%2.3f %2.3f %2.3f %2.3f\n", e, f, g, h );
  8802. %@AS@%  printf( "%2.3s\n", i );%@AE@%%@NL@%
  8803. %@NL@%
  8804. (Note that the variable %@AS@% c %@AE@% has a format specifier of %@AS@% %2.3u %@AE@% instead of %@AS@%
  8805. %@AS@%%2.3d%@AE@%.) The screen displays the following lines:  %@NL@%
  8806. %@NL@%
  8807. %@AS@%  Width 2, Precision 3:
  8808. %@AS@%  -765 001 44000 033
  8809. %@AS@%  133000000.000 -0.123 12345.679 1.000
  8810. %@AS@%  wor%@AE@%%@NL@%
  8811. %@NL@%
  8812. For integers, the precision of 3 causes at least 3 digits to print, preceded
  8813. by leading zeros. For floating-point numbers, the precision of 3 truncates
  8814. fractions to 3 digits to the right of the decimal point. For strings, the
  8815. precision of 3 causes only 3 characters to print. The string output is
  8816. truncated to the right. Numbers are never truncated, however.  %@NL@%
  8817. %@NL@%
  8818. We can change the width to 8 and the precision to 1:  %@NL@%
  8819. %@NL@%
  8820. %@AS@%  printf( "\nWidth 8, Precision 1:\n" );
  8821. %@AS@%  printf( "%8.1d %8.1d %8.1u %8.1d\n", a, b, c, d );
  8822. %@AS@%  printf( "%8.1e %8.1f %8.1f %8.1f\n", e, f, g, h );
  8823. %@AS@%  printf( "%8.1s\n", i );%@AE@%%@NL@%
  8824. %@NL@%
  8825. We made an additional modification by printing the variable %@AS@% e %@AE@% as an %@AB@%%e%@AE@%
  8826. type instead of an %@AB@%%f%@AE@% type. This prints the value of %@AS@% e %@AE@% (1.33E8) in
  8827. exponential format:  %@NL@%
  8828. %@NL@%
  8829. %@AS@%  Width 8, Precision 1:
  8830. %@AS@%      -765        1    44000       33
  8831. %@AS@%  1.3e+008     -0.1  12345.7      1.0
  8832. %@AS@%         w%@AE@%%@NL@%
  8833. %@NL@%
  8834. The width controls the printing area: all 3 variable types are printed in
  8835. fields 8 characters wide. The precision of 1 affects different data types in
  8836. different ways: the integers print at least 1 digit; the floating-point
  8837. numbers print only the first number to the right of the decimal point; and
  8838. the string prints as the first character only. Each value prints flush right
  8839. in its field.  %@NL@%
  8840. %@NL@%
  8841. Between the %@AB@%%%@AE@% and the width, you may also insert a flag. The plus flag (%@AB@%+%@AE@%),
  8842. for example, forces numbers to print with a leading sign:  %@NL@%
  8843. %@NL@%
  8844. %@AS@%  printf( "\nForced signs, Width 10, Precision 2:\n" );
  8845. %@AS@%  printf( "%+10.2d %+10.2d %+10.2u %+10.2d\n", a, b, c, d );
  8846. %@AS@%  printf( "%+10.2e %+10.2f %+10.2f %+10.2f\n", e, f, g, h );
  8847. %@AS@%  printf( "%+10.2s\n", i );%@AE@%%@NL@%
  8848. %@NL@%
  8849. Note that the plus flag has no effect on strings or on unsigned integers:  %@NL@%
  8850. %@NL@%
  8851. %@AS@%  Forced signs, Width 10, Precision 2:
  8852. %@AS@%        -765        +01      44000        +33
  8853. %@AS@%  +1.33e+008      -0.12  +12345.68      +1.00
  8854. %@AS@%          wo%@AE@%%@NL@%
  8855. %@NL@%
  8856. Another flag is the number 0, which forces leading zeros to print within the
  8857. limits of the width. If you only specify the width, the system default is
  8858. used for the precision. You can use the type %@AB@%%x%@AE@% to represent hexadecimal; it
  8859. displays the letters a-f in lowercase. If you prefer uppercase, you can use
  8860. %@AB@%%X%@AE@% instead.  %@NL@%
  8861. %@NL@%
  8862. %@AS@%  printf( "\nHexadecimal, Forced Zeros, Width 6:\n" );
  8863. %@AS@%  printf( "%06x %06x %06x %06x\n", a, b, c, d );%@AE@%%@NL@%
  8864. %@NL@%
  8865. The %@AB@%printf%@AE@% statements above display these lines:  %@NL@%
  8866. %@NL@%
  8867. %@AS@%  Hexadecimal, Forced Zeros, Width 6:
  8868. %@AS@%  00fd03 000001 00abe0 000021%@AE@%%@NL@%
  8869. %@NL@%
  8870. For strings, the width and precision specifiers describe the field width and
  8871. the number of characters printed. Note the minus sign in the final line,
  8872. which forces the truncated string to print from the left:  %@NL@%
  8873. %@NL@%
  8874. %@AS@%  printf( "\nWidth 40, Precision 10:\n" );
  8875. %@AS@%  printf( "%40.10s\n", i );
  8876. %@AS@%  
  8877. %@AS@%  printf( "\nWidth 40, Precision 20:\n" );
  8878. %@AS@%  printf( "%40.20s\n", i );
  8879. %@AS@%  
  8880. %@AS@%  printf( "\nFlush left, Width 40, Precision 20:\n" );
  8881. %@AS@%  printf( "%-40.20s\n", i );%@AE@%%@NL@%
  8882. %@NL@%
  8883. The lines are displayed on the screen as follows:  %@NL@%
  8884. %@NL@%
  8885. %@AS@%  Width 40, Precision 10:
  8886. %@AS@%                                word 1, wo
  8887. %@AS@%  
  8888. %@AS@%  Width 40, Precision 20:
  8889. %@AS@%                      word 1, word 2, word
  8890. %@AS@%  
  8891. %@AS@%  Flush left, Width 40, Precision 20:
  8892. %@AS@%  word 1, word 2, word                    %@AE@%%@NL@%
  8893. %@NL@%
  8894. %@NL@%
  8895. %@3@%%@CR:C6A00110006 @%%@AB@%Using scanf for Keyboard Input%@AE@%%@EH@%%@NL@%
  8896. %@NL@%
  8897. %@AU@%   Pass a variable address to scanf, not a variable value.%@AE@%  %@NL@%
  8898. %@NL@%
  8899. While %@AB@%printf%@AE@% is the most widely used output function, %@AB@%scanf %@AE@%is the most
  8900. popular for input. The arguments and format strings passed to%@AB@% scanf%@AE@% resemble
  8901. the arguments for %@AB@%printf%@AE@%, except for one requirement: the %@AB@%scanf%@AE@% function
  8902. always takes pointers. You never pass a variable value to %@AB@%scanf%@AE@%, you always
  8903. pass the variable address so that %@AB@%scanf%@AE@% can store data in the memory
  8904. location that contains the input variable.  %@NL@%
  8905. %@NL@%
  8906. The first argument for %@AB@%scanf%@AE@% is always a format string. Additional arguments
  8907. include the addresses of variables to which values will be assigned.  %@NL@%
  8908. %@NL@%
  8909. The program below demonstrates several ways to use %@AB@%scanf%@AE@% and various other
  8910. I/O functions:  %@NL@%
  8911. %@NL@%
  8912. %@AS@%  /* INPUT.C: Read keyboard. */
  8913. %@AS@%  
  8914. %@AS@%  #include <stdio.h>
  8915. %@AS@%  #include <conio.h>
  8916. %@AS@%  #include <ctype.h>%@AE@%%@NL@%
  8917. %@NL@%
  8918. %@AS@%  main()
  8919. %@AS@%  {
  8920. %@AS@%     int num;
  8921. %@AS@%     char c;
  8922. %@AS@%     char name[80];
  8923. %@AS@%     float rb;
  8924. %@AS@%  
  8925. %@AS@%     puts( "** Type \"Name:\" and your name" );
  8926. %@AS@%     scanf( "Name: %40s", name );
  8927. %@AS@%     printf( "** You typed this:\n%s", name );
  8928. %@AS@%     puts( "\n\n** Try again, with the gets function." );
  8929. %@AS@%     fflush( stdin );
  8930. %@AS@%     gets( name );
  8931. %@AS@%     printf( "** You typed this:\n%s\n", name );
  8932. %@AS@%  
  8933. %@AS@%     printf( "\n** Now type an integer.\n" );
  8934. %@AS@%     scanf( "%i", &num );
  8935. %@AS@%     sprintf( name, "** You typed this number: %i\n", num );
  8936. %@AS@%     puts( name );
  8937. %@AS@%  
  8938. %@AS@%     fflush( stdin );
  8939. %@AS@%     printf( "** Enter a floating-point value.\n" );
  8940. %@AS@%     scanf( "%f", &rb );
  8941. %@AS@%     printf( "** The answer is %f or %e\n", rb, rb );
  8942. %@AS@%  
  8943. %@AS@%     printf( "** Continue? Y or N\n" );
  8944. %@AS@%     do
  8945. %@AS@%     {
  8946. %@AS@%        c = getch();
  8947. %@AS@%        c = tolower( c );
  8948. %@AS@%     } while( c != 'y' && c != 'n' );
  8949. %@AS@%  }%@AE@%%@NL@%
  8950. %@NL@%
  8951. First, the %@AB@%puts%@AE@% function prints a string that requests input from the user.
  8952. Then %@AB@%scanf%@AE@% reads the input:  %@NL@%
  8953. %@NL@%
  8954. %@AS@%  puts( "** Type \"Name:\" and your name" );
  8955. %@AS@%  scanf( "Name: %40s", name );%@AE@%%@NL@%
  8956. %@NL@%
  8957. Unfortunately, the use of %@AB@%scanf%@AE@% for string input creates some difficulties.
  8958. For one thing, you're forced to type %@AS@% Name: %@AE@% before typing the rest of the
  8959. string. (If you don't type %@AS@% Name:%@AE@%, %@AB@%scanf%@AE@% won't put a value into the %@AS@% name %@AE@%
  8960. variable.)  %@NL@%
  8961. %@NL@%
  8962. A second problem is that %@AB@%scanf%@AE@% reads the input stream until it finds a
  8963. white-space character: a SPACE, TAB, or ENTER.  %@NL@%
  8964. %@NL@%
  8965. The prompt below appears on the screen:  %@NL@%
  8966. %@NL@%
  8967. %@AS@%  ** Type "Name:" and your name%@AE@%%@NL@%
  8968. %@NL@%
  8969. You might type this (you must begin the line with %@AS@%"Name:" %@AE@%):  %@NL@%
  8970. %@NL@%
  8971. %@AS@%  Name: F. Scott Fitzgerald%@AE@%%@NL@%
  8972. %@NL@%
  8973. The next line takes effect:  %@NL@%
  8974. %@NL@%
  8975. %@AS@%  printf( "** You typed this:\n%s", name );%@AE@%%@NL@%
  8976. %@NL@%
  8977. Which prints the following line:  %@NL@%
  8978. %@NL@%
  8979. %@AS@%  ** You typed this:
  8980. %@AS@%  F.%@AE@%%@NL@%
  8981. %@NL@%
  8982. The string passed to the %@AB@%scanf%@AE@% function told it to expect %@AS@% "Name:" %@AE@% and then
  8983. to read a string, storing it in the %@AS@% name %@AE@% variable.  %@NL@%
  8984. %@NL@%
  8985. Since the %@AB@%scanf%@AE@% function reads strings until it finds a white-space
  8986. character, the value of %@AS@% name %@AE@% is " %@AS@% F%@AE@%." In addition, the words %@AS@% Scott
  8987. %@AS@%Fitzgerald %@AE@% are waiting in the input stream. To clear any stream, use the
  8988. %@AB@%fflush%@AE@% function:  %@NL@%
  8989. %@NL@%
  8990. %@AS@%  puts( "\n\n** Now try it again, with the gets function." );
  8991. %@AS@%  fflush( stdin );
  8992. %@AS@%  gets( name );%@AE@%%@NL@%
  8993. %@NL@%
  8994. To clear the buffer associated with a stream (including disk files), call
  8995. %@AB@%fflush%@AE@%, passing the pointer to the file or stream. In the example above, %@AS@%
  8996. %@AS@%stdin %@AE@% is the standard input device, the keyboard.  %@NL@%
  8997. %@NL@%
  8998. The %@AB@%puts%@AE@% function acts like a limited version of %@AB@%printf%@AE@%. It prints a string
  8999. to the standard output device, but can't insert formatted variable values.
  9000. You pass it a string constant or the name of a string. Also, it always adds
  9001. a newline to the end of the string it prints.  %@NL@%
  9002. %@NL@%
  9003. %@AU@% It is usually preferable to  use gets when working  with string input.%@AE@%  %@NL@%
  9004. %@NL@%
  9005. The %@AB@%gets%@AE@% function receives an entire line from the standard input device and
  9006. places the line in an array of characters. It does not include the newline
  9007. character typed by the user. It does, however, add a null to the end of the
  9008. line, to make the series of characters into a string. When you're working
  9009. with string input, %@AB@%gets%@AE@% is generally preferable to %@AB@%scanf%@AE@%.  %@NL@%
  9010. %@NL@%
  9011. For numeric values, %@AB@%scanf%@AE@% is the function of choice:  %@NL@%
  9012. %@NL@%
  9013. %@AS@%  printf( "\n** Now type an integer.\n" );
  9014. %@AS@%  scanf( "%i", &num );
  9015. %@AS@%  sprintf( name, "** You typed the number: %i\n", num );
  9016. %@AS@%  puts( name );%@AE@%%@NL@%
  9017. %@NL@%
  9018. The format string %@AB@%%i%@AE@% forces %@AB@%scanf%@AE@% to treat the input as an integer. The
  9019. second argument is the address of the variable %@AS@% num%@AE@%.  %@NL@%
  9020. %@NL@%
  9021. The letter %@AB@%s%@AE@% in %@AB@%sprintf%@AE@% marks it as a string function. (There is also a
  9022. %@AB@%sscanf%@AE@% function that handles strings, but we won't discuss it here.) Instead
  9023. of printing the format string to the screen, as %@AB@%printf%@AE@% would do, %@AB@%sprintf%@AE@%
  9024. prints the results to another string. Note that %@AB@%scanf%@AE@% requires the address
  9025. of %@AS@% num%@AE@%, but %@AB@%sprintf%@AE@% uses its value.  %@NL@%
  9026. %@NL@%
  9027. The next %@AB@%scanf%@AE@% in program INPUT.C treats the input as a floating-point
  9028. number:  %@NL@%
  9029. %@NL@%
  9030. %@AS@%  scanf( "%f", &rb );
  9031. %@AS@%  printf( "** The answer is %f or %e\n", rb, rb );%@AE@%%@NL@%
  9032. %@NL@%
  9033. If you enter -555.12, the computer responds:  %@NL@%
  9034. %@NL@%
  9035. %@AS@%  ** The answer is -555.119995 or -5.551200e+002%@AE@%%@NL@%
  9036. %@NL@%
  9037. Finally, the program uses %@AB@%getch%@AE@% to receive a character from the input
  9038. stream:  %@NL@%
  9039. %@NL@%
  9040. %@AS@%  printf( "** Continue? Y or N\n" );
  9041. %@AS@%  
  9042. %@AS@%     do
  9043. %@AS@%     {
  9044. %@AS@%        c = getch();
  9045. %@AS@%        c = tolower( c );
  9046. %@AS@%     } while( c != 'y' && c != 'n' );%@AE@%%@NL@%
  9047. %@NL@%
  9048. The %@AB@%getch%@AE@% function returns a character. That value, in turn, is passed to
  9049. %@AB@%tolower%@AE@%, which converts any uppercase characters to lowercase (in case the
  9050. CAPS LOCK key is on). Then, the byte is assigned to the variable %@AS@% c%@AE@%. The %@AB@%do%@AE@%
  9051. loop continues processing characters until you press %@AS@% y %@AE@% or %@AS@% n%@AE@%. The program
  9052. then ends. This simple example ends no matter which key ( %@AS@% y %@AE@% or %@AS@% n %@AE@%) you
  9053. press. A real program would take some action based on the value returned by
  9054. the %@AB@%getch%@AE@% function.  %@NL@%
  9055. %@NL@%
  9056. %@NL@%
  9057. %@2@%%@CR:C6A00110007 @%%@AB@%Standard Disk I/O%@AE@%%@EH@%%@NL@%
  9058. %@NL@%
  9059. If you can read input from the keyboard and write output to the screen,
  9060. you'll find standard disk files relatively easy to manipulate. There are
  9061. three rules to remember:  %@NL@%
  9062. %@NL@%
  9063. %@NL@%
  9064.   1.  You can't do anything with a disk file until you open it. The act of
  9065.       opening a file gives you a %@AB@%FILE%@AE@% pointer through which you can access
  9066.       the file.%@NL@%
  9067. %@NL@%
  9068.   2.  While the file is open, you can use most of the screen and keyboard
  9069.       I/O functions if you precede them with the letter %@AB@%f%@AE@% (%@AB@%fprintf%@AE@% instead
  9070.       of %@AB@%printf%@AE@%, for example). The file-handling functions work the same as
  9071.       their counterparts, but you must add the %@AB@%FILE%@AE@% pointer.%@NL@%
  9072. %@NL@%
  9073.   3.  When you're finished with a file, it's good programming practice to
  9074.       close it. When %@AB@%exit%@AE@% ends the execution of a program, all previously
  9075.       open files are closed (if you'd rather leave them open, use %@AB@%_exit%@AE@%
  9076.       instead of %@AB@%exit%@AE@%).%@NL@%
  9077. %@NL@%
  9078. %@NL@%
  9079. %@NL@%
  9080. %@3@%%@CR:C6A00110008 @%%@AB@%Creating and Writing to a Text File%@AE@%%@EH@%%@NL@%
  9081. %@NL@%
  9082. The WRFILE.C program opens a text file, writes a string to it, and closes
  9083. the file.  %@NL@%
  9084. %@NL@%
  9085. %@AS@%  /* WRFILE.C: Create and write to a disk file. */
  9086. %@AS@%  
  9087. %@AS@%  #include <stdio.h>
  9088. %@AS@%  
  9089. %@AS@%  main()
  9090. %@AS@%  {
  9091. %@AS@%     FILE *fp;
  9092. %@AS@%  
  9093. %@AS@%     if( (fp = fopen( "c:\\testfile.asc","w" )) != NULL )
  9094. %@AS@%     {
  9095. %@AS@%        fputs( "Example string", fp );
  9096. %@AS@%        fputc( '\n', fp );
  9097. %@AS@%        fclose( fp );
  9098. %@AS@%     }
  9099. %@AS@%     else
  9100. %@AS@%        printf( "error message\n" );
  9101. %@AS@%  }%@AE@%%@NL@%
  9102. %@NL@%
  9103. You must include the standard I/O header file (%@AS@% #include <stdio.h> %@AE@%)
  9104. whenever you plan to call input or output functions. It contains essential
  9105. definitions and prototypes that you need.  %@NL@%
  9106. %@NL@%
  9107. The only variable in this program is %@AS@% fp %@AE@% which is declared as a pointer to
  9108. a %@AB@%FILE%@AE@%. %@AB@%FILE%@AE@% is defined in STDIO.H as a structure of %@AB@%_iobuf%@AE@% type, but we
  9109. don't need to know the specifics. We will refer to the variable %@AS@% fp %@AE@% as a
  9110. "%@AB@%FILE%@AE@% pointer."  %@NL@%
  9111. %@NL@%
  9112. The first statement combines several operations in one line:  %@NL@%
  9113. %@NL@%
  9114. %@AS@%  if( (fp = fopen( "c:\\testfile.asc", "w" )) != NULL )%@AE@%%@NL@%
  9115. %@NL@%
  9116. The %@AB@%fopen%@AE@% function opens a file. It expects two parameters, both of which
  9117. are literal strings or pointers to strings. You provide the name of the file
  9118. to be opened and the type (read, write, or append). The six types of files
  9119. are listed in Table 11.3.  %@NL@%
  9120. %@NL@%
  9121. %@AB@%Table 11.3  %@AB@%Disk File Types %@AE@%%@AE@%
  9122.  
  9123. %@TH:  24  1319 02 34 42 @%Type                              Action%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%r%@AE@%                                 Open an existing file for reading.%@AB@%w%@AE@%                                 Create and open a file for writing. Any                                   existing file is replaced. If the file                                   doesn't exist, a new file is created.%@AB@%a%@AE@%                                 Open a file for appending. Data is added                                  to the end of an existing file or                                   a new file is created.%@AB@%r+%@AE@%                                Open an existing file for reading and                                   writing. %@AS@%%@AE@%%@AB@%w+%@AE@%                                Create and open a file for reading and                                   writing. An existing file is replaced.%@AB@%a+%@AE@%                                Open a file for reading and appending.                                   Data is added to the end of an                                   existing file or a new file is created.%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  24  1319 02 34 42 @%
  9124.  
  9125. In WRFILE.C, the file called %@AS@% c:\testfile.asc %@AE@% is opened for writing with
  9126. type %@AS@%"w" %@AE@% (a string, not the character %@AS@% 'w' %@AE@% in single quotes). We plan to
  9127. write to it.  %@NL@%
  9128. %@NL@%
  9129. Notice that the file-name string as it appears in the %@AB@%fopen%@AE@% statement
  9130. contains two backslashes: %@AS@%"c:\\testfile.asc"%@AE@%. If you tried to use the string
  9131. %@AS@%"c:\testfile. asc"%@AE@%, which looks correct, the character sequence %@AS@% \t %@AE@% would
  9132. be incorrectly interpreted as a tab character. C automatically converts the
  9133. two backslashes in the string to a single backslash. %@AS@%  %@AE@%%@NL@%
  9134. %@NL@%
  9135. %@NL@%
  9136. %@4@%%@AB@%Getting a FILE Pointer%@AE@%%@EH@%%@NL@%
  9137. %@NL@%
  9138. The %@AB@%fopen%@AE@% function returns the address of a %@AB@%FILE%@AE@%. This value is assigned to %@AS@%
  9139. %@AS@%fp%@AE@%, which is the %@AB@%FILE%@AE@% pointer used in all subsequent file operations.  %@NL@%
  9140. %@NL@%
  9141. If something goes wrong─if the disk is full or not in the drive or
  9142. write-protected or whatever─%@AB@%fopen%@AE@% doesn't return a %@AB@%FILE%@AE@% pointer. When %@AB@%fopen%@AE@%
  9143. fails, it returns a null value.  %@NL@%
  9144. %@NL@%
  9145. What we're looking for is any %@AB@%FILE%@AE@% pointer that's not null:  %@NL@%
  9146. %@NL@%
  9147. %@AS@%  if( (fp = fopen( "c:\\testfile.asc","w" )) != NULL )%@AE@%%@NL@%
  9148. %@NL@%
  9149. %@NL@%
  9150. %@4@%%@AB@%Writing to the File%@AE@%%@EH@%%@NL@%
  9151. %@NL@%
  9152. As we saw earlier, %@AB@%puts%@AE@% displays a string on the screen. Add an %@AB@%f%@AE@% to it and
  9153. the result is %@AB@%fputs%@AE@%, which works similarly. It sends the string to a
  9154. specified stream (a file) instead of to the standard output device:  %@NL@%
  9155. %@NL@%
  9156. %@AS@%  fputs( "Example string", fp );%@AE@%%@NL@%
  9157. %@NL@%
  9158. The function %@AB@%fputs%@AE@% takes two parameters: a pointer to the string and the
  9159. %@AB@%FILE%@AE@% pointer. In this and other I/O functions, you refer to the file by name
  9160. only once (when you use %@AB@%fopen%@AE@%). Thereafter, you use its %@AB@%FILE%@AE@% pointer.  %@NL@%
  9161. %@NL@%
  9162. The %@AB@%fputs%@AE@% function writes the entire string to the file but does not include
  9163. the null that marks the end of the string. Nor does it write a newline
  9164. character─unless the string already contains a newline.  %@NL@%
  9165. %@NL@%
  9166. The %@AB@%fputc%@AE@% function writes a character to a file. In the following line, the
  9167. newline character is sent to the file:  %@NL@%
  9168. %@NL@%
  9169. %@AS@%  fputc( '\n', fp );%@AE@%%@NL@%
  9170. %@NL@%
  9171. %@NL@%
  9172. %@4@%%@AB@%Closing the File%@AE@%%@EH@%%@NL@%
  9173. %@NL@%
  9174. When the writing is done, %@AB@%fclose%@AE@% closes the file:  %@NL@%
  9175. %@NL@%
  9176. %@AS@%  fclose( fp );%@AE@%%@NL@%
  9177. %@NL@%
  9178. Conceptually, you can imagine that file I/O functions such as %@AB@%fputs%@AE@% and
  9179. %@AB@%fputc%@AE@% write directly to the disk file. In reality, they're storing strings
  9180. and characters in an intermediate area (called a "memory buffer"). When the
  9181. buffer fills up, the entire chunk of memory is sent to the file. The process
  9182. of emptying the buffer is called "flushing." You may forcibly flush the
  9183. buffer with the %@AB@%fflush%@AE@% and %@AB@%fclose%@AE@% functions. If you do not close the file
  9184. before exiting the program, the buffer is not flushed and you may lose data
  9185. that might remain there.  %@NL@%
  9186. %@NL@%
  9187. The %@AB@%else%@AE@% clause in WRFILE.C should execute only if something has gone wrong
  9188. with the %@AB@%fopen%@AE@% function:  %@NL@%
  9189. %@NL@%
  9190. %@AS@%  else
  9191. %@AS@%     printf( "error message\n" );%@AE@%%@NL@%
  9192. %@NL@%
  9193. This line executes if an error occurs when %@AB@%fopen%@AE@% tries to create the file.
  9194. Handling errors is covered in more detail later in this chapter.  %@NL@%
  9195. %@NL@%
  9196. %@NL@%
  9197. %@3@%%@CR:C6A00110009 @%%@AB@%Reading a Text File in Binary Mode%@AE@%%@EH@%%@NL@%
  9198. %@NL@%
  9199. The WRFILE.C program that created and wrote to a file was fairly simple.
  9200. Here's an equally simple program to read the file just created:  %@NL@%
  9201. %@NL@%
  9202. %@AS@%  /* RDFILE.C: Read a file and print characters to the screen. */
  9203. %@AS@%  
  9204. %@AS@%  #include <stdio.h>
  9205. %@AS@%  
  9206. %@AS@%  main()
  9207. %@AS@%  {
  9208. %@AS@%     int c;
  9209. %@AS@%     FILE *fp;
  9210. %@AS@%  
  9211. %@AS@%     if( fp = fopen( "c:\\testfile.asc", "rb" ) )
  9212. %@AS@%     {
  9213. %@AS@%        while( (c = fgetc( fp )) != EOF )
  9214. %@AS@%           printf( " %c\t%d\n", c, c );
  9215. %@AS@%        printf( "\nEnd of file marker: %d", c );
  9216. %@AS@%        fclose( fp );
  9217. %@AS@%     }
  9218. %@AS@%     else
  9219. %@AS@%        printf( "Error in opening file\n" );
  9220. %@AS@%  }%@AE@%%@NL@%
  9221. %@NL@%
  9222. Although we plan to read the characters as eight-bit entities, the variable %@AS@%
  9223. %@AS@%c %@AE@% should be declared as an %@AB@%int%@AE@% instead of a %@AB@%char%@AE@%. All of the incoming
  9224. characters will be bytes the size of a %@AB@%char%@AE@%, except one.  %@NL@%
  9225. %@NL@%
  9226. When the file has been read from beginning to end, the end-of-file (%@AB@%EOF%@AE@%)
  9227. marker appears on the stream. Within QuickC, an integer value of -1 (0xFFFF)
  9228. represents %@AB@%EOF%@AE@%. To correctly identify this value, the variable %@AS@% c %@AE@% must be
  9229. an integer.  %@NL@%
  9230. %@NL@%
  9231. %@NL@%
  9232. %@4@%%@AB@%Opening a File for Binary Reading%@AE@%%@EH@%%@NL@%
  9233. %@NL@%
  9234. In the line below, the %@AB@%fopen%@AE@% function attempts to open a file. The first
  9235. argument is the file name; the second is the type and mode, both of which
  9236. may be literal strings or pointers to strings:  %@NL@%
  9237. %@NL@%
  9238. %@AS@%  if( fp = fopen( "c:\\testfile.asc", "rb" ) )%@AE@%%@NL@%
  9239. %@NL@%
  9240. The single backslash character used in path specifications must once again
  9241. be represented by two backslashes. The file type is %@AB@%r%@AE@% for read-only. The
  9242. additional %@AB@%b%@AE@% character forces the file to be read in binary mode instead of
  9243. text mode. The differences between binary and text files are discussed later
  9244. in this chapter.  %@NL@%
  9245. %@NL@%
  9246. The %@AB@%fopen%@AE@% function returns a pointer to a %@AB@%FILE%@AE@%. If %@AB@%fopen%@AE@% fails, it returns a
  9247. %@AB@%NULL%@AE@% pointer.  %@NL@%
  9248. %@NL@%
  9249. Finally, the %@AB@%if%@AE@% expression tests for a null value. The original WRFILE.C
  9250. program included the %@AS@% != NULL %@AE@% test for inequality. Within the test
  9251. expression of an %@AB@%if%@AE@% or a %@AB@%while%@AE@%, a 0 value is always false and any other
  9252. value is considered true. In other words, should %@AS@% fp %@AE@% receive a valid
  9253. nonzero address from %@AB@%fopen%@AE@%, the program continues. If something goes wrong,
  9254. the remaining lines don't execute and the program drops through to the %@AB@%else%@AE@%.
  9255. %@NL@%
  9256. %@NL@%
  9257. Note that the expression above uses an assignment operator (%@AB@%=%@AE@%), not an
  9258. equality operator (%@AB@%==%@AE@%). The value returned by %@AB@%fopen%@AE@% is always assigned to %@AS@%
  9259. %@AS@%fp %@AE@%; they aren't being compared to each other. Then the %@AB@%if%@AE@% expression tests
  9260. that value for truth or falsity.  %@NL@%
  9261. %@NL@%
  9262. %@NL@%
  9263. %@4@%%@AB@%Getting a Character%@AE@%%@EH@%%@NL@%
  9264. %@NL@%
  9265. The key to the next line in RDFILE.C is the %@AB@%fgetc%@AE@% function, to which you
  9266. pass a %@AB@%FILE%@AE@% pointer. It returns the next character from the given file:  %@NL@%
  9267. %@NL@%
  9268. %@AS@%  while( (c = fgetc( fp )) != EOF )%@AE@%%@NL@%
  9269. %@NL@%
  9270. The character is assigned to the integer variable %@AS@% c%@AE@%. As long as the
  9271. character doesn't equal %@AB@%EOF%@AE@%, the %@AB@%while%@AE@% loop continues.  %@NL@%
  9272. %@NL@%
  9273. The end-of-file marker equals -1, but it's preferable to use the symbolic
  9274. constant %@AB@%EOF%@AE@%. If the program is transported to another computer, you might
  9275. find %@AB@%EOF%@AE@% has another value. Using the symbolic constant allows you to
  9276. maintain compatibility between computers and operating systems.  %@NL@%
  9277. %@NL@%
  9278. For the same reason, it's preferable to test for %@AB@%NULL%@AE@% instead of assuming
  9279. that %@AB@%NULL%@AE@% will always equal 0.  %@NL@%
  9280. %@NL@%
  9281. %@NL@%
  9282. %@4@%%@AB@%Viewing the File%@AE@%%@EH@%%@NL@%
  9283. %@NL@%
  9284. Since there's only one line inside the %@AB@%while%@AE@% loop, it's not necessary to
  9285. enclose it in curly braces. The variable %@AS@% c %@AE@% contains the character read
  9286. from the file. It then can be printed:  %@NL@%
  9287. %@NL@%
  9288. %@AS@%  printf( " %c\t%d\n", c, c );%@AE@%%@NL@%
  9289. %@NL@%
  9290. The characters from the file print twice, once as a character (%@AB@%%c%@AE@%) and once
  9291. as a decimal number (%@AB@%%d%@AE@%), separated by a tab stop. This %@AB@%printf%@AE@% statement
  9292. repeats until %@AB@%fgetc%@AE@% (inside the %@AB@%while%@AE@% loop) finds no more characters in the
  9293. file.  %@NL@%
  9294. %@NL@%
  9295. %@NL@%
  9296. %@3@%%@CR:C6A00110010 @%%@AB@%Binary and Text Files%@AE@%%@EH@%%@NL@%
  9297. %@NL@%
  9298. Normally, you wouldn't write a file in text mode and then read it in binary
  9299. mode. As a general rule, you pick whichever mode is more appropriate (text
  9300. mode for text or binary mode for data) and stick with it.  %@NL@%
  9301. %@NL@%
  9302. A somewhat baffling thing happened in the example above, however. The
  9303. WRFILE.C program wrote %@AS@%"Example string" %@AE@% to a disk file and then added a
  9304. newline character. That should be a total of 15 characters. But if you
  9305. examine the directory, you'll see the file uses 16 bytes.  %@NL@%
  9306. %@NL@%
  9307. Where did the extra byte come from?  %@NL@%
  9308. %@NL@%
  9309. %@NL@%
  9310. %@4@%%@AB@%Testing Text Mode%@AE@%%@EH@%%@NL@%
  9311. %@NL@%
  9312. If you ran the RDFILE.C program, you probably noticed two characters
  9313. followed the line: a carriage return (ASCII 13) and a linefeed (ASCII 10).
  9314. If you make the following change to the program, the output of RDFILE.C is
  9315. different:  %@NL@%
  9316. %@NL@%
  9317. %@AS@%  if( (fp = fopen( "c:\\testfile.asc","rt" )) != NULL )%@AE@%%@NL@%
  9318. %@NL@%
  9319. The only modification is that the second string is %@AS@%"rt" %@AE@% instead of %@AS@%"rb"%@AE@%.
  9320. The %@AB@%t%@AE@% represents text mode; the %@AB@%b%@AE@% is binary mode. If you don't specify a
  9321. mode, the %@AB@%fopen%@AE@% function defaults to text mode.  %@NL@%
  9322. %@NL@%
  9323. The list below shows the output of the two programs.  %@NL@%
  9324. %@NL@%
  9325. %@TH:  20  1313 02 33 43 @%RDFILE.C  (binary mode)          RDFILE.C  (text mode)%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AS@%E69%@AE@%                              %@AS@%E69%@AE@%%@AS@%x120%@AE@%                             %@AS@%x120%@AE@%%@AS@%a97%@AE@%                              %@AS@%a97%@AE@%%@AS@%m109%@AE@%                             %@AS@%m109%@AE@%%@AS@%p112%@AE@%                             %@AS@%p112%@AE@%%@AS@%l108%@AE@%                             %@AS@%l108%@AE@%%@AS@%e101%@AE@%                             %@AS@%e101%@AE@%%@AS@%32%@AE@%                               %@AS@%32%@AE@%%@AS@%s115%@AE@%                             %@AS@%s115%@AE@%%@AS@%t116%@AE@%                             %@AS@%t116%@AE@%%@AS@%r114%@AE@%                             %@AS@%r114%@AE@%%@AS@%i105%@AE@%                             %@AS@%i105%@AE@%%@AS@%n110%@AE@%                             %@AS@%n110%@AE@%%@AS@%g103%@AE@%                             %@AS@%g103%@AE@%%@AS@%13 %@AE@%                              %@AS@%10%@AE@%%@AS@%10 %@AE@%                              %@AS@%End-of-file marker: -1%@AE@%%@AS@%End-of-file marker: -1%@AE@% %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  20  1313 02 33 43 @%
  9326.  
  9327. In binary mode there seems to be two characters after the string. In text
  9328. mode there's only one.  %@NL@%
  9329. %@NL@%
  9330. %@NL@%
  9331. %@4@%%@AB@%Ends-of-Lines, Ends-of-Files%@AE@%%@EH@%%@NL@%
  9332. %@NL@%
  9333. The two modes─binary and text─treat end-of-line (%@AB@%EOL%@AE@%) characters and
  9334. end-of-file (%@AB@%EOF%@AE@%) characters in different ways.  %@NL@%
  9335. %@NL@%
  9336. In DOS, a line of text ends with a carriage return (CR) and a linefeed (LF),
  9337. which appear above as ASCII 13 plus ASCII 10. In the UNIX operating system,
  9338. which has close ties to the C language, a single ASCII 10 (the newline
  9339. character) marks the end of a line.  %@NL@%
  9340. %@NL@%
  9341. The once-popular CP/M operating system signals the end of files with a
  9342. CTRL+Z character (ASCII 26, 0x1A)─a tradition that carried forward to DOS.
  9343. This is not the case with UNIX (and C), which don't use a unique %@AB@%EOF%@AE@%
  9344. character.  %@NL@%
  9345. %@NL@%
  9346. %@NL@%
  9347. %@4@%%@AB@%Text Mode Translations%@AE@%%@EH@%%@NL@%
  9348. %@NL@%
  9349. It's important to understand the differences between text mode and binary
  9350. mode when writing and reading disk files. No translations are made in binary
  9351. mode. In text mode, however, the end-of-line and end-of-file characters are
  9352. translated.  %@NL@%
  9353. %@NL@%
  9354. When you read a file in text mode and a CR-LF combination appears in the
  9355. stream, the two characters are translated to one newline character. The
  9356. opposite translation occurs when you write a file in text mode: each CR-LF
  9357. combination is translated to one newline character. In other words, the
  9358. newline is represented by two characters on disk and one character in
  9359. memory. These translations do not occur when you read and write a file in
  9360. binary mode.  %@NL@%
  9361. %@NL@%
  9362. When you read a file in text mode and a CTRL+Z (0x1A) character appears in
  9363. the stream, the character is interpreted as the end-of-file character.
  9364. However, when you're in text mode and you close a file to which you've been
  9365. writing, a CTRL+Z is not placed in the file as the last character. In binary
  9366. mode, the CTRL+Z character has no special meaning (it is not interpreted as
  9367. the end-of-file character).  %@NL@%
  9368. %@NL@%
  9369. The difference between text mode and binary mode is relatively minor when
  9370. you're handling strings, but it's important when you're writing numeric
  9371. values to disk files.  %@NL@%
  9372. %@NL@%
  9373. %@NL@%
  9374. %@3@%%@CR:C6A00110011 @%%@AB@%Text Format for Numeric Variables%@AE@%%@EH@%%@NL@%
  9375. %@NL@%
  9376. Many programs, of course, use numeric as well as character data. When you
  9377. wish to save numbers, you have two choices: text mode or binary mode. The
  9378. SVTEXT.C program below illustrates the less desirable way of creating files
  9379. for numeric variables.  %@NL@%
  9380. %@NL@%
  9381. %@AS@%  /* SVTEXT.C: Save integer variables as text. */
  9382. %@AS@%  
  9383. %@AS@%  #include <stdio.h>
  9384. %@AS@%  
  9385. %@AS@%  int list[] = { 53, -23456, 50, 500, 5000, -99 };
  9386. %@AS@%  extern int errno;
  9387. %@AS@%  char fname[] = "numtext";
  9388. %@AS@%  char temp[81];
  9389. %@AS@%  
  9390. %@AS@%  main()
  9391. %@AS@%  {
  9392. %@AS@%     FILE *fptr;
  9393. %@AS@%     int i;
  9394. %@AS@%  
  9395. %@AS@%     if( (fptr = fopen( "numtext","wt" )) != NULL )
  9396. %@AS@%     {
  9397. %@AS@%        for( i=0; i<6; i++ )
  9398. %@AS@%           fprintf( fptr, "Item %d: %6d \n", i, list[i] );
  9399. %@AS@%        fclose( fptr );
  9400. %@AS@%     }
  9401. %@AS@%     else
  9402. %@AS@%        printf( "Error: Couldn't create file.\n" );
  9403. %@AS@%  
  9404. %@AS@%     if( (fptr = fopen( "badname", "rt" )) != NULL )
  9405. %@AS@%     {
  9406. %@AS@%        /* do nothing */
  9407. %@AS@%     }
  9408. %@AS@%     else
  9409. %@AS@%     {
  9410. %@AS@%        printf( "Error number: %d\n\t", errno );
  9411. %@AS@%        perror( "Couldn't open file BADNAME\n\t" );
  9412. %@AS@%     }
  9413. %@AS@%  
  9414. %@AS@%     if( (fptr = fopen( fname, "rt" )) != NULL )
  9415. %@AS@%     {
  9416. %@AS@%        list[0] = 0;
  9417. %@AS@%        fscanf( fptr, "Item %d: %d \n", &i, &list[0] );
  9418. %@AS@%        printf( "Values read from file:\t %d %d\n", i, list[0] );
  9419. %@AS@%        fgets( temp, 80, fptr );
  9420. %@AS@%        printf( "String from file: \t%s\n", temp );
  9421. %@AS@%        while( (i = fgetc( fptr )) != '\n' )
  9422. %@AS@%           printf( "char: %c \t ASCII: %d \n", i, i );
  9423. %@AS@%        rewind( fptr );
  9424. %@AS@%        printf( "Rewind to start -->\t%s", fgets( temp, 80, fptr ) );
  9425. %@AS@%        fclose( fptr );
  9426. %@AS@%     }
  9427. %@AS@%     else      printf( "Trouble opening %s \n", fname );
  9428. %@AS@%  }%@AE@%%@NL@%
  9429. %@NL@%
  9430. The SVTEXT.C program does three things:  %@NL@%
  9431. %@NL@%
  9432. %@NL@%
  9433.   1.  First, it creates a text file called NUMTEXT. If you TYPE NUMTEXT from
  9434.       the DOS prompt or load the NUMTEXT file into a word processor, it
  9435.       looks like this:
  9436. %@NL@%
  9437. %@AS@%      Item 0:     53 
  9438. %@AS@%      Item 1: -23456 
  9439. %@AS@%      Item 2:     50 
  9440. %@AS@%      Item 3:    500 
  9441. %@AS@%      Item 4:   5000 
  9442. %@AS@%      Item 5:    -99%@AE@%%@NL@%
  9443. %@NL@%
  9444. %@NL@%
  9445.   2.  Next, SVTEXT.C deliberately attempts to open a nonexistent file called
  9446.       BADNAME, to cause a disk error. This section serves no purpose except
  9447.       to illustrate error handling.%@NL@%
  9448. %@NL@%
  9449.   3.  Finally, it reads parts of NUMTEXT, using several file-input
  9450.       functions.%@NL@%
  9451. %@NL@%
  9452. %@NL@%
  9453. %@NL@%
  9454. %@4@%%@AB@%Opening the File for Writing%@AE@%%@EH@%%@NL@%
  9455. %@NL@%
  9456. By now, the %@AB@%fopen%@AE@% function should look familiar to you. The only change in
  9457. the block below is the %@AS@%"wt" %@AE@% mode. The %@AB@%fopen%@AE@% function returns a %@AB@%NULL%@AE@% if any
  9458. errors occur, so the block after the %@AB@%if%@AE@% should execute if %@AB@%fopen%@AE@% succeeds.  %@NL@%
  9459. %@NL@%
  9460. %@AS@%  if( (fptr = fopen( "numtext","wt" )) != NULL )
  9461. %@AS@%  {
  9462. %@AS@%     for( i=0; i<6; i++ )
  9463. %@AS@%        fprintf( fptr, "Item %d: %6d \n", i, list[i] );
  9464. %@AS@%     fclose( fptr );
  9465. %@AS@%  }
  9466. %@AS@%  else
  9467. %@AS@%     printf( "Error: Couldn't create file.\n" );%@AE@%%@NL@%
  9468. %@NL@%
  9469. The %@AB@%for%@AE@% loop counts from 0 to 5, printing 6 strings to the file. The %@AB@%fprintf%@AE@%
  9470. function works the same as %@AB@%printf%@AE@% with one change. You must place the %@AB@%FILE%@AE@%
  9471. pointer before the format string.  %@NL@%
  9472. %@NL@%
  9473. %@NL@%
  9474. %@4@%%@AB@%Error Handling%@AE@%%@EH@%%@NL@%
  9475. %@NL@%
  9476. To illustrate what happens when something goes wrong, the next line creates
  9477. a disk error (as long as you don't have a file called BADNAME in your
  9478. working directory).  %@NL@%
  9479. %@NL@%
  9480. %@AS@%  if( (fptr = fopen( "badname", "rt" )) != NULL )%@AE@%%@NL@%
  9481. %@NL@%
  9482. The %@AB@%if%@AE@% block is empty, because we expect the program to drop through to the
  9483. %@AB@%else%@AE@% clause that handles errors:  %@NL@%
  9484. %@NL@%
  9485. %@AS@%  else
  9486. %@AS@%  {
  9487. %@AS@%     printf( "Error number: %d\n\t", errno );
  9488. %@AS@%     perror( "Couldn't open file BADNAME\n\t" );
  9489. %@AS@%  }%@AE@%%@NL@%
  9490. %@NL@%
  9491. The %@AB@%else%@AE@% block shows two ways you can deal with errors. Note that the %@AB@%errno%@AE@%
  9492. variable, which was declared as an external integer, has never been assigned
  9493. a value. QuickC automatically puts error numbers into %@AB@%errno%@AE@%. In this
  9494. program, the error number is printed to the screen. In your own programs,
  9495. you might wish to branch to various error-handling routines, based on the
  9496. value in the system variable %@AB@%errno%@AE@%. For a list of values for %@AB@%errno%@AE@%, see the
  9497. individual online help entries for file-handling functions.  %@NL@%
  9498. %@NL@%
  9499. It's important to remember that the standard output device is the screen and
  9500. that %@AB@%printf%@AE@% sends messages to %@AB@%stdout%@AE@%. However, if you redirect output to a
  9501. disk file, using a command line such as %@AS@% SVTEXT > MYFILE%@AE@%, the %@AB@%printf%@AE@%
  9502. statement prints the error message to MYFILE. In most cases, you'd prefer to
  9503. see the error message on the screen.  %@NL@%
  9504. %@NL@%
  9505. The second, and better, way to handle I/O errors is the %@AB@%perror%@AE@% function,
  9506. which prints two strings: one that you pass to it and one that spells out─in
  9507. English─the error message. This message goes to the standard error stream
  9508. (%@AB@%stderr%@AE@%), which is always the screen, regardless of whether you've
  9509. redirected output or not. For this reason, %@AB@%perror%@AE@% is preferable to %@AB@%printf%@AE@%
  9510. for printing error messages.  %@NL@%
  9511. %@NL@%
  9512. The error messages should look like this on your screen:  %@NL@%
  9513. %@NL@%
  9514. %@AS@%  Error number: 2
  9515. %@AS@%     Couldn't open file BADNAME
  9516. %@AS@%     : No such file or directory%@AE@%%@NL@%
  9517. %@NL@%
  9518. %@NL@%
  9519. %@4@%%@AB@%Reading Text with fscanf%@AE@%%@EH@%%@NL@%
  9520. %@NL@%
  9521. The final %@AB@%fopen%@AE@% in SVTEXT.C opens the file created earlier:  %@NL@%
  9522. %@NL@%
  9523. %@AS@%  if( (fptr = fopen( fname, "rt" )) != NULL )%@AE@%%@NL@%
  9524. %@NL@%
  9525. Note that we passed the name of a string rather than a literal string.  %@NL@%
  9526. %@NL@%
  9527. Below, %@AB@%fscanf%@AE@% reads in two numeric variables from the first string in the
  9528. file. Note that it works the same as %@AB@%scanf%@AE@%, but you add the %@AB@%FILE%@AE@% pointer as
  9529. the first argument:  %@NL@%
  9530. %@NL@%
  9531. %@AS@%  fscanf( fptr, "Item %d: %d \n", &i, &list[0] );
  9532. %@AS@%  printf( "Values read from file:\t %d %d\n", i, list[0] );%@AE@%%@NL@%
  9533. %@NL@%
  9534. %@NL@%
  9535. %@4@%%@AB@%Reading Text with fgets and fgetc%@AE@%%@EH@%%@NL@%
  9536. %@NL@%
  9537. At this point, the first line in the file has been read and converted to two
  9538. integer values. The file is straight text, so you can treat the second line
  9539. as a string:  %@NL@%
  9540. %@NL@%
  9541. %@AS@%  fgets( temp, 80, fptr );
  9542. %@AS@%  printf( "String from file: \t%s\n", temp );%@AE@%%@NL@%
  9543. %@NL@%
  9544. The %@AB@%fgets%@AE@% function requires three arguments: a pointer to a string, the
  9545. maximum number of characters to read, and the %@AB@%FILE%@AE@% pointer. The function
  9546. stops reading characters when it encounters a newline character or when it
  9547. reaches the maximum number of characters or the end of the file.  %@NL@%
  9548. %@NL@%
  9549. If you prefer, you can input the characters one by one:  %@NL@%
  9550. %@NL@%
  9551. %@AS@%  while( (i = fgetc( fptr )) != '\n' )
  9552. %@AS@%     printf( "char: %c \t ASCII: %d \n", i, i );%@AE@%%@NL@%
  9553. %@NL@%
  9554. The %@AB@%printf%@AE@% inside the %@AB@%while%@AE@% loop prints each character as a character (%@AB@%%c%@AE@%)
  9555. and also as a decimal value (%@AB@%%d%@AE@%). The %@AB@%while%@AE@% loop continues reading
  9556. characters until it finds the end of the line.  %@NL@%
  9557. %@NL@%
  9558. %@NL@%
  9559. %@4@%%@AB@%Back to the Beginning%@AE@%%@EH@%%@NL@%
  9560. %@NL@%
  9561. The %@AB@%rewind%@AE@% function resets the position pointer to the beginning of the
  9562. file. In the program line below, the first line from the file is printed:  %@NL@%
  9563. %@NL@%
  9564. %@AS@%  rewind( fptr );
  9565. %@AS@%  printf( "Rewind to start -->\t%s", fgets( temp, 80, fptr ) );%@AE@%%@NL@%
  9566. %@NL@%
  9567. The screen output looks like this:  %@NL@%
  9568. %@NL@%
  9569. %@AS@%  Error number: 2
  9570. %@AS@%          Couldn't open file BADNAME
  9571. %@AS@%          : No such file or directory
  9572. %@AS@%  Values read from file:         0 53
  9573. %@AS@%  String from file:         Item 1: -23456 
  9574. %@AS@%  
  9575. %@AS@%  char: I          ASCII: 73 
  9576. %@AS@%  char: t          ASCII: 116 
  9577. %@AS@%  char: e          ASCII: 101 
  9578. %@AS@%  char: m          ASCII: 109 
  9579. %@AS@%  char:            ASCII: 32 
  9580. %@AS@%  char: 2          ASCII: 50 
  9581. %@AS@%  char: :          ASCII: 58 
  9582. %@AS@%  char:            ASCII: 32 
  9583. %@AS@%  char:            ASCII: 32 
  9584. %@AS@%  char:            ASCII: 32 
  9585. %@AS@%  char:            ASCII: 32 
  9586. %@AS@%  char:            ASCII: 32 
  9587. %@AS@%  char: 5          ASCII: 53 
  9588. %@AS@%  char: 0          ASCII: 48 
  9589. %@AS@%  char:            ASCII: 32 
  9590. %@AS@%  Rewind to start -->        Item 0:     53%@AE@%%@NL@%
  9591. %@NL@%
  9592. %@AU@% It is inefficient to store  numeric data in text format.%@AE@%  %@NL@%
  9593. %@NL@%
  9594. There seem to be quite a few white-space characters in the text file. Text
  9595. files are great for text, but they store numeric values in a wasteful way.
  9596. Binary format offers several advantages.  %@NL@%
  9597. %@NL@%
  9598. %@NL@%
  9599. %@3@%%@CR:C6A00110012 @%%@AB@%Using Binary Format%@AE@%%@EH@%%@NL@%
  9600. %@NL@%
  9601. When you're processing strings of ASCII characters and writing them to disk
  9602. files, it matters little whether you use text mode or binary mode, as long
  9603. as you're consistent. The advantage of text mode is that it translates
  9604. newlines to the carriage-return-line-feed combination, making it possible to
  9605. use the DOS TYPE command to view the file.  %@NL@%
  9606. %@NL@%
  9607. When you're processing numeric values (integers and floating-point numbers),
  9608. however, you may wish to save your variables in binary mode files, in binary
  9609. format, for the following reasons:  %@NL@%
  9610. %@NL@%
  9611. %@NL@%
  9612.   ■   Binary format almost always saves disk space. In text mode, the number
  9613.       12345.678 would require eight bytes for the ASCII numerals, one byte
  9614.       for the decimal point, and one or more bytes for a separator between
  9615.       variables. In binary format, a floating-point number uses four bytes,
  9616.       regardless of its value. Short integers use only two bytes.%@NL@%
  9617. %@NL@%
  9618.   ■   Binary format generally saves computer time. When you use %@AB@%fprintf%@AE@% to
  9619.       print a numeric value to disk, the computer must translate the
  9620.       internal binary representation to a series of characters. Likewise,
  9621.       when %@AB@%fscanf%@AE@% reads characters into memory, the ASCII values must be
  9622.       translated to the internal binary format. In binary format, none of
  9623.       these translations takes place.%@NL@%
  9624. %@NL@%
  9625.   ■   Binary format preserves the precision of floating-point numbers. The
  9626.       translation from binary to decimal ASCII and back to binary affects
  9627.       the precision of the value.%@NL@%
  9628. %@NL@%
  9629.   ■   A binary save of arrays or structures is fast. It's not necessary to
  9630.       read through an array of 100 items and print each one to the disk
  9631.       file. Instead, you call the %@AB@%fwrite%@AE@% function (discussed below) once,
  9632.       passing it the size of the array to be saved.%@NL@%
  9633. %@NL@%
  9634. %@STUB@%      %@AI@%NOTE  %@AE@%Binary mode is separate from binary format. The modes (binary
  9635.       and text) are parameters you pass to the %@AB@%fopen%@AE@% function. They affect
  9636.       the translation of newlines and the placing of %@AI@%EOF%@AE@% markers. The
  9637.       formats (binary and text) are ways of representing numeric values. An
  9638.       integer in binary format always occupies two bytes on disk. An integer
  9639.       in text format uses a variable number of bytes: it might contain one
  9640.       character (5) or six (-10186).%@NL@%
  9641. %@NL@%
  9642. %@NL@%
  9643. %@NL@%
  9644. %@4@%%@AB@%Opening a Binary File%@AE@%%@EH@%%@NL@%
  9645. %@NL@%
  9646. The SVBIN.C program below creates two binary mode files with the variables
  9647. saved in binary format:  %@NL@%
  9648. %@NL@%
  9649. %@AS@%  /* SVBIN.C: Save integer variables in binary format. */
  9650. %@AS@%  
  9651. %@AS@%  #include <stdio.h>
  9652. %@AS@%  #define ASIZE 10
  9653. %@AS@%  
  9654. %@AS@%  main()
  9655. %@AS@%  {
  9656. %@AS@%     FILE *ap;
  9657. %@AS@%     int zebra[ASIZE], acopy[ASIZE], bcopy[ASIZE];
  9658. %@AS@%     int i;
  9659. %@AS@%  
  9660. %@AS@%     for( i = 0; i < ASIZE; i++ )
  9661. %@AS@%        zebra[i] = 7700 + i;
  9662. %@AS@%  
  9663. %@AS@%     if( (ap = fopen( "binfile", "wb" )) != NULL )
  9664. %@AS@%     {
  9665. %@AS@%        fwrite( zebra, sizeof(zebra), 1, ap );
  9666. %@AS@%        fclose( ap );
  9667. %@AS@%     }
  9668. %@AS@%     else
  9669. %@AS@%        perror( "Write error" );
  9670. %@AS@%  
  9671. %@AS@%     if( (ap = fopen( "morebin", "wb" )) != NULL )
  9672. %@AS@%     {
  9673. %@AS@%        fwrite( &zebra[0], sizeof(zebra[0]), ASIZE, ap );
  9674. %@AS@%        fclose( ap );
  9675. %@AS@%     }
  9676. %@AS@%     else
  9677. %@AS@%        perror( "Write error" );
  9678. %@AS@%  
  9679. %@AS@%     if( (ap = fopen( "binfile", "rb" )) != NULL )
  9680. %@AS@%     {
  9681. %@AS@%        printf( "Hexadecimal values in binfile:\n" );
  9682. %@AS@%        while( (i = fgetc( ap )) != EOF )
  9683. %@AS@%           printf( "%02X ", i );
  9684. %@AS@%        rewind( ap );
  9685. %@AS@%        fread( acopy, sizeof(acopy), 1, ap );
  9686. %@AS@%        rewind( ap );
  9687. %@AS@%        fread( &bcopy[0], sizeof( bcopy[0] ), ASIZE, ap);
  9688. %@AS@%        for( i=0; i<ASIZE; i++ )
  9689. %@AS@%           printf( "\nItem %d = %d\t%d", i, acopy[i], bcopy[i] );
  9690. %@AS@%        fclose( ap );
  9691. %@AS@%  
  9692. %@AS@%     }
  9693. %@AS@%     else
  9694. %@AS@%        perror( "Read error" );
  9695. %@AS@%  
  9696. %@AS@%  }%@AE@%%@NL@%
  9697. %@NL@%
  9698. Focus your attention on the %@AS@% zebra %@AE@% array. It contains 10 integers, because
  9699. the array size %@AS@% ASIZE %@AE@% was defined as 10. First, some values are stored in %@AS@%
  9700. %@AS@%zebra %@AE@% (in a moment, we'll see why 7700-7709 are significant):  %@NL@%
  9701. %@NL@%
  9702. %@AS@%  for( i = 0; i < ASIZE; i++ )
  9703. %@AS@%     zebra[i] = 7700 + i;%@AE@%%@NL@%
  9704. %@NL@%
  9705. Next, we open a file and use %@AB@%fwrite%@AE@% to write the entire array to disk:  %@NL@%
  9706. %@NL@%
  9707. %@AS@%  if( (ap = fopen( "binfile", "wb" )) != NULL )
  9708. %@AS@%  {
  9709. %@AS@%     fwrite( zebra, sizeof(zebra), 1, ap );
  9710. %@AS@%     fclose( ap );
  9711. %@AS@%  }%@AE@%%@NL@%
  9712. %@NL@%
  9713. %@NL@%
  9714. %@4@%%@AB@%Writing an Array in One Line%@AE@%%@EH@%%@NL@%
  9715. %@NL@%
  9716. The %@AB@%fwrite%@AE@% function requires four pieces of information:  %@NL@%
  9717. %@NL@%
  9718. %@NL@%
  9719.   1.  The address of the item (a variable, array, or structure)%@NL@%
  9720. %@NL@%
  9721.   2.  The size of the item in bytes%@NL@%
  9722. %@NL@%
  9723.   3.  The number of items to be written%@NL@%
  9724. %@NL@%
  9725.   4.  The %@AB@%FILE%@AE@% pointer for a previously opened binary mode file%@NL@%
  9726. %@NL@%
  9727. %@NL@%
  9728. In this example, the first argument, %@AS@% zebra %@AE@% is an array and, as you may
  9729. remember from Chapter 8, "Pointers," the name of an array is the address of
  9730. the array.  %@NL@%
  9731. %@NL@%
  9732. To provide the second argument for %@AB@%fwrite%@AE@%, SVBIN.C uses the %@AB@%sizeof%@AE@% operator,
  9733. which returns the number of bytes a variable requires. Because %@AS@% zebra %@AE@% is an
  9734. array of 10 integers and integers use 2 bytes each, the size of %@AS@% zebra %@AE@%
  9735. should be 20. If you view a directory of your disk after running this
  9736. program, you'll notice that the file BINFILE is exactly 20 bytes long.  %@NL@%
  9737. %@NL@%
  9738. The third argument tells %@AB@%fwrite%@AE@% how many items to write to the file. We have
  9739. 1 array, so this parameter is 1.  %@NL@%
  9740. %@NL@%
  9741. The fourth argument is the %@AB@%FILE%@AE@% pointer returned by %@AB@%fopen%@AE@%.  %@NL@%
  9742. %@NL@%
  9743. There's another way to copy the 20 bytes of %@AS@% zebra %@AE@% to the file. After
  9744. writing to BINFILE, the program uses the %@AB@%fopen%@AE@% function to create a second
  9745. file called MOREBIN. The following %@AB@%fwrite%@AE@% line writes 10 integers instead of
  9746. 1 array:  %@NL@%
  9747. %@NL@%
  9748. %@AS@%  fwrite( &zebra[0], sizeof(zebra[0]), ASIZE, ap );%@AE@%%@NL@%
  9749. %@NL@%
  9750. The second and third arguments have changed. Instead of passing the size of
  9751. the array (20) and writing 1 copy of the array, we're accessing the size of
  9752. 1 element (2 bytes) and writing 10 of them (using the symbolic constant
  9753. %@AB@%ASIZE%@AE@%). The contents of this disk file should match, byte for byte, the
  9754. contents of BINFILE.  %@NL@%
  9755. %@NL@%
  9756. %@NL@%
  9757. %@4@%%@AB@%Examining the Binary Contents%@AE@%%@EH@%%@NL@%
  9758. %@NL@%
  9759. Finally, we look at what's inside the file BINFILE. It is opened for reading
  9760. as a binary file:  %@NL@%
  9761. %@NL@%
  9762. %@AS@%  if( (ap = fopen( "binfile", "rb" )) != NULL )%@AE@%%@NL@%
  9763. %@NL@%
  9764. A short %@AB@%while%@AE@% loop reads the bytes from BINFILE and displays them in
  9765. hexadecimal notation:  %@NL@%
  9766. %@NL@%
  9767. %@AS@%  printf( "Hexadecimal values in binfile:\n" );
  9768. %@AS@%  while( (i = fgetc( ap )) != EOF )
  9769. %@AS@%     printf( "%02X ", i );%@AE@%%@NL@%
  9770. %@NL@%
  9771. After running SVBIN.C, the screen displays these values:  %@NL@%
  9772. %@NL@%
  9773. %@AS@%  14 1E 15 1E 16 1E 17 1E 18 1E
  9774. %@AS@%  19 1E 1A 1E 1B 1E 1C 1E 1D 1E%@AE@%%@NL@%
  9775. %@NL@%
  9776. The low byte precedes the high byte, so the first two bytes represent the
  9777. number 0x1E14, which is 7700 in decimal. The next two bytes equal 7701, and
  9778. so on.  %@NL@%
  9779. %@NL@%
  9780. A curious thing happens when you run SVBIN.C and then try to treat the
  9781. 20-byte file as text. If you TYPE BINFILE from the DOS command line, the
  9782. file appears as gibberish (of course), and you see only 12 of the 20
  9783. characters on the screen. Where did the other characters go? Recall the
  9784. previous discussion of binary and text files. In DOS, a CTRL+Z (0x1A) marks
  9785. the end of a text file. And in the midst of our binary file is one of those
  9786. %@AB@%EOF%@AE@% characters. It's not acting as an %@AB@%EOF%@AE@%; it's part of the number 0x1E1A.
  9787. But if you ever open this file in text mode, you'll be unable to read past
  9788. the twelfth byte.  %@NL@%
  9789. %@NL@%
  9790. %@NL@%
  9791. %@4@%%@AB@%Retrieving the Values from Disk%@AE@%%@EH@%%@NL@%
  9792. %@NL@%
  9793. Most of the time, you won't want to read a binary file one byte at a time.
  9794. Instead, you call %@AB@%fread%@AE@%, which reads a disk file and stores the values in a
  9795. variable, an array, or a structure. The %@AB@%fread%@AE@% function complements %@AB@%fwrite%@AE@%.
  9796. It takes four parameters:  %@NL@%
  9797. %@NL@%
  9798. %@NL@%
  9799.   1.  The address of the variable%@NL@%
  9800. %@NL@%
  9801.   2.  The size of the variable in bytes%@NL@%
  9802. %@NL@%
  9803.   3.  The number of values to read%@NL@%
  9804. %@NL@%
  9805.   4.  The %@AB@%FILE%@AE@% pointer that references a binary file opened for reading%@NL@%
  9806. %@NL@%
  9807. %@NL@%
  9808. Here's one way to read values into an array:  %@NL@%
  9809. %@NL@%
  9810. %@AS@%  rewind( ap );
  9811. %@AS@%  fread( acopy, sizeof( acopy ), 1, ap );%@AE@%%@NL@%
  9812. %@NL@%
  9813. The %@AB@%rewind%@AE@% command is necessary because we've already read through the file
  9814. once. The %@AS@% acopy %@AE@% and %@AS@% bcopy %@AE@% arrays are the same size as our original %@AS@%
  9815. %@AS@%zebra %@AE@%array. To fill an array with this technique, pass the address, the
  9816. size of the entire array, a number 1, and the %@AB@%FILE%@AE@% pointer.  %@NL@%
  9817. %@NL@%
  9818. A second way to fill an array is to pass the size of a single element and
  9819. the number of elements you want to read:  %@NL@%
  9820. %@NL@%
  9821. %@AS@%  rewind( ap );
  9822. %@AS@%  fread( &bcopy[0], sizeof( bcopy[0] ), ASIZE, ap );%@AE@%%@NL@%
  9823. %@NL@%
  9824. In the first example of %@AB@%fread%@AE@%, we pass the information that the array %@AS@% acopy
  9825. %@AS@%%@AE@% is 20 bytes long and we want to read it once. In the second example, we
  9826. pass the size of an integer (2 bytes) and ask for 10 of them. In either
  9827. case, 20 bytes are transferred.  %@NL@%
  9828. %@NL@%
  9829. Just to make sure both arrays are equal, we can print them out:  %@NL@%
  9830. %@NL@%
  9831. %@AS@%  for( i = 0; i < ASIZE; i++ )
  9832. %@AS@%  printf( "\nItem %d = %d\t%d", i, acopy[i], bcopy[i] );
  9833. %@AS@%  fclose( ap );%@AE@%%@NL@%
  9834. %@NL@%
  9835. The screen displays the values 7700 through 7709, which survived the trek
  9836. from %@AS@% zebra %@AE@% to BINFILE and back again. These values were stored in the %@AS@%
  9837. %@AS@%zebra %@AE@% array, written to a binary file, then read back into the %@AS@% acopy %@AE@% and %@AS@%
  9838. %@AS@%bcopy %@AE@% arrays.  %@NL@%
  9839. %@NL@%
  9840. %@NL@%
  9841. %@2@%%@CR:C6A00110013 @%%@AB@%Low-Level Input and Output%@AE@%%@EH@%%@NL@%
  9842. %@NL@%
  9843. The file-handling routines such as %@AB@%fopen%@AE@%, %@AB@%fprintf%@AE@%, and %@AB@%fclose%@AE@% are called
  9844. "standard" because they're defined in the ANSI standard. Code that uses the
  9845. standard routines will generally be portable from one machine to another.  %@NL@%
  9846. %@NL@%
  9847. In addition to the standard file-handling functions, the QuickC library
  9848. includes some low-level I/O functions, which allow more direct access to
  9849. disk files.  %@NL@%
  9850. %@NL@%
  9851. The low-level I/O routines (also called "system-level") are generally not
  9852. portable. They work in DOS and OS/2, but they may not work elsewhere.
  9853. They're also a little more difficult to use. Instead of declaring a pointer
  9854. to a %@AB@%FILE%@AE@% structure, you must allocate your own buffer and manage transfer
  9855. of the bytes yourself. You move values into the buffer, then send the
  9856. contents of the buffer to disk.  %@NL@%
  9857. %@NL@%
  9858. %@AU@% Low-level routines can be  more efficient, but they  usually aren't
  9859. %@AU@%portable.%@AE@%  %@NL@%
  9860. %@NL@%
  9861. Low-level routines have some advantages, though. One is that you have more
  9862. control over the machine. Another is that low-level I/O can be faster than
  9863. standard I/O, if you know what you're doing. The choice is up to you:
  9864. portability versus efficiency. You should choose one or the other; it's not
  9865. a good idea to mix standard and system-level routines.  %@NL@%
  9866. %@NL@%
  9867. %@NL@%
  9868. %@3@%%@CR:C6A00110014 @%%@AB@%Low-Level Reading and Writing%@AE@%%@EH@%%@NL@%
  9869. %@NL@%
  9870. The program RWFILE.C illustrates some of the low-level, file-handling
  9871. commands. It creates a file, writes to it, and closes it. Then the file is
  9872. opened for reading and the contents of the file are displayed on the screen.
  9873. %@NL@%
  9874. %@NL@%
  9875. %@AS@%  /* RWFILE.C: Read and write a file. */
  9876. %@AS@%  
  9877. %@AS@%  #include <stdio.h>
  9878. %@AS@%  #include <string.h>
  9879. %@AS@%  #include <fcntl.h>
  9880. %@AS@%  #include <sys\types.h>
  9881. %@AS@%  #include <sys\stat.h>
  9882. %@AS@%  #include <io.h>
  9883. %@AS@%  
  9884. %@AS@%  #define BUFF 512
  9885. %@AS@%  
  9886. %@AS@%  main()
  9887. %@AS@%  {
  9888. %@AS@%     char inbuffer[BUFF];
  9889. %@AS@%     char outbuffer[BUFF];
  9890. %@AS@%     int infile, outfile, length, num;
  9891. %@AS@%  
  9892. %@AS@%     strcpy( outbuffer, "Happy Birthday." );
  9893. %@AS@%     length = strlen( outbuffer );
  9894. %@AS@%     length++;
  9895. %@AS@%  
  9896. %@AS@%     if( (outfile = open( "testfile.bin",
  9897. %@AS@%        O_CREAT | O_WRONLY | O_BINARY,  S_IWRITE )) != -1 )
  9898. %@AS@%     {
  9899. %@AS@%        if( (num = write( outfile, outbuffer, length )) == -1 )
  9900. %@AS@%           perror( "Error in writing" );
  9901. %@AS@%        printf( "\nBytes written to file: %d\n", num );
  9902. %@AS@%        close( outfile );
  9903. %@AS@%     }
  9904. %@AS@%     else
  9905. %@AS@%        perror( "Error opening outfile" );%@AE@%%@NL@%
  9906. %@NL@%
  9907. %@AS@%  if( (infile = open( "testfile.bin", O_RDONLY | O_BINARY )) != -1  )
  9908. %@AS@%     {
  9909. %@AS@%        while( length = read( infile, inbuffer, BUFF ) )
  9910. %@AS@%           printf( "%d bytes received so far.\n", length );
  9911. %@AS@%        close( infile );
  9912. %@AS@%        printf( "%s\n", inbuffer );
  9913. %@AS@%     }
  9914. %@AS@%     else
  9915. %@AS@%        perror( "Error opening infile" );
  9916. %@AS@%  }%@AE@%%@NL@%
  9917. %@NL@%
  9918. Several header files must be included:  %@NL@%
  9919. %@NL@%
  9920. %@AS@%  #include <stdio.h>
  9921. %@AS@%  #include <fcntl.h>
  9922. %@AS@%  #include <sys\types.h>
  9923. %@AS@%  #include <sys\stat.h>
  9924. %@AS@%  #include <io.h>%@AE@%%@NL@%
  9925. %@NL@%
  9926. The symbolic constant %@AS@% BUFF %@AE@% is defined as 512. This value is used
  9927. immediately in the declaration of two buffers:  %@NL@%
  9928. %@NL@%
  9929. %@AS@%  char inbuffer[BUFF];
  9930. %@AS@%  char outbuffer[BUFF];%@AE@%%@NL@%
  9931. %@NL@%
  9932. Note that we don't need %@AB@%FILE%@AE@% structures anywhere in the program. The
  9933. standard I/O routines automatically allocated space for a buffer. Since
  9934. we're operating closer to the DOS level, we must allocate our own buffers,
  9935. instead of relying on the system. If you set the buffer size to a
  9936. sufficiently large value, QuickC will run out of stack space. When this
  9937. happens, you may either make the buffers global variables or use the %@AB@%malloc%@AE@%
  9938. function to allocate an additional chunk of memory.  %@NL@%
  9939. %@NL@%
  9940. The %@AB@%open%@AE@% function takes three parameters:  %@NL@%
  9941. %@NL@%
  9942. %@AS@%  if( (outfile = open( "testfile.bin",
  9943. %@AS@%     O_CREAT | O_WRONLY | O_BINARY, S_IWRITE )) != -1 )%@AE@%%@NL@%
  9944. %@NL@%
  9945. The first parameter is the file name. The second is a sequence of "oflags"
  9946. that are combined with the OR operator. The oflags determine which type of
  9947. file will be opened: it will be created (%@AS@% O_CREAT%@AE@% ), it will be write-only (%@AS@%
  9948. %@AS@%O_WRONLY%@AE@% ), and it will be a binary─not a text─file (%@AS@% O_BINARY%@AE@% ). When you
  9949. create a new file, you must include the third parameter: %@AS@% S_IWRITE%@AE@%.  %@NL@%
  9950. %@NL@%
  9951. The %@AB@%open%@AE@% function returns a file handle, which is assigned to the integer
  9952. variable %@AS@% outfile%@AE@%. Note that this is an integer, not a pointer to a %@AB@%FILE%@AE@%
  9953. structure. If anything goes wrong, a value of -1 is returned by %@AB@%open%@AE@%, and we
  9954. should test for this.  %@NL@%
  9955. %@NL@%
  9956. Table 11.4 summarizes the differences between %@AB@%fopen%@AE@% and %@AB@%open%@AE@%.  %@NL@%
  9957. %@NL@%
  9958. %@AB@%Table 11.4  %@AB@%Standard vs. Low-Level%@AE@%%@AE@%
  9959.  
  9960. %@TH:  11   674 02 10 27 18 21 @%Function  Error Parameters           Returns           Condition%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%fopen%@AE@%     File name,  type           Pointer to %@AB@%FILE %@AE@%  NULL          (%@AB@%r%@AE@%, %@AB@%w%@AE@%, %@AB@%a%@AE@%), and mode                                    (%@AB@%t%@AE@%,%@AB@% b%@AE@%)                                       %@AB@%open%@AE@%      File name, oflags          File handle       -1                                     (integer)         %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  11   674 02 10 27 18 21 @%
  9961.  
  9962. %@NL@%
  9963. %@4@%%@AB@%Low-Level Writing%@AE@%%@EH@%%@NL@%
  9964. %@NL@%
  9965. The %@AB@%write%@AE@% function takes three parameters:  %@NL@%
  9966. %@NL@%
  9967. %@NL@%
  9968.   1.  The file handle returned by %@AB@%open%@AE@%%@NL@%
  9969. %@NL@%
  9970.   2.  The address of the buffer%@NL@%
  9971. %@NL@%
  9972.   3.  The number of bytes to write%@NL@%
  9973. %@NL@%
  9974. %@NL@%
  9975. You, the programmer, are responsible for filling up the buffer. The %@AB@%write%@AE@%
  9976. function returns the number of bytes actually written to the file.  %@NL@%
  9977. %@NL@%
  9978. %@AS@%  if( (num = write( outfile, outbuffer, length )) == -1 )
  9979. %@AS@%     perror( "Error in writing" );
  9980. %@AS@%  printf( "\nBytes written to file: %d\n", num );
  9981. %@AS@%  close( outfile );%@AE@%%@NL@%
  9982. %@NL@%
  9983. %@NL@%
  9984. %@4@%%@AB@%Low-Level Reading%@AE@%%@EH@%%@NL@%
  9985. %@NL@%
  9986. Next, we open the file for reading. Again, the oflags are required:  %@NL@%
  9987. %@NL@%
  9988. %@AS@%  if( (infile = open( "testfile.bin", O_RDONLY | O_BINARY )) != -1 )
  9989. %@AS@%  {
  9990. %@AS@%     while( length = read( infile, inbuffer, BUFF ) )
  9991. %@AS@%        printf( "%d bytes received so far.\n", length );
  9992. %@AS@%     close( infile );
  9993. %@AS@%     printf( "%s\n", inbuffer );
  9994. %@AS@%  }%@AE@%%@NL@%
  9995. %@NL@%
  9996. The %@AB@%read%@AE@% function takes three parameters:  %@NL@%
  9997. %@NL@%
  9998. %@NL@%
  9999.   1.  The file handle%@NL@%
  10000. %@NL@%
  10001.   2.  The address of the buffer%@NL@%
  10002. %@NL@%
  10003.   3.  The size of the buffer%@NL@%
  10004. %@NL@%
  10005. %@NL@%
  10006. The value returned is the number of bytes read. The %@AB@%while%@AE@% loop continues as
  10007. long as there are characters in the stream. In a real application, you'll
  10008. have to handle the bytes stored in the buffer.  %@NL@%
  10009. %@NL@%
  10010. The low-level file functions are unbuffered. When you call %@AB@%write%@AE@%, the bytes
  10011. are written directly to the disk file. The standard file function %@AB@%fwrite%@AE@%
  10012. doesn't write data to disk; it writes to a buffer. The buffer is transferred
  10013. to disk when the buffer fills up, when the %@AB@%fflush%@AE@% function is called, or
  10014. when the file is closed. As a gen-eral rule, you should not mix buffered and
  10015. unbuffered routines. Use the standard routines or the low-level routines,
  10016. but not both.  %@NL@%
  10017. %@NL@%
  10018. This chapter started with keyboard input and screen output and led into
  10019. discussions of file I/O. The following chapters cover in greater depth
  10020. assembly language routines and some specialized types of screen output,
  10021. including high-resolution graphics, fonts, and presentation graphics.  %@NL@%
  10022. %@NL@%
  10023. %@NL@%
  10024. %@NL@%
  10025. %@NL@%
  10026. %@NL@%
  10027. %@NL@%
  10028. %@CR:C6A00120001 @%%@1@%%@AB@%Chapter 12  Dynamic Memory Allocation%@AE@%%@EH@%%@NL@%
  10029. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  10030. %@NL@%
  10031. A program that allocates memory "dynamically" (as it runs) can respond
  10032. flexibly to a user's needs, creating new data structures when the need
  10033. arises and discarding them when their job is done.  %@NL@%
  10034. %@NL@%
  10035. As you read this chapter you'll learn how to allocate memory with the %@AB@%malloc%@AE@%
  10036. library function and free memory with the %@AB@%free%@AE@% function. We'll also look at
  10037. two related functions, %@AB@%calloc%@AE@% and %@AB@%realloc%@AE@%.  %@NL@%
  10038. %@NL@%
  10039. Memory allocation requires the use of pointers. If you're not familiar with
  10040. pointers, read Chapter 8, "Pointers," before tackling this chapter.  %@NL@%
  10041. %@NL@%
  10042. %@NL@%
  10043. %@2@%%@CR:C6A00120002 @%%@AB@%Why Allocate?%@AE@%%@EH@%%@NL@%
  10044. %@NL@%
  10045. %@AU@% The malloc family of library functions can allocate memory during run time.%@AE@%
  10046. %@NL@%
  10047. %@NL@%
  10048. The %@AB@%malloc%@AE@% (memory allocate) family of library functions enables you to
  10049. allocate blocks of memory dynamically. The capability to create new data
  10050. structures on the fly lets you tailor a program's behavior precisely to the
  10051. user's needs.  %@NL@%
  10052. %@NL@%
  10053. For simple programs, such as the examples in Part 1, memory allocation is
  10054. largely automatic. When you declare variables, as in the lines  %@NL@%
  10055. %@NL@%
  10056. %@AS@%  int count;
  10057. %@AS@%  char buffer[160];%@AE@%%@NL@%
  10058. %@NL@%
  10059. QuickC allocates enough memory to store each variable (2 bytes for the first
  10060. variable and 160 for the second). This method works fine if you know each
  10061. vari-able's size in advance. Some program memory needs aren't easy to
  10062. predict, however.  %@NL@%
  10063. %@NL@%
  10064. To take a simple example, say you write an address-book program that stores
  10065. addresses in an array of structures. A novice programmer might begin by
  10066. declaring an array of, say, 100 structures, in the following manner,  %@NL@%
  10067. %@NL@%
  10068. %@AS@%  struct address list[100];%@AE@%%@NL@%
  10069. %@NL@%
  10070. but this approach is needlessly limiting. If your list contains only a few
  10071. addresses, most of the memory in the array is wasted. And if you want to
  10072. enter more than 100 addresses, you're out of luck.  %@NL@%
  10073. %@NL@%
  10074. A better approach is to allocate memory for the array dynamically. This way,
  10075. the program can use only as much memory as needed for the current address
  10076. list. Each time you add an address, or delete one, the program can expand or
  10077. shrink the array as needed.  %@NL@%
  10078. %@NL@%
  10079. %@NL@%
  10080. %@2@%%@CR:C6A00120003 @%%@AB@%Memory Allocation Basics%@AE@%%@EH@%%@NL@%
  10081. %@NL@%
  10082. We'll use a simple example program, COPYFILE.C, to demonstrate the basics of
  10083. dynamic memory allocation─how to allocate a memory block, access its
  10084. contents, and free the block when its purpose is served.  %@NL@%
  10085. %@NL@%
  10086. The COPYFILE.C program, shown below, dynamically allocates a buffer that it
  10087. uses to store file data.  %@NL@%
  10088. %@NL@%
  10089. %@AS@%  /* COPYFILE.C: Demonstrate malloc and free functions. */
  10090. %@AS@%  
  10091. %@AS@%  #include <stdio.h>     /* printf function and NULL */
  10092. %@AS@%  #include <io.h>        /* low-level I/O functions */
  10093. %@AS@%  #include <conio.h>     /* getch function */
  10094. %@AS@%  #include <sys\types.h> /* struct members used in stat.h */
  10095. %@AS@%  #include <sys\stat.h>  /* S_ constants */
  10096. %@AS@%  #include <fcntl.h>     /* O_ constants */
  10097. %@AS@%  #include <malloc.h>    /* malloc function */
  10098. %@AS@%  #include <errno.h>     /* errno global variable */
  10099. %@AS@%  
  10100. %@AS@%  int copyfile( char *source, char *destin );
  10101. %@AS@%  
  10102. %@AS@%  main( int argc, char *argv[] )
  10103. %@AS@%  {
  10104. %@AS@%     if( argc == 3 )
  10105. %@AS@%        if( copyfile( argv[1], argv[2] ) )
  10106. %@AS@%           printf( "Copy failed\n" );
  10107. %@AS@%        else
  10108. %@AS@%           printf( "Copy successful\n" );
  10109. %@AS@%     else
  10110. %@AS@%        printf( "  SYNTAX: COPYFILE <source> <target>\n" );
  10111. %@AS@%  
  10112. %@AS@%     return 0;
  10113. %@AS@%  }
  10114. %@AS@%  
  10115. %@AS@%  int copyfile( char *source, char *target )
  10116. %@AS@%  {
  10117. %@AS@%     char *buf;
  10118. %@AS@%     int hsource, htarget, ch;
  10119. %@AS@%     unsigned count = 50000;
  10120. %@AS@%  
  10121. %@AS@%     if( (hsource = open( source, O_BINARY | O_RDONLY )) == - 1 )
  10122. %@AS@%        return errno;
  10123. %@AS@%     htarget = open( target, O_BINARY | O_WRONLY | O_CREAT | O_EXCL,
  10124. %@AS@%                             S_IREAD | S_IWRITE );
  10125. %@AS@%     if( errno == EEXIST )
  10126. %@AS@%     {
  10127. %@AS@%        cputs( "Target exists. Overwrite? " );
  10128. %@AS@%        ch = getch();
  10129. %@AS@%        if( (ch == 'y') || (ch == 'Y') )
  10130. %@AS@%           htarget = open( target, O_BINARY | O_WRONLY | O_CREAT | O_TRUNC,
  10131. %@AS@%                                   S_IREAD | S_IWRITE );
  10132. %@AS@%        printf( "\n" );
  10133. %@AS@%     }
  10134. %@AS@%     if( htarget == -1 )
  10135. %@AS@%        return errno;
  10136. %@AS@%  
  10137. %@AS@%     if( filelength( hsource ) < count )
  10138. %@AS@%        count = (int)filelength( hsource );
  10139. %@AS@%  
  10140. %@AS@%     buf = (char *)malloc( (size_t)count );
  10141. %@AS@%  
  10142. %@AS@%     if( buf == NULL )
  10143. %@AS@%     {
  10144. %@AS@%        count = _memmax();
  10145. %@AS@%        buf = (char *)malloc( (size_t)count );
  10146. %@AS@%        if( buf == NULL )
  10147. %@AS@%           return ENOMEM;
  10148. %@AS@%     }
  10149. %@AS@%  
  10150. %@AS@%     while( !eof( hsource ) )
  10151. %@AS@%     {
  10152. %@AS@%        if( (count = read( hsource, buf, count )) == -1 )
  10153. %@AS@%           return errno;
  10154. %@AS@%        if( (count = write( htarget, buf, count )) == - 1 )
  10155. %@AS@%           return errno;
  10156. %@AS@%     }
  10157. %@AS@%  
  10158. %@AS@%     close( hsource );
  10159. %@AS@%     close( htarget );
  10160. %@AS@%     free( buf );
  10161. %@AS@%     return 0;
  10162. %@AS@%  }%@AE@%%@NL@%
  10163. %@NL@%
  10164. Before we look at how COPYFILE.C works, let's note what it does. Unlike the
  10165. DOS COPY command, the COPYFILE.C program asks for confirmation before
  10166. overwriting an existing file. The program expects to receive two file names
  10167. as command-line parameters: the name of the file to copy and the name of the
  10168. new file. For instance, the following command copies the file SAMPLE.EXE to
  10169. the new file EXAMPLE.EXE:  %@NL@%
  10170. %@NL@%
  10171. %@AS@%  copyfile sample.exe example.exe%@AE@%%@NL@%
  10172. %@NL@%
  10173. If the target file already exists, COPYFILE.C displays:  %@NL@%
  10174. %@NL@%
  10175. %@AS@%  Target exists. Overwrite?%@AE@%%@NL@%
  10176. %@NL@%
  10177. COPYFILE.C overwrites an existing file only if the user presses the Y key in
  10178. response.  %@NL@%
  10179. %@NL@%
  10180. %@NL@%
  10181. %@3@%%@CR:C6A00120004 @%%@AB@%Preparing to Allocate Memory%@AE@%%@EH@%%@NL@%
  10182. %@NL@%
  10183. The COPYFILE.C program copies the source file in chunks, using an allocated
  10184. memory block as a buffer for file data. The following program lines are the
  10185. ones involved in allocating and freeing the memory block. (These are taken
  10186. from the program in order, but are not consecutive.)  %@NL@%
  10187. %@NL@%
  10188. %@AS@%  #include <malloc.h>    /* malloc function */
  10189. %@AS@%  char *buf;
  10190. %@AS@%  unsigned count = 50000;
  10191. %@AS@%  buf = (char *)malloc( (size_t)count );
  10192. %@AS@%  free( buf );%@AE@%%@NL@%
  10193. %@NL@%
  10194. The first of these,  %@NL@%
  10195. %@NL@%
  10196. %@AS@%  #include <malloc.h>  /* malloc function */%@AE@%%@NL@%
  10197. %@NL@%
  10198. includes the standard include file MALLOC.H, which contains declarations for
  10199. %@AB@%malloc%@AE@% and other memory-allocating functions.  %@NL@%
  10200. %@NL@%
  10201. The %@AB@%malloc%@AE@% function, which the program will call to allocate a memory block,
  10202. returns the address where the block begins. COPYFILE.C declares the pointer
  10203. variable %@AS@% buf %@AE@% to store this address:  %@NL@%
  10204. %@NL@%
  10205. %@AS@%  char *buf;%@AE@%%@NL@%
  10206. %@NL@%
  10207. As you'll see shortly, the pointer %@AS@% buf %@AE@% will be initialized to point to the
  10208. allocated block. Once this is done, the program can access the block's
  10209. contents through the pointer.  %@NL@%
  10210. %@NL@%
  10211. The COPYFILE.C program declares another variable, %@AS@% count%@AE@%, which is used to
  10212. tell %@AB@%malloc%@AE@% how much memory (in bytes) to allocate. The program initially
  10213. sets this value to 50,000:  %@NL@%
  10214. %@NL@%
  10215. %@AS@%  unsigned count = 50000;%@AE@%%@NL@%
  10216. %@NL@%
  10217. If the source file is smaller than 50,000 bytes, COPYFILE.C later resets %@AS@%
  10218. %@AS@%count %@AE@% to the smaller value.  %@NL@%
  10219. %@NL@%
  10220. %@NL@%
  10221. %@3@%%@CR:C6A00120005 @%%@AB@%Specifying the Size of the Allocated Block%@AE@%%@EH@%%@NL@%
  10222. %@NL@%
  10223. Now we're ready to allocate the block. The statement  %@NL@%
  10224. %@NL@%
  10225. %@AS@%  buf = (char *)malloc( (size_t)count) );%@AE@%%@NL@%
  10226. %@NL@%
  10227. in COPYFILE.C calls the %@AB@%malloc%@AE@% function, passing the value of %@AS@% count %@AE@% as an
  10228. argument. This argument indicates the size of the desired block in bytes. In
  10229. COPYFILE.C this value is 50,000 or the size of the source file, whichever is
  10230. smaller.  %@NL@%
  10231. %@NL@%
  10232. Look at the type cast preceding the function argument:  %@NL@%
  10233. %@NL@%
  10234. %@AS@%  (size_t)%@AE@%%@NL@%
  10235. %@NL@%
  10236. The cast is performed for ANSI compatibility (%@AB@%malloc%@AE@% is part of the ANSI
  10237. standard). Under ANSI, %@AB@%malloc%@AE@% is declared as taking an argument of the type %@AS@%
  10238. %@AS@%size_t%@AE@%. To ensure the portability of your programs, the value passed to
  10239. %@AB@%malloc%@AE@% should be either declared or cast as type %@AS@% size_t%@AE@%.  %@NL@%
  10240. %@NL@%
  10241. %@NL@%
  10242. %@3@%%@CR:C6A00120006 @%%@AB@%A Graphic Illustration%@AE@%%@EH@%%@NL@%
  10243. %@NL@%
  10244. Figures 12.1 and 12.2 show how the COPYFILE.C program allocates a memory
  10245. block. The figures are simplified and are not drawn to scale. They represent
  10246. the program's "data segment," which is the memory area available for the
  10247. program's data storage.  %@NL@%
  10248. %@NL@%
  10249. ────────────────────────────────────────────────────────────────────────────%@NL@%
  10250. NOTE
  10251.  
  10252. %@AI@%The details of data storage differ depending on the current "memory model,"
  10253. %@AI@%an advanced concept that goes beyond the scope of this book. For purposes of
  10254. %@AI@%discussion, this book assumes the small memory model, which is the default
  10255. %@AI@%for QuickC.%@AE@%%@NL@%
  10256. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  10257. %@NL@%
  10258. Figure 12.1 represents the program's data segment before COPYFILE.C
  10259. allocates a block of memory. The shaded area labeled "Declared data"
  10260. contains the program's declared variables and "stack," which is used for
  10261. temporary storage. The unshaded area labeled "Heap" contains the memory
  10262. available for allocation by COPYFILE.C.  %@NL@%
  10263. %@NL@%
  10264. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  10265. %@NL@%
  10266. Figure 12.2 shows COPYFILE.C immediately after the program calls the %@AB@%malloc%@AE@%
  10267. function to allocate a block of memory. The allocated block is taken from
  10268. heap memory and lies directly above the program. If COPYFILE.C allocated a
  10269. second memory block, that block would lie above the first, further
  10270. diminishing heap memory.  %@NL@%
  10271. %@NL@%
  10272. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  10273. %@NL@%
  10274. While it's common to say the %@AB@%malloc%@AE@% function "creates" a memory block, that
  10275. terminology is a bit misleading. As Figures 12.1 and 12.2 show, %@AB@%malloc%@AE@%
  10276. simply gives your program control over memory that's already present.  %@NL@%
  10277. %@NL@%
  10278. %@AU@% The malloc function does not initialize the  memory it allocates.%@AE@%  %@NL@%
  10279. %@NL@%
  10280. Since the allocated block has been present in memory all along, it may
  10281. contain random values or values left over from some previous use. The %@AB@%malloc%@AE@%
  10282. function doesn't initialize allocated memory. Substitute the %@AB@%calloc%@AE@% function
  10283. for %@AB@%malloc%@AE@% if you want to clear an allocated block before use. (See the
  10284. section "The calloc Function" below.)  %@NL@%
  10285. %@NL@%
  10286. %@NL@%
  10287. %@3@%%@CR:C6A00120007 @%%@AB@%Assigning the Address that malloc Returns%@AE@%%@EH@%%@NL@%
  10288. %@NL@%
  10289. If the call to %@AB@%malloc%@AE@% succeeds, %@AB@%malloc%@AE@% returns the address of the memory
  10290. block it allocates. COPYFILE.C assigns that return value to the pointer
  10291. variable %@AS@% buf %@AE@% and then accesses the allocated block through %@AS@% buf%@AE@%.  %@NL@%
  10292. %@NL@%
  10293. Before assigning the address that %@AB@% malloc%@AE@% returns, COPYFILE.C performs a
  10294. type cast on the address  %@NL@%
  10295. %@NL@%
  10296. %@AS@%  (char *)%@AE@%%@NL@%
  10297. %@NL@%
  10298. The type cast indicates which type of memory you are allocating. Prior to
  10299. the ANSI standard, %@AB@%malloc%@AE@% was declared as returning a pointer to type %@AB@%char%@AE@%,
  10300. so it was necessary to cast the return value when assigning the value to any
  10301. other pointer type.  %@NL@%
  10302. %@NL@%
  10303. Under ANSI, %@AB@%malloc%@AE@% returns a pointer to type %@AB@%void%@AE@%. Since a %@AB@%void%@AE@% pointer can
  10304. be converted to any pointer type, it's not strictly necessary to cast the
  10305. return from %@AB@%malloc%@AE@%. (If you omit the cast, QuickC does a silent type
  10306. conversion.) The type cast improves readability, however.  %@NL@%
  10307. %@NL@%
  10308. %@NL@%
  10309. %@3@%%@CR:C6A00120008 @%%@AB@%Checking the Return from malloc%@AE@%%@EH@%%@NL@%
  10310. %@NL@%
  10311. If a call to %@AB@%malloc%@AE@% fails─usually because not enough memory is available─the
  10312. function returns a null pointer (defined as %@AS@% NULL %@AE@% in the standard include
  10313. file STDIO.H). You should always test this return value, even if you're
  10314. confident the allocation will succeed. If you ignore the return value and
  10315. access memory through a null pointer, your program may stop with a run-time
  10316. error or overwrite unpredictable memory addresses.  %@NL@%
  10317. %@NL@%
  10318. Thus, before attempting to use the allocated memory block, COPYFILE.C checks
  10319. to make sure the call to %@AB@%malloc%@AE@% succeeded:  %@NL@%
  10320. %@NL@%
  10321. %@AS@%  if( buf == NULL )
  10322. %@AS@%  {
  10323. %@AS@%  .
  10324. %@AS@%  .
  10325. %@AS@%  .
  10326. %@AS@%  }%@AE@%%@NL@%
  10327. %@NL@%
  10328. The %@AB@%if%@AE@% statement tests whether the pointer %@AS@% buf %@AE@% has been set to %@AS@% NULL%@AE@%,
  10329. which would signal failure. In that case, the program executes the code
  10330. within the braces of the %@AB@%if%@AE@% statement.  %@NL@%
  10331. %@NL@%
  10332. Sometimes there may be enough free memory to satisfy only part of your
  10333. memory request. Look at how COPYFILE.C handles this situation:  %@NL@%
  10334. %@NL@%
  10335. %@AS@%  buf = (char *)malloc( (size_t)count );
  10336. %@AS@%  
  10337. %@AS@%  if( buf == NULL )
  10338. %@AS@%  {
  10339. %@AS@%     count = _memmax();
  10340. %@AS@%     buf = (char *)malloc( (size_t)count );
  10341. %@AS@%     if( buf == NULL )
  10342. %@AS@%        return ENOMEM;
  10343. %@AS@%  }%@AE@%%@NL@%
  10344. %@NL@%
  10345. If fewer than %@AS@% count %@AE@% bytes of memory are available, the initial call to
  10346. %@AB@%malloc%@AE@% returns %@AS@% NULL%@AE@%, indicating failure. In that event, COPYFILE.C calls
  10347. the %@AB@%_memmax%@AE@% library routine to find how much memory is available and assigns
  10348. that value to the variable %@AS@% count%@AE@%:  %@NL@%
  10349. %@NL@%
  10350. %@AS@%  count = _memmax();%@AE@%%@NL@%
  10351. %@NL@%
  10352. Then COPYFILE.C calls %@AB@%malloc%@AE@% again, requesting a smaller amount of memory.
  10353. This request is bound to succeed unless no memory is available.  %@NL@%
  10354. %@NL@%
  10355. %@NL@%
  10356. %@3@%%@CR:C6A00120009 @%%@AB@%Accessing an Allocated Memory Block%@AE@%%@EH@%%@NL@%
  10357. %@NL@%
  10358. Once you have allocated a block of memory, you can access it through its
  10359. pointer (%@AS@% buf%@AE@%, in this example). COPYFILE.C uses its allocated block as a
  10360. file buffer, alternately reading in data from the source file, through the
  10361. statement  %@NL@%
  10362. %@NL@%
  10363. %@AS@%  if( (count = read( hsource, buf, count )) == -1 )
  10364. %@AS@%     return errno;%@AE@%%@NL@%
  10365. %@NL@%
  10366. and writing it to the target file, through the statement  %@NL@%
  10367. %@NL@%
  10368. %@AS@%  if( (count = write( htarget, buf, count )) == - 1 )
  10369. %@AS@%     return errno;%@AE@%%@NL@%
  10370. %@NL@%
  10371. The %@AB@%read%@AE@% and %@AB@%write%@AE@% function calls occur within %@AB@%if%@AE@% statements that compare
  10372. the function return values to -1, which would indicate failure.  %@NL@%
  10373. %@NL@%
  10374. COPYFILE.C treats its allocated block as a single chunk of memory. To access
  10375. individual data items in an allocated block, you can use either pointer or
  10376. array notation. Both of the following statements, for instance, refer to the
  10377. third byte in the block that %@AS@% buf %@AE@% points to:  %@NL@%
  10378. %@NL@%
  10379. %@AS@%  buf[2] = 'x';
  10380. %@AS@%  *(buf+2) = 'x';%@AE@%%@NL@%
  10381. %@NL@%
  10382. %@NL@%
  10383. %@3@%%@CR:C6A00120010 @%%@AB@%Allocating Memory for Different Data Types%@AE@%%@EH@%%@NL@%
  10384. %@NL@%
  10385. Since COPYFILE.C accesses its allocated block through a %@AB@%char%@AE@% pointer, the
  10386. program must treat the items in that block as %@AB@%char%@AE@% types. If you need to use
  10387. a different type of memory, simply change the pointer declaration and cast
  10388. the return from %@AB@%malloc%@AE@% accordingly. For instance, you could use the
  10389. following statements to allocate a block large enough to store 30 %@AB@%int%@AE@%
  10390. values:  %@NL@%
  10391. %@NL@%
  10392. %@AS@%  int *buf;
  10393. %@AS@%  
  10394. %@AS@%  buf = (int *)malloc( (size_t)sizeof( int ) * 30 );%@AE@%%@NL@%
  10395. %@NL@%
  10396. Here, the %@AB@%sizeof%@AE@% operator eliminates the need to calculate how many bytes of
  10397. storage 30 integers require. The expression  %@NL@%
  10398. %@NL@%
  10399. %@AS@%  sizeof( int )%@AE@%%@NL@%
  10400. %@NL@%
  10401. returns the size of an %@AB@%int%@AE@% type, which we then multiply by the desired
  10402. number of %@AB@%int%@AE@% items.  %@NL@%
  10403. %@NL@%
  10404. If the above call to %@AB@%malloc%@AE@% succeeds, you have, in effect, a 30-element
  10405. array of integers. And since pointer notation and array notation are
  10406. interchangeable, you can access any element of the array using the pointer
  10407. name and array notation. For instance, the expression  %@NL@%
  10408. %@NL@%
  10409. %@AS@%  ptr[2] = 50;%@AE@%%@NL@%
  10410. %@NL@%
  10411. assigns the value 50 to the third element of the array. Note that this
  10412. statement accesses the third %@AB@%int%@AE@% element in the array, not the third byte.
  10413. Pointer references, as explained in Chapter 8, "Pointers," are always scaled
  10414. by the size of the type used to declare the pointer.  %@NL@%
  10415. %@NL@%
  10416. Allocating memory for structures is equally straightforward. Say that you
  10417. want to allocate memory to store 10 structures of the type %@AS@% employee%@AE@%, which
  10418. is declared in the EMPLOYEE.C program in Chapter 4, "Data Types." The
  10419. EMPLOYEE.C program uses the following structure type:  %@NL@%
  10420. %@NL@%
  10421. %@AS@%  struct employee
  10422. %@AS@%  {
  10423. %@AS@%     char name[10];
  10424. %@AS@%     int months;
  10425. %@AS@%     float wage;
  10426. %@AS@%  };%@AE@%%@NL@%
  10427. %@NL@%
  10428. You could use the statement  %@NL@%
  10429. %@NL@%
  10430. %@AS@%  struct employee *e_ptr;%@AE@%%@NL@%
  10431. %@NL@%
  10432. to declare a pointer to an item of the %@AS@% employee %@AE@% type. Once you have a
  10433. suitable pointer, you could use the following statement to allocate enough
  10434. memory to store 10 structures of the same type:  %@NL@%
  10435. %@NL@%
  10436. %@AS@%  e_ptr = (struct employee *) malloc( (size_t)sizeof( struct employee ) * 10
  10437. %@AS@%  );%@AE@%%@NL@%
  10438. %@NL@%
  10439. Here, the %@AB@%sizeof%@AE@% operator  %@NL@%
  10440. %@NL@%
  10441. %@AS@%  sizeof( struct employee )%@AE@%%@NL@%
  10442. %@NL@%
  10443. returns the size of a structure of the %@AS@% employee %@AE@% type.  %@NL@%
  10444. %@NL@%
  10445. If the allocation succeeds, you have, in effect, an array of structures of
  10446. type %@AS@%employee%@AE@%. Using structure notation, you can access any structure member
  10447. in the block. The following statements, for instance, initialize the members
  10448. of the third structure in the array:  %@NL@%
  10449. %@NL@%
  10450. %@AS@%  strcpy( e_ptr[2].name, "Isaac, N." );
  10451. %@AS@%  e_ptr[2].months = 54;
  10452. %@AS@%  e_ptr[2].wage = (float) 12.21;%@AE@%%@NL@%
  10453. %@NL@%
  10454. %@NL@%
  10455. %@3@%%@CR:C6A00120011 @%%@AB@%Deallocating Memory with the free Function%@AE@%%@EH@%%@NL@%
  10456. %@NL@%
  10457. %@AU@% The free function deallocates an allocated memory block.%@AE@%  %@NL@%
  10458. %@NL@%
  10459. When you have finished using an allocated memory block, you should free
  10460. (deallocate) the block with the %@AB@%free%@AE@% library function. The %@AB@%free%@AE@% function
  10461. takes one argument: the address of the block you wish to free. The
  10462. COPYFILE.C program frees its allocated block with the statement:  %@NL@%
  10463. %@NL@%
  10464. %@AS@%  free( buf );%@AE@%%@NL@%
  10465. %@NL@%
  10466. It's your responsibility to pass a valid address to %@AB@%free%@AE@%. Unlike most
  10467. library functions, %@AB@%free%@AE@% doesn't return any value to indicate success or
  10468. failure. If you pass an invalid address, the memory block remains allocated
  10469. and can't be used for any other purpose.  %@NL@%
  10470. %@NL@%
  10471. Figure 12.3 shows COPYFILE.C after the program frees its allocated block.
  10472. The %@AB@%free%@AE@% function releases the block from the program's control, returning
  10473. it to the heap. The same memory is still present, of course. But since your
  10474. program no longer has control of that memory, you shouldn't attempt to use
  10475. it. (See "Using Dangling Pointers" in Chapter 10, "Programming Pitfalls,"
  10476. for more information on this point.)  %@NL@%
  10477. %@NL@%
  10478. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  10479. %@NL@%
  10480. %@NL@%
  10481. %@2@%%@CR:C6A00120012 @%%@AB@%Specialized Memory-Allocating Functions%@AE@%%@EH@%%@NL@%
  10482. %@NL@%
  10483. The C library contains two specialized versions of %@AB@%malloc%@AE@% that you may find
  10484. useful. The %@AB@%calloc%@AE@% function allocates memory for an array and sets the
  10485. block's contents to 0. The %@AB@%realloc%@AE@% function can expand or shrink an existing
  10486. memory block.  %@NL@%
  10487. %@NL@%
  10488. %@NL@%
  10489. %@3@%%@CR:C6A00120013 @%%@AB@%The calloc Function%@AE@%%@EH@%%@NL@%
  10490. %@NL@%
  10491. The %@AB@%calloc%@AE@% (calculated allocate) function is especially useful for
  10492. allocating memory for an array. It works like %@AB@%malloc%@AE@% but takes two
  10493. arguments:  %@NL@%
  10494. %@NL@%
  10495. %@NL@%
  10496.   ■   The number of data items for which you wish to allocate memory%@NL@%
  10497. %@NL@%
  10498.   ■   The size of each data item%@NL@%
  10499. %@NL@%
  10500. %@NL@%
  10501. This scheme eliminates the need for you to calculate the number of bytes
  10502. needed to store the desired array. For instance, the statement  %@NL@%
  10503. %@NL@%
  10504. %@AS@%  ptr = (int *) calloc( (size_t)30, (size_t)sizeof( int ) );%@AE@%%@NL@%
  10505. %@NL@%
  10506. allocates enough memory for a 30-element integer array, and  %@NL@%
  10507. %@NL@%
  10508. %@AS@%  e_ptr = (struct employee *) calloc( (size_t)30, sizeof( struct employee )
  10509. %@AS@%  );%@AE@%%@NL@%
  10510. %@NL@%
  10511. allocates enough memory for a 30-element array of structures of type
  10512. %@AS@%employee%@AE@%.  %@NL@%
  10513. %@NL@%
  10514. %@AU@% The calloc library function allocates memory and sets every byte in the
  10515. %@AU@%block to 0.%@AE@%  %@NL@%
  10516. %@NL@%
  10517. The %@AB@%calloc%@AE@% function also sets every byte in the requested block to 0. The
  10518. %@AB@%malloc%@AE@% function, as we noted earlier, doesn't change the contents of an
  10519. allocated block. If the block contained garbage values before allocation, it
  10520. contains garbage after allocation, too.  %@NL@%
  10521. %@NL@%
  10522. %@NL@%
  10523. %@3@%%@CR:C6A00120014 @%%@AB@%The realloc Function%@AE@%%@EH@%%@NL@%
  10524. %@NL@%
  10525. Sometimes you may need to adjust the size of an allocated memory block. The
  10526. %@AB@%realloc%@AE@% (reallocate) function can expand or shrink an existing memory block.
  10527. The function takes two arguments:  %@NL@%
  10528. %@NL@%
  10529. %@NL@%
  10530.   ■   The address of an existing allocated block%@NL@%
  10531. %@NL@%
  10532.   ■   The size (in bytes) you want to give the block%@NL@%
  10533. %@NL@%
  10534. %@NL@%
  10535. %@AU@% The realloc library function expands or shrinks an  existing allocated
  10536. %@AU@%block.%@AE@%  %@NL@%
  10537. %@NL@%
  10538. If enough memory is available to accommodate the resized block, %@AB@%realloc%@AE@%
  10539. allocates sufficient memory and copies as much of the existing block as the
  10540. new block will hold. If the new block is smaller than the original, data is
  10541. truncated.  %@NL@%
  10542. %@NL@%
  10543. For instance, if you had allocated a 30-element %@AB@%int%@AE@% array with the statement
  10544. %@NL@%
  10545. %@NL@%
  10546. %@AS@%  ptr = (int *) calloc( (size_t)30, (size_t)sizeof( int ) );%@AE@%%@NL@%
  10547. %@NL@%
  10548. the following statement would expand the block to contain 20 extra elements,
  10549. for a total of 50:  %@NL@%
  10550. %@NL@%
  10551. %@AS@%  ptr = (int *)realloc( ptr, (size_t)sizeof( int ) * 50 );%@AE@%%@NL@%
  10552. %@NL@%
  10553. The address you pass to %@AB@%realloc%@AE@% can be the address returned from a previous
  10554. call to any memory-allocation function: %@AB@%malloc%@AE@%, %@AB@%calloc%@AE@%, or %@AB@%realloc%@AE@% itself.  %@NL@%
  10555. %@NL@%
  10556. Like %@AB@%malloc%@AE@%, both %@AB@%calloc%@AE@% and %@AB@%realloc%@AE@% return a null address if they fail.
  10557. Remember to check the return value whenever you call a memory-allocating
  10558. function.  %@NL@%
  10559. %@NL@%
  10560. %@NL@%
  10561. %@2@%%@CR:C6A00120015 @%%@AB@%Keeping Out of Trouble%@AE@%%@EH@%%@NL@%
  10562. %@NL@%
  10563. Here are a few rules to help you avoid trouble when allocating memory
  10564. dynamically:  %@NL@%
  10565. %@NL@%
  10566. %@NL@%
  10567.   ■   Always check the return value when allocating memory.%@NL@%
  10568. %@NL@%
  10569.   ■   Be careful not to index past the boundaries of an allocated memory
  10570.       block.%@NL@%
  10571. %@NL@%
  10572.   ■   Free allocated memory as soon as you have finished using it.%@NL@%
  10573. %@NL@%
  10574.   ■   Make sure that the address you pass to the %@AB@%free%@AE@% function is valid.%@NL@%
  10575. %@NL@%
  10576.   ■   Don't use a pointer to an allocated block after freeing the block.%@NL@%
  10577. %@NL@%
  10578. %@NL@%
  10579. Most of these points were mentioned earlier, but the second deserves a
  10580. little elaboration. As you may recall from earlier chapters, the C language
  10581. doesn't check array subscripts or pointer references for validity. It's
  10582. important to remember this rule when using a pointer to access an allocated
  10583. block.  %@NL@%
  10584. %@NL@%
  10585. For instance, suppose that you allocate a 30-element integer array with the
  10586. statement  %@NL@%
  10587. %@NL@%
  10588. %@AS@%  ptr = (int *) malloc( (size_t)sizeof( int ) * 30 );%@AE@%%@NL@%
  10589. %@NL@%
  10590. and then execute either of these statements:  %@NL@%
  10591. %@NL@%
  10592. %@AS@%  ptr[32] = 80;
  10593. %@AS@%  *(ptr+32) = 80;%@AE@%%@NL@%
  10594. %@NL@%
  10595. Since the array has only 30 elements, both of the latter statements
  10596. overwrite memory outside the allocated memory block. The statements store
  10597. the value 80 in the address four bytes (two %@AB@%int%@AE@% elements) above the highest
  10598. element in the array.  %@NL@%
  10599. %@NL@%
  10600. While uncontrolled pointer operations always carry the potential for
  10601. disaster, they can create especially tricky program bugs if you write just
  10602. beyond an allocated memory block.  %@NL@%
  10603. %@NL@%
  10604. Near the beginning of each allocated block is a tiny "link" containing
  10605. information about the block. The memory-allocating functions use these links
  10606. to keep track of allocated memory, and the more blocks you have allocated,
  10607. the more important it is to keep all the links intact. If a bad pointer
  10608. reference overwrites a link, it can cause problems in an entirely unexpected
  10609. part of your program.  %@NL@%
  10610. %@NL@%
  10611. %@NL@%
  10612. %@NL@%
  10613. %@NL@%
  10614. %@NL@%
  10615. %@NL@%
  10616. %@CR:C6A00130001 @%%@1@%%@AB@%Chapter 13  Graphics%@AE@%%@EH@%%@NL@%
  10617. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  10618. %@NL@%
  10619. This chapter explains how to call graphics functions that set points, draw
  10620. lines, change colors, and draw shapes such as rectangles and circles. The
  10621. first section lists the three steps to using high-resolution graphics,
  10622. defines important graphics terms, and works through an example program step
  10623. by step, showing how to use the basic functions. The next sections explain
  10624. coordinate systems and show how to display graphics inside viewports and
  10625. windows.  %@NL@%
  10626. %@NL@%
  10627. %@NL@%
  10628. %@2@%%@CR:C6A00130002 @%%@AB@%Graphics Mode%@AE@%%@EH@%%@NL@%
  10629. %@NL@%
  10630. There are three steps to displaying graphics in QuickC:  %@NL@%
  10631. %@NL@%
  10632. %@NL@%
  10633.   1.  Use the %@AB@%_getvideoconfig%@AE@% function to determine which video adapter is
  10634.       installed. (See the section "Checking the Current Video Mode.")%@NL@%
  10635. %@NL@%
  10636.   2.  Use the %@AB@%_setvideomode%@AE@% function to set the desired graphics mode for
  10637.       the installed video adapter. (See the section "Setting the Video
  10638.       Mode.")%@NL@%
  10639. %@NL@%
  10640.   3.  Draw the graphics on the screen. (See the section "Writing a Graphics
  10641.       Program.")%@NL@%
  10642. %@NL@%
  10643. %@NL@%
  10644. There are several definitions you need to know before you can create
  10645. graphics programs. The following list explains the most useful terms:  %@NL@%
  10646. %@NL@%
  10647. %@NL@%
  10648.   ■   The "%@AI@%x%@AE@% axis" determines the horizontal position on the screen. The
  10649.       "origin" (point 0, 0) is in the upper left corner. The maximum number
  10650.       of horizontal "pixels" (picture elements) varies from 320 to 640 to
  10651.       720, depending on the graphics card installed and the graphics mode in
  10652.       effect.%@NL@%
  10653. %@NL@%
  10654.   ■   The "%@AI@%y%@AE@% axis" is the vertical position. The origin is the upper left
  10655.       corner. The number of vertical pixels ranges from 200 to 480.%@NL@%
  10656. %@NL@%
  10657.   ■   Each graphics mode offers a "palette" from which you may choose the
  10658.       colors to be displayed. You may have access to 2, 4, 8, 16, or 256
  10659.       "color indexes," depending on the graphics card in the computer and
  10660.       the graphics mode in effect.%@NL@%
  10661. %@NL@%
  10662.   ■   The CGA (Color Graphics Adapter) modes offer four fixed palettes
  10663.       containing predefined colors that may not be changed. In EGA (Enhanced
  10664.       Graphics Adapter), MCGA (Multicolor Graphics Array), and VGA (Video
  10665.       Graphics Array) graphics modes, you may change any of the color
  10666.       indexes by providing a color value that describes the mix of colors
  10667.       you wish to use. %@NL@%
  10668. %@NL@%
  10669.   ■   A color index is always a short integer. A color value is always a
  10670.       long integer. When you're calling graphics functions that require
  10671.       color-related parameters, you should be aware of the difference
  10672.       between color indexes and color values.%@NL@%
  10673. %@NL@%
  10674. %@NL@%
  10675. %@NL@%
  10676. %@3@%%@CR:C6A00130003 @%%@AB@%Checking the Current Video Mode%@AE@%%@EH@%%@NL@%
  10677. %@NL@%
  10678. Before or after entering graphics mode, you may inquire about the current
  10679. video configuration. This requires a special structure type called
  10680. %@AB@%videoconfig%@AE@%, which is defined in the GRAPH.H header file. You pass the
  10681. address of the structure to the function %@AB@%_getvideoconfig%@AE@%, which returns the
  10682. current video configuration information.  %@NL@%
  10683. %@NL@%
  10684. All graphics programs should include the graphics header file and declare a
  10685. structure of type %@AB@%videoconfig%@AE@%. The structure contains the following
  10686. elements:  %@NL@%
  10687. %@NL@%
  10688. %@AS@%  short numxpixels;   /*number of pixels on x axis*/
  10689. %@AS@%  short numypixels;   /*number of pixels on y axis*/
  10690. %@AS@%  short numtextcols;  /*number of text columns available*/
  10691. %@AS@%  short numtextrows;  /*number of text rows available*/
  10692. %@AS@%  short numcolors;    /*number of color indexes*/
  10693. %@AS@%  short bitsperpixel; /*number of bits per pixel*/
  10694. %@AS@%  short numvideopages;/*number of available video pages*/
  10695. %@AS@%  short mode;         /*current video mode*/
  10696. %@AS@%  short adapter;      /*active display adapter*/
  10697. %@AS@%  short monitor;      /*active display monitor*/
  10698. %@AS@%  short memory;       /*adapter video memory in K bytes*/%@AE@%%@NL@%
  10699. %@NL@%
  10700. These variables within the %@AB@%videoconfig%@AE@% structure are initialized when you
  10701. call %@AB@%_getvideoconfig%@AE@%.  %@NL@%
  10702. %@NL@%
  10703. %@NL@%
  10704. %@3@%%@CR:C6A00130004 @%%@AB@%Setting the Video Mode%@AE@%%@EH@%%@NL@%
  10705. %@NL@%
  10706. Before you can start drawing pictures on the screen, your program must tell
  10707. the graphics adapter to switch from video text mode to graphics mode. To do
  10708. this, call %@AB@%_setvideomode%@AE@%, passing it a single integer that tells it which
  10709. mode to display. The following constants are defined in the GRAPH.H file.
  10710. The dimensions are listed in pixels for graphics mode and in columns for
  10711. video text modes.  %@NL@%
  10712. %@NL@%
  10713. %@TH:  43  1718 02 16 30 30 @%Constant        Video Mode                    Mode Type/Hardware%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%_DEFAULTMODE %@AE@%   Restores to original mode     Both/All%@AB@%_ERESCOLOR%@AE@%      640 x  350, 4 or 16 color     Graphics/EGA%@AB@%_ERESNOCOLOR%@AE@%    640 x  350, BW                Graphics/EGA%@AB@%_HERCMONO%@AE@%       720 x 348, BW for HGC         Graphics/HGC%@AB@%_HRES16COLOR%@AE@%    640 x  200, 16 color          Graphics/EGA%@AB@%_HRESBW%@AE@%         640 x  200, BW                Graphics/CGA%@AB@%_MRES4COLOR%@AE@%     320 x  200, 4 color           Graphics/CGA%@AB@%_MRES16COLOR%@AE@%    320 x  200, 16 color          Graphics/EGA%@AB@%_MRES256COLOR%@AE@%   320 x  200, 256 color         Graphics/VGA/                                              MCGA%@AB@%_MRESNOCOLOR%@AE@%    320 x  200, 4 gray            Graphics/CGA%@AB@%_ORESCOLOR%@AE@%      640 x  400, 1 of 16 colors    Graphics/                                              Olivetti(R)%@AB@%_TEXTBW40%@AE@%       40-column text, 16 gray       Text/CGA%@AB@%_TEXTBW80%@AE@%       80-column text, 16 gray       Text/CGA%@AB@%_TEXTC40%@AE@%        40-column text, 16/8 color    Text/CGA%@AB@%_TEXTC80%@AE@%        80-column text, 16/8 color    Text/CGA%@AB@%_TEXTMONO%@AE@%       80-column text, BW            Text/MDA%@AB@%_VRES2COLOR%@AE@%     640 x  480, BW                Graphics/VGA/                                              MCGA%@AB@%_VRES16COLOR%@AE@%    640 x  480, 16 color          Graphics/VGA%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  43  1718 02 16 30 30 @%
  10714.  
  10715. If %@AB@%_setvideomode%@AE@% returns a 0, it means the hardware does not support the
  10716. selected mode. You may continue to select alternate video modes until a
  10717. nonzero value is returned. If the hardware configuration doesn't support any
  10718. of the selected video modes, take the appropriate exit action.  %@NL@%
  10719. %@NL@%
  10720. %@NL@%
  10721. %@3@%%@CR:C6A00130005 @%%@AB@%Writing a Graphics Program%@AE@%%@EH@%%@NL@%
  10722. %@NL@%
  10723. The SINE.C program below graphs a sine curve. The program illustrates how to
  10724. call many of the important graphics functions. The %@AB@%main%@AE@% function calls five
  10725. other functions, which are defined later in this chapter. To view the
  10726. complete program, use online help.  %@NL@%
  10727. %@NL@%
  10728. ────────────────────────────────────────────────────────────────────────────%@NL@%
  10729. %@AU@%WARNING%@AE@%%@NL@%
  10730. %@NL@%
  10731. When you installed QuickC on your system, you may have chosen not to include
  10732. the graphics library. If this is the case, the programs in this chapter
  10733. won't compile unless you explicitly link the graphics library. See the
  10734. %@AI@%Microsoft QuickC Tool Kit %@AE@%for information about linking libraries.%@NL@%
  10735. ────────────────────────────────────────────────────────────────────────────%@NL@%
  10736. %@NL@%
  10737. %@AS@%  
  10738. %@AS@%  /* SINE.C: Basic graphics commands. */
  10739. %@AS@%  
  10740. %@AS@%  #include <stdio.h>
  10741. %@AS@%  #include <stdlib.h>
  10742. %@AS@%  #include <graph.h>
  10743. %@AS@%  #include <math.h>
  10744. %@AS@%  #include <conio.h>
  10745. %@AS@%  #define PI 3.14159
  10746. %@AS@%  
  10747. %@AS@%  void graphics_mode( void );
  10748. %@AS@%  void draw_lines( void );
  10749. %@AS@%  void sine_wave( void );
  10750. %@AS@%  void draw_shapes( void );
  10751. %@AS@%  void end_program( void );
  10752. %@AS@%  int newx( int );
  10753. %@AS@%  int newy( int );
  10754. %@AS@%  
  10755. %@AS@%  struct videoconfig myscreen;
  10756. %@AS@%  int maxx, maxy;
  10757. %@AS@%  unsigned char diagmask[8] =
  10758. %@AS@%  { 0x93, 0xC9, 0x64, 0xB2, 0x59, 0x2C, 0x96, 0x4B };
  10759. %@AS@%  unsigned char linemask[8] =
  10760. %@AS@%  { 0xFF, 0x00, 0x7F, 0xFE, 0x00, 0x00, 0x00, 0xCC };
  10761. %@AS@%  
  10762. %@AS@%  
  10763. %@AS@%  main()
  10764. %@AS@%  {
  10765. %@AS@%     graphics_mode();
  10766. %@AS@%     draw_lines();
  10767. %@AS@%     sine_wave();
  10768. %@AS@%     draw_shapes();
  10769. %@AS@%     end_program();
  10770. %@AS@%  }
  10771. %@AS@%  /*
  10772. %@AS@%  Definitions of functions go here
  10773. %@AS@%  */%@AE@%%@NL@%
  10774. %@NL@%
  10775. The SINE.C program's output is shown in Figure 13.1.  %@NL@%
  10776. %@NL@%
  10777. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  10778. %@NL@%
  10779. %@NL@%
  10780. %@4@%%@AB@%Turning on Graphics Mode%@AE@%%@EH@%%@NL@%
  10781. %@NL@%
  10782. Before you can display graphics, you must put the graphics adapter into a
  10783. graphics mode. The %@AB@%_setvideomode%@AE@% function performs this task. Before calling
  10784. %@AB@%_setvideomode%@AE@%, you must decide which graphics modes are acceptable for your
  10785. purposes. The first function in SINE.C is named %@AS@% graphics_mode%@AE@%. It selects
  10786. the highest possible resolution available, based on the graphics card
  10787. currently in use.  %@NL@%
  10788. %@NL@%
  10789. Four header files are included in the SINE.C program:  %@NL@%
  10790. %@NL@%
  10791. %@AS@%  #include <stdio.h>%@AE@%%@NL@%
  10792. %@NL@%
  10793. %@AS@%  #include <stdlib.h>%@AE@%%@NL@%
  10794. %@NL@%
  10795. %@AS@%  #include <graph.h>%@AE@%%@NL@%
  10796. %@NL@%
  10797. %@AS@%  #include <math.h>%@AE@%%@NL@%
  10798. %@NL@%
  10799. Although the MATH.H file is not required for graphics programs, we include
  10800. it in the SINE.C program because it contains floating-point math functions
  10801. such as %@AB@%sin%@AE@%.  %@NL@%
  10802. %@NL@%
  10803. Later in the program we'll need to get information about the screen size, so
  10804. the %@AB@%videoconfig%@AE@% structure called %@AS@% myscreen %@AE@% is declared:  %@NL@%
  10805. %@NL@%
  10806. %@AS@%  struct videoconfig myscreen;%@AE@%%@NL@%
  10807. %@NL@%
  10808. The functions called by %@AB@%main%@AE@% aren't in the standard library; they're defined
  10809. within SINE.C.  %@NL@%
  10810. %@NL@%
  10811. The first function is %@AS@% graphics_mode%@AE@%, which turns on graphics capabilities:
  10812. %@NL@%
  10813. %@NL@%
  10814. %@AS@%  void graphics_mode( void )
  10815. %@AS@%  {
  10816. %@AS@%     _getvideoconfig( &myscreen );
  10817. %@AS@%     switch( myscreen.adapter )
  10818. %@AS@%     {
  10819. %@AS@%        case _CGA:
  10820. %@AS@%           _setvideomode( _HRESBW );
  10821. %@AS@%           break;
  10822. %@AS@%        case _OCGA:
  10823. %@AS@%           _setvideomode( _ORESCOLOR );
  10824. %@AS@%           break;
  10825. %@AS@%        case _EGA:
  10826. %@AS@%        case _OEGA:
  10827. %@AS@%           if( myscreen.monitor == _MONO )
  10828. %@AS@%              _setvideomode( _ERESNOCOLOR );
  10829. %@AS@%           else
  10830. %@AS@%              _setvideomode( _ERESCOLOR );
  10831. %@AS@%           break;
  10832. %@AS@%        case _VGA:
  10833. %@AS@%        case _OVGA:
  10834. %@AS@%        case _MCGA:
  10835. %@AS@%           _setvideomode( _VRES2COLOR );
  10836. %@AS@%           break;
  10837. %@AS@%        case _HGC:
  10838. %@AS@%           _setvideomode( _HERCMONO );
  10839. %@AS@%           break;
  10840. %@AS@%        default:
  10841. %@AS@%           printf( "This program requires a CGA, EGA, VGA, or Hercules
  10842. %@AS@%card\n" );
  10843. %@AS@%           exit( 0 );
  10844. %@AS@%     }
  10845. %@AS@%     _getvideoconfig( &myscreen );
  10846. %@AS@%     maxx = myscreen.numxpixels - 1;
  10847. %@AS@%     maxy = myscreen.numypixels - 1;
  10848. %@AS@%  }%@AE@%%@NL@%
  10849. %@NL@%
  10850. ────────────────────────────────────────────────────────────────────────────%@NL@%
  10851. NOTE
  10852.  
  10853. %@AI@%If you use a Hercules(R) adapter, you must run the MSHERC.COM program before
  10854. %@AI@%attempting to display any graphics. Always run MSHERC.COM before running
  10855. %@AI@%QuickC (do not run it from QuickC's DOS shell).%@AE@%%@NL@%
  10856. ────────────────────────────────────────────────────────────────────────────%@NL@%%@NL@%
  10857. %@NL@%
  10858. The function begins by calling %@AB@%_getvideoconfig%@AE@%, passing the address of the
  10859. %@AB@%videoconfig%@AE@% structure. Within the structure a member called %@AB@%adapter%@AE@% tells us
  10860. the type of adapter currently in use. With that knowledge, and a switch
  10861. statement, we can enter the appropriate graphics mode.  %@NL@%
  10862. %@NL@%
  10863. But how much screen do we have to work with? The screen might be 720 x 348,
  10864. 640 x 480, 640 x 400, 640 x 350, or 640 x 200. Whenever you call
  10865. %@AB@%_setvideomode%@AE@%, you can ask for information about the currently displayed
  10866. screen with %@AB@%_getvideoconfig%@AE@%. Just pass it the address of the %@AB@%videoconfig%@AE@%
  10867. structure that was declared earlier:%@AB@%  %@AE@%%@NL@%
  10868. %@NL@%
  10869. %@AS@%  _getvideoconfig( &myscreen );
  10870. %@AS@%  maxx = myscreen.numxpixels - 1;
  10871. %@AS@%  maxy = myscreen.numypixels - 1;%@AE@%%@NL@%
  10872. %@NL@%
  10873. Let's say your computer has an EGA card, which means that at this point,
  10874. %@AB@%_ERESNOCOLOR%@AE@% is in effect. The horizontal screen size is 640 pixels and
  10875. vertical screen size is 350. The two assignments above assign these values
  10876. to %@AS@% maxx %@AE@% and %@AS@% maxy%@AE@%, less 1. The horizontal resolution might be 640, but the
  10877. pixels are numbered 0-639. Thus, the %@AS@% maxx %@AE@% variable─the highest available
  10878. pixel number─must be 1 less than the total number of pixels:  %@NL@%
  10879. %@NL@%
  10880. %@AS@%  myscreen.numxpixels - 1%@AE@%%@NL@%
  10881. %@NL@%
  10882. Two short functions perform conversions from an imaginary 1000 x 1000 screen
  10883. to whatever graphics mode is in effect. From this point forward, the program
  10884. will assume it has 1000 pixels in each direction, passing the values to %@AS@%
  10885. %@AS@%newx %@AE@% and %@AS@% newy %@AE@% for conversion to actual coordinates:  %@NL@%
  10886. %@NL@%
  10887. %@AS@%  int newx( int xcoord )
  10888. %@AS@%  {
  10889. %@AS@%     int nx;
  10890. %@AS@%     float tempx;
  10891. %@AS@%     tempx = ((float)maxx)/ 1000.0;
  10892. %@AS@%     tempx = ((float)xcoord) * tempx + 0.5;
  10893. %@AS@%     return( (int)tempx );
  10894. %@AS@%  }
  10895. %@AS@%  
  10896. %@AS@%  int newy( int ycoord )
  10897. %@AS@%  {
  10898. %@AS@%     int ny;
  10899. %@AS@%     float tempy;
  10900. %@AS@%     tempy = ((float)maxy)/ 1000.0;
  10901. %@AS@%     tempy = ((float)ycoord) * tempy + 0.5;
  10902. %@AS@%     return( (int)tempy );
  10903. %@AS@%  }%@AE@%%@NL@%
  10904. %@NL@%
  10905. %@NL@%
  10906. %@4@%%@AB@%Drawing Rectangles and Lines%@AE@%%@EH@%%@NL@%
  10907. %@NL@%
  10908. The next function called in SINE.C is %@AS@% draw_lines%@AE@%. As the name implies, the %@AS@%
  10909. %@AS@%draw_lines %@AE@% function draws several lines on the screen: a rectangle around
  10910. the outer edges of the screen and three horizontal lines that cut the screen
  10911. into quarters.  %@NL@%
  10912. %@NL@%
  10913. %@AS@%  void draw_lines( void )
  10914. %@AS@%  {
  10915. %@AS@%     _rectangle( _GBORDER, 0, 0, maxx, maxy );
  10916. %@AS@%     /* _setcliprgn( 20, 20, maxx - 20, maxy - 20 ); */
  10917. %@AS@%     _setvieworg( 0, newy( 500 ) );
  10918. %@AS@%  
  10919. %@AS@%     _moveto( 0, 0 );
  10920. %@AS@%     _lineto( newx( 1000 ), 0 );
  10921. %@AS@%     _setlinestyle( 0xAA3C );
  10922. %@AS@%     _moveto( 0, newy( -250) );
  10923. %@AS@%     _lineto( newx( 1000 ), newy( -250 ) );
  10924. %@AS@%     _setlinestyle( 0x8888 );
  10925. %@AS@%     _moveto( 0, newy( 250 ) );
  10926. %@AS@%     _lineto( newx( 1000 ), newy( 250 ) );
  10927. %@AS@%  }%@AE@%%@NL@%
  10928. %@NL@%
  10929. The call to the%@AB@% _rectangle%@AE@% function has five arguments. The first argument
  10930. is the fill flag, which may be either %@AB@%_GBORDER%@AE@% or %@AB@%_GFILLINTERIOR%@AE@%. Choose
  10931. %@AB@%_GBORDER%@AE@% if you want a rectangle of four lines (a border only, in the
  10932. current line style). Or you can choose %@AB@%_GFILLINTERIOR%@AE@% if you want a solid
  10933. rectangle (filled in with the current color and fill pattern). We will
  10934. discuss how to choose the color and fill pattern later in this chapter.  %@NL@%
  10935. %@NL@%
  10936. The second and third arguments are the %@AI@%x%@AE@% and %@AI@%y%@AE@% coordinates of one corner of
  10937. the rectangle. The fourth and fifth arguments are the coordinates of the
  10938. opposite corner. Since the coordinates for the two corners are  (%@AS@% 0, 0 %@AE@%) and
  10939. (%@AS@% maxx%@AE@%, %@AS@%maxy %@AE@%) , the call to %@AB@%_rectangle%@AE@% frames the screen.  %@NL@%
  10940. %@NL@%
  10941. %@AS@%  _rectangle( _GBORDER, 0, 0, maxx, maxy );%@AE@%%@NL@%
  10942. %@NL@%
  10943. Drawing lines is a two-step process. Move to one location on the screen and
  10944. draw the line to another location, using the %@AB@%_moveto%@AE@% and %@AB@%_lineto%@AE@% functions:
  10945. %@NL@%
  10946. %@NL@%
  10947. %@AS@%  _setlinestyle( 0xAA3C );
  10948. %@AS@%  _moveto( 0, newy(-250) );
  10949. %@AS@%  _lineto( newx(1000), newy(-250) );%@AE@%%@NL@%
  10950. %@NL@%
  10951. Use the %@AB@%_setlinestyle%@AE@% function to change from a solid line to a dashed line
  10952. by passing it one integer value. In the example above, the number 0xAA3C
  10953. causes the line to become the graphics equivalent of binary 1010 1010 0011
  10954. 1100.  %@NL@%
  10955. %@NL@%
  10956. The %@AB@%_moveto%@AE@% function positions an imaginary pixel cursor at a spot on the
  10957. screen. Nothing visible appears on the screen. The %@AB@%_lineto%@AE@% function draws a
  10958. line. The negative value -250 might seem to be an impossible screen
  10959. coordinate. It would be, but the program has changed the viewport
  10960. organization of the screen with the %@AB@%_setvieworg%@AE@% function. The top half of
  10961. the screen now contains negative %@AI@%y%@AE@% coordinates, and the bottom half contains
  10962. positive %@AI@%y%@AE@% coordinates. Viewports are explained in more detail later in this
  10963. chapter.  %@NL@%
  10964. %@NL@%
  10965. %@NL@%
  10966. %@4@%%@AB@%Setting a Pixel%@AE@%%@EH@%%@NL@%
  10967. %@NL@%
  10968. The next step in the SINE.C program is to draw the sine curve. This requires
  10969. the %@AS@% sine_wave %@AE@% function which is shown below. This function calculates
  10970. positions for two sine waves and plots them on the screen:  %@NL@%
  10971. %@NL@%
  10972. %@AS@%  void sine_wave( void )
  10973. %@AS@%  {
  10974. %@AS@%   int locx, locy;
  10975. %@AS@%    double i, rad;
  10976. %@AS@%  
  10977. %@AS@%   for( i = 0; i < 1000; i += 3 )
  10978. %@AS@%     {
  10979. %@AS@%        rad = -sin( (PI * (float) i) / 250.0 );
  10980. %@AS@%        locx = newx( (int) i );
  10981. %@AS@%        locy = newy( (int) (rad * 250.0) );
  10982. %@AS@%        _setpixel( locx, locy );
  10983. %@AS@%     }
  10984. %@AS@%  }%@AE@%%@NL@%
  10985. %@NL@%
  10986. The only graphics function called is %@AB@%_setpixel%@AE@%, which takes two parameters,
  10987. an %@AI@%x%@AE@% and a %@AI@%y%@AE@% coordinate. The function turns on the pixel at that location.  %@NL@%
  10988. %@NL@%
  10989. %@NL@%
  10990. %@4@%%@AB@%Drawing Shapes%@AE@%%@EH@%%@NL@%
  10991. %@NL@%
  10992. After the sine curve is drawn, the SINE.C program calls the %@AS@% draw_shapes %@AE@%
  10993. function to draw two rectangles and two ellipses on the screen. The fill
  10994. mask alternates between %@AB@%_GBORDER%@AE@% and %@AB@%_GFILLINTERIOR%@AE@%:  %@NL@%
  10995. %@NL@%
  10996. %@AS@%  void draw_shapes( void )
  10997. %@AS@%  {
  10998. %@AS@%     _setlinestyle( 0xFFFF );
  10999. %@AS@%     _setfillmask( diagmask );
  11000. %@AS@%     _rectangle( _GBORDER, newx( 50 ), newy( -325 ), newx( 200 ), newy( -425
  11001. %@AS@%) );
  11002. %@AS@%     _rectangle(_GFILLINTERIOR,newx(550),newy(-325),newx(700),newy(-425));
  11003. %@AS@%  
  11004. %@AS@%     _setfillmask( linemask );
  11005. %@AS@%     _ellipse( _GBORDER, newx( 50 ), newy( 325 ), newx( 200 ), newy( 425 )
  11006. %@AS@%);
  11007. %@AS@%     _ellipse( _GFILLINTERIOR,newx( 550 ),newy( 325 ),newx( 700 ),newy( 425
  11008. %@AS@%) );
  11009. %@AS@%  }%@AE@%%@NL@%
  11010. %@NL@%
  11011. Note that %@AB@%_setlinestyle%@AE@% resets the line pattern to solid. If you omit this
  11012. function (or comment it out), the first rectangle would be drawn with dashes
  11013. instead of a solid line.  %@NL@%
  11014. %@NL@%
  11015. The %@AB@%_ellipse%@AE@% function draws an ellipse on the screen. Its parameters
  11016. resemble the parameters for %@AB@%_rectangle%@AE@%. Both functions require a fill flag
  11017. and two corners of a "bounding rectangle." When the ellipse is drawn, four
  11018. points touch the edges of the bounding rectangle.  %@NL@%
  11019. %@NL@%
  11020. The %@AB@%_GFILLINTERIOR%@AE@% flag fills the shape with the current fill pattern. To
  11021. select a pattern, you must first use the %@AB@%_setfillmask%@AE@% function, passing the
  11022. address of an eight-byte array of unsigned characters. Earlier in the
  11023. program %@AS@% diagmask %@AE@% was defined as the shape shown in Table 13.1 below.  %@NL@%
  11024. %@NL@%
  11025. %@AB@%Table 13.1  %@AB@%Fill Patterns%@AE@%%@AE@%
  11026.  
  11027. %@TH:  11   790 02 34 42 @%Bit Pattern                       Value in diagmask%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%  ∙  ∙    ∙  ∙                %@AS@%diagmask[0] %@AE@%= 0x93    ∙  ∙    ∙  ∙              %@AS@%diagmask[1] %@AE@%= 0xC9∙      ∙  ∙    ∙  ∙            %@AS@%diagmask[2] %@AE@%= 0x64  ∙      ∙  ∙    ∙            %@AS@%diagmask[3] %@AE@%= 0xB2∙    ∙      ∙  ∙              %@AS@%diagmask[4] %@AE@%= 0x59∙  ∙    ∙      ∙  ∙            %@AS@%diagmask[5] %@AE@%= 0x2C  ∙  ∙    ∙      ∙            %@AS@%diagmask[6] %@AE@%= 0x96∙    ∙  ∙    ∙                %@AS@%diagmask[7] %@AE@%= 0x4B%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  11   790 02 34 42 @%
  11028.  
  11029. %@NL@%
  11030. %@4@%%@AB@%Exiting Graphics Mode%@AE@%%@EH@%%@NL@%
  11031. %@NL@%
  11032. The final function to be called by the SINE.C program is %@AS@% end_program%@AE@%, which
  11033. waits for a key press and then sets the screen back to normal:  %@NL@%
  11034. %@NL@%
  11035. %@AS@%  void end_program( void )
  11036. %@AS@%  {
  11037. %@AS@%     getch();
  11038. %@AS@%     _setvideomode( _DEFAULTMODE );
  11039. %@AS@%  }%@AE@%%@NL@%
  11040. %@NL@%
  11041. %@NL@%
  11042. %@3@%%@CR:C6A00130006 @%%@AB@%Using Color Graphics Modes%@AE@%%@EH@%%@NL@%
  11043. %@NL@%
  11044. In this example, the program COLOR.C sets a mode with as many color choices
  11045. as possible for the available hardware:  %@NL@%
  11046. %@NL@%
  11047. %@AS@%  /* COLOR.C: Sets a medium resolution mode
  11048. %@AS@%     with maximum color choices. */
  11049. %@AS@%  
  11050. %@AS@%  #include <stdio.h>
  11051. %@AS@%  #include <stdlib.h>
  11052. %@AS@%  #include <graph.h>
  11053. %@AS@%  #include <conio.h>
  11054. %@AS@%  struct videoconfig vc;
  11055. %@AS@%  
  11056. %@AS@%  main()
  11057. %@AS@%  {
  11058. %@AS@%     if( _setvideomode( _MRES256COLOR ) );
  11059. %@AS@%     else if( _setvideomode( _MRES16COLOR ) );
  11060. %@AS@%     else if( _setvideomode( _MRES4COLOR ) );
  11061. %@AS@%     else
  11062. %@AS@%     {
  11063. %@AS@%        printf( "Error: No color graphics capability\n" );
  11064. %@AS@%        exit( 0 );
  11065. %@AS@%     }
  11066. %@AS@%  
  11067. %@AS@%     _getvideoconfig( &vc );
  11068. %@AS@%  
  11069. %@AS@%     printf( "%d available colors\n", vc.numcolors );
  11070. %@AS@%     printf( "%d horizontal pixels\n", vc.numxpixels );
  11071. %@AS@%     printf( "%d vertical pixels\n", vc.numypixels );
  11072. %@AS@%  
  11073. %@AS@%     getch();
  11074. %@AS@%     _clearscreen( _GCLEARSCREEN );
  11075. %@AS@%     _setvideomode( _DEFAULTMODE );
  11076. %@AS@%  }%@AE@%%@NL@%
  11077. %@NL@%
  11078. Although color graphics are an improvement over black and white, if you use
  11079. color you must make a compromise. When you request the maximum number of
  11080. colors, you sacrifice some resolution─a 320 x 200 screen instead of a higher
  11081. resolution. Thus, the COLORS.C program always creates a screen 320 pixels
  11082. wide and 200 pixels high. Note also the use of the function %@AB@%_clearscreen%@AE@%,
  11083. which clears the screen in any video mode (text or graphics).  %@NL@%
  11084. %@NL@%
  11085. To view every possible graphics mode, you can run the program GRAPHIC.C
  11086. shown below. Explanations of the various color graphics modes─CGA, EGA, and
  11087. VGA─follow.  %@NL@%
  11088. %@NL@%
  11089. %@AS@%  /* GRAPHIC.C: Display every graphics mode. */
  11090. %@AS@%  #include <stdio.h>
  11091. %@AS@%  #include <graph.h>
  11092. %@AS@%  #include <conio.h>
  11093. %@AS@%  
  11094. %@AS@%  struct videoconfig screen;
  11095. %@AS@%  int modes[12] =
  11096. %@AS@%  {
  11097. %@AS@%     _MRES4COLOR, _MRESNOCOLOR, _HRESBW, _HERCMONO,
  11098. %@AS@%     _MRES16COLOR, _HRES16COLOR, _ERESNOCOLOR, _ERESCOLOR,
  11099. %@AS@%     _VRES2COLOR, _VRES16COLOR, _MRES256COLOR, _ORESCOLOR
  11100. %@AS@%  };
  11101. %@AS@%  
  11102. %@AS@%  void print_menu( void );
  11103. %@AS@%  void show_mode( char );
  11104. %@AS@%  
  11105. %@AS@%  main()
  11106. %@AS@%  {
  11107. %@AS@%     char key;
  11108. %@AS@%     print_menu();
  11109. %@AS@%     while( (key = getch()) != 'x' )
  11110. %@AS@%        show_mode( key );
  11111. %@AS@%  }
  11112. %@AS@%  
  11113. %@AS@%  void print_menu( void )
  11114. %@AS@%  {
  11115. %@AS@%     _setvideomode( _DEFAULTMODE );
  11116. %@AS@%     _clearscreen( _GCLEARSCREEN );
  11117. %@AS@%     printf( "Please choose a graphics mode\nType 'x' to exit.\n\n" );
  11118. %@AS@%     printf( "0 _MRES4COLOR\n1 _MRESNOCOLOR\n2 _HRESBW\n" );
  11119. %@AS@%     printf( "3 _HERCMONO\n4 _MRES16COLOR\n5 _HRES16COLOR\n" );
  11120. %@AS@%     printf( "6 _ERESNOCOLOR\n7 _ERESCOLOR\n" );
  11121. %@AS@%     printf( "8 _VRES2COLOR\n9 _VRES16COLOR\na _MRES256COLOR\n" );
  11122. %@AS@%     printf( "b _ORESCOLOR\n" );
  11123. %@AS@%  }
  11124. %@AS@%  
  11125. %@AS@%  void show_mode( char which )
  11126. %@AS@%  {
  11127. %@AS@%     int nc, i;
  11128. %@AS@%     int height, width;
  11129. %@AS@%     int mode = which;
  11130. %@AS@%  
  11131. %@AS@%     if( mode < '0' || mode > '9' )
  11132. %@AS@%        if( mode == 'a' )
  11133. %@AS@%      mode = '9' + 1;
  11134. %@AS@%        else if( mode == 'b' )
  11135. %@AS@%      mode = '9' + 2;%@AE@%%@NL@%
  11136. %@NL@%
  11137. %@AS@%  else
  11138. %@AS@%      return;
  11139. %@AS@%  
  11140. %@AS@%     if( _setvideomode( modes[mode - '0'] ) )
  11141. %@AS@%     {
  11142. %@AS@%        _getvideoconfig( &screen );
  11143. %@AS@%        nc = screen.numcolors;
  11144. %@AS@%        width = screen.numxpixels/nc;
  11145. %@AS@%        height = screen.numypixels/2;
  11146. %@AS@%        for( i = 0; i < nc; i++ )
  11147. %@AS@%        {
  11148. %@AS@%      _setcolor( i );
  11149. %@AS@%      _rectangle( _GFILLINTERIOR, i * width, 0, (i + 1) * width, height );
  11150. %@AS@%        }
  11151. %@AS@%     }
  11152. %@AS@%     else
  11153. %@AS@%     {
  11154. %@AS@%        printf( " \nVideo mode %c is not available.\n", which );
  11155. %@AS@%        printf( "Please press a key.\n" );
  11156. %@AS@%     }
  11157. %@AS@%     getch();
  11158. %@AS@%     _setvideomode( _DEFAULTMODE );
  11159. %@AS@%     print_menu();
  11160. %@AS@%  }%@AE@%%@NL@%
  11161. %@NL@%
  11162. %@NL@%
  11163. %@4@%%@AB@%CGA Color Graphics Modes%@AE@%%@EH@%%@NL@%
  11164. %@NL@%
  11165. The CGA color graphics modes %@AB@%_MRES4COLOR%@AE@% and %@AB@%_MRESNOCOLOR%@AE@% display four
  11166. colors selected from one of several predefined palettes of colors. They
  11167. display these foreground colors against a background color which can be any
  11168. one of the 16 available colors. With the CGA hardware, the palette of
  11169. foreground colors is predefined and cannot be changed. Each palette number
  11170. is an integer as shown in Table 13.2.  %@NL@%
  11171. %@NL@%
  11172. %@AB@%Table 13.2  %@AB@%Available CGA Colors%@AE@%%@AE@%
  11173.  
  11174. %@TH:   8   529 03 18 14 16 28 @%Palette Number    Color Index                     1             2               3%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%0                 Green         Red             Brown1                 Cyan          Magenta         Light gray2                 Light green   Light red       Yellow3                 Light cyan    Light magenta   White%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   8   529 03 18 14 16 28 @%
  11175.  
  11176. The %@AB@%_MRESNOCOLOR%@AE@% graphics mode produces palettes containing various shades
  11177. of gray on black-and-white monitors. The %@AB@%_MRESNOCOLOR%@AE@% mode displays colors
  11178. when used with a color display. However, only two palettes  %@NL@%
  11179. %@NL@%
  11180. are available with a color display. You can use the %@AB@%_selectpalette%@AE@% function
  11181. to select one of these predefined palettes. Table 13.3 shows the
  11182. correspondence between the color indexes and the palettes.  %@NL@%
  11183. %@NL@%
  11184. %@AB@%Table 13.3  %@AB@%CGA Colors:  _MRESNOCOLOR Mode%@AE@%%@AE@%
  11185.  
  11186. %@TH:   6   404 03 18 13 12 33 @%Palette Number    Color Index                    1            2           3%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%0                 Blue         Red         Light gray1                 Light blue   Light red   White%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   6   404 03 18 13 12 33 @%
  11187.  
  11188. You may use the %@AB@%_selectpalette%@AE@% function only with the %@AB@%_MRES4COLOR%@AE@% and
  11189. %@AB@%_MRESNOCOLOR%@AE@% graphics modes. To change palettes in other graphics modes, use
  11190. the %@AB@%_remappalette%@AE@% or %@AB@%_remapallpalette%@AE@% functions.  %@NL@%
  11191. %@NL@%
  11192. The following program sets the video mode to %@AB@%_MRES4COLOR%@AE@% and then cycles
  11193. through background colors and palette combinations. It works on computers
  11194. equipped with CGA, EGA, MCGA, or VGA cards. A color monitor is required.  %@NL@%
  11195. %@NL@%
  11196. %@AS@%  /* CGA.C: Demonstrate CGA colors. */
  11197. %@AS@%  
  11198. %@AS@%  #include <stdio.h>
  11199. %@AS@%  #include <graph.h>
  11200. %@AS@%  #include <conio.h>
  11201. %@AS@%  
  11202. %@AS@%  long bkcolor[8] = 
  11203. %@AS@%  {
  11204. %@AS@%     _BLACK, _BLUE, _GREEN, _CYAN,
  11205. %@AS@%    _RED, _MAGENTA, _BROWN, _WHITE
  11206. %@AS@%  };
  11207. %@AS@%  
  11208. %@AS@%  char *bkcolor_name[] =
  11209. %@AS@%  {
  11210. %@AS@%     "_BLACK", "_BLUE", "_GREEN", "_CYAN",
  11211. %@AS@%     "_RED", "_MAGENTA", "_BROWN", "_WHITE"
  11212. %@AS@%  };%@AE@%%@NL@%
  11213. %@NL@%
  11214. %@AS@%  main()
  11215. %@AS@%  {
  11216. %@AS@%     int i, j, k;
  11217. %@AS@%     _setvideomode( _MRES4COLOR );
  11218. %@AS@%     for( i=0; i<= 3; i++ )
  11219. %@AS@%     {
  11220. %@AS@%        _selectpalette( i );
  11221. %@AS@%        for( k=0; k <= 7; k++ )
  11222. %@AS@%        {
  11223. %@AS@%           _setbkcolor( bkcolor[k] );
  11224. %@AS@%           for( j=0; j<=3; j++ )
  11225. %@AS@%           {
  11226. %@AS@%              _settextposition( 1, 1 );
  11227. %@AS@%              printf( "background color: %8s\n", bkcolor_name[k] );
  11228. %@AS@%              printf( "palette: %d\ncolor: %d\n", i, j );
  11229. %@AS@%              _setcolor( j );
  11230. %@AS@%              _rectangle( _GFILLINTERIOR, 160, 100, 320, 200 );
  11231. %@AS@%               getch();
  11232. %@AS@%           }
  11233. %@AS@%        }
  11234. %@AS@%     }
  11235. %@AS@%     _setvideomode( _DEFAULTMODE );
  11236. %@AS@%  }%@AE@%%@NL@%
  11237. %@NL@%
  11238. %@NL@%
  11239. %@4@%%@AB@%EGA, MCGA, and VGA Palettes%@AE@%%@EH@%%@NL@%
  11240. %@NL@%
  11241. At the beginning of this chapter, we mentioned the difference between color
  11242. indexes and color values. An analogy might make things clearer. Imagine a
  11243. painter who owns 64 tubes of paint and a painter's palette that has room for
  11244. only 16 globs of paint at any one time. A painting created under these
  11245. constraints could contain only 16 colors (selected from a total of 64). One
  11246. of the EGA graphics modes (%@AB@%_ERESCOLOR%@AE@%) is similar: 16 color indexes chosen
  11247. from a total of 64 color values. (Color indexes are sometimes called "color
  11248. attributes," or "pixel values." Color values are sometimes called "actual
  11249. colors.")  %@NL@%
  11250. %@NL@%
  11251. %@AB@%VGA Color Mixing%@AE@% - VGA offers the widest variety of color values: 262,144
  11252. (256K). Depending on the graphics mode, the VGA palette size may be 2, 16,
  11253. or 256. When you select a color value, you specify a level of intensity
  11254. ranging from 0-63 for each of the red, green, and blue color values. The
  11255. long integer that defines a color value consists of four bytes (32 bits):%@NL@%
  11256. %@NL@%
  11257. %@AS@%  MSB                             LSB
  11258. %@AS@%  zzzzzzzz zzBBBBBB zzGGGGGG zzRRRRRR%@AE@%%@NL@%
  11259. %@NL@%
  11260. The most-significant byte must contain all zeros. The two high bits in the
  11261. remaining three bytes must also be 0. To mix a light red (pink), turn red
  11262. all the way up, and mix in some green and blue:  %@NL@%
  11263. %@NL@%
  11264. %@AS@%  00000000 00100000 00100000 00111111%@AE@%%@NL@%
  11265. %@NL@%
  11266. To represent this value in hexadecimal, use the number %@AS@% 0x0020203FL%@AE@% (the %@AS@% L %@AE@%
  11267. marks it as a long value). You could also use the following macro:  %@NL@%
  11268. %@NL@%
  11269. %@AS@%  #define RGB ( r, g, b ) (0x3F3F3FL & ((long)(b) << 16 | (g) << 8 | (r)))%@AE@%%@NL@%
  11270. %@NL@%
  11271. To create pure yellow (100% red plus 100% green) and assign it to a variable
  11272. %@AS@% y1%@AE@%, use this line:  %@NL@%
  11273. %@NL@%
  11274. %@AS@%  y1 = RGB( 63, 63, 0 );%@AE@%%@NL@%
  11275. %@NL@%
  11276. For white, turn all the colors on: %@AS@% RGB( 63, 63, 63 )%@AE@%. For black, set all
  11277. colors to 0: %@AS@% RGB( 0, 0, 0 )%@AE@%.  %@NL@%
  11278. %@NL@%
  11279. %@AB@%EGA Color Mixing%@AE@% - Mixing colors in EGA modes is similar to the mixing
  11280. described above, but there are fewer intensities for the red, green, and
  11281. blue components. In the modes that offer 64 colors, the R, G, and B values
  11282. cover 2 bits and can range from 0 to 3. The long integer that defines an RGB
  11283. color looks like this:%@NL@%
  11284. %@NL@%
  11285. %@AS@%  MSB                             LSB
  11286. %@AS@%  zzzzzzzz zzBB???? zzGG???? zzRR????%@AE@%%@NL@%
  11287. %@NL@%
  11288. The bits marked %@AS@% z %@AE@% must be zeros and the bits marked with question marks
  11289. can be any value. To form a pure red color value, you would use the constant
  11290. %@AS@% 0x00000030L%@AE@%. For cyan (blue plus green), use %@AS@% 0x00303000L%@AE@%. The RGB macro
  11291. defined above is easily modified for EGA monitors:  %@NL@%
  11292. %@NL@%
  11293. %@AS@%  #define EGARGB( r, g, b ) (0x3F3F3FL & ((long)(b) << 20 | (g) << 12 | (r
  11294. %@AS@%  << 4)))%@AE@%%@NL@%
  11295. %@NL@%
  11296. In this macro, you would pass values in the range 0-3 instead of 0-63.  %@NL@%
  11297. %@NL@%
  11298. %@NL@%
  11299. %@4@%%@AB@%EGA Color Graphics Modes%@AE@%%@EH@%%@NL@%
  11300. %@NL@%
  11301. The %@AB@%_MRES16COLOR%@AE@%, %@AB@%_HRES16COLOR%@AE@%, or %@AB@%_ERESCOLOR%@AE@% video modes display the best
  11302. color graphics with an EGA adapter. The CGA modes will also display on the
  11303. EGA but with the lower CGA resolution and decreased color options.  %@NL@%
  11304. %@NL@%
  11305. The %@AB@%_remappalette%@AE@% function assigns a new color value to a color index. For
  11306. example, when you first enter an EGA graphics mode, color index 1 equals the
  11307. color value blue. To reassign the pure red color value to color index 1, you
  11308. could use this line:  %@NL@%
  11309. %@NL@%
  11310. %@AS@%  _remappalette( 1, 0x000030L );%@AE@%%@NL@%
  11311. %@NL@%
  11312. Or, use the symbolic constant %@AB@%_RED%@AE@%, which is defined in the %@AS@% GRAPH.H %@AE@% file:
  11313. %@NL@%
  11314. %@NL@%
  11315. %@AS@%  _remappalette( 1, _RED );%@AE@%%@NL@%
  11316. %@NL@%
  11317. After this function call, any object currently drawn in color index 1 will
  11318. instantly switch from blue to red.  %@NL@%
  11319. %@NL@%
  11320. For EGA graphics, the first value is an integer in the range 0-15 and the
  11321. second value is a%@AB@% long int%@AE@% defined as a mixture of red, green, and blue (you
  11322. may also use the symbolic constants such as %@AB@%_RED%@AE@%).  %@NL@%
  11323. %@NL@%
  11324. The %@AB@%_remapallpalette%@AE@% function changes all of the color indexes
  11325. simultaneously. You pass it an array of color values. The first color value
  11326. in the list becomes the new color associated with the color index 0.  %@NL@%
  11327. %@NL@%
  11328. The number in a function call to set the color (such as %@AB@%_setcolor%@AE@%) is an
  11329. index into the palette of available colors. In the default text palette, an
  11330. index of 1 refers to blue but the palette could be remapped to change index
  11331. 1 to any other available color. As a result, the color produced by that
  11332. pixel value also changes. The number of color indexes depends on the number
  11333. of colors supported by the current video mode.  %@NL@%
  11334. %@NL@%
  11335. The %@AB@%_remappalette%@AE@% and %@AB@%_remapallpalette%@AE@% functions work in all modes but only
  11336. with the EGA, MCGA, or VGA hardware. The %@AB@%_remappalette%@AE@% and %@AB@%_remapallpalette%@AE@%
  11337. functions fail and return a value of -1 when you attempt to remap a palette
  11338. without the EGA, MCGA, or VGA hardware.  %@NL@%
  11339. %@NL@%
  11340. The following program draws a rectangle with a red interior. In the default
  11341. EGA palette, color index 4 is red. This color index is changed to %@AB@%_BLUE%@AE@% in
  11342. this program.  %@NL@%
  11343. %@NL@%
  11344. %@AS@%  /* EGA.C: EGA palettes. */
  11345. %@AS@%  
  11346. %@AS@%  #include <stdio.h>
  11347. %@AS@%  #include <conio.h>
  11348. %@AS@%  #include <graph.h>
  11349. %@AS@%  
  11350. %@AS@%  main()
  11351. %@AS@%  {
  11352. %@AS@%     _setvideomode( _ERESCOLOR );
  11353. %@AS@%     _setcolor( 4 );
  11354. %@AS@%     _rectangle( _GFILLINTERIOR, 50, 50, 200, 200 );
  11355. %@AS@%  
  11356. %@AS@%     _settextposition( 1, 1 );
  11357. %@AS@%     printf( "Normal palette\n" );
  11358. %@AS@%     printf( "Press a key" );
  11359. %@AS@%     getch();
  11360. %@AS@%  
  11361. %@AS@%     _remappalette( 4, _BLUE );
  11362. %@AS@%  
  11363. %@AS@%     _settextposition( 1, 1 );
  11364. %@AS@%     printf( "Remapped palette\n" );
  11365. %@AS@%     printf( "Press a key" );
  11366. %@AS@%     getch();
  11367. %@AS@%  
  11368. %@AS@%     _remappalette( 4, _RED );
  11369. %@AS@%  
  11370. %@AS@%     _settextposition( 1, 1 );
  11371. %@AS@%     printf( "Restored palette\n" );
  11372. %@AS@%     printf( "Press a key to clear the screen" );
  11373. %@AS@%     getch();
  11374. %@AS@%  
  11375. %@AS@%     _clearscreen( _GCLEARSCREEN );
  11376. %@AS@%     _setvideomode( _DEFAULTMODE );
  11377. %@AS@%  }%@AE@%%@NL@%
  11378. %@NL@%
  11379. %@NL@%
  11380. %@4@%%@AB@%VGA Color Graphics Modes%@AE@%%@EH@%%@NL@%
  11381. %@NL@%
  11382. The VGA card adds graphics modes %@AB@%_VRES2COLOR%@AE@%, %@AB@%_VRES16COLOR%@AE@%, and
  11383. %@AB@%_MRES256COLOR%@AE@% to your repertoire. EGA and CGA modes can also be used with
  11384. the VGA hardware, but with either lower resolution or fewer color choices.  %@NL@%
  11385. %@NL@%
  11386. The VGA color graphics modes operate with a range of 262,144 (256K)  color
  11387. values. The %@AB@%_VRES2COLOR%@AE@% graphics mode displays two colors, the %@AB@%_VRES16COLOR%@AE@%
  11388. graphics mode displays 16, and the %@AB@%_MRES256COLOR%@AE@% graphics mode displays 256
  11389. colors from the available VGA colors.  %@NL@%
  11390. %@NL@%
  11391. %@AB@%Changing the Palette%@AE@% - The %@AB@%_remappalette%@AE@% function changes a color index to a
  11392. specified color value. The function below remaps the color index 1 to the
  11393. color value given by the symbolic constant %@AB@%_RED%@AE@% (which represents red).
  11394. After this statement is executed, whatever was displayed as blue will now
  11395. appear as red:%@NL@%
  11396. %@NL@%
  11397. %@AS@%  _remappalette( 1, _RED );  /*reassign color index 1
  11398. %@AS@%                               to VGA red */%@AE@%%@NL@%
  11399. %@NL@%
  11400. Use the %@AB@%_remapallpalette%@AE@% function to remap all of the available color
  11401. indexes simultaneously. The function's argument references an array of color
  11402. values that reflects the remapping. The first color number in the list
  11403. becomes the new color associated with color index 0.  %@NL@%
  11404. %@NL@%
  11405. Symbolic constants for the default color numbers are supplied so that the
  11406. remapping of VGA colors is compatible with EGA practice. The names of these
  11407. constants are self-explanatory. For example, the color numbers for black,
  11408. red, and light yellow are represented by the symbolic constants %@AB@%_BLACK%@AE@%,
  11409. %@AB@%_RED%@AE@%, and %@AB@%_LIGHTYELLOW%@AE@%.  %@NL@%
  11410. %@NL@%
  11411. All of the VGA display modes operate with any VGA video monitor. Colors are
  11412. displayed as shades of gray when the monochrome analog display is connected.
  11413. %@NL@%
  11414. %@NL@%
  11415. If you have a VGA card, the HORIZON.C program illustrates what can be done
  11416. with the range of 256 colors:  %@NL@%
  11417. %@NL@%
  11418. %@AS@%  /* HORIZON.C: VGA graphics with cycling of 256 colors. */
  11419. %@AS@%  
  11420. %@AS@%  #include <stdio.h>
  11421. %@AS@%  #include <stdlib.h>
  11422. %@AS@%  #include <conio.h>
  11423. %@AS@%  #include <graph.h>
  11424. %@AS@%  
  11425. %@AS@%  #define RED 0x0000003FL
  11426. %@AS@%  #define GRN 0x00003F00L
  11427. %@AS@%  #define BLU 0x003F0000L
  11428. %@AS@%  #define WHT 0x003F3F3FL
  11429. %@AS@%  #define STEP 21
  11430. %@AS@%  
  11431. %@AS@%  struct videoconfig screen;
  11432. %@AS@%  long int rainbow[512];
  11433. %@AS@%  
  11434. %@AS@%  main()
  11435. %@AS@%  {
  11436. %@AS@%     int i;
  11437. %@AS@%     long int col, gray;
  11438. %@AS@%  
  11439. %@AS@%     if( _setvideomode( _MRES256COLOR ) == 0 )
  11440. %@AS@%     {
  11441. %@AS@%        printf( "This program requires a VGA card.\n" );
  11442. %@AS@%        exit( 0 );
  11443. %@AS@%     }
  11444. %@AS@%     for( col = 0; col < 64; col++ )
  11445. %@AS@%     {
  11446. %@AS@%        gray = col | (col << 8) | (col << 16);
  11447. %@AS@%        rainbow[col] = rainbow[col + 256] = BLU & gray;
  11448. %@AS@%        rainbow[col + 64] = rainbow[col + 64 + 256] = BLU | gray;
  11449. %@AS@%        rainbow[col + 128] = rainbow[col + 128 + 256] = RED | (WHT & ~gray);
  11450. %@AS@%        rainbow[col + 192] = rainbow[col + 192 + 256] = RED & ~gray;
  11451. %@AS@%     }
  11452. %@AS@%     _setvieworg( 160, 85 );
  11453. %@AS@%  
  11454. %@AS@%     for( i = 0; i < 255; i++ )
  11455. %@AS@%     {
  11456. %@AS@%        _setcolor( 255 - i );
  11457. %@AS@%        _moveto( i, i - 255 );
  11458. %@AS@%        _lineto( -i, 255 - i );
  11459. %@AS@%        _moveto( -i, i - 255 );
  11460. %@AS@%        _lineto( i, 255 - i );
  11461. %@AS@%        _ellipse( _GBORDER, -i, -i / 2, i, i / 2 );
  11462. %@AS@%     }
  11463. %@AS@%     for( i = 0; !kbhit(); i += STEP, i %= 256 )
  11464. %@AS@%        _remapallpalette( &(rainbow[i]) );
  11465. %@AS@%  
  11466. %@AS@%     _setvideomode( _DEFAULTMODE );
  11467. %@AS@%  }%@AE@%%@NL@%
  11468. %@NL@%
  11469. %@NL@%
  11470. %@3@%%@CR:C6A00130007 @%%@AB@%Using the Color Video Text Modes%@AE@%%@EH@%%@NL@%
  11471. %@NL@%
  11472. Two color video text modes, %@AB@%_TEXTC40%@AE@% and %@AB@%_TEXTC80%@AE@%, can be used with the CGA,
  11473. EGA, and VGA displays. These modes display steady or blinking text in any of
  11474. 16 foreground colors with any one of 8 background colors.  %@NL@%
  11475. %@NL@%
  11476. %@NL@%
  11477. %@4@%%@AB@%Basics of Text Color Selection%@AE@%%@EH@%%@NL@%
  11478. %@NL@%
  11479. In a video text mode, each displayed character requires two bytes of video
  11480. memory. The first byte contains the ASCII code representing the character
  11481. and the second byte contains the display attribute. In the CGA color video
  11482. text modes, the attribute byte determines the color and whether it will
  11483. blink. Sixteen colors are available: the CGA pixel values, and the default
  11484. EGA and VGA pixel values. Since the EGA and VGA palette can be remapped,
  11485. these values can be made to correspond to any set of 16 colors with the
  11486. appropriate palette mapping.  %@NL@%
  11487. %@NL@%
  11488. %@NL@%
  11489. %@4@%%@AB@%Using Text Colors%@AE@%%@EH@%%@NL@%
  11490. %@NL@%
  11491. Use the %@AB@%_gettextcolor%@AE@% and %@AB@%_getbkcolor%@AE@% functions to find the current text
  11492. foreground and background colors.  %@NL@%
  11493. %@NL@%
  11494. Values in the range 0-15 are interpreted as normal color. Values in the
  11495. range 16-31 are the same colors as those in the range 0-15 but with blinking
  11496. text.  %@NL@%
  11497. %@NL@%
  11498. Use the %@AB@%_settextcolor%@AE@% and %@AB@%_setbkcolor%@AE@% functions to set foreground and
  11499. background colors in video text mode. These functions use a single argument
  11500. that specifies the pixel value to be used for text displayed with the
  11501. %@AB@%_outtext%@AE@% function. The color indexes for color video text modes are defined
  11502. in Table 13.4.  %@NL@%
  11503. %@NL@%
  11504. %@AB@%Table 13.4  %@AB@%Text Colors%@AE@%%@AE@%
  11505.  
  11506. %@TH:  11   556 02 08 10 08 50 @%Number  Color     Number  Color%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%0       Black     8       Dark gray1       Blue      9       Light blue2       Green     10      Light green3       Cyan      11      Light cyan4       Red       12      Light red5       Magenta   13      Light magenta6       Brown     14      Light brown7       White     15      Light white%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  11   556 02 08 10 08 50 @%
  11507.  
  11508. %@NL@%
  11509. %@4@%%@AB@%Displaying Color Text%@AE@%%@EH@%%@NL@%
  11510. %@NL@%
  11511. The %@AB@%_settextposition%@AE@% function moves the cursor to a row and column for
  11512. displaying color text. The %@AB@%_outtext%@AE@% function displays the text.  %@NL@%
  11513. %@NL@%
  11514. %@NL@%
  11515. %@4@%%@AB@%Example: Viewing Text Colors%@AE@%%@EH@%%@NL@%
  11516. %@NL@%
  11517. The following program displays a chart showing all possible combinations of
  11518. text and background colors:  %@NL@%
  11519. %@NL@%
  11520. %@AS@%  /* COLTEXT.C: Display text in color. */
  11521. %@AS@%  
  11522. %@AS@%  #include <stdio.h>
  11523. %@AS@%  #include <conio.h>
  11524. %@AS@%  #include <graph.h>
  11525. %@AS@%  
  11526. %@AS@%  char buffer [80];
  11527. %@AS@%  
  11528. %@AS@%  main()
  11529. %@AS@%  {
  11530. %@AS@%     int blink,fgd;
  11531. %@AS@%     long bgd;
  11532. %@AS@%  
  11533. %@AS@%     _clearscreen( _GCLEARSCREEN );
  11534. %@AS@%     printf( "Text color attributes:\n" );
  11535. %@AS@%  
  11536. %@AS@%     for( blink=0; blink<=16; blink+=16 )
  11537. %@AS@%     {
  11538. %@AS@%        for( bgd=0; bgd<8; bgd++ )
  11539. %@AS@%        {
  11540. %@AS@%           _setbkcolor( bgd );
  11541. %@AS@%           _settextposition( bgd + ((blink / 16) * 9) + 3, 1 );
  11542. %@AS@%           _settextcolor( 7 );
  11543. %@AS@%           sprintf( buffer, "Bgd: %d Fgd:", bgd );
  11544. %@AS@%           _outtext( buffer );
  11545. %@AS@%   
  11546. %@AS@%           for( fgd=0; fgd<16; fgd++ )
  11547. %@AS@%           {
  11548. %@AS@%              _settextcolor( fgd+blink );
  11549. %@AS@%              sprintf( buffer, " %2d ", fgd+blink );
  11550. %@AS@%              _outtext( buffer );
  11551. %@AS@%           }
  11552. %@AS@%        }
  11553. %@AS@%     }
  11554. %@AS@%     getch();
  11555. %@AS@%     _setvideomode( _DEFAULTMODE );
  11556. %@AS@%  }%@AE@%%@NL@%
  11557. %@NL@%
  11558. %@NL@%
  11559. %@2@%%@CR:C6A00130008 @%%@AB@%Text Coordinates%@AE@%%@EH@%%@NL@%
  11560. %@NL@%
  11561. Before you can write a program to print a word %@AI@%over there%@AE@% on the screen, you
  11562. need a system that describes to the compiler where %@AI@%there%@AE@% really is. QuickC
  11563. divides the text screen into rows and columns. See Figure 13.2.  %@NL@%
  11564. %@NL@%
  11565. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  11566. %@NL@%
  11567. Two important conventions to keep in mind about video text mode are:  %@NL@%
  11568. %@NL@%
  11569. %@NL@%
  11570.   1.  Numbering starts at 1, not 0. An 80-column screen contains columns
  11571.       1-80.%@NL@%
  11572. %@NL@%
  11573.   2.  The row is always listed before the column.%@NL@%
  11574. %@NL@%
  11575. %@NL@%
  11576. If the screen is in a video text mode that displays 25 rows and 80 columns
  11577. (as in Figure 13.2), the rows are numbered 1-25 and the columns are numbered
  11578. 1-80. In functions such as %@AB@%_settextposition%@AE@%, which is called in the next
  11579. example program, the parameters you pass are row and column (in that order).
  11580. %@NL@%
  11581. %@NL@%
  11582. %@NL@%
  11583. %@2@%%@CR:C6A00130009 @%%@AB@%Graphics Coordinates%@AE@%%@EH@%%@NL@%
  11584. %@NL@%
  11585. A similar (but slightly different) system is used for locating pixels on a
  11586. graphics screen. There are three ways of describing the location of pixels
  11587. on the screen:  %@NL@%
  11588. %@NL@%
  11589. %@NL@%
  11590.   1.  The physical screen coordinates%@NL@%
  11591. %@NL@%
  11592.   2.  The viewport coordinates%@NL@%
  11593. %@NL@%
  11594.   3.  The window coordinates%@NL@%
  11595. %@NL@%
  11596. %@NL@%
  11597. Each method is explained in the following sections.  %@NL@%
  11598. %@NL@%
  11599. %@NL@%
  11600. %@3@%%@CR:C6A00130010 @%%@AB@%The Physical Screen%@AE@%%@EH@%%@NL@%
  11601. %@NL@%
  11602. Suppose you write a program that calls %@AB@%_setvideomode%@AE@% and puts the screen
  11603. into the VGA graphics mode %@AB@%_VRES16COLOR%@AE@%. This gives you a screen containing
  11604. 640 horizontal pixels and 480 vertical pixels. The individual pixels are
  11605. named by their location relative to the %@AI@%x%@AE@% axis and %@AI@%y%@AE@% axis, as shown in
  11606. Figure 13.3.  %@NL@%
  11607. %@NL@%
  11608. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  11609. %@NL@%
  11610. Two important differences between text coordinates and pixel coordinates
  11611. are:  %@NL@%
  11612. %@NL@%
  11613. %@NL@%
  11614.   1.  Numbering starts at 0, not 1. If there are 640 pixels, they're
  11615.       numbered 0-639.%@NL@%
  11616. %@NL@%
  11617.   2.  The %@AI@%x%@AE@% coordinate (equivalent to a text column) is listed before the %@AI@%y%@AE@%
  11618.       coordinate.%@NL@%
  11619. %@NL@%
  11620. %@NL@%
  11621. The upper left corner is called the "origin." The %@AI@%x%@AE@% and %@AI@%y%@AE@% coordinates for
  11622. the origin are always (0, 0). If you use variables to refer to pixel
  11623. locations, declare them as integers.  %@NL@%
  11624. %@NL@%
  11625. %@NL@%
  11626. %@4@%%@AB@%Changing the Origin with _setvieworg%@AE@%%@EH@%%@NL@%
  11627. %@NL@%
  11628. The %@AB@%_setvieworg%@AE@% function changes the current location of the viewport's
  11629. origin. When you first enter graphics mode, the "viewport" is equivalent to
  11630. the physical  %@NL@%
  11631. %@NL@%
  11632. screen. You pass two integers, which represent the %@AI@%x%@AE@% and %@AI@%y%@AE@% coordinates of a
  11633. physical screen location. For example, the following line would move the
  11634. origin to the physical screen location (50, 100):  %@NL@%
  11635. %@NL@%
  11636. %@AS@%  _setvieworg( 50, 100 );%@AE@%%@NL@%
  11637. %@NL@%
  11638. The effect on the screen is illustrated in Figure 13.4.  %@NL@%
  11639. %@NL@%
  11640. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  11641. %@NL@%
  11642. The number of pixels hasn't changed, but the names given to the points have
  11643. changed. The %@AI@%x%@AE@% axis now ranges from -50 to +589 instead of 0 to 639. The %@AI@%y%@AE@%
  11644. axis now covers the values -100 to +379. (If you own an adapter other than
  11645. the VGA, the numbers are different but the effect is the same.)  %@NL@%
  11646. %@NL@%
  11647. All standard graphics functions are affected by the new origin, including
  11648. %@AB@%_arc%@AE@%, %@AB@%_ellipse%@AE@%, %@AB@%_lineto%@AE@%, %@AB@%_moveto%@AE@%, %@AB@%_pie%@AE@%, and %@AB@%_rectangle%@AE@%.  %@NL@%
  11649. %@NL@%
  11650. For example, if you call the %@AB@%_rectangle%@AE@% function after relocating the
  11651. viewport origin, and pass it the values (0, 0) and (40, 40), the rectangle
  11652. would be drawn 50 pixels from the left edge of the screen and 100 pixels
  11653. from the top. It would not appear in the upper left corner.  %@NL@%
  11654. %@NL@%
  11655. The values passed to %@AB@%_setvieworg%@AE@% are always physical screen locations.
  11656. Suppose you called the same function twice:  %@NL@%
  11657. %@NL@%
  11658. %@AS@%  _setvieworg( 50, 100 );
  11659. %@AS@%  _setvieworg( 50, 100 );%@AE@%%@NL@%
  11660. %@NL@%
  11661. The viewport origin would not move to (100, 200). It would remain at the
  11662. phys-ical screen location (50, 100).  %@NL@%
  11663. %@NL@%
  11664. %@NL@%
  11665. %@4@%%@AB@%Defining a Clipping Region with _setcliprgn%@AE@%%@EH@%%@NL@%
  11666. %@NL@%
  11667. The %@AB@%_setcliprgn%@AE@% function creates an invisible rectangular area on the screen
  11668. called a "clipping region." Attempts to draw inside the clipping region are
  11669. successful, while attempts to draw outside the region are not.  %@NL@%
  11670. %@NL@%
  11671. When you first enter a graphics mode, the default clipping region occupies
  11672. the entire screen. QuickC ignores any attempts to draw outside the screen.  %@NL@%
  11673. %@NL@%
  11674. Changing the clipping region requires one call to %@AB@%_setcliprgn%@AE@%. Suppose
  11675. you've entered the CGA graphics mode %@AB@% _MRES4COLOR%@AE@%, which has a screen
  11676. resolution of 320 x 200. If you draw a diagonal line from (0, 0) to (319,
  11677. 199), from the top left to the bottom right corner, the screen looks like
  11678. Figure 13.5.  %@NL@%
  11679. %@NL@%
  11680. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  11681. %@NL@%
  11682. You could create a clipping region with this line:  %@NL@%
  11683. %@NL@%
  11684. %@AS@%  _setcliprgn( 10, 10, 309, 189 )%@AE@%%@NL@%
  11685. %@NL@%
  11686. With the clipping region in effect, the same %@AB@%_lineto%@AE@% command would put the
  11687. line shown in Figure 13.6 on the screen.  %@NL@%
  11688. %@NL@%
  11689. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  11690. %@NL@%
  11691. The broken lines don't actually print on the screen. They indicate the outer
  11692. bounds of the clipping region.  %@NL@%
  11693. %@NL@%
  11694. %@NL@%
  11695. %@3@%%@CR:C6A00130011 @%%@AB@%Viewport Coordinates%@AE@%%@EH@%%@NL@%
  11696. %@NL@%
  11697. The %@AB@%_setviewport%@AE@% function establishes a new viewport within the boundaries
  11698. of the physical screen. A standard viewport has two distinguishing features:
  11699. %@NL@%
  11700. %@NL@%
  11701. %@NL@%
  11702.   1.  The origin of a viewport is in the upper left corner.%@NL@%
  11703. %@NL@%
  11704.   2.  The clipping region matches the outer boundaries of the viewport.%@NL@%
  11705. %@NL@%
  11706. %@NL@%
  11707. The %@AB@%_setviewport%@AE@% function does the same thing as calling the %@AB@%_setvieworg%@AE@% and
  11708. the %@AB@%_setcliprgn%@AE@% functions.  %@NL@%
  11709. %@NL@%
  11710. %@NL@%
  11711. %@3@%%@CR:C6A00130012 @%%@AB@%Real Coordinates in a Window%@AE@%%@EH@%%@NL@%
  11712. %@NL@%
  11713. Functions that refer to coordinates on the physical screen and within the
  11714. viewport require integer values. In real-life graphing applications, you
  11715. might wish to use floating-point values─stock prices, the price of wheat,
  11716. average rainfall, and so on. The %@AB@%_setwindow%@AE@% function allows you to scale the
  11717. screen to almost any size. In addition, the window-related functions take
  11718. double-precision, floating-point values instead of integers.  %@NL@%
  11719. %@NL@%
  11720. For example, say you want to graph 12 months of average temperatures that
  11721. range from -40 to +100. You could add the following line to your program:  %@NL@%
  11722. %@NL@%
  11723. %@AS@%  _setwindow( TRUE, 1.0, -40.0, 12.0, 100.0 );%@AE@%%@NL@%
  11724. %@NL@%
  11725. The first argument is the invert flag, which puts the lowest %@AI@%y%@AE@% value in the
  11726. bottom left corner. The minimum and maximum Cartesian coordinates follow
  11727. (the decimal point marks them as floating-point values). The new
  11728. organization of the screen is shown in Figure 13.7.  %@NL@%
  11729. %@NL@%
  11730. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  11731. %@NL@%
  11732. Note that January and December are plotted on the left and right edges of
  11733. the screen. In an application like this, it might be better to number the %@AI@%x%@AE@%
  11734. axis from 0.0 to 13.0, to provide some extra space.  %@NL@%
  11735. %@NL@%
  11736. If you next plot a point with %@AB@%_setpixel_w%@AE@% or draw a line with %@AB@%_lineto_w%@AE@%, the
  11737. values are automatically scaled to the established window.  %@NL@%
  11738. %@NL@%
  11739. Follow these four steps to use real-coordinate graphics:  %@NL@%
  11740. %@NL@%
  11741. %@NL@%
  11742.   1.  Enter a graphics mode with %@AB@%_setvideomode%@AE@%.%@NL@%
  11743. %@NL@%
  11744.   2.  Use %@AB@%_setviewport%@AE@% to create a viewport area. (This step is optional if
  11745.       you plan to use the entire screen.)%@NL@%
  11746. %@NL@%
  11747.   3.  Create a real-coordinate window with %@AB@%_setwindow%@AE@%, passing an %@AB@%int%@AE@% invert
  11748.       flag and four %@AB@%double%@AE@% %@AI@%x%@AE@% and %@AI@%y%@AE@% coordinates for the minimum and maximum
  11749.       values.%@NL@%
  11750. %@NL@%
  11751.   4.  Draw graphics shapes with %@AB@%_rectangle_w%@AE@% and other functions. Do not
  11752.       confuse %@AB@%_rectangle%@AE@% (the viewport function) with %@AB@%_rectangle_w%@AE@% (the
  11753.       window function for drawing rectangles). All window functions end with
  11754.       an underscore and a letter %@AB@%w%@AE@% or an underscore and %@AB@%wxy%@AE@%.%@NL@%
  11755. %@NL@%
  11756. %@NL@%
  11757. Real-coordinate graphics can give you a lot of flexibility. For example, you
  11758. can fit either axis into a small range (such as 151.25 to 151.45) or into a
  11759. large range (-50,000 to +80,000), depending on the type of data you're
  11760. graphing. In addition, by changing the window coordinates, you can create
  11761. the effects of zooming in or panning across a figure.  %@NL@%
  11762. %@NL@%
  11763. %@NL@%
  11764. %@4@%%@AB@%Example Program%@AE@%%@EH@%%@NL@%
  11765. %@NL@%
  11766. The program below illustrates some ways to use the real-coordinate windowing
  11767. functions.  %@NL@%
  11768. %@NL@%
  11769. %@AS@%  /* REALG.C: Real-coordinate graphics. */
  11770. %@AS@%  
  11771. %@AS@%  #include <stdio.h>
  11772. %@AS@%  #include <conio.h>
  11773. %@AS@%  #include <graph.h>
  11774. %@AS@%  
  11775. %@AS@%  #define TRUE 1
  11776. %@AS@%  #define FALSE 0
  11777. %@AS@%  
  11778. %@AS@%  int four_colors( void );
  11779. %@AS@%  void three_graphs( void );
  11780. %@AS@%  void grid_shape( void );
  11781. %@AS@%  
  11782. %@AS@%  int halfx, halfy;
  11783. %@AS@%  struct videoconfig screen;
  11784. %@AS@%  double bananas[] =
  11785. %@AS@%    {
  11786. %@AS@%      -0.3, -0.2, -0.224, -0.1, -0.5, +0.21, +2.9,
  11787. %@AS@%      +0.3, +0.2, 0.0, -0.885, -1.1, -0.3, -0.2,
  11788. %@AS@%      +.001, +.005, +0.14, 0.0, -0.9, -0.13, +0.3
  11789. %@AS@%    };
  11790. %@AS@%  
  11791. %@AS@%  main()
  11792. %@AS@%  {
  11793. %@AS@%    if( four_colors() )
  11794. %@AS@%       three_graphs();
  11795. %@AS@%    else
  11796. %@AS@%       printf( "This program requires a CGA, EGA,\
  11797. %@AS@%               or VGA graphics card.\n" );
  11798. %@AS@%  }
  11799. %@AS@%  /*
  11800. %@AS@%  . Additional functions defined below
  11801. %@AS@%  .
  11802. %@AS@%  .
  11803. %@AS@%  */%@AE@%%@NL@%
  11804. %@NL@%
  11805. The %@AB@%main%@AE@% function is very short. It calls the %@AS@% four_colors %@AE@% function
  11806. (defined below), which attempts to enter a graphics mode where at least four
  11807. colors are available. If it succeeds, the %@AS@% three_graphs %@AE@% function is called,
  11808. which uses the numbers in the %@AS@% bananas %@AE@% array to draw three graphs. The
  11809. REALG.C screen output is shown in Figure 13.8.  %@NL@%
  11810. %@NL@%
  11811. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  11812. %@NL@%
  11813. It's worth noting that the %@AS@% grid_shape %@AE@% function (defined below) that draws
  11814. the graphs is using the same numbers in each case. However, the program uses
  11815. three different real-coordinate windows. The two windows in the top half are
  11816. the same size in physical coordinates, but they have different window sizes.
  11817. In all three cases, the grid is 2 units wide. In the upper left corner, the
  11818. window is 4 units wide; in the upper right, the window is 6 units wide,
  11819. which makes the graph appear smaller.  %@NL@%
  11820. %@NL@%
  11821. In two of the three graphs, one of the lines goes off the edge, outside the
  11822. clipping region. The lines do not intrude into the other windows, since
  11823. defining a window creates a clipping region.  %@NL@%
  11824. %@NL@%
  11825. Finally, note that the graph on the bottom of the screen seems to be upside
  11826. down with respect to the two graphs above it.  %@NL@%
  11827. %@NL@%
  11828. %@NL@%
  11829. %@4@%%@AB@%Checking the Adapter%@AE@%%@EH@%%@NL@%
  11830. %@NL@%
  11831. The first step in any graphics program is to enter a graphics mode. The %@AS@%
  11832. %@AS@%four_colors %@AE@% function performs this step:  %@NL@%
  11833. %@NL@%
  11834. %@AS@%  /* four_colors function from REALG.C. */
  11835. %@AS@%  
  11836. %@AS@%  int four_colors( void )
  11837. %@AS@%  {
  11838. %@AS@%     _getvideoconfig( &screen );
  11839. %@AS@%     switch( screen.adapter )
  11840. %@AS@%     {
  11841. %@AS@%        case _CGA:
  11842. %@AS@%        case _OCGA:
  11843. %@AS@%           _setvideomode( _MRES4COLOR );
  11844. %@AS@%           break;
  11845. %@AS@%        case _EGA:
  11846. %@AS@%        case _OEGA:
  11847. %@AS@%           _setvideomode( _ERESCOLOR );
  11848. %@AS@%           break;
  11849. %@AS@%        case _VGA:
  11850. %@AS@%        case _OVGA:
  11851. %@AS@%           _setvideomode( _VRES16COLOR );
  11852. %@AS@%           break;
  11853. %@AS@%        default:
  11854. %@AS@%           return( FALSE );
  11855. %@AS@%     }
  11856. %@AS@%     _getvideoconfig( &screen );
  11857. %@AS@%     return( TRUE );
  11858. %@AS@%  }%@AE@%%@NL@%
  11859. %@NL@%
  11860. The %@AB@%_getvideoconfig%@AE@% function places some information into the %@AB@%videoconfig%@AE@%
  11861. structure called %@AS@% screen%@AE@%. Then we use the member %@AS@% screen.adapter %@AE@% in a
  11862. %@AB@%switch%@AE@% statement construct to turn on the matching graphics mode. The
  11863. symbolic constants %@AB@%_CGA%@AE@% and the rest are defined in the GRAPH.H file. The
  11864. modes that begin with the letter %@AB@%O%@AE@% are Olivetti modes.  %@NL@%
  11865. %@NL@%
  11866. If the computer is equipped with a color card, %@AB@%_getvideoconfig%@AE@% returns a
  11867. %@AB@%TRUE%@AE@%. If it is not, it returns a %@AB@%FALSE%@AE@%, which causes %@AB@%main%@AE@% to skip the %@AS@%
  11868. %@AS@%three_graphs %@AE@% function.  %@NL@%
  11869. %@NL@%
  11870. %@NL@%
  11871. %@4@%%@AB@%Three Windows, Three Graphs%@AE@%%@EH@%%@NL@%
  11872. %@NL@%
  11873. If the %@AS@% four_colors %@AE@% function works properly, %@AB@%main%@AE@% calls the function below,
  11874. which prints the three graphs.  %@NL@%
  11875. %@NL@%
  11876. %@AS@%  /* three_graphs function from REALG.C. */
  11877. %@AS@%  
  11878. %@AS@%  void three_graphs( void )
  11879. %@AS@%  {
  11880. %@AS@%     int xwidth, yheight, cols, rows;
  11881. %@AS@%     struct _wxycoord upleft, botright;
  11882. %@AS@%  
  11883. %@AS@%     _clearscreen( _GCLEARSCREEN );
  11884. %@AS@%     xwidth = screen.numxpixels;
  11885. %@AS@%     yheight = screen.numypixels;
  11886. %@AS@%     halfx = xwidth/2;
  11887. %@AS@%     halfy = yheight/2;
  11888. %@AS@%     cols = screen.numtextcols;
  11889. %@AS@%     rows = screen.numtextrows;
  11890. %@AS@%   
  11891. %@AS@%     /* first window */
  11892. %@AS@%     _setviewport( 0, 0, halfx-1, halfy-1 );
  11893. %@AS@%     _settextwindow( 1, 1, rows/2, cols/2 );
  11894. %@AS@%     _setwindow( FALSE, -2.0, -2.0, 2.0, 2.0 );
  11895. %@AS@%     grid_shape();
  11896. %@AS@%     _rectangle( _GBORDER, 0, 0, halfx-1, halfy-1 );
  11897. %@AS@%   
  11898. %@AS@%     /* second window */
  11899. %@AS@%     _setviewport( halfx, 0, xwidth-1, halfy-1 );
  11900. %@AS@%     _settextwindow( 1, cols/2+1, rows/2, cols );
  11901. %@AS@%     _setwindow( FALSE, -3.0, -3.0, 3.0, 3.0 );
  11902. %@AS@%     grid_shape();
  11903. %@AS@%     _rectangle_w( _GBORDER, -3.0, -3.0, 3.0, 3.0 );
  11904. %@AS@%   
  11905. %@AS@%     /* third window */
  11906. %@AS@%     _setviewport( 0, halfy, xwidth-1, yheight-1 );
  11907. %@AS@%     _settextwindow( rows/2+1, 1, rows, cols );
  11908. %@AS@%     _setwindow( TRUE, -3.0, -1.5, 1.5, 1.5 );
  11909. %@AS@%     grid_shape();
  11910. %@AS@%     upleft.wx = -3.0;
  11911. %@AS@%     upleft.wy = -1.5;
  11912. %@AS@%     botright.wx = 1.5;
  11913. %@AS@%     botright.wy = 1.5;
  11914. %@AS@%     _rectangle_wxy( _GBORDER, &upleft, &botright );
  11915. %@AS@%   
  11916. %@AS@%     getch();
  11917. %@AS@%     _setvideomode( _DEFAULTMODE );
  11918. %@AS@%  }%@AE@%%@NL@%
  11919. %@NL@%
  11920. %@AB@%Clearing the Screen%@AE@% - Although entering a graphics mode automatically clears
  11921. the screen, it doesn't hurt to be sure, so %@AS@% three_graphs %@AE@% calls the
  11922. %@AB@%_clearscreen%@AE@% function:%@NL@%
  11923. %@NL@%
  11924. %@AS@%  _clearscreen( _GCLEARSCREEN );%@AE@%%@NL@%
  11925. %@NL@%
  11926. The %@AB@%_GCLEARSCREEN%@AE@% constant causes the entire physical screen to clear. Other
  11927. options include %@AB@%_GVIEWPORT%@AE@% and %@AB@%_GWINDOW%@AE@%, which clear the current viewport
  11928. and the current text window, respectively.  %@NL@%
  11929. %@NL@%
  11930. %@AB@%The First Window%@AE@% - After assigning values to some variables, the %@AS@%
  11931. %@AS@%three_graphs %@AE@% function creates the first window:%@NL@%
  11932. %@NL@%
  11933. %@AS@%  _setviewport( 0, 0, halfx - 1, halfy - 1 );
  11934. %@AS@%  _settextwindow( 1, 1, rows / 2, cols / 2 );
  11935. %@AS@%  _setwindow( FALSE, -2.0, -2.0, 2.0, 2.0 );%@AE@%%@NL@%
  11936. %@NL@%
  11937. First a viewport is defined to cover the upper left quarter of the screen.
  11938. Next, a text window is defined within the boundaries of that border. (Note
  11939. the numbering starts at 1 and the row location precedes the column.)
  11940. Finally, a window is defined. The %@AB@%FALSE%@AE@% constant forces the %@AI@%y%@AE@% axis to
  11941. increase from top to bottom. The corners of the window are (-2.0, -2.0) in
  11942. the upper left and (2.0, 2.0) in the bottom right corner.  %@NL@%
  11943. %@NL@%
  11944. Next, the function %@AS@% grid_shape %@AE@% is called, and a border is added to the
  11945. window:  %@NL@%
  11946. %@NL@%
  11947. %@AS@%  grid_shape();
  11948. %@AS@%  _rectangle( _GBORDER, 0, 0, halfx-1, halfy-1 );%@AE@%%@NL@%
  11949. %@NL@%
  11950. Note that this is the standard %@AB@%_rectangle%@AE@% function, which takes coordinates
  11951. relative to the viewport (not window coordinates).  %@NL@%
  11952. %@NL@%
  11953. %@AB@%Two More Windows%@AE@% - The two other windows are similar to the first. All three
  11954. call %@AS@% grid_shape %@AE@% (defined below), which draws a grid from location (-1.0,
  11955. -1.0) to (+1.0, +1.0). The grid appears in different sizes because the
  11956. coordinates in the windows vary. The second window ranges from (-3.0, -3.0)
  11957. to (+3.0, +3.0), so the width of the grid is one-third the width of the
  11958. second window, while it is one-half the width of the first.%@NL@%
  11959. %@NL@%
  11960. Note also that the third window contains a %@AB@%TRUE%@AE@% as the first argument. This
  11961. causes the%@AI@% y %@AE@%axis to increase from bottom to top, instead of top to bottom.
  11962. As a result, this graph appears to be upside down in relation to the other
  11963. two.  %@NL@%
  11964. %@NL@%
  11965. After calling %@AS@% grid_shape%@AE@%, the program frames each window with one of the
  11966. following functions:  %@NL@%
  11967. %@NL@%
  11968. %@AS@%  _rectangle( _GBORDER, 0, 0, halfx -1, halfy -1 );%@AE@%%@NL@%
  11969. %@NL@%
  11970. %@AS@%  _rectangle_w( _GBORDER, -3.0, -3.0, 3.0, 3.0 ); %@AE@%%@NL@%
  11971. %@NL@%
  11972. %@AS@%  _rectangle_wxy( _GBORDER, &upleft, &botright );%@AE@%%@NL@%
  11973. %@NL@%
  11974. All three functions contain a fill flag as the first argument. The
  11975. %@AB@%_rectangle%@AE@% function takes integer arguments that refer to the viewport
  11976. screen coordinates. The function %@AB@%_rectangle_w%@AE@% takes four double-precision,
  11977. floating-point values referring to window coordinates: upper left %@AI@%x%@AE@%, upper
  11978. left %@AI@%y%@AE@%, lower right %@AI@%x%@AE@%, and lower right %@AI@%y%@AE@%. The function %@AB@%_rectangle_wxy%@AE@% takes
  11979. two arguments: the addresses of two structures of type %@AB@%_wxycoord%@AE@%, which
  11980. contains two %@AB@%double%@AE@% types named %@AB@%wx%@AE@% and %@AB@%wy%@AE@%. The structure is defined in
  11981. GRAPH.H. The values are assigned just before %@AB@%_rectangle_wxy%@AE@% is called.  %@NL@%
  11982. %@NL@%
  11983. %@AB@%Text, Colors, and Lines%@AE@% - The %@AS@% grid_shape %@AE@% function is shown below:%@NL@%
  11984. %@NL@%
  11985. %@AS@%  /* grid_shape from the REALG.C program. */
  11986. %@AS@%  
  11987. %@AS@%  void grid_shape( void )
  11988. %@AS@%  {
  11989. %@AS@%     int i, numc, x1, y1, x2, y2;
  11990. %@AS@%     double x, y;
  11991. %@AS@%     char txt[80];%@AE@%%@NL@%
  11992. %@NL@%
  11993. %@AS@%  numc = screen.numcolors;
  11994. %@AS@%     for( i = 1; i <numc; i++ )
  11995. %@AS@%     {
  11996. %@AS@%        _settextposition( i, 2 );
  11997. %@AS@%        _settextcolor( i );
  11998. %@AS@%        sprintf( txt, "Color %d", i );
  11999. %@AS@%        _outtext( txt );
  12000. %@AS@%     }
  12001. %@AS@%     _setcolor( 1 );
  12002. %@AS@%     _rectangle_w( _GBORDER, -1.0, -1.0, 1.0, 1.0 );
  12003. %@AS@%     _rectangle_w( _GBORDER, -1.02, -1.02, 1.02, 1.02 );%@AE@%%@NL@%
  12004. %@NL@%
  12005. %@AS@%  for( x = -0.9, i = 0; x <0.9; x += 0.1 )
  12006. %@AS@%     {
  12007. %@AS@%        _setcolor( 2 );
  12008. %@AS@%        _moveto_w( x, -1.0 );
  12009. %@AS@%        _lineto_w( x, 1.0 );
  12010. %@AS@%        _moveto_w( -1.0, x );
  12011. %@AS@%        _lineto_w( 1.0, x );
  12012. %@AS@%  
  12013. %@AS@%        _setcolor( 3 );
  12014. %@AS@%        _moveto_w( x - 0.1, bananas[i++] );
  12015. %@AS@%        _lineto_w( x, bananas[i] );
  12016. %@AS@%     }
  12017. %@AS@%     _moveto_w( 0.9, bananas[i++] );
  12018. %@AS@%     _lineto_w( 1.0, bananas[i] );
  12019. %@AS@%  }%@AE@%%@NL@%
  12020. %@NL@%
  12021. First, the number of available color indexes is assigned to the %@AS@% numc %@AE@%
  12022. variable and a %@AB@%for%@AE@% loop displays all of the available colors:  %@NL@%
  12023. %@NL@%
  12024. %@AS@%  numc = screen.numcolors;
  12025. %@AS@%  for( i = 1; i < numc; i++ )
  12026. %@AS@%  {
  12027. %@AS@%     _settextposition( i, 2 );
  12028. %@AS@%     _settextcolor( i );
  12029. %@AS@%     sprintf( txt, "Color %d", i );
  12030. %@AS@%     _outtext( txt );
  12031. %@AS@%  }%@AE@%%@NL@%
  12032. %@NL@%
  12033. The names of the functions are self-explanatory. The advantage of using
  12034. %@AB@%_outtext%@AE@% in graphics mode is that, unlike %@AB@%printf%@AE@%, you can control the text
  12035. color.  %@NL@%
  12036. %@NL@%
  12037. The function names that end with %@AB@%_w%@AE@% work the same as their viewport
  12038. equivalents, except you pass double-precision, floating-point values instead
  12039. of integers. For example, you pass integers to %@AB@%_lineto%@AE@% but floating-point
  12040. values to %@AB@%_lineto_w.%@AE@%  %@NL@%
  12041. %@NL@%
  12042. If you're interested in further explorations of graphics, Chapters 14 and 15
  12043. introduce Presentation Graphics and fonts, both of which offer even more
  12044. graphics options.  %@NL@%
  12045. %@NL@%
  12046. %@NL@%
  12047. %@NL@%
  12048. %@NL@%
  12049. %@NL@%
  12050. %@NL@%
  12051. %@CR:C6A00140001 @%%@1@%%@AB@%Chapter 14  Presentation Graphics%@AE@%%@EH@%%@NL@%
  12052. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  12053. %@NL@%
  12054. Presentation Graphics is the name given to a library of chart-generating
  12055. functions included with the QuickC package. With these functions your QuickC
  12056. programs can display data as a variety of graphs such as pie charts, bar and
  12057. column charts, line graphs, and scatter diagrams. Whole columns of
  12058. unintelligible numbers can be reduced to a single expressive picture with
  12059. Presentation Graphics.  %@NL@%
  12060. %@NL@%
  12061. This chapter shows you how to use the Presentation Graphics library in your
  12062. QuickC programs. The first section is an introduction to Presentation
  12063. Graphics. It explains terminology and describes some of the library's many
  12064. capabilities. The middle sections of this chapter list the steps involved in
  12065. writing a charting program and illustrate them with short examples.  %@NL@%
  12066. %@NL@%
  12067. The concluding portions of the chapter delve more deeply into Presentation
  12068. Graphics. Here you'll learn about the Presentation Graphics default data
  12069. structures and how to manipulate them. The final section presents a short
  12070. reference list of all the functions that comprise the Presentation Graphics
  12071. library.  %@NL@%
  12072. %@NL@%
  12073. To use Presentation Graphics you need a graphics adapter and a monitor
  12074. capable of bit-mapped display─the same equipment mentioned in Chapter 13,
  12075. "Graphics." Support is provided for CGA, EGA, VGA, MCGA, Hercules monochrome
  12076. graphics, and Olivetti Color Board.  %@NL@%
  12077. %@NL@%
  12078. %@NL@%
  12079. %@2@%%@CR:C6A00140002 @%%@AB@%Terminology%@AE@%%@EH@%%@NL@%
  12080. %@NL@%
  12081. Certain terms and phrases pertaining to Presentation Graphics and its
  12082. functions are used throughout this chapter. The following description of
  12083. Presentation Graphics terminology will help you better understand this
  12084. chapter.  %@NL@%
  12085. %@NL@%
  12086. %@NL@%
  12087. %@4@%%@AB@%Data Series%@AE@%%@EH@%%@NL@%
  12088. %@NL@%
  12089. %@AU@% Groups or series of data can be graphed on the same chart.%@AE@%  %@NL@%
  12090. %@NL@%
  12091. Data that are related by a common idea or purpose constitutes a "series."
  12092. For example, the prices of a futures commodity over the course of a year
  12093. form a single series of data. The commodity's volume and open interest form
  12094. two more series for the same period of time. Presentation Graphics allows
  12095. you to plot multiple series on the same graph. In theory only your system's
  12096. memory capacity restricts the number of data series that can appear on a
  12097. graph. However, there are practical considerations.  %@NL@%
  12098. %@NL@%
  12099. Characteristics such as color and pattern help distinguish one series from
  12100. an-other. You can more readily differentiate series on a color monitor than
  12101. you can on a monochrome monitor. The number of series that can comfortably
  12102. appear on the same chart depends on the chart type and the number of
  12103. available colors. Only experimentation can tell you what is best for your
  12104. system.  %@NL@%
  12105. %@NL@%
  12106. %@NL@%
  12107. %@4@%%@AB@%Categories%@AE@%%@EH@%%@NL@%
  12108. %@NL@%
  12109. Categories are non-numeric data. A set of categories forms a frame of
  12110. reference for the comparisons of numeric data. For example, the months of
  12111. the year are categories against which numeric data such as rainfall can be
  12112. plotted.  %@NL@%
  12113. %@NL@%
  12114. Regional sales provide another example. A chart can show comparisons of a
  12115. company's sales in different parts of the country. Each region forms a
  12116. category. The sales within each region are numeric data that have meaning
  12117. only within the context of a particular category.  %@NL@%
  12118. %@NL@%
  12119. %@NL@%
  12120. %@4@%%@AB@%Values%@AE@%%@EH@%%@NL@%
  12121. %@NL@%
  12122. Values are numeric data. Sales, stock prices, air temperatures,
  12123. populations─all are series of values that can be plotted against categories
  12124. or against other values.  %@NL@%
  12125. %@NL@%
  12126. Presentation Graphics allows you to overlay different series of value data
  12127. on a single graph. For example, average monthly temperatures or monthly
  12128. sales of heating oil during different years─or a combination of temperatures
  12129. and sales─can be plotted together on the same graph.  %@NL@%
  12130. %@NL@%
  12131. %@NL@%
  12132. %@4@%%@AB@%Pie Charts%@AE@%%@EH@%%@NL@%
  12133. %@NL@%
  12134. %@AU@%(Please refer to the printed book.)%@AE@%%@NL@%
  12135. %@NL@%
  12136. "Pie charts" are used to represent data by showing the relationship of each
  12137. part to the whole. A good example is a company's monthly sales figures. The
  12138. sales to the company's various accounts can be represented as slices of the
  12139. pie.  %@NL@%
  12140. %@NL@%
  12141. Presentation Graphics can display either a standard or an "exploded" pie
  12142. chart. The exploded view shows the pie with one or more pieces separated for
  12143. emphasis. Presentation Graphics optionally labels each slice of a pie chart
  12144. with a percentage figure.  %@NL@%
  12145. %@NL@%
  12146. %@NL@%
  12147. %@4@%%@AB@%Bar and Column Charts%@AE@%%@EH@%%@NL@%
  12148. %@NL@%
  12149. %@AU@%(Please refer to the printed book.)%@AE@%%@NL@%
  12150. %@NL@%
  12151. As the name implies, a "bar chart" shows data as horizontal bars. Bar charts
  12152. show comparisons among items rather than absolute value.  %@NL@%
  12153. %@NL@%
  12154. "Column charts"%@NL@%
  12155. %@NL@%
  12156. %@AU@%(Please refer to the printed book.)%@AE@%%@NL@%
  12157. %@NL@%
  12158. are vertical bar charts. Column charts are frequently used to show
  12159. variations over a period of time, since they suggest time flow better than a
  12160. bar chart  %@NL@%
  12161. %@4@%%@AB@%Line Graphs%@AE@%%@EH@%%@NL@%
  12162. %@NL@%
  12163. %@AU@%(Please refer to the printed book.)%@AE@%%@NL@%
  12164. %@NL@%
  12165. "Line graphs" illustrate trends or changes in data. They show how a series
  12166. of values varies against some category─for example, average temperatures
  12167. throughout a particular year.  %@NL@%
  12168. %@NL@%
  12169. Traditionally, line graphs show a collection of data points connected by
  12170. lines; hence the name. However, Presentation Graphics can also plot points
  12171. that are not connected by lines.  %@NL@%
  12172. %@NL@%
  12173. %@NL@%
  12174. %@4@%%@AB@%Scatter Diagrams%@AE@%%@EH@%%@NL@%
  12175. %@NL@%
  12176. %@AU@%(Please refer to the printed book.)%@AE@%%@NL@%
  12177. %@NL@%
  12178. A "scatter diagram" is the only type of graph available in Presentation
  12179. Graphics that compares values with values. A scatter diagram simply plots
  12180. points. One value may correspond to several other values.  %@NL@%
  12181. %@NL@%
  12182. Scatter diagrams illustrate the relationship between numeric values in
  12183. different groups of data. They graphically show trends and correlations not
  12184. easily detected from rows and columns of raw numbers. This explains why
  12185. scatter diagrams are a favorite tool of statisticians and forecasters.  %@NL@%
  12186. %@NL@%
  12187. They are most useful with relatively large populations of data. Consider,
  12188. for example, the relationship between personal income and family size. If
  12189. you poll one thousand wage earners for their income and family size, you
  12190. have a scatter diagram with one thousand points. If you combine your results
  12191. so that you're left with one average income for each family size, you have a
  12192. line graph.  %@NL@%
  12193. %@NL@%
  12194. %@NL@%
  12195. %@4@%%@AB@%Axes%@AE@%%@EH@%%@NL@%
  12196. %@NL@%
  12197. All Presentation Graphics charts except pie charts are displayed with two
  12198. perpendicular reference lines called "axes." The vertical or %@AI@%y%@AE@% axis runs
  12199. from top to bottom of the chart and is placed against the left side of the
  12200. screen. The horizontal or %@AI@%x%@AE@% axis runs from left to right across the bottom
  12201. of the screen.  %@NL@%
  12202. %@NL@%
  12203. %@AU@% The chart type determines which axes are used for category and value data.%@AE@%
  12204. %@NL@%
  12205. %@NL@%
  12206. The %@AI@%x%@AE@% axis is the category axis for column and line charts and the value
  12207. axis for bar charts. The %@AI@%y%@AE@% axis is the value axis for column and line charts
  12208. and the category axis for bar charts.  %@NL@%
  12209. %@NL@%
  12210. %@NL@%
  12211. %@4@%%@AB@%Chart Windows%@AE@%%@EH@%%@NL@%
  12212. %@NL@%
  12213. The "chart window" defines that part of the screen on which the chart is
  12214. drawn. Normally the window fills the entire screen, but Presentation
  12215. Graphics allows you to resize the window for smaller graphs. By redefining
  12216. the chart window to different screen locations, you can view separate graphs
  12217. together on the same screen.  %@NL@%
  12218. %@NL@%
  12219. %@NL@%
  12220. %@4@%%@AB@%Data Windows%@AE@%%@EH@%%@NL@%
  12221. %@NL@%
  12222. While the chart window defines the entire graph including axes and labels,
  12223. the "data window" defines only the actual plotting area. This is the portion
  12224. of the graph to the right of the %@AI@%y%@AE@% axis and above the %@AI@%x%@AE@% axis. You cannot
  12225. directly specify the size of the data window. Presentation Graphics
  12226. automatically determines its size based on the dimensions of the chart
  12227. window.  %@NL@%
  12228. %@NL@%
  12229. %@NL@%
  12230. %@4@%%@AB@%Chart Styles%@AE@%%@EH@%%@NL@%
  12231. %@NL@%
  12232. Each of the five types of Presentation Graphics charts can appear in two
  12233. different chart styles, as described in Table 14.1.  %@NL@%
  12234. %@NL@%
  12235. %@AB@%Table 14.1  %@AB@%Presentation Graphics Chart Styles%@AE@%%@AE@%
  12236.  
  12237. %@TH:   8   488 02 12 20 44 @%Chart Type  Chart Style #1      Chart Style #2%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%Pie         With percentages    Without percentagesBar         Side-by-side        StackedColumn      Side-by-side        StackedLine        Points with lines   Points onlyScatter     Points with lines   Points only%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   8   488 02 12 20 44 @%
  12238.  
  12239. Bar and column charts have only one style when displaying a single series of
  12240. data. The styles "side-by-side" and "stacked" are applicable when more than
  12241. one series appear on the same chart. The first style arranges the bars or
  12242. columns for the different series side by side, showing relative heights or
  12243. lengths. The stacked style, illustrated in Figure 14.1 for a column chart,
  12244. emphasizes relative sizes between bars or columns.  %@NL@%
  12245. %@NL@%
  12246. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  12247. %@NL@%
  12248. %@NL@%
  12249. %@4@%%@AB@%Legends%@AE@%%@EH@%%@NL@%
  12250. %@NL@%
  12251. %@AU@% Legends help identify  individual data series.%@AE@%  %@NL@%
  12252. %@NL@%
  12253. When displaying more than one data series on a chart, Presentation Graphics
  12254. uses different colors, line styles, or patterns to differentiate the series.
  12255. Presentation Graphics also can display a "legend" that labels the different
  12256. series of a chart. For a pie chart, the legend labels individual slices of
  12257. the pie.  %@NL@%
  12258. %@NL@%
  12259. The format is similar to the legends found on printed graphs and maps. A
  12260. sample of the color and pattern used to graph the series appears next to the
  12261. series label. This identifies which set of data the labels belong to. The
  12262. "Palettes" section later in this chapter explains how different data series
  12263. are identified by color and pattern.  %@NL@%
  12264. %@NL@%
  12265. %@NL@%
  12266. %@2@%%@CR:C6A00140003 @%%@AB@%Presentation Graphics Program Structure%@AE@%%@EH@%%@NL@%
  12267. %@NL@%
  12268. QuickC programs that use Presentation Graphics typically follow seven steps:
  12269. %@NL@%
  12270. %@NL@%
  12271. %@AB@%Step%@AE@%                              %@AB@%Comments%@AE@%
  12272. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  12273. Include required header files.    Along with other header files your 
  12274.                                   program may need, you must include the 
  12275.                                   files GRAPH.H and PGCHART.H.
  12276.  
  12277. Set video mode to graphics.       Refer to Chapter 13, "Graphics," for a 
  12278.                                   discussion 
  12279.                                   of video modes supported by QuickC. This
  12280.                                   chapter explains how to change modes 
  12281.                                   within a QuickC program.
  12282.  
  12283. Initialize Presentation Graphics  Presentation Graphics places charting 
  12284. chart                             parameters in a data structure. These 
  12285. environment.                      parameters determine how a graph will 
  12286.                                   appear on the screen. Collectively they 
  12287.                                   make up the "chart environment," 
  12288.                                   described in the section "Customizing 
  12289.                                   Presentation Graphics."  Presentation 
  12290.                                   Graphics sets the environment parameters
  12291.                                   to default values. The amount of 
  12292.                                   initialization that must be done by your
  12293.                                   program depends on how extensively it 
  12294.                                   relies on defaults.
  12295.  
  12296. Assemble plot data.               Data can be collected in a variety of 
  12297.                                   ways:  by calculating it elsewhere in 
  12298.                                   the program, reading it from files, or 
  12299.                                   entering it from the keyboard. All plot 
  12300.                                   data
  12301.                                   must be assembled in arrays because the 
  12302.                                   Presentation Graphics functions locate 
  12303.                                   them through
  12304.                                   pointers.
  12305.  
  12306. Call Presentation Graphics        Display your chart.
  12307. functions.                        
  12308.  
  12309. Pause while chart is on the       Your program should pause after a chart 
  12310. screen.                           is displayed. This step allows 
  12311.                                   sufficient time to read the chart. A 
  12312.                                   common method is to wait for a keyboard 
  12313.                                   entry before resuming.
  12314.  
  12315. Reset video mode.                 When your program detects the signal to 
  12316.                                   continue,
  12317.                                   it should normally reset the video to 
  12318.                                   its original
  12319.                                   mode.
  12320.  
  12321. Once your program successfully compiles, you must link it to the library
  12322. modules PGCHART.LIB and GRAPHICS.LIB. Use the Microsoft Overlay Linker  %@NL@%
  12323. %@NL@%
  12324. QLINK.EXE or the QCL command-line interface to link programs outside the
  12325. QuickC environment. For descriptions of QLINK and QCL, see the %@AI@%Microsoft
  12326. %@AI@%QuickC Tool Kit%@AE@%, Chapter 1, "Creating Executable Programs."  %@NL@%
  12327. %@NL@%
  12328. %@NL@%
  12329. %@2@%%@CR:C6A00140004 @%%@AB@%Five Example Chart Programs%@AE@%%@EH@%%@NL@%
  12330. %@NL@%
  12331. You'll have a better idea of Presentation Graphics capabilities once you've
  12332. seen what it can do. To that end some simple examples are presented in this
  12333. section. The sample programs that follow use only five of the 22
  12334. Presentation Graphics functions: %@AB@%_pg_initchart%@AE@%, %@AB@%_pg_defaultchart%@AE@%,
  12335. %@AB@%_pg_chartpie%@AE@%, %@AB@%_pg_chart%@AE@%, and %@AB@%_pg_chartscatter%@AE@%. Appendix B, "C Library
  12336. Guide," and online help document these functions and their arguments. But
  12337. the example code is straightforward, and you should be able to follow easily
  12338. for now. Each program is commented so that you can recognize the seven steps
  12339. given above.  %@NL@%
  12340. %@NL@%
  12341. %@NL@%
  12342. %@4@%%@AB@%A Sample Data Set%@AE@%%@EH@%%@NL@%
  12343. %@NL@%
  12344. Suppose a grocer wants to graph the sales of orange juice over the course of
  12345. a single year. Sales figures are on a monthly basis, so the grocer selects
  12346. as category data the months of the year from January through December. The
  12347. sales figures are shown below.  %@NL@%
  12348. %@NL@%
  12349. %@TH:  15   614 02 26 50 @%Month                     Quantity (cases)%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%January                   33February                  27March                     42April                     64May                       106June                      157July                      182August                    217September                 128October                   62November                  43December                  36%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  15   614 02 26 50 @%
  12350.  
  12351. %@NL@%
  12352. %@4@%%@AB@%Example: Pie Chart%@AE@%%@EH@%%@NL@%
  12353. %@NL@%
  12354. The following program uses Presentation Graphics to display a pie chart for
  12355. the grocer's data. The chart, which is shown in Figure 14.2, remains on the
  12356. screen until a key is pressed.  %@NL@%
  12357. %@NL@%
  12358. The Presentation Graphics functions return values that identify error
  12359. conditions. A return value of 0 indicates that the function has completed
  12360. its work without error. Refer to the header file PGCHART.H and online help
  12361. for descriptions of the nonzero error codes.  %@NL@%
  12362. %@NL@%
  12363. %@AS@%  /* PIE.C:  Create sample pie chart.  */
  12364. %@AS@%  
  12365. %@AS@%  #include <conio.h>
  12366. %@AS@%  #include <string.h>
  12367. %@AS@%  #include <graph.h>
  12368. %@AS@%  #include <pgchart.h>%@AE@%%@NL@%
  12369. %@NL@%
  12370. %@AS@%  #define MONTHS 12
  12371. %@AS@%  
  12372. %@AS@%  typedef enum {FALSE, TRUE} boolean;
  12373. %@AS@%  
  12374. %@AS@%  float far value[MONTHS] = 
  12375. %@AS@%  {
  12376. %@AS@%     33.0, 27.0, 42.0, 64.0,106.0,157.0,
  12377. %@AS@%    182.0,217.0,128.0, 62.0, 43.0, 36.0
  12378. %@AS@%  };
  12379. %@AS@%  char far *category[MONTHS] = 
  12380. %@AS@%  {
  12381. %@AS@%    "Jan", "Feb", "Mar", "Apr",
  12382. %@AS@%    "May", "Jun", "Jly", "Aug",
  12383. %@AS@%    "Sep", "Oct", "Nov", "Dec"
  12384. %@AS@%  };
  12385. %@AS@%  short far explode[MONTHS] = {0};
  12386. %@AS@%  
  12387. %@AS@%  main()
  12388. %@AS@%  {
  12389. %@AS@%    chartenv env;
  12390. %@AS@%    int mode = _VRES16COLOR;
  12391. %@AS@%  
  12392. %@AS@%    /* Set highest video mode available */
  12393. %@AS@%    while(!_setvideomode( mode ))
  12394. %@AS@%       mode--;
  12395. %@AS@%    if(mode == _TEXTMONO)
  12396. %@AS@%       return( 0 );
  12397. %@AS@%  
  12398. %@AS@%    /* Initialize chart library and a default pie chart */
  12399. %@AS@%    _pg_initchart();
  12400. %@AS@%    _pg_defaultchart( &env, _PG_PIECHART, _PG_PERCENT );
  12401. %@AS@%  
  12402. %@AS@%    /* Add titles and some chart options */
  12403. %@AS@%    strcpy( env.maintitle.title, "Good Neighbor Grocery" );
  12404. %@AS@%    env.maintitle.titlecolor = 6;
  12405. %@AS@%    env.maintitle.justify = _PG_RIGHT;
  12406. %@AS@%    strcpy( env.subtitle.title, "Orange Juice Sales" ); 
  12407. %@AS@%    env.subtitle.titlecolor = 6;
  12408. %@AS@%    env.subtitle.justify = _PG_RIGHT;
  12409. %@AS@%    env.chartwindow.border = FALSE;%@AE@%%@NL@%
  12410. %@NL@%
  12411. %@AS@%    /* Parameters for call to _pg_chartpie are:
  12412. %@AS@%     *  
  12413. %@AS@%     *    env        - Environment variable
  12414. %@AS@%     *    category   - Category labels
  12415. %@AS@%     *    value      - Data to chart
  12416. %@AS@%     *    explode    - Separated pieces
  12417. %@AS@%     *    MONTHS     - Number of data values
  12418. %@AS@%     */
  12419. %@AS@%    if(_pg_chartpie( &env, category, value,
  12420. %@AS@%                      explode, MONTHS ))
  12421. %@AS@%    {
  12422. %@AS@%       _setvideomode( _DEFAULTMODE );
  12423. %@AS@%       _outtext( "Error:  can't draw chart" );
  12424. %@AS@%    }
  12425. %@AS@%    else
  12426. %@AS@%    {
  12427. %@AS@%       getch(); 
  12428. %@AS@%       _setvideomode( _DEFAULTMODE );
  12429. %@AS@%    }
  12430. %@AS@%    return( 0 );
  12431. %@AS@%  } %@AE@%%@NL@%
  12432. %@NL@%
  12433. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  12434. %@NL@%
  12435. %@NL@%
  12436. %@4@%%@AB@%Example: Bar Chart%@AE@%%@EH@%%@NL@%
  12437. %@NL@%
  12438. The code for the PIE.C program needs only minor alterations to produce bar,
  12439. column, and line charts for the same data:  %@NL@%
  12440. %@NL@%
  12441. %@NL@%
  12442.   ■   Replace the call to %@AB@%_pg_chartpie%@AE@% with %@AB@%_pg_chart%@AE@%. This function
  12443.       produces bar, column, and line charts depending on the value of the
  12444.       second argument for %@AB@%_pg_defaultchart%@AE@%.%@NL@%
  12445. %@NL@%
  12446.   ■   Give new arguments to %@AB@%_pg_defaultchart%@AE@% that specify chart type and
  12447.       style.%@NL@%
  12448. %@NL@%
  12449.   ■   Assign titles for the %@AI@%x%@AE@% axis and %@AI@%y%@AE@% axis in the structure %@AS@% env %@AE@%.%@NL@%
  12450. %@NL@%
  12451.   ■   Remove references to array %@AS@% explode %@AE@%(applicable only to pie charts).%@NL@%
  12452. %@NL@%
  12453. %@NL@%
  12454. The following example produces the bar chart shown in Figure 14.3.  %@NL@%
  12455. %@NL@%
  12456. %@AS@%  /* BAR.C:  Create sample bar chart. */
  12457. %@AS@%  #include <conio.h>
  12458. %@AS@%  #include <string.h>
  12459. %@AS@%  #include <graph.h>
  12460. %@AS@%  #include <pgchart.h>
  12461. %@AS@%  #define MONTHS 12
  12462. %@AS@%  typedef enum {FALSE, TRUE} boolean;
  12463. %@AS@%  float far value[MONTHS] = 
  12464. %@AS@%  { 
  12465. %@AS@%     33.0, 27.0, 42.0, 64.0,106.0,157.0,
  12466. %@AS@%    182.0,217.0,128.0, 62.0, 43.0, 36.0
  12467. %@AS@%  };
  12468. %@AS@%  char far *category[MONTHS] =
  12469. %@AS@%  {
  12470. %@AS@%    "Jan", "Feb", "Mar", "Apr",
  12471. %@AS@%    "May", "Jun", "Jly", "Aug",
  12472. %@AS@%    "Sep", "Oct", "Nov", "Dec"
  12473. %@AS@%  };
  12474. %@AS@%  
  12475. %@AS@%  main()
  12476. %@AS@%  {
  12477. %@AS@%    chartenv env;
  12478. %@AS@%    int mode = _VRES16COLOR;
  12479. %@AS@%    /* Set highest video mode available */
  12480. %@AS@%    while(!_setvideomode( mode ))
  12481. %@AS@%       mode--;
  12482. %@AS@%    if(mode == _TEXTMONO)
  12483. %@AS@%       return(0);
  12484. %@AS@%  
  12485. %@AS@%    /* Initialize chart library and a default bar chart */       
  12486. %@AS@%    _pg_initchart();
  12487. %@AS@%    _pg_defaultchart( &env, _PG_BARCHART, _PG_PLAINBARS );
  12488. %@AS@%  
  12489. %@AS@%    /* Add titles and some chart options */
  12490. %@AS@%    strcpy( env.maintitle.title, "Good Neighbor Grocery" );
  12491. %@AS@%    env.maintitle.titlecolor = 6;
  12492. %@AS@%    env.maintitle.justify = _PG_RIGHT;
  12493. %@AS@%    strcpy( env.subtitle.title, "Orange Juice Sales" );
  12494. %@AS@%    env.subtitle.titlecolor = 6;
  12495. %@AS@%    env.subtitle.justify = _PG_RIGHT;
  12496. %@AS@%    strcpy( env.yaxis.axistitle.title, "Months" );
  12497. %@AS@%    strcpy( env.xaxis.axistitle.title, "Quantity (cases)" );
  12498. %@AS@%    env.chartwindow.border = FALSE;
  12499. %@AS@%  
  12500. %@AS@%    /* Parameters for call to _pg_chart are:
  12501. %@AS@%     *    env        - Environment variable
  12502. %@AS@%     *    category   - Category labels
  12503. %@AS@%     *    value      - Data to chart
  12504. %@AS@%     *    MONTHS     - Number of data values */
  12505. %@AS@%    if(_pg_chart( &env, category, value, MONTHS ))
  12506. %@AS@%    {
  12507. %@AS@%       _setvideomode( _DEFAULTMODE );
  12508. %@AS@%       _outtext( "Error:  can't draw chart" );
  12509. %@AS@%    }
  12510. %@AS@%    else
  12511. %@AS@%    {
  12512. %@AS@%        getch(); 
  12513. %@AS@%        _setvideomode( _DEFAULTMODE );
  12514. %@AS@%     }
  12515. %@AS@%     return(0);
  12516. %@AS@%  }%@AE@%%@NL@%
  12517. %@NL@%
  12518. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  12519. %@NL@%
  12520. %@NL@%
  12521. %@4@%%@AB@%Example: Column Chart%@AE@%%@EH@%%@NL@%
  12522. %@NL@%
  12523. The grocer's bar chart becomes a column chart in two easy steps. Simply
  12524. specify the new chart type when calling %@AB@%_pg_defaultchart%@AE@% and switch the axis
  12525. titles. To produce a column chart for the data, replace the call to%@AB@%
  12526. %@AB@%_pg_defaultchart%@AE@% with:  %@NL@%
  12527. %@NL@%
  12528. %@AS@%  _pg_defaultchart( &env, _PG_COLUMNCHART, _PG_PLAINBARS );%@AE@%%@NL@%
  12529. %@NL@%
  12530. and replace the last two calls to %@AB@%strcpy%@AE@% with:  %@NL@%
  12531. %@NL@%
  12532. %@AS@%  strcpy( env.xaxis.axistitle.title, "Months" );
  12533. %@AS@%  strcpy( env.yaxis.axistitle.title, "Quantity (cases)" );%@AE@%%@NL@%
  12534. %@NL@%
  12535. Notice that now the %@AI@%x%@AE@% axis is labeled "Months" and the %@AI@%y %@AE@%axis is labeled
  12536. "Quantity (cases)." Figure 14.4 shows the resulting column chart.  %@NL@%
  12537. %@NL@%
  12538. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  12539. %@NL@%
  12540. %@NL@%
  12541. %@4@%%@AB@%Example: Line Chart%@AE@%%@EH@%%@NL@%
  12542. %@NL@%
  12543. Creating an equivalent line chart requires only one change. Use the same
  12544. code as for the column chart and replace the call to %@AB@%_pg_defaultchart%@AE@% with:
  12545. %@NL@%
  12546. %@NL@%
  12547. %@AS@%  _pg_defaultchart( &env, _PG_LINECHART, _PG_POINTANDLINE );%@AE@%%@NL@%
  12548. %@NL@%
  12549. Figure 14.5 shows the line chart for the grocer's data.  %@NL@%
  12550. %@NL@%
  12551. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  12552. %@NL@%
  12553. %@NL@%
  12554. %@4@%%@AB@%Example: Scatter Diagram%@AE@%%@EH@%%@NL@%
  12555. %@NL@%
  12556. Now suppose that the store owner wants to compare the sales of orange juice
  12557. to the sales of another product, say hot chocolate. Possible monthly sales
  12558. are shown below.  %@NL@%
  12559. %@NL@%
  12560. %@TH:  15   729 02 12 22 42 @%Months      Orange Juice (cases)  Hot Chocolate (cases)%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%January     33                    37February    27                    37March       42                    30April       64                    19May         106                   10June        157                   5July        182                   2August      217                   1September   128                   7%@AB@%%@AE@%October     62                    15November    43                    28December    36                    39%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  15   729 02 12 22 42 @%
  12561.  
  12562. The program SCATTER.C displays a scatter diagram that illustrates the
  12563. relationship between the sales of orange juice and hot chocolate throughout
  12564. a 12-month period.  %@NL@%
  12565. %@NL@%
  12566. %@AS@%  /* SCATTER.C:  Create sample scatter diagram. */
  12567. %@AS@%  
  12568. %@AS@%  #include <conio.h>
  12569. %@AS@%  #include <string.h>
  12570. %@AS@%  #include <graph.h>
  12571. %@AS@%  #include <pgchart.h>
  12572. %@AS@%  
  12573. %@AS@%  #define MONTHS 12
  12574. %@AS@%  typedef enum {FALSE, TRUE} boolean;
  12575. %@AS@%  
  12576. %@AS@%  /* Orange juice sales */
  12577. %@AS@%  
  12578. %@AS@%  float far xvalue[MONTHS] = 
  12579. %@AS@%  { 
  12580. %@AS@%     33.0, 27.0, 42.0, 64.0,106.0,157.0,
  12581. %@AS@%    182.0,217.0,128.0, 62.0, 43.0, 36.0
  12582. %@AS@%  };
  12583. %@AS@%  /* Hot chocolate sales */
  12584. %@AS@%  
  12585. %@AS@%  float far yvalue[MONTHS] = 
  12586. %@AS@%  { 
  12587. %@AS@%    37.0, 37.0, 30.0, 19.0, 10.0,  5.0,
  12588. %@AS@%     2.0,  1.0,  7.0, 15.0, 28.0, 39.0
  12589. %@AS@%  };
  12590. %@AS@%  
  12591. %@AS@%  main()
  12592. %@AS@%  {
  12593. %@AS@%    chartenv env;
  12594. %@AS@%    int mode = _VRES16COLOR;
  12595. %@AS@%  
  12596. %@AS@%    /* Set highest video mode available */
  12597. %@AS@%  
  12598. %@AS@%    while(!_setvideomode( mode ))
  12599. %@AS@%       mode--;
  12600. %@AS@%    if(mode == _TEXTMONO)
  12601. %@AS@%       return(0);%@AE@%%@NL@%
  12602. %@NL@%
  12603. %@AS@%    /* Initialize chart library and default
  12604. %@AS@%     * scatter diagram
  12605. %@AS@%     */
  12606. %@AS@%    _pg_initchart();
  12607. %@AS@%    _pg_defaultchart( &env, _PG_SCATTERCHART,
  12608. %@AS@%                      _PG_POINTONLY );
  12609. %@AS@%  
  12610. %@AS@%    /* Add titles and some chart options */
  12611. %@AS@%  
  12612. %@AS@%    strcpy( env.maintitle.title, "Good Neighbor Grocery" );
  12613. %@AS@%    env.maintitle.titlecolor = 6;
  12614. %@AS@%    env.maintitle.justify = _PG_RIGHT;
  12615. %@AS@%    strcpy( env.subtitle.title,
  12616. %@AS@%            "Orange Juice vs Hot Chocolate" );
  12617. %@AS@%    env.subtitle.titlecolor = 6;
  12618. %@AS@%    env.subtitle.justify = _PG_RIGHT;
  12619. %@AS@%    env.yaxis.grid = TRUE;
  12620. %@AS@%    strcpy( env.xaxis.axistitle.title,
  12621. %@AS@%            "Orange Juice Sales" );
  12622. %@AS@%    strcpy( env.yaxis.axistitle.title,
  12623. %@AS@%            "Hot Chocolate Sales" );
  12624. %@AS@%    env.chartwindow.border = FALSE;
  12625. %@AS@%  
  12626. %@AS@%    /* Parameters for call to _pg_chartscatter are:
  12627. %@AS@%     *    env        - Environment variable
  12628. %@AS@%     *    xvalue     - X-axis data
  12629. %@AS@%     *    yvalue     - Y-axis data
  12630. %@AS@%     *    MONTHS     - Number of data values
  12631. %@AS@%     */%@AE@%%@NL@%
  12632. %@NL@%
  12633. %@AS@%    if(_pg_chartscatter( &env, xvalue,
  12634. %@AS@%                          yvalue, MONTHS ))
  12635. %@AS@%    {
  12636. %@AS@%       _setvideomode( _DEFAULTMODE );
  12637. %@AS@%       _outtext( "Error:  can't draw chart" );
  12638. %@AS@%    }
  12639. %@AS@%    else
  12640. %@AS@%    {
  12641. %@AS@%       getch(); 
  12642. %@AS@%       _setvideomode( _DEFAULTMODE );
  12643. %@AS@%    }
  12644. %@AS@%    return(0);
  12645. %@AS@%  } %@AE@%%@NL@%
  12646. %@NL@%
  12647. Figure 14.6 shows the results of SCATTER.C. Notice that the scatter points
  12648. form a slightly curved line, indicating a correlation exists between the
  12649. sales of the two products. The store owner can conclude from the scatter
  12650. diagram that the demand for orange juice is roughly inverse to the demand
  12651. for hot chocolate.  %@NL@%
  12652. %@NL@%
  12653. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  12654. %@NL@%
  12655. %@NL@%
  12656. %@2@%%@CR:C6A00140005 @%%@AB@%Palettes%@AE@%%@EH@%%@NL@%
  12657. %@NL@%
  12658. Presentation Graphics displays each data series in a way that makes it
  12659. discernible from other series. It does this by defining a separate "palette"
  12660. for every data series in a chart. Palettes consist of entries that determine
  12661. color, line style, fill pattern, and plot character used to graph the
  12662. series.  %@NL@%
  12663. %@NL@%
  12664. Presentation Graphics maintains its palettes as an array of structures. The
  12665. header file PGCHART.H defines the palette structures as:  %@NL@%
  12666. %@NL@%
  12667. %@AS@%  /* Typedef for pattern bitmap */
  12668. %@AS@%  typedef unsigned char fillmap[8];
  12669. %@AS@%  
  12670. %@AS@%  /* Typedef for palette entry definition */
  12671. %@AS@%  typedef struct 
  12672. %@AS@%  {
  12673. %@AS@%    unsigned short color;
  12674. %@AS@%    unsigned short style;
  12675. %@AS@%    fillmap        fill;
  12676. %@AS@%    char           plotchar;
  12677. %@AS@%  } paletteentry;
  12678. %@AS@%  
  12679. %@AS@%  /* Typedef for palette definition */
  12680. %@AS@%  typedef paletteentry palettetype[_PG_PALETTELEN];%@AE@%%@NL@%
  12681. %@NL@%
  12682. It's important not to confuse the Presentation Graphics palettes with the
  12683. adapter display palettes, which are register values kept by the video
  12684. controller. The function %@AB@%_selectpalette%@AE@% described in Chapter 13, "Graphics,"
  12685. sets the display palette. It does not define the data series palettes used
  12686. by Presentation Graphics.  %@NL@%
  12687. %@NL@%
  12688. %@NL@%
  12689. %@3@%%@CR:C6A00140006 @%%@AB@%Color Pool%@AE@%%@EH@%%@NL@%
  12690. %@NL@%
  12691. Presentation Graphics organizes all chart colors into a "color pool." The
  12692. color pool consists of pixel values valid for the current graphics mode.
  12693. (Refer to Chapter 13, "Graphics," or the Glossary for a definition of pixel
  12694. values.) Palette structures contain color codes that refer to the color
  12695. pool. A palette's color code determines the color used to graph the data
  12696. series associated with the palette. Colors of labels, titles, legends, and
  12697. axes are also determined by the contents of the color pool.  %@NL@%
  12698. %@NL@%
  12699. The first element of the color pool is always 0, which is the pixel value
  12700. for the screen background color. The second element is always the highest
  12701. pixel value available for the graphics mode. The remaining elements are
  12702. repeating sequences of available pixel values, beginning with 1.  %@NL@%
  12703. %@NL@%
  12704. As shown above, the first member of a palette data structure is:  %@NL@%
  12705. %@NL@%
  12706. %@AS@%  unsigned short color;%@AE@%%@NL@%
  12707. %@NL@%
  12708. This variable defines the color code for the data series associated with the
  12709. palette. The color code is neither a display attribute nor a pixel value. It
  12710. is an index number of the color pool.  %@NL@%
  12711. %@NL@%
  12712. An example should make this clearer. A graphics mode of %@AB@%_MRES4COLOR%@AE@%  (320 x
  12713. 200 graphics) provides four colors for display. Pixel values from 0 to 3
  12714. determine the possible pixel colors─say, black, green, red, and brown
  12715. respectively. In this case the first 8 elements of the color pool would be
  12716. the following:  %@NL@%
  12717. %@NL@%
  12718. %@TH:  11   550 02 18 13 45 @%Color Pool Index  Pixel Value  Color%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%0                 0            Black1                 3            Brown2                 1            Green3                 2            Red4                 3            Brown5                 1            Green6                 2            Red7                 3            Brown%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  11   550 02 18 13 45 @%
  12719.  
  12720. Notice that the sequence of available foreground colors repeats from the
  12721. third element. The first data series in this case would be plotted in brown,
  12722. the second series in green, the third series in red, the fourth series again
  12723. in brown, and so forth.  %@NL@%
  12724. %@NL@%
  12725. Video adapters such as the EGA or the Hercules InColor(tm) Card allow 16
  12726. on-screen colors. This allows Presentation Graphics to graph more series
  12727. without duplicating colors.  %@NL@%
  12728. %@NL@%
  12729. %@NL@%
  12730. %@3@%%@CR:C6A00140007 @%%@AB@%Style Pool%@AE@%%@EH@%%@NL@%
  12731. %@NL@%
  12732. Presentation Graphics matches the color pool with a collection of different
  12733. line styles called the "style pool." Entries in the style pool define the
  12734. appearance of lines such as axes and grids. Lines can be solid, dotted,
  12735. dashed, or of some combination.  %@NL@%
  12736. %@NL@%
  12737. The second member of a palette structure defines a style code as:  %@NL@%
  12738. %@NL@%
  12739. %@AS@%  unsigned short style;%@AE@%%@NL@%
  12740. %@NL@%
  12741. Each palette contains a style code that refers to an entry in the style pool
  12742. in the same way that it contains a color code that refers to an entry in the
  12743. color pool. The style code value in a palette is applicable only to line
  12744. graphs and lined scatter diagrams. The style code determines the appearance
  12745. of the lines drawn between points.  %@NL@%
  12746. %@NL@%
  12747. The palette's style code adds further variety to the lines of a multiseries
  12748. graph. It is most useful when the number of lines in a chart exceeds the
  12749. number of available colors. For example, a graph of nine different data
  12750. series must repeat colors if only three foreground colors are available for
  12751. display. However, the style code for each color repetition will be
  12752. different, ensuring that none of the lines looks the same.  %@NL@%
  12753. %@NL@%
  12754. %@NL@%
  12755. %@3@%%@CR:C6A00140008 @%%@AB@%Pattern Pool%@AE@%%@EH@%%@NL@%
  12756. %@NL@%
  12757. Presentation Graphics also maintains a pool of "fill patterns." Patterns
  12758. determine the fill design for column, bar, and pie charts. The third member
  12759. of a palette structure holds the palette's fill pattern. The pattern member
  12760. is an array:  %@NL@%
  12761. %@NL@%
  12762. %@AS@%  fillmap fill;%@AE@%%@NL@%
  12763. %@NL@%
  12764. where %@AS@% fillmap %@AE@% is type-defined as:  %@NL@%
  12765. %@NL@%
  12766. %@AS@%  typedef unsigned char fillmap[8];%@AE@%%@NL@%
  12767. %@NL@%
  12768. Each fill pattern array holds an 8 x 8 bit map that defines the fill pattern
  12769. for the data series associated with the palette. Table 14.2 shows how a fill
  12770. pattern of diagonal stripes is created with the %@AS@% fill %@AE@% pattern array.  %@NL@%
  12771. %@NL@%
  12772. The bit map below corresponds to screen pixels. Each of the 8 layers of the
  12773. map are binary numbers, where a solid circle signifies 1 and an open circle
  12774. signifies 0. Thus the first layer of the map─that is, the first
  12775. byte─represents the binary number 10011001, which is the decimal number 153.
  12776. %@NL@%
  12777. %@NL@%
  12778. %@AB@%Table 14.2  %@AB@%Fill Patterns%@AE@%%@AE@%
  12779.  
  12780. %@TH:  11   765 02 35 41 @%Bit Map                            Value in fill%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%  ∙  ∙      ∙  ∙               %@AS@% fill[0]%@AE@% = 153    ∙  ∙      ∙  ∙             %@AS@% fill[1]%@AE@% = 204∙      ∙  ∙      ∙             %@AS@% fill[2]%@AE@% = 102∙  ∙      ∙  ∙                 %@AS@% fill[3]%@AE@% =   51  ∙  ∙      ∙  ∙               %@AS@% fill[4]%@AE@% = 153    ∙  ∙      ∙  ∙             %@AS@% fill[5]%@AE@% = 204∙      ∙  ∙      ∙             %@AS@% fill[6]%@AE@% = 102∙  ∙      ∙  ∙                 %@AS@% fill[7]%@AE@% =   51%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  11   765 02 35 41 @%
  12781.  
  12782. If you wish to create the above pattern for your chart's first data series,
  12783. you must reset the %@AS@% fill %@AE@% array for the first palette structure. You can do
  12784. this in five steps:  %@NL@%
  12785. %@NL@%
  12786. %@NL@%
  12787.   1.  Declare a structure of type %@AB@%palettetype%@AE@% to hold the palette
  12788.       parameters.%@NL@%
  12789. %@NL@%
  12790.   2.  Call %@AB@%_pg_initchart%@AE@% to initialize the palettes with default values.%@NL@%
  12791. %@NL@%
  12792.   3.  Call the Presentation Graphics function %@AB@%_pg_getpalette%@AE@% to retrieve a
  12793.       copy of the current palette data.%@NL@%
  12794. %@NL@%
  12795.   4.  Assign the values given in Table 14.2 to the array %@AS@% fill %@AE@% for the
  12796.       first palette. %@NL@%
  12797. %@NL@%
  12798.   5.  Call the Presentation Graphics function %@AB@%_pg_setpalette%@AE@% to load the
  12799.       modified palette values.%@NL@%
  12800. %@NL@%
  12801. %@NL@%
  12802. The following lines of code demonstrate these five steps:  %@NL@%
  12803. %@NL@%
  12804. %@AS@%  /* Declare a structure array for palette data. */
  12805. %@AS@%  
  12806. %@AS@%  palettetype palette_struct;
  12807. %@AS@%  .
  12808. %@AS@%  .
  12809. %@AS@%  .
  12810. %@AS@%  /* Initialize chart library */
  12811. %@AS@%  
  12812. %@AS@%  _pg_initchart();
  12813. %@AS@%  .
  12814. %@AS@%  .
  12815. %@AS@%  .
  12816. %@AS@%  /* Copy current palette data into palette_struct */
  12817. %@AS@%  
  12818. %@AS@%  _pg_getpalette( palette_struct );
  12819. %@AS@%  
  12820. %@AS@%  /* Reinitialize fill pattern for first palette using
  12821. %@AS@%     values in Table 14.2 */
  12822. %@AS@%  
  12823. %@AS@%  palette_struct[1].fill[0] = 153;
  12824. %@AS@%  palette_struct[1].fill[1] = 204;
  12825. %@AS@%  palette_struct[1].fill[2] = 102;
  12826. %@AS@%  palette_struct[1].fill[3] =  51;
  12827. %@AS@%  palette_struct[1].fill[4] = 153;
  12828. %@AS@%  palette_struct[1].fill[5] = 204;
  12829. %@AS@%  palette_struct[1].fill[6] = 102;
  12830. %@AS@%  palette_struct[1].fill[7] =  51;
  12831. %@AS@%  
  12832. %@AS@%  /* Load new palette data */
  12833. %@AS@%  
  12834. %@AS@%  _pg_setpalette( palette_struct );%@AE@%%@NL@%
  12835. %@NL@%
  12836. Now when you display your bar or column chart the first series appears
  12837. filled with the striped pattern shown in Table 14.2.  %@NL@%
  12838. %@NL@%
  12839. Pie charts are a bit different. The idea of multiple series does not really
  12840. apply to them. Instead, palette structures correspond to individual slices.
  12841. If the number of slices exceeds the constant %@AB@%_PG_PALETTELEN%@AE@%, palettes are
  12842. recycled. Thus the first palette dictates not only the appearance of the
  12843. first slice, but of slice number %@AB@%_PG_PALETTELEN%@AE@% as well. The second palette
  12844. determines the appearance of both the second slice and of slice number
  12845. %@AB@%_PG_PALETTELEN%@AE@% + 1, and so forth.  %@NL@%
  12846. %@NL@%
  12847. %@NL@%
  12848. %@3@%%@CR:C6A00140009 @%%@AB@%Character Pool%@AE@%%@EH@%%@NL@%
  12849. %@NL@%
  12850. The last member of a palette structure is an index number in a pool of ASCII
  12851. characters:  %@NL@%
  12852. %@NL@%
  12853. %@AS@%  char plotchar;%@AE@%%@NL@%
  12854. %@NL@%
  12855. The member %@AS@%plotchar %@AE@% represents plot points on line graphs and scatter
  12856. diagrams. Each palette uses a different character to distinguish plot points
  12857. between data series.  %@NL@%
  12858. %@NL@%
  12859. %@NL@%
  12860. %@2@%%@CR:C6A00140010 @%%@AB@%Customizing Presentation Graphics%@AE@%%@EH@%%@NL@%
  12861. %@NL@%
  12862. Presentation Graphics is built for flexibility. You can use its system of
  12863. default values to produce professional-looking charts with a minimum of
  12864. programming effort. Or you can fine-tune the appearance of your charts by
  12865. overriding default values and initializing variables explicitly in your
  12866. program. The following section shows you how.  %@NL@%
  12867. %@NL@%
  12868. %@NL@%
  12869. %@3@%%@CR:C6A00140011 @%%@AB@%Chart Environment%@AE@%%@EH@%%@NL@%
  12870. %@NL@%
  12871. The header file PGCHART.H defines a structure type %@AB@%chartenv%@AE@%. This structure
  12872. type organizes the set of variables known as the "chart environment." The
  12873. chart environment describes everything about a chart except the plots
  12874. themselves. It's the blank page, in other words, ready for plotting data.
  12875. The environment determines the appearance of text, axes, grid lines, and
  12876. legends.  %@NL@%
  12877. %@NL@%
  12878. Calling the %@AB@%_pg_defaultchart%@AE@% function fills the chart environment with
  12879. default values. Presentation Graphics allows you to reset any variable in
  12880. the environment before displaying a chart. Except for adjusting the palette
  12881. values, all initialization of data is done through a %@AB@%chartenv%@AE@% type
  12882. structure.  %@NL@%
  12883. %@NL@%
  12884. The sample chart programs provided earlier illustrate how to adjust
  12885. variables in the chart environment. These programs create a structure %@AS@% env %@AE@%
  12886. of the type %@AB@%chartenv%@AE@%. The structure %@AS@% env %@AE@% contains the chart environment
  12887. variables, initialized by the call to %@AB@%_pg_defaultchart%@AE@%. Environment
  12888. variables such as the chart title are then given specific values, as in:  %@NL@%
  12889. %@NL@%
  12890. %@AS@%  strcpy( env.maintitle.title, "Good Neighbor Grocery" );%@AE@%%@NL@%
  12891. %@NL@%
  12892. Environment variables that determine colors and line styles deserve special
  12893. mention. The chart environment holds several such variables, which can be
  12894. recognized by their names. For example, the variable %@AI@%titlecolor%@AE@% specifies
  12895. the color of title text. Similarly, the variable %@AI@%gridstyle%@AE@% specifies the
  12896. line style used to draw the chart grid.  %@NL@%
  12897. %@NL@%
  12898. %@AU@% Colors and line styles in the chart environment are taken from palettes.%@AE@%  %@NL@%
  12899. %@NL@%
  12900. These variables are index numbers, but do not refer directly to the color
  12901. pool or line pool. They correspond instead to palette numbers. If you set
  12902. %@AI@%titlecolor%@AE@% to 2, Presentation Graphics uses the color code in the second
  12903. palette to determine the title's color. Thus the title in this case would be
  12904. the same color as the chart's second data series. If you change the color
  12905. code in the palette, you'll also change the title's color.  %@NL@%
  12906. %@NL@%
  12907. A structure of type %@AB@%chartenv%@AE@% consists of four secondary structures. The file
  12908. PGCHART.H type-defines the secondary structures as:  %@NL@%
  12909. %@NL@%
  12910. %@AS@%  titletype
  12911. %@AS@%  axistype
  12912. %@AS@%  windowtype
  12913. %@AS@%  legendtype%@AE@%%@NL@%
  12914. %@NL@%
  12915. The remainder of this section describes the chart environment of
  12916. Presentation Graphics. It first examines structures of the four secondary
  12917. types that make up the chart environment structure. The section concludes
  12918. with a description of the %@AB@%chartenv%@AE@% structure type. Each discussion begins
  12919. with a brief explanation of the structure's purpose, followed by a listing
  12920. of the structure type definition as it appears in the PGCHART.H file. All
  12921. symbolic constants are defined in the file PGCHART.H.  %@NL@%
  12922. %@NL@%
  12923. %@NL@%
  12924. %@3@%%@CR:C6A00140012 @%%@AB@%titletype%@AE@%%@EH@%%@NL@%
  12925. %@NL@%
  12926. Structures of type %@AB@%titletype%@AE@% determine text, color, and placement of titles
  12927. appearing in the graph. The PGCHART.H file defines the structure type as:  %@NL@%
  12928. %@NL@%
  12929. %@AS@%  typedef struct
  12930. %@AS@%  {
  12931. %@AS@%    char     title[_PG_TITLELEN];  /* Title text */
  12932. %@AS@%    short    titlecolor;           /* Palette color
  12933. %@AS@%                                      for title text */
  12934. %@AS@%    short    justify;              /* _PG_LEFT, _PG_CENTER,
  12935. %@AS@%                                      _PG_RIGHT */
  12936. %@AS@%  } titletype;%@AE@%%@NL@%
  12937. %@NL@%
  12938. The following list describes %@AB@%titletype%@AE@% members:  %@NL@%
  12939. %@NL@%
  12940. %@AB@%Member Variable%@AE@%                   %@AB@%Description%@AE@%
  12941. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  12942. %@AI@%justify%@AE@%                           An integer specifying how the title is 
  12943.                                   justified
  12944.                                   within the chart window. The symbolic 
  12945.                                   constants
  12946.                                   defined in the PGCHART.H file for this 
  12947.                                   variable are %@AB@%_PG_LEFT%@AE@%, %@AB@%_PG_CENTER%@AE@%, and %@AB@%%@AE@%
  12948.                                   %@AB@%_PG_RIGHT%@AE@%.
  12949.  
  12950. %@AI@%titlecolor%@AE@%                        An integer between 1 and %@AB@%_PG_PALETTELEN%@AE@%
  12951.                                   that specifies a title's color. The 
  12952.                                   default value for %@AI@%titlecolor%@AE@% is 1.
  12953.  
  12954. %@AI@%title%@AE@%%@AB@%[_PG_TITLELEN]%@AE@%               A character array containing title text.
  12955.                                   For example, if %@AS@% env %@AE@% is a structure of 
  12956.                                   type%@AB@% chartenv%@AE@%, then %@AS@% env.maintitle.title%@AE@%
  12957.                                   holds the character string used for the 
  12958.                                   main title of the chart. Similarly, %@AS@%%@AE@%
  12959.                                   %@AS@%env.xaxis.axistitle.title%@AE@%  contains the 
  12960.                                   axis title. The number of characters in 
  12961.                                   a title must be one less than %@AB@%%@AE@%
  12962.                                   %@AB@%_PG_TITLELEN%@AE@% to allow room for a null 
  12963.                                   terminator. 
  12964.  
  12965. %@NL@%
  12966. %@3@%%@CR:C6A00140013 @%%@AB@%axistype%@AE@%%@EH@%%@NL@%
  12967. %@NL@%
  12968. Structures of type %@AB@%axistype%@AE@% contain variables for the axes such as color,
  12969. scale, grid style, and tick marks. The PGCHART.H file defines the structure
  12970. type as:  %@NL@%
  12971. %@NL@%
  12972. %@AS@%  typedef struct
  12973. %@AS@%  {
  12974. %@AS@%    short       grid;          /* TRUE=grid lines drawn;
  12975. %@AS@%                                  FALSE=no lines */
  12976. %@AS@%    short       gridstyle;     /* Style bytes for grid */
  12977. %@AS@%    titletype   axistitle;     /* Title definition
  12978. %@AS@%                                  for axis */
  12979. %@AS@%    short       axiscolor;     /* Color for axis */
  12980. %@AS@%    short       labeled;       /* TRUE=ticks marks and titles
  12981. %@AS@%                                  drawn */
  12982. %@AS@%    short       rangetype;     /* _PG_LINEARAXIS,
  12983. %@AS@%                                  _PG_LOGAXIS */
  12984. %@AS@%    float       logbase;       /* Base used if log axis */
  12985. %@AS@%    short       autoscale;     /* TRUE=next 7 values
  12986. %@AS@%                                  calculated by system */
  12987. %@AS@%    float       scalemin;      /* Minimum value of scale */
  12988. %@AS@%    float       scalemax;      /* Maximum value of scale */
  12989. %@AS@%    float       scalefactor;   /* Scale factor for data on
  12990. %@AS@%                                  this axis */
  12991. %@AS@%    titletype   scaletitle;    /* Title definition for
  12992. %@AS@%                                  scaling factor */
  12993. %@AS@%    float       ticinterval;   /* Distance between tick marks
  12994. %@AS@%                                  (world coord.) */
  12995. %@AS@%    short       ticformat;     /* _PG_EXPFORMAT or
  12996. %@AS@%                                  _PG_DECFORMAT */
  12997. %@AS@%    short       ticdecimals;   /* Number of decimals for tick
  12998. %@AS@%                                  labels (max=9) */
  12999. %@AS@%  } axistype;%@AE@%%@NL@%
  13000. %@NL@%
  13001. The following list describes %@AB@%axistype%@AE@% member variables:  %@NL@%
  13002. %@NL@%
  13003. %@AB@%Member Variable%@AE@%                   %@AB@%Description%@AE@%
  13004. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  13005. %@AI@%autoscale%@AE@%                         A boolean variable. If %@AI@%autoscale%@AE@% is %@AB@%TRUE%@AE@%,
  13006.                                   Presentation Graphics automatically 
  13007.                                   determines values
  13008.                                   for %@AI@% scalefactor%@AE@%, %@AI@%scalemax%@AE@%, %@AI@%scalemin%@AE@%, %@AI@%%@AE@%
  13009.                                   %@AI@%scaletitle%@AE@%, %@AI@%ticdecimals%@AE@%, %@AI@%ticformat%@AE@%, and %@AI@%%@AE@%
  13010.                                   %@AI@%ticinterval%@AE@% (see below).
  13011.                                   If %@AI@%autoscale%@AE@% equals %@AB@%FALSE%@AE@%, these seven 
  13012.                                   variables must be specified in your 
  13013.                                   program.
  13014.  
  13015. %@AI@%axiscolor%@AE@%                         An integer between 1 and %@AB@%_PG_PALETTELEN%@AE@% 
  13016.                                   that specifies the color used for the 
  13017.                                   axis and parallel grid lines. (See 
  13018.                                   description for %@AI@%gridstyle%@AE@% above.) Note 
  13019.                                   that this member does not determine the 
  13020.                                   color of the axis title. That selection 
  13021.                                   is made through the structure %@AI@%%@AE@%
  13022.                                   %@AI@%axistitle%@AE@%.
  13023.  
  13024. %@AI@%axistitle%@AE@%                         A%@AB@% titletype%@AE@% structure that defines the 
  13025.                                   title of the associated axis. The title 
  13026.                                   of the %@AI@%y%@AE@% axis displays vertically to the
  13027.                                   left of the %@AI@%y%@AE@% axis, and the title of the
  13028.                                   %@AI@%x%@AE@% axis displays horizontally below the %@AI@%x%@AE@%
  13029.                                   axis.
  13030.  
  13031. %@AI@%grid%@AE@%                              A boolean true/false value that 
  13032.                                   determines whether grid lines are drawn 
  13033.                                   for the associated axis. Grid lines span
  13034.                                   the data window perpendicular to the 
  13035.                                   axis.
  13036.  
  13037. %@AI@%gridstyle%@AE@%                         An integer between 1 and %@AB@%_PG_PALETTELEN%@AE@% 
  13038.                                   that specifies the grid's line style. 
  13039.                                   Lines can be solid, dashed, dotted, or 
  13040.                                   some combination. The default value for %@AI@%%@AE@%
  13041.                                   %@AI@%gridstyle%@AE@% is 1. Note that the color of 
  13042.                                   the parallel axis determines the color 
  13043.                                   of the grid lines. Thus the %@AI@%x%@AE@% axis grid 
  13044.                                   is the same color as the %@AI@%y%@AE@% axis, and the
  13045.                                   %@AI@%y%@AE@% axis grid is the same color as the %@AI@%x%@AE@% 
  13046.                                   axis.
  13047.  
  13048. %@AI@%labeled%@AE@%                           A boolean value that determines whether 
  13049.                                   tick marks and labels are drawn on the 
  13050.                                   axis. Axis labels should not be confused
  13051.                                   with axis titles. Axis labels are 
  13052.                                   numbers or descriptions such as "23.2" 
  13053.                                   or "January" attached to each tick mark.
  13054.  
  13055. %@AI@%logbase%@AE@%                           If %@AI@%rangetype%@AE@% is logarithmic, the %@AI@%logbase%@AE@%
  13056.                                   variable determines the log base used to
  13057.                                   scale the axis. Default value is 10.
  13058.  
  13059. %@AI@%rangetype%@AE@%                         An integer that determines whether the 
  13060.                                   scale of the axis is linear or 
  13061.                                   logarithmic. The %@AI@%rangetype%@AE@% variable 
  13062.                                   applies only to value data.
  13063.  
  13064.                                   Specify a linear scale with the %@AB@%%@AE@%
  13065.                                   %@AB@%_PG_LINEARAXIS%@AE@% constant. A linear scale 
  13066.                                   is best when the difference between axis
  13067.                                   minimum and maximum is relatively small.
  13068.                                   For example, a linear axis range 0-10 
  13069.                                   results in 10 tick marks evenly spaced 
  13070.                                   along the axis.
  13071.  
  13072.                                   Use %@AB@%_PG_LOGAXIS%@AE@% to specify a logarithmic
  13073.                                   %@AI@%rangetype%@AE@%. Logarithmic scales are useful
  13074.                                   when the range is very large or when the
  13075.                                   data varies exponentially. Line graphs 
  13076.                                   of exponentially varying data can be 
  13077.                                   made straight with a logarithmic %@AI@%%@AE@%
  13078.                                   %@AI@%rangetype%@AE@%.
  13079.  
  13080. %@AI@%scalefactor%@AE@%                       All numeric data are scaled by dividing 
  13081.                                   each value by %@AI@%scalefactor%@AE@%. For 
  13082.                                   relatively small values, the variable %@AI@%%@AE@%
  13083.                                   %@AI@%scalefactor%@AE@% should be 1, which is the 
  13084.                                   default.
  13085.                                   But data with large values should be 
  13086.                                   scaled by an
  13087.                                   appropriate factor. For example, data in
  13088.                                   the range
  13089.                                   2 million-20 million should be plotted 
  13090.                                   with %@AI@%scale-min%@AE@% set to 2, %@AI@%scalemax%@AE@% set to
  13091.                                   20, and %@AI@%scalefactor%@AE@% set to 1 million.
  13092.  
  13093.                                   If %@AI@%autoscale%@AE@% is set to %@AB@%TRUE%@AE@%, 
  13094.                                   Presentation Graphics automatically 
  13095.                                   determines a suitable value for %@AI@%%@AE@%
  13096.                                   %@AI@%scalefactor%@AE@% based on the range of data 
  13097.                                   to be plotted. Presentation Graphics 
  13098.                                   selects only values that are
  13099.                                   a factor of 1 thousand─that is, values 
  13100.                                   such as 1
  13101.                                   thousand, 1 million, or 1 billion. It 
  13102.                                   then labels the %@AI@%scaletitle%@AE@% appropriately
  13103.                                   (see below). If you desire some other 
  13104.                                   value for scaling, you must set %@AI@%%@AE@%
  13105.                                   %@AI@%autoscale%@AE@% to %@AB@%FALSE%@AE@% and set %@AI@%scalefactor%@AE@% 
  13106.                                   to the desired scaling value.
  13107.  
  13108. %@AI@%scalemax%@AE@%                          Highest value represented by the axis. 
  13109.  
  13110. %@AI@%scalemin%@AE@%                          Lowest value represented by the axis.
  13111.  
  13112. %@AI@%scaletitle%@AE@%                        A %@AB@%titletype%@AE@% structure defining a string 
  13113.                                   of text that 
  13114.                                   describes the value of %@AI@%scalefactor%@AE@%. If %@AI@%%@AE@%
  13115.                                   %@AI@%autoscale%@AE@% is %@AB@%TRUE%@AE@%, Presentation Graphics
  13116.                                   automatically writes
  13117.                                   a scale description to %@AI@%scaletitle%@AE@%. If%@AI@% %@AE@%
  13118.                                   %@AI@%autoscale%@AE@% equals %@AB@%FALSE%@AE@% and %@AI@%scalefactor%@AE@% 
  13119.                                   is 1, %@AI@%scaletitle.title%@AE@% should be blank. 
  13120.                                   Otherwise your program should copy an 
  13121.                                   appropriate scale description to %@AI@%%@AE@%
  13122.                                   %@AI@%scaletitle.title%@AE@%, such as "( x 1000)," 
  13123.                                   "(in millions of units)," "times 10 
  13124.                                   thousand dollars," etc.
  13125.  
  13126.                                   For the %@AI@%y%@AE@% axis, the %@AI@%scaletitle%@AE@% text 
  13127.                                   displays vertically between the axis 
  13128.                                   title and the %@AI@%y%@AE@% axis. For the %@AI@%x %@AE@%axis,%@AI@% %@AE@%
  13129.                                   the scale title appears below the%@AI@% x%@AE@% axis
  13130.                                   title.
  13131.  
  13132. %@AI@%ticdecimals%@AE@%                       Number of digits to display after the 
  13133.                                   decimal point in tick labels. Maximum 
  13134.                                   value is 9. Note that this variable 
  13135.                                   applies only to axes with value data. It
  13136.                                   is
  13137.                                   ignored for the category axis.
  13138.  
  13139. %@AI@%ticformat%@AE@%                         An integer that determines the format of
  13140.                                   the labels assigned to each tick mark. 
  13141.                                   Set %@AI@%ticformat%@AE@% to %@AB@%_PG_EXPFORMAT%@AE@% for 
  13142.                                   exponential format or set it to  %@AB@%%@AE@%
  13143.                                   %@AB@%_PG_DECFORMAT%@AE@% for decimal. The default 
  13144.                                   is %@AB@%_PG_DECFORMAT%@AE@%. Note that this 
  13145.                                   variable applies only to axes with value
  13146.                                   data. It is ignored for the category 
  13147.                                   axis.
  13148.  
  13149. %@AI@%ticinterval%@AE@%                       Sets interval between tick marks on the 
  13150.                                   axis. The tick interval is measured in 
  13151.                                   the same units as the numeric data 
  13152.                                   associated with the axis. For example, 
  13153.                                   if 2 sequential tick marks correspond to
  13154.                                   the values 20 and 25, the tick interval 
  13155.                                   between them is 5. Note that this 
  13156.                                   variable applies only to axes with value
  13157.                                   data. It is ignored for the category 
  13158.                                   axis.
  13159.  
  13160. %@NL@%
  13161. %@3@%%@CR:C6A00140014 @%%@AB@%windowtype%@AE@%%@EH@%%@NL@%
  13162. %@NL@%
  13163. Structures of type %@AB@%windowtype%@AE@% contain sizes, locations, and color codes for
  13164. the three windows produced by Presentation Graphics: the chart window, the
  13165. data window, and the legend. Refer to the "Terminology" section at the
  13166. beginning of this chapter for definitions of these terms. Windows are
  13167. located on the screen relative to the screen's logical origin. By changing
  13168. the logical origin, you can display charts that are partly or completely off
  13169. the screen. The PGCHART.H file defines %@AB@%windowtype%@AE@% as:  %@NL@%
  13170. %@NL@%
  13171. %@AS@%  typedef struct
  13172. %@AS@%  {
  13173. %@AS@%    short  x1;            /* Left edge of window in
  13174. %@AS@%                             pixels */
  13175. %@AS@%    short  y1;            /* Top edge of window in
  13176. %@AS@%                             pixels */
  13177. %@AS@%    short  x2;            /* Right edge of window in
  13178. %@AS@%                             pixels */
  13179. %@AS@%    short  y2;            /* Bottom edge of window in
  13180. %@AS@%                             pixels */
  13181. %@AS@%    short  border;        /* TRUE for border, FALSE
  13182. %@AS@%                             otherwise */
  13183. %@AS@%    short  background;    /* Internal palette color for
  13184. %@AS@%                             window background */
  13185. %@AS@%    short  borderstyle;   /* Style bytes for window
  13186. %@AS@%                             border */
  13187. %@AS@%    short  bordercolor;   /* Internal palette color for
  13188. %@AS@%                             window border */
  13189. %@AS@%  } windowtype;%@AE@%%@NL@%
  13190. %@NL@%
  13191. The following list describes %@AB@%windowtype%@AE@% member variables:  %@NL@%
  13192. %@NL@%
  13193. %@AB@%Member Variable%@AE@%                   %@AB@%Description%@AE@%
  13194. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  13195. %@AI@%x1%@AE@%, %@AI@%y1%@AE@%, %@AI@%x2%@AE@%, %@AI@%y2%@AE@%                    Window coordinates in pixels. The 
  13196.                                   ordered pair (%@AI@%x1%@AE@%, %@AI@%y1%@AE@%) specifies the 
  13197.                                   coordinate of the upper left corner of 
  13198.                                   the window. The ordered pair (%@AI@%x2%@AE@%, %@AI@%y2%@AE@%) 
  13199.                                   specifies the coordinate of the lower 
  13200.                                   right corner.
  13201.  
  13202.                                   The reference point for the coordinates 
  13203.                                   depends on the type of window. The chart
  13204.                                   window is located relative to the 
  13205.                                   logical origin, usually the upper left 
  13206.                                   corner of the screen. The data and 
  13207.                                   legend windows are located relative to 
  13208.                                   the upper left corner of the chart 
  13209.                                   window. This allows you to change the 
  13210.                                   position of the chart window without 
  13211.                                   having to redefine coordinates for the 
  13212.                                   other two windows.
  13213.  
  13214. %@AI@%background%@AE@%                        An integer between 1 and %@AB@%_PG_PALETTELEN%@AE@% 
  13215.                                   that specifies the window's background 
  13216.                                   color. The default value for %@AI@%background%@AE@% 
  13217.                                   is 1.
  13218.  
  13219. %@AI@%border%@AE@%                            A boolean variable that determines 
  13220.                                   whether a border frame is drawn around a
  13221.                                   window. 
  13222.  
  13223. %@AI@%bordercolor%@AE@%                       An integer between 1 and %@AB@%_PG_PALETTELEN%@AE@% 
  13224.                                   that specifies the color of the window's
  13225.                                   border frame. The default value is 1.
  13226.  
  13227. %@AI@%borderstyle%@AE@%                       An integer between 1 and %@AB@%_PG_PALETTELEN%@AE@%
  13228.                                   that specifies the line style of the 
  13229.                                   window's border frame. The default value
  13230.                                   is 1.
  13231.  
  13232. %@NL@%
  13233. %@3@%%@CR:C6A00140015 @%%@AB@%legendtype%@AE@%%@EH@%%@NL@%
  13234. %@NL@%
  13235. Structures of type %@AB@%legendtype%@AE@% contain size, location, and colors of the
  13236. chart legend. The PGCHART.H file defines the structure type as:  %@NL@%
  13237. %@NL@%
  13238. %@AS@%  typedef struct
  13239. %@AS@%  {
  13240. %@AS@%    short      legend;        /* TRUE=draw legend;
  13241. %@AS@%                                 FALSE=no legend */
  13242. %@AS@%    short      place;         /* _PG_RIGHT, _PG_BOTTOM,
  13243. %@AS@%                                 _PG_OVERLAY */
  13244. %@AS@%    short      textcolor;     /* Palette color for text*/
  13245. %@AS@%    short      autosize;      /* TRUE=system calculates
  13246. %@AS@%                                 legend size */
  13247. %@AS@%    windowtypelegendwindow;  /* Window definition for
  13248. %@AS@%                                 legend */
  13249. %@AS@%  } legendtype;%@AE@%%@NL@%
  13250. %@NL@%
  13251. The following list describes %@AB@%legendtype%@AE@% member variables:  %@NL@%
  13252. %@NL@%
  13253. %@AB@%Member Variable%@AE@%                   %@AB@%Description%@AE@%
  13254. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  13255. %@AI@%autosize%@AE@%                          A boolean true/false variable that 
  13256.                                   determines whether Presentation Graphics
  13257.                                   is to automatically calculate the size 
  13258.                                   of the legend. If %@AI@%autosize%@AE@% equals %@AB@%FALSE%@AE@%,
  13259.                                   the legend window must be specified in 
  13260.                                   the %@AI@%legendwindow%@AE@% structure (see below).
  13261.  
  13262. %@AI@%legend%@AE@%                            A boolean true/false variable that 
  13263.                                   determines whether a legend is to appear
  13264.                                   on the chart. The %@AI@%legend%@AE@% variable is 
  13265.                                   ignored by functions that graph 
  13266.                                   single-series charts.
  13267.  
  13268. %@AI@%legendwindow%@AE@%                      A %@AB@%windowtype%@AE@% structure that defines 
  13269.                                   coordinates, background color, and 
  13270.                                   border frame for the legend. Coordinates
  13271.                                   given in %@AI@%legendwindow%@AE@% are ignored if %@AI@%%@AE@%
  13272.                                   %@AI@%autosize%@AE@% is %@AB@%TRUE%@AE@%.
  13273.  
  13274. %@AI@%place%@AE@%                             An integer that specifies the location 
  13275.                                   of the legend relative to the data 
  13276.                                   window. Setting the variable %@AI@%place%@AE@% equal
  13277.                                   to the constant %@AB@%_PG_RIGHT%@AE@% positions the 
  13278.                                   legend to the right of the data window. 
  13279.                                   Setting %@AI@%place%@AE@% to %@AB@%_PG_BOTTOM%@AE@% positions 
  13280.                                   the legend below the data window. 
  13281.                                   Setting %@AI@%place%@AE@% to %@AB@%_PG_OVERLAY%@AE@% positions 
  13282.                                   the legend within the data window.
  13283.  
  13284.                                   These settings influence the size of the
  13285.                                   data
  13286.                                   window. If %@AI@%place%@AE@% is equal to %@AB@%_PG_BOTTOM%@AE@% 
  13287.                                   or %@AB@%_PG_RIGHT%@AE@%, Presentation Graphics 
  13288.                                   automatically sizes the data window to 
  13289.                                   accommodate the legend.
  13290.                                   If %@AI@%place%@AE@% equals %@AB@%_PG_OVERLAY%@AE@% the data 
  13291.                                   window is sized without regard to the 
  13292.                                   legend.
  13293.  
  13294. %@AI@%textcolor%@AE@%                         An integer between 1 and %@AB@%_PG_PALETTELEN%@AE@% 
  13295.                                   that specifies the color of text within 
  13296.                                   the legend window.
  13297.  
  13298. %@NL@%
  13299. %@3@%%@CR:C6A00140016 @%%@AB@%chartenv%@AE@%%@EH@%%@NL@%
  13300. %@NL@%
  13301. A structure of type%@AB@% chartenv%@AE@% defines the chart environment. The following
  13302. code shows that a %@AB@%chartenv%@AE@% type structure consists almost entirely of
  13303. structures of the four types discussed above.  %@NL@%
  13304. %@NL@%
  13305. The PGCHART.H file defines the %@AB@%chartenv%@AE@% structure type as:  %@NL@%
  13306. %@NL@%
  13307. %@AS@%  typedef struct
  13308. %@AS@%  {
  13309. %@AS@%    short       charttype;     /* Chart type */
  13310. %@AS@%    short       chartstyle;    /* Chart style */
  13311. %@AS@%    windowtype  chartwindow;   /* Window definition for
  13312. %@AS@%                                  overall chart */
  13313. %@AS@%    windowtype  datawindow;    /* Window definition for data
  13314. %@AS@%                                  part of chart */
  13315. %@AS@%    titletype   maintitle;     /* Main chart title */
  13316. %@AS@%    titletype   subtitle;      /* Chart subtitle */
  13317. %@AS@%    axistype    xaxis;         /* Definition for x axis */
  13318. %@AS@%    axistype    yaxis;         /* Definition for y axis */
  13319. %@AS@%    legendtype  legend;        /* Definition for legend */
  13320. %@AS@%  } chartenv;%@AE@%%@NL@%
  13321. %@NL@%
  13322. %@AU@% Initialize the chart  environment with the _pg_defaultchart function.%@AE@%  %@NL@%
  13323. %@NL@%
  13324. Note that all the data in a %@AB@%chartenv%@AE@% type structure is initialized by
  13325. calling the %@AB@%_pg_defaultchart%@AE@% function. If your program does not call
  13326. %@AB@%_pg_defaultchart%@AE@%, it must explicitly define every variable in the chart
  13327. environment─a tedious and unnecessary procedure. The recommended method for
  13328. adjusting the appearance of your chart is to initialize variables for the
  13329. proper chart type by calling the %@AB@%_pg_defaultchart%@AE@% function, and then
  13330. reassign selected environment variables such as titles.  %@NL@%
  13331. %@NL@%
  13332. The following list describes %@AB@%chartenv%@AE@% member variables:  %@NL@%
  13333. %@NL@%
  13334. %@AB@%Member Variable%@AE@%                   %@AB@%Description%@AE@%
  13335. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  13336. %@AI@%chartstyle%@AE@%                        An integer that determines the style of 
  13337.                                   the chart
  13338.                                   (see Table 14.1). Legal values for %@AI@%%@AE@%
  13339.                                   %@AI@%chartstyle%@AE@%
  13340.                                   are %@AB@%_PG_PERCENT%@AE@% and %@AB@%_PG_NOPERCENT%@AE@%
  13341.                                   for pie charts; %@AB@%_PG_STACKEDBARS%@AE@% and %@AB@%%@AE@%
  13342.                                   %@AB@%_PG_PLAINBARS%@AE@% for bar and column charts;
  13343.                                   and %@AB@%_PG_POINTANDLINE%@AE@% and %@AB@%_PG_POINTONLY%@AE@% 
  13344.                                   for line graphs and scatter diagrams. 
  13345.                                   This variable corresponds to the third 
  13346.                                   argument for the %@AB@%_pg_defaultchart%@AE@% 
  13347.                                   function.
  13348.  
  13349. %@AI@%charttype%@AE@%                         An integer that determines the type of 
  13350.                                   chart displayed. The value of the 
  13351.                                   variable %@AI@%charttype%@AE@% is  %@AB@%_PG_BARCHART%@AE@%, %@AB@%%@AE@%
  13352.                                   %@AB@%_PG_COLUMNCHART%@AE@%, %@AB@%_PG_LINECHART%@AE@%, %@AB@%%@AE@%
  13353.                                   %@AB@%_PG_SCATTERCHART%@AE@%, or %@AB@%_PG_PIECHART%@AE@%. This 
  13354.                                   variable corresponds to the second 
  13355.                                   argument for the %@AB@%_pg_defaultchart%@AE@% 
  13356.                                   function.
  13357.  
  13358. %@AI@%chartwindow%@AE@%                       A %@AB@%windowtype%@AE@% structure that defines the 
  13359.                                   appearance of the chart window.
  13360.  
  13361. %@AI@%datawindow%@AE@%                        A %@AB@%windowtype%@AE@% structure that defines the 
  13362.                                   appearance of the data window.
  13363.  
  13364. %@AI@%legend%@AE@%                            A %@AB@%legendtype%@AE@% structure that defines the 
  13365.                                   appearance of the legend window.
  13366.  
  13367. %@AI@%maintitle%@AE@%                         A %@AB@%titletype%@AE@% structure that defines the 
  13368.                                   appearance of the main title of the 
  13369.                                   chart.
  13370.  
  13371. %@AI@%subtitle%@AE@%                          A %@AB@%titletype%@AE@% structure that defines the 
  13372.                                   appearance of the chart's subtitle.
  13373.  
  13374. %@AI@%xaxis%@AE@%                             An %@AB@%axistype%@AE@% structure that defines the 
  13375.                                   appearance of the %@AI@%x%@AE@% axis. (This variable
  13376.                                   is not applicable for pie charts.)
  13377.  
  13378. %@AI@%yaxis%@AE@%                             An %@AB@%axistype%@AE@% structure that defines the 
  13379.                                   appearance of the %@AI@%y%@AE@% axis. (This variable
  13380.                                   is not applicable for pie charts.)
  13381.  
  13382. %@NL@%
  13383. %@2@%%@CR:C6A00140017 @%%@AB@%An Overview of the Presentation Graphics Functions%@AE@%%@EH@%%@NL@%
  13384. %@NL@%
  13385. The chapter concludes with a few words about the 22 functions that make up
  13386. the Presentation Graphics library. They are listed in Table 14.3 for
  13387. convenient reference. Refer to Appendix B, "C Library Guide," or online help
  13388. for a description of the functions and their arguments.  %@NL@%
  13389. %@NL@%
  13390. %@AB@%Table 14.3  %@AB@%Presentation Graphics Functions%@AE@%%@AE@%
  13391.  
  13392. %@TH:  11   975 02 21 22 33 @%Primary Functions    Secondary Functions%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%_pg_initchart%@AE@%        %@AB@%_pg_hlabelchart%@AE@%       %@AB@%_pg_setpalette%@AE@%%@AB@%_pg_defaultchart%@AE@%     %@AB@%_pg_vlabelchart%@AE@%       %@AB@%_pg_resetpalette%@AE@%%@AB@%_pg_chart%@AE@%            %@AB@%_pg_analyzechart%@AE@%      %@AB@%_pg_getstyleset%@AE@%%@AB@%_pg_chartms%@AE@%          %@AB@%_pg_analyzechartms%@AE@%    %@AB@%_pg_setstyleset%@AE@%%@AB@%_pg_chartscatter%@AE@%     %@AB@%_pg_analyzescatter%@AE@%    %@AB@%_pg_resetstyleset%@AE@%%@AB@%_pg_chartscatterms%@AE@%   %@AB@%_pg_analyzescatterms%@AE@%  %@AB@%_pg_getchardef%@AE@%%@AB@%_pg_chartpie%@AE@%         %@AB@%_pg_analyzepie%@AE@%        %@AB@%_pg_setchardef%@AE@%                     %@AB@%_pg_getpalette%@AE@%%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  11   975 02 21 22 33 @%
  13393.  
  13394. In most cases you need only be concerned with seven of the routines, called
  13395. the "primary functions." These functions initialize variables and display
  13396. the selected chart types. As demonstrated in example programs earlier in
  13397. this chapter, you can create very acceptable charts with programs that call
  13398. only three of the Presentation Graphics primary functions.  %@NL@%
  13399. %@NL@%
  13400. The 15 secondary functions of Presentation Graphics do not directly display
  13401. charts. Most of them retrieve or set data in the Presentation Graphics chart
  13402. environment.  %@NL@%
  13403. %@NL@%
  13404. Of special interest among the secondary functions are the "analysis
  13405. functions," identified by the prefix %@AB@%_pg_analyze%@AE@% in their function names.
  13406. These five functions calculate default values that pertain to a given chart
  13407. type and data set. Calling an analysis function has the same effect as
  13408. calling a corresponding primary function, except that the chart is not
  13409. displayed. This allows you to pass on to the library the burden of
  13410. calculating values. You can then make modifications  to the resulting values
  13411. and call a primary routine to display the chart.  %@NL@%
  13412. %@NL@%
  13413. Use the %@AB@%_pg_hlabelchart%@AE@% and %@AB@%_pg_vlabelchart%@AE@% functions to display text on
  13414. your chart that is not part of a title or axis label. These functions enable
  13415. you to attach notes or other messages to your chart. You may also find them
  13416. useful for labeling separate lines of a multiseries line graph.  %@NL@%
  13417. %@NL@%
  13418. %@NL@%
  13419. %@NL@%
  13420. %@NL@%
  13421. %@NL@%
  13422. %@NL@%
  13423. %@CR:C6A00150001 @%%@1@%%@AB@%Chapter 15  Fonts%@AE@%%@EH@%%@NL@%
  13424. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  13425. %@NL@%
  13426. Preceding chapters have discussed how to write QuickC programs that generate
  13427. graphics and display charts. QuickC has yet another capability when it comes
  13428. to graphics: fonted text. Your programs can display various styles and sizes
  13429. of text in any graphics image or chart.  %@NL@%
  13430. %@NL@%
  13431. This chapter tells how. It assumes you have already read Chapter 13,
  13432. "Graphics." You should understand such terms as "graphics mode" and "text
  13433. mode," and be familiar with the functions %@AB@%_setvideomode%@AE@% and %@AB@%_moveto%@AE@%. Other
  13434. than that, there's very little to it. Fonts are simple to learn and even
  13435. simpler to use, yet they can add to your graphics a final touch of polish.  %@NL@%
  13436. %@NL@%
  13437. %@NL@%
  13438. %@2@%%@CR:C6A00150002 @%%@AB@%QuickC Fonts%@AE@%%@EH@%%@NL@%
  13439. %@NL@%
  13440. A "font" is a collection of stylized text characters. Each font consists of
  13441. several type sizes and a typeface.  %@NL@%
  13442. %@NL@%
  13443. "Typeface" is a printer's term that refers to the style of the displayed
  13444. text─ Courier, for example, or Roman. The list on the following page shows
  13445. six of the typefaces available with QuickC's font library.  %@NL@%
  13446. %@NL@%
  13447. "Type size" measures the screen area occupied by individual characters. This
  13448. term is also borrowed from the printer's lexicon, but for our purposes is
  13449. specified in units of screen pixels. For example, "Courier 16 x 9" denotes
  13450. text of Courier typeface, with each character occupying a screen area of 16
  13451. vertical pixels by 9 horizontal pixels.  %@NL@%
  13452. %@NL@%
  13453. QuickC's font functions use two methods to create fonts. The first technique
  13454. generates Courier, Helv, and Tms Rmn fonts through a "bit-mapping" (or
  13455. "raster-mapping") technique. Bit-mapping defines character images with
  13456. binary data. Each bit in the map corresponds to a screen pixel. If a bit is
  13457. 1, its associated pixel is set to the current screen color. A bit value of 0
  13458. clears the pixel. Video adapters use this same technique to display text in
  13459. graphics mode.  %@NL@%
  13460. %@NL@%
  13461. The second method creates the remaining three type styles─Modern, Script,
  13462. and Roman─as "vector-mapped" fonts. Vector-mapping represents each character
  13463. in terms of lines and arcs. In a literal sense vector-mapped characters are
  13464. drawn on the screen. You might think of bit-mapped characters as being
  13465. stenciled.  %@NL@%
  13466. %@NL@%
  13467. Each method has advantages and disadvantages. Bit-mapped characters are more
  13468. completely formed since the pixel mapping is predetermined. However, they
  13469. cannot be scaled. Vector-mapped text can be scaled to any size, but the
  13470. characters tend to lack the solid appearance of the bit-mapped characters.  %@NL@%
  13471. %@NL@%
  13472. %@AU@%(This figure may be found in the printed book.)%@AE@%%@NL@%
  13473. %@NL@%
  13474. Table 15.1 lists available sizes for each font. Notice that the bit-mapped
  13475. fonts come in preset sizes as measured in pixels. The exact size of any
  13476. fonted character depends on screen resolution and display type.  %@NL@%
  13477. %@NL@%
  13478. %@AB@%Table 15.1  %@AB@%Typefaces and Type Sizes in the QuickC Library%@AE@%%@AE@%
  13479.  
  13480. %@TH:  19   749 02 10 09 26 31 @%Typeface  Mapping  Size (in pixels)          Spacing%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%Courier   Bit      13 x 8, 16 x 9,           Fixed                   20 x 12                   Helv      Bit      13 x 5, 16 x 7, 20 x 8,   Fixed                   13 x 15, 16 x 6, 19 x 8   Tms Rmn   Bit      10 x 5, 12 x 6, 15 x 8,   Fixed                   16 x 9, 20 x 12, 26 x 16  Modern    Vector   Scaled                    ProportionalScript    Vector   Scaled                    ProportionalRoman     Vector   Scaled                    Proportional%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  19   749 02 10 09 26 31 @%
  13481.  
  13482. QuickC's font routines can display characters 32-255, including most
  13483. extended characters (ASCII 128-255). A few extended characters cannot be
  13484. displayed; these are represented as either an underscore (_) or period (.)
  13485. character.  %@NL@%
  13486. %@NL@%
  13487. %@NL@%
  13488. %@2@%%@CR:C6A00150003 @%%@AB@%Using QuickC's Font Library%@AE@%%@EH@%%@NL@%
  13489. %@NL@%
  13490. Data for both bit-mapped and vector-mapped fonts reside in files on disk. A
  13491. .FON extension identifies the files. The names of the .FON files indicate
  13492. their content. For example, the files MODERN.FON, ROMAN.FON, and SCRIPT.FON
  13493. hold data for the three vector-mapped fonts.  %@NL@%
  13494. %@NL@%
  13495. %@AU@% You can use Microsoft Windows .FON files.%@AE@%  %@NL@%
  13496. %@NL@%
  13497. QuickC .FON files are identical to the .FON files used in the Microsoft
  13498. Windows operating environment. If you have access to Windows you can use any
  13499. of its .FON files with QuickC's font functions. Windows .FON files are also
  13500. available for purchase separately. In addition, several vendors offer
  13501. software that can create or modify .FON files, allowing you to design your
  13502. own fonts.  %@NL@%
  13503. %@NL@%
  13504. Your programs should follow these three steps to display fonted text:  %@NL@%
  13505. %@NL@%
  13506. %@NL@%
  13507.   1.  Register fonts%@NL@%
  13508. %@NL@%
  13509.   2.  Set the current font from the register%@NL@%
  13510. %@NL@%
  13511.   3.  Display text using the current font%@NL@%
  13512. %@NL@%
  13513. %@NL@%
  13514. The following sections describe each of the three steps in detail. An
  13515. example program later in the chapter demonstrates the steps.  %@NL@%
  13516. %@NL@%
  13517. %@NL@%
  13518. %@3@%%@CR:C6A00150004 @%%@AB@%Register Fonts%@AE@%%@EH@%%@NL@%
  13519. %@NL@%
  13520. The fonts you plan to use must first be organized into a list in memory, a
  13521. process called "registering." The register list contains information about
  13522. the available .FON files. Register fonts by calling the function
  13523. %@AB@%_registerfonts%@AE@%. This function reads header information from specified .FON
  13524. files. It builds a list of file information but does not read mapping data
  13525. from the files.  %@NL@%
  13526. %@NL@%
  13527. The GRAPH.H file prototypes the %@AB@%_registerfonts%@AE@% function as:  %@NL@%
  13528. %@NL@%
  13529. %@AS@%  short far _registerfonts( unsigned char far * );%@AE@%%@NL@%
  13530. %@NL@%
  13531. The argument points to a string containing a file name. The file name is the
  13532. name of the .FON file for the desired font. The file name can include wild
  13533. cards, allowing you to register several fonts with one call to
  13534. %@AB@%_registerfonts%@AE@%.  %@NL@%
  13535. %@NL@%
  13536. If it successfully reads one or more .FON files, %@AB@%_registerfonts%@AE@% returns the
  13537. number of fonts registered. If the function fails, it returns a negative
  13538. error code. Refer to Appendix B, "C Library Guide," or to online help for a
  13539. description of error codes.  %@NL@%
  13540. %@NL@%
  13541. %@NL@%
  13542. %@3@%%@CR:C6A00150005 @%%@AB@%Set Current Font%@AE@%%@EH@%%@NL@%
  13543. %@NL@%
  13544. Call the function %@AB@%_setfont%@AE@% to select a current font. This function checks to
  13545. see if the requested font is registered, then reads the mapping data from
  13546. the appropriate .FON file. A font must be registered and marked current
  13547. before your program can display text of that font.  %@NL@%
  13548. %@NL@%
  13549. The GRAPH.H file prototypes %@AB@%_setfonts%@AE@% as  %@NL@%
  13550. %@NL@%
  13551. %@AS@%  short far _setfont( unsigned char far * );%@AE@%%@NL@%
  13552. %@NL@%
  13553. The function's argument is a pointer to a character string. The string
  13554. consists of letter codes that describe the desired font, as outlined below:
  13555. %@NL@%
  13556. %@NL@%
  13557. %@AB@%Option Code%@AE@%                       %@AB@%Meaning%@AE@%
  13558. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  13559. %@AB@%b%@AE@%                                 Select the best fit from the registered 
  13560.                                   fonts. This option instructs %@AB@%_setfont%@AE@% to
  13561.                                   accept the closest-fitting font if a 
  13562.                                   font of the specified size is not 
  13563.                                   registered.
  13564.  
  13565.                                   If at least one font is registered, the %@AB@%%@AE@%
  13566.                                   %@AB@%b%@AE@% option always sets a current font. If 
  13567.                                   you do not specify the %@AB@%b%@AE@% option and an 
  13568.                                   exact matching font is not registered, %@AB@%%@AE@%
  13569.                                   %@AB@%_setfont%@AE@% will fail. In this case, any 
  13570.                                   existing current font remains current. 
  13571.                                   Refer to online help for a description 
  13572.                                   of error codes returned by %@AB@%_setfont%@AE@%.
  13573.  
  13574.                                   The %@AB@%_setfont%@AE@% function uses four criteria
  13575.                                   for selecting the best fit. In 
  13576.                                   descending order of precedence the four 
  13577.                                   criteria are pixel height, typeface, 
  13578.                                   pixel width, and spacing (fixed or 
  13579.                                   proportional). If you request a 
  13580.                                   vector-mapped font,%@AB@%_setfont%@AE@% sizes the 
  13581.                                   font to correspond with the specified 
  13582.                                   pixel height and width. If you request a
  13583.                                   raster-mapped (bit-mapped) font, %@AB@%%@AE@%
  13584.                                   %@AB@%_setfont%@AE@% chooses the closest available 
  13585.                                   size. If the requested type size for a 
  13586.                                   raster-mapped font fits exactly between 
  13587.                                   two registered fonts, the smaller size 
  13588.                                   takes precedence.
  13589.  
  13590. %@AB@%f%@AE@%                                 Select only a fixed-spaced font.
  13591.  
  13592. %@AB@%h%@AE@%%@AI@%y%@AE@%                                Character height, where %@AI@%y%@AE@% is the height 
  13593.                                   in pixels.
  13594.  
  13595. %@AB@%n%@AE@%%@AI@%x%@AE@%                                Select font number %@AI@%x%@AE@%, where %@AI@%x%@AE@% is less 
  13596.                                   than or equal to the value returned by %@AB@%%@AE@%
  13597.                                   %@AB@%_registerfonts%@AE@%. For example, the option %@AB@%%@AE@%
  13598.                                   %@AB@%n3%@AE@% makes the third registered font 
  13599.                                   current, assuming that three or more 
  13600.                                   fonts are registered.
  13601.  
  13602. %@AB@%p%@AE@%                                 Select only a proportional-spaced font.
  13603.  
  13604. %@AB@%r%@AE@%                                 Select only a raster-mapped (bit-mapped)
  13605.                                   font.
  13606.  
  13607. %@AB@%t`%@AE@%%@AI@%fontname%@AE@%%@AB@%'%@AE@%                       Typeface of the font in single quotes. 
  13608.                                   The %@AI@%fontname%@AE@% string is one of the 
  13609.                                   following:
  13610.  
  13611.                                   courier     modern
  13612.  
  13613.                                   helv        script
  13614.  
  13615.                                   tms rmn     roman
  13616.  
  13617.                                   Notice the space in "tms rmn."  
  13618.                                   Additional font files use other names 
  13619.                                   for %@AI@%fontname%@AE@%. Refer to the vendor's 
  13620.                                   documentation for these names.
  13621.  
  13622. %@AB@%v%@AE@%                                 Select only a vector-mapped font.
  13623.  
  13624. %@AB@%w%@AE@%%@AI@%x%@AE@%                                Character width, where %@AI@%x%@AE@% is the width in
  13625.                                   pixels.
  13626.  
  13627. Option codes are not case-sensitive and can be listed in any order. You can
  13628. separate codes with spaces or any other character that is not a valid option
  13629. code. The %@AB@%_setfont%@AE@% function ignores all invalid codes.  %@NL@%
  13630. %@NL@%
  13631. The %@AB@%_setfont%@AE@% function updates a data area with parameters of the current
  13632. font. The data area is in the form of a structure, defined in the GRAPH.H
  13633. file as  %@NL@%
  13634. %@NL@%
  13635. %@AS@%  struct _fontinfo 
  13636. %@AS@%  {
  13637. %@AS@%     int     type;          /* set = vector,clear = bit map */
  13638. %@AS@%     int     ascent;        /* pix dist from top to base */
  13639. %@AS@%     int     pixwidth;      /* character width in pixels */
  13640. %@AS@%     int     pixheight;     /* character height in pixels */
  13641. %@AS@%     int     avgwidth;      /* average character width */
  13642. %@AS@%     char    filename[81];  /* file name including path */
  13643. %@AS@%     char    faceName[32];  /* font name */
  13644. %@AS@%  };%@AE@%%@NL@%
  13645. %@NL@%
  13646. If you wish to retrieve the parameters of the current font, call the
  13647. function %@AB@%_getfontinfo%@AE@%. Refer to Appendix B, "C Library Guide," or online
  13648. help for a description of this function.  %@NL@%
  13649. %@NL@%
  13650. %@NL@%
  13651. %@3@%%@CR:C6A00150006 @%%@AB@%Display Text%@AE@%%@EH@%%@NL@%
  13652. %@NL@%
  13653. The last step consists of two parts. First, select a screen position for the
  13654. text with the graphics function %@AB@%_moveto%@AE@%. Then display fonted text at that
  13655. position with the function %@AB@%_outgtext%@AE@%. The %@AB@%_moveto%@AE@% function takes pixel
  13656. coordinates as arguments. The coordinates locate the top left of the first
  13657. character in the text string.  %@NL@%
  13658. %@NL@%
  13659. %@NL@%
  13660. %@2@%%@CR:C6A00150007 @%%@AB@%An Example Program%@AE@%%@EH@%%@NL@%
  13661. %@NL@%
  13662. QuickC's font functions shine when used in conjunction with your other
  13663. graphics functions. They allow you to dress up any image on the screen. Yet
  13664. they can make a visual impression when used by themselves, as an example
  13665. will show.  %@NL@%
  13666. %@NL@%
  13667. The program SAMPLER.C displays sample text in all the available fonts, then
  13668. exits when a key is pressed. Make sure the .FON files are in the current
  13669. directory before running the program.  %@NL@%
  13670. %@NL@%
  13671. %@AS@%  /* SAMPLER.C: Display sample text in various fonts. */
  13672. %@AS@%  
  13673. %@AS@%  #include <stdio.h>
  13674. %@AS@%  #include <conio.h>
  13675. %@AS@%  #include <stdlib.h>
  13676. %@AS@%  #include <graph.h>
  13677. %@AS@%  #include <string.h>%@AE@%%@NL@%
  13678. %@NL@%
  13679. %@AS@%  #define NFONTS 6
  13680. %@AS@%  
  13681. %@AS@%  main()
  13682. %@AS@%  
  13683. %@AS@%  {
  13684. %@AS@%    static unsigned char *text[2*NFONTS] =
  13685. %@AS@%    {
  13686. %@AS@%        "COURIER",        "courier",
  13687. %@AS@%        "HELV",           "helv",
  13688. %@AS@%        "TMS RMN",        "tms rmn",
  13689. %@AS@%        "MODERN",         "modern",
  13690. %@AS@%        "SCRIPT",         "script",
  13691. %@AS@%        "ROMAN",          "roman"
  13692. %@AS@%    };
  13693. %@AS@%    static unsigned char *face[NFONTS] =
  13694. %@AS@%    {
  13695. %@AS@%        "t'courier'",
  13696. %@AS@%        "t'helv'",
  13697. %@AS@%        "t'tms rmn'",
  13698. %@AS@%        "t'modern'",
  13699. %@AS@%        "t'script'",
  13700. %@AS@%        "t'roman'"
  13701. %@AS@%    };
  13702. %@AS@%    static unsigned char list[20];
  13703. %@AS@%    struct videoconfig vc;
  13704. %@AS@%    int mode = _VRES16COLOR;
  13705. %@AS@%    register i;
  13706. %@AS@%  
  13707. %@AS@%    /*  Read header info from all .FON files in
  13708. %@AS@%     *  current directory   */
  13709. %@AS@%  
  13710. %@AS@%    if(_registerfonts( "*.FON" )<0 )
  13711. %@AS@%    {
  13712. %@AS@%       _outtext("Error:  can't register fonts");
  13713. %@AS@%       exit( 0 );
  13714. %@AS@%    }
  13715. %@AS@%  
  13716. %@AS@%    /*   Set highest available video mode */
  13717. %@AS@%  
  13718. %@AS@%    while( !_setvideomode( mode ) )
  13719. %@AS@%       mode--;
  13720. %@AS@%    if( mode == _TEXTMONO )
  13721. %@AS@%       exit ( 0 );
  13722. %@AS@%  
  13723. %@AS@%    /*   Copy video configuration into structure vc */
  13724. %@AS@%  
  13725. %@AS@%    _getvideoconfig( &vc );%@AE@%%@NL@%
  13726. %@NL@%
  13727. %@AS@%    /*   Display six lines of sample text */
  13728. %@AS@%  
  13729. %@AS@%    for( i = 0; i<NFONTS; i++ )
  13730. %@AS@%    {
  13731. %@AS@%       strcpy( list, face[i] );
  13732. %@AS@%       strcat( list, "h30w24b" );
  13733. %@AS@%  
  13734. %@AS@%       if( !_setfont( list ) )
  13735. %@AS@%       {
  13736. %@AS@%           _setcolor( i + 1 );
  13737. %@AS@%           _moveto( 0, (i * vc.numypixels) / NFONTS );
  13738. %@AS@%           _outgtext( text[i * 2] );
  13739. %@AS@%           _moveto( vc.numxpixels / 2,
  13740. %@AS@%                       (i * vc.numypixels) / NFONTS );
  13741. %@AS@%           _outgtext( text[(i * 2) + 1] );
  13742. %@AS@%       }
  13743. %@AS@%       else
  13744. %@AS@%       {
  13745. %@AS@%           _setvideomode( _DEFAULTMODE );
  13746. %@AS@%           _outtext( "Error:  can't set font" );
  13747. %@AS@%           exit( 0 );
  13748. %@AS@%       }
  13749. %@AS@%    }
  13750. %@AS@%    getch();
  13751. %@AS@%    _setvideomode( _DEFAULTMODE );
  13752. %@AS@%  
  13753. %@AS@%    /* Return memory when finished with fonts */
  13754. %@AS@%  
  13755. %@AS@%    _unregisterfonts();
  13756. %@AS@%    exit ( 0 );
  13757. %@AS@%  }%@AE@%%@NL@%
  13758. %@NL@%
  13759. Notice that SAMPLER.C calls the graphics function %@AB@%_moveto%@AE@% to establish the
  13760. starting position for each text string. Chapter 13, "Graphics," describes
  13761. the %@AB@%_moveto%@AE@% function in the section "Graphics Coordinates." The function
  13762. %@AB@%_setfont%@AE@% takes a character string as an argument. The string is a list of
  13763. options that specifies typeface and the best fit for a character height of
  13764. 30 pixels, and a width of 24 pixels. See Appendix B, "C Library Guide," and
  13765. online help for complete descriptions of the QuickC font functions.  %@NL@%
  13766. %@NL@%
  13767. %@NL@%
  13768. %@2@%%@CR:C6A00150008 @%%@AB@%A Few Hints%@AE@%%@EH@%%@NL@%
  13769. %@NL@%
  13770. Fonted text is simply another form of graphics, and using fonts effectively
  13771. requires little programming effort. Still, there are a few things to watch:
  13772. %@NL@%
  13773. %@NL@%
  13774. %@NL@%
  13775.   ■   Remember the video should be set only once to establish a graphics
  13776.       mode. If you generate an image─say, with Presentation Graphics─and
  13777.       wish to incorporate fonted text into it, don't reset the video mode
  13778.       prior to calling the font routines. Doing so will blank the screen,
  13779.       destroying the original image.%@NL@%
  13780. %@NL@%
  13781.   ■   The %@AB@%_setfont%@AE@% function reads specified .FON files to obtain mapping
  13782.       data for the current font. Each call to %@AB@%_setfont%@AE@% causes a disk access
  13783.       and overwrites the old font data in memory. If you wish to show text
  13784.       of different styles on the same screen, display all text of one font
  13785.       before moving on to the others. By minimizing the number of calls to
  13786.       %@AB@%_setfont%@AE@% you'll save time spent in disk I/O and memory reloads. %@NL@%
  13787. %@NL@%
  13788.   ■   When your program finishes with the fonts library, you might wish to
  13789.       free the memory occupied by the register list. Call the function
  13790.       %@AB@%_unregisterfonts%@AE@% to do this. As its name implies, this function frees
  13791.       the memory allocated by %@AB@%_registerfonts%@AE@%. The register information for
  13792.       each type size of each font takes up approximately 140 bytes of
  13793.       memory. Thus the amount of memory returned by %@AB@%_unregisterfonts%@AE@% is
  13794.       significant only if you have many fonts registered.%@NL@%
  13795. %@NL@%
  13796.   ■   As for aesthetics, the same suggestions for the printed page apply to
  13797.       fonted screen text. Typefaces are more effective when they are not
  13798.       competing with each other for attention. Restricting the number of
  13799.       styles per screen to one or two generally results in a more pleasing,
  13800.       less cluttered image.%@NL@%
  13801. %@NL@%
  13802. %@NL@%
  13803. %@NL@%
  13804. %@NL@%
  13805. %@NL@%
  13806. %@NL@%
  13807. %@NL@%
  13808. %@CR:C6A00160001 @%%@1@%%@AB@%Chapter 16  In-Line Assembly%@AE@%%@EH@%%@NL@%
  13809. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  13810. %@NL@%
  13811. QuickC has the ability to handle assembly-language instructions right in
  13812. your C programs. This powerful feature is called "in-line assembly."  %@NL@%
  13813. %@NL@%
  13814. Assembly language serves many purposes, such as improving program speed,
  13815. reducing memory needs, and controlling hardware. The in-line assembler lets
  13816. you embed assembly-language instructions directly in your C source programs
  13817. without extra assembly and link steps. And the assembler is built into the
  13818. compiler─you don't need a separate assembler such as the Microsoft Macro
  13819. Assembler (MASM).  %@NL@%
  13820. %@NL@%
  13821. This chapter assumes that you are familiar with assembly-language terms and
  13822. concepts. If you have never programmed in assembly language, refer to the
  13823. section "References and Books on Assembly Language," at the end of this
  13824. chapter.  %@NL@%
  13825. %@NL@%
  13826. %@NL@%
  13827. %@2@%%@CR:C6A00160002 @%%@AB@%Advantages of In-Line Assembly%@AE@%%@EH@%%@NL@%
  13828. %@NL@%
  13829. Because QuickC's in-line assembler doesn't require separate assembly and
  13830. link steps, it is more convenient than a separate assembler. In-line
  13831. assembly code can use any C variable or function name that is visible (in
  13832. scope), so it is easy to integrate it with your program's C code. And
  13833. because the assembly code can be mixed in-line with C statements, it can do
  13834. tasks that are cumbersome or impossible in C alone.  %@NL@%
  13835. %@NL@%
  13836. The uses of in-line assembly include  %@NL@%
  13837. %@NL@%
  13838. %@NL@%
  13839.   ■   Writing the body of a function in assembly language%@NL@%
  13840. %@NL@%
  13841.   ■   Spot-optimizing speed-critical sections of code%@NL@%
  13842. %@NL@%
  13843.   ■   Calling DOS and BIOS routines with the %@AB@%INT%@AE@% instruction%@NL@%
  13844. %@NL@%
  13845.   ■   Creating TSR (terminate-and-stay-resident) code or handler routines
  13846.       that require knowledge of processor states%@NL@%
  13847. %@NL@%
  13848. %@NL@%
  13849. In-line assembly is a special-purpose tool. If you plan to transport an
  13850. application, you'll probably want to place machine-specific code in a
  13851. separate module. And because the in-line assembler doesn't support all MASM
  13852. directives, you may find it more convenient to use MASM for such modules.  %@NL@%
  13853. %@NL@%
  13854. %@NL@%
  13855. %@2@%%@CR:C6A00160003 @%%@AB@%The _asm Keyword%@AE@%%@EH@%%@NL@%
  13856. %@NL@%
  13857. The %@AB@%_asm%@AE@% keyword invokes the in-line assembler and can appear wherever a C
  13858. statement is legal. It cannot appear by itself. It must be followed by an
  13859. assembly instruction, a group of instructions enclosed in braces, or, at the
  13860. very least, an empty pair of braces. The term "%@AB@%_asm%@AE@% block" here refers to
  13861. any instruction or group of instructions, whether or not in braces.  %@NL@%
  13862. %@NL@%
  13863. Below is a simple %@AB@%_asm%@AE@% block enclosed in braces. (The code prints the "beep"
  13864. character, ASCII 7.)  %@NL@%
  13865. %@NL@%
  13866. %@AS@%  _asm
  13867. %@AS@%  {
  13868. %@AS@%     mov ah, 2
  13869. %@AS@%     mov dl, 7
  13870. %@AS@%     int 21h
  13871. %@AS@%  }%@AE@%%@NL@%
  13872. %@NL@%
  13873. Alternatively, you can put %@AB@%_asm%@AE@% in front of each assembly instruction:  %@NL@%
  13874. %@NL@%
  13875. %@AS@%  _asm mov ah, 2
  13876. %@AS@%  _asm mov dl, 7
  13877. %@AS@%  _asm int 21h%@AE@%%@NL@%
  13878. %@NL@%
  13879. Since the %@AB@%_asm%@AE@% keyword is a statement separator, you can also put assembly
  13880. instructions on the same line:  %@NL@%
  13881. %@NL@%
  13882. %@AS@%  _asm mov ah, 2   _asm mov dl, 7   _asm int 21h%@AE@%%@NL@%
  13883. %@NL@%
  13884. %@AU@%  Braces can prevent ambiguity and needless repetition.%@AE@%  %@NL@%
  13885. %@NL@%
  13886. All three examples generate the same code, but the first style─enclosing the
  13887. %@AB@%_asm%@AE@% block in braces─has some advantages. The braces clearly separate
  13888. assembly code from C code and avoid needless repetition of the %@AB@%_asm%@AE@% keyword.
  13889. Braces can also prevent ambiguities. If you want to put a C statement on the
  13890. same line as an %@AB@%_asm%@AE@% block, you must enclose the block in braces. Without
  13891. the braces, the compiler cannot tell where assembly code stops and C
  13892. statements begin. Finally, since the text in braces has the same format as
  13893. ordinary MASM text, you can easily cut and paste text from existing MASM
  13894. source files.  %@NL@%
  13895. %@NL@%
  13896. The braces enclosing an %@AB@%_asm%@AE@% block don't affect variable visibility, as do
  13897. braces in C. You can also nest %@AB@%_asm%@AE@% blocks, but the nesting doesn't affect
  13898. variable visibility.  %@NL@%
  13899. %@NL@%
  13900. %@NL@%
  13901. %@2@%%@CR:C6A00160004 @%%@AB@%Using Assembly Language in _asm Blocks%@AE@%%@EH@%%@NL@%
  13902. %@NL@%
  13903. The in-line assembler has much in common with other assemblers. For example,
  13904. it accepts any expression that is legal in MASM, and it supports almost all
  13905. 80286 and 80287 instructions. This section describes the use of
  13906. assembly-language features in %@AB@%_asm%@AE@% blocks.  %@NL@%
  13907. %@NL@%
  13908. %@NL@%
  13909. %@3@%%@CR:C6A00160005 @%%@AB@%Instruction Set%@AE@%%@EH@%%@NL@%
  13910. %@NL@%
  13911. The in-line assembler supports the full instruction set of the Intel(R)
  13912. 80286 and 80287 processors, except for privileged instructions that control
  13913. the processor's protected mode (protected mode is available in the OS/2 and
  13914. XENIX(R) operating systems, but not in DOS). It does not recognize 80386-
  13915. and 80387-specific instructions. To use assembly instructions specific to
  13916. the 80286 and 80287 processors, you must compile your QuickC program with
  13917. the /G2 switch included in the command line. For a description of the
  13918. compiler /G command-line switch, refer to Chapter 4, "QCL Command
  13919. Reference," in the %@AI@%Microsoft QuickC Tool Kit%@AE@%.  %@NL@%
  13920. %@NL@%
  13921. %@NL@%
  13922. %@3@%%@CR:C6A00160006 @%%@AB@%Expressions%@AE@%%@EH@%%@NL@%
  13923. %@NL@%
  13924. In-line assembly code can use any MASM expression, that is, any combination
  13925. of operands and operators that evaluates to a single value or address.  %@NL@%
  13926. %@NL@%
  13927. %@NL@%
  13928. %@3@%%@CR:C6A00160007 @%%@AB@%Data Directives and Operators%@AE@%%@EH@%%@NL@%
  13929. %@NL@%
  13930. Although an %@AB@%_asm%@AE@% block can reference C data types and objects, it cannot
  13931. define data objects with MASM directives or operators. Specifically, you
  13932. cannot use the definition directives %@AB@%DB%@AE@%, %@AB@%DW%@AE@%, %@AB@%DD%@AE@%, %@AB@%DQ%@AE@%, %@AB@%DT%@AE@%, and %@AB@%DF%@AE@%, or the
  13933. operators %@AB@%DUP%@AE@% or %@AB@%THIS%@AE@%. Nor are MASM structures and records available. The
  13934. in-line assembler doesn't accept the directives %@AB@%STRUC%@AE@%, %@AB@%RECORD%@AE@%, %@AB@%WIDTH%@AE@%, or
  13935. %@AB@%MASK%@AE@%.  %@NL@%
  13936. %@NL@%
  13937. %@NL@%
  13938. %@3@%%@CR:C6A00160008 @%%@AB@%EVEN and ALIGN Directives%@AE@%%@EH@%%@NL@%
  13939. %@NL@%
  13940. While the in-line assembler doesn't support most MASM directives, it does
  13941. support %@AB@%EVEN%@AE@% and %@AB@%ALIGN%@AE@%. These directives put %@AB@%NOP%@AE@% (no operation) instructions
  13942. in the assembly code as needed to align labels to specific boundaries. This
  13943. makes instruction-fetch operations more efficient for some processors (not
  13944. including eight-bit processors such as the Intel 8088).  %@NL@%
  13945. %@NL@%
  13946. %@NL@%
  13947. %@3@%%@CR:C6A00160009 @%%@AB@%Macros%@AE@%%@EH@%%@NL@%
  13948. %@NL@%
  13949. The in-line assembler is not a macro assembler. You cannot use MASM macro
  13950. directives (%@AB@%MACRO%@AE@%, %@AB@%REPT%@AE@%, %@AB@%IRC%@AE@%, %@AB@%IRP%@AE@%, and %@AB@%ENDM%@AE@%) or macro operators ( %@AB@%<>%@AE@%, %@AB@%!%@AE@%, %@AB@%&%@AE@%,
  13951. %@AB@%%%@AE@%, and %@AB@%.TYPE%@AE@%). An %@AB@%_asm%@AE@% block can use C preprocessor directives, however. See
  13952. the section "Using C in %@AB@%_asm%@AE@% Blocks" for more information.  %@NL@%
  13953. %@NL@%
  13954. %@NL@%
  13955. %@3@%%@CR:C6A00160010 @%%@AB@%Segment References%@AE@%%@EH@%%@NL@%
  13956. %@NL@%
  13957. You must refer to segments by register rather than by name (the segment name
  13958. %@AB@%_TEXT%@AE@% is invalid, for instance). Segment overrides must use the register
  13959. explicitly, as in ES:[BX].  %@NL@%
  13960. %@NL@%
  13961. %@NL@%
  13962. %@3@%%@CR:C6A00160011 @%%@AB@%Type and Variable Sizes%@AE@%%@EH@%%@NL@%
  13963. %@NL@%
  13964. The %@AB@%LENGTH%@AE@%, %@AB@%SIZE%@AE@%, and %@AB@%TYPE%@AE@% operators have a limited meaning in in-line
  13965. assembly. They cannot be used at all with the %@AB@%DUP%@AE@% operator (because you
  13966. cannot define data with MASM directives or operators). But you can use them
  13967. to find the size of C variables or types:  %@NL@%
  13968. %@NL@%
  13969. %@NL@%
  13970.   ■   The %@AB@%LENGTH%@AE@% operator can return the number of elements in an array. It
  13971.       returns the value 1 for nonarray variables.%@NL@%
  13972. %@NL@%
  13973.   ■   The %@AB@%SIZE%@AE@% operator can return the size of a C variable. A variable's
  13974.       size is the product of its %@AB@%LENGTH%@AE@% and %@AB@%TYPE%@AE@%.%@NL@%
  13975. %@NL@%
  13976.   ■   The %@AB@%TYPE%@AE@% operator can return the size of a C type or variable. If the
  13977.       variable is an array, %@AB@%TYPE%@AE@% returns the size of a single element of the
  13978.       array.%@NL@%
  13979. %@NL@%
  13980. %@NL@%
  13981. For instance, if your program has an eight-element %@AB@%int%@AE@% array,  %@NL@%
  13982. %@NL@%
  13983. %@AS@%  int arr[8];%@AE@%%@NL@%
  13984. %@NL@%
  13985. the following C and assembly expressions yield the size of %@AS@% arr %@AE@% and its
  13986. elements:  %@NL@%
  13987. %@NL@%
  13988. %@TH:   6   465 02 12 28 36 @%_asm        C                           Size%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AS@%LENGTH arr%@AE@%  %@AS@%sizeof(arr)/sizeof(arr[0])%@AE@%  8%@AS@%SIZE arr%@AE@%    %@AS@%sizeof(arr)%@AE@%                 16%@AS@%TYPE arr%@AE@%    %@AS@%sizeof(arr[0])%@AE@%              2%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:   6   465 02 12 28 36 @%
  13989.  
  13990. %@NL@%
  13991. %@4@%%@AB@%Comments%@AE@%%@EH@%%@NL@%
  13992. %@NL@%
  13993. Instructions in an %@AB@%_asm%@AE@% block can use assembly-language comments:  %@NL@%
  13994. %@NL@%
  13995. %@AS@%  _asm mov ax, offset buff ; Load address of buff%@AE@%%@NL@%
  13996. %@NL@%
  13997. Because C macros expand into a single logical line, avoid using
  13998. assemblylanguage comments in macros (see the section "Defining %@AB@%_asm%@AE@% Blocks
  13999. as C Macros," below). An %@AB@%_asm%@AE@% block can also contain C-style comments, as
  14000. noted below.  %@NL@%
  14001. %@NL@%
  14002. %@NL@%
  14003. %@4@%%@AB@%Debugging with the CodeView(R) Debugger%@AE@%%@EH@%%@NL@%
  14004. %@NL@%
  14005. %@AU@% In-line assembly code can be debugged with CodeView.%@AE@%  %@NL@%
  14006. %@NL@%
  14007. Programs containing in-line assembly code can be debugged with the CodeView
  14008. debugger, assuming you compile with the /Zi option.  %@NL@%
  14009. %@NL@%
  14010. Note that putting multiple assembly instructions or C statements on one line
  14011. can hamper debugging with CodeView. In source mode, the CodeView debugger
  14012. lets you set breakpoints on a single line but not on individual statements
  14013. on the same line. The same principle applies to an %@AB@%_asm%@AE@% block defined as a C
  14014. macro, which expands to a single logical line.  %@NL@%
  14015. %@NL@%
  14016. %@NL@%
  14017. %@2@%%@CR:C6A00160012 @%%@AB@%Using C in _asm Blocks%@AE@%%@EH@%%@NL@%
  14018. %@NL@%
  14019. Because in-line assembly instructions can be mixed with C statements, they
  14020. can refer to C variables by name and use many other elements of C. An %@AB@%_asm%@AE@%
  14021. block can use the following C language elements:  %@NL@%
  14022. %@NL@%
  14023. %@NL@%
  14024.   ■   Symbols, including labels and variable and function names%@NL@%
  14025. %@NL@%
  14026.   ■   Constants, including symbolic constants and %@AB@%enum%@AE@% members%@NL@%
  14027. %@NL@%
  14028.   ■   Macros and preprocessor directives%@NL@%
  14029. %@NL@%
  14030.   ■   Comments (%@AS@% /* */ %@AE@%)%@NL@%
  14031. %@NL@%
  14032.   ■   Type names (wherever a MASM type would be legal)%@NL@%
  14033. %@NL@%
  14034.   ■   %@AB@%typedef%@AE@% names, generally used with operators such as %@AB@%PTR%@AE@% and %@AB@%TYPE%@AE@% or
  14035.       to specify structure or union members%@NL@%
  14036. %@NL@%
  14037. %@NL@%
  14038. Within an %@AB@%_asm%@AE@% block, you can specify integer constants with either C
  14039. notation or assembler radix notation (0x100 and 100h are equivalent, for
  14040. instance). This allows you to define (using %@AB@%#define%@AE@%) a constant in C, and
  14041. use it in both C and assembly portions of the program. You can also specify
  14042. constants in octal by preceding them with a 0. For example, 0777 specifies
  14043. an octal constant.  %@NL@%
  14044. %@NL@%
  14045. %@NL@%
  14046. %@3@%%@CR:C6A00160013 @%%@AB@%Using Operators%@AE@%%@EH@%%@NL@%
  14047. %@NL@%
  14048. An %@AB@%_asm%@AE@% block cannot use C-specific operators, such as the %@AB@%%@AE@% operator.
  14049. However, operators shared by QuickC and MASM, such as the %@AB@%*%@AE@% operator, are
  14050. interpreted as assembly-language operators. For instance, outside an %@AB@%_asm%@AE@%
  14051. block, square brackets (%@AB@% [] %@AE@%) are interpreted as enclosing array subscripts,
  14052. which C automatically scales to the size of an element in the array. Inside
  14053. an %@AB@%_asm%@AE@% block, they are seen as the MASM index operator, which yields an
  14054. unscaled byte offset from any data object or label (not just an array). The
  14055. following code illustrates the difference:  %@NL@%
  14056. %@NL@%
  14057. %@AS@%  int array[10];
  14058. %@AS@%  
  14059. %@AS@%  _asm mov array[6], bx ;  Store BX at array+6 (not scaled)
  14060. %@AS@%  
  14061. %@AS@%  array[6] = 0;         /* Store 0 at array+12 (scaled) */%@AE@%%@NL@%
  14062. %@NL@%
  14063. The first reference to %@AS@% array %@AE@% is not scaled, but the second is. Note that
  14064. you can use the %@AB@%TYPE%@AE@% operator to achieve scaling based on a constant. For
  14065. instance, the following statements are equivalent:  %@NL@%
  14066. %@NL@%
  14067. %@AS@%  _asm mov array[6 * TYPE int], 0 ; Store 0 at array + 12
  14068. %@AS@%  
  14069. %@AS@%  array[6] = 0;                   /* Store 0 at array + 12 */%@AE@%%@NL@%
  14070. %@NL@%
  14071. %@NL@%
  14072. %@3@%%@CR:C6A00160014 @%%@AB@%Using C Symbols%@AE@%%@EH@%%@NL@%
  14073. %@NL@%
  14074. An %@AB@%_asm%@AE@% block can refer to any C symbol that is visible (in scope) where the
  14075. block appears. (C symbols are variable names, function names, and labels─in
  14076. other words, names that aren't symbolic constants or %@AB@%enum%@AE@% members.)  %@NL@%
  14077. %@NL@%
  14078. A few restrictions apply to the use of C symbols:  %@NL@%
  14079. %@NL@%
  14080. %@NL@%
  14081.   ■   Each assembly-language statement can contain only one C symbol.
  14082.       Multiple symbols can appear in the same assembly instruction only with
  14083.       %@AB@%OFFSET%@AE@%,%@AB@% LENGTH%@AE@%,%@AB@% TYPE%@AE@%, and %@AB@%SIZE%@AE@% expressions.%@NL@%
  14084. %@NL@%
  14085.   ■   Functions referenced in an %@AB@%_asm%@AE@% block must be declared (prototyped)
  14086.       earlier in the program. Otherwise, the compiler cannot distinguish
  14087.       between function names and labels in the %@AB@%_asm%@AE@% block.%@NL@%
  14088. %@NL@%
  14089.   ■   An %@AB@%_asm%@AE@% block cannot use any C symbols with the same spelling as MASM
  14090.       reserved words (regardless of case). MASM reserved words include
  14091.       instruction names such as %@AB@%PUSH%@AE@% and register names such as SI.%@NL@%
  14092. %@NL@%
  14093.   ■   Structure and union tags are not recognized in %@AB@%_asm%@AE@% blocks.%@NL@%
  14094. %@NL@%
  14095. %@NL@%
  14096. %@NL@%
  14097. %@3@%%@CR:C6A00160015 @%%@AB@%Accessing C Data%@AE@%%@EH@%%@NL@%
  14098. %@NL@%
  14099. A great convenience of in-line assembly is the ability to refer to C
  14100. variables by name. An %@AB@%_asm%@AE@% block can refer to any symbols─including variable
  14101. names─that are visible where the block appears. For instance, if the C
  14102. variable %@AS@% var %@AE@% is visible, the instruction  %@NL@%
  14103. %@NL@%
  14104. %@AS@%  _asm mov ax, var%@AE@%%@NL@%
  14105. %@NL@%
  14106. stores the value of %@AS@% var %@AE@% in AX.  %@NL@%
  14107. %@NL@%
  14108. If a structure or union member has a unique name, an %@AB@%_asm%@AE@% block can refer to
  14109. it using only the member name, without specifying the C variable or %@AB@%typedef%@AE@%
  14110. name before the period (%@AB@%.%@AE@%) operator. If the member name is not unique,
  14111. however, you must place a variable or %@AB@%typedef%@AE@% name immediately before the
  14112. period (%@AB@%.%@AE@%) operator. For instance, the following structure types share %@AS@%
  14113. %@AS@%same_name %@AE@% as their member name:  %@NL@%
  14114. %@NL@%
  14115. %@AS@%  struct first_type
  14116. %@AS@%  {
  14117. %@AS@%     char *weasel;
  14118. %@AS@%     int same_name;
  14119. %@AS@%  };
  14120. %@AS@%  
  14121. %@AS@%  struct second_type
  14122. %@AS@%  {
  14123. %@AS@%     int wonton;
  14124. %@AS@%     long same_name;
  14125. %@AS@%  };%@AE@%%@NL@%
  14126. %@NL@%
  14127. If you declare variables with the types  %@NL@%
  14128. %@NL@%
  14129. %@AS@%  struct first_type hal;
  14130. %@AS@%  struct second_type oat;%@AE@%%@NL@%
  14131. %@NL@%
  14132. all references to the member %@AS@% same_name %@AE@% must use the variable name, because
  14133. %@AS@%same_name %@AE@% is not unique. But the member %@AS@% weasel %@AE@% has a unique name, so you
  14134. can refer to it using only its member name:  %@NL@%
  14135. %@NL@%
  14136. %@AS@%  _asm
  14137. %@AS@%  {
  14138. %@AS@%     mov bx, OFFSET hal
  14139. %@AS@%     mov cx, [bx]hal.same_name ; Must use 'hal'
  14140. %@AS@%     mov si, [bx].weasel       ; Can omit 'hal'
  14141. %@AS@%  }%@AE@%%@NL@%
  14142. %@NL@%
  14143. Note that omitting the variable name is merely a coding convenience. The
  14144. same assembly instructions are generated whether or not it is present.  %@NL@%
  14145. %@NL@%
  14146. %@NL@%
  14147. %@3@%%@CR:C6A00160016 @%%@AB@%Writing Functions%@AE@%%@EH@%%@NL@%
  14148. %@NL@%
  14149. If you write a function with in-line assembly code, it's a simple matter to
  14150. pass arguments to the function and return a value from it. The following
  14151. examples compare a function first written for a separate assembler and then
  14152. rewritten for the in-line assembler. The function, called %@AS@% power2%@AE@%, receives
  14153. two parameters, multiplying the first parameter by 2 to the power of the
  14154. second parameter. Written for a separate assembler, the function might look
  14155. like this:  %@NL@%
  14156. %@NL@%
  14157. %@AS@%  ; POWER.ASM
  14158. %@AS@%  ; Compute the power of an integer
  14159. %@AS@%  ;
  14160. %@AS@%         PUBLIC _power2
  14161. %@AS@%  _TEXT SEGMENT WORD PUBLIC 'CODE'
  14162. %@AS@%  _power2 PROC
  14163. %@AS@%  
  14164. %@AS@%          push bp         ; Save BP
  14165. %@AS@%          mov bp, sp      ; Move SP into BP so we can refer
  14166. %@AS@%                          ;   to arguments on the stack
  14167. %@AS@%          mov ax, [bp+4]  ; Get first argument
  14168. %@AS@%          mov cx, [bp+6]  ; Get second argument
  14169. %@AS@%          shl ax, cl      ; AX = AX * ( 2 ^ CL )
  14170. %@AS@%          pop bp          ; Restore BP
  14171. %@AS@%          ret             ; Return with sum in AX
  14172. %@AS@%  
  14173. %@AS@%  _power2 ENDP
  14174. %@AS@%  _TEXT   ENDS
  14175. %@AS@%          END%@AE@%%@NL@%
  14176. %@NL@%
  14177. %@AU@% Function arguments are usually passed on the stack.%@AE@%  %@NL@%
  14178. %@NL@%
  14179. Since it's written for a separate assembler, the function requires a
  14180. separate source file and assembly and link steps. C function arguments
  14181. usually are passed on the stack, so this version of the %@AS@% power2 %@AE@% function
  14182. accesses its arguments by their positions on the stack. (Note that the %@AB@%MODEL%@AE@%
  14183. directive, available in MASM and some other assemblers, also allows you to
  14184. access stack arguments and local stack variables by name.)  %@NL@%
  14185. %@NL@%
  14186. The POWER2.C program below writes the %@AS@% power2 %@AE@% function with in-line
  14187. assembly code:  %@NL@%
  14188. %@NL@%
  14189. %@AS@%  /* POWER2.C */
  14190. %@AS@%  #include <stdio.h>
  14191. %@AS@%  
  14192. %@AS@%  int power2( int num, int power );
  14193. %@AS@%  
  14194. %@AS@%  void main( void )
  14195. %@AS@%  {
  14196. %@AS@%     printf( "3 times 2 to the power of 5 is %d\n", \
  14197. %@AS@%             power2( 3, 5) );
  14198. %@AS@%  }
  14199. %@AS@%  
  14200. %@AS@%  int power2( int num, int power )
  14201. %@AS@%  {
  14202. %@AS@%     _asm
  14203. %@AS@%     {
  14204. %@AS@%        mov ax, num    ; Get first argument
  14205. %@AS@%        mov cx, power  ; Get second argument
  14206. %@AS@%        shl ax, cl     ; AX = AX * ( 2 to the power of CL )
  14207. %@AS@%     }
  14208. %@AS@%     /* Return with result in AX */
  14209. %@AS@%  }%@AE@%%@NL@%
  14210. %@NL@%
  14211. The in-line version of the %@AS@% power2 %@AE@% function refers to its arguments by name
  14212. and appears in the same source file as the rest of the program. This version
  14213. also requires fewer assembly instructions. Since C automatically preserves
  14214. BP, the %@AB@%_asm%@AE@% block doesn't need to do so. It can also dispense with the %@AB@%RET%@AE@%
  14215. instruction, since the C part of the function performs the return.  %@NL@%
  14216. %@NL@%
  14217. Because the in-line version of %@AS@% power2 %@AE@% doesn't execute a C %@AB@%return%@AE@%
  14218. statement, it causes a harmless warning if you compile at warning levels 2
  14219. or higher:  %@NL@%
  14220. %@NL@%
  14221. %@AS@%  warning C4035: 'power2' : no return value%@AE@%%@NL@%
  14222. %@NL@%
  14223. The function does return a value, but QuickC cannot tell that in the absence
  14224. of a %@AB@%return%@AE@% statement. Simply ignore the warning in this context.  %@NL@%
  14225. %@NL@%
  14226. %@NL@%
  14227. %@2@%%@CR:C6A00160017 @%%@AB@%Using and Preserving Registers%@AE@%%@EH@%%@NL@%
  14228. %@NL@%
  14229. In general, you should not assume that a register will have a given value
  14230. when an %@AB@%_asm%@AE@% block begins. An %@AB@%_asm%@AE@% block inherits whatever register values
  14231. happen to result from the normal flow of control.  %@NL@%
  14232. %@NL@%
  14233. As you may have noticed in the POWER2.C example in the previous section, the
  14234. %@AS@%power2 %@AE@% function doesn't preserve the value in the AX register. When you
  14235. write a function in assembly language, you don't need to preserve the AX,
  14236. BX, CX, DX, ES, and flags registers. However, you should preserve any other
  14237. registers you use (DI, SI, DS, SS, SP, and BP).  %@NL@%
  14238. %@NL@%
  14239. ────────────────────────────────────────────────────────────────────────────%@NL@%
  14240. %@AU@%WARNING%@AE@%%@NL@%
  14241. %@NL@%
  14242. If your in-line assembly code changes the direction flag using the STD or
  14243. CLD instructions, you must restore the flag to its original value.%@NL@%
  14244. ────────────────────────────────────────────────────────────────────────────%@NL@%
  14245. %@NL@%
  14246. The POWER2.C example in the previous section also shows that functions
  14247. return values in registers. This is true whether the function is written in
  14248. assembly language or in C.  %@NL@%
  14249. %@NL@%
  14250. %@AU@% Functions return values in the AX and DX registers.%@AE@%  %@NL@%
  14251. %@NL@%
  14252. If the return value is short (a %@AB@%char%@AE@%, %@AB@%int%@AE@%, or %@AB@%near%@AE@% pointer), it is stored in
  14253. AX. The POWER2.C example returned a value by terminating with the desired
  14254. value in AX.  %@NL@%
  14255. %@NL@%
  14256. If the return value is long, store the high word in DX and the low word in
  14257. AX. To return a longer value (such as a floating-point value), store the
  14258. value in memory and return a pointer to the value (in AX if %@AB@%near%@AE@% or in DX:AX
  14259. if %@AB@%far%@AE@%).  %@NL@%
  14260. %@NL@%
  14261. Assembly instructions that appear in-line with C statements are free to
  14262. alter the AX, BX, CX, and DX registers. C doesn't expect these registers to
  14263. be maintained between statements, so you don't need to preserve them. The
  14264. same is true of the SI and DI registers, with some exceptions (see the
  14265. section "Optimizing," below). You should preserve the SP and BP registers
  14266. unless you have some reason to change them─to switch stacks, for instance.  %@NL@%
  14267. %@NL@%
  14268. %@NL@%
  14269. %@2@%%@CR:C6A00160018 @%%@AB@%Jumping to Labels%@AE@%%@EH@%%@NL@%
  14270. %@NL@%
  14271. Like an ordinary C label, a label in an %@AB@%_asm%@AE@% block is visible (has scope)
  14272. throughout the function in which it is defined (not only in the block). Both
  14273. assembly instructions and C %@AB@%goto%@AE@% statements can jump to labels inside or
  14274. outside the %@AB@%_asm%@AE@% block.  %@NL@%
  14275. %@NL@%
  14276. %@AU@% Labels in _asm blocks have function scope and are not case sensitive.%@AE@%  %@NL@%
  14277. %@NL@%
  14278. Unlike C labels, labels defined in %@AB@%_asm%@AE@% blocks are not case sensitive, even
  14279. when used in C statements. C labels are not case sensitive in an %@AB@%_asm%@AE@% block,
  14280. either. (Outside an %@AB@%_asm%@AE@% block, a C label is case sensitive as usual.) The
  14281. following do-nothing code shows all the permutations.  %@NL@%
  14282. %@NL@%
  14283. %@AS@%  void func( void )
  14284. %@AS@%  {
  14285. %@AS@%     goto C_Dest;  /* legal */
  14286. %@AS@%     goto c_dest;  /* error */
  14287. %@AS@%  
  14288. %@AS@%     goto A_Dest;  /* legal */
  14289. %@AS@%     goto a_dest;  /* legal */
  14290. %@AS@%  
  14291. %@AS@%     _asm
  14292. %@AS@%     {
  14293. %@AS@%        jmp C_Dest ; legal
  14294. %@AS@%        jmp c_dest ; legal
  14295. %@AS@%  
  14296. %@AS@%        jmp A_Dest ; legal
  14297. %@AS@%        jmp a_dest ; legal
  14298. %@AS@%  
  14299. %@AS@%        a_dest:    ; _asm label
  14300. %@AS@%     }
  14301. %@AS@%  
  14302. %@AS@%     C_Dest:       /* C label */ 
  14303. %@AS@%     return;
  14304. %@AS@%  }%@AE@%%@NL@%
  14305. %@NL@%
  14306. Don't use C library function names as labels in %@AB@%_asm%@AE@% blocks. For instance,
  14307. you might be tempted to use %@AS@% exit %@AE@% as a label,  %@NL@%
  14308. %@NL@%
  14309. %@AS@%  jne exit
  14310. %@AS@%     .
  14311. %@AS@%     .
  14312. %@AS@%     .
  14313. %@AS@%  exit:
  14314. %@AS@%     ; More _asm code follows%@AE@%%@NL@%
  14315. %@NL@%
  14316. forgetting that %@AB@%exit%@AE@% is the name of a C library function. The code doesn't
  14317. cause a compiler error, but it might cause a jump to the %@AB@%exit%@AE@% function
  14318. instead of the desired location.  %@NL@%
  14319. %@NL@%
  14320. As in MASM programs, the dollar symbol (%@AB@%$%@AE@%) serves as the current location
  14321. counter─a label for the instruction currently being assembled. In %@AB@%_asm%@AE@%
  14322. blocks, its main use is to make long conditional jumps:  %@NL@%
  14323. %@NL@%
  14324. %@AS@%  jne $+5 ; next instruction is 5 bytes long
  14325. %@AS@%  jmp farlabel
  14326. %@AS@%  ; $+5
  14327. %@AS@%     .
  14328. %@AS@%     .
  14329. %@AS@%     .
  14330. %@AS@%  farlabel:%@AE@%%@NL@%
  14331. %@NL@%
  14332. %@NL@%
  14333. %@2@%%@CR:C6A00160019 @%%@AB@%Calling C Functions%@AE@%%@EH@%%@NL@%
  14334. %@NL@%
  14335. An %@AB@%_asm%@AE@% block can call C functions, including C library routines. The
  14336. following example calls the %@AB@%printf%@AE@% library routine:  %@NL@%
  14337. %@NL@%
  14338. %@AS@%  #include <stdio.h>
  14339. %@AS@%  
  14340. %@AS@%  char format[] = "%s %s\n";
  14341. %@AS@%  char hello[] = "Hello";
  14342. %@AS@%  char world[] = "world";
  14343. %@AS@%  
  14344. %@AS@%  void main( void )
  14345. %@AS@%  {
  14346. %@AS@%     _asm
  14347. %@AS@%     {
  14348. %@AS@%        mov  ax, offset world
  14349. %@AS@%        push ax
  14350. %@AS@%        mov  ax, offset hello
  14351. %@AS@%        push ax
  14352. %@AS@%        mov  ax, offset format
  14353. %@AS@%        push ax
  14354. %@AS@%        call printf
  14355. %@AS@%       add sp, 6
  14356. %@AS@%     }
  14357. %@AS@%  }%@AE@%%@NL@%
  14358. %@NL@%
  14359. Since function arguments are passed on the stack, you simply push the needed
  14360. arguments─string pointers, in the example above─before calling the function.
  14361. The arguments are pushed in reverse order, so they come off the stack in the
  14362. desired order. To emulate the C statement  %@NL@%
  14363. %@NL@%
  14364. %@AS@%  printf( format, hello, world );%@AE@%%@NL@%
  14365. %@NL@%
  14366. the example pushes pointers to %@AS@% world%@AE@%, %@AS@% hello%@AE@%, and %@AS@% format%@AE@%, in that order,
  14367. then calls %@AB@%printf%@AE@%. The last instruction in the %@AB@%_asm%@AE@% block adjusts the stack
  14368. to account for the arguments previously pushed onto it.  %@NL@%
  14369. %@NL@%
  14370. %@NL@%
  14371. %@2@%%@CR:C6A00160020 @%%@AB@%Defining _asm Blocks as C Macros%@AE@%%@EH@%%@NL@%
  14372. %@NL@%
  14373. C macros offer a convenient way to insert assembly code into C code, but
  14374. they demand extra care because a macro expands into a single logical line.
  14375. To create trouble-free macros, follow these rules:  %@NL@%
  14376. %@NL@%
  14377. %@NL@%
  14378.   ■   Enclose the %@AB@%_asm%@AE@% block in braces%@NL@%
  14379. %@NL@%
  14380.   ■   Put the %@AB@%_asm%@AE@% keyword in front of each assembly instruction%@NL@%
  14381. %@NL@%
  14382.   ■   Use old-style C comments (%@AS@% /* comment */ %@AE@%) instead of assembly-style
  14383.       comments (%@AS@% ; comment %@AE@%)%@NL@%
  14384. %@NL@%
  14385. %@NL@%
  14386. To illustrate, the following example defines a simple macro:  %@NL@%
  14387. %@NL@%
  14388. %@AS@%  #define BEEP _asm \
  14389. %@AS@%  /* Beep sound */       \
  14390. %@AS@%  {                       \
  14391. %@AS@%     _asm mov ah, 2       \
  14392. %@AS@%     _asm mov dl, 7       \
  14393. %@AS@%     _asm int 21h         \
  14394. %@AS@%  }%@AE@%%@NL@%
  14395. %@NL@%
  14396. At first glance, the last three %@AB@%_asm%@AE@% keywords seem superfluous. They are
  14397. needed, however, because the macro expands into a single line:  %@NL@%
  14398. %@NL@%
  14399. %@AS@%  _asm /* Beep sound */ { _asm mov ah, 2  _asm mov dl, 7 _asm int 21h }%@AE@%%@NL@%
  14400. %@NL@%
  14401. The third and fourth %@AB@%_asm%@AE@% keywords are needed as statement separators. The
  14402. only statement separators recognized in %@AB@%_asm%@AE@% blocks are the newline
  14403. character and %@AB@%_asm%@AE@% keyword. And since a block defined as a macro is one
  14404. logical line, you must separate each instruction with %@AB@%_asm%@AE@%.  %@NL@%
  14405. %@NL@%
  14406. The braces are essential as well. If you omit them, the compiler can be
  14407. confused by C statements on the same line to the right of the macro
  14408. invocation. Without the closing brace, QuickC cannot tell where assembly
  14409. code stops, and it sees C statements after the %@AB@%_asm%@AE@% block as assembly
  14410. instructions.  %@NL@%
  14411. %@NL@%
  14412. %@AU@%  Use C comments in _asm blocks written as macros.%@AE@%  %@NL@%
  14413. %@NL@%
  14414. Assembly-style comments that start with a semicolon (;) continue to the end
  14415. of the line. This causes problems in macros because QuickC ignores
  14416. everything after the comment, all the way to the end of the logical line. To
  14417. prevent errors, use C comments (%@AS@% /* comment */ %@AE@%) in %@AB@%_asm%@AE@% blocks defined as
  14418. macros.  %@NL@%
  14419. %@NL@%
  14420. An %@AB@%_asm%@AE@% block written as a C macro can take arguments. Unlike an ordinary C
  14421. macro, however, an %@AB@%_asm%@AE@% macro cannot return a value. So you cannot use such
  14422. macros in C expressions.  %@NL@%
  14423. %@NL@%
  14424. %@AU@% You can convert MASM macros to C macros.%@AE@%  %@NL@%
  14425. %@NL@%
  14426. Note that some MASM-style macros can be written as C macros. Below is a MASM
  14427. macro that sets the video page to the value specified in the %@AS@% page %@AE@%
  14428. argument:  %@NL@%
  14429. %@NL@%
  14430. %@AS@%  setpage   MACRO page
  14431. %@AS@%            mov ah, 5
  14432. %@AS@%            mov al, page
  14433. %@AS@%            int 10h
  14434. %@AS@%            ENDM%@AE@%%@NL@%
  14435. %@NL@%
  14436. The following code defines %@AS@% setpage %@AE@% as a C macro:  %@NL@%
  14437. %@NL@%
  14438. %@AS@%  #define setpage( page ) _asm  \
  14439. %@AS@%     {                                \
  14440. %@AS@%        _asm mov ah, 5                \
  14441. %@AS@%        _asm mov al, page             \
  14442. %@AS@%        _asm int 10h                  \
  14443. %@AS@%  }%@AE@%%@NL@%
  14444. %@NL@%
  14445. Both macros do the same job.  %@NL@%
  14446. %@NL@%
  14447. %@NL@%
  14448. %@2@%%@CR:C6A00160021 @%%@AB@%Optimizing%@AE@%%@EH@%%@NL@%
  14449. %@NL@%
  14450. The presence of an %@AB@%_asm%@AE@% block in a function affects optimization in a few
  14451. different ways. First, as you might expect, QuickC doesn't try to optimize
  14452. the %@AB@%_asm%@AE@% block itself. What you write in assembly language is exactly what
  14453. you get.  %@NL@%
  14454. %@NL@%
  14455. Second, the presence of an %@AB@%_asm%@AE@% block affects register variable storage.
  14456. (See the section "Register Variables" in Chapter 5, "Advanced Data Types,"
  14457. for a discussion of register variables.) Under normal circumstances, QuickC
  14458. automatically stores variables in registers. This is not done, however, in
  14459. any function that contains an %@AB@%_asm%@AE@% block. To get register variable storage
  14460. in such a function, you must request it with the %@AB@%register%@AE@% keyword.  %@NL@%
  14461. %@NL@%
  14462. Since the compiler stores register variables in the SI and DI registers,
  14463. these registers represent variables in functions that request register
  14464. storage. The first eligible variable is stored in SI and the second in DI.
  14465. Preserve SI and DI in such functions unless you want to change the register
  14466. variables.  %@NL@%
  14467. %@NL@%
  14468. Keep in mind that the name of a variable declared with %@AB@%register%@AE@% translates
  14469. directly into a register reference (assuming a register is available for
  14470. such use). For instance, if you declare  %@NL@%
  14471. %@NL@%
  14472. %@AS@%  register int sample;%@AE@%%@NL@%
  14473. %@NL@%
  14474. and the variable %@AS@% sample %@AE@% happens to be stored in SI, then the %@AB@%_asm%@AE@%
  14475. instruction  %@NL@%
  14476. %@NL@%
  14477. %@AS@%  _asm mov ax, sample%@AE@%%@NL@%
  14478. %@NL@%
  14479. is equivalent to  %@NL@%
  14480. %@NL@%
  14481. %@AS@%  _asm mov ax, si%@AE@%%@NL@%
  14482. %@NL@%
  14483. If you declare a variable with %@AB@%register%@AE@% and the compiler cannot store the
  14484. variable in a register, QuickC issues a compiler error if you reference the
  14485. variable in an %@AB@%_asm%@AE@% block. The solution is to remove the %@AB@%register%@AE@%
  14486. declaration from that variable.  %@NL@%
  14487. %@NL@%
  14488. Register variables form a slight exception to the general rule that an
  14489. assembly-language statement can contain no more than one C symbol. If one of
  14490. the symbols is a register variable, for example,  %@NL@%
  14491. %@NL@%
  14492. %@AS@%  register int v1;
  14493. %@AS@%  int v2;%@AE@%%@NL@%
  14494. %@NL@%
  14495. then an instruction can use two C symbols, as in  %@NL@%
  14496. %@NL@%
  14497. %@AS@%  mov v1, v2%@AE@%%@NL@%
  14498. %@NL@%
  14499. Finally, the presence of in-line assembly code inhibits loop optimization
  14500. for the entire function in which the code appears. (Loop optimization can be
  14501. selected with the /Ol command-line switch; see Chapter 4, "QCL Command
  14502. Reference," in %@AI@%Microsoft QuickC Tool Kit%@AE@%.) This optimization is suppressed
  14503. no matter which compiler options you use.  %@NL@%
  14504. %@NL@%
  14505. %@NL@%
  14506. %@2@%%@CR:C6A00160022 @%%@AB@%References and Books on Assembly Language%@AE@%%@EH@%%@NL@%
  14507. %@NL@%
  14508. Assembly language varies widely for different computer processors. In
  14509. selecting a reference on assembly language, make sure it describes assembly
  14510. for the Intel 8086 family of processors or compatibles. These are the
  14511. microprocessors used in the IBM and IBM-compatible computers able to run
  14512. QuickC.  %@NL@%
  14513. %@NL@%
  14514. The following books and articles may be useful in learning to program in
  14515. assembly language:  %@NL@%
  14516. %@NL@%
  14517. Chesley, Harry R. and Mitchell Waite. %@AI@%Supercharging C with Assembly
  14518. %@AI@%Language%@AE@%.
  14519.     Reading, Massachusetts: Addison-Wesley Publishing Company,  Inc., 1987.%@NL@%
  14520. %@NL@%
  14521. Duncan, Ray. %@AI@%Advanced MS-DOS Programming, %@AE@%2nd ed. Redmond,  Washington:
  14522. Microsoft Press, 1988.%@NL@%
  14523. %@NL@%
  14524. Lafore, Robert. %@AI@%Assembly Language Primer for the IBM PC & XT%@AE@%.
  14525.     New York, New York: Plume/Waite, 1984.%@NL@%
  14526. %@NL@%
  14527. Metcalf, Christopher D. and Marc B. Sugiyama. %@AI@%COMPUTE!'s Beginner's Guide to
  14528. %@AI@%Machine Language on the IBM PC & PCjr%@AE@%.
  14529.     Greensboro, North Carolina: COMPUTE! Publications, Inc., 1985.%@NL@%
  14530. %@NL@%
  14531. Microsoft. %@AI@%Microsoft Macro Assembler 5.1 Programmer's Guide%@AE@%.
  14532.     Redmond, Washington, 1987. (Included with Microsoft Macro Assembler.)%@NL@%
  14533. %@NL@%
  14534. Microsoft. %@AI@%Microsoft Macro Assembler 5.1 Reference%@AE@%.
  14535.     Redmond, Washington, 1987. (Included with Microsoft Macro Assembler.)%@NL@%
  14536. %@NL@%
  14537. Sargent, Murray and Richard L. Shoemaker. %@AI@%The IBM Personal Computer from the
  14538. %@AI@%Inside Out%@AE@%.
  14539.     Reading, Massachusetts: Addison-Wesley Publishing Company, Inc., 1986.%@NL@%
  14540. %@NL@%
  14541. The above references are listed for your convenience only. With the
  14542. exception of those published by Microsoft, Microsoft Corporation does not
  14543. endorse these books or recommend them over others on the same subject.  %@NL@%
  14544. %@NL@%
  14545. %@NL@%
  14546. %@NL@%
  14547. %@NL@%
  14548. %@NL@%
  14549. %@NL@%
  14550. %@CR:C6A-A0001   @%%@1@%%@AB@%Appendix A  C Language Guide%@AE@%%@EH@%%@NL@%
  14551. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  14552. %@NL@%
  14553. This appendix provides a quick summary of C language fundamentals. It does
  14554. not attempt to teach you the C language (Part 1 of this book does that) or
  14555. document all the details of C. Use it as a refresher or ready reference
  14556. after you have read all the material in Chapters 1 through 10.  %@NL@%
  14557. %@NL@%
  14558. To simplify reference, this appendix has the same general organization as
  14559. the chapters in Part 1. Each major section lists the chapter(s) where you
  14560. may find more detailed information on a given topic.  %@NL@%
  14561. %@NL@%
  14562. You can also use QuickC's online help to get instant information on any
  14563. topic. The online help index and table of contents provide alternate ways to
  14564. access information.  %@NL@%
  14565. %@NL@%
  14566. %@NL@%
  14567. %@2@%%@CR:C6A-A0002   @%%@AB@%General Syntax%@AE@%%@EH@%%@NL@%
  14568. %@NL@%
  14569. Basic C-language syntax is explained in Chapter 1, "Anatomy of a C Program."
  14570. %@NL@%
  14571. %@NL@%
  14572. A C statement consists of keywords, expressions, and function calls. A
  14573. statement always ends with a semicolon. A statement block is a collection of
  14574. statements enclosed by braces (%@AB@%{%@AE@% %@AB@%}%@AE@%). A statement block can appear anywhere a
  14575. simple C statement appears. No semicolon occurs after the closing brace.  %@NL@%
  14576. %@NL@%
  14577. C is a free-format programming language. You can insert "whitespace"
  14578. characters (spaces, tabs, carriage returns, and form feeds) almost anywhere,
  14579. to indent statement blocks and otherwise make your code more readable.  %@NL@%
  14580. %@NL@%
  14581. Comments begin with the slash-asterisk sequence (%@AB@%/*%@AE@%) and end with the
  14582. asterisk-slash sequence (%@AB@%*/%@AE@%). Comments are legal anywhere a space is legal,
  14583. but they cannot be nested.  %@NL@%
  14584. %@NL@%
  14585. %@NL@%
  14586. %@3@%%@CR:C6A-A0003   @%%@AB@%User-Defined Names%@AE@%%@EH@%%@NL@%
  14587. %@NL@%
  14588. The rules governing user-defined names are explained in Chapter 1, "Anatomy
  14589. of a C Program," and Chapter 4, "Basic Data Types."  %@NL@%
  14590. %@NL@%
  14591. You can define your own names ("identifiers") for variables, functions, and
  14592. user-defined types. Identifiers are case sensitive. For instance, the
  14593. identifier %@AS@%myVariable %@AE@% is not the same as the identifier %@AS@% Myvariable%@AE@%. You
  14594. cannot use a C keyword (see the list below) as an identifier.  %@NL@%
  14595. %@NL@%
  14596. An identifier can contain only the following characters:  %@NL@%
  14597. %@NL@%
  14598. %@NL@%
  14599.   ■   abcdefghijklmnopqrstuvwxyz%@NL@%
  14600. %@NL@%
  14601.   ■   ABCDEFGHIJKLMNOPQRSTUVWXYZ%@NL@%
  14602. %@NL@%
  14603.   ■   0123456789%@NL@%
  14604. %@NL@%
  14605.   ■   _ (underscore)%@NL@%
  14606. %@NL@%
  14607. %@NL@%
  14608. The first character of an identifier must be a letter or the underscore
  14609. character. The first 31 characters of local identifiers are significant. The
  14610. name can contain more than 31 characters, but QuickC ignores everything
  14611. beyond the thirty-first character. Global identifiers are normally
  14612. significant to 30 characters.  %@NL@%
  14613. %@NL@%
  14614. %@NL@%
  14615. %@3@%%@CR:C6A-A0004   @%%@AB@%Keywords%@AE@%%@EH@%%@NL@%
  14616. %@NL@%
  14617. A keyword has a special meaning in the C language. You must spell keywords
  14618. as shown in the following list, and you cannot use them as user-defined
  14619. names (see above).  %@NL@%
  14620. %@NL@%
  14621. %@AB@%_asm            _emit           _interrupt       signed%@AE@%
  14622. %@AB@% auto            enum           _loadds          sizeof%@AE@%
  14623. %@AB@%_based          _export          long            static%@AE@%
  14624. %@AB@%  break          extern         _near            struct%@AE@%
  14625. %@AB@% case           _far            _pascal          switch%@AE@%
  14626. %@AB@%_cdecl          _fastcall        register        typedef%@AE@%
  14627. %@AB@% char            float           return          union%@AE@%
  14628. %@AB@% const           for            _saveregs        unsigned%@AE@%
  14629. %@AB@% continue       _fortran        _segname         void%@AE@%
  14630. %@AB@% default         goto           _segment         volatile%@AE@%
  14631. %@AB@% do             _huge           _self            while%@AE@%
  14632. %@AB@% double          if              short          %@AE@%
  14633. %@AB@% else            int            
  14634.  
  14635. A few other words, such as %@AB@%main%@AE@%, have a special meaning but are not keywords
  14636. in the strict sense. Use online help to get details on all such words.  %@NL@%
  14637. %@NL@%
  14638. %@NL@%
  14639. %@2@%%@CR:C6A-A0005   @%%@AB@%Functions%@AE@%%@EH@%%@NL@%
  14640. %@NL@%
  14641. The rules governing C functions are explained in Chapter 2, "Functions."  %@NL@%
  14642. %@NL@%
  14643. Every C program must have at least one function, named %@AB@%main%@AE@%, which marks the
  14644. beginning and end of the program's execution. Every executable statement in
  14645. a C program must occur within a function.  %@NL@%
  14646. %@NL@%
  14647. Variables can be declared inside or outside functions. Variables declared
  14648. inside a function are "local" and can only be accessed in that function.
  14649. Variables declared outside all functions are "global" and can be accessed
  14650. from any function in your program.  %@NL@%
  14651. %@NL@%
  14652. You call a C function by stating its name. If the function requires
  14653. "arguments" (data), you list the arguments in the parentheses that follow
  14654. the function name. Arguments that you pass to a function become local
  14655. variables in the function.  %@NL@%
  14656. %@NL@%
  14657. A function can return a value (using the %@AB@%return%@AE@% keyword) or return nothing.
  14658. If the function contains no %@AB@%return%@AE@% statement, it ends automatically when
  14659. execution reaches the closing brace of the function definition.  %@NL@%
  14660. %@NL@%
  14661. A function "prototype" (declaration) tells QuickC the function's name, the
  14662. type of value it returns, and the number and type of arguments it requires.
  14663. Function prototypes normally appear near the beginning of the program. They
  14664. allow QuickC to check the accuracy of every reference to the function.  %@NL@%
  14665. %@NL@%
  14666. %@NL@%
  14667. %@2@%%@CR:C6A-A0006   @%%@AB@%Flow Control%@AE@%%@EH@%%@NL@%
  14668. %@NL@%
  14669. Flow-control statements are explained in Chapter 3, "Flow Control."  %@NL@%
  14670. %@NL@%
  14671. The C language provides several kinds of flow-control statements. The %@AB@%for%@AE@%,
  14672. %@AB@%while%@AE@%, and %@AB@%do%@AE@% statements create loops. The %@AB@%if%@AE@% and %@AB@%switch%@AE@% statements perform
  14673. a branch. The %@AB@%break%@AE@%, %@AB@%continue%@AE@%, %@AB@%return%@AE@%, and %@AB@%goto%@AE@% statements perform an
  14674. unconditional "jump" to another location in your program.  %@NL@%
  14675. %@NL@%
  14676. The following sections describe the C flow-control statements in
  14677. alphabetical order.  %@NL@%
  14678. %@NL@%
  14679. %@NL@%
  14680. %@3@%%@CR:C6A-A0007   @%%@AB@%The break Statement%@AE@%%@EH@%%@NL@%
  14681. %@NL@%
  14682. The %@AB@%break%@AE@% statement terminates the smallest enclosing %@AB@%do%@AE@%, %@AB@%for%@AE@%, %@AB@%switch%@AE@%, or
  14683. %@AB@%while%@AE@% statement in which it appears. It passes control to the statement
  14684. following the terminated statement.  %@NL@%
  14685. %@NL@%
  14686. This statement is often used to exit from a loop or %@AB@%switch%@AE@% statement (see
  14687. below). The following example illustrates %@AB@%break%@AE@%:  %@NL@%
  14688. %@NL@%
  14689. %@AS@%  while( c != 'Q' )
  14690. %@AS@%  {
  14691. %@AS@%    /* Some C statements here */
  14692. %@AS@%    if( number_of_characters > 80 )
  14693. %@AS@%        break;  /* Break out of while loop */
  14694. %@AS@%    /* More C statements here */
  14695. %@AS@%  }
  14696. %@AS@%  /* Execution continues here after break statement */%@AE@%%@NL@%
  14697. %@NL@%
  14698. %@NL@%
  14699. %@3@%%@CR:C6A-A0008   @%%@AB@%The continue Statement%@AE@%%@EH@%%@NL@%
  14700. %@NL@%
  14701. The %@AB@%continue%@AE@% statement is the opposite of the %@AB@%break%@AE@% statement. It passes
  14702. control to the next iteration of the smallest enclosing %@AB@%do%@AE@%, %@AB@%for%@AE@%, or %@AB@%while%@AE@%
  14703. statement in which it appears.  %@NL@%
  14704. %@NL@%
  14705. This statement is often used to return to the start of a loop from within a
  14706. deeply nested loop.  %@NL@%
  14707. %@NL@%
  14708. The following example illustrates %@AB@%continue%@AE@%:  %@NL@%
  14709. %@NL@%
  14710. %@AS@%  while( c != 'Q' )
  14711. %@AS@%  {
  14712. %@AS@%    /* Some C statements here*/
  14713. %@AS@%    if( c == 0x20 )
  14714. %@AS@%       continue;    /* Skip rest of loop */
  14715. %@AS@%    /* More C statements here */
  14716. %@AS@%  }%@AE@%%@NL@%
  14717. %@NL@%
  14718. In the example, the %@AB@%continue%@AE@% statement skips to the next iteration of the
  14719. loop whenever %@AS@% c %@AE@% equals 0x20, the ASCII value for a space character.  %@NL@%
  14720. %@NL@%
  14721. %@NL@%
  14722. %@3@%%@CR:C6A-A0009   @%%@AB@%The do Statement%@AE@%%@EH@%%@NL@%
  14723. %@NL@%
  14724. The %@AB@%do%@AE@% statement repeats a statement until a specified expression becomes
  14725. false. The test expression in the loop is evaluated after the body of the
  14726. loop executes. Thus, the body of a %@AB@%do%@AE@% loop always executes at least once.  %@NL@%
  14727. %@NL@%
  14728. Use a %@AB@%break%@AE@%, %@AB@%goto%@AE@%, or %@AB@%return%@AE@% statement when you need to exit a %@AB@%do%@AE@% loop
  14729. early. Use the %@AB@%continue%@AE@% statement to terminate an iteration without exiting
  14730. the loop. The %@AB@%continue%@AE@% statement passes control to the next iteration of the
  14731. loop.  %@NL@%
  14732. %@NL@%
  14733. The following example illustrates %@AB@%do%@AE@%:  %@NL@%
  14734. %@NL@%
  14735. %@AS@%  sample = 1;
  14736. %@AS@%  do
  14737. %@AS@%    printf( "%d\t%d\n", sample, sample * sample );
  14738. %@AS@%  while( ++x <= 7 );%@AE@%%@NL@%
  14739. %@NL@%
  14740. The %@AB@%printf%@AE@% statement in the example always executes at least once, no matter
  14741. what value %@AS@% x %@AE@% has when the loop begins.  %@NL@%
  14742. %@NL@%
  14743. %@NL@%
  14744. %@3@%%@CR:C6A-A0010   @%%@AB@%The for Statement%@AE@%%@EH@%%@NL@%
  14745. %@NL@%
  14746. The %@AB@%for%@AE@% statement lets you repeat a statement a specified number of times.
  14747. It consists of three expressions:  %@NL@%
  14748. %@NL@%
  14749. %@NL@%
  14750.   ■   An initializing expression, which is evaluated when the loop begins%@NL@%
  14751. %@NL@%
  14752.   ■   A test expression, which is evaluated before each iteration of the
  14753.       loop%@NL@%
  14754. %@NL@%
  14755.   ■   A modifying expression, which is evaluated at the end of each
  14756.       iteration of the loop%@NL@%
  14757. %@NL@%
  14758. %@NL@%
  14759. These expressions are enclosed in parentheses and followed by the loop
  14760. body─the statement the loop is to execute. Each expression in the
  14761. parentheses can be any legal C statement.  %@NL@%
  14762. %@NL@%
  14763. The %@AB@%for%@AE@% statement works as follows:  %@NL@%
  14764. %@NL@%
  14765. %@NL@%
  14766.   1.  The initializing expression is evaluated.%@NL@%
  14767. %@NL@%
  14768.   2.  As long as the test expression evaluates to a nonzero value, the loop
  14769.       body is executed. When the test expression becomes 0, control passes
  14770.       to the statement following the loop body.%@NL@%
  14771. %@NL@%
  14772.   3.  At the end of each iteration of the loop, the modifying expression is
  14773.       evaluated.%@NL@%
  14774. %@NL@%
  14775. %@NL@%
  14776. You can use a %@AB@%break%@AE@%, %@AB@%goto%@AE@%, or %@AB@%return%@AE@% statement to exit a %@AB@%for%@AE@% loop early. Use
  14777. the %@AB@%continue%@AE@% statement to terminate an iteration without exiting the %@AB@%for%@AE@%
  14778. loop. The %@AB@%continue%@AE@% statement passes control to the next iteration of the
  14779. loop.  %@NL@%
  14780. %@NL@%
  14781. The following example illustrates %@AB@%for%@AE@%:  %@NL@%
  14782. %@NL@%
  14783. %@AS@%  for( counter = 0; counter < 100; counter++ )
  14784. %@AS@%  {
  14785. %@AS@%    x[counter] = 0; /* Set every array element to zero */
  14786. %@AS@%  }%@AE@%%@NL@%
  14787. %@NL@%
  14788. %@NL@%
  14789. %@3@%%@CR:C6A-A0011   @%%@AB@%The goto Statement%@AE@%%@EH@%%@NL@%
  14790. %@NL@%
  14791. The %@AB@%goto%@AE@% statement performs a jump to the statement following the specified
  14792. label. A %@AB@%goto%@AE@% statement can jump anywhere within the current function.  %@NL@%
  14793. %@NL@%
  14794. A common use of %@AB@%goto%@AE@% is to exit immediately from a deeply nested loop. For
  14795. instance:  %@NL@%
  14796. %@NL@%
  14797. %@AS@%  for( ... ) 
  14798. %@AS@%  {
  14799. %@AS@%     for( ... ) 
  14800. %@AS@%     {
  14801. %@AS@%        /* Do something here */
  14802. %@AS@%        if(c == CTRL_C)
  14803. %@AS@%           goto myplace;
  14804. %@AS@%     }
  14805. %@AS@%     /* Do something else here */
  14806. %@AS@%  }
  14807. %@AS@%  
  14808. %@AS@%  /* The goto label is named myplace */
  14809. %@AS@%  myplace:
  14810. %@AS@%  /* The goto statement transfers control here */%@AE@%%@NL@%
  14811. %@NL@%
  14812. %@NL@%
  14813. %@3@%%@CR:C6A-A0012   @%%@AB@%The if Statement%@AE@%%@EH@%%@NL@%
  14814. %@NL@%
  14815. The %@AB@%if%@AE@% statement performs a branch based on the outcome of a conditional
  14816. test. If the test expression is true, the body of the %@AB@%if%@AE@% statement executes.
  14817. If it is false, the statement body is skipped.  %@NL@%
  14818. %@NL@%
  14819. The %@AB@%else%@AE@% keyword is used with %@AB@%if%@AE@% to form an either-or construct that
  14820. executes one statement when the test expression is true and another when
  14821. it's false. C does not offer an "else-if" keyword. You can combine %@AB@%if%@AE@% and
  14822. %@AB@%else%@AE@% statements to achieve the same effect. C pairs each %@AB@%else%@AE@% with the most
  14823. recent %@AB@%if%@AE@% that lacks an %@AB@%else%@AE@%.  %@NL@%
  14824. %@NL@%
  14825. Below is a simple %@AB@%if%@AE@% statement:  %@NL@%
  14826. %@NL@%
  14827. %@AS@%  if( score < 70 )
  14828. %@AS@%    grade = 'F';
  14829. %@AS@%  else
  14830. %@AS@%    grade = 'P';%@AE@%%@NL@%
  14831. %@NL@%
  14832. If the value of the variable %@AS@% score %@AE@% is less than 70, the variable %@AS@% grade %@AE@%
  14833. is set to the constant %@AS@% F%@AE@%. Otherwise, %@AS@% score %@AE@% is set to %@AS@% P%@AE@%.  %@NL@%
  14834. %@NL@%
  14835. %@NL@%
  14836. %@3@%%@CR:C6A-A0013   @%%@AB@%The return Statement%@AE@%%@EH@%%@NL@%
  14837. %@NL@%
  14838. The %@AB@%return%@AE@% statement ends the execution of the function in which it appears.
  14839. It can also return a value to the calling function. For example:  %@NL@%
  14840. %@NL@%
  14841. %@AS@%  return;      /* End function and return no value */
  14842. %@AS@%  
  14843. %@AS@%  return myvariable; /* End function and return value of myvariable */%@AE@%%@NL@%
  14844. %@NL@%
  14845. %@NL@%
  14846. %@3@%%@CR:C6A-A0014   @%%@AB@%The switch Statement%@AE@%%@EH@%%@NL@%
  14847. %@NL@%
  14848. The %@AB@%switch%@AE@% statement allows you to branch to various sections of code based
  14849. on the value of a single variable. This variable must evaluate to a %@AB@%char%@AE@%,
  14850. %@AB@%int%@AE@%, or %@AB@%long%@AE@% constant.  %@NL@%
  14851. %@NL@%
  14852. Each section of code in the %@AB@%switch%@AE@% statement is marked with a case label─the
  14853. keyword %@AB@%case%@AE@% followed by a constant or constant expression. The value of the
  14854. %@AB@%switch%@AE@% test expression is compared to the constant in each case label. If a
  14855. match is found, control transfers to the statement after the matching label
  14856. and continues until you reach a %@AB@%break%@AE@% statement or the end of the %@AB@%switch%@AE@%
  14857. statement.  %@NL@%
  14858. %@NL@%
  14859. For example:  %@NL@%
  14860. %@NL@%
  14861. %@AS@%  switch( answer )
  14862. %@AS@%  {
  14863. %@AS@%    case 'y': /* First case */
  14864. %@AS@%       printf( "lowercase y\n" );
  14865. %@AS@%       break;
  14866. %@AS@%  
  14867. %@AS@%    case 'n': /* Another case */
  14868. %@AS@%       printf( "lowercase n\n" );
  14869. %@AS@%       break;
  14870. %@AS@%  
  14871. %@AS@%    default:  /* Default case */
  14872. %@AS@%       printf( "not a lowercase y or n\n" );
  14873. %@AS@%       break;
  14874. %@AS@%  }%@AE@%%@NL@%
  14875. %@NL@%
  14876. The example tests the value of the variable %@AS@% answer%@AE@%. If %@AS@% answer %@AE@% evaluates
  14877. to the constant %@AS@% 'y'%@AE@%, control transfers to the first case in the %@AB@%switch%@AE@%
  14878. statement. If it equals %@AS@% 'n'%@AE@%, control transfers to the second case.  %@NL@%
  14879. %@NL@%
  14880. A case labelled with the %@AB@%default%@AE@% keyword executes when none of the other
  14881. case constants matches the value of the %@AB@%switch%@AE@% test expression. In the
  14882. example, the %@AB@%default%@AE@% case executes when %@AS@% answer %@AE@% equals any value other than
  14883. %@AS@% 'y' %@AE@% or %@AS@% 'n'%@AE@%.  %@NL@%
  14884. %@NL@%
  14885. If you omit the %@AB@%break%@AE@% statement at the end of a case, execution falls
  14886. through to the next case.  %@NL@%
  14887. %@NL@%
  14888. If you omit the %@AB@%default%@AE@% case and no matching case is found, nothing in the
  14889. %@AB@%switch%@AE@% statement executes.  %@NL@%
  14890. %@NL@%
  14891. No two %@AB@%case%@AE@% constants in the same %@AB@%switch%@AE@% statement can have the same value.
  14892. %@NL@%
  14893. %@NL@%
  14894. %@NL@%
  14895. %@3@%%@CR:C6A-A0015   @%%@AB@%The while Statement%@AE@%%@EH@%%@NL@%
  14896. %@NL@%
  14897. The %@AB@%while%@AE@% statement repeats a statement until its test expression becomes
  14898. false. A %@AB@%while%@AE@% loop evaluates its test expression before executing its loop
  14899. body. If the test expression is false when the loop begins, the loop body
  14900. never executes. (Contrast this behavior with the %@AB@%do%@AE@% loop, which always
  14901. executes its loop body at least once.)  %@NL@%
  14902. %@NL@%
  14903. For example:  %@NL@%
  14904. %@NL@%
  14905. %@AS@%  while( !sample )  /* Repeat until sample equals 1 */
  14906. %@AS@%  {
  14907. %@AS@%    printf( "%d\t%d\n", x, x*x );
  14908. %@AS@%    x += 6;
  14909. %@AS@%    if( x > 20 )
  14910. %@AS@%       sample = 1;
  14911. %@AS@%  }%@AE@%%@NL@%
  14912. %@NL@%
  14913. You can exit a %@AB@%while%@AE@% loop early with a %@AB@%break%@AE@% or %@AB@%goto%@AE@% statement. The %@AB@%continue%@AE@%
  14914. statement skips to the next iteration of the loop.  %@NL@%
  14915. %@NL@%
  14916. %@NL@%
  14917. %@2@%%@CR:C6A-A0016   @%%@AB@%Data Types%@AE@%%@EH@%%@NL@%
  14918. %@NL@%
  14919. Data types are explained in Chapter 4, "Data Types," and Chapter 5,
  14920. "Advanced Data Types." A brief description is given here.  %@NL@%
  14921. %@NL@%
  14922. %@NL@%
  14923. %@3@%%@CR:C6A-A0017   @%%@AB@%Basic Data Types%@AE@%%@EH@%%@NL@%
  14924. %@NL@%
  14925. The basic data types in C are character (%@AB@%char%@AE@%), integer (%@AB@%int%@AE@%), and floating
  14926. point (%@AB@%float%@AE@% and %@AB@%double%@AE@%). All other data types are derived from these basic
  14927. types. For example, a string is an array of %@AB@%char%@AE@% values.  %@NL@%
  14928. %@NL@%
  14929. Table A.1 lists the range of values for each data type.  %@NL@%
  14930. %@NL@%
  14931. %@AB@%Table A.1  %@AB@%Basic Data Types%@AE@%%@AE@%
  14932.  
  14933. %@TH:  38  1806 02 17 29 30 @%Type Name        Other Names                  Range of Values%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%char%@AE@%             %@AB@%signed char%@AE@%                  -128 to 127%@AB@%unsigned char%@AE@%    none                         0 to 255%@AB@%int%@AE@%              %@AB@%signed%@AE@%, %@AB@%signed int%@AE@%           -32,768 to 32,767%@AB@%unsigned%@AE@%         %@AB@%unsigned int%@AE@%                 0 to 65,535%@AB@%unsigned short%@AE@%   %@AB@%unsigned short int%@AE@%           0 to 65,535%@AB@%short%@AE@%            %@AB@%short int%@AE@%, %@AB@%signed short%@AE@%      -32,768 to                 %@AB@%signed short int%@AE@%             32,767%@AB@%long%@AE@%             %@AB@%long int%@AE@%, %@AB@%signed long%@AE@%        -2,147,483,647 to                 %@AB@%signed long int%@AE@%              2,147,483,648%@AB@%unsigned long%@AE@%    %@AB@%unsigned long int%@AE@%            0 to 4,294,967,295%@AB@%_segment%@AE@%         none                         0 to 65,535%@AB@%enum%@AE@%             none                         -32,768 to 32,767%@AB@%float%@AE@%            none                         Approximately 1.2E-38 to                                               3.4E+38 (7-digit precision)%@AB@%double%@AE@%           none                         Approximately 2.2E-308 to                                               1.8E+308 (15-digit precision)%@AB@%long double%@AE@%      none                         Approximately 3.4E-4932 to                                               1.2E+4932 (19-digit                                               precision)%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  38  1806 02 17 29 30 @%
  14934.  
  14935. %@NL@%
  14936. %@4@%%@AB@%Character Type%@AE@%%@EH@%%@NL@%
  14937. %@NL@%
  14938. The character type (%@AB@%char%@AE@%) occupies one byte of storage and can express a
  14939. whole number in the range of -128 to 127. Unsigned characters have a range
  14940. of 0 to 255. You can represent any ASCII character as an %@AB@%unsigned char%@AE@%
  14941. value.  %@NL@%
  14942. %@NL@%
  14943. Typical declarations of character types are shown below:  %@NL@%
  14944. %@NL@%
  14945. %@AS@%  char answer; /* Declare a character variable answer */
  14946. %@AS@%  
  14947. %@AS@%  char alpha = 'a'; /* Declare character variable alpha
  14948. %@AS@%                       and initialize it */%@AE@%%@NL@%
  14949. %@NL@%
  14950. A character constant represents a single ASCII character. Typical character
  14951. constants are shown below:  %@NL@%
  14952. %@NL@%
  14953. %@AS@%  char alpha = 'a'; /* Declare and initialize */
  14954. %@AS@%  
  14955. %@AS@%  char c2 = 0x61; /* Declare and initialize with
  14956. %@AS@%                     hexadecimal value for 'a' */%@AE@%%@NL@%
  14957. %@NL@%
  14958. %@AB@%Escape Sequences%@AE@% - Escape sequences represent special characters, such as
  14959. the carriage return. An escape sequence consists of a backslash character
  14960. plus a letter or punctuation mark. Table A.2 lists the C escape sequences;
  14961. they are also listed in online help.%@NL@%
  14962. %@NL@%
  14963. %@AB@%Table A.2  %@AB@%C Escape Sequences%@AE@%%@AE@%
  14964.  
  14965. %@TH:  14   834 02 11 22 43 @%Character  Meaning               Hexadecimal Value%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%\a%@AE@%         Alert (bell)          0x07%@AB@%\n%@AE@%         New line (linefeed)   0x0A%@AB@%\b%@AE@%         Backspace             0x08%@AB@%\r%@AE@%         Carriage return       0x0D%@AB@%\f%@AE@%         Formfeed              0x0C%@AB@%\t%@AE@%         Tab                   0x09%@AB@%\v%@AE@%         Vertical tab          0x0B%@AB@%\\%@AE@%         Backslash             0x5C%@AB@%\%@AS@%'%@AE@%%@AE@%         Single quote          0x27%@AB@%\"%@AE@%         Double quote          0x22%@AB@%\0%@AE@%         Null                  0x00%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  14   834 02 11 22 43 @%
  14966.  
  14967. %@NL@%
  14968. %@4@%%@AB@%Integer Type%@AE@%%@EH@%%@NL@%
  14969. %@NL@%
  14970. The integer (%@AB@%int%@AE@%) type occupies two bytes of storage and can express a whole
  14971. number in the range -32,768 to 32,767. Unsigned integers (%@AB@%unsigned%@AE@% or
  14972. %@AB@%unsigned int%@AE@%) have a range of 0 to 65,535.  %@NL@%
  14973. %@NL@%
  14974. In QuickC, short integers (%@AB@%short%@AE@% or %@AB@%short int%@AE@%) are the same as integers
  14975. (%@AB@%int%@AE@%). Note that the %@AB@%short%@AE@% and %@AB@%int%@AE@% types are not the same in some operating
  14976. systems other than DOS.  %@NL@%
  14977. %@NL@%
  14978. Signed long integers (%@AB@%long%@AE@%) occupy four bytes and have a range of
  14979. -2,147,483,648 to 2,147,483,647. Unsigned long integers have a range of 0 to
  14980. 4,294,967,295.  %@NL@%
  14981. %@NL@%
  14982. Integer variables are declared with the keywords %@AB@%int%@AE@%, %@AB@%short%@AE@%, %@AB@%unsigned%@AE@%, or
  14983. %@AB@%long%@AE@%. Typical declarations of integer types are shown below:  %@NL@%
  14984. %@NL@%
  14985. %@AS@%  int z; /* Declare an int variable z */
  14986. %@AS@%  
  14987. %@AS@%  int ten = 10; /* Declare int variable and
  14988. %@AS@%                   assign it the value 10 */
  14989. %@AS@%  
  14990. %@AS@%  unsigned int a; /* Declare unsigned int variable */
  14991. %@AS@%  
  14992. %@AS@%  unsigned long BigInt = 2000000001UL; /* Declare and
  14993. %@AS@%                                          initialize */%@AE@%%@NL@%
  14994. %@NL@%
  14995. Integer constants are used to represent decimal, octal, and hexadecimal
  14996. numbers. There are three types of integer constants:  %@NL@%
  14997. %@NL@%
  14998. %@NL@%
  14999.   1.  Decimal constants can only contain the digits 0-9. The first digit
  15000.       must not be 0.%@NL@%
  15001. %@NL@%
  15002.   2.  Octal constants can only contain the digits 0-7. The first digit must
  15003.       be 0.%@NL@%
  15004. %@NL@%
  15005.   3.  Hexadecimal constants can only contain the digits 0-9, plus the
  15006.       letters a-f or A-F. The constant must begin with either 0x or 0X.%@NL@%
  15007. %@NL@%
  15008. %@NL@%
  15009. You can specify that an integer constant is long by adding the suffix %@AB@%l%@AE@% or
  15010. %@AB@%L%@AE@%. The suffix can be used with decimal, hexadecimal, or octal notation.  %@NL@%
  15011. %@NL@%
  15012. To specify that an integer constant is short, add the suffix %@AB@%u%@AE@% or %@AB@%U%@AE@%. This
  15013. suffix can also be used with decimal, hexadecimal, or octal notation.  %@NL@%
  15014. %@NL@%
  15015. Typical integer constants are shown below:  %@NL@%
  15016. %@NL@%
  15017. %@AS@%  42    /* Decimal constant */
  15018. %@AS@%  
  15019. %@AS@%  0x34  /* Hexadecimal constant */
  15020. %@AS@%  
  15021. %@AS@%  0x3cL /* Long hexadecimal constant */%@AE@%%@NL@%
  15022. %@NL@%
  15023. %@AS@%  052   /* Octal constant */%@AE@%%@NL@%
  15024. %@NL@%
  15025. %@NL@%
  15026. %@4@%%@AB@%Floating-Point Types%@AE@%%@EH@%%@NL@%
  15027. %@NL@%
  15028. You can declare floating-point variables using the keywords %@AB@%float%@AE@% or %@AB@%double%@AE@%.
  15029. The %@AB@%float%@AE@% type occupies four bytes of storage and can express a
  15030. floating-point value in the range 1.2E-38 to 3.4E+38. This type has
  15031. seven-digit precision.  %@NL@%
  15032. %@NL@%
  15033. The %@AB@%double%@AE@% type occupies eight bytes of storage and can express a
  15034. floating-point value in the range 2.2E-308 to 1.8E+308. This type has
  15035. fifteen-digit precision.  %@NL@%
  15036. %@NL@%
  15037. The %@AB@%long double%@AE@% type occupies ten bytes of storage and can express a
  15038. floating-point value in the range 3.4E-4932 to 1.2E+4932. This type has
  15039. nineteen-digit precision.  %@NL@%
  15040. %@NL@%
  15041. Typical declarations of floating-point types are shown below:  %@NL@%
  15042. %@NL@%
  15043. %@AS@%  float SmallPi = 3.14;  /* Declare floating-point variable */
  15044. %@AS@%  
  15045. %@AS@%  double AccuratePi = 3.141592653596  /* Declare
  15046. %@AS@%                                        double-precision */%@AE@%%@NL@%
  15047. %@NL@%
  15048. Floating-point constants can represent decimal numbers in either single or
  15049. double precision. A floating-point constant must either contain a decimal
  15050. point or end with the suffix %@AB@%e%@AE@% or %@AB@%E%@AE@%. Typical floating-point constants are
  15051. shown below:  %@NL@%
  15052. %@NL@%
  15053. %@AS@%  2.78   /* Floating-point constant */
  15054. %@AS@%  
  15055. %@AS@%  3E     /* Floating-point constant */%@AE@%%@NL@%
  15056. %@NL@%
  15057. %@NL@%
  15058. %@3@%%@CR:C6A-A0018   @%%@AB@%Aggregate Data Types%@AE@%%@EH@%%@NL@%
  15059. %@NL@%
  15060. Aggregate data types are built from one or more of the basic data types.
  15061. These include the following:  %@NL@%
  15062. %@NL@%
  15063. %@NL@%
  15064.   ■   Arrays (including strings)%@NL@%
  15065. %@NL@%
  15066.   ■   Structures%@NL@%
  15067. %@NL@%
  15068.   ■   Unions%@NL@%
  15069. %@NL@%
  15070. %@NL@%
  15071. %@NL@%
  15072. %@4@%%@AB@%Arrays and Strings%@AE@%%@EH@%%@NL@%
  15073. %@NL@%
  15074. An "array" is a collection of data elements of a single type. An array can
  15075. contain any data type. You can access an element of an array by using the
  15076. array name and a numeric subscript.  %@NL@%
  15077. %@NL@%
  15078. A "string" is an array of characters that terminates with the null character
  15079. (%@AB@%\0%@AE@%). Arrays that contain strings must allow space for the final null
  15080. character.  %@NL@%
  15081. %@NL@%
  15082. Typical arrays and strings are shown below:  %@NL@%
  15083. %@NL@%
  15084. %@AS@%  int id_number[10]; /* One-dimensional; 
  15085. %@AS@%                        10 elements; integer */
  15086. %@AS@%  
  15087. %@AS@%  char name[30]; /* String */
  15088. %@AS@%  
  15089. %@AS@%  float matrix[5][3]; /* Two-dimensional array, 
  15090. %@AS@%                         5 rows, 3 columns */
  15091. %@AS@%  
  15092. %@AS@%  char baby[30] = "Peter Roddy"; /* String initialization */%@AE@%%@NL@%
  15093. %@NL@%
  15094. %@NL@%
  15095. %@4@%%@AB@%Structures%@AE@%%@EH@%%@NL@%
  15096. %@NL@%
  15097. A "structure" is a collection of data items of different types. Once you
  15098. have defined a structure type, you can declare a structure variable using
  15099. that type.  %@NL@%
  15100. %@NL@%
  15101. The following example illustrates a simple structure:  %@NL@%
  15102. %@NL@%
  15103. %@AS@%  struct date
  15104. %@AS@%  {
  15105. %@AS@%    int month;
  15106. %@AS@%    int day;
  15107. %@AS@%    int year;
  15108. %@AS@%  }
  15109. %@AS@%  
  15110. %@AS@%  struct date today;%@AE@%%@NL@%
  15111. %@NL@%
  15112. The example defines a structure type named %@AS@% date %@AE@% and declares a structure
  15113. variable %@AS@% today %@AE@% to be of type %@AS@% date%@AE@%.  %@NL@%
  15114. %@NL@%
  15115. Use the structure-member operator ( . ) to access the "elements" (members)
  15116. of a structure. The name  %@NL@%
  15117. %@NL@%
  15118. %@AS@%  today.month%@AE@%%@NL@%
  15119. %@NL@%
  15120. refers to the %@AS@% month %@AE@% member of the %@AS@% today %@AE@% structure in the example.  %@NL@%
  15121. %@NL@%
  15122. %@NL@%
  15123. %@4@%%@AB@%Unions%@AE@%%@EH@%%@NL@%
  15124. %@NL@%
  15125. A "union" is a set of data items of different types sharing the same storage
  15126. space in memory. One use of unions is accessing the computer's DOS
  15127. registers. For instance, QuickC defines the union %@AB@%REGS%@AE@% as the following:  %@NL@%
  15128. %@NL@%
  15129. %@AS@%  union REGS 
  15130. %@AS@%  {
  15131. %@AS@%    struct WORDREGS x;
  15132. %@AS@%    struct BYTEREGS h;
  15133. %@AS@%  };%@AE@%%@NL@%
  15134. %@NL@%
  15135. %@NL@%
  15136. %@2@%%@CR:C6A-A0019   @%%@AB@%Advanced Data Types%@AE@%%@EH@%%@NL@%
  15137. %@NL@%
  15138. Advanced data topics are explained in Chapter 5, "Advanced Data Types." A
  15139. brief description of each topic is given here.  %@NL@%
  15140. %@NL@%
  15141. %@NL@%
  15142. %@3@%%@CR:C6A-A0020   @%%@AB@%Visibility%@AE@%%@EH@%%@NL@%
  15143. %@NL@%
  15144. Variables declared outside all functions are global and can be accessed
  15145. anywhere in the current source file. Variables declared inside a function
  15146. are local and can be accessed only in that function. Use the %@AB@%extern%@AE@% keyword
  15147. to make a variable declared in another source file visible in the current
  15148. source file.  %@NL@%
  15149. %@NL@%
  15150. %@NL@%
  15151. %@3@%%@CR:C6A-A0021   @%%@AB@%Lifetime%@AE@%%@EH@%%@NL@%
  15152. %@NL@%
  15153. Global variables, and local variables declared with the %@AB@%static%@AE@% keyword,
  15154. exist for the lifetime of the program. Other local variables are
  15155. "automatic;" they come into being when the function starts and evaporate
  15156. when it ends.  %@NL@%
  15157. %@NL@%
  15158. %@NL@%
  15159. %@3@%%@CR:C6A-A0022   @%%@AB@%Type Conversions%@AE@%%@EH@%%@NL@%
  15160. %@NL@%
  15161. A type conversion occurs automatically when an expression mixes two
  15162. different data types. QuickC converts the lower-ranking type to the
  15163. higher-ranking type before it performs the specified operation.  %@NL@%
  15164. %@NL@%
  15165. You can also "cast" (manually convert) a value to any type by placing the
  15166. desired type name in parentheses in front of the value. The example below
  15167. casts the value of %@AS@% sample %@AE@% to type %@AB@%float%@AE@% and assigns the value to %@AS@% x%@AE@%:  %@NL@%
  15168. %@NL@%
  15169. %@AS@%  int sample; 
  15170. %@AS@%  float x; 
  15171. %@AS@%  x = (float)sample;%@AE@%%@NL@%
  15172. %@NL@%
  15173. %@NL@%
  15174. %@3@%%@CR:C6A-A0023   @%%@AB@%User-Defined Types%@AE@%%@EH@%%@NL@%
  15175. %@NL@%
  15176. The %@AB@%typedef%@AE@% keyword allows you to create user-defined types, which are
  15177. synonyms for existing data types. User-defined types can make your program
  15178. more readable. For example, a type called %@AS@% string %@AE@% may be easier to
  15179. understand than a type called %@AB@%char *%@AE@%.  %@NL@%
  15180. %@NL@%
  15181. A simple %@AB@%typedef%@AE@% declaration is shown below. The name of an existing type
  15182. (%@AB@%long int%@AE@%) is followed by the synonym %@AS@% income%@AE@%.  %@NL@%
  15183. %@NL@%
  15184. %@AS@%  typedef long int income;%@AE@%%@NL@%
  15185. %@NL@%
  15186. Once you have created a new type name, you can use it wherever the original
  15187. type name could be used:  %@NL@%
  15188. %@NL@%
  15189. %@AS@%  income net_income, gross_income;%@AE@%%@NL@%
  15190. %@NL@%
  15191. In the example above, the variables %@AS@% net_income %@AE@% and %@AS@% gross_income %@AE@% are of
  15192. type %@AS@% income%@AE@%, which is the same as %@AB@%long int%@AE@%.  %@NL@%
  15193. %@NL@%
  15194. %@NL@%
  15195. %@3@%%@CR:C6A-A0024   @%%@AB@%Enumerated Types%@AE@%%@EH@%%@NL@%
  15196. %@NL@%
  15197. An enumerated type (declared with %@AB@%enum%@AE@%) has values limited to a specified
  15198. set. If the %@AB@%enum%@AE@% declaration does not specify any values, QuickC assigns
  15199. sequential integers to the enumeration identifiers beginning at zero.  %@NL@%
  15200. %@NL@%
  15201. The example below assigns the values of 0, 1, and 2 to the enumeration
  15202. identifiers %@AS@% zero%@AE@%, %@AS@% one%@AE@%, and %@AS@% two%@AE@%, respectively. It also creates an
  15203. enumerated type %@AS@% small_numbers %@AE@% that can be used to declare other variables.
  15204. %@NL@%
  15205. %@NL@%
  15206. %@AS@%  /* Enumerated data type */
  15207. %@AS@%  enum small_numbers {zero, one, two};
  15208. %@AS@%  
  15209. %@AS@%  /* Variable my_numbers is of type small_numbers */
  15210. %@AS@%  enum small_numbers  my_numbers;%@AE@%%@NL@%
  15211. %@NL@%
  15212. The following example explicitly assigns values to the enumeration
  15213. identifiers:  %@NL@%
  15214. %@NL@%
  15215. %@AS@%  /* Enumerated data type */
  15216. %@AS@%  enum even_numbers { two = 2, four = 4, six = 6 };%@AE@%%@NL@%
  15217. %@NL@%
  15218. %@NL@%
  15219. %@2@%%@CR:C6A-A0025   @%%@AB@%Operators%@AE@%%@EH@%%@NL@%
  15220. %@NL@%
  15221. C-language operators are explained in Chapter 6, "Operators."  %@NL@%
  15222. %@NL@%
  15223. An "operand" is a constant or variable manipulated by an operator in an
  15224. expression. An "operator" specifies how the operands in an expression are to
  15225. be evaluated. Operators also produce a result that can be nested within a
  15226. larger expression.  %@NL@%
  15227. %@NL@%
  15228. C provides a rich set of operators covering everything from basic arithmetic
  15229. operations to logical and bitwise operations. You can also combine the
  15230. assignment operator (%@AB@%=%@AE@%) with any arithmetic or bitwise operator to form a
  15231. combined assignment operator.  %@NL@%
  15232. %@NL@%
  15233. C operators have two properties, precedence and associativity. You can
  15234. change the normal order of evaluation by enclosing expressions in
  15235. parentheses.  %@NL@%
  15236. %@NL@%
  15237. Table A.3 lists the C operators and their precedence and associativity
  15238. values. The lines in the table separate precedence levels. The highest
  15239. precedence level is at the top of the table.  %@NL@%
  15240. %@NL@%
  15241. %@AB@%Table A.3  %@AB@%C Operators%@AE@%%@AE@%
  15242.  
  15243. %@TH: 114  4500 02 24 37 15 @%Symbol                  Name or Meaning                      Associativity%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%( )                     Function call                        Left to right[ ]                     Array element %@AB@%.%@AE@%                       Structure or union                         member %@AB@%->%@AE@%                      Pointer to structure                         member %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%- -%@AE@%                     Decrement                            Right to left%@AB@%++%@AE@%                      Increment %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%:>                      Base operator                        Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%!%@AE@%                       Logical NOT                          Right to left%@AB@%~ %@AE@%                      One's complement%@AB@% %@AE@%%@AB@%-%@AE@%                       Unary minus %@AB@%+%@AE@%                       Unary plus %@AB@%&%@AE@%                       Address %@AB@%*%@AE@%                       Indirection %@AB@%sizeof%@AE@%                  Size in bytes %@AB@%(%@AE@%type%@AB@%)%@AE@%                  Type cast [for example, (%@AB@%float%@AE@%) i] %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%*%@AE@%                       Multiply                             Left to right%@AB@%/%@AE@%                       Divide %@AB@%%%@AE@%                       Modulus (remainder)%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%+%@AE@%                       Add                                  Left to right%@AB@%-%@AE@%                       Subtract %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%<<%@AE@%                      Left shift                           Left to right%@AB@%>>%@AE@%                      Right shift %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%<%@AE@%                       Less than                            Left to right%@AB@%<=%@AE@%                      Less than or equal to %@AB@%>%@AE@%                       Greater than %@AB@%>=%@AE@%                      Greater than or equal to %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%==%@AE@%                      Equal                                Left to right%@AB@%!=%@AE@%                      Not equal %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%&%@AE@%                       Bitwise AND                          Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%^%@AE@%                       Bitwise exclusive OR                 Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%|%@AE@%                       Bitwise OR                           Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%&&%@AE@%                      Logical AND                          Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%||%@AE@%                      Logical OR                           Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%? : %@AE@%                    Conditional                          Right to left%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%=%@AE@%                       Assignment                           Right to left%@AB@%*=, /=, %=, +=, -=%@AE@%      Compound assignment%@AB@%<<=, >>=,  &=, ^=, |=%@AE@%   %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%,%@AE@%                       Comma                                Left to right%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE: 114  4500 02 24 37 15 @%
  15244.  
  15245. %@NL@%
  15246. %@2@%%@CR:C6A-A0026   @%%@AB@%Preprocessor Directives%@AE@%%@EH@%%@NL@%
  15247. %@NL@%
  15248. Preprocessor directives are explained in Chapter 7, "Preprocessor
  15249. Directives."  %@NL@%
  15250. %@NL@%
  15251. A "preprocessor directive" is a command to the QuickC compiler, which
  15252. processes all such commands before it compiles your source program. A
  15253. preprocessor directive begins with the %@AB@%#%@AE@% symbol, followed by the directive
  15254. and any arguments the directive needs. Since a preprocessor directive is not
  15255. a C language statement, it doesn't end in a semicolon.  %@NL@%
  15256. %@NL@%
  15257. The two most commonly used directives are %@AB@%#define%@AE@% and %@AB@%#include%@AE@%. Use the
  15258. %@AB@%#define%@AE@% directive to give a meaningful name to some constant in your
  15259. program. The following directive tells QuickC to replace %@AS@% PI %@AE@% with %@AS@% 3.14159 %@AE@%
  15260. everywhere in the source program:  %@NL@%
  15261. %@NL@%
  15262. %@AS@%  #define PI  3.14159%@AE@%%@NL@%
  15263. %@NL@%
  15264. The %@AB@%#include%@AE@% directive below tells QuickC to insert the contents of a
  15265. specified file at the current location in your source program.  %@NL@%
  15266. %@NL@%
  15267. %@AS@%  #include <stdio.h> /* Standard header file */%@AE@%%@NL@%
  15268. %@NL@%
  15269. Such files are called "include files" or "header files." Standard header
  15270. files, such as STDIO.H, end with the .H extension and contain function
  15271. prototypes and other definitions needed for QuickC library routines.  %@NL@%
  15272. %@NL@%
  15273. Table A.4 lists and describes the QuickC standard header files. Consult
  15274. online help for information on the header files needed by individual library
  15275. functions.  %@NL@%
  15276. %@NL@%
  15277. %@AB@%Table A.4  %@AB@%QuickC Header Files%@AE@%%@AE@%
  15278.  
  15279. %@TH:  73  2606 02 34 42 @%File Name                         Major Contents%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%ASSERT.H                          %@AB@%assert%@AE@% debugging macroBIOS.H                            BIOS service functionsCONIO.H                           Console and port I/O routinesCTYPE.H                           Character classificationDIRECT.H                          Directory controlDOS.H                             MS-DOS interface functionsERRNO.H                           System-wide error numbersFCNTL.H                           Flags used in %@AB@%open%@AE@% and %@AB@%sopen%@AE@% functionsFLOAT.H                           Constants needed by math functionsGRAPH.H                           Low-level graphics and font routinesIO.H                              File-handling and low-level I/OLIMITS.H                          Ranges of integers and character typesLOCALE.H                          Internationalization functionsMALLOC.H                          Memory-allocation functionsMATH.H                            Floating-point math routinesMEMORY.H                          Buffer-manipulation routinesPGCHART.H                         Presentation graphicsPROCESS.H                         Process-control routinesSEARCH.H                          Searching and sorting functionsSETJMP.H                          %@AB@%setjmp%@AE@% and %@AB@%longjmp%@AE@% functionsSHARE.H                           Flags used in %@AB@%sopen%@AE@%SIGNAL.H                          Constants used by signal functionSTDARG.H                          Macros used to access variable-length                                   argument-list functionsSTDDEF.H                          Commonly used data types and valuesSTDIO.H                           Standard I/O header fileSTDLIB.H                          Commonly used library functionsSTRING.H                          String-manipulation functionsTIME.H                            General time functionsVARARGS.H                         Variable-length argument-list functionsSYS\LOCKING.H                     Flags used by %@AB@%locking%@AE@% functionSYS\STAT.H                        File-status structures and functionsSYS\TIMEB.H                       %@AB@%time%@AE@% functionSYS\TYPES.H                       File-status and time typesSYS\UTIME.H                       %@AB@%utime%@AE@% function%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE:  73  2606 02 34 42 @%
  15280.  
  15281. %@NL@%
  15282. %@2@%%@CR:C6A-A0027   @%%@AB@%Pointers%@AE@%%@EH@%%@NL@%
  15283. %@NL@%
  15284. Pointers are described in Chapter 8, "Pointers," and Chapter 9, "Advanced
  15285. Pointers."  %@NL@%
  15286. %@NL@%
  15287. A "pointer" is a variable that contains the memory address of an item rather
  15288. than its value. A pointer can point to any type of data item or to a
  15289. function. The following code illustrates pointer declarations:  %@NL@%
  15290. %@NL@%
  15291. %@AS@%  int *intptr; /* Pointer to an integer */
  15292. %@AS@%  char *name; /* Pointer to char */%@AE@%%@NL@%
  15293. %@NL@%
  15294. The following operators are used with pointers:  %@NL@%
  15295. %@NL@%
  15296. %@NL@%
  15297.   ■   The indirection operator (%@AB@%*%@AE@%) has two uses. In a declaration, it means
  15298.       that the declared item is a pointer. In an expression, it denotes the
  15299.       data being pointed to.%@NL@%
  15300. %@NL@%
  15301.   ■   The address-of operator (%@AB@%&%@AE@%) yields the memory address at which an item
  15302.       is stored.%@NL@%
  15303. %@NL@%
  15304. %@NL@%
  15305. You can perform four arithmetic operations on pointers:  %@NL@%
  15306. %@NL@%
  15307. %@NL@%
  15308.   1.  Adding a pointer and an integer%@NL@%
  15309. %@NL@%
  15310.   2.  Subtracting an integer from a pointer%@NL@%
  15311. %@NL@%
  15312.   3.  Subtracting two pointers%@NL@%
  15313. %@NL@%
  15314.   4.  Comparing two pointers%@NL@%
  15315. %@NL@%
  15316. %@NL@%
  15317. Pointer arithmetic operations are automatically scaled by the size of the
  15318. object pointed to. For instance, adding 1 to a pointer to a %@AB@%float%@AE@% item
  15319. causes the address stored in the pointer to be incremented four bytes, the
  15320. size of one %@AB@%float%@AE@% item.  %@NL@%
  15321. %@NL@%
  15322. QuickC 2.5 also supports based pointers, a highly advanced feature, that are
  15323. compatible with Microsoft C version 6.0. Please refer to your C 6.0
  15324. documentation for more information about based pointers and objects.  %@NL@%
  15325. %@NL@%
  15326. %@NL@%
  15327. %@NL@%
  15328. %@NL@%
  15329. %@NL@%
  15330. %@NL@%
  15331. %@CR:C6A-B0001   @%%@1@%%@AB@%Appendix B  C Library Guide%@AE@%%@EH@%%@NL@%
  15332. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  15333. %@NL@%
  15334. This appendix outlines the features of the C run-time library provided with
  15335. QuickC. It does not intend to be a complete presentation of the complete C
  15336. run-time library. Instead, this appendix presents the most fundamental
  15337. routines, grouped by category so you can begin experimenting with C and with
  15338. QuickC.  %@NL@%
  15339. %@NL@%
  15340. Remember, use online help to get instant help on any topic of interest. The
  15341. online help system provided with QuickC provides complete reference
  15342. information for all C library functions, keywords, and preprocessor
  15343. directives.  %@NL@%
  15344. %@NL@%
  15345. %@NL@%
  15346. %@2@%%@CR:C6A-B0002   @%%@AB@%Overview of the C Run-Time Library%@AE@%%@EH@%%@NL@%
  15347. %@NL@%
  15348. At last count, the C run-time library contained over 400 functions to use in
  15349. C programs. This appendix describes the major categories of functions
  15350. included in the library and, within those categories, the fundamental
  15351. routines every C programmer should know.  %@NL@%
  15352. %@NL@%
  15353. The discussions of these categories give only a brief overview of the
  15354. capabilities of the run-time library. You can find a complete description of
  15355. the syntax and use of each routine in online help.  %@NL@%
  15356. %@NL@%
  15357. The routines in the C run-time library are divided into the following
  15358. categories:  %@NL@%
  15359. %@NL@%
  15360. %@AB@%Table B.1  %@AB@%C Run-Time Library Routines%@AE@%%@AE@%
  15361.  
  15362. %@TH: 131  7080 02 32 35 09 @%Category                        Function Routines                  Page%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Buffer Manipulation%@AE@%             memchr, memcmp, memcpy, memmove,   345                                memset                             %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Character Classification%@AE@%        isalnum, isalpha, isascii,         346%@AB@%and Conversion%@AE@%                  iscntrl, isdigit, isgraph,                                         islower, isprint, ispunct,                                         isspace, isupper, isxdigit,                                        tolower, toupper                   %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Data Conversion%@AE@%                 atof, atoi, atol, itoa, ltoa,      348                                ultoa, strtod, strtol, strtoul     %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Error Message%@AE@%                   assert, perror, strerror,          349                                _strerror                          %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Graphics 1: Low-Level%@AE@%                                              350%@AB@%                     Graphics%@AE@%                                      Configure Mode and              _displaycursor, _getvideoconfig,   351   Environment                  _setvideomode                      Set Coordinates                 _getcurrentposition,               352                                _getphyscoord,_getviewcoord,                                       _getwindowcoord,                                                   _setcliprgn,_setvieworg,                                           _setviewport, _setwindow           Set Palette                     _remapallpalette, remappallette,   354                                _selectpalette                     Set Attributes                  _getbkcolor, _getcolor,            355                                _setbkcolor, _setcolor             Output Images                   _arc, _clearscreen, _ellipse,      356                                _floodfill, _getpixel,                                             _lineto, _moveto, _pie,                                            _rectangle, _setpixel              Output Text                     _displaycursor, _gettextcolor,     359                                _gettextcursor,                                                    _gettextposition, _outtext,                                        _settextposition,                                                  _settextcolor, _settextwindow      Transfer Images                 _getimage, _imagesize, _putimage   361%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Graphics 2: Presentation%@AE@%        _pg_chart, _pg_chartms,            362%@AB@%                     Graphics%@AE@%   _pg_chartpie, _pg_chartscatter,                                    _pg_chartscatterms,                                                _pg_defaultchart, _pg_initchart    %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Graphics 3: Font Display%@AE@%        _getfontinfo, _getgtextextent,     365                                _outgtext, _registerfonts,                                         _setfont, _unregisterfonts         %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Input and Output%@AE@%                                                   367Stream Routines                 clearerr, fclose, feof, ferror,    367                                fflush, fgetc, fgetpos, fgets,                                     fopen, fprintf, fputc, fputs,                                      fread, freopen, fscanf, fseek,                                     fsetpos, ftell, fwrite, getc,                                      getchar, gets, printf, putc,                                       putchar, puts, rewind, scanf,                                      sprintf, sscanf, tmpfile, tmpnam,                                  ungetc                             Low-Level Routines              close, creat, eof, lseek, open,    373                                read, tell, write                  Console and Port                cgets, cprintf, cputs, cscanf,     375   I/O Routines                 getch, getche, kbhit,                                              putch, ungetch                     %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Math%@AE@%                            abs, fabs, labs, acos, asin, atan,  377                                atan2, ceil, cos, cosh,                                            exp, floor, fmod, frexp, ldexp,                                    log, log10, modf, pow, rand,                                       srand, sin, sinh, sqrt, tan, tanh  %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Memory Allocation%@AE@%               calloc, free, _ffree, hfree,       381                                _nfree, malloc, _fmalloc,                                          _nmalloc, realloc                  %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Process Control%@AE@%                 abort, atexit, exit, _exit,        383                                system                             %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Searching and Sorting%@AE@%           bsearch, lfind, lsearch, qsort     384%@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%String Manipulation%@AE@%             strcat, strcpy, strdup, strncat,   385                                strncpy, strchr,                                                   strcspn, strpbrk, strrchr, strspn,                                  strstr, strcmp,                                                    strcmpi, stricmp, strncmp,                                         strnicmp, strlen, strlwr,                                          strupr, strnset, strset, strtok    %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@AB@%Time%@AE@%                            asctime, clock, ctime, difftime,   389                                ftime, gmtime,                                                     mktime, time                       %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@TE: 131  7080 02 32 35 09 @%
  15363.  
  15364. %@NL@%
  15365. %@2@%%@CR:C6A-B0003   @%%@AB@%Buffer-Manipulation Routines%@AE@%%@EH@%%@NL@%
  15366. %@NL@%
  15367. Buffer manipulation routines are used with areas of memory on a
  15368. character-by-character basis. Buffers are arrays of characters (bytes).
  15369. Unlike strings, however, they are not usually terminated with a null
  15370. character (%@AB@%\0%@AE@%).  %@NL@%
  15371. %@NL@%
  15372. %@AB@%memchr%@AE@% - Returns a pointer to the first occurrence, within a specified
  15373. number of characters, of a given character in the buffer.%@NL@%
  15374. %@NL@%
  15375. %@AB@%Include%@AE@%                           STRING.H
  15376.  
  15377. %@AB@%Prototype%@AE@%                         %@AB@%void *memchr( const void *%@AE@%%@AI@%buf%@AE@%%@AB@%,  int%@AE@% %@AI@%c%@AE@%%@AB@%, %@AE@%
  15378.                                   %@AB@%size_t%@AE@% %@AI@%count %@AE@%%@AB@%);%@AE@%
  15379.  
  15380. %@AB@%Arguments%@AE@%                         %@AI@%buf%@AE@%         Pointer to buffer
  15381.  
  15382.                                   %@AI@%c%@AE@%           Character to copy
  15383.  
  15384.                                   %@AI@%count%@AE@%       Number of characters
  15385.  
  15386. %@AB@%Returns%@AE@%                           A pointer to the location of %@AI@%c%@AE@% in %@AI@%buf%@AE@%  
  15387.                                   if successful;
  15388.  
  15389.                                   %@AB@%NULL%@AE@% if %@AI@%c%@AE@% is not within first %@AI@%count%@AE@% 
  15390.                                   bytes  of %@AI@%buf%@AE@%
  15391.  
  15392. ────────────────────────────────────────────────────────────────────────────
  15393. %@AB@%memcmp%@AE@% - Compares a specified number of characters from two buffers.%@NL@%
  15394. %@NL@%
  15395. %@AB@%Include%@AE@%                           STRING.H
  15396.  
  15397. %@AB@%Prototype%@AE@%                         %@AB@%int memcmp( const void *%@AE@%%@AI@%buf1%@AE@%%@AB@%,%@AE@%  %@AB@%const %@AE@%
  15398.                                   %@AB@%void%@AE@% *%@AI@%buf2%@AE@%%@AB@%, size_t %@AE@%%@AI@%count %@AE@%%@AB@%);%@AE@%
  15399.  
  15400. %@AB@%Arguments%@AE@%                         %@AI@%buf1%@AE@%        First buffer
  15401.  
  15402.                                   %@AI@%buf2%@AE@%        Second buffer
  15403.  
  15404.                                   %@AI@%count%@AE@%       Number of characters
  15405.  
  15406. %@AB@%Returns%@AE@%                           A negative value if %@AI@%buf1%@AE@% < %@AI@%buf2%@AE@%,  0 if %@AI@%%@AE@%
  15407.                                   %@AI@%buf1%@AE@% = %@AI@%buf2%@AE@%,
  15408.  
  15409.                                   a positive value if %@AI@%buf1%@AE@% > %@AI@%buf2%@AE@%
  15410.  
  15411. ────────────────────────────────────────────────────────────────────────────
  15412. %@AB@%memcpy%@AE@% - Copies a specified number of characters from one buffer to another.%@NL@%
  15413. %@NL@%
  15414. %@AB@%Include%@AE@%                           STRING.H
  15415.  
  15416. %@AB@%Prototype%@AE@%                         %@AB@%void%@AE@% %@AB@%*memcpy( void *%@AE@%%@AI@%dest%@AE@%%@AB@%,  const void *%@AE@%%@AI@%%@AE@%
  15417.                                   %@AI@%src%@AE@%%@AB@%, size_t %@AE@%%@AI@%count %@AE@%%@AB@%);%@AE@%
  15418.  
  15419. %@AB@%Arguments%@AE@%                         %@AI@%dest%@AE@%        New buffer
  15420.  
  15421.                                   %@AI@%src%@AE@%         Buffer to copy from
  15422.  
  15423.                                   %@AI@%count%@AE@%       Number of characters to copy
  15424.  
  15425. %@AB@%Returns%@AE@%                           A pointer to %@AI@%dest%@AE@%
  15426.  
  15427. ────────────────────────────────────────────────────────────────────────────
  15428. %@AB@%memmove%@AE@% - Copies a specified number of characters from one buffer to
  15429. another. When the source and target areas overlap, the %@AB@%memmove%@AE@% function is
  15430. guaranteed to properly copy the full source. %@NL@%
  15431. %@NL@%
  15432. %@AB@%Include%@AE@%                           STRING.H
  15433.  
  15434. %@AB@%Prototype%@AE@%                         %@AB@%void *memmove( void *%@AE@%%@AI@%dest%@AE@%%@AB@%,  const void *%@AE@%
  15435.                                   %@AI@%src%@AE@%%@AB@%, size_t %@AE@%%@AI@%count %@AE@%%@AB@%);%@AE@%
  15436.  
  15437. %@AB@%Arguments%@AE@%                         %@AI@%dest%@AE@%        Target object
  15438.  
  15439.                                   %@AI@%src%@AE@%         Source object
  15440.  
  15441.                                   %@AI@%count%@AE@%       Number of characters to copy
  15442.  
  15443. %@AB@%Returns%@AE@%                           The value of %@AI@%dest%@AE@%
  15444.  
  15445. ────────────────────────────────────────────────────────────────────────────
  15446. %@AB@%memset%@AE@% - Uses a given character to initialize a specified number of bytes in
  15447. the buffer.%@NL@%
  15448. %@NL@%
  15449. %@AB@%Include%@AE@%                           STRING.H
  15450.  
  15451. %@AB@%Prototype%@AE@%                         %@AB@%void *memset( void *%@AE@%%@AI@%dest%@AE@%%@AB@%,  int %@AE@%%@AI@%c%@AE@%%@AB@%, size_t%@AE@%
  15452.                                   %@AB@%%@AE@%%@AI@%count %@AE@%%@AB@%);%@AE@%
  15453.  
  15454. %@AB@%Arguments%@AE@%                         %@AI@%dest%@AE@%        Pointer to destination
  15455.  
  15456.                                   %@AI@%c%@AE@%           Character to set
  15457.  
  15458.                                   %@AI@%count%@AE@%       Number of characters
  15459.  
  15460. %@AB@%Returns%@AE@%                           A pointer to%@AI@% dest%@AE@%
  15461.  
  15462. %@NL@%
  15463. %@2@%%@CR:C6A-B0004   @%%@AB@%Character Classification and Conversion Routines%@AE@%%@EH@%%@NL@%
  15464. %@NL@%
  15465. The classification routines (%@AB@%is...%@AE@%) test a character and return a one (1) if
  15466. the character is in the set that the routine is testing for. The conversion
  15467. routines (%@AB@%to...%@AE@%) convert characters between uppercase and lowercase. These
  15468. routines are generally faster than writing a test expression such as the
  15469. following:  %@NL@%
  15470. %@NL@%
  15471. %@AS@%  if ((c >= 0) || c <= 0x7f))%@AE@%%@NL@%
  15472. %@NL@%
  15473. ────────────────────────────────────────────────────────────────────────────
  15474. %@AB@%isalnum, isalpha, isascii, iscntrl, isdigit, isgraph, islower, isprint,
  15475. %@AB@%ispunct, isspace, isupper, isxdigit%@AE@% These routines test a character for a
  15476. specified condition and return a nonzero value if the condition is true. - %@NL@%
  15477. %@NL@%
  15478. %@AB@%Include%@AE@%                           CTYPE.H
  15479.  
  15480. %@AB@%Prototypes%@AE@%                        %@AB@%int isalnum( int %@AE@%%@AI@%c %@AE@%%@AB@%);%@AE@%  (alphanumeric  
  15481.                                   character: A-Z, a-z, 0-9)
  15482.  
  15483.  
  15484.  
  15485. %@AB@%%@AE@%                                  int isalpha( int %@AI@%c %@AE@%%@AB@%);%@AE@%   (alphabetic 
  15486.                                   character: A-Z%@AB@%,%@AE@%  a-z)%@AB@%%@AE@%
  15487.  
  15488.  
  15489.  
  15490.                                   int isascii( int %@AI@%c %@AE@%%@AB@%);%@AE@%     (ASCII 
  15491.                                   character: 0x00-0x7F)%@AB@%%@AE@%
  15492.  
  15493.  
  15494.  
  15495.                                   int iscntrl( int %@AI@%c%@AE@%%@AB@% );%@AE@%    (control 
  15496.                                   character: 0x00-0x1F,  0x7F)%@AB@%%@AE@%
  15497.  
  15498.  
  15499.  
  15500.                                   int isdigit( int %@AI@%c %@AE@%%@AB@%);    %@AE@% (decimal 
  15501.                                   digit: 0-9)
  15502.  
  15503. %@AB@%%@AE@%
  15504.  
  15505.                                   int isgraph( int %@AI@%c%@AE@%%@AB@% );%@AE@%   (printable 
  15506.                                   character, not space:  0x21-0x7E)
  15507.  
  15508. %@AB@%%@AE@%
  15509.  
  15510.                                   int islower( int %@AI@%c %@AE@%%@AB@%); %@AE@%  (lowercase 
  15511.                                   letter: a-z)%@AB@%%@AE@%
  15512.  
  15513.  
  15514.  
  15515.                                   int isprint( int %@AI@%c %@AE@%%@AB@%);%@AE@%    (printable 
  15516.                                   character: 0x20-0x7E)%@AB@%%@AE@%
  15517.  
  15518.  
  15519.  
  15520.                                   int ispunct( int %@AI@%c %@AE@%%@AB@%);%@AE@%   (punctuation 
  15521.                                   character)%@AB@%%@AE@%
  15522.  
  15523.  
  15524.  
  15525.                                   int isspace( int %@AI@%c %@AE@%%@AB@%);%@AE@%    (white-space 
  15526.                                   character:  0x09-0x0D, 0x20)
  15527.  
  15528.  
  15529.  
  15530.                                   %@AB@%int isupper( int %@AE@%%@AI@%c %@AE@%%@AB@%);%@AE@%   (uppercase 
  15531.                                   letter: A-Z)%@AB@%%@AE@%
  15532.  
  15533.  
  15534.  
  15535.                                   int isxdigit( int %@AI@%c %@AE@%%@AB@%);%@AE@%   (hexadecimal 
  15536.                                   digit: A-F,  a-f, 0-9)
  15537.  
  15538. %@AB@%Argument%@AE@%                          %@AI@%c%@AE@%           Character to be tested
  15539.  
  15540. %@AB@%Returns%@AE@%                           A nonzero value if the condition is true
  15541.  
  15542. ────────────────────────────────────────────────────────────────────────────
  15543. %@AB@%tolower, toupper%@AE@% - These routines accept a character argument and return a
  15544. converted character. The %@AB@%tolower%@AE@% and %@AB@%toupper%@AE@% routines are also implemented
  15545. as functions. To use the function versions, you must do the following:%@NL@%
  15546. %@NL@%
  15547. %@NL@%
  15548.   ■   Include CTYPE.H if necessary for other macro definitions%@NL@%
  15549. %@NL@%
  15550.   ■   If CTYPE.H is included, give %@AB@%#undef%@AE@% directives for %@AB@%tolower%@AE@% and %@AB@%toupper%@AE@%%@NL@%
  15551. %@NL@%
  15552.   ■   Include STDLIB.H (which contains the function prototypes)%@NL@%
  15553. %@NL@%
  15554. %@NL@%
  15555. %@AB@%Include%@AE@%                           CTYPE.H
  15556.  
  15557. %@AB@%Prototypes%@AE@%                        %@AB@%int tolower( int %@AE@%%@AI@%c %@AE@%%@AB@%);%@AE@%
  15558.  
  15559.  
  15560.  
  15561.                                   int toupper( int %@AI@%c %@AE@%%@AB@%);%@AE@%
  15562.  
  15563. %@AB@%Argument%@AE@%                          %@AI@%c%@AE@%           Character to be converted
  15564.  
  15565. %@AB@%Returns%@AE@%                           tolower: the lowercase equivalent of %@AI@%c%@AE@%  
  15566.                                   only if %@AI@%c%@AE@% is an uppercase letter
  15567.  
  15568.                                   %@AB@%toupper%@AE@%: the uppercase equivalent of %@AI@%c%@AE@% 
  15569.                                   only if %@AI@%c%@AE@%  is a lowercase letter
  15570.  
  15571. %@NL@%
  15572. %@2@%%@CR:C6A-B0005   @%%@AB@%Data Conversion Routines%@AE@%%@EH@%%@NL@%
  15573. %@NL@%
  15574. The data conversion routines convert numbers to strings of ASCII characters
  15575. and vice versa.  %@NL@%
  15576. %@NL@%
  15577. ────────────────────────────────────────────────────────────────────────────
  15578. %@AB@%atof, atoi, atol%@AE@% - These ASCII-to-number routines convert an ASCII string to
  15579. a %@AB@%float%@AE@%, an %@AB@%integer%@AE@%, and a %@AB@%long%@AE@%, respectively.%@NL@%
  15580. %@NL@%
  15581. %@AB@%Include%@AE@%                           STDLIB.H or MATH.H (%@AB@%atof%@AE@%)
  15582.  
  15583.                                   STDLIB.H (%@AB@%atoi%@AE@%, %@AB@%atol%@AE@%)
  15584.  
  15585. %@AB@%Prototypes%@AE@%                        %@AB@%double atof( const char *%@AE@%%@AI@%string%@AE@%%@AB@%  );%@AE@%
  15586.  
  15587.  
  15588.  
  15589.                                   %@AB@%int atoi( const char%@AE@% %@AB@%*%@AE@%%@AI@%string %@AE@%%@AB@%);%@AE@%
  15590.  
  15591.  
  15592.  
  15593.                                   long atol( const char *%@AI@%string %@AE@%%@AB@%);%@AE@%
  15594.  
  15595. %@AB@%Argument%@AE@%                          %@AI@%string%@AE@%      String to be converted
  15596.  
  15597. %@AB@%Returns%@AE@%                           The converted string, or 0 if %@AI@%string%@AE@% 
  15598.                                   cannot  be converted
  15599.  
  15600. ────────────────────────────────────────────────────────────────────────────
  15601. %@AB@%itoa, ltoa, ultoa%@AE@% - These number-to-ASCII routines convert an integer, a
  15602. long value, or an unsigned long value to an ASCII string.%@NL@%
  15603. %@NL@%
  15604. %@AB@%Include%@AE@%                           STDLIB.H
  15605.  
  15606. %@AB@%Prototypes%@AE@%                        %@AB@%char * itoa( int %@AE@%%@AI@%value%@AE@%%@AB@%, char  *%@AE@%%@AI@%string%@AE@%%@AB@%, %@AE@%
  15607.                                   %@AB@%int %@AE@%%@AI@%radix %@AE@%%@AB@%);%@AE@%
  15608.  
  15609.  
  15610.  
  15611.                                   char * ltoa( long %@AI@%value%@AE@%%@AB@%, char *%@AE@%%@AI@%string%@AE@%%@AB@%,  %@AE@%
  15612.                                   %@AB@%int %@AE@%%@AI@%radix %@AE@%%@AB@%);%@AE@%
  15613.  
  15614.  
  15615.  
  15616.                                   char * ultoa( unsigned long %@AI@%value%@AE@%%@AB@%, char %@AE@%
  15617.                                   %@AB@%*%@AE@%%@AI@%string%@AE@%%@AB@%,  int %@AE@%%@AI@%radix%@AE@%%@AB@% );%@AE@%
  15618.  
  15619. %@AB@%Arguments%@AE@%                         %@AI@%value%@AE@%       Number to be converted
  15620.  
  15621.                                   %@AI@%string%@AE@%      String result
  15622.  
  15623.                                   %@AI@%radix%@AE@%       Number base of value
  15624.  
  15625. %@AB@%Returns%@AE@%                           A pointer to %@AI@%string%@AE@%; there is no error  
  15626.                                   return
  15627.  
  15628. ────────────────────────────────────────────────────────────────────────────
  15629. %@AB@%strtod, strtol, strtoul%@AE@% - These routines convert a string to a %@AB@%double%@AE@%, a
  15630. %@AB@%long%@AE@%, and an %@AB@%unsigned long%@AE@%, respectively.%@NL@%
  15631. %@NL@%
  15632. %@AB@%Include%@AE@%                           STDLIB.H
  15633.  
  15634. %@AB@%Prototypes%@AE@%                        %@AB@%double strtod( const char *%@AE@%%@AI@%nptr%@AE@%%@AB@%,  char %@AE@%
  15635.                                   %@AB@%**%@AE@%%@AI@%endptr %@AE@%%@AB@%);%@AE@%
  15636.  
  15637.  
  15638.  
  15639.                                   long strtol( const char *%@AI@%nptr%@AE@%%@AB@%, char **%@AE@%%@AI@%%@AE@%
  15640.                                   %@AI@%endptr%@AE@%%@AB@%,  int %@AE@%%@AI@%base%@AE@%%@AB@% );%@AE@%
  15641.  
  15642.  
  15643.  
  15644.                                   unsigned long strtoul( const char *%@AI@%nptr%@AE@%%@AB@%,%@AE@%
  15645.                                   %@AB@%char **%@AE@%%@AI@%endptr%@AE@%%@AB@%,  int %@AE@%%@AI@%base%@AE@%%@AB@% );%@AE@%
  15646.  
  15647. %@AB@%Arguments%@AE@%                         %@AI@%nptr%@AE@%        String to convert
  15648.  
  15649.                                   %@AI@%endptr%@AE@%      End of scan
  15650.  
  15651.                                   %@AI@%base%@AE@%        Number base to use
  15652.  
  15653. %@AB@%Returns%@AE@%                           %@AB@%strtod%@AE@%: the converted value; overflow 
  15654.                                   returns  %@AB@%HUGE_VAL%@AE@%,
  15655.  
  15656.                                   underflow returns 0
  15657.  
  15658.                                   %@AB@%strtol%@AE@%: the converted value; overflow 
  15659.                                   returns %@AB@%LONG_MAX%@AE@%  
  15660.  
  15661. %@AB@%%@AE@%                                  or%@AB@% LONG_MIN%@AE@%, depending on sign of 
  15662.                                   converted  value
  15663.  
  15664.                                   %@AB@%strtoul%@AE@%: the converted value if 
  15665.                                   successful, 0 if not, and
  15666.  
  15667.                                   %@AB@%ULONG_MAX%@AE@% on overflow
  15668.  
  15669. %@NL@%
  15670. %@2@%%@CR:C6A-B0006   @%%@AB@%Error Message Routines%@AE@%%@EH@%%@NL@%
  15671. %@NL@%
  15672. The routines in this category handle the display of error messages.  %@NL@%
  15673. %@NL@%
  15674. The %@AB@%assert%@AE@% macro is typically used to test for program logic errors; it
  15675. prints a message when a given "assertion" fails to hold true. Defining the
  15676. identifier %@AB@%NDEBUG%@AE@% to any value causes occurrences of %@AB@%assert%@AE@% to be disabled
  15677. in the source file, thus allowing you to turn off assertion checking without
  15678. modifying the source file.  %@NL@%
  15679. %@NL@%
  15680. The %@AB@%perror%@AE@% routine prints the system-error message, along with a
  15681. user-supplied message, for the last system-level call that produced an
  15682. error. The %@AB@%perror%@AE@% routine is declared in the include files STDLIB.H and
  15683. STDIO.H. The error number is obtained from the %@AB@%errno%@AE@% variable. The system
  15684. message is taken from the %@AB@%sys_errlist%@AE@% array.  %@NL@%
  15685. %@NL@%
  15686. The %@AB@%strerror%@AE@% and %@AB@%_strerror%@AE@% routines store error messages in a string.  %@NL@%
  15687. %@NL@%
  15688. ────────────────────────────────────────────────────────────────────────────
  15689. %@AB@%assert%@AE@% - Tests for logic error.%@NL@%
  15690. %@NL@%
  15691. %@AB@%Include%@AE@%                           ASSERT.H, STDIO.H
  15692.  
  15693. %@AB@%Prototype%@AE@%                         %@AB@%void assert(%@AE@%%@AI@% expression %@AE@%%@AB@%);%@AE@%
  15694.  
  15695. %@AB@%Argument%@AE@%                          %@AI@%expression%@AE@%  Expression to test
  15696.  
  15697. %@AB@%Returns%@AE@%                           Void
  15698.  
  15699. ────────────────────────────────────────────────────────────────────────────
  15700. %@AB@%perror%@AE@% - Prints error message.%@NL@%
  15701. %@NL@%
  15702. %@AB@%Include%@AE@%                           STDIO.H
  15703.  
  15704. %@AB@%Prototypes%@AE@%                        %@AB@%void perror( const char *%@AE@%%@AI@%string%@AE@%%@AB@%  );%@AE@%
  15705.  
  15706.                                   int %@AI@%errno%@AE@%%@AB@%;%@AE@%
  15707.  
  15708.                                   int %@AI@%sys_nerr%@AE@%%@AB@%;%@AE@%
  15709.  
  15710.                                   char *%@AI@%sys_errlist %@AE@%%@AB@%[%@AE@%%@AI@%sys_nerr%@AE@%%@AB@%];%@AE@%
  15711.  
  15712. %@AB@%Arguments%@AE@%                         %@AI@%string%@AE@%      User-supplied message
  15713.  
  15714.                                   %@AI@%errno%@AE@%       Error number
  15715.  
  15716.                                   %@AI@%sys_nerr%@AE@%    Number of system-error 
  15717.                                               messages
  15718.  
  15719.                                   %@AI@%sys_errlis%@AE@%  Array of error messages
  15720.                                   %@AI@%t%@AE@%           
  15721.  
  15722. %@AB@%Returns%@AE@%                           Void
  15723.  
  15724. ────────────────────────────────────────────────────────────────────────────
  15725. %@AB@%strerror, _strerror %@AE@% - Saves system-error message and optional user-error
  15726. message in string. The routine %@AB@%strerror%@AE@% is the ANSI-compatible version.%@NL@%
  15727. %@NL@%
  15728. %@AB@%Include%@AE@%                           STRING.H
  15729.  
  15730. %@AB@%Prototypes%@AE@%                        %@AB@%char *strerror( int %@AE@%%@AI@%errnum%@AE@%%@AB@%  );%@AE@%
  15731.  
  15732.                                   %@AB@%char *_strerror( char *%@AE@%%@AI@%string%@AE@%%@AB@% );%@AE@%
  15733.  
  15734.                                   %@AB@%int%@AE@% %@AI@%errno%@AE@%%@AB@%;%@AE@%
  15735.  
  15736.                                   %@AB@%int%@AE@% %@AI@%sys_nerr%@AE@%%@AB@%;%@AE@%
  15737.  
  15738.                                   %@AB@%char *%@AE@%%@AI@%sys_errlist %@AE@%%@AB@%[%@AE@%%@AI@%sys_nerr%@AE@%%@AB@%];%@AE@%
  15739.  
  15740. %@AB@%Arguments%@AE@%                         %@AI@%errnum%@AE@%      Error number
  15741.  
  15742.                                   %@AI@%string%@AE@%      User-supplied message
  15743.  
  15744.                                   %@AI@%errno%@AE@%       Error number
  15745.  
  15746.                                   %@AI@%sys_nerr%@AE@%    Number of system-error 
  15747.                                               messages
  15748.  
  15749.                                   %@AI@%sys_errlis%@AE@%  Array of error messages
  15750.                                   %@AI@%t%@AE@%           
  15751.  
  15752. %@AB@%Returns%@AE@%                           A pointer to the error-message string
  15753.  
  15754. %@NL@%
  15755. %@2@%%@CR:C6A-B0007   @%%@AB@%Graphics 1: Low-Level Graphics Routines%@AE@%%@EH@%%@NL@%
  15756. %@NL@%
  15757. The low-level graphics routines provide line, figure, and pixel manipulation
  15758. capabilities. The routines for presentation graphics are described in the
  15759. next section. The routines for displaying fonts follow the presentation
  15760. graphics section.  %@NL@%
  15761. %@NL@%
  15762. The graphics package supports the IBM(R) (and compatible) Enhanced Graphics
  15763. Adapter (EGA), Color Graphics Adapter (CGA), certain operating modes of the
  15764. Video Graphics Array (VGA) hardware configurations, and the MCGA (Multicolor
  15765. Graphics Array). The graphics package also supports the Hercules Graphics
  15766. Card, Graphics Card Plus, InColor Card, and 100 percent compatible cards, as
  15767. well as the special Olivetti(R) modes available on AT&T(R) computers.  %@NL@%
  15768. %@NL@%
  15769. The low-level graphics routines can be divided into the seven categories
  15770. listed below, corresponding to the different tasks involved with creating
  15771. and manipulating graphic objects:  %@NL@%
  15772. %@NL@%
  15773. %@AB@%Category%@AE@%                          %@AB@%Task%@AE@%
  15774. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  15775. Configure mode and environment    Selects the proper display mode for the 
  15776.                                   hardware and establishes memory areas 
  15777.                                   for writing and displaying images
  15778.  
  15779. Set coordinates                   Specifies the logical origin and the 
  15780.                                   active display area within the screen
  15781.  
  15782. Set palette                       Specifies a palette mapping
  15783.  
  15784. Set attributes                    Specifies background and foreground 
  15785.                                   colors and mask and line styles
  15786.  
  15787. Output images                     Draws and fills figures on the screen
  15788.  
  15789. Output text                       Writes text to the screen
  15790.  
  15791. Transfer images                   Stores images in memory and retrieves 
  15792.                                   them
  15793.  
  15794. %@NL@%
  15795. %@4@%%@AB@%Configure Mode and Environment%@AE@%%@EH@%%@NL@%
  15796. %@NL@%
  15797. The configure category of functions sets the status of the cursor, sets
  15798. active and visual pages, and determines and sets video display modes.  %@NL@%
  15799. %@NL@%
  15800. The %@AB@%_setvideomode%@AE@% and %@AB@%_getvideoconfig%@AE@% functions are generally used at the
  15801. very beginning of a graphics program.  %@NL@%
  15802. %@NL@%
  15803. ────────────────────────────────────────────────────────────────────────────
  15804. %@AB@%_displaycursor%@AE@% - Determines whether the cursor will be left on or turned off
  15805. on exit from graphics routines.%@NL@%
  15806. %@NL@%
  15807. %@AB@%Include%@AE@%                           GRAPH.H
  15808.  
  15809. %@AB@%Prototype%@AE@%                         %@AB@%short _far _displaycursor( short %@AE@%%@AI@%toggle %@AE@%
  15810.                                   %@AI@%%@AE@%%@AB@%);%@AE@%
  15811.  
  15812. %@AB@%Argument%@AE@%                          %@AI@%toggle%@AE@%      Cursor state (%@AB@%_GCURSORON%@AE@%,%@AB@%  %@AE@%
  15813.                                               %@AB@%_GCURSOROFF%@AE@%  )
  15814.  
  15815. %@AB@%Returns%@AE@%                           The previous value of toggle
  15816.  
  15817. ────────────────────────────────────────────────────────────────────────────
  15818. %@AB@%_getvideoconfig%@AE@% - Obtains status of current graphics environment.%@NL@%
  15819. %@NL@%
  15820. %@AB@%Include%@AE@%                           GRAPH.H
  15821.  
  15822. %@AB@%Prototype%@AE@%                         %@AB@%struct videoconfig _far * _far %@AE@%
  15823.                                   %@AB@%_getvideoconfig%@AE@%
  15824.  
  15825.                                   ( struct videoconfig _far *%@AI@%config %@AE@%%@AB@%);%@AE@%
  15826.  
  15827. %@AB@%Argument%@AE@%                          %@AI@%config%@AE@%      Configuration information
  15828.  
  15829. %@AB@%Returns%@AE@%                           The configuration information as a %@AB@%%@AE@%
  15830.                                   %@AB@%videoconfig%@AE@%  structure
  15831.  
  15832. ────────────────────────────────────────────────────────────────────────────
  15833. %@AB@%_setvideomode%@AE@% - Selects screen display mode.%@NL@%
  15834. %@NL@%
  15835. %@AB@%Include%@AE@%                           GRAPH.H
  15836.  
  15837. %@AB@%Prototype%@AE@%                         %@AB@%short _far _setvideomode( short %@AE@%%@AI@%mode%@AE@%%@AB@% );%@AE@%
  15838.  
  15839. %@AB@%Argument%@AE@%                          %@AI@%mode%@AE@%        Desired mode (%@AB@%_DEFAULTMODE%@AE@%,%@AB@% %@AE@%
  15840.                                               %@AB@%_TEXTBW40%@AE@%,%@AB@%%@AE@%
  15841.  
  15842.                                   _TEXTC40,%@AB@% _TEXTBW80%@AE@%,%@AB@% _TEXTC80%@AE@%,%@AB@% %@AE@%
  15843.                                   %@AB@%_MRES4COLOR%@AE@%,
  15844.  
  15845.                                   %@AB@%_MRESNOCOLOR%@AE@%,%@AB@% _HRESBW%@AE@%,%@AB@% _TEXTMONO%@AE@%,%@AB@%%@AE@%
  15846.  
  15847.                                   _HERCMONO, %@AB@%_MRES16COLOR%@AE@%,%@AB@% _HRES16COLOR%@AE@%,%@AB@%%@AE@%
  15848.  
  15849.                                   _ERESNOCOLOR,%@AB@% _ERESCOLOR%@AE@%,%@AB@% _VRES2COLOR%@AE@%,%@AB@%%@AE@%
  15850.  
  15851.                                   _VRES16COLOR,%@AB@% _MRES256COLOR%@AE@%,%@AB@% _ORESCOLOR%@AE@%%@AI@%,%@AE@%
  15852.  
  15853.                                   %@AB@%_MAXRESMODE%@AE@%, %@AB@%_MAXCOLORMODE%@AE@%)
  15854.  
  15855. %@AB@%Returns%@AE@%                           A nonzero value if successful, 0 if not
  15856.  
  15857. %@NL@%
  15858. %@4@%%@AB@%Set Coordinates%@AE@%%@EH@%%@NL@%
  15859. %@NL@%
  15860. The Microsoft C graphics routines recognize three sets of coordinates:  %@NL@%
  15861. %@NL@%
  15862. %@NL@%
  15863.   1.  Window coordinates defined with real-number values that are mapped to
  15864.       a specified viewport%@NL@%
  15865. %@NL@%
  15866.   2.  Viewport coordinates defined by the application (viewport coordinates)%@NL@%
  15867. %@NL@%
  15868.   3.  Fixed physical coordinates determined by the hardware and display
  15869.       configuration of the user's environment (physical coordinates)%@NL@%
  15870. %@NL@%
  15871. %@NL@%
  15872. The functions in this category alter the coordinate systems and provide a
  15873. means to translate coordinates between the various systems.  %@NL@%
  15874. %@NL@%
  15875. Most of these routines have two or three forms. The functions are listed by
  15876. the "base" name, without a suffix. Note, though, that function names that
  15877. end with a %@AB@%_w%@AE@%, such as %@AB@%_getcurrentposition_w%@AE@%, use the window-coordinate
  15878. system. Those that end with a %@AB@%_wxy%@AE@%, such as %@AB@%_getviewcoord_wxy%@AE@%, use the
  15879. window-coordinate system and a %@AB@%_wxycoord%@AE@% structure to define the
  15880. coordinates.  %@NL@%
  15881. %@NL@%
  15882. The default viewport-coordinate system is identical to the physical one. The
  15883. physical origin (0, 0) is always in the upper left corner of the display.
  15884. The %@AI@%x%@AE@% axis extends in the positive direction left to right, and the %@AI@%y%@AE@% axis
  15885. extends in the positive direction top to bottom.  %@NL@%
  15886. %@NL@%
  15887. The dimensions of the %@AI@%x%@AE@% and %@AI@%y%@AE@% axes depend upon the hardware display
  15888. configuration and the selected mode. These values are accessible at run time
  15889. by examining the %@AB@%numxpixels%@AE@% and %@AB@%numypixels%@AE@% fields of the %@AB@%videoconfig%@AE@%
  15890. structure returned by %@AB@%_getvideoconfig%@AE@%.  %@NL@%
  15891. %@NL@%
  15892. ────────────────────────────────────────────────────────────────────────────
  15893. %@AB@%_getcurrentposition%@AE@% - Obtains the coordinates of the current graphic-output
  15894. position. The %@AB@%_getcurrentposition%@AE@% function returns the position as an
  15895. %@AB@%xycoord%@AE@% structure and the %@AB@%_getcurrentposition_w%@AE@% function returns the
  15896. position as a %@AB@% _wxycoord%@AE@% structure.%@NL@%
  15897. %@NL@%
  15898. %@AB@%Include%@AE@%                           GRAPH.H
  15899.  
  15900. %@AB@%Prototypes%@AE@%                        %@AB@%struct xycoord _far _getcurrentposition(%@AE@%
  15901.                                   %@AB@%void );%@AE@%
  15902.  
  15903.  
  15904.  
  15905.                                   struct _wxycoord _far 
  15906.                                   _getcurrentposition_w( void );
  15907.  
  15908. %@AB@%Arguments%@AE@%                         None
  15909.  
  15910. %@AB@%Returns%@AE@%                           %@AB@%_getcurrentposition%@AE@%: the coordinates of 
  15911.                                   the current position as
  15912.  
  15913.                                   an %@AB@%xycoord%@AE@% structure
  15914.  
  15915.                                   %@AB@%_getcurrentposition_w%@AE@%: the coordinates 
  15916.                                   of the current position
  15917.  
  15918.                                   as a %@AB@%_wxycoord%@AE@% structure
  15919.  
  15920. ────────────────────────────────────────────────────────────────────────────
  15921. %@AB@%_getphyscoord%@AE@% - Converts viewport coordinates to physical coordinates.%@NL@%
  15922. %@NL@%
  15923. %@AB@%Include%@AE@%                           GRAPH.H
  15924.  
  15925. %@AB@%Prototype%@AE@%                         %@AB@%struct xycoord _far _getphyscoord( short%@AE@%
  15926.                                   %@AB@%%@AE@%%@AI@%x%@AE@%%@AB@%, short %@AE@%%@AI@%y%@AE@%%@AB@% );%@AE@%
  15927.  
  15928. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%, %@AI@%y%@AE@%        View point to translate
  15929.  
  15930. %@AB@%Returns%@AE@%                           A pair of physical coordinates as an %@AB@%%@AE@%
  15931.                                   %@AB@%xycoord%@AE@%  structure
  15932.  
  15933. ────────────────────────────────────────────────────────────────────────────
  15934. %@AB@%_getviewcoord %@AE@% - Converts specified coordinates to viewport coordinates.%@NL@%
  15935. %@NL@%
  15936. %@AB@%Include%@AE@%                           GRAPH.H
  15937.  
  15938. %@AB@%Prototypes%@AE@%                        %@AB@%struct xycoord _far _getviewcoord( short%@AE@%
  15939.                                   %@AB@%%@AE@%%@AI@%x%@AE@%%@AB@%, short  );%@AE@%
  15940.  
  15941.  
  15942.  
  15943.                                   struct xycoord _far _getviewcoord_w( 
  15944.                                   double %@AI@%wx%@AE@%%@AB@%, double %@AE@%%@AI@%wy  %@AE@%%@AB@%);%@AE@%
  15945.  
  15946.  
  15947.  
  15948.                                   struct xycoord _far _getviewcoord_wxy( 
  15949.                                   struct _wxycoord _far
  15950.  
  15951.                                   *%@AI@%pwxy1%@AE@%%@AB@% );%@AE@%
  15952.  
  15953. %@AB@%Arguments%@AE@%                         %@AI@%x%@AE@%, %@AI@%y%@AE@%        Physical point to translate
  15954.  
  15955.                                   %@AI@%wx%@AE@%, %@AI@%wy%@AE@%      Window-coordinate point to 
  15956.                                               translate
  15957.  
  15958.                                   %@AI@%pwxy1%@AE@%       Window-coordinate point to 
  15959.                                               translate
  15960.  
  15961. %@AB@%Returns%@AE@%                           A pair of logical coordinates as an %@AB@%%@AE@%
  15962.                                   %@AB@%xycoord%@AE@%  structure
  15963.  
  15964. ────────────────────────────────────────────────────────────────────────────
  15965. %@AB@%_getwindowcoord%@AE@% - Converts physical coordinates to window coordinates.%@NL@%
  15966. %@NL@%
  15967. %@AB@%Include%@AE@%                           GRAPH.H
  15968.  
  15969. %@AB@%Prototype%@AE@%                         %@AB@%struct _wxycoord _far _getwindowcoord( %@AE@%
  15970.                                   %@AB@%short  %@AE@%%@AI@%x%@AE@%%@AB@%, short %@AE@%%@AI@%y%@AE@%%@AB@% );%@AE@%
  15971.  
  15972. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%, %@AI@%y%@AE@%        Physical point to translate
  15973.  
  15974. %@AB@%Returns%@AE@%                           A pair of window coordinates as a %@AB@%%@AE@%
  15975.                                   %@AB@%_wxycoord%@AE@%  structure
  15976.  
  15977. ────────────────────────────────────────────────────────────────────────────
  15978. %@AB@%_setcliprgn%@AE@% - Limits graphic output to part of the screen.%@NL@%
  15979. %@NL@%
  15980. %@AB@%Include%@AE@%                           GRAPH.H
  15981.  
  15982. %@AB@%Prototype%@AE@%                         %@AB@%void _far _setcliprgn( short %@AE@%%@AI@%x1%@AE@%%@AB@%, short  %@AE@%
  15983.                                   %@AI@%y1%@AE@%%@AB@%, short %@AE@%%@AI@%x2%@AE@%%@AB@%, short %@AE@%%@AI@%y2 %@AE@%%@AB@%);%@AE@%
  15984.  
  15985. %@AB@%Arguments%@AE@%                         %@AI@%x1%@AE@%,%@AI@% y1%@AE@%      Upper left corner of clip  
  15986.                                               region
  15987.  
  15988.                                   %@AI@%x2, y2%@AE@%      Lower right corner of clip 
  15989.                                               region
  15990.  
  15991. %@AB@%Returns%@AE@%                           Void
  15992.  
  15993. ────────────────────────────────────────────────────────────────────────────
  15994. %@AB@%_setvieworg%@AE@% - Positions the logical origin.%@NL@%
  15995. %@NL@%
  15996. %@AB@%Include%@AE@%                           GRAPH.H
  15997.  
  15998. %@AB@%Prototype%@AE@%                         %@AB@%struct xycoord _far _setvieworg( short %@AE@%%@AI@%x%@AE@%
  15999.                                   %@AB@%,  short %@AE@%%@AI@%y %@AE@%%@AB@%);%@AE@%
  16000.  
  16001. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%, %@AI@%y%@AE@%        New origin point
  16002.  
  16003. %@AB@%Returns%@AE@%                           The physical coordinates of the previous
  16004.                                   viewport  origin in an
  16005.  
  16006.                                   %@AB@%xycoord%@AE@% structure
  16007.  
  16008. ────────────────────────────────────────────────────────────────────────────
  16009. %@AB@%_setviewport%@AE@% - Limits graphic output and positions the logical origin within
  16010. a limited area.%@NL@%
  16011. %@NL@%
  16012. %@AB@%Include%@AE@%                           GRAPH.H
  16013.  
  16014. %@AB@%Prototype%@AE@%                         %@AB@%void _far _setviewport( short %@AE@%%@AI@%x1%@AE@%%@AB@%,  short%@AE@%
  16015.                                   %@AB@%%@AE@%%@AI@%y1%@AE@%%@AB@%, short %@AE@%%@AI@%x2%@AE@%%@AB@%, short %@AE@%%@AI@%y2%@AE@%%@AB@% );%@AE@%
  16016.  
  16017. %@AB@%Arguments%@AE@%                         %@AI@%x1%@AE@%, %@AI@%y1%@AE@%      Upper left corner of window
  16018.  
  16019.                                   %@AI@%x2%@AE@%, %@AI@%y2%@AE@%      Lower right corner of window
  16020.  
  16021. %@AB@%Returns%@AE@%                           Void
  16022.  
  16023. ────────────────────────────────────────────────────────────────────────────
  16024. %@AB@%_setwindow%@AE@% - Defines a window-coordinate system.%@NL@%
  16025. %@NL@%
  16026. %@AB@%Include%@AE@%                           GRAPH.H
  16027.  
  16028. %@AB@%Prototype%@AE@%                         %@AB@%void _far _setwindow( short %@AE@%%@AI@%finvert%@AE@%%@AB@%,  %@AE@%
  16029.                                   %@AB@%double %@AE@%%@AI@%wx1%@AE@%%@AB@%, double %@AE@%%@AI@%wy1%@AE@%%@AB@%,%@AE@%
  16030.  
  16031.                                   double %@AI@%wx2%@AE@%%@AB@%, double %@AE@%%@AI@%wy2%@AE@%%@AB@% );%@AE@%
  16032.  
  16033. %@AB@%Arguments%@AE@%                         %@AI@%wx1%@AE@%,%@AI@% wy1%@AE@%    Upper left corner of  window
  16034.  
  16035.                                   %@AI@%wx2%@AE@%,%@AI@% wy2%@AE@%    Lower right corner of window
  16036.  
  16037.                                   %@AI@%finvert%@AE@%     Invert flag  (TRUE, FALSE)
  16038.  
  16039. %@AB@%Returns%@AE@%                           Void
  16040.  
  16041. %@NL@%
  16042. %@4@%%@AB@%Set Palette%@AE@%%@EH@%%@NL@%
  16043. %@NL@%
  16044. A screen pixel can be represented as a one-, two-, or four-bit value,
  16045. depending on the particular mode. The byte representation is called the
  16046. "color value."  %@NL@%
  16047. %@NL@%
  16048. Each color that can be displayed is represented by a unique ordinal value
  16049. called a "color index." A palette is simply a mapping of the actual display
  16050. colors to the legal values.  %@NL@%
  16051. %@NL@%
  16052. ────────────────────────────────────────────────────────────────────────────
  16053. %@AB@%_remapallpalette, _remappalette%@AE@% - The %@AB@%_remapallpalette%@AE@% routine assigns
  16054. colors to all color values. The %@AB@%_remappalette%@AE@% routine assigns color indexes
  16055. to selected color values.%@NL@%
  16056. %@NL@%
  16057. %@AB@%Include%@AE@%                           GRAPH.H
  16058.  
  16059. %@AB@%Prototypes%@AE@%                        %@AB@%short _far _remapallpalette( long _far *%@AE@%
  16060.                                   %@AI@%colors%@AE@%%@AB@%  );%@AE@%
  16061.  
  16062.  
  16063.  
  16064.                                   long _far _remappalette( short %@AI@%index%@AE@%%@AB@%, %@AE@%
  16065.                                   %@AB@%long %@AE@%%@AI@%color%@AE@%%@AB@% );%@AE@%
  16066.  
  16067.  
  16068.  
  16069. Arguments                         %@AI@%colors%@AE@%      Color value array: %@AB@%(_BLACK%@AE@%, 
  16070.                                               %@AB@%_BLUE%@AE@%, %@AB@%_GREEN,%@AE@%
  16071.  
  16072.                                   _CYAN, _RED,%@AB@% _MAGENTA%@AE@%,%@AB@% _BROWN%@AE@%,%@AB@%  _WHITE%@AE@%,
  16073.  
  16074. %@AB@%%@AE@%                                  _GRAY, %@AB@%_LIGHTBLUE, ,_LIGHTGREEN,%@AE@%
  16075.  
  16076.                                   _LIGHTCYAN, %@AB@%_LIGHTRED, _LIGHTMAGENTA,%@AE@%
  16077.  
  16078.                                   _LIGHTYELL  %@AB@%_BRIGHTWHITE)%@AE@%
  16079.                                   OW,         
  16080.  
  16081.                                   %@AI@%index%@AE@%       Color index to reassign
  16082.  
  16083.                                   %@AI@%color%@AE@%       Color value to assign color 
  16084.                                               index
  16085.  
  16086. %@AB@%Returns%@AE@%                           %@AB@%_remapallpalette:%@AE@%  0 if successful, -1  
  16087.                                   if not
  16088.  
  16089.                                   %@AB@%_remappalette:%@AE@%  the previous color value
  16090.                                   of the index argument
  16091.  
  16092.                                   if successful, -1 if not
  16093.  
  16094. ────────────────────────────────────────────────────────────────────────────
  16095. %@AB@%_selectpalette%@AE@% - Selects a predefined palette.%@NL@%
  16096. %@NL@%
  16097. %@AB@%Include%@AE@%                           GRAPH.H
  16098.  
  16099. %@AB@%Prototype%@AE@%                         %@AB@%short _far _selectpalette( short %@AE@%%@AI@%number %@AE@%
  16100.                                   %@AI@%%@AE@%%@AB@%);%@AE@%
  16101.  
  16102. %@AB@%Argument%@AE@%                          %@AI@%number%@AE@%      Palette number
  16103.  
  16104. %@AB@%Returns%@AE@%                           The value of the previous palette
  16105.  
  16106. %@NL@%
  16107. %@4@%%@AB@%Set Attributes%@AE@%%@EH@%%@NL@%
  16108. %@NL@%
  16109. Attributes are characteristics (color, fill pattern, or line style) that can
  16110. be specified for low-level graphics routines.  %@NL@%
  16111. %@NL@%
  16112. A fill mask is an 8-by-8-bit template array, with each bit representing a
  16113. pixel. If a bit is 0, the pixel in memory is left untouched: the mask is
  16114. transparent to that pixel. If a bit is 1, the pixel is assigned the current
  16115. color value. The template is repeated over the entire fill area.  %@NL@%
  16116. %@NL@%
  16117. A line style is a 16-bit template buffer, with each bit corresponding to a
  16118. pixel. If a bit is 0, the pixel is set to the current background color. If a
  16119. bit is 1, the pixel is set to the current color. The template is repeated
  16120. for the length of the line.  %@NL@%
  16121. %@NL@%
  16122. ────────────────────────────────────────────────────────────────────────────
  16123. %@AB@%_getbkcolor%@AE@% - Reports the current background color.%@NL@%
  16124. %@NL@%
  16125. %@AB@%Include%@AE@%                           GRAPH.H
  16126.  
  16127. %@AB@%Prototype%@AE@%                         %@AB@%long _far _getbkcolor( void );%@AE@%
  16128.  
  16129. %@AB@%Arguments%@AE@%                         None
  16130.  
  16131. %@AB@%Returns%@AE@%                           The current background color
  16132.  
  16133. ────────────────────────────────────────────────────────────────────────────
  16134. %@AB@%_getcolor%@AE@% - Obtains the current color.%@NL@%
  16135. %@NL@%
  16136. %@AB@%Include%@AE@%                           GRAPH.H
  16137.  
  16138. %@AB@%Prototype%@AE@%                         %@AB@%short _far _getcolor( void );%@AE@%
  16139.  
  16140. %@AB@%Arguments%@AE@%                         None
  16141.  
  16142. %@AB@%Returns%@AE@%                           The current color
  16143.  
  16144. ────────────────────────────────────────────────────────────────────────────
  16145. %@AB@%_setbkcolor%@AE@% - Sets the current background color.%@NL@%
  16146. %@NL@%
  16147. %@AB@%Include%@AE@%                           GRAPH.H
  16148.  
  16149. %@AB@%Prototype%@AE@%                         %@AB@%long _far _setbkcolor( long %@AE@%%@AI@%color %@AE@%%@AB@%);%@AE@%
  16150.  
  16151. %@AB@%Argument%@AE@%                          %@AI@%color%@AE@%       Desired color value
  16152.  
  16153. %@AB@%Returns%@AE@%                           The color value of the previous 
  16154.                                   background color
  16155.  
  16156. ────────────────────────────────────────────────────────────────────────────
  16157. %@AB@%_setcolor%@AE@% - Sets the current color.%@NL@%
  16158. %@NL@%
  16159. %@AB@%Include%@AE@%                           GRAPH.H
  16160.  
  16161. %@AB@%Prototype%@AE@%                         %@AB@%short _far _setcolor( short %@AE@%%@AI@%color %@AE@%%@AB@%);%@AE@%
  16162.  
  16163. %@AB@%Argument%@AE@%                          %@AI@%color%@AE@%       Desired color index
  16164.  
  16165. %@AB@%Returns%@AE@%                           The previous color
  16166.  
  16167. %@NL@%
  16168. %@4@%%@AB@%Output Images%@AE@%%@EH@%%@NL@%
  16169. %@NL@%
  16170. These routines display graphic elements (arcs, lines, pixels, etc.) on the
  16171. screen.  %@NL@%
  16172. %@NL@%
  16173. Circular figures such as arcs and ellipses are centered within a "bounding
  16174. rectangle," specified by two points that define the diagonally opposed
  16175. corners of the rectangle. The center of the rectangle becomes the center of
  16176. the figure, and the rectangle's borders determine the size of the figure.  %@NL@%
  16177. %@NL@%
  16178. Most of these routines have two or three forms. The functions are listed by
  16179. the "base" name, without a suffix. Note, though, that function names that
  16180. end with a %@AB@%_w%@AE@%, such as %@AB@%_arc_w%@AE@%, use the window coordinate system. Those that
  16181. end with a %@AB@%_wxy%@AE@%, such as %@AB@%_ellipse_wxy%@AE@%, use the window coordinate system and
  16182. a %@AB@% _wxycoord%@AE@% structure to define the coordinates.  %@NL@%
  16183. %@NL@%
  16184. ────────────────────────────────────────────────────────────────────────────
  16185. %@AB@%_arc %@AE@% - Draws an arc.%@NL@%
  16186. %@NL@%
  16187. %@AB@%Include%@AE@%                           GRAPH.H
  16188.  
  16189. %@AB@%Prototypes%@AE@%                        %@AB@%short _far _arc( short %@AE@%%@AI@%x1%@AE@%%@AB@%, short  %@AE@%%@AI@%y1%@AE@%%@AB@%, %@AE@%
  16190.                                   %@AB@%short %@AE@%%@AI@%x2%@AE@%%@AB@%, short %@AE@%%@AI@%y2%@AE@%%@AB@%,%@AE@%
  16191.  
  16192.                                   short %@AI@%x3%@AE@%%@AB@%, short %@AE@%%@AI@%y3%@AE@%%@AB@%, short %@AE@%%@AI@%x4%@AE@%%@AB@%, short %@AE@%%@AI@%y4  %@AE@%
  16193.                                   %@AB@%);%@AE@%
  16194.  
  16195.  
  16196.  
  16197.                                   short _far _arc_wxy( struct _wxycoord %@AI@%%@AE@%
  16198.                                   %@AI@%pwxy1%@AE@%%@AB@%,%@AE@%
  16199.  
  16200.                                   struct _wxycoord*%@AI@%pwxy2%@AE@%%@AB@%, struct %@AE@%
  16201.                                   %@AB@%_wxycoord*%@AE@%%@AI@%pwxy3%@AE@%%@AB@%,  %@AE@%
  16202.  
  16203.                                   struct _wxycoord*%@AI@%pwxy4 %@AE@%%@AB@%);%@AE@%
  16204.  
  16205. %@AB@%Arguments%@AE@%                         %@AI@%x1%@AE@%, %@AI@%y1%@AE@%      Upper left corner of 
  16206.                                               bounding  rectangle
  16207.  
  16208.                                   %@AI@%x2%@AE@%,%@AI@% y2%@AE@%      Lower right corner of 
  16209.                                               bounding rectangle
  16210.  
  16211.                                   %@AI@%x%@AE@%3,%@AI@% y3%@AE@%      Start vector
  16212.  
  16213.                                   %@AI@%x4%@AE@%,%@AI@% y4%@AE@%      End vector
  16214.  
  16215.                                   %@AI@%pwxy1%@AE@%       Upper left corner of 
  16216.                                               bounding rectangle
  16217.  
  16218.                                   %@AI@%pwxy2%@AE@%       Lower right corner of 
  16219.                                               bounding rectangle
  16220.  
  16221.                                   %@AI@%pwxy3%@AE@%       Start vector
  16222.  
  16223.                                   %@AI@%pwxy4%@AE@%       End vector
  16224.  
  16225. %@AB@%Returns%@AE@%                           A nonzero value if the arc is drawn 
  16226.                                   successfully,  0 if not
  16227.  
  16228. ────────────────────────────────────────────────────────────────────────────
  16229. %@AB@%_clearscreen%@AE@% - Erases the screen and fills it with the current background
  16230. color.%@NL@%
  16231. %@NL@%
  16232. %@AB@%Include%@AE@%                           GRAPH.H
  16233.  
  16234. %@AB@%Prototype%@AE@%                         %@AB@%void _far _clearscreen( short %@AE@%%@AI@%area %@AE@%%@AB@%);%@AE@%
  16235.  
  16236. %@AB@%Argument%@AE@%                          %@AI@%area%@AE@%        Target area (%@AB@%_GCLEARSCREEN%@AE@%,%@AB@% %@AE@%
  16237.                                               %@AB@%_GVIEWPORT%@AE@%,
  16238.  
  16239.                                   %@AB@% _GWINDOW%@AE@%)
  16240.  
  16241. %@AB@%Returns%@AE@%                           Void
  16242.  
  16243. ────────────────────────────────────────────────────────────────────────────
  16244. %@AB@%_ellipse %@AE@% - Draws an ellipse.%@NL@%
  16245. %@NL@%
  16246. %@AB@%Include%@AE@%                           GRAPH.H
  16247.  
  16248. %@AB@%Prototypes%@AE@%                        %@AB@%short _far _ellipse( short %@AE@%%@AI@%control%@AE@%%@AB@%,  %@AE@%
  16249.                                   %@AB@%short %@AE@%%@AI@%x1%@AE@%%@AB@%, short %@AE@%%@AI@%y1%@AE@%%@AB@%,%@AE@%
  16250.  
  16251.                                   short %@AI@%x2%@AE@%%@AB@%, short %@AE@%%@AI@%y2 %@AE@%%@AB@%);%@AE@%
  16252.  
  16253.  
  16254.  
  16255.                                   short _far _ellipse_w( short %@AI@%control%@AE@%%@AB@%, %@AE@%
  16256.                                   %@AB@%double %@AE@%%@AI@%wx1%@AE@%%@AB@%,  double %@AE@%%@AI@%wy1%@AE@%%@AB@%,%@AE@%
  16257.  
  16258.                                   double %@AI@%wx2%@AE@%%@AB@%, double %@AE@%%@AI@%wy2 %@AE@%%@AB@%);%@AE@%
  16259.  
  16260.  
  16261.  
  16262.                                   short _far _ellipse_wxy( short %@AI@%control%@AE@%%@AB@%, %@AE@%
  16263.                                   %@AB@%struct _wxycoord*  %@AE@%%@AI@%pwxy1%@AE@%%@AB@%,%@AE@%
  16264.  
  16265.                                   struct _wxycoord*%@AI@%pwxy2%@AE@%%@AB@% );%@AE@%
  16266.  
  16267. %@AB@%Arguments%@AE@%                         %@AI@%control%@AE@%     Fill flag (%@AB@% _GFILLINTERIOR%@AE@%, 
  16268.                                               %@AB@% _GBORDER%@AE@%)
  16269.  
  16270.                                   %@AI@%x1%@AE@%, %@AI@%y1%@AE@%      Upper left corner of 
  16271.                                               bounding rectangle (view  
  16272.  
  16273.                                   coordinates)
  16274.  
  16275.                                   %@AI@%x2%@AE@%, %@AI@%y2%@AE@%      Lower right corner of 
  16276.                                               bounding rectangle (view  
  16277.  
  16278.                                   coordinates)
  16279.  
  16280.                                   %@AI@%wx1, wy1%@AE@%    Upper left corner of 
  16281.                                               bounding rectangle (window  
  16282.  
  16283.                                   coordinates)
  16284.  
  16285.                                   %@AI@%wx%@AE@%2,%@AI@% wy2%@AE@%    Lower right corner of 
  16286.                                               bounding rectangle  (window
  16287.  
  16288.                                   coordinates)
  16289.  
  16290.                                   %@AI@%pwxy1%@AE@%       Upper left corner of 
  16291.                                               bounding rectangle (window
  16292.  
  16293.                                   coordinates)
  16294.  
  16295.                                   %@AI@%pwxy2%@AE@%       Lower right corner of 
  16296.                                               bounding rectangle (window
  16297.  
  16298.                                   coordinates)
  16299.  
  16300. %@AB@%Returns%@AE@%                           A nonzero value if the ellipse is drawn 
  16301.                                   successfully,  0 if not
  16302.  
  16303. ────────────────────────────────────────────────────────────────────────────
  16304. %@AB@%_floodfill %@AE@% - Fills an area of the screen with the current color.%@NL@%
  16305. %@NL@%
  16306. %@AB@%Include%@AE@%                           GRAPH.H
  16307.  
  16308. %@AB@%Prototypes%@AE@%                        %@AB@%short _far _floodfill( short %@AE@%%@AI@%x%@AE@%%@AB@%, short  %@AE@%%@AI@%y%@AE@%
  16309.                                   %@AB@%, short %@AE@%%@AI@%boundary %@AE@%%@AB@%);%@AE@%
  16310.  
  16311.  
  16312.  
  16313.                                   short _far _floodfill_w( double %@AI@%wx%@AE@%%@AB@%, %@AE@%
  16314.                                   %@AB@%double %@AE@%%@AI@%wy%@AE@%%@AB@%, short  %@AE@%%@AI@%boundary %@AE@%%@AB@%);%@AE@%
  16315.  
  16316. %@AB@%Arguments%@AE@%                         %@AI@%x, y%@AE@%        Start point (view 
  16317.                                               coordinates)%@AB@%%@AE@%
  16318.  
  16319.                                   %@AI@%wx%@AE@%, %@AI@%wy%@AE@%      Start point (window 
  16320.                                               coordinates)
  16321.  
  16322.                                   %@AI@%boundary%@AE@%    Boundary color
  16323.  
  16324. %@AB@%Returns%@AE@%                           A nonzero value if successful, 0 if not
  16325.  
  16326. ────────────────────────────────────────────────────────────────────────────
  16327. %@AB@%_getpixel %@AE@% - Obtains a pixel's color index. The coordinates can be specified
  16328. in either view coordinates (%@AB@%_getpixel%@AE@%) or in window coordinates
  16329. (%@AB@%_getpixel_w%@AE@%).%@NL@%
  16330. %@NL@%
  16331. %@AB@%Include%@AE@%                           GRAPH.H
  16332.  
  16333. %@AB@%Prototypes%@AE@%                        %@AB@%short _far _getpixel( short %@AE@%%@AI@%x%@AE@%%@AB@%, short  %@AE@%%@AI@%y %@AE@%
  16334.                                   %@AB@%);%@AE@%
  16335.  
  16336.  
  16337.  
  16338.                                   short _far _getpixel_w( double %@AI@%wx%@AE@%%@AB@%, %@AE@%
  16339.                                   %@AB@%double %@AE@%%@AI@%wy%@AE@%%@AB@% );%@AE@%
  16340.  
  16341. %@AB@%Arguments%@AE@%                         %@AI@%x%@AE@%,%@AI@% y%@AE@%        Pixel position (view 
  16342.                                               coordinates)
  16343.  
  16344.                                   %@AI@%wx, wy%@AE@%      Pixel position (window 
  16345.                                               coordinates)
  16346.  
  16347. %@AB@%Returns%@AE@%                           The color index if successful, -1 if not
  16348.  
  16349. ────────────────────────────────────────────────────────────────────────────
  16350. %@AB@%_lineto %@AE@% - Draws a line from the current graphic output position to a
  16351. specified point. The coordinate of the end point can be specified in either
  16352. view coordinates (%@AB@%_lineto%@AE@%) or in window coordinates (%@AB@%_lineto_w%@AE@%).%@NL@%
  16353. %@NL@%
  16354. %@AB@%Include%@AE@%                           GRAPH.H
  16355.  
  16356. %@AB@%Prototypes%@AE@%                        %@AB@%short _far _lineto( short %@AE@%%@AI@%x%@AE@%%@AB@%, short  %@AE@%%@AI@%y %@AE@%%@AB@%);%@AE@%
  16357.  
  16358.  
  16359.  
  16360.                                   short _far _lineto_w( double %@AI@%wx%@AE@%%@AB@%, double %@AE@%
  16361.                                   %@AI@%wy %@AE@%%@AB@%);%@AE@%
  16362.  
  16363. %@AB@%Arguments%@AE@%                         %@AI@%x%@AE@%, %@AI@%y%@AE@%        End point (view coordinates)
  16364.  
  16365.                                   %@AI@%wx%@AE@%,%@AI@% wy%@AE@%      End point (window 
  16366.                                               coordinates)
  16367.  
  16368. %@AB@%Returns%@AE@%                           A nonzero value if the line is drawn 
  16369.                                   successfully,  0 if not
  16370.  
  16371. ────────────────────────────────────────────────────────────────────────────
  16372. %@AB@%_moveto %@AE@% - Moves the current graphic-output position to a specified point.
  16373. The coordinates can be specified in either view coordinates (%@AB@%_moveto%@AE@%) or in
  16374. window coordinates (%@AB@%_moveto_w%@AE@%).%@NL@%
  16375. %@NL@%
  16376. %@AB@%Include%@AE@%                           GRAPH.H
  16377.  
  16378. %@AB@%Prototypes%@AE@%                        %@AB@%struct xycoord _far _moveto( short %@AE@%%@AI@%x%@AE@%%@AB@%,  %@AE@%
  16379.                                   %@AB@%short );%@AE@%
  16380.  
  16381.  
  16382.  
  16383.                                   struct _wxycoord _far _moveto_w( double %@AI@%%@AE@%
  16384.                                   %@AI@%wx%@AE@%%@AB@%, double %@AE@%%@AI@%wy  %@AE@%%@AB@%);%@AE@%
  16385.  
  16386. %@AB@%Arguments%@AE@%                         %@AI@%x%@AE@%, %@AI@%y%@AE@%        Target position (view 
  16387.                                               coordinates)
  16388.  
  16389.                                   %@AI@%wx%@AE@%, %@AI@%wy%@AE@%      Target position (window 
  16390.                                               coordinates)
  16391.  
  16392. %@AB@%Returns%@AE@%                           The logical coordinates of the previous 
  16393.                                   position  as an %@AB@%xycoord%@AE@%
  16394.  
  16395.                                   structure (%@AB@%_moveto%@AE@%) or as a %@AB@%_wxycoord%@AE@% 
  16396.                                   structure (%@AB@%_moveto_w%@AE@%)
  16397.  
  16398. ────────────────────────────────────────────────────────────────────────────
  16399. %@AB@%_pie %@AE@% - Draws a figure shaped like a pie wedge.%@NL@%
  16400. %@NL@%
  16401. %@AB@%Include%@AE@%                           GRAPH.H
  16402.  
  16403. %@AB@%Prototypes%@AE@%                        %@AB@%short _far _pie( short %@AE@%%@AI@%control%@AE@%%@AB@%, short  %@AE@%%@AI@%%@AE@%
  16404.                                   %@AI@%x1%@AE@%%@AB@%, short %@AE@%%@AI@%y1%@AE@%%@AB@%,%@AE@%
  16405.  
  16406.                                   short %@AI@%x2%@AE@%%@AB@%, short %@AE@%%@AI@%y2%@AE@%%@AB@%, short %@AE@%%@AI@%x3%@AE@%%@AB@%, short %@AE@%%@AI@%y3%@AE@%%@AB@%, %@AE@%
  16407.                                   %@AB@%short %@AE@%%@AI@%x4%@AE@%%@AB@%, short %@AE@%%@AI@%y%@AE@%%@AB@%4 );%@AE@%
  16408.  
  16409.  
  16410.  
  16411.                                   short _far _pie_wxy( short %@AI@%control%@AE@%%@AB@%, %@AE@%
  16412.                                   %@AB@%struct _wxycoord*  %@AE@%%@AI@%pwxy1%@AE@%%@AB@%,%@AE@%
  16413.  
  16414.                                   struct _wxycoord*%@AI@%pwxy2%@AE@%%@AB@%, struct %@AE@%
  16415.                                   %@AB@%_wxycoord* %@AE@%%@AI@%pwxy3%@AE@%%@AB@%,%@AE@%
  16416.  
  16417.                                   struct _wxycoord*%@AI@%pwxy4 %@AE@%%@AB@%);%@AE@%
  16418.  
  16419. %@AB@%Arguments%@AE@%                         %@AI@%control%@AE@%     Fill flag (%@AB@%_GFILLINTERIOR%@AE@%,  %@AB@%%@AE@%
  16420.                                               %@AB@%_GBORDER%@AE@%)
  16421.  
  16422.                                   %@AI@%x1, y1%@AE@%      Upper left corner of 
  16423.                                               bounding rectangle (view 
  16424.                                               coordinates)
  16425.  
  16426.                                   %@AI@%x2%@AE@%, %@AI@%y2%@AE@%      Lower right corner of 
  16427.                                               bounding rectangle (view  
  16428.                                               coordinates)
  16429.  
  16430.                                   %@AI@%x3%@AE@%, %@AI@%y3%@AE@%      Start vector(view 
  16431.                                               coordinates)
  16432.  
  16433.                                   %@AI@%x4%@AE@%,%@AI@% y4%@AE@%      End vector (view coordinates)
  16434.  
  16435.                                   %@AI@%pwxy1%@AE@%       Upper left corner of 
  16436.                                               bounding rectangle (window 
  16437.                                               coordinates)
  16438.  
  16439.                                   %@AI@%pwxy2%@AE@%       Lower right corner of 
  16440.                                               bounding rectangle (window 
  16441.                                               coordinates)
  16442.  
  16443.                                   %@AI@%pwxy3%@AE@%       Start vector (window 
  16444.                                               coordinates)
  16445.  
  16446.                                   %@AI@%pwxy4%@AE@%       End vector (window 
  16447.                                               coordinates)
  16448.  
  16449. %@AB@%Returns%@AE@%                           A nonzero value if the pie is drawn 
  16450.                                   successfully,  0 if not
  16451.  
  16452.  
  16453.  
  16454. ────────────────────────────────────────────────────────────────────────────
  16455. %@AB@%_rectangle %@AE@% - Draws a rectangle.%@NL@%
  16456. %@NL@%
  16457. %@AB@%Include%@AE@%                           GRAPH.H
  16458.  
  16459. %@AB@%Prototypes%@AE@%                        %@AB@%short _far _rectangle( short %@AE@%%@AI@%control%@AE@%%@AB@%,  %@AE@%
  16460.                                   %@AB@%short %@AE@%%@AI@%x1%@AE@%%@AB@%, short %@AE@%%@AI@%y1%@AE@%%@AB@%,%@AE@%
  16461.  
  16462.                                   short %@AI@%x2%@AE@%%@AB@%, short %@AE@%%@AI@%y2 %@AE@%%@AB@%);%@AE@%
  16463.  
  16464.  
  16465.  
  16466.                                   short _far _rectangle_w( short%@AI@% control%@AE@%%@AB@%, %@AE@%
  16467.                                   %@AB@%double %@AE@%%@AI@%wx1%@AE@%%@AB@%,  double %@AE@%%@AI@%wy1%@AE@%%@AB@%,%@AE@%
  16468.  
  16469.                                   double %@AI@%wx2%@AE@%%@AB@%, double %@AE@%%@AI@%wy2 %@AE@%%@AB@%);%@AE@%
  16470.  
  16471.  
  16472.  
  16473.                                   short _far _rectangle_wxy( short %@AI@%control%@AE@%
  16474.                                   %@AB@%, struct _wxycoord*%@AE@%%@AI@%pwxy1%@AE@%%@AB@%,%@AE@%
  16475.  
  16476.                                   struct _wxycoord*%@AI@%pwxy2%@AE@%%@AB@% );%@AE@%
  16477.  
  16478. %@AB@%Arguments%@AE@%                         %@AI@%control%@AE@%     Fill flag (%@AB@%_GFILLINTERIOR%@AE@%,  %@AB@%%@AE@%
  16479.                                               %@AB@%_GBORDER%@AE@%)
  16480.  
  16481.                                   %@AI@%x1%@AE@%,%@AI@% y1%@AE@%      Upper left corner (view 
  16482.                                               coordinates)
  16483.  
  16484.                                   %@AI@%x2%@AE@%,%@AI@% y2%@AE@%      Lower right corner (view 
  16485.                                               coordinates)
  16486.  
  16487.                                   %@AI@%wx1%@AE@%, %@AI@%wy1%@AE@%    Upper left corner (window 
  16488.                                               coordinates)
  16489.  
  16490.                                   %@AI@%wx2%@AE@%, %@AI@%wy2%@AE@%    Lower right corner (window 
  16491.                                               coordinates)
  16492.  
  16493.                                   %@AI@%pwxy1%@AE@%       Upper left corner (window 
  16494.                                               coordinates)
  16495.  
  16496.                                   %@AI@%pwxy2%@AE@%       Lower right corner (window 
  16497.                                               coordinates)
  16498.  
  16499. %@AB@%Returns%@AE@%                           A nonzero value if the rectangle is 
  16500.                                   drawn successfully,  0 if not
  16501.  
  16502. ────────────────────────────────────────────────────────────────────────────
  16503. %@AB@%_setpixel %@AE@% - Sets a pixel's color index.%@NL@%
  16504. %@NL@%
  16505. %@AB@%Include%@AE@%                           GRAPH.H
  16506.  
  16507. %@AB@%Prototypes%@AE@%                        %@AB@%short _far _setpixel( short %@AE@%%@AI@%x%@AE@%%@AB@%, short  %@AE@%%@AI@%y %@AE@%
  16508.                                   %@AB@%);%@AE@%
  16509.  
  16510.  
  16511.  
  16512.                                   short _far _setpixel_w( double %@AI@%wx%@AE@%%@AB@%, %@AE@%
  16513.                                   %@AB@%double%@AE@%%@AI@% wy%@AE@%%@AB@% );%@AE@%
  16514.  
  16515. %@AB@%Arguments%@AE@%                         %@AI@%x%@AE@%, %@AI@%y%@AE@%        Target pixel (view 
  16516.                                               coordinates)
  16517.  
  16518.                                   %@AI@%wx%@AE@%, %@AI@%wy%@AE@%      Target pixel (window 
  16519.                                               coordinates)
  16520.  
  16521. %@AB@%Returns%@AE@%                           The pixel's previous value if successful,
  16522.                                   -1  if not
  16523.  
  16524. %@NL@%
  16525. %@4@%%@AB@%Output Text%@AE@%%@EH@%%@NL@%
  16526. %@NL@%
  16527. These routines provide text output in both graphics and text modes.  %@NL@%
  16528. %@NL@%
  16529. These functions recognize text window boundaries and should be used in
  16530. applications using text windows.  %@NL@%
  16531. %@NL@%
  16532. No formatting capability is provided. If you want to output integer or
  16533. floating-point values, you must convert the values into a string variable
  16534. before calling these routines. All screen positions are specified as
  16535. character-row and character-column coordinates.  %@NL@%
  16536. %@NL@%
  16537. ────────────────────────────────────────────────────────────────────────────
  16538. %@AB@%_displaycursor%@AE@% - Sets the cursor "on" or "off" on exit from a graphics
  16539. routine.%@NL@%
  16540. %@NL@%
  16541. %@AB@%Include%@AE@%                           GRAPH.H
  16542.  
  16543. %@AB@%Prototype%@AE@%                         %@AB@%short _far _displaycursor( short %@AE@%%@AI@%toggle %@AE@%
  16544.                                   %@AI@%%@AE@%%@AB@%);%@AE@%
  16545.  
  16546. %@AB@%Argument%@AE@%                          %@AI@%toggle%@AE@%      Cursor state (%@AB@%_GCURSORON%@AE@%,%@AB@%  %@AE@%
  16547.                                               %@AB@%_GCURSOROFF%@AE@%)
  16548.  
  16549. %@AB@%Returns%@AE@%                           The previous value of %@AI@%toggle%@AE@%
  16550.  
  16551. ────────────────────────────────────────────────────────────────────────────
  16552. %@AB@%_gettextcolor%@AE@% - Obtains the current text color.%@NL@%
  16553. %@NL@%
  16554. %@AB@%Include%@AE@%                           GRAPH.H
  16555.  
  16556. %@AB@%Prototype%@AE@%                         %@AB@%short _far _gettextcolor( void );%@AE@%
  16557.  
  16558. %@AB@%Arguments%@AE@%                         None
  16559.  
  16560. %@AB@%Returns%@AE@%                           The color index of the current text 
  16561.                                   color
  16562.  
  16563. ────────────────────────────────────────────────────────────────────────────
  16564. %@AB@%_gettextcursor%@AE@% - Obtains the current cursor attribute.%@NL@%
  16565. %@NL@%
  16566. %@AB@%Include%@AE@%                           GRAPH.H
  16567.  
  16568. %@AB@%Prototype%@AE@%                         %@AB@%short _far _gettextcursor( void );%@AE@%
  16569.  
  16570. %@AB@%Arguments%@AE@%                         None
  16571.  
  16572. %@AB@%Returns%@AE@%                           The current cursor attribute
  16573.  
  16574. ────────────────────────────────────────────────────────────────────────────
  16575. %@AB@%_gettextposition%@AE@% - Obtains the current text-output position.%@NL@%
  16576. %@NL@%
  16577. %@AB@%Include%@AE@%                           GRAPH.H
  16578.  
  16579. %@AB@%Prototype%@AE@%                         %@AB@%struct rccoord _far _gettextposition( %@AE@%
  16580.                                   %@AB@%void  );%@AE@%
  16581.  
  16582. %@AB@%Arguments%@AE@%                         None
  16583.  
  16584. %@AB@%Returns%@AE@%                           The current text position as an %@AB@%rccoord%@AE@% 
  16585.                                   structure
  16586.  
  16587. ────────────────────────────────────────────────────────────────────────────
  16588. %@AB@%_outtext%@AE@% - Outputs text to the screen at the current position.%@NL@%
  16589. %@NL@%
  16590. %@AB@%Include%@AE@%                           GRAPH.H
  16591.  
  16592. %@AB@%Prototype%@AE@%                         %@AB@%void _far _outtext( unsigned char _far *%@AE@%
  16593.                                   %@AI@%text  %@AE@%%@AB@%);%@AE@%
  16594.  
  16595. %@AB@%Argument%@AE@%                          %@AI@%text%@AE@%        Text to be output
  16596.  
  16597. %@AB@%Returns%@AE@%                           Void
  16598.  
  16599. ────────────────────────────────────────────────────────────────────────────
  16600. %@AB@%_settextposition%@AE@% - Relocates the current text position.%@NL@%
  16601. %@NL@%
  16602. %@AB@%Include%@AE@%                           GRAPH.H
  16603.  
  16604. %@AB@%Prototype%@AE@%                         %@AB@%struct rccoord _far _settextposition( %@AE@%
  16605.                                   %@AB@%short  %@AE@%%@AI@%row%@AE@%%@AB@%, short %@AE@%%@AI@%col %@AE@%%@AB@%);%@AE@%
  16606.  
  16607. %@AB@%Arguments%@AE@%                         %@AI@%row%@AE@%         Row coordinate of new output
  16608.                                               position
  16609.  
  16610.                                   %@AI@%col%@AE@%         Column coordinate of new 
  16611.                                               output position
  16612.  
  16613. %@AB@%Returns%@AE@%                           The previous text position in an%@AB@% rccoord%@AE@%
  16614.                                   structure
  16615.  
  16616. ────────────────────────────────────────────────────────────────────────────
  16617. %@AB@%_settextcolor%@AE@% - Sets the current text color.%@NL@%
  16618. %@NL@%
  16619. %@AB@%Include%@AE@%                           GRAPH.H
  16620.  
  16621. %@AB@%Prototype%@AE@%                         %@AB@%short _far _settextcolor( short %@AE@%%@AI@%index  %@AE@%%@AB@%%@AE@%
  16622.                                   %@AB@%);%@AE@%
  16623.  
  16624. %@AB@%Argument%@AE@%                          %@AI@%index%@AE@%       Desired color index
  16625.  
  16626. %@AB@%Returns%@AE@%                           The value of the previous color
  16627.  
  16628. ────────────────────────────────────────────────────────────────────────────
  16629. %@AB@%_settextwindow%@AE@% - Sets the current text-display window.%@NL@%
  16630. %@NL@%
  16631. %@AB@%Include%@AE@%                           GRAPH.H
  16632.  
  16633. %@AB@%Prototype%@AE@%                         %@AB@%void _far _settextwindow( short %@AE@%%@AI@%r1%@AE@%%@AB@%,  %@AE@%
  16634.                                   %@AB@%short %@AE@%%@AI@%c1%@AE@%%@AB@%, short %@AE@%%@AI@%r2%@AE@%%@AB@%, short %@AE@%%@AI@%c2 %@AE@%%@AB@%);%@AE@%
  16635.  
  16636. %@AB@%Arguments%@AE@%                         %@AI@%r1%@AE@%,%@AI@% c1%@AE@%      Upper left corner of window
  16637.  
  16638.                                   %@AI@%r2%@AE@%, %@AI@%c2%@AE@%      Lower right corner of window
  16639.  
  16640. %@AB@%Returns%@AE@%                           Void
  16641.  
  16642. %@NL@%
  16643. %@4@%%@AB@%Transfer Images%@AE@%%@EH@%%@NL@%
  16644. %@NL@%
  16645. These functions transfer screen images between memory and the display, using
  16646. a buffer allocated by the application. You can use these functions to
  16647. animate graphics elements on the screen.  %@NL@%
  16648. %@NL@%
  16649. Most of these routines have two or three forms. The functions are listed by
  16650. the "base" name, without a suffix. Note, though, that function names that
  16651. end in a %@AB@%_w%@AE@%, such as %@AB@%_getimage_w%@AE@%, use the window-coordinate system. Those
  16652. that end with a %@AB@%_wxy%@AE@%, such as %@AB@%_imagesize_wxy%@AE@%, use the window-coordinate
  16653. system and a %@AB@%_wxycoord%@AE@% structure to define the coordinates.  %@NL@%
  16654. %@NL@%
  16655. The %@AB@%_imagesize%@AE@% function is used to find the size in bytes of the buffer
  16656. needed to store a given image.  %@NL@%
  16657. %@NL@%
  16658. ────────────────────────────────────────────────────────────────────────────
  16659. %@AB@%_getimage%@AE@% - Stores a screen image in memory.%@NL@%
  16660. %@NL@%
  16661. %@AB@%Include%@AE@%                           GRAPH.H
  16662.  
  16663. %@AB@%Prototypes%@AE@%                        %@AB@%void _far _getimage( short %@AE@%%@AI@%x1%@AE@%%@AB@%, short  %@AE@%%@AI@%y1%@AE@%
  16664.                                   %@AB@%,%@AE@%
  16665.  
  16666.                                   short %@AI@%x2%@AE@%%@AB@%, short %@AE@%%@AI@%y2%@AE@%%@AB@%, char _huge *%@AE@%%@AI@%image%@AE@%%@AB@%  %@AE@%
  16667.                                   %@AB@%);%@AE@%
  16668.  
  16669.  
  16670.  
  16671.                                   void _far _getimage_w( double %@AI@%wx1%@AE@%%@AB@%, %@AE@%
  16672.                                   %@AB@%double %@AE@%%@AI@%wy1%@AE@%%@AB@%,%@AE@%
  16673.  
  16674.                                   double %@AI@%wx2%@AE@%%@AB@%, double %@AE@%%@AI@%wy2%@AE@%%@AB@%, char _huge *%@AE@%%@AI@%%@AE@%
  16675.                                   %@AI@%image  %@AE@%%@AB@%);%@AE@%
  16676.  
  16677.  
  16678.  
  16679.                                   void _far _getimage_wxy( struct 
  16680.                                   _wxycoord*%@AI@%pwxy1%@AE@%%@AB@%,%@AE@%
  16681.  
  16682.                                   struct _wxycoord*%@AI@%pwxy2%@AE@%%@AB@%, char _huge *%@AE@%%@AI@%%@AE@%
  16683.                                   %@AI@%image  %@AE@%%@AB@%);%@AE@%
  16684.  
  16685. %@AB@%Arguments%@AE@%                         %@AI@%x1%@AE@%,%@AI@% y1%@AE@%      Upper left corner of 
  16686.                                               bounding  rectangle (view 
  16687.                                               coordinates)
  16688.  
  16689.                                   %@AI@%x2%@AE@%, %@AI@%y2%@AE@%      Lower right corner of 
  16690.                                               bounding rectangle (view  
  16691.                                               coordinates)
  16692.  
  16693.                                   %@AI@%wx1%@AE@%,%@AI@% wy1%@AE@%    Upper left corner of 
  16694.                                               bounding rectangle (window  
  16695.                                               coordinates)
  16696.  
  16697.                                   %@AI@%wx2%@AE@%, %@AI@%wy2%@AE@%    Lower right corner of 
  16698.                                               bounding rectangle  (window 
  16699.                                               coordinates)
  16700.  
  16701.                                   %@AI@%pwxy1%@AE@%       Upper left corner of 
  16702.                                               bounding rectangle (window 
  16703.                                               coordinates)
  16704.  
  16705.                                   %@AI@%pwxy2%@AE@%       Lower right corner of 
  16706.                                               bounding rectangle (window 
  16707.                                               coordinates)
  16708.  
  16709.                                   %@AI@%image%@AE@%       Storage buffer for screen 
  16710.                                               image
  16711.  
  16712. %@AB@%Returns%@AE@%                           Void
  16713.  
  16714. ────────────────────────────────────────────────────────────────────────────
  16715. %@AB@%_imagesize%@AE@% - Returns image size in bytes.%@NL@%
  16716. %@NL@%
  16717. %@AB@%Include%@AE@%                           GRAPH.H
  16718.  
  16719. %@AB@%Prototypes%@AE@%                        %@AB@%long _far _imagesize( short %@AE@%%@AI@%x1%@AE@%%@AB@%, short  %@AE@%%@AI@%%@AE@%
  16720.                                   %@AI@%y1%@AE@%%@AB@%, short %@AE@%%@AI@%x2%@AE@%%@AB@%, short %@AE@%%@AI@%y2 %@AE@%%@AB@%);%@AE@%
  16721.  
  16722.  
  16723.  
  16724.                                   long _far _imagesize_w( double %@AI@%wx1%@AE@%%@AB@%, %@AE@%
  16725.                                   %@AB@%double %@AE@%%@AI@%wy1%@AE@%%@AB@%, double  %@AE@%%@AI@%wx2%@AE@%%@AB@%,%@AE@%
  16726.  
  16727.                                   double %@AI@%wy2 %@AE@%%@AB@%);%@AE@%
  16728.  
  16729.  
  16730.  
  16731.                                   long _far _imagesize_wxy( struct 
  16732.                                   _wxycoord* %@AI@%pwxy1%@AE@%%@AB@%,%@AE@%
  16733.  
  16734.                                   struct _wxycoord* %@AI@%pwxy2 %@AE@%%@AB@%);%@AE@%
  16735.  
  16736. %@AB@%Arguments%@AE@%                         %@AI@%x1%@AE@%, %@AI@%y1%@AE@%      Upper left corner of 
  16737.                                               bounding  rectangle (view 
  16738.                                               coordinates)
  16739.  
  16740.                                   %@AI@%x2%@AE@%,%@AI@% y2%@AE@%      Lower right corner of 
  16741.                                               bounding rectangle (view  
  16742.                                               coordinates)
  16743.  
  16744.                                   %@AI@%wx1%@AE@%,%@AI@% wy1%@AE@%    Upper left corner of 
  16745.                                               bounding rectangle (window  
  16746.                                               coordinates)
  16747.  
  16748.                                   %@AI@%wx2%@AE@%,%@AI@% wy2%@AE@%    Lower right corner of 
  16749.                                               bounding rectangle  (window 
  16750.                                               coordinates)
  16751.  
  16752.                                   %@AI@%pwxy1%@AE@%       Upper left corner of 
  16753.                                               bounding rectangle (window 
  16754.                                               coordinates)
  16755.  
  16756.                                   %@AI@%pwxy2%@AE@%       Lower right corner of 
  16757.                                               bounding rectangle (window 
  16758.                                               coordinates)
  16759.  
  16760. %@AB@%Returns%@AE@%                           The storage size of the image in bytes
  16761.  
  16762. ────────────────────────────────────────────────────────────────────────────
  16763. %@AB@%_putimage%@AE@% - Retrieves an image from memory and displays it.%@NL@%
  16764. %@NL@%
  16765. %@AB@%Include%@AE@%                           GRAPH.H
  16766.  
  16767. %@AB@%Prototypes%@AE@%                        %@AB@%void _far _putimage( short %@AE@%%@AI@%x%@AE@%%@AB@%, short  %@AE@%%@AI@%y%@AE@%%@AB@%,%@AE@%
  16768.  
  16769.                                   char _huge *%@AI@%image%@AE@%%@AB@%, short %@AE@%%@AI@%action%@AE@%%@AB@% );%@AE@%
  16770.  
  16771.  
  16772.  
  16773.                                   void _far _putimage_w( double %@AI@%wx%@AE@%%@AB@%, double%@AE@%
  16774.                                   %@AB@%%@AE@%%@AI@%wy%@AE@%%@AB@%,%@AE@%
  16775.  
  16776.                                   char _huge *%@AI@%image%@AE@%%@AB@%, short %@AE@%%@AI@%action %@AE@%%@AB@%);%@AE@%
  16777.  
  16778. %@AB@%Arguments%@AE@%                         %@AI@%x, y%@AE@%        Position of upper left 
  16779.                                               corner of  image (view 
  16780.                                               coordinates)
  16781.  
  16782.                                   %@AI@%wx%@AE@%,%@AI@% wy%@AE@%      Position of upper left 
  16783.                                               corner of image (window  
  16784.                                               coordinates)
  16785.  
  16786.                                   %@AI@%image%@AE@%       Stored image buffer
  16787.  
  16788.                                   %@AI@%action%@AE@%      Interaction with existing 
  16789.                                               screen image (%@AB@%_GAND%@AE@%,  %@AB@% _GOR,%@AE@%
  16790.  
  16791.                                   _GXOR, %@AB@%_GPSET%@AE@%, %@AB@%_GPRESET%@AE@%)
  16792.  
  16793. %@AB@%Returns%@AE@%                           Void
  16794.  
  16795. %@NL@%
  16796. %@2@%%@CR:C6A-B0008   @%%@AB@%Graphics 2: Presentation Graphics Routines%@AE@%%@EH@%%@NL@%
  16797. %@NL@%
  16798. The presentation graphics routines provide complete charting capabilities
  16799. for line, bar, column, scatter, and pie charts.  %@NL@%
  16800. %@NL@%
  16801. Some charts plot both "categories," or non-numeric data such as time
  16802. periods, and "values," or specific numeric data, such as sales. Presentation
  16803. graphics routines support the following kinds of charts:  %@NL@%
  16804. %@NL@%
  16805. %@AB@%Chart Name%@AE@%                        %@AB@%Description%@AE@%
  16806. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  16807. Line                              Category/value chart, with styles for 
  16808.                                   lines between points and for no lines
  16809.  
  16810. Bar                               Category/value chart, horizontal bars, 
  16811.                                   styles for stacked and unstacked 
  16812.                                   multiple series charts
  16813.  
  16814. Column                            Category/value chart, vertical bars, 
  16815.                                   with styles for stacked and unstacked 
  16816.                                   multiple series charts
  16817.  
  16818. Scatter                           Value/value chart, with styles for lines
  16819.                                   connecting points or for no lines
  16820.  
  16821. Pie                               Pie chart, with optional percentages and
  16822.                                   exploded sections
  16823.  
  16824. The graphics package supports the IBM (and compatible) Enhanced Graphics
  16825. Adapter (EGA), Color Graphics Adapter (CGA), certain operating modes of the
  16826. Video Graphics Array (VGA) hardware configurations, and the Multicolor
  16827. Graphics Array (MCGA). The graphics package also supports the Hercules
  16828. Graphics Card, Graphics Card Plus, InColor Card, and 100-percent compatible
  16829. cards, as well as the special Olivetti modes available on AT&T computers.  %@NL@%
  16830. %@NL@%
  16831. The %@AB@%_pg_initchart%@AE@% and %@AB@%_pg_defaultchart%@AE@% functions are generally used at the
  16832. very beginning of a presentation graphics program.  %@NL@%
  16833. %@NL@%
  16834. The %@AB@%_pg_chart%@AE@% functions produce column charts, line charts, and bar charts.
  16835. The %@AB@%_pg_chartscatter%@AE@% functions produce a scatter plot of data. The
  16836. %@AB@%_pg_chartpie%@AE@% function generates a pie chart.  %@NL@%
  16837. %@NL@%
  16838. ────────────────────────────────────────────────────────────────────────────
  16839. %@AB@%_pg_chart%@AE@% - Generates a chart of the type specified in the %@AI@%env%@AE@% environment
  16840. variable. It produces a column, bar, or line chart for a single series of
  16841. data.%@NL@%
  16842. %@NL@%
  16843. %@AB@%Include%@AE@%                           PGCHART.H
  16844.  
  16845. %@AB@%Prototype%@AE@%                         %@AB@%short _far _pg_chart( chartenv _far*%@AE@%%@AI@%env%@AE@%%@AB@%,%@AE@%
  16846.                                   %@AB@%%@AE@%
  16847.  
  16848.                                   char _far*_far *%@AI@%categories%@AE@%%@AB@%, float _far*%@AE@%%@AI@%%@AE@%
  16849.                                   %@AI@%values%@AE@%%@AB@%,  short %@AE@%%@AI@%n %@AE@%%@AB@%);%@AE@%
  16850.  
  16851. %@AB@%Arguments%@AE@%                         %@AI@%env%@AE@%         Chart environment variable
  16852.  
  16853.                                   %@AI@%categories%@AE@%  Array of category variables
  16854.  
  16855.                                   %@AI@%values%@AE@%      Array of data values
  16856.  
  16857.                                   %@AI@%n%@AE@%           Number of data values to 
  16858.                                               chart
  16859.  
  16860. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  16861.  
  16862. ────────────────────────────────────────────────────────────────────────────
  16863. %@AB@%_pg_chartms%@AE@% - Generates a multiple series of charts of the type specified in
  16864. the %@AI@%env%@AE@% environment variable. It produces a column, bar, or line chart for a
  16865. multiple series of data. All series must be the same length.%@NL@%
  16866. %@NL@%
  16867. %@AB@%Include%@AE@%                           PGCHART.H
  16868.  
  16869. %@AB@%Prototype%@AE@%                         %@AB@%short _far _pg_chartms( chartenv _far *%@AE@%%@AI@%%@AE@%
  16870.                                   %@AI@%env%@AE@%%@AB@%,  %@AE@%
  16871.  
  16872.                                   char _far * _far*%@AI@%categories%@AE@%%@AB@%, float _far*%@AE@%
  16873.                                   %@AI@%values%@AE@%%@AB@%,  short %@AE@%%@AI@%n%@AE@%%@AB@%, short %@AE@%%@AI@%nseries%@AE@%%@AB@%,%@AE@%
  16874.  
  16875.                                   short %@AI@%arraydim%@AE@%%@AB@%, char _far* _far*%@AE@%%@AI@%%@AE@%
  16876.                                   %@AI@%serieslabels  %@AE@%%@AB@%);%@AE@%
  16877.  
  16878. %@AB@%Arguments%@AE@%                         %@AI@%env%@AE@%         Chart environment variable
  16879.  
  16880.                                   %@AI@%categories%@AE@%  Array of category variables
  16881.  
  16882.                                   %@AI@%values%@AE@%      Two-dimensional array of 
  16883.                                               data values (series, data)
  16884.  
  16885.                                   %@AI@%n%@AE@%           Number of data values to 
  16886.                                               chart in a series
  16887.  
  16888.                                   %@AI@%nseries%@AE@%     Number of series to chart
  16889.  
  16890.                                   %@AI@%arraydim%@AE@%    Second (row) dimension of 
  16891.                                               data array
  16892.  
  16893.                                   %@AI@%serieslabe%@AE@%  Array of labels for series
  16894.                                   %@AI@%ls%@AE@%          
  16895.  
  16896. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  16897.  
  16898. ────────────────────────────────────────────────────────────────────────────
  16899. %@AB@%_pg_chartpie%@AE@% - Generates a pie chart for a single series of data.%@NL@%
  16900. %@NL@%
  16901. %@AB@%Include%@AE@%                           PGCHART.H
  16902.  
  16903. %@AB@%Prototype%@AE@%                         %@AB@%short _far _pg_chartpie( chartenv _far*%@AE@%%@AI@%%@AE@%
  16904.                                   %@AI@%env%@AE@%%@AB@%,  %@AE@%
  16905.  
  16906.                                   char _far* _far*%@AI@%categories%@AE@%%@AB@%, float _far*%@AE@%%@AI@%%@AE@%
  16907.                                   %@AI@%values%@AE@%%@AB@%,  %@AE@%
  16908.  
  16909.                                   short _far*%@AI@%explode%@AE@%%@AB@%, short %@AE@%%@AI@%n %@AE@%%@AB@%);%@AE@%
  16910.  
  16911. %@AB@%Arguments%@AE@%                         %@AI@%env%@AE@%         Chart environment variable
  16912.  
  16913.                                   %@AI@%categories%@AE@%  Array of category names
  16914.  
  16915.                                   %@AI@%values%@AE@%      Array of data values
  16916.  
  16917.                                   %@AI@%explode %@AE@%    Array of explode flags; 
  16918.                                               1=explode, 0=do not explode
  16919.  
  16920.                                   %@AI@%n%@AE@%           Number of data values to 
  16921.                                               chart
  16922.  
  16923. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  16924.  
  16925. ────────────────────────────────────────────────────────────────────────────
  16926. %@AB@%_pg_chartscatter%@AE@% - Generates a scatter chart for a single series of data. %@NL@%
  16927. %@NL@%
  16928. %@AB@%Include%@AE@%                           PGCHART.H
  16929.  
  16930. %@AB@%Prototype%@AE@%                         %@AB@%short _far _pg_chartscatter( chartenv %@AE@%
  16931.                                   %@AB@%_far  *%@AE@%%@AI@%env%@AE@%%@AB@%, float _far *%@AE@%%@AI@%xvalues%@AE@%%@AB@%,%@AE@%
  16932.  
  16933.                                   float _far *%@AI@%yvalues%@AE@%%@AB@%, short %@AE@%%@AI@%n %@AE@%%@AB@%);%@AE@%
  16934.  
  16935. %@AB@%Arguments%@AE@%                         %@AI@%env%@AE@%         Chart environment variable
  16936.  
  16937.                                   %@AI@%xvalues%@AE@%     Array of %@AI@%x-%@AE@%axis data values
  16938.  
  16939.                                   %@AI@%yvalues%@AE@%     Array of%@AI@% y%@AE@%-axis data values
  16940.  
  16941.                                   %@AI@%n%@AE@%           Number of data values to 
  16942.                                               chart
  16943.  
  16944. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  16945.  
  16946. ────────────────────────────────────────────────────────────────────────────
  16947. %@AB@%_pg_chartscatterms%@AE@% - Generates a scatter chart for a multiple series of
  16948. data. %@NL@%
  16949. %@NL@%
  16950. %@AB@%Include%@AE@%                           PGCHART.H
  16951.  
  16952. %@AB@%Prototype%@AE@%                         %@AB@%short _far _pg_chartscatterms( chartenv %@AE@%
  16953.                                   %@AB@%_far  *%@AE@%%@AI@%env%@AE@%%@AB@%,%@AE@%
  16954.  
  16955.                                   float _far*%@AI@%xvalues%@AE@%%@AB@%, float _far*%@AE@%%@AI@%yvalues%@AE@%%@AB@%, %@AE@%
  16956.                                   %@AB@%short %@AE@%%@AI@%nseries%@AE@%%@AB@%, short %@AE@%%@AI@%n%@AE@%%@AB@%,%@AE@%
  16957.  
  16958.                                   short %@AI@%rowdim%@AE@%%@AB@%, char _far* _far *%@AE@%%@AI@%%@AE@%
  16959.                                   %@AI@%serieslabels  %@AE@%%@AB@%);%@AE@%
  16960.  
  16961. %@AB@%Arguments%@AE@%                         %@AI@%env%@AE@%         Chart environment variable
  16962.  
  16963.                                   %@AI@%xvalues%@AE@%     Two-dimensional array of %@AI@%x%@AE@%
  16964.                                               -axis values
  16965.  
  16966.                                   %@AI@%yvalues%@AE@%     Two-dimensional array of %@AI@%y%@AE@%
  16967.                                               -axis values
  16968.  
  16969.                                   %@AI@%n%@AE@%           Number of data values to 
  16970.                                               chart in a series
  16971.  
  16972.                                   %@AI@%nseries%@AE@%     Number of series to chart
  16973.  
  16974.                                   %@AI@%rowdim%@AE@%      Second (row) dimension of 
  16975.                                               data array
  16976.  
  16977.                                   %@AI@%serieslabe%@AE@%  Array of labels for series
  16978.                                   %@AI@%ls%@AE@%          
  16979.  
  16980. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  16981.  
  16982. ────────────────────────────────────────────────────────────────────────────
  16983. %@AB@%_pg_defaultchart%@AE@% - Initializes all necessary variables in the chart
  16984. environment for the specified default chart and chart style.%@NL@%
  16985. %@NL@%
  16986. %@AB@%Include%@AE@%                           PGCHART.H
  16987.  
  16988. %@AB@%Prototype%@AE@%                         %@AB@%short _far _pg_defaultchart( chartenv %@AE@%
  16989.                                   %@AB@%_far  *%@AE@%%@AI@%env%@AE@%%@AB@%, short %@AE@%%@AI@%charttype%@AE@%%@AB@%,%@AE@%
  16990.  
  16991.                                   short %@AI@%chartstyle %@AE@%%@AB@%);%@AE@%
  16992.  
  16993. %@AB@%Arguments%@AE@%                         %@AI@%env%@AE@%         Chart environment variable
  16994.  
  16995.                                   %@AI@%charttype%@AE@%   Chart type (%@AB@%_PG_BARCHART%@AE@%,
  16996.  
  16997.                                   %@AB@%_PG_COLUMNCHART%@AE@%, %@AB@%_PG_LINECHART%@AE@%,
  16998.  
  16999.                                   %@AB@%_PG_SCATTERCHART%@AE@%, %@AB@%_PG_PIECHART)%@AE@%
  17000.  
  17001.                                   %@AI@%chartstyle%@AE@%  Chart style
  17002.  
  17003. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  17004.  
  17005. ────────────────────────────────────────────────────────────────────────────
  17006. %@AB@%_pg_initchart%@AE@% - Initializes chart line-style set, default palettes, screen
  17007. modes, and character fonts. You must call this routine before any other
  17008. charting routine.%@NL@%
  17009. %@NL@%
  17010. %@AB@%Include%@AE@%                           PGCHART.H
  17011.  
  17012. %@AB@%Prototype%@AE@%                         %@AB@%short _far _pg_initchart( void );%@AE@%
  17013.  
  17014. %@AB@%Arguments%@AE@%                         None
  17015.  
  17016. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  17017.  
  17018. %@NL@%
  17019. %@2@%%@CR:C6A-B0009   @%%@AB@%Graphics 3: Font Display Routines%@AE@%%@EH@%%@NL@%
  17020. %@NL@%
  17021. The font graphics routines display font-based characters on the screen.  %@NL@%
  17022. %@NL@%
  17023. The %@AB@%_registerfonts%@AE@% function initializes the fonts package with a set of
  17024. disk-based type fonts. This must be done at the very beginning of any fonts
  17025. program. The %@AB@%_unregisterfonts%@AE@% function frees fonts from memory when they are
  17026. no longer needed.  %@NL@%
  17027. %@NL@%
  17028. The %@AB@%_setfont%@AE@% function makes a specified font the current active font for
  17029. output. The %@AB@%_outgtext%@AE@% function displays text on the screen using the current
  17030. font.  %@NL@%
  17031. %@NL@%
  17032. ────────────────────────────────────────────────────────────────────────────
  17033. %@AB@%_getfontinfo%@AE@% - Obtains the current font characteristics.%@NL@%
  17034. %@NL@%
  17035. %@AB@%Include%@AE@%                           GRAPH.H
  17036.  
  17037. %@AB@%Prototype%@AE@%                         %@AB@%short _far _getfontinfo( struct %@AE@%
  17038.                                   %@AB@%_fontinfo  _far *%@AE@%%@AI@%fontbuffer%@AE@%%@AB@% );%@AE@%
  17039.  
  17040. %@AB@%Argument%@AE@%                          %@AI@%fontbuffer%@AE@%  Font information
  17041.  
  17042. %@AB@%Returns%@AE@%                           Font information as a %@AB@%_fontinfo%@AE@% 
  17043.                                   structure
  17044.  
  17045. ────────────────────────────────────────────────────────────────────────────
  17046. %@AB@%_getgtextextent%@AE@% - Determines the width of the specified text in the current
  17047. font.%@NL@%
  17048. %@NL@%
  17049. %@AB@%Include%@AE@%                           GRAPH.H
  17050.  
  17051. %@AB@%Prototype%@AE@%                         %@AB@%short _far _getgtextextent( unsigned %@AE@%
  17052.                                   %@AB@%char  _far * %@AE@%%@AI@%text %@AE@%%@AB@%);%@AE@%
  17053.  
  17054. %@AB@%Argument%@AE@%                          %@AI@%text%@AE@%        Text to be analyzed
  17055.  
  17056. %@AB@%Returns%@AE@%                           The width of the text in pixels
  17057.  
  17058. ────────────────────────────────────────────────────────────────────────────
  17059. %@AB@%_outgtext%@AE@% - Outputs text in the current font to the screen at the current
  17060. position.%@NL@%
  17061. %@NL@%
  17062. %@AB@%Include%@AE@%                           GRAPH.H
  17063.  
  17064. %@AB@%Prototype%@AE@%                         %@AB@%void _far _outgtext( unsigned char _far %@AE@%
  17065.                                   %@AB@%*%@AE@%%@AI@%text  %@AE@%%@AB@%);%@AE@%
  17066.  
  17067. %@AB@%Argument%@AE@%                          %@AI@%text%@AE@%        Text to be output
  17068.  
  17069. %@AB@%Returns%@AE@%                           Void
  17070.  
  17071. ────────────────────────────────────────────────────────────────────────────
  17072. %@AB@%_registerfonts%@AE@% - Initializes the font library.%@NL@%
  17073. %@NL@%
  17074. %@AB@%Include%@AE@%                           GRAPH.H
  17075.  
  17076. %@AB@%Prototype%@AE@%                         %@AB@%short _far _registerfonts( unsigned char%@AE@%
  17077.                                   %@AB@%_far *%@AE@%%@AI@%filename %@AE@%%@AB@%);%@AE@%
  17078.  
  17079. %@AB@%Argument%@AE@%                          %@AI@%file name%@AE@%   File name of .FON files to 
  17080.                                               register
  17081.  
  17082. %@AB@%Returns%@AE@%                           Void
  17083.  
  17084. ────────────────────────────────────────────────────────────────────────────
  17085. %@AB@%_setfont%@AE@% - Finds a single font that matches a specified set of
  17086. characteristics and makes this font the current font.%@NL@%
  17087. %@NL@%
  17088. %@AB@%Include%@AE@%                           GRAPH.H
  17089.  
  17090. %@AB@%Prototype%@AE@%                         %@AB@%short _far _setfont( unsigned char _far %@AE@%
  17091.                                   %@AB@%*%@AE@%%@AI@%options%@AE@%%@AB@%  );%@AE@%
  17092.  
  17093. %@AB@%Argument%@AE@%                          %@AI@%options%@AE@%     Font options string
  17094.  
  17095. %@AB@%Returns%@AE@%                           Void
  17096.  
  17097. ────────────────────────────────────────────────────────────────────────────
  17098. %@AB@%_unregisterfonts%@AE@% - Frees memory associated with fonts.%@NL@%
  17099. %@NL@%
  17100. %@AB@%Include%@AE@%                           GRAPH.H
  17101.  
  17102. %@AB@%Prototype%@AE@%                         %@AB@%void _far _unregisterfonts( void );%@AE@%
  17103.  
  17104. %@AB@%Arguments%@AE@%                         None
  17105.  
  17106. %@AB@%Returns%@AE@%                           Void
  17107.  
  17108. %@NL@%
  17109. %@2@%%@CR:C6A-B0010   @%%@AB@%Input and Output Routines%@AE@%%@EH@%%@NL@%
  17110. %@NL@%
  17111. The input and output (I/O) routines of the standard C library allow you to
  17112. read and write data to and from files and devices. In C, there are no
  17113. predefined file structures; all data is treated as sequences of bytes.  %@NL@%
  17114. %@NL@%
  17115. Three types of I/O functions are available:  %@NL@%
  17116. %@NL@%
  17117. %@NL@%
  17118.   ■   Stream I/O,  in which the data file is a stream of individual
  17119.       characters%@NL@%
  17120. %@NL@%
  17121.   ■   Low-level I/O, which uses the system's I/O capabilities directly%@NL@%
  17122. %@NL@%
  17123.   ■   Console and port I/O, which are stream routines for console or port%@NL@%
  17124. %@NL@%
  17125. %@NL@%
  17126. Stream I/O uses the %@AB@%FILE%@AE@% structure. The stream routines provide for
  17127. buffered, formatted, or unformatted input and output.  %@NL@%
  17128. %@NL@%
  17129. Low-level I/O uses a file "handle" to access files. This handle is an
  17130. integer value that is used to refer to the file in subsequent operations.  %@NL@%
  17131. %@NL@%
  17132. Do not mix stream and low-level routines on the same file or device.  %@NL@%
  17133. %@NL@%
  17134. %@NL@%
  17135. %@4@%%@AB@%Stream Routines%@AE@%%@EH@%%@NL@%
  17136. %@NL@%
  17137. In the stream routines listed below, the following manifest constants are
  17138. used:  %@NL@%
  17139. %@NL@%
  17140. %@NL@%
  17141.   ■   %@AB@%EOF%@AE@% is defined to be the value returned at end-of-file%@NL@%
  17142. %@NL@%
  17143.   ■   %@AB@%NULL%@AE@% is the null pointer%@NL@%
  17144. %@NL@%
  17145.   ■   %@AB@%FILE%@AE@% is the structure that maintains information about a stream%@NL@%
  17146. %@NL@%
  17147.   ■   %@AB@%BUFSIZ%@AE@% defines the default size of stream buffers, in bytes%@NL@%
  17148. %@NL@%
  17149. %@NL@%
  17150. ────────────────────────────────────────────────────────────────────────────
  17151. %@AB@%clearerr%@AE@% - Clears the error indicator for a stream.%@NL@%
  17152. %@NL@%
  17153. %@AB@%Include%@AE@%                           STDIO.H
  17154.  
  17155. %@AB@%Prototype%@AE@%                         %@AB@%void clearerr( FILE%@AE@% %@AB@%*%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17156.  
  17157. %@AB@%Argument%@AE@%                          %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17158.  
  17159. %@AB@%Returns%@AE@%                           Void
  17160.  
  17161. ────────────────────────────────────────────────────────────────────────────
  17162. %@AB@%fclose%@AE@% - Closes a stream.%@NL@%
  17163. %@NL@%
  17164. %@AB@%Include%@AE@%                           STDIO.H
  17165.  
  17166. %@AB@%Prototype%@AE@%                         %@AB@%int fclose( FILE *%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17167.  
  17168. %@AB@%Argument%@AE@%                          %@AI@%stream%@AE@%      Target %@AB@%FILE%@AE@% structure
  17169.  
  17170. %@AB@%Returns%@AE@%                           0 if successful, %@AB@%EOF%@AE@% if not
  17171.  
  17172. ────────────────────────────────────────────────────────────────────────────
  17173. %@AB@%feof%@AE@% - Tests for end-of-file on a stream.%@NL@%
  17174. %@NL@%
  17175. %@AB@%Include%@AE@%                           STDIO.H
  17176.  
  17177. %@AB@%Prototype%@AE@%                         %@AB@%int feof( FILE *%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17178.  
  17179. %@AB@%Argument%@AE@%                          %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17180.  
  17181. %@AB@%Returns%@AE@%                           A nonzero value when the current 
  17182.                                   position is  the end-of-file,
  17183.  
  17184.                                   0 if not
  17185.  
  17186. ────────────────────────────────────────────────────────────────────────────
  17187. %@AB@%ferror%@AE@% - Tests for error on a stream.%@NL@%
  17188. %@NL@%
  17189. %@AB@%Include%@AE@%                           STDIO.H
  17190.  
  17191. %@AB@%Prototype%@AE@%                         %@AB@%int ferror( FILE *%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17192.  
  17193. %@AB@%Argument%@AE@%                          %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17194.  
  17195. %@AB@%Returns%@AE@%                           A nonzero value to indicate an error in 
  17196.                                   stream,  0 to indicate
  17197.  
  17198.                                   no error
  17199.  
  17200. ────────────────────────────────────────────────────────────────────────────
  17201. %@AB@%fflush%@AE@% - Flushes a stream. %@NL@%
  17202. %@NL@%
  17203. %@AB@%Include%@AE@%                           STDIO.H
  17204.  
  17205. %@AB@%Prototype%@AE@%                         %@AB@%int fflush( FILE *%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17206.  
  17207. %@AB@%Argument%@AE@%                          %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17208.  
  17209. %@AB@%Returns%@AE@%                           0 if successful, if stream has no buffer,
  17210.                                   or  if stream is open
  17211.  
  17212.                                   only for reading; returns %@AB@%EOF%@AE@% otherwise
  17213.  
  17214. ────────────────────────────────────────────────────────────────────────────
  17215. %@AB@%fgetc%@AE@% - Reads a character from a stream (function version).%@NL@%
  17216. %@NL@%
  17217. %@AB@%Include%@AE@%                           STDIO.H
  17218.  
  17219. %@AB@%Prototype%@AE@%                         %@AB@%int fgetc( FILE *%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17220.  
  17221. %@AB@%Argument%@AE@%                          %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17222.  
  17223. %@AB@%Returns%@AE@%                           The character read; %@AB@%EOF%@AE@% may indicate  
  17224.                                   error
  17225.  
  17226. ────────────────────────────────────────────────────────────────────────────
  17227. %@AB@%fgetpos%@AE@% - Gets the position indicator of a stream.%@NL@%
  17228. %@NL@%
  17229. %@AB@%Include%@AE@%                           STDIO.H
  17230.  
  17231. %@AB@%Prototype%@AE@%                         %@AB@%int fgetpos( FILE *%@AE@%%@AI@%stream%@AE@%%@AB@%,  fpos_t *%@AE@%%@AI@%pos %@AE@%
  17232.                                   %@AB@%);%@AE@%
  17233.  
  17234. %@AB@%Arguments%@AE@%                         %@AI@%stream%@AE@%      Target stream
  17235.  
  17236.                                   %@AI@%pos%@AE@%         Position indicator storage
  17237.  
  17238. %@AB@%Returns%@AE@%                           0 if successful, a nonzero value if not
  17239.  
  17240.                                   %@AB@%errno%@AE@%:%@AB@%  EINVAL%@AE@%,%@AB@% EBADF%@AE@%
  17241.  
  17242. ────────────────────────────────────────────────────────────────────────────
  17243. %@AB@%fgets%@AE@% - Reads a string from a stream.%@NL@%
  17244. %@NL@%
  17245. %@AB@%Include%@AE@%                           STDIO.H
  17246.  
  17247. %@AB@%Prototype%@AE@%                         %@AB@%char *fgets( char *%@AE@%%@AI@%string%@AE@%%@AB@%,  int %@AE@%%@AI@%n%@AE@%%@AB@%, FILE %@AE@%
  17248.                                   %@AB@%*%@AE@%%@AI@%stream %@AE@%%@AB@%);%@AE@%
  17249.  
  17250. %@AB@%Arguments%@AE@%                         %@AI@%string%@AE@%      Storage location for data
  17251.  
  17252.                                   %@AI@%n%@AE@%           Number of characters stored
  17253.  
  17254.                                   %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17255.  
  17256. %@AB@%Returns%@AE@%                           A pointer to string if successful, %@AB@%NULL%@AE@% 
  17257.                                   if unsuccessful or at
  17258.  
  17259.                                   end-of-file
  17260.  
  17261. ────────────────────────────────────────────────────────────────────────────
  17262. %@AB@%fopen%@AE@% - Opens a stream.%@NL@%
  17263. %@NL@%
  17264. %@AB@%Include%@AE@%                           STDIO.H
  17265.  
  17266. %@AB@%Prototype%@AE@%                         %@AB@%FILE *fopen( const char *%@AE@%%@AI@%filename%@AE@%%@AB@%,  %@AE@%
  17267.                                   %@AB@%const char *%@AE@%%@AI@%mode %@AE@%%@AB@%);%@AE@%
  17268.  
  17269. %@AB@%Arguments%@AE@%                         %@AI@%filename%@AE@%    Path name of file
  17270.  
  17271.                                   %@AI@%mode%@AE@%        Type of access permitted 
  17272.                                               such as %@AB@%r%@AE@%, %@AB@%w%@AE@%,%@AB@%  a%@AE@%,%@AB@% r+%@AE@%,%@AB@% w+%@AE@%,%@AB@% a+%@AE@%
  17273.                                               ,%@AB@% t%@AE@%,%@AB@% b%@AE@%
  17274.  
  17275.                                   (appended to type to indicate mode)
  17276.  
  17277. %@AB@%Returns%@AE@%                           A pointer to the open file if successful,
  17278.                                   %@AB@%NULL%@AE@%  if not
  17279.  
  17280. ────────────────────────────────────────────────────────────────────────────
  17281. %@AB@%fprintf%@AE@% - Writes formatted data to a stream.%@NL@%
  17282. %@NL@%
  17283. %@AB@%Include%@AE@%                           STDIO.H
  17284.  
  17285. %@AB@%Prototype%@AE@%                         %@AB@%int fprintf( FILE *%@AE@%%@AI@%stream%@AE@%%@AB@%,  const char *%@AE@%
  17286.                                   %@AI@%format %@AE@%[[,%@AI@% argument%@AE@%]]...  );
  17287.  
  17288. %@AB@%Arguments%@AE@%                         %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17289.  
  17290.                                   %@AI@%format%@AE@%      Format-control string
  17291.  
  17292. %@AB@%Returns%@AE@%                           The number of characters printed
  17293.  
  17294. ────────────────────────────────────────────────────────────────────────────
  17295. %@AB@%fputc%@AE@% - Writes a character to a stream (function version).%@NL@%
  17296. %@NL@%
  17297. %@AB@%Include%@AE@%                           STDIO.H
  17298.  
  17299. %@AB@%Prototype%@AE@%                         %@AB@%int fputc( int %@AE@%%@AI@%c%@AE@%%@AB@%, FILE *%@AE@%%@AI@%stream%@AE@%%@AB@%  );%@AE@%
  17300.  
  17301. %@AB@%Arguments%@AE@%                         %@AI@%c%@AE@%           Character to be written
  17302.  
  17303.                                   %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17304.  
  17305. %@AB@%Returns%@AE@%                           The character written; %@AB@%EOF%@AE@% may indicate 
  17306.                                   error
  17307.  
  17308. ────────────────────────────────────────────────────────────────────────────
  17309. %@AB@%fputs%@AE@% - Writes a string to a stream.%@NL@%
  17310. %@NL@%
  17311. %@AB@%Include%@AE@%                           STDIO.H
  17312.  
  17313. %@AB@%Prototype%@AE@%                         %@AB@%int fputs( const char *%@AE@%%@AI@%string%@AE@%%@AB@%,  FILE *%@AE@%%@AI@%%@AE@%
  17314.                                   %@AI@%stream %@AE@%%@AB@%);%@AE@%
  17315.  
  17316. %@AB@%Arguments%@AE@%                         %@AI@%string%@AE@%      String to be output
  17317.  
  17318.                                   %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17319.  
  17320. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  17321.  
  17322. ────────────────────────────────────────────────────────────────────────────
  17323. %@AB@%fread%@AE@% - Reads unformatted data from a stream. %@NL@%
  17324. %@NL@%
  17325. %@AB@%Include%@AE@%                           STDIO.H
  17326.  
  17327. %@AB@%Prototype%@AE@%                         %@AB@%size_t fread( void *%@AE@%%@AI@%buffer%@AE@%%@AB@%,  size_t %@AE@%%@AI@%size%@AE@%
  17328.                                   %@AB@%, size_t %@AE@%%@AI@%count%@AE@%%@AB@%, FILE *%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17329.  
  17330. %@AB@%Arguments%@AE@%                         %@AI@%buffer%@AE@%      Storage location for data
  17331.  
  17332.                                   %@AI@%size%@AE@%        Item size in bytes
  17333.  
  17334.                                   %@AI@%count%@AE@%       Maximum number of items to 
  17335.                                               be read
  17336.  
  17337.                                   %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17338.  
  17339. %@AB@%Returns%@AE@%                           The number of items actually read
  17340.  
  17341. ────────────────────────────────────────────────────────────────────────────
  17342. %@AB@%freopen%@AE@% - Reassigns a %@AB@%FILE%@AE@% pointer. %@NL@%
  17343. %@NL@%
  17344. %@AB@%Include%@AE@%                           STDIO.H
  17345.  
  17346. %@AB@%Prototype%@AE@%                         %@AB@%FILE *freopen( const char  *%@AE@%%@AI@%filename%@AE@%%@AB@%, %@AE@%
  17347.                                   %@AB@%const char *%@AE@%%@AI@%mode%@AE@%%@AB@%,%@AE@%
  17348.  
  17349.                                   FILE *%@AI@%stream %@AE@%%@AB@%);%@AE@%
  17350.  
  17351. %@AB@%Arguments%@AE@%                         %@AI@%filename%@AE@%    Path name of new file
  17352.  
  17353.                                   %@AI@%mode%@AE@%        Type of access permitted 
  17354.                                               such as %@AB@%r,  w,  a,  r+,  w+,%@AE@%
  17355.  
  17356.                                   a+,  t,  b (appended to type to indicate
  17357.                                   mode)
  17358.  
  17359.                                   %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17360.  
  17361. %@AB@%Returns%@AE@%                           A pointer to the newly opened file if 
  17362.                                   successful,  a %@AB@%NULL%@AE@%
  17363.  
  17364.                                   pointer if not
  17365.  
  17366. ────────────────────────────────────────────────────────────────────────────
  17367. %@AB@%fscanf%@AE@% - Reads formatted data from a stream.%@NL@%
  17368. %@NL@%
  17369. %@AB@%Include%@AE@%                           STDIO.H
  17370.  
  17371. %@AB@%Prototype%@AE@%                         %@AB@%int fscanf( FILE *%@AE@%%@AI@%stream%@AE@%%@AB@%,  const char* %@AE@%%@AI@%%@AE@%
  17372.                                   %@AI@%format %@AE@%%@AB@% %@AE@%[[%@AB@%,%@AE@%%@AI@% argument%@AE@%]]  ... );
  17373.  
  17374. %@AB@%Arguments%@AE@%                         %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17375.  
  17376.                                   %@AI@%format%@AE@%      Format-control string
  17377.  
  17378. %@AB@%Returns%@AE@%                           The number of fields successfully 
  17379.                                   converted and  assigned;
  17380.  
  17381.                                   %@AB@%EOF%@AE@% indicates an attempt to read the 
  17382.                                   end-of-file
  17383.  
  17384. ────────────────────────────────────────────────────────────────────────────
  17385. %@AB@%fseek%@AE@% - Repositions %@AB@%FILE%@AE@% pointer to given location.%@NL@%
  17386. %@NL@%
  17387. %@AB@%Include%@AE@%                           STDIO.H
  17388.  
  17389. %@AB@%Prototype%@AE@%                         %@AB@%int fseek( FILE *%@AE@%%@AI@%stream%@AE@%%@AB@%,  long %@AE@%%@AI@%offset%@AE@%%@AB@%, %@AE@%
  17390.                                   %@AB@%int %@AE@%%@AI@%origin%@AE@%%@AB@% );%@AE@%
  17391.  
  17392. %@AB@%Arguments%@AE@%                         %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17393.  
  17394.                                   %@AI@%offset%@AE@%      Number of bytes from origin
  17395.  
  17396.                                   %@AI@%origin%@AE@%      Initial position (%@AB@%SEEK_SET%@AE@%,%@AB@% %@AE@%
  17397.                                               %@AB@%SEEK_CUR%@AE@%,%@AB@%  SEEK_END)%@AE@%
  17398.  
  17399. %@AB@%Returns%@AE@%                           0 if successful, a nonzero value if not
  17400.  
  17401. ────────────────────────────────────────────────────────────────────────────
  17402. %@AB@%fsetpos%@AE@% - Sets the position indicator of a stream.%@NL@%
  17403. %@NL@%
  17404. %@AB@%Include%@AE@%                           STDIO.H
  17405.  
  17406. %@AB@%Prototype%@AE@%                         %@AB@%int fsetpos( FILE *%@AE@%%@AI@%stream%@AE@%%@AB@%,  const fpos_t%@AE@%
  17407.                                   %@AB@%*%@AE@%%@AI@%pos %@AE@%%@AB@%);%@AE@%
  17408.  
  17409. %@AB@%Arguments%@AE@%                         %@AI@%stream%@AE@%      Target stream
  17410.  
  17411.                                   %@AI@%pos%@AE@%         Position-indicator storage
  17412.  
  17413. %@AB@%Returns%@AE@%                           0 if successful, a nonzero value if not
  17414.  
  17415.                                   %@AB@%errno%@AE@%:  %@AB@%EINVAL%@AE@%, %@AB@%EBADF%@AE@%
  17416.  
  17417. ────────────────────────────────────────────────────────────────────────────
  17418. %@AB@%ftell%@AE@% - Gets current %@AB@%FILE%@AE@% pointer position.%@NL@%
  17419. %@NL@%
  17420. %@AB@%Include%@AE@%                           STDIO.H
  17421.  
  17422. %@AB@%Prototype%@AE@%                         %@AB@%long ftell( FILE *%@AE@%%@AI@%stream%@AE@%%@AB@%  );%@AE@%
  17423.  
  17424. %@AB@%Argument%@AE@%                          %@AI@%stream%@AE@%      Target %@AB@%FILE%@AE@% structure
  17425.  
  17426. %@AB@%Returns%@AE@%                           The current position if successful, -1L 
  17427.                                   if  not
  17428.  
  17429.                                   %@AB@%errno%@AE@%:  %@AB@%EINVAL%@AE@%, %@AB@%EBADF%@AE@%
  17430.  
  17431. ────────────────────────────────────────────────────────────────────────────
  17432. %@AB@%fwrite%@AE@% - Writes unformatted data items to a stream.%@NL@%
  17433. %@NL@%
  17434. %@AB@%Include%@AE@%                           STDIO.H
  17435.  
  17436. %@AB@%Prototype%@AE@%                         %@AB@%size_t fwrite( const void *%@AE@%%@AI@%buffer%@AE@%%@AB@%,  %@AE@%
  17437.                                   %@AB@%size_t %@AE@%%@AI@%size%@AE@%%@AB@%, size_t %@AE@%%@AI@%count%@AE@%%@AB@%,%@AE@%
  17438.  
  17439.                                   FILE *%@AI@%stream %@AE@%%@AB@%);%@AE@%
  17440.  
  17441. %@AB@%Arguments%@AE@%                         %@AI@%buffer%@AE@%      Pointer to data to be 
  17442.                                               written
  17443.  
  17444.                                   %@AI@%size%@AE@%        Item size in bytes
  17445.  
  17446.                                   %@AI@%count%@AE@%       Maximum number of items to 
  17447.                                               be written
  17448.  
  17449.                                   %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17450.  
  17451. %@AB@%Returns%@AE@%                           The number of full items actually 
  17452.                                   written
  17453.  
  17454. ────────────────────────────────────────────────────────────────────────────
  17455. %@AB@%getc%@AE@% - Reads a character from a stream (macro version). %@NL@%
  17456. %@NL@%
  17457. %@AB@%Include%@AE@%                           STDIO.H
  17458.  
  17459. %@AB@%Prototype%@AE@%                         %@AB@%int getc( FILE *%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17460.  
  17461. %@AB@%Argument%@AE@%                          %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17462.  
  17463. %@AB@%Returns%@AE@%                           The character read; %@AB@%EOF%@AE@% may indicate  
  17464.                                   error
  17465.  
  17466. ────────────────────────────────────────────────────────────────────────────
  17467. %@AB@%getchar%@AE@% - Reads a character from %@AB@%stdin%@AE@% (macro version).%@NL@%
  17468. %@NL@%
  17469. %@AB@%Include%@AE@%                           STDIO.H
  17470.  
  17471. %@AB@%Prototype%@AE@%                         %@AB@%int getchar( void );%@AE@%
  17472.  
  17473. %@AB@%Arguments%@AE@%                         None
  17474.  
  17475. %@AB@%Returns%@AE@%                           The character read; %@AB@%EOF%@AE@% may indicate  
  17476.                                   error
  17477.  
  17478. ────────────────────────────────────────────────────────────────────────────
  17479. %@AB@%gets%@AE@% - Reads a line from %@AB@%stdin%@AE@%.%@NL@%
  17480. %@NL@%
  17481. %@AB@%Include%@AE@%                           STDIO.H
  17482.  
  17483. %@AB@%Prototype%@AE@%                         %@AB@%char *gets( char *%@AE@%%@AI@%buffer%@AE@%%@AB@%  );%@AE@%
  17484.  
  17485. %@AB@%Argument%@AE@%                          %@AI@%buffer%@AE@%      Storage location for input 
  17486.                                               string
  17487.  
  17488. %@AB@%Returns%@AE@%                           A pointer to its argument if successful,
  17489.                                   a %@AB@%NULL%@AE@%  pointer if at
  17490.  
  17491.                                   end-of-file or unsuccessful
  17492.  
  17493. ────────────────────────────────────────────────────────────────────────────
  17494. %@AB@%printf%@AE@% - Writes formatted data to %@AB@%stdout%@AE@%.%@NL@%
  17495. %@NL@%
  17496. %@AB@%Include%@AE@%                           STDIO.H
  17497.  
  17498. %@AB@%Prototype%@AE@%                         %@AB@%int printf( const char *%@AE@%%@AI@%format%@AE@%%@AB@%  %@AE@%[[, %@AI@%%@AE@%
  17499.                                   %@AI@%argument%@AE@%]]... );
  17500.  
  17501. %@AB@%Argument%@AE@%                          %@AI@%format%@AE@%      Format-control string
  17502.  
  17503. %@AB@%Returns%@AE@%                           The number of characters printed
  17504.  
  17505. ────────────────────────────────────────────────────────────────────────────
  17506. %@AB@%putc%@AE@% - Writes a character to a stream (macro version). %@NL@%
  17507. %@NL@%
  17508. %@AB@%Include%@AE@%                           STDIO.H
  17509.  
  17510. %@AB@%Prototype%@AE@%                         %@AB@%int putc( int %@AE@%%@AI@%c%@AE@%%@AB@%, FILE *%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17511.  
  17512. %@AB@%Arguments%@AE@%                         %@AI@%c%@AE@%           Character to be written
  17513.  
  17514.                                   %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17515.  
  17516. %@AB@%Returns%@AE@%                           The character written; %@AB@%EOF%@AE@% may indicate 
  17517.                                   error
  17518.  
  17519. ────────────────────────────────────────────────────────────────────────────
  17520. %@AB@%putchar%@AE@% - Writes a character to %@AB@%stdout%@AE@% (macro version).%@NL@%
  17521. %@NL@%
  17522. %@AB@%Include%@AE@%                           STDIO.H
  17523.  
  17524. %@AB@%Prototype%@AE@%                         %@AB@%int putchar( int %@AE@%%@AI@%c %@AE@%%@AB@%);%@AE@%
  17525.  
  17526. %@AB@%Argument%@AE@%                          %@AI@%c%@AE@%           Character to be written
  17527.  
  17528. %@AB@%Returns%@AE@%                           The character written; %@AB@%EOF%@AE@% may indicate 
  17529.                                   error
  17530.  
  17531. ────────────────────────────────────────────────────────────────────────────
  17532. %@AB@%puts%@AE@% - Writes a line to a stream.%@NL@%
  17533. %@NL@%
  17534. %@AB@%Include%@AE@%                           STDIO.H
  17535.  
  17536. %@AB@%Prototype%@AE@%                         %@AB@%int puts( const char *%@AE@%%@AI@%string %@AE@%%@AB@%);%@AE@%
  17537.  
  17538. %@AB@%Argument%@AE@%                          %@AI@%string%@AE@%      String to be output
  17539.  
  17540. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  17541.  
  17542. ────────────────────────────────────────────────────────────────────────────
  17543. %@AB@%rewind%@AE@% - Repositions %@AB@%FILE%@AE@% pointer to beginning of a stream.%@NL@%
  17544. %@NL@%
  17545. %@AB@%Include%@AE@%                           STDIO.H
  17546.  
  17547. %@AB@%Prototype%@AE@%                         %@AB@%void rewind( FILE *%@AE@%%@AI@%stream  %@AE@%%@AB@%);%@AE@%
  17548.  
  17549. %@AB@%Argument%@AE@%                          %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17550.  
  17551. %@AB@%Returns%@AE@%                           Void
  17552.  
  17553. ────────────────────────────────────────────────────────────────────────────
  17554. %@AB@%scanf%@AE@% - Reads formatted data from %@AB@%stdin%@AE@%.%@NL@%
  17555. %@NL@%
  17556. %@AB@%Include%@AE@%                           STDIO.H
  17557.  
  17558. %@AB@%Prototype%@AE@%                         %@AB@%int scanf( const char *%@AE@%%@AI@%format %@AE@%[[,  %@AI@%%@AE@%
  17559.                                   %@AI@%argument%@AE@%]]... );
  17560.  
  17561. %@AB@%Argument%@AE@%                          %@AI@%format%@AE@%      Format control string
  17562.  
  17563. %@AB@%Returns%@AE@%                           The number of fields converted and 
  17564.                                   assigned if  successful, 0 if
  17565.  
  17566.                                   no fields were assigned, %@AB@%EOF%@AE@% for an 
  17567.                                   attempt to read  end-of-file
  17568.  
  17569. ────────────────────────────────────────────────────────────────────────────
  17570. %@AB@%sprintf%@AE@% - Writes formatted data to string.%@NL@%
  17571. %@NL@%
  17572. %@AB@%Include%@AE@%                           STDIO.H
  17573.  
  17574. %@AB@%Prototype%@AE@%                         %@AB@%int sprintf( char *%@AE@%%@AI@%buffer%@AE@%%@AB@%, const  char *%@AE@%
  17575.                                   %@AI@%format %@AE@%[[, %@AI@%argument%@AE@%]]  ... );%@AI@%%@AE@%
  17576.  
  17577. %@AB@%Arguments%@AE@%                         %@AI@%buffer%@AE@%      Storage location for output
  17578.  
  17579.                                   %@AI@%format%@AE@%      Format-control string
  17580.  
  17581. %@AB@%Returns%@AE@%                           The number of characters stored in 
  17582.                                   buffer
  17583.  
  17584. ────────────────────────────────────────────────────────────────────────────
  17585. %@AB@%sscanf%@AE@% - Reads formatted data from string.%@NL@%
  17586. %@NL@%
  17587. %@AB@%Include%@AE@%                           STDIO.H
  17588.  
  17589. %@AB@%Prototype%@AE@%                         %@AB@%int sscanf( const char *%@AE@%%@AI@%buffer%@AE@%%@AB@%,  const %@AE@%
  17590.                                   %@AB@%char *%@AE@%%@AI@%format %@AE@%[[, %@AI@%argument%@AE@%]]  ... );
  17591.  
  17592. %@AB@%Arguments%@AE@%                         %@AI@%buffer%@AE@%      Stored data
  17593.  
  17594.                                   %@AI@%format%@AE@%      Format-control string
  17595.  
  17596. %@AB@%Returns%@AE@%                           The number of fields converted and 
  17597.                                   assigned if  successful, 0 if
  17598.  
  17599.                                   no fields were assigned, %@AB@%EOF%@AE@% for an 
  17600.                                   attempt to read  at end-of-string
  17601.  
  17602. ────────────────────────────────────────────────────────────────────────────
  17603. %@AB@%tmpfile%@AE@% - Creates a temporary file.%@NL@%
  17604. %@NL@%
  17605. %@AB@%Include%@AE@%                           STDIO.H
  17606.  
  17607. %@AB@%Prototype%@AE@%                         %@AB@%FILE *tmpfile( void );%@AE@%
  17608.  
  17609. %@AB@%Arguments%@AE@%                         None
  17610.  
  17611. %@AB@%Returns%@AE@%                           A stream pointer if successful, %@AB@%NULL%@AE@%  if
  17612.                                   not
  17613.  
  17614. ────────────────────────────────────────────────────────────────────────────
  17615. %@AB@%tmpnam%@AE@% - Generates a temporary file name.%@NL@%
  17616. %@NL@%
  17617. %@AB@%Include%@AE@%                           STDIO.H
  17618.  
  17619. %@AB@%Prototype%@AE@%                         %@AB@%char *tmpnam( char *%@AE@%%@AI@%string  %@AE@%%@AB@%);%@AE@%
  17620.  
  17621. %@AB@%Argument%@AE@%                          %@AI@%string%@AE@%      Pointer to temporary name
  17622.  
  17623. %@AB@%Returns%@AE@%                           A pointer to the new name if successful,
  17624.                                   %@AB@%NULL%@AE@%  if not
  17625.  
  17626. ────────────────────────────────────────────────────────────────────────────
  17627. %@AB@%ungetc%@AE@% - Places a character in the input stream buffer.%@NL@%
  17628. %@NL@%
  17629. %@AB@%Include%@AE@%                           STDIO.H
  17630.  
  17631. %@AB@%Prototype%@AE@%                         %@AB@%int ungetc( int %@AE@%%@AI@%c%@AE@%%@AB@%, FILE  *%@AE@%%@AI@%stream %@AE@%%@AB@%);%@AE@%
  17632.  
  17633. %@AB@%Arguments%@AE@%                         %@AI@%c%@AE@%           Character to be pushed
  17634.  
  17635.                                   %@AI@%stream%@AE@%      Pointer to %@AB@%FILE%@AE@% structure
  17636.  
  17637. %@AB@%Returns%@AE@%                           The character argument %@AI@%c%@AE@% if successful, 
  17638.                                   %@AB@%EOF%@AE@% if not
  17639.  
  17640. %@NL@%
  17641. %@4@%%@AB@%Low-Level Routines%@AE@%%@EH@%%@NL@%
  17642. %@NL@%
  17643. The low-level input and output calls do not buffer or format data.  %@NL@%
  17644. %@NL@%
  17645. Files opened by low-level calls are referenced by a "file handle," an
  17646. integer value used by the operating system to refer to the file.  %@NL@%
  17647. %@NL@%
  17648. ────────────────────────────────────────────────────────────────────────────
  17649. %@AB@%close%@AE@% - Closes a file.%@NL@%
  17650. %@NL@%
  17651. %@AB@%Include%@AE@%                           IO.H
  17652.  
  17653. %@AB@%Prototype%@AE@%                         %@AB@%int close( int %@AE@%%@AI@%handle%@AE@%%@AB@% );%@AE@%
  17654.  
  17655. %@AB@%Argument%@AE@%                          %@AI@%handle%@AE@%      Handle referring to open 
  17656.                                               file
  17657.  
  17658. %@AB@%Returns%@AE@%                           0 if successful, -1 if not
  17659.  
  17660.                                   %@AB@%errno%@AE@%: %@AB@%EBADF%@AE@%
  17661.  
  17662. ────────────────────────────────────────────────────────────────────────────
  17663. %@AB@%creat %@AE@% - Creates a file.%@NL@%
  17664. %@NL@%
  17665. %@AB@%Include%@AE@%                           IO.H, SYS\TYPES.H, SYS\STAT.H
  17666.  
  17667. %@AB@%Prototype%@AE@%                         %@AB@%int creat( char *%@AE@%%@AI@%filename%@AE@%%@AB@%, int  %@AE@%%@AI@%pmode %@AE@%%@AB@%);%@AE@%
  17668.  
  17669. %@AB@%Arguments%@AE@%                         %@AI@%filename%@AE@%    Path name of new file
  17670.  
  17671.                                   %@AI@%pmode%@AE@%       Permission setting ( %@AB@%%@AE@%
  17672.                                               %@AB@%S_IWRITE , S_IREAD%@AE@%,%@AB@%  %@AE@%
  17673.  
  17674.                                   S_IREAD | S_IWRITE)
  17675.  
  17676. %@AB@%Returns%@AE@%                           A handle if successful, -1 if not
  17677.  
  17678.                                   %@AB@%errno%@AE@%:  %@AB@%EACCES%@AE@%, %@AB@%EMFILE%@AE@%, %@AB@%ENOENT%@AE@%
  17679.  
  17680. ────────────────────────────────────────────────────────────────────────────
  17681. %@AB@%eof %@AE@% - Tests for end-of-file.%@NL@%
  17682. %@NL@%
  17683. %@AB@%Include%@AE@%                           IO.H
  17684.  
  17685. %@AB@%Prototype%@AE@%                         %@AB@%int eof( int %@AE@%%@AI@%handle%@AE@%%@AB@% );%@AE@%
  17686.  
  17687. %@AB@%Argument%@AE@%                          %@AI@%handle%@AE@%      Handle referring to open 
  17688.                                               file
  17689.  
  17690. %@AB@%Returns%@AE@%                           1 if the current position is the 
  17691.                                   end-of-file  and 0 if it is not,
  17692.  
  17693.                                   -1 to indicate an error
  17694.  
  17695.                                   %@AB@%errno%@AE@%:  %@AB@%EBADF%@AE@%
  17696.  
  17697. ────────────────────────────────────────────────────────────────────────────
  17698. %@AB@%lseek %@AE@% - Repositions file pointer to a given location. %@NL@%
  17699. %@NL@%
  17700. %@AB@%Include%@AE@%                           IO.H, STDIO.H
  17701.  
  17702. %@AB@%Prototype%@AE@%                         %@AB@%long lseek( int %@AE@%%@AI@%handle%@AE@%%@AB@%, long %@AE@%%@AI@%offset%@AE@%%@AB@%,  %@AE@%
  17703.                                   %@AB@%int %@AE@%%@AI@%origin%@AE@%%@AB@% );%@AE@%
  17704.  
  17705. %@AB@%Arguments%@AE@%                         %@AI@%handle%@AE@%      Handle referring to open 
  17706.                                               file
  17707.  
  17708.                                   %@AI@%offset%@AE@%      Number of bytes from origin
  17709.  
  17710.                                   %@AI@%origin%@AE@%      Initial position (%@AB@%SEEK_SET%@AE@%,%@AB@% %@AE@%
  17711.                                               %@AB@%SEEK_CUR%@AE@%,%@AB@%  SEEK_END)%@AE@%
  17712.  
  17713. %@AB@%Returns%@AE@%                           The new position offset (in bytes) from 
  17714.                                   the beginning  of
  17715.  
  17716.                                   the file if successful, -1L if not
  17717.  
  17718.                                   %@AB@%errno%@AE@%:  %@AB@%EBADF%@AE@%, %@AB@%EINVAL%@AE@%
  17719.  
  17720. ────────────────────────────────────────────────────────────────────────────
  17721. %@AB@%open %@AE@% - Opens a file.%@NL@%
  17722. %@NL@%
  17723. %@AB@%Include%@AE@%                           FCNTL.H, IO.H, SYS\TYPES.H, SYS\STAT.H
  17724.  
  17725. %@AB@%Prototype%@AE@%                         %@AB@%int open( char *%@AE@%%@AI@%path%@AE@%%@AB@%, int %@AE@%%@AI@%oflag %@AE@%[[,  int
  17726.                                   %@AI@%pmode%@AE@%]] );
  17727.  
  17728. %@AB@%Arguments%@AE@%                         %@AI@%path%@AE@%        File path name
  17729.  
  17730.                                   %@AI@%oflag%@AE@%       Type of operations allowed 
  17731.                                               such as %@AB@%O_APPEND%@AE@%,%@AB@%%@AE@%
  17732.  
  17733.                                   O_BINARY,%@AB@% O_CREAT%@AE@%,%@AB@%  O_EXCL%@AE@%, %@AB@%  O_RDONLY%@AE@%,%@AB@%%@AE@%
  17734.  
  17735.                                   O_RDWR,%@AB@%   O_TEXT%@AE@%,%@AB@%   O_TRUNC%@AE@%,%@AB@%  O_WRONLY%@AE@%
  17736.  
  17737.                                   (may be joined by%@AB@% |%@AE@% )
  17738.  
  17739.                                   %@AI@%pmode%@AE@%       Permission setting (%@AB@%S_IWRITE%@AE@%
  17740.                                               ,%@AB@%  S_IREAD%@AE@%,%@AB@%%@AE@%
  17741.  
  17742.                                   S_IREAD | S_IWRITE)
  17743.  
  17744. %@AB@%Returns%@AE@%                           A handle if successful, -1 if not
  17745.  
  17746.                                   %@AB@%errno%@AE@%:  %@AB@%EACCES%@AE@%, %@AB@%EEXIST%@AE@%, %@AB@%EMFILE%@AE@%,  %@AB@%ENOENT%@AE@%
  17747.  
  17748. ────────────────────────────────────────────────────────────────────────────
  17749. %@AB@%read %@AE@% - Reads data from a file.%@NL@%
  17750. %@NL@%
  17751. %@AB@%Include%@AE@%                           IO.H
  17752.  
  17753. %@AB@%Prototype%@AE@%                         %@AB@%int read( int %@AE@%%@AI@%handle%@AE@%%@AB@%, char *%@AE@%%@AI@%buffer%@AE@%%@AB@%,  %@AE@%
  17754.                                   %@AB@%unsigned int %@AE@%%@AI@%count%@AE@%%@AB@% );%@AE@%
  17755.  
  17756. %@AB@%Arguments%@AE@%                         %@AI@%handle%@AE@%      Handle referring to open 
  17757.                                               file
  17758.  
  17759.                                   %@AI@%buffer%@AE@%      Storage location of data
  17760.  
  17761.                                   %@AI@%count%@AE@%       Maximum number of bytes
  17762.  
  17763. %@AB@%Returns%@AE@%                           The number of bytes actually read or 0 
  17764.                                   at end-of-file  if
  17765.  
  17766.                                   successful; -1 if not
  17767.  
  17768.                                   %@AB@%errno%@AE@%:  %@AB@%EBADF%@AE@%
  17769.  
  17770. ────────────────────────────────────────────────────────────────────────────
  17771. %@AB@%tell %@AE@% - Gets current file-pointer position.%@NL@%
  17772. %@NL@%
  17773. %@AB@%Include%@AE@%                           IO.H
  17774.  
  17775. %@AB@%Prototype%@AE@%                         %@AB@%long tell( int %@AE@%%@AI@%handle %@AE@%%@AB@%);%@AE@%
  17776.  
  17777. %@AB@%Argument%@AE@%                          %@AI@%handle%@AE@%      Handle referring to open 
  17778.                                               file
  17779.  
  17780. %@AB@%Returns%@AE@%                           The current position if successful, -1L 
  17781.                                   if  not
  17782.  
  17783.                                   %@AB@%errno%@AE@%:  %@AB@%EBADF%@AE@%
  17784.  
  17785. ────────────────────────────────────────────────────────────────────────────
  17786. %@AB@%write %@AE@% - Writes data to a file.%@NL@%
  17787. %@NL@%
  17788. %@AB@%Include%@AE@%                           IO.H
  17789.  
  17790. %@AB@%Prototype%@AE@%                         %@AB@%int write( int %@AE@%%@AI@%handle%@AE@%%@AB@%, void *%@AE@%%@AI@%buffer%@AE@%%@AB@%,  %@AE@%
  17791.                                   %@AB@%unsigned int %@AE@%%@AI@%count %@AE@%%@AB@%);%@AE@%
  17792.  
  17793. %@AB@%Arguments%@AE@%                         %@AI@%handle%@AE@%      Handle referring to open 
  17794.                                               file
  17795.  
  17796.                                   %@AI@%buffer%@AE@%      Data to be written
  17797.  
  17798.                                   %@AI@%count%@AE@%       Number of bytes
  17799.  
  17800. %@AB@%Returns%@AE@%                           The number of bytes actually written if 
  17801.                                   successful,  -1 if not
  17802.  
  17803.                                   %@AB@%errno%@AE@%: %@AB@% EBADF%@AE@%, %@AB@%ENOSPC%@AE@%
  17804.  
  17805.   %@NL@%
  17806. %@NL@%
  17807. %@NL@%
  17808. %@4@%%@AB@%Console and Port I/O Routines%@AE@%%@EH@%%@NL@%
  17809. %@NL@%
  17810. The console and port I/O routines perform reading and writing operations on
  17811. your console or on the specified port.  %@NL@%
  17812. %@NL@%
  17813. The %@AB@%cgets%@AE@%, %@AB@%cscanf%@AE@%, %@AB@%getch%@AE@%, %@AB@%getche%@AE@%, and %@AB@%kbhit%@AE@% routines take input from the
  17814. console.  %@NL@%
  17815. %@NL@%
  17816. The %@AB@%cprintf%@AE@%, %@AB@%cputs%@AE@%, %@AB@%putch%@AE@%, and %@AB@%ungetch%@AE@% routines write to the console.  %@NL@%
  17817. %@NL@%
  17818. The console or port does not have to be opened or closed before I/O is
  17819. performed.  %@NL@%
  17820. %@NL@%
  17821. The console I/O routines use the corresponding MS-DOS system calls to read
  17822. and write characters. Since these routines are not compatible with stream or
  17823. low-level library routines, console routines should not be used with them.  %@NL@%
  17824. %@NL@%
  17825. ────────────────────────────────────────────────────────────────────────────
  17826. %@AB@%cgets%@AE@% - Reads a string from the console.%@NL@%
  17827. %@NL@%
  17828. %@AB@%Include%@AE@%                           CONIO.H
  17829.  
  17830. %@AB@%Prototype%@AE@%                         %@AB@%char *cgets( char *%@AE@%%@AI@%buffer  %@AE@%%@AB@%);%@AE@%
  17831.  
  17832. %@AB@%Argument%@AE@%                          %@AI@%buffer%@AE@%      Storage location for data
  17833.  
  17834. %@AB@%Returns%@AE@%                           A pointer to the start of the string, 
  17835.                                   which is  at %@AI@%str%@AE@%[2]
  17836.  
  17837. ────────────────────────────────────────────────────────────────────────────
  17838. %@AB@%cprintf%@AE@% - Writes formatted data to the console.%@NL@%
  17839. %@NL@%
  17840. %@AB@%Include%@AE@%                           CONIO.H
  17841.  
  17842. %@AB@%Prototype%@AE@%                         %@AB@%int cprintf( char *%@AE@%%@AI@%format %@AE@%[[,  %@AI@%argument%@AE@%]]
  17843.                                   ... );
  17844.  
  17845. %@AB@%Argument%@AE@%                          %@AI@%format%@AE@%      Format-control string
  17846.  
  17847. %@AB@%Returns%@AE@%                           The number of characters printed
  17848.  
  17849. ────────────────────────────────────────────────────────────────────────────
  17850. %@AB@%cputs%@AE@% - Writes a string to the console.%@NL@%
  17851. %@NL@%
  17852. %@AB@%Include%@AE@%                           CONIO.H
  17853.  
  17854. %@AB@%Prototype%@AE@%                         %@AB@%int cputs( char *%@AE@%%@AI@%string %@AE@%%@AB@%);%@AE@%
  17855.  
  17856. %@AB@%Argument%@AE@%                          %@AI@%string%@AE@%      Output string
  17857.  
  17858. %@AB@%Returns%@AE@%                           0 if successful, nonzero if not
  17859.  
  17860. ────────────────────────────────────────────────────────────────────────────
  17861. %@AB@%cscanf%@AE@% - Reads formatted data from the console.%@NL@%
  17862. %@NL@%
  17863. %@AB@%Include%@AE@%                           CONIO.H
  17864.  
  17865. %@AB@%Prototype%@AE@%                         %@AB@%int cscanf( char *%@AE@%%@AI@%format %@AE@%[[,  %@AI@%argument%@AE@%
  17866.                                   ]]... );
  17867.  
  17868. %@AB@%Argument%@AE@%                          %@AI@%format%@AE@%      Format-control string
  17869.  
  17870. %@AB@%Returns%@AE@%                           The number of fields converted and 
  17871.                                   assigned if  successful
  17872.  
  17873.                                   (0 means no fields were assigned), %@AB@%EOF%@AE@% 
  17874.                                   for an attempt  to read
  17875.  
  17876.                                   end-of-file
  17877.  
  17878. ────────────────────────────────────────────────────────────────────────────
  17879. %@AB@%getch%@AE@% - Reads a character from the console.%@NL@%
  17880. %@NL@%
  17881. %@AB@%Include%@AE@%                           CONIO.H
  17882.  
  17883. %@AB@%Prototype%@AE@%                         %@AB@%int getch( void );%@AE@%
  17884.  
  17885. %@AB@%Arguments%@AE@%                         None
  17886.  
  17887. %@AB@%Returns%@AE@%                           The character read
  17888.  
  17889. ────────────────────────────────────────────────────────────────────────────
  17890. %@AB@%getche%@AE@% - Reads a character from the console and echoes it.%@NL@%
  17891. %@NL@%
  17892. %@AB@%Include%@AE@%                           CONIO.H
  17893.  
  17894. %@AB@%Prototype%@AE@%                         %@AB@%int getche( void );%@AE@%
  17895.  
  17896. %@AB@%Arguments%@AE@%                         None
  17897.  
  17898. %@AB@%Returns%@AE@%                           The character read
  17899.  
  17900. ────────────────────────────────────────────────────────────────────────────
  17901. %@AB@%kbhit%@AE@% - Checks for a keystroke at the console.%@NL@%
  17902. %@NL@%
  17903. %@AB@%Include%@AE@%                           CONIO.H
  17904.  
  17905. %@AB@%Prototype%@AE@%                         %@AB@%int kbhit( void );%@AE@%
  17906.  
  17907. %@AB@%Arguments%@AE@%                         None
  17908.  
  17909. %@AB@%Returns%@AE@%                           A nonzero value if a key has been 
  17910.                                   pressed, 0  if not
  17911.  
  17912. ────────────────────────────────────────────────────────────────────────────
  17913. %@AB@%putch%@AE@% - Writes a character to the console.%@NL@%
  17914. %@NL@%
  17915. %@AB@%Include%@AE@%                           CONIO.H
  17916.  
  17917. %@AB@%Prototype%@AE@%                         %@AB@%int putch( int %@AE@%%@AI@%c%@AE@%%@AB@% );%@AE@%
  17918.  
  17919. %@AB@%Argument%@AE@%                          %@AI@%c%@AE@%           Character to be output
  17920.  
  17921. %@AB@%Returns%@AE@%                           The argument %@AI@%c%@AE@% if successful, %@AB@%EOF%@AE@%  if 
  17922.                                   not
  17923.  
  17924. ────────────────────────────────────────────────────────────────────────────
  17925. %@AB@%ungetch%@AE@% - "Ungets" the last character read from the console so that it
  17926. becomes the next character read.%@NL@%
  17927. %@NL@%
  17928. %@AB@%Include%@AE@%                           CONIO.H
  17929.  
  17930. %@AB@%Prototype%@AE@%                         %@AB@%int ungetch( int %@AE@%%@AI@%c %@AE@%%@AB@%);%@AE@%
  17931.  
  17932. %@AB@%Argument%@AE@%                          %@AI@%c%@AE@%           Character to be pushed
  17933.  
  17934. %@AB@%Returns%@AE@%                           The argument %@AI@%c%@AE@% if successful, %@AB@%EOF%@AE@%  if 
  17935.                                   not
  17936.  
  17937. %@NL@%
  17938. %@2@%%@CR:C6A-B0011   @%%@AB@%Math Routines%@AE@%%@EH@%%@NL@%
  17939. %@NL@%
  17940. The math routines allow you to perform common mathematical calculations.  %@NL@%
  17941. %@NL@%
  17942. All math routines work with floating-point values and therefore require
  17943. floating-point support.  %@NL@%
  17944. %@NL@%
  17945. ────────────────────────────────────────────────────────────────────────────
  17946. %@AB@%abs, fabs, labs%@AE@% - The %@AB@%abs%@AE@%, %@AB@%fabs%@AE@%, and %@AB@%labs%@AE@% routines return the absolute value
  17947. of an integer, a double, and a long argument, respectively.%@NL@%
  17948. %@NL@%
  17949. %@AB@%Includes%@AE@%                          STDLIB.H (%@AB@%abs%@AE@%, %@AB@%labs%@AE@%), MATH.H (%@AB@%fabs%@AE@%)
  17950.  
  17951. %@AB@%Prototypes%@AE@%                        %@AB@%int abs( int %@AE@%%@AI@%n %@AE@%%@AB@%)%@AE@%;
  17952.  
  17953.  
  17954.  
  17955.                                   %@AB@%double fabs( double%@AE@% %@AI@%x %@AE@%%@AB@%);%@AE@%
  17956.  
  17957.  
  17958.  
  17959.                                   long labs( long %@AI@%x %@AE@%%@AB@%);%@AE@%
  17960.  
  17961. %@AB@%Arguments%@AE@%                         %@AI@%n%@AE@%           Integer (%@AB@%abs%@AE@%) or long (%@AB@%labs%@AE@%)
  17962.                                               value
  17963.  
  17964.                                   %@AI@%x%@AE@%           Floating-point value
  17965.  
  17966. %@AB@%Returns%@AE@%                           Absolute value of its argument
  17967.  
  17968. ────────────────────────────────────────────────────────────────────────────
  17969. %@AB@%acos%@AE@% - Calculates the arccosine.%@NL@%
  17970. %@NL@%
  17971. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  17972.  
  17973. %@AB@%Prototype%@AE@%                         %@AB@%double acos( double %@AE@%%@AI@%x%@AE@%%@AB@% );%@AE@%
  17974.  
  17975. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Value whose arccosine is to 
  17976.                                               be calculated
  17977.  
  17978. %@AB@%Returns%@AE@%                           The arccosine result if successful, or 0
  17979.                                   if %@AI@%x%@AE@%  > 1
  17980.  
  17981.                                   %@AB@%errno%@AE@%:  %@AB@%EDOM%@AE@%
  17982.  
  17983. ────────────────────────────────────────────────────────────────────────────
  17984. %@AB@%asin%@AE@% - Calculates the arcsine.%@NL@%
  17985. %@NL@%
  17986. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  17987.  
  17988. %@AB@%Prototype%@AE@%                         %@AB@%double asin( double %@AE@%%@AI@%x %@AE@%%@AB@%);%@AE@%
  17989.  
  17990. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Value whose arcsine is to be
  17991.                                               calculated
  17992.  
  17993. %@AB@%Returns%@AE@%                           The arcsine result if successful, or 0 
  17994.                                   if %@AI@%x%@AE@%  >1
  17995.  
  17996.                                   %@AB@%errno%@AE@%:  %@AB@%EDOM%@AE@%
  17997.  
  17998. ────────────────────────────────────────────────────────────────────────────
  17999. %@AB@%atan, atan2%@AE@% - Calculates the arctangent of %@AI@%x%@AE@% (%@AB@%atan%@AE@%) or the arctangent of %@AI@%y%@AE@%/%@AI@%x%@AE@%
  18000. (%@AB@%atan2%@AE@%).%@NL@%
  18001. %@NL@%
  18002. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18003.  
  18004. %@AB@%Prototypes%@AE@%                        %@AB@%double atan( double %@AE@%%@AI@%x%@AE@%%@AB@% );%@AE@%
  18005.  
  18006.  
  18007.  
  18008.                                   %@AB@%double atan2( double%@AE@% %@AI@%y%@AE@%%@AB@%, double %@AE@%%@AI@%x %@AE@%%@AB@%);%@AE@%
  18009.  
  18010. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%, %@AI@%y%@AE@%        Floating-point values
  18011.  
  18012. %@AB@%Returns%@AE@%                           %@AB@%atan%@AE@%: the arctangent result
  18013.  
  18014.                                   %@AB@%atan2%@AE@%: the arctangent of %@AI@%y%@AE@%/%@AI@%x%@AE@%, or 0 if 
  18015.                                   both arguments  are 0
  18016.  
  18017.                                   %@AB@%errno%@AE@%:  %@AB@%EDOM%@AE@%
  18018.  
  18019. ────────────────────────────────────────────────────────────────────────────
  18020. %@AB@%ceil%@AE@% - Rounds the argument up to an integer.%@NL@%
  18021. %@NL@%
  18022. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18023.  
  18024. %@AB@%Prototype%@AE@%                         %@AB@%double ceil( double %@AE@%%@AI@%x %@AE@%%@AB@%);%@AE@%
  18025.  
  18026. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Floating-point value
  18027.  
  18028. %@AB@%Returns%@AE@%                           The double result
  18029.  
  18030. ────────────────────────────────────────────────────────────────────────────
  18031. %@AB@%cos, cosh%@AE@% - Calculates the cosine (%@AB@%cos%@AE@%) or the hyperbolic cosine (%@AB@%cosh%@AE@%).%@NL@%
  18032. %@NL@%
  18033. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18034.  
  18035. %@AB@%Prototypes%@AE@%                        %@AB@%double cos( double %@AE@%%@AI@%x %@AE@%%@AB@%);%@AE@%
  18036.  
  18037.  
  18038.  
  18039.                                   %@AB@%double cosh( double%@AE@% %@AI@%x%@AE@%%@AB@% );%@AE@%
  18040.  
  18041. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Angle (in radians)
  18042.  
  18043. %@AB@%Returns%@AE@%                           %@AB@%cos%@AE@%: the cosine result if successful, 0 
  18044.                                   if not
  18045.  
  18046.                                   %@AB@%cosh%@AE@%: the hyperbolic result if 
  18047.                                   successful, or %@AB@%HUGE_VAL%@AE@%  if the
  18048.  
  18049.                                   result is too large
  18050.  
  18051.                                   %@AB@%errno:%@AE@%  %@AB@%ERANGE%@AE@%
  18052.  
  18053. ────────────────────────────────────────────────────────────────────────────
  18054. %@AB@%exp%@AE@% - Calculates the exponential function.%@NL@%
  18055. %@NL@%
  18056. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18057.  
  18058. %@AB@%Prototype%@AE@%                         %@AB@%double exp( double %@AE@%%@AI@%x %@AE@%%@AB@%);%@AE@%
  18059.  
  18060. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Floating-point value
  18061.  
  18062. %@AB@%Returns%@AE@%                           The exponential value if successful, %@AB@%%@AE@%
  18063.                                   %@AB@%HUGE_VAL%@AE@%  on overflow,
  18064.  
  18065.                                   0 on underflow
  18066.  
  18067.                                   %@AB@%errno%@AE@%:  %@AB@%ERANGE%@AE@%
  18068.  
  18069. ────────────────────────────────────────────────────────────────────────────
  18070. %@AB@%floor%@AE@% - Rounds the argument down to an integer.%@NL@%
  18071. %@NL@%
  18072. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18073.  
  18074. %@AB@%Prototype%@AE@%                         %@AB@%double floor( double %@AE@%%@AI@%x%@AE@%%@AB@% );%@AE@%
  18075.  
  18076. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Floating-point value
  18077.  
  18078. %@AB@%Returns%@AE@%                           The floating-point result
  18079.  
  18080. ────────────────────────────────────────────────────────────────────────────
  18081. %@AB@%fmod%@AE@% - Finds the floating-point remainder.%@NL@%
  18082. %@NL@%
  18083. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18084.  
  18085. %@AB@%Prototype%@AE@%                         %@AB@%double fmod( double %@AE@%%@AI@%x%@AE@%%@AB@%, double %@AE@%%@AI@%y  %@AE@%%@AB@%);%@AE@%
  18086.  
  18087. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%,%@AI@% y%@AE@%        Floating-point values
  18088.  
  18089. %@AB@%Returns%@AE@%                           The floating-point remainder, or 0 if %@AI@%y%@AE@% 
  18090.                                   is 0
  18091.  
  18092. ────────────────────────────────────────────────────────────────────────────
  18093. %@AB@%frexp%@AE@% - Calculates an exponential value.%@NL@%
  18094. %@NL@%
  18095. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18096.  
  18097. %@AB@%Prototype%@AE@%                         %@AB@%double frexp( double %@AE@%%@AI@%x%@AE@%%@AB@%, int *%@AE@%%@AI@%expptr  %@AE@%%@AB@%);%@AE@%
  18098.  
  18099. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Floating-point value
  18100.  
  18101.                                   %@AI@%expptr%@AE@%      Pointer to stored integer 
  18102.                                               exponent
  18103.  
  18104. %@AB@%Returns%@AE@%                           The mantissa, or 0 if %@AI@%x%@AE@% is 0
  18105.  
  18106. ────────────────────────────────────────────────────────────────────────────
  18107. %@AB@%ldexp%@AE@% - Calculates the argument times 2exp.%@NL@%
  18108. %@NL@%
  18109. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18110.  
  18111. %@AB@%Prototype%@AE@%                         %@AB@%double ldexp( double %@AE@%%@AI@%x%@AE@%%@AB@%, int %@AE@%%@AI@%exp  %@AE@%%@AB@%);%@AE@%
  18112.  
  18113. %@AB@%Arguments%@AE@%                         %@AI@%x%@AE@%           Floating-point value
  18114.  
  18115.                                   %@AI@%exp%@AE@%         Integer exponent
  18116.  
  18117. %@AB@%Returns%@AE@%                           An exponential value if successful, %@AB@%%@AE@%
  18118.                                   %@AB@%HUGE_VAL%@AE@%  on overflow
  18119.  
  18120.                                   %@AB@%errno:%@AE@%  %@AB@%ERANGE%@AE@%
  18121.  
  18122. ────────────────────────────────────────────────────────────────────────────
  18123. %@AB@%log, log10%@AE@% - Calculates the natural logarithm (%@AB@%log%@AE@%) or the base-10 log
  18124. (%@AB@%log10%@AE@%).%@NL@%
  18125. %@NL@%
  18126. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18127.  
  18128. %@AB@%Prototypes%@AE@%                        %@AB@%double log( double %@AE@%%@AI@%x%@AE@%%@AB@% );%@AE@%
  18129.  
  18130.  
  18131.  
  18132.                                   double log10( double %@AI@%x %@AE@%%@AB@%);%@AE@%
  18133.  
  18134. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Floating-point value
  18135.  
  18136. %@AB@%Returns%@AE@%                           A logarithm result if successful, %@AB@%%@AE@%
  18137.                                   %@AB@%-HUGE_VAL%@AE@%  if not
  18138.  
  18139.                                   %@AB@%errno%@AE@%: %@AB@%EDOM%@AE@% (if %@AI@%x%@AE@% < 0), %@AB@%ERANGE%@AE@%  (if %@AI@%x%@AE@% = 
  18140.                                   0)
  18141.  
  18142. ────────────────────────────────────────────────────────────────────────────
  18143. %@AB@%modf%@AE@% - Breaks argument into integer and fractional parts.%@NL@%
  18144. %@NL@%
  18145. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18146.  
  18147. %@AB@%Prototype%@AE@%                         %@AB@%double modf( double %@AE@%%@AI@%x%@AE@%%@AB@%, double *%@AE@%%@AI@%intptr%@AE@%%@AB@%  %@AE@%
  18148.                                   %@AB@%);%@AE@%
  18149.  
  18150. %@AB@%Arguments%@AE@%                         %@AI@%x%@AE@%           Floating-point value
  18151.  
  18152.                                   %@AI@%intptr%@AE@%      Pointer to stored integer 
  18153.                                               position
  18154.  
  18155. %@AB@%Returns%@AE@%                           The signed fractional portion of %@AI@%x%@AE@%
  18156.  
  18157. ────────────────────────────────────────────────────────────────────────────
  18158. %@AB@%pow%@AE@% - Calculates a value raised to a power. %@NL@%
  18159. %@NL@%
  18160. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18161.  
  18162. %@AB@%Prototype%@AE@%                         %@AB@%double pow( double %@AE@%%@AI@%x%@AE@%%@AB@%, double %@AE@%%@AI@%y  %@AE@%%@AB@%);%@AE@%
  18163.  
  18164. %@AB@%Arguments%@AE@%                         %@AI@%x%@AE@%           Number to be raised
  18165.  
  18166.                                   %@AI@%y%@AE@%           Power of %@AI@%x%@AE@%
  18167.  
  18168. %@AB@%Returns%@AE@%                           The argument %@AI@%x%@AE@% raised to the %@AI@%y%@AE@%  power if
  18169.                                   successful,
  18170.  
  18171.                                   %@AB@%HUGE_VAL%@AE@% if not
  18172.  
  18173. ────────────────────────────────────────────────────────────────────────────
  18174. %@AB@%rand, srand%@AE@% - The %@AB@%rand%@AE@% function returns a pseudorandom integer in the range
  18175. 0-32,767. The %@AB@%srand%@AE@% function initializes the random number generator.%@NL@%
  18176. %@NL@%
  18177. %@AB@%Include%@AE@%                           STDLIB.H
  18178.  
  18179. %@AB@%Prototypes%@AE@%                        %@AB@%int rand( void );%@AE@%
  18180.  
  18181.  
  18182.  
  18183.                                   %@AB@%void srand( unsigned%@AE@% %@AI@%seed %@AE@%%@AB@%)%@AE@%;
  18184.  
  18185. %@AB@%Argument%@AE@%                          %@AI@%seed%@AE@%        Seed for random-number 
  18186.                                               generation  (%@AB@%srand%@AE@%)
  18187.  
  18188. %@AB@%Returns%@AE@%                           %@AB@%rand%@AE@%: a pseudorandom number
  18189.  
  18190.                                   %@AB@%srand%@AE@%: void
  18191.  
  18192. ────────────────────────────────────────────────────────────────────────────
  18193. %@AB@%sin, sinh%@AE@% - Calculates the sine (%@AB@%sin%@AE@%) or hyperbolic sine (%@AB@%sinh%@AE@%).%@NL@%
  18194. %@NL@%
  18195. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18196.  
  18197. %@AB@%Prototypes%@AE@%                        %@AB@%double sin( double %@AE@%%@AI@%x%@AE@%%@AB@% );%@AE@%
  18198.  
  18199.  
  18200.  
  18201.                                   %@AB@%double sinh( double%@AE@% %@AI@%x %@AE@%%@AB@%);%@AE@%
  18202.  
  18203. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Angle (in radians)
  18204.  
  18205. %@AB@%Returns%@AE@%                           %@AB@%sin%@AE@%: the sine of %@AI@%x%@AE@% if successful,  0 if 
  18206.                                   not
  18207.  
  18208.                                   %@AB@%sinh%@AE@%: the hyperbolic sine of %@AI@%x%@AE@% if 
  18209.                                   successful, %@AB@%HUGE_VAL%@AE@%  if not
  18210.  
  18211.                                   %@AB@%errno%@AE@%:  %@AB@%ERANGE%@AE@%
  18212.  
  18213. ────────────────────────────────────────────────────────────────────────────
  18214. %@AB@%sqrt%@AE@% - Finds the square root.%@NL@%
  18215. %@NL@%
  18216. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18217.  
  18218. %@AB@%Prototype%@AE@%                         %@AB@%double sqrt( double %@AE@%%@AI@%x%@AE@%%@AB@% );%@AE@%
  18219.  
  18220. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Nonnegative floating-point 
  18221.                                               value
  18222.  
  18223. %@AB@%Returns%@AE@%                           A square root if successful, 0 if not
  18224.  
  18225.                                   %@AB@%errno%@AE@%:  %@AB@%EDOM%@AE@%
  18226.  
  18227. ────────────────────────────────────────────────────────────────────────────
  18228. %@AB@%tan, tanh%@AE@% - Calculates the tangent (%@AB@%tan%@AE@%) or hyperbolic tangent (%@AB@%tanh%@AE@%).%@NL@%
  18229. %@NL@%
  18230. %@AB@%Includes%@AE@%                          FLOAT.H, MATH.H
  18231.  
  18232. %@AB@%Prototypes%@AE@%                        %@AB@%double tan( double %@AE@%%@AI@%x%@AE@%%@AB@% );%@AE@%
  18233.  
  18234.  
  18235.  
  18236.                                   %@AB@%double tanh( double%@AE@% %@AI@%x%@AE@%%@AB@% );%@AE@%
  18237.  
  18238. %@AB@%Argument%@AE@%                          %@AI@%x%@AE@%           Angle (in radians)
  18239.  
  18240. %@AB@%Returns%@AE@%                           %@AB@%tan:%@AE@% the tangent of %@AI@%x%@AE@% if successful,  0 
  18241.                                   if not
  18242.  
  18243.                                   %@AB@%tanh:%@AE@% the hyperbolic tangent of %@AI@%x%@AE@%
  18244.  
  18245.                                   %@AB@%errno%@AE@%:  %@AB@%ERANGE%@AE@% (%@AB@%tan%@AE@% only)
  18246.  
  18247. %@NL@%
  18248. %@2@%%@CR:C6A-B0012   @%%@AB@%Memory-Allocation Routines%@AE@%%@EH@%%@NL@%
  18249. %@NL@%
  18250. The memory-allocation routines allocate, free, analyze, and reallocate
  18251. blocks of memory.  %@NL@%
  18252. %@NL@%
  18253. Many of the memory-allocation functions are prefixed by an %@AB@%_f%@AE@% or an %@AB@%_n%@AE@%. This
  18254. notation means use the far (%@AB@%_f%@AE@%) heap or the near (%@AB@%_n%@AE@%) heap.  %@NL@%
  18255. %@NL@%
  18256. The %@AB@%malloc%@AE@% family of routines (%@AB@%malloc%@AE@%, %@AB@%_fmalloc%@AE@%, and %@AB@%_nmalloc%@AE@%) allocates
  18257. memory blocks of a specified size. The %@AB@%calloc%@AE@% function allocates storage for
  18258. an array. The %@AB@%halloc%@AE@% function allocates storage for a huge array.  %@NL@%
  18259. %@NL@%
  18260. The %@AB@%realloc%@AE@% routine changes the size of an allocated block.  %@NL@%
  18261. %@NL@%
  18262. ────────────────────────────────────────────────────────────────────────────
  18263. %@AB@%calloc%@AE@% - Allocates storage for an array. %@NL@%
  18264. %@NL@%
  18265. %@AB@%Includes%@AE@%                          MALLOC.H, STDLIB.H
  18266.  
  18267. %@AB@%Prototype%@AE@%                         %@AB@%void *calloc( size_t %@AE@%%@AI@%num%@AE@%%@AB@%,  size_t %@AE@%%@AI@%size %@AE@%%@AB@%%@AE@%
  18268.                                   %@AB@%);%@AE@%
  18269.  
  18270. %@AB@%Arguments%@AE@%                         %@AI@%num%@AE@%         Number of elements
  18271.  
  18272.                                   %@AI@%size%@AE@%        Length in bytes of each 
  18273.                                               element
  18274.  
  18275. %@AB@%Returns%@AE@%                           A void pointer to the allocated space if
  18276.                                   successful,
  18277.  
  18278.                                   %@AB@%NULL%@AE@% if not
  18279.  
  18280. ────────────────────────────────────────────────────────────────────────────
  18281. %@AB@%free, _ffree, hfree, _nfree%@AE@% - Frees a block of memory previously allocated
  18282. by the corresponding %@AB@%malloc%@AE@% routine. The corresponding routines are listed
  18283. below:%@NL@%
  18284. %@NL@%
  18285. %@AB@%Free Function%@AE@%                     %@AB@%Allocation Function%@AE@%
  18286. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  18287. %@AB@%_ffree%@AE@%                            %@AB@%_fmalloc%@AE@%
  18288.  
  18289. %@AB@%free%@AE@%                              %@AB@%calloc, malloc, realloc%@AE@%
  18290.  
  18291. %@AB@%hfree%@AE@%                             %@AB@%halloc%@AE@%
  18292.  
  18293. %@AB@%_nfree%@AE@%                            %@AB@%_nmalloc%@AE@%
  18294.  
  18295.                                   
  18296.  
  18297. %@AB@%Includes%@AE@%                          MALLOC.H, STDLIB.H (ANSI-compatible %@AB@%free%@AE@%
  18298.                                   only)
  18299.  
  18300. %@AB@%Prototypes%@AE@%                        %@AB@%void _ffree( void _far *%@AE@%%@AI@%memblock  %@AE@%%@AB@%);%@AE@%
  18301.  
  18302.  
  18303.  
  18304.                                   %@AB@%void free( void *%@AE@%%@AI@%memblock%@AE@%%@AB@% );%@AE@%
  18305.  
  18306.  
  18307.  
  18308.                                   void _nfree( void near *%@AI@%memblock%@AE@%%@AB@% );%@AE@%
  18309.  
  18310.  
  18311.  
  18312.                                               %@AB@%void hfree( void huge *%@AE@%%@AI@%%@AE@%
  18313.                                               %@AI@%memblock%@AE@%%@AB@% );%@AE@%
  18314.  
  18315. %@AB@%Argument%@AE@%                          %@AI@%memblock%@AE@%    Allocated memory block
  18316.  
  18317. %@AB@%Returns%@AE@%                           Void
  18318.  
  18319. ────────────────────────────────────────────────────────────────────────────
  18320. %@AB@%malloc , _fmalloc,  _nmalloc%@AE@% - Allocates a block of memory. The %@AB@%_fmalloc%@AE@%
  18321. function allocates the block in the far heap. The %@AB@%_nmalloc%@AE@% function
  18322. allocates the block in the near heap.%@NL@%
  18323. %@NL@%
  18324. %@AB@%Includes%@AE@%                          MALLOC.H, STDLIB.H (ANSI-compatible %@AB@%%@AE@%
  18325.                                   %@AB@%malloc%@AE@%  only)
  18326.  
  18327. %@AB@%Prototypes%@AE@%                        %@AB@%void *malloc( size_t %@AE@%%@AI@%size %@AE@%%@AB@%);%@AE@%
  18328.  
  18329.  
  18330.  
  18331.                                   void _far *_fmalloc( size_t %@AI@%size %@AE@%%@AB@%);%@AE@%
  18332.  
  18333.  
  18334.  
  18335.                                   void near *_nmalloc( size_t %@AI@%size %@AE@%%@AB@%);%@AE@%
  18336.  
  18337. %@AB@%Argument%@AE@%                          %@AI@%size%@AE@%        Bytes to allocate
  18338.  
  18339. %@AB@%Returns%@AE@%                           A void pointer to the allocated space if
  18340.                                   successful,  %@AB@%NULL%@AE@% if not
  18341.  
  18342. ────────────────────────────────────────────────────────────────────────────
  18343. %@AB@%realloc%@AE@% - Reallocates a block. %@NL@%
  18344. %@NL@%
  18345. %@AB@%Include%@AE@%                           MALLOC.H, STDLIB.H
  18346.  
  18347. %@AB@%Prototype%@AE@%                         %@AB@%void *realloc( void *%@AE@%%@AI@%memblock%@AE@%%@AB@%,  size_t %@AE@%%@AI@%%@AE@%
  18348.                                   %@AI@%size%@AE@%%@AB@% );%@AE@%
  18349.  
  18350. %@AB@%Arguments%@AE@%                         %@AI@%memblock%@AE@%    Pointer to previously 
  18351.                                               allocated  memory block
  18352.  
  18353.                                   %@AI@%size%@AE@%        New size in bytes
  18354.  
  18355. %@AB@%Returns%@AE@%                           A pointer to the reallocated memory if 
  18356.                                   successful,  %@AB@%NULL%@AE@%
  18357.  
  18358.                                   if not
  18359.  
  18360. %@NL@%
  18361. %@2@%%@CR:C6A-B0013   @%%@AB@%Process-Control Routines%@AE@%%@EH@%%@NL@%
  18362. %@NL@%
  18363. The term "process" refers to a program being executed by the operating
  18364. system.  %@NL@%
  18365. %@NL@%
  18366. Use the process-control routines to  %@NL@%
  18367. %@NL@%
  18368. %@NL@%
  18369.   ■   Terminate a process (%@AB@%abort%@AE@%, %@AB@%exit%@AE@%, and %@AB@%_exit%@AE@%)%@NL@%
  18370. %@NL@%
  18371.   ■   Call a new function when a process terminates (%@AB@%atexit%@AE@%)%@NL@%
  18372. %@NL@%
  18373.   ■   Start a new process (%@AB@%system%@AE@%) %@NL@%
  18374. %@NL@%
  18375. %@NL@%
  18376. Use the %@AB@%abort%@AE@% and %@AB@%_exit%@AE@% functions to exit without flushing stream buffers.
  18377. Use the %@AB@%exit%@AE@% function to exit after flushing stream buffers.  %@NL@%
  18378. %@NL@%
  18379. Use the %@AB@%atexit%@AE@% function to create a list of functions to be executed when
  18380. the calling program exits.  %@NL@%
  18381. %@NL@%
  18382. Use the %@AB@%system%@AE@% call to execute a given MS-DOS command.  %@NL@%
  18383. %@NL@%
  18384. ────────────────────────────────────────────────────────────────────────────
  18385. %@AB@%abort%@AE@% - Aborts a process.%@NL@%
  18386. %@NL@%
  18387. %@AB@%Include%@AE@%                           PROCESS.H or STDLIB.H
  18388.  
  18389. %@AB@%Prototype%@AE@%                         %@AB@%void abort( void );%@AE@%
  18390.  
  18391. %@AB@%Arguments%@AE@%                         None
  18392.  
  18393. %@AB@%Returns%@AE@%                           Void
  18394.  
  18395. ────────────────────────────────────────────────────────────────────────────
  18396. %@AB@%atexit%@AE@% - Executes functions at program termination.%@NL@%
  18397. %@NL@%
  18398. %@AB@%Include%@AE@%                           STDLIB.H
  18399.  
  18400. %@AB@%Prototype%@AE@%                         %@AB@%int atexit( void (*%@AE@%%@AI@%func%@AE@%%@AB@%)( void  ) );%@AE@%
  18401.  
  18402. %@AB@%Argument%@AE@%                          %@AI@%func%@AE@%        Function to be called
  18403.  
  18404. %@AB@%Returns%@AE@%                           A pointer to %@AI@%func%@AE@% if successful, %@AB@%NULL%@AE@%  
  18405.                                   if not
  18406.  
  18407. ────────────────────────────────────────────────────────────────────────────
  18408. %@AB@%exit, _exit%@AE@% - Terminates the process after flushing buffers (%@AB@%exit%@AE@%);
  18409. terminates the process without flushing buffers (%@AB@%_exit%@AE@%).%@NL@%
  18410. %@NL@%
  18411. %@AB@%Include%@AE@%                           PROCESS.H or%@AB@% %@AE@%STDLIB.H%@AB@%%@AE@%
  18412.  
  18413. %@AB@%Prototypes%@AE@%                        %@AB@%void exit( int %@AE@%%@AI@%status %@AE@%%@AB@%);%@AE@%
  18414.  
  18415.  
  18416.  
  18417.                                   %@AB@%void _exit( int %@AE@%%@AI@%status%@AE@%%@AB@% );%@AE@%
  18418.  
  18419. %@AB@%Argument%@AE@%                          %@AI@%status%@AE@%      Exit status
  18420.  
  18421. %@AB@%Returns%@AE@%                           Void
  18422.  
  18423. ────────────────────────────────────────────────────────────────────────────
  18424. %@AB@%system%@AE@% - Executes an MS-DOS command.%@NL@%
  18425. %@NL@%
  18426. %@AB@%Include%@AE@%                           PROCESS.H, STDLIB.H
  18427.  
  18428. %@AB@%Prototype%@AE@%                         %@AB@%int system( const char *%@AE@%%@AI@%command %@AE@%%@AB@%);%@AE@%
  18429.  
  18430. %@AB@%Argument%@AE@%                          %@AI@%command%@AE@%     Command to be executed%@AB@%%@AE@%
  18431.  
  18432. %@AB@%Returns%@AE@%                           0 if successful, -1 if not
  18433.  
  18434.                                   %@AB@%errno%@AE@%:  %@AB@%E2BIG, ENOENT, ENOEXEC, ENOMEM%@AE@%
  18435.  
  18436.  
  18437.  
  18438.  
  18439.  
  18440. %@NL@%
  18441. %@2@%%@CR:C6A-B0014   @%%@AB@%Searching and Sorting Routines%@AE@%%@EH@%%@NL@%
  18442. %@NL@%
  18443. The %@AB@%bsearch%@AE@%, %@AB@%lfind%@AE@%, %@AB@%lsearch%@AE@%, and %@AB@%qsort%@AE@% routines provide helpful
  18444. binary-search, linear-search, and quick-sort utilities.  %@NL@%
  18445. %@NL@%
  18446. ────────────────────────────────────────────────────────────────────────────
  18447. %@AB@%bsearch%@AE@% - Performs a binary search.%@NL@%
  18448. %@NL@%
  18449. %@AB@%Includes%@AE@%                          STDLIB.H, SEARCH.H
  18450.  
  18451. %@AB@%Prototype%@AE@%                         %@AB@%void *bsearch( const void *%@AE@%%@AI@%key%@AE@%%@AB@%,  const %@AE@%
  18452.                                   %@AB@%void *%@AE@%%@AI@%base%@AE@%%@AB@%,%@AE@%
  18453.  
  18454.                                   size_t %@AI@%num%@AE@%%@AB@%, size_t %@AE@%%@AI@%width%@AE@%%@AB@%, int( *%@AE@%%@AI@%compare %@AE@%
  18455.                                   %@AB@%)(  const void *%@AE@%%@AI@%elem1%@AE@%%@AB@%,%@AE@%
  18456.  
  18457.                                   const void *%@AI@%elem2%@AE@%%@AB@% ) );%@AE@%
  18458.  
  18459. %@AB@%Arguments%@AE@%                         %@AI@%key%@AE@%         Object to search for
  18460.  
  18461.                                   %@AI@%base%@AE@%        Pointer to base of search 
  18462.                                               data
  18463.  
  18464.                                   %@AI@%num%@AE@%         Number of elements
  18465.  
  18466.                                   %@AI@%width%@AE@%       Width of elements
  18467.  
  18468.                                   %@AI@%compare%@AE@%     Compare function
  18469.  
  18470.                                   %@AI@%elem1%@AE@%, %@AI@%%@AE@%     Array elements to compare
  18471.                                   %@AI@%elem2%@AE@%       
  18472.  
  18473. %@AB@%Returns%@AE@%                           A pointer if successful, %@AB@%NULL%@AE@% if  not
  18474.  
  18475. ────────────────────────────────────────────────────────────────────────────
  18476. %@AB@%lfind, lsearch%@AE@% - Performs a linear search for given value. If the value is
  18477. not found, %@AB@%lsearch%@AE@% adds it to the end of the list.%@NL@%
  18478. %@NL@%
  18479. %@AB@%Includes%@AE@%                          STDLIB.H, SEARCH.H
  18480.  
  18481. %@AB@%Prototypes%@AE@%                        %@AB@%char *lfind( char *%@AE@%%@AI@%key%@AE@%%@AB@%,  char *%@AE@%%@AI@%base%@AE@%%@AB@%, %@AE@%
  18482.                                   %@AB@%unsigned *%@AE@%%@AI@%num%@AE@%%@AB@%,%@AE@%
  18483.  
  18484.                                   unsigned %@AI@%width%@AE@%%@AB@%, int( *%@AE@%%@AI@%compare %@AE@%%@AB@%)( const %@AE@%
  18485.                                   %@AB@%void  *%@AE@%%@AI@%elem1%@AE@%%@AB@%,%@AE@%
  18486.  
  18487.                                   const void *%@AI@%elem2%@AE@%%@AB@% ) );%@AE@%
  18488.  
  18489.  
  18490.  
  18491.                                   char *lsearch( const char *%@AI@%key%@AE@%%@AB@%, const %@AE@%
  18492.                                   %@AB@%char  *%@AE@%%@AI@%base%@AE@%%@AB@%,%@AE@%
  18493.  
  18494.                                   unsigned *%@AI@%num%@AE@%%@AB@%, unsigned %@AE@%%@AI@%width%@AE@%%@AB@%, int( *%@AE@%%@AI@%%@AE@%
  18495.                                   %@AI@%compare  %@AE@%%@AB@%)%@AE@%
  18496.  
  18497.                                   ( const void *%@AI@%elem1%@AE@%%@AB@%, const void *%@AE@%%@AI@%elem2 %@AE@%%@AB@%)%@AE@%
  18498.                                   %@AB@%);%@AE@%
  18499.  
  18500. %@AB@%Arguments%@AE@%                         %@AI@%key%@AE@%         Object to search for
  18501.  
  18502.                                   %@AI@%base%@AE@%        Pointer to base of search 
  18503.                                               data
  18504.  
  18505.                                   %@AI@%num%@AE@%         Number of elements
  18506.  
  18507.                                   %@AI@%width%@AE@%       Width of elements
  18508.  
  18509.                                   %@AI@%compare%@AE@%     Compare function
  18510.  
  18511.                                   %@AI@%elem1%@AE@%, %@AI@%%@AE@%     Array elements to compare
  18512.                                   %@AI@%elem2%@AE@%       
  18513.  
  18514. %@AB@%Returns%@AE@%                           A pointer if successful, %@AB@%NULL%@AE@% if  not
  18515.  
  18516. ────────────────────────────────────────────────────────────────────────────
  18517. %@AB@%qsort%@AE@% - Performs a quick sort.%@NL@%
  18518. %@NL@%
  18519. %@AB@%Includes%@AE@%                          STDLIB.H, SEARCH.H
  18520.  
  18521. %@AB@%Prototype%@AE@%                         %@AB@%void qsort( void *%@AE@%%@AI@%base%@AE@%%@AB@%, size_t  %@AE@%%@AI@%num%@AE@%%@AB@%, %@AE@%
  18522.                                   %@AB@%size_t %@AE@%%@AI@%width%@AE@%%@AB@%,%@AE@%
  18523.  
  18524.                                   int( *%@AI@%compare%@AE@%%@AB@% )( const void *%@AE@%%@AI@%elem1%@AE@%%@AB@%, %@AE@%
  18525.                                   %@AB@%const  void *%@AE@%%@AI@%elem2 %@AE@%%@AB@%) );%@AE@%
  18526.  
  18527. %@AB@%Arguments%@AE@%                         %@AI@%base%@AE@%        Start of target array
  18528.  
  18529.                                   %@AI@%num%@AE@%         Array size in elements
  18530.  
  18531.                                   %@AI@%width%@AE@%       Element size in bytes
  18532.  
  18533.                                   %@AI@%compare%@AE@%     Compare function
  18534.  
  18535.                                   %@AI@%elem1%@AE@%, %@AI@%%@AE@%     Array elements to compare
  18536.                                   %@AI@%elem2%@AE@%       
  18537.  
  18538. %@AB@%Returns%@AE@%                           Void%@AB@%%@AE@%
  18539.  
  18540. %@NL@%
  18541. %@2@%%@CR:C6A-B0015   @%%@AB@%String-Manipulation Routines%@AE@%%@EH@%%@NL@%
  18542. %@NL@%
  18543. A wide variety of string routines is available in the run-time library. With
  18544. these functions, you can do the following:  %@NL@%
  18545. %@NL@%
  18546. %@NL@%
  18547.   ■   Copy strings (%@AB@%strcat%@AE@%, %@AB@%strcpy%@AE@%, %@AB@%strdup%@AE@%, %@AB@%strncat%@AE@%, %@AB@%strncpy%@AE@%)%@NL@%
  18548. %@NL@%
  18549.   ■   Search for strings, individual characters, or characters from a given
  18550.       set (%@AB@%strchr%@AE@%, %@AB@%strcspn%@AE@%, %@AB@%strpbrk%@AE@%, %@AB@%strrchr%@AE@%, %@AB@%strspn%@AE@%, %@AB@%strstr%@AE@%)%@NL@%
  18551. %@NL@%
  18552.   ■   Perform string comparisons (%@AB@%strcmp%@AE@%, %@AB@%strcmpi%@AE@%, %@AB@%stricmp%@AE@%, %@AB@%strncmp%@AE@%,
  18553.       %@AB@%strnicmp%@AE@%)%@NL@%
  18554. %@NL@%
  18555.   ■   Find the length of a string (%@AB@%strlen%@AE@%)%@NL@%
  18556. %@NL@%
  18557.   ■   Convert strings to a different case (%@AB@%strlwr%@AE@%, %@AB@%strupr%@AE@%)%@NL@%
  18558. %@NL@%
  18559.   ■   Set characters of the string to a given character (%@AB@%strnset%@AE@%, %@AB@%strset%@AE@%)%@NL@%
  18560. %@NL@%
  18561.   ■   Break strings into tokens (%@AB@%strtok%@AE@%)%@NL@%
  18562. %@NL@%
  18563. %@NL@%
  18564. All string functions work on null-terminated character strings.  %@NL@%
  18565. %@NL@%
  18566. Use the buffer-manipulation routines described earlier in this appendix for
  18567. manipulating character arrays that do not end with a null character.  %@NL@%
  18568. %@NL@%
  18569. ────────────────────────────────────────────────────────────────────────────
  18570. %@AB@%strcat, strcpy, strdup, strncat, strncpy%@AE@% - Use these routines to copy and
  18571. concatenate strings. The list below describes each function.%@NL@%
  18572. %@NL@%
  18573. %@AB@%Function%@AE@%                          %@AB@%Action%@AE@%
  18574. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  18575. %@AB@%strcat%@AE@%                            Append (concatenate) a string
  18576.  
  18577. %@AB@%strcpy%@AE@%                            Copy one string to another
  18578.  
  18579. %@AB@%strdup%@AE@%                            Duplicate a string
  18580.  
  18581. %@AB@%strncat%@AE@%                           Append a specified number of characters 
  18582.                                   to a string
  18583.  
  18584. %@AB@%strncpy%@AE@%                           Copy a specified number of characters 
  18585.                                   from one string to another
  18586.  
  18587. %@AB@%Include%@AE@%                           STRING.H
  18588.  
  18589. %@AB@%Prototypes%@AE@%                        %@AB@%char *strcat( char *%@AE@%%@AI@%dest%@AE@%%@AB@%,  const char *%@AE@%%@AI@%%@AE@%
  18590.                                   %@AI@%src%@AE@% );
  18591.  
  18592.  
  18593.  
  18594.                                   char *strcpy( char *%@AI@%dest%@AE@%%@AB@%, const char *%@AE@%%@AI@%%@AE@%
  18595.                                   %@AI@%src%@AE@%  );
  18596.  
  18597.  
  18598.  
  18599.                                   char *strdup( const char *%@AI@%string %@AE@%%@AB@%);%@AE@%
  18600.  
  18601.  
  18602.  
  18603.                                   char *strncat( char *%@AI@%dest%@AE@%%@AB@%, const char *%@AE@%%@AI@%%@AE@%
  18604.                                   %@AI@%src%@AE@%%@AB@%,  size_t %@AE@%%@AI@%n%@AE@%%@AB@% );%@AE@%
  18605.  
  18606.  
  18607.  
  18608.                                   char *strncpy( char *%@AI@%dest%@AE@%%@AB@%, const char *%@AE@%%@AI@%%@AE@%
  18609.                                   %@AI@%src%@AE@%%@AB@%,  size_t %@AE@%%@AI@%n %@AE@%%@AB@%);%@AE@%
  18610.  
  18611. %@AB@%Arguments%@AE@%                         %@AI@%dest%@AE@%        Destination string
  18612.  
  18613.                                   %@AI@%src%@AE@%         Source string
  18614.  
  18615.                                   %@AI@%string%@AE@%      Null-terminated string
  18616.  
  18617.                                   %@AI@%n%@AE@%           Number of characters
  18618.  
  18619. %@AB@%Returns%@AE@%                           %@AB@%strcat:%@AE@% a pointer to the concatenated 
  18620.                                   string
  18621.  
  18622.                                   %@AB@%strcpy%@AE@%: %@AI@%dest%@AE@% string
  18623.  
  18624.                                   %@AB@%strdup%@AE@%: a pointer if successful, %@AB@%NULL%@AE@% if
  18625.                                   not
  18626.  
  18627.                                   %@AB@%strncat, strncpy%@AE@%: a pointer to%@AI@% dest%@AE@% 
  18628.                                   string
  18629.  
  18630. ────────────────────────────────────────────────────────────────────────────
  18631. %@AB@%strchr, strcspn, strpbrk, strrchr, strspn, strstr%@AE@% - Use these routines to
  18632. search strings. The list below describes each function.%@NL@%
  18633. %@NL@%
  18634. %@AB@%Function%@AE@%                          %@AB@%Action%@AE@%
  18635. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  18636. %@AB@%strchr%@AE@%                            Finds first occurrence of a given 
  18637.                                   character in a string
  18638.  
  18639. %@AB@%strcspn%@AE@%                           Finds first occurrence of a character 
  18640.                                   from a given character set in a string 
  18641.  
  18642. %@AB@%strpbrk%@AE@%                           Finds first occurrence of a character 
  18643.                                   from one string in another
  18644.  
  18645. %@AB@%strrchr%@AE@%                           Finds last occurrence of a given 
  18646.                                   character in a string
  18647.  
  18648. %@AB@%strspn%@AE@%                            Finds first substring from a given 
  18649.                                   character set in a string
  18650.  
  18651. %@AB@%strstr%@AE@%                            Finds first occurrence of a given string
  18652.                                   in another string
  18653.  
  18654. %@AB@%Include%@AE@%                           STRING.H
  18655.  
  18656. %@AB@%Prototypes%@AE@%                        %@AB@%char *strchr( const char *%@AE@%%@AI@%string%@AE@%%@AB@%,  int%@AE@%%@AI@% c%@AE@%
  18657.                                   %@AB@% );%@AE@%
  18658.  
  18659.  
  18660.  
  18661.                                   size_t strcspn( const char *%@AI@%string1%@AE@%%@AB@%, %@AE@%
  18662.                                   %@AB@%const char *%@AE@%%@AI@%string2  %@AE@%%@AB@%);%@AE@%
  18663.  
  18664.  
  18665.  
  18666.                                   char *strpbrk( const char *%@AI@%string1%@AE@%%@AB@%, %@AE@%
  18667.                                   %@AB@%const char  *%@AE@%%@AI@%string2%@AE@%%@AB@% );%@AE@%
  18668.  
  18669.  
  18670.  
  18671.                                   char *strrchr( const char *%@AI@%string%@AE@%%@AB@%, int %@AE@%%@AI@%c%@AE@%
  18672.                                   %@AB@%  );%@AE@%
  18673.  
  18674.  
  18675.  
  18676.                                   size_t strspn( const char *%@AI@%string1%@AE@%%@AB@%, %@AE@%
  18677.                                   %@AB@%const char *%@AE@%%@AI@%string2  %@AE@%%@AB@%);%@AE@%
  18678.  
  18679.  
  18680.  
  18681.                                   char *strstr( const char *%@AI@%string1%@AE@%%@AB@%, const%@AE@%
  18682.                                   %@AB@%char  *%@AE@%%@AI@%string2%@AE@%%@AB@% );%@AE@%
  18683.  
  18684. %@AB@%Arguments%@AE@%                         %@AI@%string%@AE@%, %@AI@%%@AE@%    Null-terminated  strings
  18685.                                   %@AI@%string1%@AE@%, %@AI@%%@AE@%   
  18686.                                   %@AI@%string2%@AE@%     
  18687.  
  18688.                                   %@AI@%c%@AE@%           Character
  18689.  
  18690. %@AB@%Returns%@AE@%                           %@AB@%strchr%@AE@%: a pointer if successful, %@AB@%NULL%@AE@%  
  18691.                                   if not
  18692.  
  18693.                                   %@AB@%strcspn%@AE@%: an offset into %@AI@%string1%@AE@%
  18694.  
  18695.                                   %@AB@%strpbrk%@AE@%: a pointer to the first matching
  18696.                                   character in %@AI@%string1%@AE@%,  
  18697.  
  18698.                                   %@AB@%NULL%@AE@% if no match is found
  18699.  
  18700.                                   %@AB@%strrchr%@AE@%: a pointer to the last 
  18701.                                   occurrence of %@AI@%c%@AE@% in%@AI@%  string%@AE@%, %@AB@%NULL%@AE@%
  18702.  
  18703.                                   if %@AI@%c%@AE@% is not found
  18704.  
  18705.                                   %@AB@%strspn%@AE@%: the position of the first 
  18706.                                   nonmatching character in  %@AI@%string1%@AE@%
  18707.  
  18708.                                   %@AB@%strstr%@AE@%: a pointer to the first 
  18709.                                   occurrence of%@AI@% string2%@AE@%  in %@AI@%string1%@AE@%,
  18710.  
  18711.                                   or %@AB@%NULL%@AE@% if %@AI@%string2%@AE@% is not found
  18712.  
  18713. ────────────────────────────────────────────────────────────────────────────
  18714. %@AB@%strcmp, strcmpi, stricmp, strncmp, strnicmp%@AE@% - Use these routines to compare
  18715. strings. The list below describes the operation of each function. An "%@AB@%n%@AE@%" in
  18716. the function name means to use up to %@AB@%n%@AE@% characters; "%@AB@%i%@AE@%" in the name means to
  18717. operate without regard to the case of the string.%@NL@%
  18718. %@NL@%
  18719. %@AB@%Function%@AE@%                          %@AB@%Action%@AE@%
  18720. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%
  18721. %@AB@%strcmp%@AE@%                            Compares two strings
  18722.  
  18723. %@AB@%strcmpi%@AE@%                           Compares two strings without regard to 
  18724.                                   case ("%@AB@%i%@AE@%" 
  18725.                                   indicates that this function is case 
  18726.                                   insensitive)
  18727.  
  18728. %@AB@%stricmp%@AE@%                           Compares two strings without regard to 
  18729.                                   case (identical to %@AB@%strcmpi%@AE@%)
  18730.  
  18731. %@AB@%strncmp%@AE@%                           Compares characters of two strings
  18732.  
  18733. %@AB@%strnicmp%@AE@%                          Compares characters of two strings 
  18734.                                   without regard to case
  18735.  
  18736. %@AB@%Include%@AE@%                           STRING.H
  18737.  
  18738. %@AB@%Prototypes%@AE@%                        %@AB@%int strcmp( const char *%@AE@%%@AI@%string1%@AE@%%@AB@%,  const %@AE@%
  18739.                                   %@AB@%char *%@AE@%%@AI@%string2 %@AE@%%@AB@%);%@AE@%
  18740.  
  18741.  
  18742.  
  18743.                                   int strcmpi( const char *%@AI@%string1%@AE@%%@AB@%, const %@AE@%
  18744.                                   %@AB@%char *%@AE@%%@AI@%string2%@AE@%%@AB@%  );%@AE@%
  18745.  
  18746.  
  18747.  
  18748.                                   int stricmp( const char *%@AI@%string1%@AE@%%@AB@%, const %@AE@%
  18749.                                   %@AB@%char *%@AE@%%@AI@%string2  %@AE@%%@AB@%);%@AE@%
  18750.  
  18751.  
  18752.  
  18753.                                   int strncmp( const char *%@AI@%string1%@AE@%%@AB@%, const %@AE@%
  18754.                                   %@AB@%char *%@AE@%%@AI@%string2%@AE@%%@AB@%,  size_t %@AE@%%@AI@%n%@AE@%%@AB@% );%@AE@%
  18755.  
  18756.  
  18757.  
  18758.                                   int strnicmp( const char *%@AI@%string1%@AE@%%@AB@%, const%@AE@%
  18759.                                   %@AB@%char *%@AE@%%@AI@%string2%@AE@%%@AB@%,  size_t %@AE@%%@AI@%n %@AE@%%@AB@%);%@AE@%
  18760.  
  18761. %@AB@%Arguments%@AE@%                         %@AI@%string1%@AE@%     Destination string
  18762.  
  18763.                                   %@AI@%string2%@AE@%     Source string
  18764.  
  18765.                                   %@AI@%n%@AE@%           Number of characters
  18766.  
  18767. %@AB@%Returns%@AE@%                           A negative value if %@AI@%string1%@AE@%, 0  if %@AI@%%@AE@%
  18768.                                   %@AI@%string1%@AE@% = %@AI@%string2%@AE@%,
  18769.  
  18770.                                   a positive value if%@AI@% string1>string2%@AE@%
  18771.  
  18772. ────────────────────────────────────────────────────────────────────────────
  18773. %@AB@%strlen%@AE@% - The %@AB@%strlen%@AE@% function returns the length in bytes of the string, not
  18774. including the terminating null character (%@AB@%\0%@AE@%).%@NL@%
  18775. %@NL@%
  18776. %@AB@%Include%@AE@%                           STRING.H
  18777.  
  18778. %@AB@%Prototype%@AE@%                         %@AB@%size_t strlen( const char *%@AE@%%@AI@%string%@AE@%%@AB@%  );%@AE@%
  18779.  
  18780. %@AB@%Argument%@AE@%                          %@AI@%string%@AE@%      Null-terminated string
  18781.  
  18782. %@AB@%Returns%@AE@%                           The string length
  18783.  
  18784. ────────────────────────────────────────────────────────────────────────────
  18785. %@AB@%strlwr, strupr%@AE@% - The %@AB@%strlwr%@AE@% and %@AB@%strupr%@AE@% routines convert the characters of a
  18786. string to lowercase and uppercase, respectively.%@NL@%
  18787. %@NL@%
  18788. %@AB@%Include%@AE@%                           STRING.H
  18789.  
  18790. %@AB@%Prototypes%@AE@%                        %@AB@%char *strlwr( char *%@AE@%%@AI@%string  %@AE@%%@AB@%);%@AE@%
  18791.  
  18792.  
  18793.  
  18794.                                   %@AB@%char *strupr( char *%@AE@%%@AI@%string %@AE@%%@AB@%);%@AE@%
  18795.  
  18796. %@AB@%Argument%@AE@%                          %@AI@%string%@AE@%      String to be converted
  18797.  
  18798. %@AB@%Returns%@AE@%                           A pointer to a copy of the converted 
  18799.                                   input string
  18800.  
  18801. ────────────────────────────────────────────────────────────────────────────
  18802. %@AB@%strnset, strset%@AE@% - The routines %@AB@%strnset%@AE@% and %@AB@%strset%@AE@% set the characters of a
  18803. string to a specified character. The%@AB@% strnset%@AE@% function sets the first %@AI@%n%@AE@%
  18804. characters in the string to the specified character. The %@AB@%strset%@AE@% function
  18805. sets the entire string to the specified character.%@NL@%
  18806. %@NL@%
  18807. %@AB@%Include%@AE@%                           STRING.H
  18808.  
  18809. %@AB@%Prototypes%@AE@%                        %@AB@%char *strnset( char *%@AE@%%@AI@%string%@AE@%%@AB@%,  int %@AE@%%@AI@%c%@AE@%%@AB@%, %@AE@%
  18810.                                   %@AB@%size_t %@AE@%%@AI@%n%@AE@%%@AB@% );%@AE@%
  18811.  
  18812.  
  18813.  
  18814.                                   char*strset( char *%@AB@%string, int c );%@AE@%
  18815.  
  18816. %@AB@%Arguments%@AE@%                         %@AI@%string%@AE@%      String to be set
  18817.  
  18818.                                   %@AI@%c%@AE@%           Character setting
  18819.  
  18820.                                   %@AI@%n%@AE@%           Number of characters set
  18821.  
  18822. %@AB@%Returns%@AE@%                           A pointer to %@AI@%string%@AE@%
  18823.  
  18824.  
  18825.  
  18826.  
  18827.  
  18828.  
  18829.  
  18830. ────────────────────────────────────────────────────────────────────────────
  18831. %@AB@%strtok%@AE@% - The %@AB@%strtok%@AE@% function finds a token in a string. A "token" is a
  18832. series of characters delimited by a character from a specified set. For
  18833. example, use the %@AB@%strtok%@AE@% function to break an input line into the component
  18834. words.%@NL@%
  18835. %@NL@%
  18836. %@AB@%Include%@AE@%                           STRING.H
  18837.  
  18838. %@AB@%Prototype%@AE@%                         %@AB@%char *strtok( char *%@AE@%%@AI@%string1%@AE@%%@AB@%,  const char%@AE@%
  18839.                                   %@AB@%*%@AE@%%@AI@%string2%@AE@%%@AB@% );%@AE@%
  18840.  
  18841. %@AB@%Arguments%@AE@%                         %@AI@%string1%@AE@%     String containing tokens
  18842.  
  18843.                                   %@AI@%string2%@AE@%     Set of delimiter characters
  18844.  
  18845. %@AB@%Returns%@AE@%                           A pointer to a token in %@AI@%string1%@AE@%
  18846.  
  18847. %@NL@%
  18848. %@2@%%@CR:C6A-B0016   @%%@AB@%Time Routines%@AE@%%@EH@%%@NL@%
  18849. %@NL@%
  18850. Use the time routines to get the current time, convert it to a convenient
  18851. format, and store it according to your particular needs.  %@NL@%
  18852. %@NL@%
  18853. The current time is always taken from the system time.  %@NL@%
  18854. %@NL@%
  18855. The %@AB@%time%@AE@% function returns the current time as the number of seconds elapsed
  18856. since Greenwich mean time, January 1, 1970.  %@NL@%
  18857. %@NL@%
  18858. Use the %@AB@%asctime%@AE@%, %@AB@%ctime%@AE@%, %@AB@%gmtime%@AE@%, and %@AB@%mktime%@AE@% functions to manipulate the time
  18859. value.  %@NL@%
  18860. %@NL@%
  18861. ────────────────────────────────────────────────────────────────────────────
  18862. %@AB@%asctime%@AE@% - Converts a time from a structure to a character string.%@NL@%
  18863. %@NL@%
  18864. %@AB@%Include%@AE@%                           TIME.H
  18865.  
  18866. %@AB@%Prototype%@AE@%                         %@AB@%char *asctime( const struct tm *%@AE@%%@AI@%timeptr %@AE@%
  18867.                                   %@AI@%%@AE@%%@AB@%);%@AE@%
  18868.  
  18869. %@AB@%Argument%@AE@%                          %@AI@%timeptr%@AE@%     Time structure
  18870.  
  18871. %@AB@%Returns%@AE@%                           A pointer to string result
  18872.  
  18873. ────────────────────────────────────────────────────────────────────────────
  18874. %@AB@%clock%@AE@% - Returns the elapsed CPU time for a process.%@NL@%
  18875. %@NL@%
  18876. %@AB@%Include%@AE@%                           TIME.H
  18877.  
  18878. %@AB@%Prototype%@AE@%                         %@AB@%clock_t clock( void );%@AE@%
  18879.  
  18880. %@AB@%Arguments%@AE@%                         None
  18881.  
  18882. %@AB@%Returns%@AE@%                           The elapsed processor time if successful,
  18883.                                   -1  if not
  18884.  
  18885. ────────────────────────────────────────────────────────────────────────────
  18886. %@AB@%ctime%@AE@% - Converts time from a long integer to a character string.%@NL@%
  18887. %@NL@%
  18888. %@AB@%Include%@AE@%                           TIME.H
  18889.  
  18890. %@AB@%Prototype%@AE@%                         %@AB@%char *ctime( const time_t *%@AE@%%@AI@%timer  %@AE@%%@AB@%);%@AE@%
  18891.  
  18892. %@AB@%Argument%@AE@%                          %@AI@%timer%@AE@%       Pointer to stored time
  18893.  
  18894. %@AB@%Returns%@AE@%                           A pointer to string result; %@AB@%NULL%@AE@%  if 
  18895.                                   time represents a date
  18896.  
  18897.                                   before 1980
  18898.  
  18899. ────────────────────────────────────────────────────────────────────────────
  18900. %@AB@%difftime%@AE@% - Computes the difference between two times.%@NL@%
  18901. %@NL@%
  18902. %@AB@%Include%@AE@%                           TIME.H
  18903.  
  18904. %@AB@%Prototype%@AE@%                         %@AB@%double difftime( time_t %@AE@%%@AI@%timer1%@AE@%%@AB@%, time_t  %@AE@%
  18905.                                   %@AI@%timer0 %@AE@%%@AB@%);%@AE@%
  18906.  
  18907. %@AB@%Arguments%@AE@%                         %@AI@%timer0%@AE@%,%@AI@% %@AE@%    Beginning and ending  times
  18908.                                   %@AI@%timer1%@AE@%      
  18909.  
  18910. %@AB@%Returns%@AE@%                           The difference in elapsed time between%@AI@% %@AE@%
  18911.                                   %@AI@%timer1%@AE@%  and%@AI@% timer0%@AE@%
  18912.  
  18913. ────────────────────────────────────────────────────────────────────────────
  18914. %@AB@%ftime%@AE@% - Gets current system time as structure.%@NL@%
  18915. %@NL@%
  18916. %@AB@%Includes%@AE@%                          SYS\TYPES.H, SYS\TIMEB.H
  18917.  
  18918. %@AB@%Prototype%@AE@%                         %@AB@%void ftime( struct timeb *%@AE@%%@AI@%timeptr  %@AE@%%@AB@%);%@AE@%
  18919.  
  18920. %@AB@%Argument%@AE@%                          %@AI@%timeptr%@AE@%     Pointer to time structure
  18921.  
  18922. %@AB@%Returns%@AE@%                           Void
  18923.  
  18924. ────────────────────────────────────────────────────────────────────────────
  18925. %@AB@%gmtime%@AE@% - Converts time from integer to structure.%@NL@%
  18926. %@NL@%
  18927. %@AB@%Include%@AE@%                           TIME.H
  18928.  
  18929. %@AB@%Prototype%@AE@%                         %@AB@%struct tm *gmtime( const time_t *%@AE@%%@AI@%timer  %@AE@%
  18930.                                   %@AB@%);%@AE@%
  18931.  
  18932. %@AB@%Argument%@AE@%                          %@AI@%timer%@AE@%       Pointer to stored time
  18933.  
  18934. %@AB@%Returns%@AE@%                           A pointer to a structure
  18935.  
  18936. ────────────────────────────────────────────────────────────────────────────
  18937. %@AB@%mktime%@AE@% - Converts time to a calendar value.%@NL@%
  18938. %@NL@%
  18939. %@AB@%Include%@AE@%                           TIME.H
  18940.  
  18941. %@AB@%Prototype%@AE@%                         %@AB@%time_t mktime( struct tm *%@AE@%%@AI@%timeptr  %@AE@%%@AB@%);%@AE@%
  18942.  
  18943. %@AB@%Argument%@AE@%                          %@AI@%timeptr%@AE@%     Local time structure
  18944.  
  18945. %@AB@%Returns%@AE@%                           The encoded calendar time if successful,
  18946.                                   -1  if not
  18947.  
  18948. ────────────────────────────────────────────────────────────────────────────
  18949. %@AB@%time%@AE@% - Gets current system time as a long integer.%@NL@%
  18950. %@NL@%
  18951. %@AB@%Include%@AE@%                           TIME.H
  18952.  
  18953. %@AB@%Prototype%@AE@%                         %@AB@%time_t time( time_t *%@AE@%%@AI@%timer%@AE@%%@AB@% );%@AE@%
  18954.  
  18955. %@AB@%Argument%@AE@%                          %@AI@%timer%@AE@%       Storage location for time
  18956.  
  18957. %@AB@%Returns%@AE@%                           The elapsed time
  18958.  
  18959. %@NL@%
  18960. %@NL@%
  18961. %@NL@%
  18962.  %@NL@%
  18963. %@NL@%
  18964. %@NL@%
  18965. %@NL@%
  18966. %@NL@%
  18967. %@CR:C6A-B0028   @%%@1@%%@AB@%Glossary%@AE@%%@EH@%%@NL@%
  18968. %@AB@%────────────────────────────────────────────────────────────────────────────%@AE@%%@NL@%%@NL@%
  18969. %@NL@%
  18970. %@2@%%@AB@%8087 or 80287 coprocessor:%@AE@%%@EH@%%@NL@%
  18971. %@3@%Intel hardware products that provide very fast and precise floating-point%@EH@%
  18972. number processing.%@NL@%%@NL@%
  18973. %@NL@%
  18974. %@2@%%@AB@%%@CR:C6A00170029 @%%@CR:C6A00170030 @%aggregate types:%@AE@%%@EH@%%@NL@%
  18975. %@3@%Arrays, structures, and unions.%@NL@%%@NL@%%@EH@%
  18976. %@NL@%
  18977. %@2@%%@AB@%%@CR:C6A00170031 @%ANSI (American National Standards Institute):%@AE@%%@EH@%%@NL@%
  18978. %@3@%The national institute responsible for defining programming-language%@EH@%
  18979. standards to promote portability of these languages between different
  18980. computer systems. The ANSI standard for C will become official in 1990.%@NL@%%@NL@%
  18981. %@NL@%
  18982. %@2@%%@AB@%argc:%@AE@%%@EH@%%@NL@%
  18983. %@3@%The traditional name for the first argument to the %@AB@%main%@AE@% function in a C%@EH@%
  18984. source program. It is an integer that specifies how many arguments are
  18985. passed to the program from the command line.%@NL@%%@NL@%
  18986. %@NL@%
  18987. %@2@%%@AB@%%@CR:C6A00170032 @%argument:%@AE@%%@EH@%%@NL@%
  18988. %@3@%A value passed to a function.%@NL@%%@NL@%%@EH@%
  18989. %@NL@%
  18990. %@2@%%@AB@%argv:%@AE@%%@EH@%%@NL@%
  18991. %@3@%The traditional name for the second argument to the %@AB@%main%@AE@% function in a C%@EH@%
  18992. source program. It is a pointer to an array of strings. Traditionally, the
  18993. first string is the program name, and each following string is an argument
  18994. passed to the program from the command line.%@NL@%%@NL@%
  18995. %@NL@%
  18996. %@2@%%@AB@%%@CR:C6A00170033 @%array:%@AE@%%@EH@%%@NL@%
  18997. %@3@%A set of elements with the same type.%@NL@%%@NL@%%@EH@%
  18998. %@NL@%
  18999. %@2@%%@AB@%array pointer:%@AE@%%@EH@%%@NL@%
  19000. %@3@%A pointer that holds the address of any element of an array.%@NL@%%@NL@%%@EH@%
  19001. %@NL@%
  19002. %@2@%%@AB@%%@CR:C6A00170034 @%ASCII (American Standard Code for Information Interchange):%@AE@%%@EH@%%@NL@%
  19003. %@3@%A set of 256 codes that many computers use to represent letters, digits,%@EH@%
  19004. special characters, and other symbols. Only the first 128 of these codes are
  19005. standardized; the remaining 128 are special characters that are defined by
  19006. the computer manufacturer.%@NL@%%@NL@%
  19007. %@NL@%
  19008. %@2@%%@AB@%%@CR:C6A00170035 @%%@CR:C6A00170036 @%automatic variable:%@AE@%%@EH@%%@NL@%
  19009. %@3@%A variable, declared in a block, whose value is discarded when the program%@EH@%
  19010. exits from the block. See "static variable" and "lifetime."%@NL@%%@NL@%
  19011. %@NL@%
  19012. %@2@%%@AB@%background color:%@AE@%%@EH@%%@NL@%
  19013. %@3@%A long integer representing the background color of the display screen. In%@EH@%
  19014. graphics modes, the background color applies to the entire screen. In text
  19015. modes, the background color specifies the text background for each
  19016. character. See "foreground color."%@NL@%%@NL@%
  19017. %@NL@%
  19018. %@2@%%@AB@%basic data types:%@CR:C6A00170037 @%%@CR:C6A00170038 @%%@AE@%%@EH@%%@NL@%
  19019. %@3@%The integral, enumerated, floating-point, and pointer types in the C%@EH@%
  19020. language.%@NL@%%@NL@%
  19021. %@NL@%
  19022. %@2@%%@AB@%binary file:%@CR:C6A00170039 @%%@AE@%%@EH@%%@NL@%
  19023. %@3@%A file that is not used for text processing. It may be an executable file, a%@EH@%
  19024. data file, or some other nontext file.%@NL@%%@NL@%
  19025. %@NL@%
  19026. %@2@%%@AB@%binary format:%@CR:C6A00170040 @%%@AE@%%@EH@%%@NL@%
  19027. %@3@%A method of data representation in which data are stored directly from%@EH@%
  19028. memory to disk with no translations. In binary format, numeric values are
  19029. stored as binary numbers and are not translated to ASCII characters.%@NL@%%@NL@%
  19030. %@NL@%
  19031. %@2@%%@AB@%binary mode:%@CR:C6A00170041 @%%@AE@%%@EH@%%@NL@%
  19032. %@3@%A method of accessing files in which no translations are performed. There is%@EH@%
  19033. no specific end-of-file character.%@NL@%%@NL@%
  19034. %@NL@%
  19035. %@2@%%@AB@%binary operator:%@CR:C6A00170042 @%%@CR:C6A00170043 @%%@AE@%%@EH@%%@NL@%
  19036. %@3@%An operator that takes two operands. Binary operators in the C language are%@EH@%
  19037. the multiplicative operators (%@AB@%*/%@AE@%), additive operators (%@AB@%+ -%@AE@%), shift operators
  19038. (%@AB@%<<  >>%@AE@%), relational operators (%@AB@%< >>  <=  >=  ==  !=%@AE@%), bitwise operators (%@AB@%&
  19039. %@AB@%| ^%@AE@%), logical operators (%@AB@%&& ||%@AE@%), and the sequential-evaluation operator (%@AB@%,%@AE@%).%@NL@%%@NL@%
  19040. %@NL@%
  19041. %@2@%%@AB@%bit:%@CR:C6A00170044 @%%@AE@%%@EH@%%@NL@%
  19042. %@3@%A binary digit (either 0 or 1), the smallest unit of information used with%@EH@%
  19043. computers. Eight bits make up one byte.%@NL@%%@NL@%
  19044. %@NL@%
  19045. %@2@%%@AB@%bit field:%@CR:C6A00170045 @%%@CR:C6A00170046 @%%@AE@%%@EH@%%@NL@%
  19046. %@3@%A type of structure that allows manipulation of individual bits or groups of%@EH@%
  19047. bits. %@NL@%%@NL@%
  19048. %@NL@%
  19049. %@2@%%@AB@%bit-mapped font:%@CR:C6A00170047 @%%@AE@%%@EH@%%@NL@%
  19050. %@3@%A font in which each character is defined by (mapped to) the bits of an%@EH@%
  19051. array.%@NL@%%@NL@%
  19052. %@NL@%
  19053. %@2@%%@AB@%bitwise operator:%@CR:C6A00170048 @%%@CR:C6A00170049 @%%@CR:C6A00170050 @%%@CR:C6A00170051 @%%@CR:C6A00170052 @%%@CR:C6A00170053 @%%@CR:C6A00170054 @%%@CR:C6A00170055 @%%@CR:C6A00170056 @%%@CR:C6A00170057 @%%@CR:C6A00170058 @%%@EH@%
  19054. %@AB@%%@CR:C6A00170059 @%%@CR:C6A00170060 @%%@CR:C6A00170061 @%%@CR:C6A00170062 @%%@AE@%%@NL@%
  19055. %@3@%An operator used to manipulate bits in an integer expression. Bitwise%@EH@%
  19056. operators in the C language are %@AB@%&%@AE@% (AND),%@AB@% |%@AE@% (inclusive OR), %@AB@%^%@AE@% (exclusive OR),
  19057. %@AB@%%@AE@% (left shift), %@AB@%>>%@AE@% (right shift), and %@AB@%~%@AE@% (one's complement).%@NL@%%@NL@%
  19058. %@NL@%
  19059. %@2@%%@AB@%block:%@CR:C6A00170063 @%%@AE@%%@EH@%%@NL@%
  19060. %@3@%A sequence of declarations, definitions, and statements enclosed within%@EH@%
  19061. curly braces ({}).%@NL@%%@NL@%
  19062. %@NL@%
  19063. %@2@%%@AB@%bounding rectangle:%@CR:C6A00170064 @%%@AE@%%@EH@%%@NL@%
  19064. %@3@%An imaginary rectangle that defines the outer limits of a rounded shape such%@EH@%
  19065. as an ellipse, arc, or pie.%@NL@%%@NL@%
  19066. %@NL@%
  19067. %@2@%%@AB@%byte:%@CR:C6A00170065 @%%@AE@%%@EH@%%@NL@%
  19068. %@3@%The unit of measure used for computer memory and data storage. One byte%@EH@%
  19069. contains eight bits and can store one ASCII character.%@NL@%%@NL@%
  19070. %@NL@%
  19071. %@2@%%@AB@%case label:%@CR:C6A00170066 @%%@AE@%%@EH@%%@NL@%
  19072. %@3@%The %@AB@%case%@AE@% keyword and the constant, or constant expression, that follows it. %@NL@%%@NL@%%@EH@%
  19073. %@NL@%
  19074. %@2@%%@AB@%CGA:%@AE@%%@EH@%%@NL@%
  19075. %@3@%IBM's Color Graphics Adapter.%@NL@%%@NL@%%@EH@%
  19076. %@NL@%
  19077. %@2@%%@AB@%character code:%@AE@%%@EH@%%@NL@%
  19078. %@3@%A numeric code that represents a character. The default ASCII character set%@EH@%
  19079. used in all PCs and PS/2s comprises 256 eight-bit character codes.%@NL@%%@NL@%
  19080. %@NL@%
  19081. %@2@%%@AB@%character constant:%@CR:C6A00170067 @%%@AE@%%@EH@%%@NL@%
  19082. %@3@%A character enclosed in single quotes, for example, %@AS@%'p'%@AE@%. A character%@EH@%
  19083. constant has a type of %@AB@%char%@AE@%. See "string constant."%@NL@%%@NL@%
  19084. %@NL@%
  19085. %@2@%%@AB@%character set:%@AE@%%@EH@%%@NL@%
  19086. %@3@%A set of alphabetic and numeric characters and symbols.%@NL@%%@NL@%%@EH@%
  19087. %@NL@%
  19088. %@2@%%@AB@%clipping:%@AE@%%@EH@%%@NL@%
  19089. %@3@%The process of determining which parts of a graphics image lie within the%@EH@%
  19090. clipping region. Parts of the image that lie outside this region are
  19091. "clipped"; that is, they are not displayed.%@NL@%%@NL@%
  19092. %@NL@%
  19093. %@2@%%@AB@%clipping region:%@CR:C6A00170068 @%%@AE@%%@EH@%%@NL@%
  19094. %@3@%The rectangular area of the screen where graphics display occurs.%@NL@%%@NL@%%@EH@%
  19095. %@NL@%
  19096. %@2@%%@AB@%color index:%@CR:C6A00170069 @%%@AE@%%@EH@%%@NL@%
  19097. %@3@%A short integer that represents a displayable color. See "remapping" and%@EH@%
  19098. "color value."%@NL@%%@NL@%
  19099. %@NL@%
  19100. %@2@%%@AB@%color value:%@CR:C6A00170070 @%%@AE@%%@EH@%%@NL@%
  19101. %@3@%A long integer representing an absolute color. See "remapping" and "color%@EH@%
  19102. index."%@NL@%%@NL@%
  19103. %@NL@%
  19104. %@2@%%@AB@%command-line argument:%@AE@%%@EH@%%@NL@%
  19105. %@3@%A value passed to a program when the program begins execution.%@NL@%%@NL@%%@EH@%
  19106. %@NL@%
  19107. %@2@%%@AB@%%@CR:C6A00170071 @%conditional expression:%@AE@%%@EH@%%@NL@%
  19108. %@3@%An expression consisting of three operands joined by the ternary (%@AB@%? :%@AE@%)%@EH@%
  19109. operator. Similar to an %@AB@%if%@AE@%-%@AB@%else%@AE@% construct, a conditional expression is used
  19110. to evaluate either of two expressions depending on the value of a third
  19111. expression. %@NL@%%@NL@%
  19112. %@NL@%
  19113. %@2@%%@AB@%constant expression:%@CR:C6A00170072 @%%@CR:C6A00170073 @%%@AE@%%@EH@%%@NL@%
  19114. %@3@%An expression that evaluates to a constant. A constant expression may%@EH@%
  19115. involve integer constants, character constants, floating-point constants,
  19116. enumeration constants, type casts to integral and floating-point types, and
  19117. other constant expressions. %@NL@%%@NL@%
  19118. %@NL@%
  19119. %@2@%%@AB@%current color:%@CR:C6A00170074 @%%@CR:C6A00170075 @%%@AE@%%@EH@%%@NL@%
  19120. %@3@%The color index for the color in which graphics pixels are displayed. The%@EH@%
  19121. current color can be examined with %@AB@%_getcolor%@AE@% or changed with%@AB@% _setcolor%@AE@%. %@NL@%%@NL@%
  19122. %@NL@%
  19123. %@2@%%@AB@%declaration:%@CR:C6A00170076 @%%@AE@%%@EH@%%@NL@%
  19124. %@3@%A construct that associates the name and the attributes of a variable,%@EH@%
  19125. function, or type. %@NL@%%@NL@%
  19126. %@NL@%
  19127. %@2@%%@AB@%default:%@AE@%%@EH@%%@NL@%
  19128. %@3@%A condition that is assumed by a program if not specified.%@NL@%%@NL@%%@EH@%
  19129. %@NL@%
  19130. %@2@%%@AB@%definition:%@CR:C6A00170077 @%%@AE@%%@EH@%%@NL@%
  19131. %@3@%A construct that initializes and allocates storage for a variable or that%@EH@%
  19132. specifies the name, formal parameters, body, and return type of a function.%@NL@%%@NL@%
  19133. %@NL@%
  19134. %@2@%%@AB@%%@CR:C6A00170078 @%%@CR:C6A00170079 @%dimension:%@AE@%%@EH@%%@NL@%
  19135. %@3@%The number of subscripts required to specify a single array element.%@NL@%%@NL@%%@EH@%
  19136. %@NL@%
  19137. %@2@%%@AB@%directive:%@CR:C6A00170080 @%%@AE@%%@EH@%%@NL@%
  19138. %@3@%An instruction to the C preprocessor to perform an action on source-program%@EH@%
  19139. text before compilation.%@NL@%%@NL@%
  19140. %@NL@%
  19141. %@2@%%@AB@%double precision:%@CR:C6A00170081 @%%@AE@%%@EH@%%@NL@%
  19142. %@3@%A real (floating-point) value that occupies eight bytes of memory. Double%@EH@%
  19143. precision values are accurate to 15 or 16 digits.%@NL@%%@NL@%
  19144. %@NL@%
  19145. %@2@%%@AB@%EGA:%@AE@%%@EH@%%@NL@%
  19146. %@3@%Enhanced Graphics Adapter.%@NL@%%@NL@%%@EH@%
  19147. %@NL@%
  19148. %@2@%%@AB@%enumeration type:%@CR:C6A00170082 @%%@CR:C6A00170083 @%%@AE@%%@EH@%%@NL@%
  19149. %@3@%A user-defined data type with values that range over a set of named integral%@EH@%
  19150. constants.%@NL@%%@NL@%
  19151. %@NL@%
  19152. %@2@%%@AB@%escape sequence:%@CR:C6A00170084 @%%@AE@%%@EH@%%@NL@%
  19153. %@3@%A specific combination of a backslash (%@AB@%\%@AE@%) followed by a letter or%@EH@%
  19154. combination of digits, which represents white space and nonprinting
  19155. characters within strings and character constants. %@NL@%%@NL@%
  19156. %@NL@%
  19157. %@2@%%@AB@%expression:%@CR:C6A00170085 @%%@AE@%%@EH@%%@NL@%
  19158. %@3@%A combination of operands and operators that yields a single value.%@NL@%%@NL@%%@EH@%
  19159. %@NL@%
  19160. %@2@%%@AB@%%@CR:C6A00170086 @%%@CR:C6A00170087 @%external variable:%@AE@%%@EH@%%@NL@%
  19161. %@3@%A variable that is defined outside any function in a C source file and is%@EH@%
  19162. used in other source files in the same program.%@NL@%%@NL@%
  19163. %@NL@%
  19164. %@2@%%@AB@%file handle:%@CR:C6A00170088 @%%@AE@%%@EH@%%@NL@%
  19165. %@3@%An integer value that is returned when a library function that performs%@EH@%
  19166. low-level input/output opens or creates a file. The file handle is used to
  19167. refer to that file in later operations.%@NL@%%@NL@%
  19168. %@NL@%
  19169. %@2@%%@AB@%file pointer:%@CR:C6A00170089 @%%@CR:C6A00170090 @%%@CR:C6A00170091 @%%@AE@%%@EH@%%@NL@%
  19170. %@3@%A value that keeps track of the current position in an input or output%@EH@%
  19171. stream. It is updated to reflect the new position each time a read or write
  19172. operation takes place.%@NL@%%@NL@%
  19173. %@NL@%
  19174. %@2@%%@AB@%FILE pointer:%@AE@%%@EH@%%@NL@%
  19175. %@3@%A pointer to a structure of type FILE that contains information about a%@EH@%
  19176. file. It is returned by library functions that create or open files and use
  19177. stream input/output.%@NL@%%@NL@%
  19178. %@NL@%
  19179. %@2@%%@AB@%fill flag:%@CR:C6A00170092 @%%@AE@%%@EH@%%@NL@%
  19180. %@3@%A parameter that determines whether a shape will be drawn as a solid.%@NL@%%@NL@%%@EH@%
  19181. %@NL@%
  19182. %@2@%%@AB@%%@CR:C6A00170093 @%fill mask:%@AE@%%@EH@%%@NL@%
  19183. %@3@%A group of pixels that defines the pattern used to fill a graphics shape.%@NL@%%@NL@%%@EH@%
  19184. %@NL@%
  19185. %@2@%%@AB@%fill pattern:%@CR:C6A00170094 @%%@AE@%%@EH@%%@NL@%
  19186. %@3@%The design defined by the fill mask and used to fill a shape.%@NL@%%@NL@%%@EH@%
  19187. %@NL@%
  19188. %@2@%%@AB@%font:%@CR:C6A00170095 @%%@AE@%%@EH@%%@NL@%
  19189. %@3@%A description of the style and shapes of the characters in a character set. %@NL@%%@NL@%%@EH@%
  19190. %@NL@%
  19191. %@2@%%@AB@%foreground color:%@CR:C6A00170096 @%%@AE@%%@EH@%%@NL@%
  19192. %@3@%The color index for the color in which text is displayed. See "background%@EH@%
  19193. color."%@NL@%%@NL@%
  19194. %@NL@%
  19195. %@2@%%@AB@%format specification:%@CR:C6A00170097 @%%@AE@%%@EH@%%@NL@%
  19196. %@3@%A string that specifies how the %@AB@%printf%@AE@% and %@AB@%scanf%@AE@% families of functions%@EH@%
  19197. interpret input and output data.%@NL@%%@NL@%
  19198. %@NL@%
  19199. %@2@%%@AB@%function:%@CR:C6A00170098 @%%@AE@%%@EH@%%@NL@%
  19200. %@3@%A collection of declarations and statements that has a unique name and can%@EH@%
  19201. return a value. %@NL@%%@NL@%
  19202. %@NL@%
  19203. %@2@%%@AB@%function body:%@AE@%%@EH@%%@NL@%
  19204. %@3@%A statement block containing the local variable declarations and statements%@EH@%
  19205. of a function.%@NL@%%@NL@%
  19206. %@NL@%
  19207. %@2@%%@AB@%function call:%@CR:C6A00170099 @%%@AE@%%@EH@%%@NL@%
  19208. %@3@%An expression that passes control and arguments (if any) to a function.%@NL@%%@NL@%%@EH@%
  19209. %@NL@%
  19210. %@2@%%@AB@%function declaration:%@CR:C6A00170100 @%%@CR:C6A00170101 @%%@AE@%%@EH@%%@NL@%
  19211. %@3@%A declaration that states the name, return type, and storage class of a%@EH@%
  19212. function that is defined explicitly elsewhere in the program.%@NL@%%@NL@%
  19213. %@NL@%
  19214. %@2@%%@AB@%function definition:%@CR:C6A00170102 @%%@AE@%%@EH@%%@NL@%
  19215. %@3@%A definition that specifies a function's name, its formal parameters, the%@EH@%
  19216. declarations and statements that define what it does, and (optionally) its
  19217. return type and storage class.%@NL@%%@NL@%
  19218. %@NL@%
  19219. %@2@%%@AB@%function pointer:%@CR:C6A00170103 @%%@AE@%%@EH@%%@NL@%
  19220. %@3@%A pointer that holds the address of a function.%@NL@%%@NL@%%@EH@%
  19221. %@NL@%
  19222. %@2@%%@AB@%function prototype:%@CR:C6A00170104 @%%@CR:C6A00170105 @%%@AE@%%@EH@%%@NL@%
  19223. %@3@%A function declaration that includes a list of the names and types of formal%@EH@%
  19224. parameters in the parentheses following the function name.%@NL@%%@NL@%
  19225. %@NL@%
  19226. %@2@%%@AB@%global:%@AE@%%@EH@%%@NL@%
  19227. %@3@%See "visibility."%@NL@%%@NL@%%@EH@%
  19228. %@NL@%
  19229. %@2@%%@AB@%graphics mode:%@CR:C6A00170106 @%%@AE@%%@EH@%%@NL@%
  19230. %@3@%See "video mode."%@NL@%%@NL@%%@EH@%
  19231. %@NL@%
  19232. %@2@%%@AB@%header file:%@AE@%%@EH@%%@NL@%
  19233. %@3@%An external source file that contains commonly used declarations and%@EH@%
  19234. definitions. The %@AB@%#include%@AE@% directive is used to insert the contents of a
  19235. header file into a C source file.%@NL@%%@NL@%
  19236. %@NL@%
  19237. %@2@%%@AB@%hexadecimal:%@CR:C6A00170107 @%%@AE@%%@EH@%%@NL@%
  19238. %@3@%The base-16 numbering system whose digits are 0 through F. The letters A%@EH@%
  19239. through F represent the decimal numbers 10 through 15. It is often used in
  19240. computer programming because it is easily converted to and from binary, the
  19241. base-2 numbering system the computer itself uses.%@NL@%%@NL@%
  19242. %@NL@%
  19243. %@2@%%@AB@%HGC:%@AE@%%@EH@%%@NL@%
  19244. %@3@%Hercules monochrome Graphics Card. %@NL@%%@NL@%%@EH@%
  19245. %@NL@%
  19246. %@2@%%@AB@%identifier:%@CR:C6A00170108 @%%@AE@%%@EH@%%@NL@%
  19247. %@3@%A user-defined name in a C program. Identifiers name variables, functions,%@EH@%
  19248. macros, constants, and data types.%@NL@%%@NL@%
  19249. %@NL@%
  19250. %@2@%%@AB@%include file:%@CR:C6A00170109 @%%@AE@%%@EH@%%@NL@%
  19251. %@3@%See "header file."%@NL@%%@NL@%%@EH@%
  19252. %@NL@%
  19253. %@2@%%@AB@%Incolor Card:%@CR:C6A00170110 @%%@CR:C6A00170111 @%%@AE@%%@EH@%%@NL@%
  19254. %@3@%Hercules InColor Card, a 16-color version of the HGC+.%@NL@%%@NL@%%@EH@%
  19255. %@NL@%
  19256. %@2@%%@AB@%indirection:%@CR:C6A00170112 @%%@AE@%%@EH@%%@NL@%
  19257. %@3@%Accessing a data object through a pointer, rather than directly by name.%@NL@%%@NL@%%@EH@%
  19258. %@NL@%
  19259. %@2@%%@AB@%initialize:%@CR:C6A00170113 @%%@AE@%%@EH@%%@NL@%
  19260. %@3@%To assign a value to a variable, often at the time the variable is declared.%@NL@%%@NL@%%@EH@%
  19261. %@NL@%
  19262. %@2@%%@AB@%in-line assembler:%@CR:C6A00170114 @%%@AE@%%@EH@%%@NL@%
  19263. %@3@%The part of QuickC that converts assembly-language instructions into machine%@EH@%
  19264. language.%@NL@%%@NL@%
  19265. %@NL@%
  19266. %@2@%%@AB@%in-line assembly code:%@CR:C6A00170115 @%%@AE@%%@EH@%%@NL@%
  19267. %@3@%Assembly language instructions that appear within a QuickC source program.%@NL@%%@NL@%%@EH@%
  19268. %@NL@%
  19269. %@2@%%@AB@%input/output:%@AE@%%@EH@%%@NL@%
  19270. %@3@%The processes involved in reading (input) and writing (output) data.%@NL@%%@NL@%%@EH@%
  19271. %@NL@%
  19272. %@2@%%@AB@%integer:%@CR:C6A00170116 @%%@AE@%%@EH@%%@NL@%
  19273. %@3@%A whole number represented in the machine as a 16-bit two's-complement%@EH@%
  19274. binary number. A signed integer has a range of -32,768 to 32,767. An
  19275. unsigned integer has a range of 0 to 65,535. See "long integer."%@NL@%%@NL@%
  19276. %@NL@%
  19277. %@2@%%@AB@%I/O:%@CR:C6A00170117 @%%@AE@%%@EH@%%@NL@%
  19278. %@3@%Abbreviation for input/output.%@NL@%%@NL@%%@EH@%
  19279. %@NL@%
  19280. %@2@%%@AB@%keyword:%@CR:C6A00170118 @%%@AE@%%@EH@%%@NL@%
  19281. %@3@%A word with a special, predefined meaning for the C compiler. %@NL@%%@NL@%%@EH@%
  19282. %@NL@%
  19283. %@2@%%@AB@%%@CR:C6A00170119 @%label:%@AE@%%@EH@%%@NL@%
  19284. %@3@%A unique name followed by a colon. Labels are used to denote statements to%@EH@%
  19285. which a %@AB@%goto%@AE@% statement can branch. See "%@AB@%case%@AE@% label."%@NL@%%@NL@%
  19286. %@NL@%
  19287. %@2@%%@AB@%library:%@CR:C6A00170120 @%%@AE@%%@EH@%%@NL@%
  19288. %@3@%A file containing compiled modules. The linker extracts modules from the%@EH@%
  19289. library file and combines them with the user-created object file to form an
  19290. executable program.%@NL@%%@NL@%
  19291. %@NL@%
  19292. %@2@%%@AB@%lifetime:%@CR:C6A00170121 @%%@CR:C6A00170122 @%%@AE@%%@EH@%%@NL@%
  19293. %@3@%The time, during program execution, that a variable or function exists. An%@EH@%
  19294. "automatic" variable has storage and a defined value only in the block where
  19295. it is defined or declared. A "static" variable exists for the duration of
  19296. the program. %@NL@%%@NL@%
  19297. %@NL@%
  19298. %@2@%%@AB@%line style:%@CR:C6A00170123 @%%@AE@%%@EH@%%@NL@%
  19299. %@3@%An unsigned short integer (16 bits) that specifies the pattern with which%@EH@%
  19300. lines will be drawn. Each bit specifies whether a corresponding pixel in the
  19301. line will be displayed. The default line style is a solid line.%@NL@%%@NL@%
  19302. %@NL@%
  19303. %@2@%%@AB@%local:%@AE@%%@EH@%%@NL@%
  19304. %@3@%See "visibility."%@NL@%%@NL@%%@EH@%
  19305. %@NL@%
  19306. %@2@%%@AB@%long integer:%@AE@%%@EH@%%@NL@%
  19307. %@3@%A whole number represented inside the machine as a 32-bit two's-complement%@EH@%
  19308. binary number. A signed long integer has a range of -2,147,483,648 to
  19309. 2,147,483,647. An unsigned long integer has a range of 0 to 4,294,967,295.
  19310. See "integer."%@NL@%%@NL@%
  19311. %@NL@%
  19312. %@2@%%@AB@%low-level input and output routines:%@AE@%%@EH@%%@NL@%
  19313. %@3@%Run-time library routines that perform unbuffered, unformatted I/O%@EH@%
  19314. operations, for example, %@AB@%creat%@AE@%, %@AB@%read%@AE@%, %@AB@%write%@AE@%, and %@AB@%lseek%@AE@%.%@NL@%%@NL@%
  19315. %@NL@%
  19316. %@2@%%@AB@%lvalue:%@CR:C6A00170124 @%%@AE@%%@EH@%%@NL@%
  19317. %@3@%An expression (such as a variable name) that refers to a memory location and%@EH@%
  19318. is required as the left-hand operand of an assignment operation, or as the
  19319. single operand of a unary operator.%@NL@%%@NL@%
  19320. %@NL@%
  19321. %@2@%%@AB@%machine language:%@CR:C6A00170125 @%%@AE@%%@EH@%%@NL@%
  19322. %@3@%A series of binary numbers that a computer executes as program instructions.%@EH@%
  19323. %@NL@%%@NL@%
  19324. %@NL@%
  19325. %@2@%%@AB@%macro:%@CR:C6A00170126 @%%@AE@%%@EH@%%@NL@%
  19326. %@3@%An identifier defined in a %@AB@%#define%@AE@% preprocessor directive to represent%@EH@%
  19327. another series of characters. %@NL@%%@NL@%
  19328. %@NL@%
  19329. %@2@%%@AB@%main function:%@CR:C6A00170127 @%%@CR:C6A00170128 @%%@AE@%%@EH@%%@NL@%
  19330. %@3@%The function with which program execution begins (the program's entry%@EH@%
  19331. point).%@NL@%%@NL@%
  19332. %@NL@%
  19333. %@2@%%@AB@%manifest constant:%@AE@%%@EH@%%@NL@%
  19334. %@3@%See "symbolic constant." %@NL@%%@NL@%%@EH@%
  19335. %@NL@%
  19336. %@2@%%@AB@%MCGA (%@AB@%Multicolor Graphics Array%@AE@%):%@CR:C6A00170129 @%%@AE@%%@EH@%%@NL@%
  19337. %@3@%The video subsystem integrated into the PS/2 Model 30. Also, Memory%@EH@%
  19338. Controller Gate Array, one of the components of the Model 30's video
  19339. subsystem. %@NL@%%@NL@%
  19340. %@NL@%
  19341. %@2@%%@AB@%member:%@CR:C6A00170130 @%%@AE@%%@EH@%%@NL@%
  19342. %@3@%One of the elements of a structure or union.%@NL@%%@NL@%%@EH@%
  19343. %@NL@%
  19344. %@2@%%@AB@%member-of operator:%@AE@%%@EH@%%@NL@%
  19345. %@3@%The dot operator (%@AB@%.%@AE@%), which is used with the name of a structure and one or%@EH@%
  19346. more fields to identify a structure member.%@NL@%%@NL@%
  19347. %@NL@%
  19348. %@2@%%@AB@%mode:%@CR:C6A00170131 @%%@AE@%%@EH@%%@NL@%
  19349. %@3@%See "video mode."%@NL@%%@NL@%%@EH@%
  19350. %@NL@%
  19351. %@2@%%@AB@%monochrome display:%@AE@%%@EH@%%@NL@%
  19352. %@3@%A computer monitor capable of showing only two colors─black and a second%@EH@%
  19353. color such as white, green, or amber. Some monochrome monitors can also show
  19354. the second color with higher intensity or with underlined text.%@NL@%%@NL@%
  19355. %@NL@%
  19356. %@2@%%@AB@%Monochrome Display Adapter (MDA):%@CR:C6A00170132 @%%@AE@%%@EH@%%@NL@%
  19357. %@3@%A printed-circuit card that controls the display and can show text only at%@EH@%
  19358. medium resolution in one color.%@NL@%%@NL@%
  19359. %@NL@%
  19360. %@2@%%@AB@%newline character:%@CR:C6A00170133 @%%@CR:C6A00170134 @%%@AE@%%@EH@%%@NL@%
  19361. %@3@%The character used to mark the end of a line in a text file, or the escape%@EH@%
  19362. sequence (%@AB@%\n%@AE@%) used to represent this character. %@NL@%%@NL@%
  19363. %@NL@%
  19364. %@2@%%@AB@%%@CR:C6A00170135 @%%@CR:C6A00170136 @%null character:%@AE@%%@EH@%%@NL@%
  19365. %@3@%The ASCII character encoded as the value 0, represented as the escape%@EH@%
  19366. sequence (%@AB@%\0%@AE@%) in a source file. A null character marks the end of a string.%@NL@%%@NL@%
  19367. %@NL@%
  19368. %@2@%%@AB@%%@CR:C6A00170137 @%%@CR:C6A00170138 @%null pointer:%@AE@%%@EH@%%@NL@%
  19369. %@3@%A pointer to nothing, expressed as the value 0.%@NL@%%@NL@%%@EH@%
  19370. %@NL@%
  19371. %@2@%%@AB@%one's complement:%@AE@%%@EH@%%@NL@%
  19372. %@3@%The arithmetic operation in which all 1 bits are converted to 0 bits and%@EH@%
  19373. vice versa. The tilde character (%@AB@%~%@AE@%) is the one's-complement operator.%@NL@%%@NL@%
  19374. %@NL@%
  19375. %@2@%%@AB@%%@CR:C6A00170139 @%operand:%@AE@%%@EH@%%@NL@%
  19376. %@3@%A constant or variable value that is manipulated in an expression.%@NL@%%@NL@%%@EH@%
  19377. %@NL@%
  19378. %@2@%%@AB@%operator:%@CR:C6A00170140 @%%@AE@%%@EH@%%@NL@%
  19379. %@3@%One or more symbols that specify how the operand or operands of an%@EH@%
  19380. expression are manipulated. See "unary operator," "binary operator," and
  19381. "ternary operator."%@NL@%%@NL@%
  19382. %@NL@%
  19383. %@2@%%@AB@%origin:%@CR:C6A00170141 @%%@AE@%%@EH@%%@NL@%
  19384. %@3@%The point on the screen at which the %@AI@%x%@AE@% and %@AI@%y%@AE@% coordinates are both equal to%@EH@%
  19385. 0. On the physical screen, the origin is at the upper left corner%@AB@%.%@AE@%%@NL@%%@NL@%
  19386. %@NL@%
  19387. %@2@%%@AB@%palette:%@CR:C6A00170142 @%%@AE@%%@EH@%%@NL@%
  19388. %@3@%The displayable colors for a given video mode. The CGA modes operate with a%@EH@%
  19389. set of predetermined palette colors. The EGA, VGA, and MCGA color modes
  19390. operate with a redefinable palette of colors. %@NL@%%@NL@%
  19391. %@NL@%
  19392. %@2@%%@AB@%parameter:%@CR:C6A00170143 @%%@AE@%%@EH@%%@NL@%
  19393. %@3@%An identifier that receives a value passed to a function.%@NL@%%@NL@%%@EH@%
  19394. %@NL@%
  19395. %@2@%%@AB@%path:%@CR:C6A00170144 @%%@AE@%%@EH@%%@NL@%
  19396. %@3@%The name that defines the location of a file or directory. A path may%@EH@%
  19397. include a drive name and one or more directory names.%@NL@%%@NL@%
  19398. %@NL@%
  19399. %@2@%%@AB@%PGA (Professional Graphics Adapter):%@CR:C6A00170145 @%%@AE@%%@EH@%%@NL@%
  19400. %@3@%Another name for IBM's PGC.%@NL@%%@NL@%%@EH@%
  19401. %@NL@%
  19402. %@2@%%@AB@%physical coordinates:%@CR:C6A00170146 @%%@AE@%%@EH@%%@NL@%
  19403. %@3@%The coordinate system defined by the hardware. The physical coordinate%@EH@%
  19404. system has the origin (0, 0) at the upper left corner of the screen. The
  19405. value of %@AI@%x%@AE@% increases from left to right, and the value of %@AI@%y%@AE@% increases from
  19406. top to bottom. See "viewport coordinates."%@NL@%%@NL@%
  19407. %@NL@%
  19408. %@2@%%@AB@%pixel:%@CR:C6A00170147 @%%@AE@%%@EH@%%@NL@%
  19409. %@3@%A single dot on the screen. It is the smallest item that may be manipulated%@EH@%
  19410. with the graphics library, and it is the basic unit of the
  19411. viewport-coordinate system. %@NL@%%@NL@%
  19412. %@NL@%
  19413. %@2@%%@AB@%pointer:%@CR:C6A00170148 @%%@AE@%%@EH@%%@NL@%
  19414. %@3@%A variable containing the address of another variable, function, or%@EH@%
  19415. constant. %@NL@%%@NL@%
  19416. %@NL@%
  19417. %@2@%%@AB@%pointer arithmetic:%@CR:C6A00170149 @%%@CR:C6A00170150 @%%@AE@%%@EH@%%@NL@%
  19418. %@3@%The use of addition or subtraction to change a pointer's value. Pointer%@EH@%
  19419. arithmetic is typically used with array pointers, though it is not illegal
  19420. on other kinds of pointers.%@NL@%%@NL@%
  19421. %@NL@%
  19422. %@2@%%@AB@%pointer-member operator:%@CR:C6A00170151 @%%@CR:C6A00170152 @%%@CR:C6A00170153 @%%@AE@%%@EH@%%@NL@%
  19423. %@3@%The %@AB@%->%@AE@% operator, used with structure pointers to name a structure member.%@NL@%%@NL@%%@EH@%
  19424. %@NL@%
  19425. %@2@%%@AB@%pragma:%@CR:C6A00170154 @%%@AE@%%@EH@%%@NL@%
  19426. %@3@%An instruction to the compiler to perform an action at compile time.%@NL@%%@NL@%%@EH@%
  19427. %@NL@%
  19428. %@2@%%@AB@%precedence:%@CR:C6A00170155 @%%@AE@%%@EH@%%@NL@%
  19429. %@3@%The relative position of an operator in the hierarchy that determines the%@EH@%
  19430. order in which expressions are evaluated.%@NL@%%@NL@%
  19431. %@NL@%
  19432. %@2@%%@AB@%preprocessor:%@CR:C6A00170156 @%%@AE@%%@EH@%%@NL@%
  19433. %@3@%A text processor that manipulates the contents of a C source file during the%@EH@%
  19434. first phase of compilation.%@NL@%%@NL@%
  19435. %@NL@%
  19436. %@2@%%@AB@%preprocessor directive:%@AE@%%@EH@%%@NL@%
  19437. %@3@%See "directive."%@NL@%%@NL@%%@EH@%
  19438. %@NL@%
  19439. %@2@%%@AB@%prototype:%@AE@%%@EH@%%@NL@%
  19440. %@3@%See "function prototype."%@NL@%%@NL@%%@EH@%
  19441. %@NL@%
  19442. %@2@%%@AB@%recursion:%@CR:C6A00170157 @%%@AE@%%@EH@%%@NL@%
  19443. %@3@%The process by which a function calls itself.%@NL@%%@NL@%%@EH@%
  19444. %@NL@%
  19445. %@2@%%@AB@%%@CR:C6A00170158 @%register variable:%@AE@%%@EH@%%@NL@%
  19446. %@3@%An integer variable that is placed in a machine register, which may cause%@EH@%
  19447. the program to be smaller and faster.%@NL@%%@NL@%
  19448. %@NL@%
  19449. %@2@%%@AB@%remapping:%@AE@%%@EH@%%@NL@%
  19450. %@3@%%@CR:C6A00170159 @%The process of assigning new color values to color indexes. Remapping a%@EH@%
  19451. color index changes the screen color of any pixels that have been drawn with
  19452. that color index.%@NL@%%@NL@%
  19453. %@NL@%
  19454. %@2@%%@AB@%reserved word:%@AE@%%@EH@%%@NL@%
  19455. %@3@%See "keyword."%@NL@%%@NL@%%@EH@%
  19456. %@NL@%
  19457. %@2@%%@AB@%return value:%@AE@%%@EH@%%@NL@%
  19458. %@3@%%@CR:C6A00170160 @%The value that a function returns to the calling function.%@NL@%%@NL@%%@EH@%
  19459. %@NL@%
  19460. %@2@%%@AB@%run time:%@AE@%%@EH@%%@NL@%
  19461. %@3@%%@CR:C6A00170161 @%The time during which a previously compiled and linked program is executing.%@NL@%%@NL@%%@EH@%
  19462. %@NL@%
  19463. %@2@%%@AB@%run-time library:%@AE@%%@EH@%%@NL@%
  19464. %@3@%A file containing the routines needed to implement certain functions of the%@EH@%
  19465. Microsoft QuickC language.%@NL@%%@NL@%
  19466. %@NL@%
  19467. %@2@%%@AB@%scaling:%@AE@%%@EH@%%@NL@%
  19468. %@3@%The mapping of real-window coordinates to viewport coordinates.%@NL@%%@NL@%%@EH@%
  19469. %@NL@%
  19470. %@2@%%@AB@%%@CR:C6A00170162 @%scope:%@AE@%%@EH@%%@NL@%
  19471. %@3@%The parts of a program in which an item can be referenced by name. The scope%@EH@%
  19472. of an item may be limited to the file, function, block, or function
  19473. prototype in which it appears.%@NL@%%@NL@%
  19474. %@NL@%
  19475. %@2@%%@AB@%screen mode:%@AE@%%@EH@%%@NL@%
  19476. %@3@%See "video mode."%@NL@%%@NL@%%@EH@%
  19477. %@NL@%
  19478. %@2@%%@AB@%single precision:%@AE@%%@EH@%%@NL@%
  19479. %@3@%%@CR:C6A00170163 @%A real (floating-point) value that occupies four bytes of memory. Single-%@EH@%
  19480. precision values are accurate to seven decimal places. %@NL@%%@NL@%
  19481. %@NL@%
  19482. %@2@%%@AB@%sizeof operator:%@AE@%%@EH@%%@NL@%
  19483. %@3@%%@CR:C6A00170164 @%%@CR:C6A00170165 @%%@CR:C6A00170166 @%A C operator that returns the amount of storage, in bytes, associated with%@EH@%
  19484. an identifier or a type.%@NL@%%@NL@%
  19485. %@NL@%
  19486. %@2@%%@AB@%source file:%@AE@%%@EH@%%@NL@%
  19487. %@3@%A text file containing C language code.%@NL@%%@NL@%%@EH@%
  19488. %@NL@%
  19489. %@2@%%@AB@%standard error:%@AE@%%@EH@%%@NL@%
  19490. %@3@%The device to which a program sends its error messages unless the error%@EH@%
  19491. output is redirected. In normal DOS operation, standard error is the
  19492. display. The predefined stream %@AB@%stderr%@AE@% is associated with standard error in
  19493. the C language. %@NL@%%@NL@%
  19494. %@NL@%
  19495. %@2@%%@AB@%%@CR:C6A00170167 @%standard input:%@AE@%%@EH@%%@NL@%
  19496. %@3@%The device from which a program reads its input unless the input is%@EH@%
  19497. redirected. In normal DOS operation, standard input is the keyboard. The
  19498. predefined stream %@AB@%stdin%@AE@% is associated with standard input in the C language.%@NL@%%@NL@%
  19499. %@NL@%
  19500. %@2@%%@AB@%standard output:%@AE@%%@EH@%%@NL@%
  19501. %@3@%%@CR:C6A00170168 @%The device to which a program sends its output unless the output is%@EH@%
  19502. redirected. In normal DOS operation, standard output is the display. The
  19503. predefined stream %@AB@%stdout%@AE@% is associated with standard output in the C
  19504. language.%@NL@%%@NL@%
  19505. %@NL@%
  19506. %@2@%%@AB@%%@CR:C6A00170169 @%static variable:%@AE@%%@EH@%%@NL@%
  19507. %@3@%A variable that keeps its value even after the program exits the block in%@EH@%
  19508. which the variable is declared.%@NL@%%@NL@%
  19509. %@NL@%
  19510. %@2@%%@AB@%stream:%@AE@%%@EH@%%@NL@%
  19511. %@3@%%@CR:C6A00170170 @%A sequence of bytes flowing into (input) or out of (output) a program.%@NL@%%@NL@%%@EH@%
  19512. %@NL@%
  19513. %@2@%%@AB@%stream functions:%@AE@%%@EH@%%@NL@%
  19514. %@3@%Run-time library functions that treat data files and data items as "streams"%@EH@%
  19515. of individual characters.%@NL@%%@NL@%
  19516. %@NL@%
  19517. %@2@%%@AB@%string:%@AE@%%@EH@%%@NL@%
  19518. %@3@%%@CR:C6A00170171 @%An array of characters, terminated by a null character (%@AB@%\0%@AE@%).%@NL@%%@NL@%%@EH@%
  19519. %@NL@%
  19520. %@2@%%@AB@%string constant:%@AE@%%@EH@%%@NL@%
  19521. %@3@%A string of characters and escape sequences enclosed in double quotes ("").%@EH@%
  19522. Every string constant is an array of elements of type %@AB@%char%@AE@%. See "character
  19523. constant."%@NL@%%@NL@%
  19524. %@NL@%
  19525. %@2@%%@AB@%structure:%@AE@%%@EH@%%@NL@%
  19526. %@3@%%@CR:C6A00170172 @%A set of elements, which may be of different types, grouped under a single%@EH@%
  19527. name.%@NL@%%@NL@%
  19528. %@NL@%
  19529. %@2@%%@AB@%structure member:%@AE@%%@EH@%%@NL@%
  19530. %@3@%One of the elements of a structure.%@NL@%%@NL@%%@EH@%
  19531. %@NL@%
  19532. %@2@%%@AB@%structure pointer:%@AE@%%@EH@%%@NL@%
  19533. %@3@%%@CR:C6A00170173 @%%@CR:C6A00170174 @%A pointer to a structure. Structure pointers identify structure members by%@EH@%
  19534. specifying the name of the structure, the pointer-member operator (%@AB@%->%@AE@%), and
  19535. the member name.%@NL@%%@NL@%
  19536. %@NL@%
  19537. %@2@%%@AB@%symbolic constant:%@AE@%%@EH@%%@NL@%
  19538. %@3@%%@CR:C6A00170175 @%An identifier defined in a %@AB@%#define%@AE@% preprocessor directive to represent a%@EH@%
  19539. constant value.%@NL@%%@NL@%
  19540. %@NL@%
  19541. %@2@%%@AB@%tag:%@AE@%%@EH@%%@NL@%
  19542. %@3@%%@CR:C6A00170176 @%The name assigned to a structure, union, or enumeration type.%@NL@%%@NL@%%@EH@%
  19543. %@NL@%
  19544. %@2@%%@AB@%ternary operator:%@AE@%%@EH@%%@NL@%
  19545. %@3@%An operator used in ternary (three-part) expressions. C has one ternary%@EH@%
  19546. operator, the conditional operator (%@AB@%? :%@AE@%).%@NL@%%@NL@%
  19547. %@NL@%
  19548. %@2@%%@AB@%text:%@AE@%%@EH@%%@NL@%
  19549. %@3@%Ordinary, readable characters, including the uppercase and lowercase letters%@EH@%
  19550. of the alphabet, the numerals 0-9, and punctuation marks.%@NL@%%@NL@%
  19551. %@NL@%
  19552. %@2@%%@AB@%text file:%@AE@%%@EH@%%@NL@%
  19553. %@3@%A file of ASCII characters that you can read with the TYPE command or a word%@EH@%
  19554. processor.%@NL@%%@NL@%
  19555. %@NL@%
  19556. %@2@%%@AB@%text format:%@AE@%%@EH@%%@NL@%
  19557. %@3@%%@CR:C6A00170177 @%A method of disk storage in which all data are converted to ASCII format.%@NL@%%@NL@%%@EH@%
  19558. %@NL@%
  19559. %@2@%%@AB@%text mode:%@AE@%%@EH@%%@NL@%
  19560. %@3@%%@CR:C6A00170178 @%%@CR:C6A00170179 @%See "video mode."%@NL@%%@NL@%%@EH@%
  19561. %@NL@%
  19562. %@2@%%@AB@%text window:%@AE@%%@EH@%%@NL@%
  19563. %@3@%%@CR:C6A00170180 @%%@CR:C6A00170181 @%A window defined in row and column coordinates where text output to the%@EH@%
  19564. screen will be displayed. Text printed beyond the edge of the text window is
  19565. not visible. The default text window is the whole screen.%@NL@%%@NL@%
  19566. %@NL@%
  19567. %@2@%%@AB@%two's complement:%@AE@%%@EH@%%@NL@%
  19568. %@3@%A kind of base-2 notation used to represent positive and negative numbers in%@EH@%
  19569. which negative values are formed by complementing all bits and adding 1 to
  19570. the results.%@NL@%%@NL@%
  19571. %@NL@%
  19572. %@2@%%@AB@%type:%@AE@%%@EH@%%@NL@%
  19573. %@3@%%@CR:C6A00170182 @%%@CR:C6A00170183 @%%@CR:C6A00170184 @%%@CR:C6A00170185 @%A description of a set of values. For example, the type %@AB@%char%@AE@% comprises the%@EH@%
  19574. 256 values in the ASCII character set.%@NL@%%@NL@%
  19575. %@NL@%
  19576. %@2@%%@AB@%type cast:%@AE@%%@EH@%%@NL@%
  19577. %@3@%%@CR:C6A00170186 @%An operation in which a value of one type is converted to a value of a%@EH@%
  19578. different type.%@NL@%%@NL@%
  19579. %@NL@%
  19580. %@2@%%@AB@%type checking:%@AE@%%@EH@%%@NL@%
  19581. %@3@%%@CR:C6A00170187 @%An operation in which the compiler verifies that the operands of an operator%@EH@%
  19582. are valid, or that the actual arguments in a function call are of the same
  19583. types as the corresponding formal parameters in the function definition and
  19584. function prototype.%@NL@%%@NL@%
  19585. %@NL@%
  19586. %@2@%%@AB@%type declaration:%@AE@%%@EH@%%@NL@%
  19587. %@3@%%@CR:C6A00170188 @%A declaration that defines the name and members of a structure or union%@EH@%
  19588. type, or the name and enumeration set of an enumeration type.%@NL@%%@NL@%
  19589. %@NL@%
  19590. %@2@%%@AB@%typedef declaration:%@AE@%%@EH@%%@NL@%
  19591. %@3@%A declaration that defines a shorter or more meaningful name for an existing%@EH@%
  19592. C data type or for a user-defined data type. Names defined in a %@AB@%typedef%@AE@%
  19593. declaration are often referred to as "typedefs." %@NL@%%@NL@%
  19594. %@NL@%
  19595. %@2@%%@AB@%typeface:%@AE@%%@EH@%%@NL@%
  19596. %@3@%%@CR:C6A00170189 @%%@CR:C6A00170190 @%The style of displayed text.%@NL@%%@NL@%%@EH@%
  19597. %@NL@%
  19598. %@2@%%@AB@%type name:%@AE@%%@EH@%%@NL@%
  19599. %@3@%The name of a data type. See "type."%@NL@%%@NL@%%@EH@%
  19600. %@NL@%
  19601. %@2@%%@AB@%type qualifier:%@AE@%%@EH@%%@NL@%
  19602. %@3@%The keywords %@AB@%short%@AE@%, %@AB@%long%@AE@%, %@AB@%signed%@AE@%, and %@AB@%unsigned%@AE@%, which modify a basic data%@EH@%
  19603. type.%@NL@%%@NL@%
  19604. %@NL@%
  19605. %@2@%%@AB@%type size:%@AE@%%@EH@%%@NL@%
  19606. %@3@%%@CR:C6A00170191 @%%@CR:C6A00170192 @%%@CR:C6A00170193 @%A measure of the screen area occupied by individual characters in a font,%@EH@%
  19607. typically specified in pixels.%@NL@%%@NL@%
  19608. %@NL@%
  19609. %@2@%%@AB@%unary expression:%@AE@%%@EH@%%@NL@%
  19610. %@3@%An expression consisting of a single operand preceded or followed by a unary%@EH@%
  19611. operator.%@NL@%%@NL@%
  19612. %@NL@%
  19613. %@2@%%@AB@%unary operator:%@AE@%%@EH@%%@NL@%
  19614. %@3@%An operator that takes a single operand. Unary operators in the C language%@EH@%
  19615. are the complement operators (%@AB@%- ~ !%@AE@%), indirection operator %@AB@%(*%@AE@%), increment
  19616. (%@AB@%++%@AE@%) and decrement (%@AB@%- -%@AE@%) operators, address-of operator (%@AB@%&%@AE@%), and %@AB@%sizeof%@AE@%
  19617. operator. The unary plus (%@AB@%+%@AE@%) operator is legal but has no effect.%@NL@%%@NL@%
  19618. %@NL@%
  19619. %@2@%%@AB@%union:%@AE@%%@EH@%%@NL@%
  19620. %@3@%%@CR:C6A00170194 @%%@CR:C6A00170195 @%%@CR:C6A00170196 @%%@CR:C6A00170197 @%A set of values of different types that occupy the same storage space.%@NL@%%@NL@%%@EH@%
  19621. %@NL@%
  19622. %@2@%%@AB@%vector-mapped font:%@AE@%%@EH@%%@NL@%
  19623. %@3@%%@CR:C6A00170198 @%A font in which each character is defined in terms of lines and arcs. %@NL@%%@NL@%%@EH@%
  19624. %@NL@%
  19625. %@2@%%@AB@%VGA (Video Graphics Array):%@AE@%%@EH@%%@NL@%
  19626. %@3@%%@CR:C6A00170199 @% Many users refer to the video subsystem integrated into the PS/2 Models 50,%@EH@%
  19627. 60, and 80, as well as the IBM PS/2 Display Adapter, as the "VGA."%@NL@%%@NL@%
  19628. %@NL@%
  19629. %@2@%%@AB@%video adapter:%@AE@%%@EH@%%@NL@%
  19630. %@3@%%@CR:C6A00170200 @%A printed-circuit card that generates video output. Well-known IBM PC video%@EH@%
  19631. adapters include the MDA, CGA, HGC, EGA, MCGA, and VGA Adapters.%@NL@%%@NL@%
  19632. %@NL@%
  19633. %@2@%%@AB@%video mode:%@AE@%%@EH@%%@NL@%
  19634. %@3@%%@CR:C6A00170201 @%An integer that specifies the resolution and other characteristics of video%@EH@%
  19635. output. QuickC supports 17 different video modes, although some of them are
  19636. available only with certain video adapters.%@NL@%%@NL@%
  19637. %@NL@%
  19638. %@2@%%@AB@%viewport:%@AE@%%@EH@%%@NL@%
  19639. %@3@%%@CR:C6A00170202 @%A clipping region in which the origin (0, 0) may be redefined. The initial%@EH@%
  19640. origin of a viewport is the upper left corner.%@NL@%%@NL@%
  19641. %@NL@%
  19642. %@2@%%@AB@%viewport coordinates:%@AE@%%@EH@%%@NL@%
  19643. %@3@%The integer coordinate system defined by the programmer for a specific%@EH@%
  19644. viewport. By default, the viewport-coordinate system has the origin (0, 0)
  19645. at the upper left corner of the viewport, but this may be changed by a call
  19646. to %@AB@%_setvieworg%@AE@%.%@NL@%%@NL@%
  19647. %@NL@%
  19648. %@2@%%@AB@%visibility:%@AE@%%@EH@%%@NL@%
  19649. %@3@%%@CR:C6A00170203 @%The parts of the program in which a particular variable or function can be%@EH@%
  19650. referenced by name. An item has global visibility if it is visible in all
  19651. source files constituting the program and local visibility if its use is
  19652. restricted.%@NL@%%@NL@%
  19653. %@NL@%
  19654. %@2@%%@AB@%white-space character:%@AE@%%@EH@%%@NL@%
  19655. %@3@%%@CR:C6A00170204 @%A space, tab, line-feed, carriage-return, form-feed, vertical-tab, or%@EH@%
  19656. newline character.%@NL@%%@NL@%
  19657. %@NL@%
  19658. %@2@%%@AB@%window:%@AE@%%@EH@%%@NL@%
  19659. %@3@%%@CR:C6A00170205 @%%@CR:C6A00170206 @%%@CR:C6A00170207 @%An imaginary rectangle on the screen where output takes place. See "text%@EH@%
  19660. window" and "window coordinates."%@NL@%%@NL@%
  19661. %@NL@%
  19662. %@2@%%@AB@%window coordinates:%@AE@%%@EH@%%@NL@%
  19663. %@3@%%@CR:C6A00170208 @%The coordinate system defined by the programmer.%@NL@%%@NL@%%@EH@%
  19664. %@NL@%
  19665. %@NL@%
  19666. %@NL@%
  19667. %@NL@%
  19668.