home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / watc11up.zip / c_hlp_os2.zip / binp / help / clr.inf (.txt) < prev    next >
OS/2 Help File  |  2001-08-28  |  374KB  |  11,241 lines

  1.  
  2. ΓòÉΓòÉΓòÉ 1. Introduction to C ΓòÉΓòÉΓòÉ
  3.  
  4.  
  5. ΓòÉΓòÉΓòÉ 1.1. History ΓòÉΓòÉΓòÉ
  6.  
  7.  
  8. The C programming language was developed by Dennis Ritchie in 1972 for the UNIX 
  9. operating system.  Over the years, the language has appeared on many other 
  10. systems, satisfying a need of programmers who want to be able to develop 
  11. applications that can run in many different environments. 
  12.  
  13. Because the C language was never formally defined, each implementation 
  14. interpreted the behavior of the language in slightly different ways, and also 
  15. introduced their own extensions.  As a result, the goal of true software 
  16. portability was not achieved. 
  17.  
  18. In 1982, the American National Standards Committee formed the X3J11 Technical 
  19. Committee on the C Programming Language, whose purpose was to formally define 
  20. the C language and its library functions, and to describe its interaction with 
  21. the execution environment.  The C Programming Language standard was completed 
  22. in 1989. 
  23.  
  24. The Watcom C/16 and C/32 compiler has evolved from 8086 code generation 
  25. technology developed and refined at Watcom and the University of Waterloo since 
  26. 1980.  The first Watcom C/16 compiler was released in 1988.  The first Watcom 
  27. C/32 compiler was released in 1989. 
  28.  
  29.  
  30. ΓòÉΓòÉΓòÉ 1.2. Uses ΓòÉΓòÉΓòÉ
  31.  
  32.  
  33. C is sometimes called a "low-level" language, referring to the fact that C 
  34. programmers tend to think in terms of bits, bytes, addresses and other concepts 
  35. fundamental to assembly-language programming. 
  36.  
  37. But C is also a "broad spectrum" language.  In addition to accessing the basic 
  38. components of the computer, it also provides features common to many 
  39. "high-level" languages.  Structured program control, data structures and 
  40. modular program design are recent additions to some high-level languages, but 
  41. have been part of the C language since its inception. 
  42.  
  43. C gives the programmer the ability to write applications at a level just above 
  44. the assembly language level, without having to know the assembly language of 
  45. the machine.  Language compilers provided this ability in the past, but the 
  46. application was often quite "fat", because the code produced by the compiler 
  47. was never as good as could be written by a good assembly language programmer. 
  48. But with modern code generation techniques it is often difficult, if not 
  49. impossible, to distinguish an assembly language program written by a human from 
  50. the same program generated by a C compiler (based on code size).  In fact, some 
  51. compilers now generate better code than all but the best assembly language 
  52. programmers. 
  53.  
  54. So, what can C be used for?  It can be used to write virtually anything, the 
  55. same way that assembly language can be used.  But other programming languages 
  56. continue to be used for specific programming applications at which they excel. 
  57.  
  58. C tends to be used for "systems programming", a term that refers to the writing 
  59. of operating systems, programming languages and other software tools that don't 
  60. fall into the class of "applications programming".  A classic example is the 
  61. UNIX operating system, developed by Bell Laboratories.  It is written almost 
  62. entirely in C and is one of the most portable operating systems available. 
  63.  
  64. C is also used for writing large programs that require more efficiency than the 
  65. average application.  Typical examples are interpreters and compilers for 
  66. programming languages. 
  67.  
  68. Another area where C is commonly used is large-scale application programs, such 
  69. as databases, spreadsheets, word processors and so on.  These require a high 
  70. degree of efficiency and compactness, since they are often basic to an 
  71. individual's or company's computing needs, and therefore consume a lot of 
  72. computer resources. 
  73.  
  74. It seems that C is used extensively for commercially available products, but C 
  75. can also be used for any application that just requires more efficiency.  For 
  76. example, a large transaction processing system may be written in COBOL, but to 
  77. squeeze the last bit of speed out of the system, it may be desirable to rewrite 
  78. it in C.  That application could certainly be written in assembly language, but 
  79. many programmers now prefer to avoid programming at such a low level, when a C 
  80. compiler can generate code that is just as efficient. 
  81.  
  82. Finally, of course, a major reason for writing a program in C is that it will 
  83. run with little or no modification on any system with a C compiler.  In the 
  84. past, with the proliferation of C compilers and no standard to guide their 
  85. design, it was much more difficult.  Today, with the appearance of the ANSI 
  86. standard for the C programming language, a program written entirely in a 
  87. conforming C implementation should be transportable to a new compiler with 
  88. relatively little work.  Of course, issues like file names, memory layout and 
  89. command line parameter syntax will vary from one system to another, but a 
  90. properly designed C application will isolate these parts of the code in 
  91. "system-dependent" files, which can be changed for each system.  (Refer to 
  92. "Writing Portable Programs".) 
  93.  
  94.  
  95. ΓòÉΓòÉΓòÉ 1.3. Advantages ΓòÉΓòÉΓòÉ
  96.  
  97.  
  98. C has a number of major advantages over other programming languages. 
  99.  
  100.      Most systems provide a C compiler. 
  101.  
  102.  Vendors of computer systems realize that the success of a system is dependent 
  103.  upon the availability of software for that system.  With the large body of 
  104.  C-based programs in existence, most vendors provide a C compiler in order to 
  105.  encourage the transporting of some of these programs to their system.  For 
  106.  systems that don't provide a C compiler, independent companies may develop a 
  107.  compiler. 
  108.  
  109.  With the development of the ANSI C standard, the trend towards universal 
  110.  availability of C compilers will probably accelerate. 
  111.  
  112.      C programs can be transported easily to other computers and operating 
  113.       systems. 
  114.  
  115.  Many programming languages claim transportability.  FORTRAN, COBOL and Pascal 
  116.  programs all have standards describing them, so a program written entirely 
  117.  within the standard definition of the language will likely be portable.  The 
  118.  same is true of C.  However, few languages can match portability with the 
  119.  other advantages of C, including efficiency of generated code and the ability 
  120.  to work close to the machine level. 
  121.  
  122.      Programs written in C are very efficient in both execution speed and code 
  123.       size. 
  124.  
  125.  Few languages can match C in efficiency.  A good assembly language programmer 
  126.  may be able to produce code better than a C compiler, but he/she will have to 
  127.  spend much more time in the development of the application, because assembly 
  128.  language programming lends itself more easily to errors.  Compilers for other 
  129.  languages may produce efficient code for applications within their scope, but 
  130.  few produce efficient code for all applications. 
  131.  
  132.      C programs can get close to the hardware, controlling devices directly if 
  133.       necessary. 
  134.  
  135.  Most programs do not need this ability, but if necessary, the program can 
  136.  access particular features of the computer.  For example, a fixed memory 
  137.  location may exist that contains a certain value of use to the program.  It is 
  138.  easy to access it from C, but not from many other languages.  (Of course, if 
  139.  the program is designed to be portable, this section of code will be isolated 
  140.  and clearly marked as depending on the operating system.) 
  141.  
  142.      C programs are easy to maintain. 
  143.  
  144.  Assembly language code is difficult to maintain owing to the very low level of 
  145.  programming (registers, addressing modes, branching).  C programs provide 
  146.  comparable functionality, but at a higher level.  The programmer still thinks 
  147.  in terms of machine capabilities, but without having to know the exact 
  148.  operation of the hardware, leaving the programmer free to concentrate on 
  149.  program design rather than the intimate details of coding on that particular 
  150.  machine. 
  151.  
  152.      C programs are easy to understand. 
  153.  
  154.  "Easy" is, of course, a relative term.  C programs are definitely easier to 
  155.  understand than the equivalent assembly language program.  Another programming 
  156.  language may be easier to understand for a particular kind of application, but 
  157.  in general C is a good choice. 
  158.  
  159.      All of the above advantages apply regardless of the application or the 
  160.       hardware or operating system on which it is running. 
  161.  
  162.  This is the biggest advantage.  Because C programs are portable, and C is not 
  163.  suited only to a certain class of applications, it is often the best choice 
  164.  for developing an application. 
  165.  
  166.  
  167. ΓòÉΓòÉΓòÉ 1.4. How to Use This Book ΓòÉΓòÉΓòÉ
  168.  
  169.  
  170. This book is a description of the C programming language as implemented by the 
  171. Watcom C/16 and C/32 compilers for the 80x86 family of processors.  It is 
  172. intended to be an easy-to-read description of the C language.  The ANSI C 
  173. standard is the last word on details about the language, but it describes the 
  174. language in terms that must be interpreted for each implementation of a C 
  175. compiler. 
  176.  
  177. This book attempts to describe the C language in terms of general behavior, and 
  178. the specific behavior of the C compiler when the standard describes the 
  179. behavior as  implementation-defined. 
  180.  
  181.  Areas that are shaded describe the interpretation of the behavior that the 
  182. Watcom C/16 and C/32 compilers follow. 
  183.  
  184. Programmers who are writing a program that will be ported to other systems 
  185. should pay particular attention when using these features, since other 
  186. compilers may behave in other ways.  As much as possible, an attempt is made to 
  187. describe other likely behaviors. 
  188.  
  189. This book does not describe any of the library functions that a C program might 
  190. use to interact with its environment.  In particular, input and output is not 
  191. described in this manual.  The C language does not contain any I/O 
  192. capabilities.  The Watcom C Library Reference manual describes all of the 
  193. library functions, including those used for input and output. 
  194.  
  195. A glossary is included in the appendix, and describes all terms used in the 
  196. book. 
  197.  
  198.  
  199. ΓòÉΓòÉΓòÉ 2. Notation ΓòÉΓòÉΓòÉ
  200.  
  201.  
  202.  
  203. The C programming language contains many useful features, each of which has a 
  204. number of optional parts.  The ANSI C standard describes the language in very 
  205. precise terms, often giving syntax diagrams to describe the features. 
  206.  
  207. This book attempts to describe the C language in more friendly terms.  Where 
  208. possible, features are described using ordinary English.  Jargon is avoided, 
  209. although by necessity, new terminology is introduced throughout the book.  A 
  210. glossary is provided at the end of the book to describe any terms that are 
  211. used. 
  212.  
  213. Where the variety of features would create excessive amounts of text, simple 
  214. syntax diagrams are used.  It is hoped that these are mostly self-explanatory. 
  215. However, a brief explanation of the notation used is offered here: 
  216.  
  217.    1. Required keywords are in normal lettering style (for example, enum). 
  218.  
  219.    2. Terms that describe a class of object that replace the term are in 
  220.       italics (for example, identifier). 
  221.  
  222.    3. When two or more optional forms are available, they are shown as follows: 
  223.  
  224.        form 1 
  225.        or 
  226.        form 2 
  227.  
  228.    4. Any other symbol that appears is required, unless otherwise noted. 
  229.  
  230.  The following example is for an enumerated type: 
  231.  
  232.   enum identifier 
  233.   or 
  234.   enum { enumeration-constant-list } 
  235.   or 
  236.   enum identifier { enumeration-constant-list } 
  237.  
  238.  An enumerated type has three forms: 
  239.  
  240.    1. The required keyword  enum followed by an identifier that names the type. 
  241.       The identifier is chosen by the programmer. 
  242.  
  243.    2. The required keyword  enum followed by a brace-enclosed list of 
  244.       enumeration constants.  The braces are required, and 
  245.       enumeration-constant-list is described elsewhere. 
  246.  
  247.    3. The required keyword  enum followed by an identifier and a brace-enclosed 
  248.       list of enumeration constants.  As with the previous two forms, the 
  249.       identifier may be chosen by the programmer, the braces are required and 
  250.       enumeration-constant-list is described elsewhere. 
  251.  
  252.  
  253. ΓòÉΓòÉΓòÉ 3. Basic Language Elements ΓòÉΓòÉΓòÉ
  254.  
  255.  
  256. The following topics are discussed: 
  257.  
  258.      Character Sets 
  259.      Keywords 
  260.      Identifiers 
  261.      Comments 
  262.  
  263.  
  264. ΓòÉΓòÉΓòÉ 3.1. Character Sets ΓòÉΓòÉΓòÉ
  265.  
  266.  
  267. The source character set contains the characters used during the translation of 
  268. the C source file into object code.  The  execution character set contains the 
  269. characters used during the execution of the C program.  In most cases, these 
  270. two character sets are the same, since the program is compiled and executed on 
  271. the same machine.  However, C is sometimes used to  cross-compile, whereby the 
  272. compilation of the program occurs on one machine, but the compiler generates 
  273. code for some other machine.  If the two machines have different character sets 
  274. (say EBCDIC and ASCII), then the compiler will, where appropriate, map 
  275. characters from the source character set to the execution character set.  This 
  276. mapping is implementation-defined, but generally maps the visual representation 
  277. of the character. 
  278.  
  279. Regardless of which C compiler is used, the source and execution character sets 
  280. contain (at least) the following characters: 
  281.  
  282.  
  283.    a b c d e f g h i j k l m n o p q r s t u v w x y z 
  284.    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
  285.    0 1 2 3 4 5 6 7 8 9 
  286.    ! " # % & ' ( ) * + , - . / 
  287.    : ; < = > ? [ \ ] ^ _ { | } ~ 
  288.  
  289. as well as the space (blank),  horizontal tab,  vertical tab and  form feed. 
  290. Also, a  new line character will exist for both the source and execution 
  291. character sets. 
  292.  
  293. Any character other than those previously listed should appear in a source file 
  294. in a character constant, a string or a comment, otherwise the behavior is 
  295. undefined. 
  296.  
  297. If the character set of the computer being used to compile the program does not 
  298. contain a certain character, a  trigraph sequence may be used to represent it. 
  299. Refer to the section "Character Constants". 
  300.  
  301.  The Watcom C/16 and C/32 compilers use the full IBM PC character set as both 
  302. the source and execution character sets.  The set of values from hexadecimal 00 
  303. to 7F constitute the ASCII character set. 
  304.  
  305.  
  306. ΓòÉΓòÉΓòÉ 3.1.1. Multibyte Characters ΓòÉΓòÉΓòÉ
  307.  
  308.  
  309. A multibyte character, as its name implies, is a character whose representation 
  310. consists of more than one byte. Multibyte characters allow compilers to provide 
  311. extended character sets, often for human languages that contain more characters 
  312. than those found in the one-byte character set. 
  313.  
  314. Multibyte characters are generally restricted to: 
  315.  
  316.      comments, 
  317.      string literals, 
  318.      character constants, 
  319.      header names. 
  320.  
  321.  The method for specifying multibyte characters generally varies depending upon 
  322.  the extended character set. 
  323.  
  324.  
  325. ΓòÉΓòÉΓòÉ 3.2. Keywords ΓòÉΓòÉΓòÉ
  326.  
  327.  
  328. The following words are reserved as part of the C language and are called 
  329. keywords.  They may not be used for any kind of identifier, including object 
  330. names, function names, labels, structure or union tags (names). 
  331.  
  332.  auto    double   int     struct 
  333.  break    else    long    switch 
  334.  case    enum    register  typedef 
  335.  char    extern   return   union 
  336.  const    float    short    unsigned 
  337.  continue  for     signed   void 
  338.  default   goto    sizeof   volatile 
  339.  do     if     static   while 
  340.  
  341.  The Watcom C/16 and C/32 compilers also reserve the following keywords: 
  342.  
  343.   __based   __fortran  __near   __segment 
  344.   __cdecl   __huge   _Packed   __segname 
  345.   __export  __interrupt      __pascal 
  346.   __self 
  347.   __far    __loadds  __saveregs __syscall 
  348.  The Watcom C/32 compiler also reserves the following keywords: 
  349.  
  350.  
  351.   __far16   _Seg16   __stdcall 
  352.  
  353. Note that, since C is sensitive to the case of letters, changing one or more 
  354. letters in a keyword to upper case will prevent the compiler from recognizing 
  355. it as a keyword, thereby allowing it to be used as an identifier.  However, 
  356. this is not a recommended programming practice. 
  357.  
  358.  
  359. ΓòÉΓòÉΓòÉ 3.3. Identifiers ΓòÉΓòÉΓòÉ
  360.  
  361.  
  362. Identifiers are used as: 
  363.  
  364.      object or variable names, 
  365.      function names, 
  366.      labels, 
  367.      structure, union or enumeration tags, 
  368.      the name of a member of a structure or union, 
  369.      enumeration constants, 
  370.      macro names, 
  371.      typedef names. 
  372.  
  373.  An identifier is formed by a sequence of the following characters: 
  374.  
  375.      upper-case letters "A" through "Z", 
  376.      lower-case letters "a" through "z", 
  377.      the digits "0" through "9", 
  378.      the underscore "_". 
  379.  
  380.  The first character may not be a digit. 
  381.  
  382.  An identifier cannot be a member of the list of keywords. 
  383.  
  384.  Identifiers can consist of any number of characters, but the compiler is not 
  385.  required to consider more than 31 characters as being significant, provided 
  386.  the identifier does not have  external linkage (shared by more than one 
  387.  compiled module of the program).  If the identifier is external, the compiler 
  388.  is not required to consider more than 6 characters as being significant. 
  389.  External identifiers may be case-sensitive. 
  390.  
  391.  Of course, any particular compiler may choose to consider more characters as 
  392.  being significant, but a portable C program will strictly adhere to the above 
  393.  rules.  (This restriction is likely to be relaxed in future versions of the 
  394.  ANSI C standard and corresponding C compilers.) 
  395.  
  396.   The Watcom C/16 and C/32 compilers do not restrict the number of significant 
  397.  characters for functions or objects with external or internal linkage. 
  398.  
  399.   The linker provided with Watcom C/16 and C/32 restricts the number of 
  400.  significant characters in external identifiers to 40 characters, and by 
  401.  default, does not distinguish between identifiers that differ only in the case 
  402.  of the letters.  An option may be used to force the linker to respect case 
  403.  differences. 
  404.  
  405.  Any external identifier that starts with the underscore character ("_") may be 
  406.  reserved by the compiler.  Any other identifier that starts with two 
  407.  underscores, or an underscore and an upper-case letter may be reserved. 
  408.  Generally, a program should avoid creating identifiers that start with an 
  409.  underscore. 
  410.  
  411.  
  412. ΓòÉΓòÉΓòÉ 3.4. Comments ΓòÉΓòÉΓòÉ
  413.  
  414.  
  415. A comment is identified by /* followed by any characters and terminated by */. 
  416. Comments are recognized anywhere in a program, except inside a character 
  417. constant or string.  Once the /* is found, characters are examined only until 
  418. the */ is found.  This excludes nesting of comments. 
  419.  
  420. A comment is treated as a "white-space" character, meaning that it is like a 
  421. space character. 
  422.  
  423. For example, the program fragment, 
  424.  
  425.  
  426.    /* Close all the files. 
  427.    */ 
  428.      for( i = 0; i < fcount; i++ ) { /* loop through list */ 
  429.        fclose( flist[i] );     /* close the file */ 
  430.      } 
  431.  
  432. is equivalent to, 
  433.  
  434.      for( i = 0; i < fcount; i++ ) { 
  435.        fclose( flist[i] ); 
  436.      } 
  437.  
  438. Comments are sometimes used to temporarily remove a section of code during 
  439. testing or debugging of a program.  For example, the second program fragment 
  440. could be "commented out" as follows: 
  441.  
  442.  
  443.    /* 
  444.      for( i = 0; i < fcount; i++ ) { 
  445.        fclose( flist[i] ); 
  446.      } 
  447.    */ 
  448.  
  449. This technique will not work on the first fragment because it contains 
  450. comments, and comments may not be nested.  For these cases, the  #if directive 
  451. of the C preprocessor may be used.  Refer to the chapter "The Preprocessor" for 
  452. more details. 
  453.  
  454.  The Watcom C/16 and C/32 compilers support an extension for comments.  The 
  455. symbol // can be used at any point in a physical source line (except inside a 
  456. character constant or string literal).  Any characters from the // to the end 
  457. of the line are treated as comment characters.  The comment is terminated by 
  458. the end of the line.  There is no explicit symbol for terminating the comment. 
  459. For example, the program fragment used at the beginning of this section can be 
  460. rewritten as, 
  461.  
  462.  
  463.     // Close all the files. 
  464.  
  465.       for( i = 0; i < fcount; i++ ) { // loop through list 
  466.         fclose( flist[i] );     // close the file 
  467.       } 
  468.  
  469.  This form of comment can be used to "comment out" code without the 
  470. difficulties encountered with /*. 
  471.  
  472.  
  473. ΓòÉΓòÉΓòÉ 4. Basic Types ΓòÉΓòÉΓòÉ
  474.  
  475.  
  476. The following topics are discussed: 
  477.  
  478.      Declarations of Objects 
  479.      Integer Types 
  480.      Floating-Point Types 
  481.      Enumerated Types 
  482.      Arrays 
  483.      Strings 
  484.  
  485.  
  486. ΓòÉΓòÉΓòÉ 4.1. Declarations of Objects ΓòÉΓòÉΓòÉ
  487.  
  488.  
  489. When a name is used in a program, the compiler needs to know what that name 
  490. represents.  A  declaration describes to the compiler what a name is, 
  491. including: 
  492.  
  493.      How much storage it occupies (objects) or how much storage is required 
  494.       for the value that is returned (functions), and how the value in that 
  495.       storage is to be interpreted.  This is called the type.  Examples include 
  496.       int, float and struct list. 
  497.      Whether the name is visible only within the module being compiled, or 
  498.       throughout the program.  This is called the linkage, and is part of the 
  499.       storage class.  The keywords  extern and  static determine the linkage. 
  500.      For object names, whether the object is created every time the function 
  501.       is called and destroyed every time the function returns.  This is called 
  502.       the storage duration, and is part of the storage class.  The keywords 
  503.       extern,  static,  auto and  register determine the storage duration. 
  504.  
  505.  The placement of the declaration within the program determines whether the 
  506.  declaration applies to all functions within the module, or just to the 
  507.  function within which the declaration appears. 
  508.  
  509.  The definition of an object is similar to its declaration, except that the 
  510.  storage for the object is reserved.  Whether the declaration of an object is 
  511.  also a definition depends upon the placement of the declaration and the 
  512.  attributes of the object. 
  513.  
  514.  The usual form for defining (creating) an object is as follows: 
  515.  
  516.   storage-class-specifier type-specifier declarator; 
  517.   or 
  518.   storage-class-specifier type-specifier declarator = initializer; 
  519.  
  520.  The storage-class-specifier is optional, and is thoroughly discussed in the 
  521.  chapter "Storage Classes".  The type-specifier is also optional, and is 
  522.  thoroughly discussed in the next section and in the chapter "Advanced Types". 
  523.  At least one of the storage-class-specifier and type-specifier must be 
  524.  specified, and they may be specified in either order, although it is 
  525.  recommended that the storage-class-specifier always be placed first. 
  526.  
  527.  The declarator is the name of the object being defined along with other 
  528.  information about its type.  There may be several declarators, separated by 
  529.  commas. 
  530.  
  531.  The initializer is discussed in the chapter "Initialization of Objects". 
  532.  
  533.  The following are examples of declarations of objects, along with a brief 
  534.  description of what each one means.  A more complete discussion of the terms 
  535.  used may be found in the relevant section. 
  536.  
  537.  
  538.     int x; 
  539.  
  540.  Inside a function 
  541.       The object x is declared to be an integer, with automatic storage 
  542.       duration.  Its value is available only within the function (or compound 
  543.       statement) in which it is defined.  This is also a definition. 
  544.  
  545.  Outside a function 
  546.       The object x is created and declared to be an integer with static storage 
  547.       duration.  Its value is available within the module in which it is 
  548.       defined, and has external linkage so that any other module may refer to 
  549.       it by using the declaration, 
  550.  
  551.  
  552.          extern int x; 
  553.  
  554.       This is also a definition. 
  555.  
  556.  
  557.     register void * memptr; 
  558.  
  559.  Inside a function 
  560.       The object memptr is declared to be a pointer to  void (no particular 
  561.       type of object), and is used frequently in the function.  This is also a 
  562.       definition. 
  563.  
  564.  Outside a function 
  565.       Not valid because of the  register storage class. 
  566.  
  567.  
  568.     auto long int x, y; 
  569.  
  570.  Inside a function 
  571.       The objects x and y are declared to be signed long integers with 
  572.       automatic storage duration.  This is also a definition. 
  573.  
  574.  Outside a function 
  575.       Not valid because of the  auto storage class. 
  576.  
  577.  
  578.     static int nums[10]; 
  579.  
  580.  Inside a function 
  581.       The object nums is declared to be an array of 10 integers with static 
  582.       storage duration.  Its value is only available within the function, and 
  583.       will be preserved between calls to the function.  This is also a 
  584.       definition. 
  585.  
  586.  Outside a function 
  587.       The object nums is declared to be an array of 10 integers with static 
  588.       storage duration.  Its value is only available within the module.  (The 
  589.       difference is the  scope of the object nums.) This is also a definition. 
  590.  
  591.  
  592.     extern int x; 
  593.  
  594.  Inside a function 
  595.       The object x is declared to be an integer with static storage duration. 
  596.       No other functions within the current module may refer to x unless they 
  597.       also declare it.  The object is defined in another module, or elsewhere 
  598.       in this function or module. 
  599.  
  600.  Outside a function 
  601.       The object x is declared to be an integer with static storage duration. 
  602.       Its value is available to all functions within the module.  The object is 
  603.       defined in another module, or elsewhere in this module. 
  604.  
  605.  The appendix "Examples of Declarations" contains many more examples of 
  606.  declarations of objects and functions. 
  607.  
  608.  
  609. ΓòÉΓòÉΓòÉ 4.2. Name Scope ΓòÉΓòÉΓòÉ
  610.  
  611.  
  612. An identifier may be referenced only within its scope. 
  613.  
  614. An identifier declared within a function or within a compound statement within 
  615. a function has block scope, and may be referenced only in the block in which it 
  616. is declared.  The object's scope includes any enclosed blocks and terminates at 
  617. the } which terminates the enclosing block. 
  618.  
  619. An identifier declared within a function prototype (as a parameter to that 
  620. function) has function prototype scope, and may not be referenced elsewhere. 
  621. Its scope terminates at the ) which terminates the prototype. 
  622.  
  623. An identifier declared outside of any function or function prototype has file 
  624. scope, and may be referenced anywhere within the module in which it is 
  625. declared.  If a function contains a declaration for the same identifier, the 
  626. identifier with file scope is hidden within the function.  Following the 
  627. terminating } of the function, the identifier with file scope becomes visible 
  628. again. 
  629.  
  630. A label, which must appear within a function, has function scope. 
  631.  
  632.  
  633. ΓòÉΓòÉΓòÉ 4.3. Type Specifiers ΓòÉΓòÉΓòÉ
  634.  
  635.  
  636. Every object has a type associated with it.  Functions may be defined to return 
  637. a value, and that value also has a type.  The type describes the interpretation 
  638. of a value of that type, such as whether it is signed or unsigned, a pointer, 
  639. etc.  The type also describes the amount of storage required.  Together, the 
  640. amount of storage and the interpretation of stored values describes the range 
  641. of values that may be stored in that type. 
  642.  
  643. There are a number of different types defined by the C language.  They provide 
  644. a great deal of power in selecting methods for storing and moving data, and 
  645. also contribute to the readability of the program. 
  646.  
  647. There are a number of "basic types", those which will appear in virtually every 
  648. program.  More sophisticated types provide methods to describe data structures, 
  649. and are discussed in the chapter "Advanced Types". 
  650.  
  651. A type specifier is one or more of: 
  652.  
  653.  
  654.    char 
  655.  
  656.    double 
  657.  
  658.    float 
  659.  
  660.    int 
  661.  
  662.    long 
  663.  
  664.    short 
  665.  
  666.    signed 
  667.  
  668.    unsigned 
  669.  
  670.    void 
  671.    enumeration 
  672.    structure 
  673.    union 
  674.    typedef name 
  675.  
  676. and may also include the following type qualifiers: 
  677.  
  678.  
  679.    const 
  680.  
  681.    volatile 
  682.  
  683.  The Watcom C/16 and C/32 compilers also provide the following type qualifiers: 
  684.  
  685.  
  686.   __based   __fortran  __near   __segment 
  687.   __cdecl   __huge   _Packed   __segname 
  688.   __export  __interrupt      __pascal 
  689.   __self 
  690.   __far    __loadds  __saveregs __syscall 
  691.  The Watcom C/32 compiler also provides the following type qualifiers: 
  692.  
  693.  
  694.   __far16   _Seg16   __stdcall 
  695.  The keywords  __based,  __segment,  __segname and  __self are described in the 
  696. section "Based Pointers for Watcom C/16 and C/32". 
  697.  
  698.  The keywords  __far,  __huge and  __near are described in the sections 
  699. "Special Pointer Types for Watcom C/16" and "Special Pointer Types for Watcom 
  700. C/32". 
  701.  
  702.  The keywords  __far16 and  _Seg16 are described in the section "Special 
  703. Pointer Types for Watcom C/32". 
  704.  
  705.  The  _Packed keyword is described in the section "Structures". 
  706.  
  707.  For the remaining keywords, see the appendix "Compiler Keywords". 
  708.  
  709. Various combinations of these keywords may be used when declaring an object. 
  710. Refer to the section on the type being defined. 
  711.  
  712. The main types are  char,  int,  float and  double.  The keywords  short, 
  713. long,  signed,  unsigned,  const and  volatile modify these types. 
  714.  
  715.  
  716. ΓòÉΓòÉΓòÉ 4.4. Integer Types ΓòÉΓòÉΓòÉ
  717.  
  718.  
  719. The most commonly used type is the integer.  Integers are used for storing most 
  720. numbers that do not require a decimal point, such as counters, sizes and 
  721. indices into arrays.  The range of integers is limited by the underlying 
  722. machine architecture and is usually determined by the range of values that can 
  723. be handled by the most convenient storage type of the hardware.  Most 16-bit 
  724. machines can handle integers in the range -32768 to 32767.  Larger machines 
  725. typically handle integers in the range -2147483648 to 2147483647. 
  726.  
  727. The general integer type includes a selection of types, specifying whether or 
  728. not the value is to be considered as signed (negative and positive values) or 
  729. unsigned (non-negative values), character (holds one character of the character 
  730. set), short (small range) or long (large range). 
  731.  
  732. Just specifying the type  int indicates that the amount of storage should 
  733. correspond to the most convenient storage type of the hardware.  The value is 
  734. treated as being a signed quantity.  According to the C language standard, the 
  735. minimum range for  int is -32767 to 32767, although a compiler may provide a 
  736. greater range. 
  737.  
  738.  With Watcom C/16,  int has a range of -32768 to 32767. 
  739.  
  740.  With Watcom C/32,  int has a range of -2147483648 to 2147483647. 
  741.  
  742. Specifying the type  char indicates that the amount of storage is large enough 
  743. to store any member of the execution character set.  If a member of the 
  744. required source character set (see "Character Sets") is stored in an object of 
  745. type  char, then the value is guaranteed to be positive.  Whether or not other 
  746. characters are positive is implementation-defined.  (In other words, whether 
  747. char is signed or unsigned is implementation-defined.  If it is necessary for 
  748. the object of type  char to be signed or unsigned, then the object should be 
  749. declared explicitly, as described below.) 
  750.  
  751.  The Watcom C/16 and C/32 compilers define  char to be  unsigned, allowing 
  752. objects of that type to store values in the range 0 to 255.  A command line 
  753. switch may be specified to cause  char to be treated as  signed.  This switch 
  754. should only be used when porting a C program from a system where  char is 
  755. signed. 
  756.  
  757. The  int keyword may be specified with the keywords  short or  long.  These 
  758. keywords provide additional information about the range of values to be stored 
  759. in an object of this type.  According to the C language standard, a signed 
  760. short integer has a minimum range of -32767 to 32767.  A signed long integer 
  761. has a minimum range of -2147483647 to 2147483647. 
  762.  
  763.  With Watcom C/16 and C/32,  short int has a range of -32768 to 32767, while 
  764. long int has a range of -2147483648 to 2147483647. 
  765.  
  766. The  char and  int types may be specified with the keywords  signed or 
  767. unsigned.  These keywords explicitly indicate whether the type represents a 
  768. signed or unsigned (non-negative) quantity. 
  769.  
  770. The keyword  int may be omitted from the declaration if one (or more) of the 
  771. keywords  signed,  unsigned,  short or  long is specified.  In other words, 
  772. short is equivalent to  signed short int and  unsigned long is equivalent to 
  773. unsigned long int. 
  774.  
  775. The appendix "Macros for Numerical Limits" discusses a set of macro definitions 
  776. describing the range and other characteristics of the various numeric types. 
  777. The macros from the header  <limits.h>, which describe the integer types, are 
  778. discussed. 
  779.  
  780.  The following table describes all of the various integer types and their 
  781. ranges as implemented by the Watcom C/16 and C/32 compilers.  Note that the 
  782. table is in order of increasing storage size. 
  783.  
  784.  
  785.                  Minimum   Maximum 
  786.     Type            Value    Value 
  787.     --------------------- ----------- ---------- 
  788.     signed char          -128     127 
  789.  
  790.     unsigned char          0     255 
  791.  
  792.     char               0     255 
  793.  
  794.     short int          -32768    32767 
  795.  
  796.     unsigned short int        0    65535 
  797.  
  798.     int (C/16)         -32768    32767 
  799.     int (C/32)       -2147483648 2147483647 
  800.  
  801.     unsigned int (C/16)       0    65535 
  802.     unsigned int (C/32)       0 4294967295 
  803.  
  804.     long int        -2147483648 2147483647 
  805.  
  806.     unsigned long int        0 4294967295 
  807.  
  808.  With Watcom C/16, an object of type  int has the same range as an object of 
  809. type  short int. 
  810.  
  811.  With Watcom C/32, an object of type  int has the same range as an object of 
  812. type  long int. 
  813.  
  814. The following are some examples of declarations of objects with integer type: 
  815.  
  816.  
  817.    short        a; 
  818.    unsigned short int b; 
  819.    int         c,d; 
  820.    signed       e; 
  821.    unsigned int    f; 
  822.    long        g; 
  823.    signed long     h; 
  824.    unsigned long int  i; 
  825.  
  826.  
  827. ΓòÉΓòÉΓòÉ 4.5. Floating-Point Types ΓòÉΓòÉΓòÉ
  828.  
  829.  
  830. A floating-point number is a number which may contain a decimal point and 
  831. digits following the decimal point.  The range of floating-point numbers is 
  832. usually considerably larger than that of integers, but the efficiency of 
  833. integers is usually much greater.  Integers are always exact quantities, 
  834. whereas floating-point numbers sometimes suffer from round-off error and loss 
  835. of precision. 
  836.  
  837. On some computers, floating-point arithmetic is emulated (simulated) by 
  838. software, rather than hardware.  Software emulation can greatly reduce the 
  839. speed of a program.  While this should not affect the portability of a program, 
  840. a prudent programmer limits the use of floating-point numbers. 
  841.  
  842. There are three floating-point number types,  float,  double, and  long double. 
  843.  
  844. The appendix "Macros for Numerical Limits" discusses a set of macro definitions 
  845. describing the range and other characteristics of the various numeric types. 
  846. The macros from the header  <float.h>, which describe the floating-point types, 
  847. are discussed. 
  848.  
  849.  The following table gives the ranges available on the 80x86/80x87 using the 
  850. Watcom C/16 and C/32 compiler.  The floating-point format is the IEEE Standard 
  851. for Binary Floating-Point Arithmetic (ANSI/IEEE Std 754-1985). 
  852.  
  853.  
  854.            Smallest  Largest   Digits   80x87 
  855.            Absolute  Absolute  Of     Type 
  856.     Type      Value    Value    Accuracy  Name 
  857.     -----------  --------  --------  --------  ---------- 
  858.     float     1.1E-38   3.4E+38   6      short real 
  859.     double     2.2E-308  1.7E+308  15     long real 
  860.     long double  2.2E-308  1.7E+308  15     long real 
  861.  
  862.  By default, the Watcom C/16 and C/32 compilers emulate floating-point 
  863. arithmetic.  If the 8087 or 80x87 Numeric Processor Extension (numeric 
  864. coprocessor, math chip) will be present at execution time, the compiler can be 
  865. forced to generate floating-point instructions for the coprocessor by 
  866. specifying a command line switch, as described in the User's Guide.  Other than 
  867. an improvement in execution speed, the final result should be the same as if 
  868. the processor is not present. 
  869.  
  870. The following are some examples of declarations of objects with floating-point 
  871. type: 
  872.  
  873.  
  874.    float    a; 
  875.    double    b; 
  876.    long double c; 
  877.  
  878.  
  879. ΓòÉΓòÉΓòÉ 4.6. Enumerated Types ΓòÉΓòÉΓòÉ
  880.  
  881.  
  882. Sometimes it is desirable to have a list of constant values representing 
  883. different things, and the exact values are not relevant.  They may need to be 
  884. unique or may have duplicates.  For example, a set of actions, colors or keys 
  885. might be represented in such a list.  An  enumerated type allows the creation 
  886. of a list of items. 
  887.  
  888. An  enumerated type is a set of identifiers that correspond to constants of 
  889. type  int.  These identifiers are called  enumeration constants.  The first 
  890. identifier in the set has the value 0, and subsequent identifiers are given the 
  891. previous value plus one.  Wherever a constant of type  int is allowed, an 
  892. enumeration constant may be specified. 
  893.  
  894. The following type specifier defines the set of actions available in a simple 
  895. memo program: 
  896.  
  897.  
  898.    enum actions { DISPLAY, EDIT, PURGE }; 
  899.  
  900. The enumeration constant DISPLAY is equivalent to the integer constant 0, and 
  901. EDIT and PURGE are equivalent to 1 and 2 respectively. 
  902.  
  903. An enumerated type may be given an optional  tag (name) with which it may be 
  904. identified elsewhere in the program.  In the example above, the tag of the 
  905. enumerated type is actions, which becomes a new type.  If no tag is given, then 
  906. only those objects listed following the definition of the type may have the 
  907. enumerated type. 
  908.  
  909. The  name space for enumerated type tags is different from that of object 
  910. names, labels and member names of structures and unions, so a tag may be the 
  911. same identifier as one of these other kinds.  An enumerated type tag may not be 
  912. the same as the tag of a structure or union, or another enumerated type. 
  913.  
  914. Enumeration constants may be given a specific value by specifying '=' followed 
  915. by the value.  For example, 
  916.  
  917.  
  918.    enum colors { RED = 1, BLUE = 2, GREEN = 4 }; 
  919.  
  920. creates the constants RED, BLUE and GREEN with values 1, 2 and 4 respectively. 
  921.  
  922.  
  923.    enum fruits { GRAPE, ORANGE = 6, APPLE, PLUM }; 
  924.  
  925. creates constants with values 0, 6, 7 and 8. 
  926.  
  927.  
  928.    enum fruits { GRAPE, PLUM, RAISIN = GRAPE, PRUNE = PLUM }; 
  929.  
  930. makes GRAPE and RAISIN equal to 0, and PLUM and PRUNE equal to 1. 
  931.  
  932. The formal specification of an enumerated type is as follows: 
  933.  
  934.  enum identifier 
  935.  or 
  936.  enum { enumeration-constant-list } 
  937.  or 
  938.  enum identifier { enumeration-constant-list } 
  939.  
  940.  enumeration-constant-list: 
  941.   enumeration-constant 
  942.  or 
  943.  enumeration-constant, enumeration-constant-list 
  944.  
  945.  enumeration-constant: 
  946.   identifier 
  947.  or 
  948.  identifier = constant-expression 
  949.  
  950. The type of an enumeration is implementation-defined, although it must be 
  951. compatible with an integer type.  Many compilers will use int. 
  952.  
  953.  From the following table, the Watcom C/16 compiler will choose the smallest 
  954. type that has sufficient range to represent all of the constants of a 
  955. particular enumeration: 
  956.  
  957.  
  958.            Smallest  Largest 
  959.     Type       Value   Value 
  960.     -------------  --------  -------- 
  961.     signed char    -128    127 
  962.     unsigned char     0    255 
  963.     signed int    -32768   32767 
  964.     unsigned int     0   65535 
  965.  
  966.  A command-line option may be used to force all enumerations to  int. 
  967.  
  968.  From the following table, the Watcom C/32 compiler will choose the smallest 
  969. type that has sufficient range to represent all of the constants of a 
  970. particular enumeration: 
  971.  
  972.  
  973.              Smallest    Largest 
  974.     Type        Value     Value 
  975.     -------------  ------------  ------------ 
  976.     signed char       -128      127 
  977.     unsigned char       0      255 
  978.     signed short     -32768     32767 
  979.     unsigned short       0     65535 
  980.     signed int    -2147483648   2147483647 
  981.     unsigned int        0   4294967295 
  982.  
  983.  A command-line option may be used to force all enumerations to  int. 
  984.  
  985. To create an object with enumerated type, one of two forms may be used.  The 
  986. first form is to create the type as shown above, and then to declare an object 
  987. as follows: 
  988.  
  989.  enum tag object-name; 
  990.  
  991. For example, the declaration, 
  992.  
  993.  
  994.    enum fruits fruit; 
  995.  
  996. declares the object fruit to be the enumerated type fruits. 
  997.  
  998. The second form is to list the identifiers of the objects following the closing 
  999. brace of the enumeration declaration.  For example, 
  1000.  
  1001.  
  1002.    enum fruits { GRAPE, ORANGE, APPLE, PLUM } fruit; 
  1003.  
  1004. Provided no other objects with the same enumeration are going to be declared, 
  1005. the enumerated type tag fruits is not required.  The declaration could be 
  1006. specified as, 
  1007.  
  1008.  
  1009.    enum { GRAPE, ORANGE, APPLE, PLUM } fruit; 
  1010.  
  1011. An identifier that is an enumeration constant may only appear in one 
  1012. enumeration type.  For example, the constant ORANGE may not be included in 
  1013. another enumeration, because the compiler would then have two values for 
  1014. ORANGE. 
  1015.  
  1016.  
  1017. ΓòÉΓòÉΓòÉ 4.7. Arrays ΓòÉΓòÉΓòÉ
  1018.  
  1019.  
  1020. An array is a collection of objects which are all of the same type.  All 
  1021. elements (objects) in the array are stored in contiguous (adjacent) memory. 
  1022.  
  1023. References to array elements are usually made through indexing into the array. 
  1024. To facilitate this, the elements of the array are numbered starting at zero. 
  1025. Hence an array declared with n elements is indexed using indices between 0 and 
  1026. n-1. 
  1027.  
  1028. An array may either be given an explicit size (using a constant expression) or 
  1029. its size may be determined by the number of values used to initialize it. 
  1030. Also, it is possible to declare an array without any size information, in the 
  1031. following cases: 
  1032.  
  1033.      a parameter to a function is declared as "array of type" (in which case 
  1034.       the compiler alters the type to be "pointer to type"), 
  1035.      an array object has external linkage (extern) and the definition which 
  1036.       creates the array is given elsewhere, 
  1037.      the array is fully declared later in the same module. 
  1038.  
  1039.  An array of undetermined size is an  incomplete type. 
  1040.  
  1041.  An array declaration is of the following form: 
  1042.  
  1043.   type identifier [ constant-expression ]; 
  1044.   or 
  1045.   type identifier[] = { initializer-list }; 
  1046.   or 
  1047.   type identifier[ constant-expression ] = { initializer-list }; 
  1048.   or 
  1049.   type identifier[]; 
  1050.  
  1051.  where type is the type of each element of the array, identifier is the name of 
  1052.  the array, constant-expression is an expression that evaluates to a positive 
  1053.  integer defining the number of elements in the array, and initializer-list is 
  1054.  a list of values (of type type) to be assigned to successive elements of the 
  1055.  array. 
  1056.  
  1057.  For example, 
  1058.  
  1059.  
  1060.     int values[10]; 
  1061.  
  1062.  declares values to be an array of 10 integers, with indices from 0 to 9.  The 
  1063.  expression values[5] refers to the sixth integer in the array. 
  1064.  
  1065.  
  1066.     char text[] = { "some stuff" }; 
  1067.  
  1068.  declares text to be an array of 11 characters, each containing successive 
  1069.  letters from "some stuff".  The value of text[10] is '\0' (the null 
  1070.  character), representing the terminating character in the string (see 
  1071.  Strings). 
  1072.  
  1073.  
  1074.     extern NODES nodelist[]; 
  1075.  
  1076.  declares nodelist to be an array of NODES (defined elsewhere), and the array 
  1077.  is of unknown size.  In another source file or later in the current file, 
  1078.  there must be a corresponding declaration of nodelist which defines how big 
  1079.  the array actually is. 
  1080.  
  1081.  It is possible to declare multi-dimensional arrays by including more than one 
  1082.  set of dimensions.  For example, 
  1083.  
  1084.  
  1085.     int tbl[2][3]; 
  1086.  
  1087.  defines a 2-row by 3-column array of integers.  In fact, it defines an array 
  1088.  of 2 arrays of 3 integers. The values are stored in memory in the following 
  1089.  order: 
  1090.  
  1091.  
  1092.     tbl[0][0] 
  1093.     tbl[0][1] 
  1094.     tbl[0][2] 
  1095.     tbl[1][0] 
  1096.     tbl[1][1] 
  1097.     tbl[1][2] 
  1098.  
  1099.  The rows of the table are stored together.  This form of storing an array is 
  1100.  called row-major order.  The expression tbl[1][2] refers to the element in the 
  1101.  last row and last column of the array. 
  1102.  
  1103.  In an expression, if an array is named without specifying any indices, the 
  1104.  value of the array name is the address of its first element.  In the example, 
  1105.  
  1106.  
  1107.     int  array[10]; 
  1108.     int * aptr; 
  1109.  
  1110.     aptr = array; 
  1111.  
  1112.  the assignment to aptr is equivalent to, 
  1113.  
  1114.  
  1115.     aptr = &array[0]; 
  1116.  
  1117.  Since multi-dimensional arrays are just arrays of arrays, it follows that 
  1118.  omission of some, but not all, dimensions is equivalent to taking the address 
  1119.  of the first element of the sub-array.  In the example, 
  1120.  
  1121.  
  1122.     int  array[9][5][2]; 
  1123.     int * aptr; 
  1124.  
  1125.     aptr = array[7]; 
  1126.  
  1127.  the assignment to aptr is equivalent to, 
  1128.  
  1129.  
  1130.     aptr = &array[7][0][0]; 
  1131.  
  1132.  Note that no checking of indices is performed at execution time.  An invalid 
  1133.  index (less than zero or greater than the highest index) will refer to memory 
  1134.  as if the array was extended to accommodate the index. 
  1135.  
  1136.  
  1137. ΓòÉΓòÉΓòÉ 4.8. Strings ΓòÉΓòÉΓòÉ
  1138.  
  1139.  
  1140. A string is a special form of the type "array of characters", specifically an 
  1141. array of characters terminated by a  null character.  The null character is a 
  1142. character with the value zero, represented as \0 within a string, or as the 
  1143. character constant '\0'.  Because string processing is such a common task in 
  1144. programming, C provides a set of library functions for handling strings. 
  1145.  
  1146. A string is represented by the address of the first character in the string. 
  1147. The length of a string is the number of characters up to, but not including, 
  1148. the null character. 
  1149.  
  1150. An array can be initialized to be a string using the following form: 
  1151.  
  1152.  type identifier[] = { "string value " }; 
  1153.  
  1154. (The braces are optional.) For example, 
  1155.  
  1156.  
  1157.    char ident[] = "This is my program"; 
  1158.  
  1159. declares ident to be an array of 19 characters, the last of which has the value 
  1160. zero.  The string has 18 characters plus the null character. 
  1161.  
  1162. In the above example, ident is an array whose value is a string.  However, the 
  1163. quote-enclosed value used to initialize the array is called a  string literal. 
  1164. String literals are described in the "Constants" chapter. 
  1165.  
  1166. A string may be used anywhere in a program where a "pointer to  char" may be 
  1167. used.  For example, if the declaration, 
  1168.  
  1169.  
  1170.    char * ident; 
  1171.  
  1172. was encountered, the statement, 
  1173.  
  1174.  
  1175.    ident = "This is my program"; 
  1176.  
  1177. would set the value of ident to be the address of the string "This is my 
  1178. program". 
  1179.  
  1180.  
  1181. ΓòÉΓòÉΓòÉ 5. Constants ΓòÉΓòÉΓòÉ
  1182.  
  1183.  
  1184. A constant is a value which is fixed at compilation time and is often just a 
  1185. number, character or string.  Every constant has a type which is determined by 
  1186. its form and value.  For example, the value 1 may have the type  signed int, 
  1187. while the value 400000 may have the type  signed long.  In many cases, the type 
  1188. of the constant does not matter.  If, for example, the value 1 is assigned to 
  1189. an object of type  long int, then the value 1 will be converted to a long 
  1190. integer before the assignment takes place. 
  1191.  
  1192.  
  1193. ΓòÉΓòÉΓòÉ 5.1. Integer Constants ΓòÉΓòÉΓòÉ
  1194.  
  1195.  
  1196. An integer constant begins with a digit and contains no fractional or exponent 
  1197. part.  A prefix may be included which defines whether the constant is in octal, 
  1198. decimal or hexadecimal format. 
  1199.  
  1200. A constant may be suffixed by u or U indicating an  unsigned int, or by l or L 
  1201. indicating a  long int, or by both indicating an  unsigned long int. 
  1202.  
  1203. If a constant does not start with a zero and contains a sequence of digits, 
  1204. then it is interpreted as a decimal (base 10) constant.  These are decimal 
  1205. constants: 
  1206.  
  1207.  
  1208.    7 
  1209.    762 
  1210.    98765L 
  1211.  
  1212. If the constant starts with 0x or 0X followed by the digits from 0 through 9 
  1213. and the letters a (or A) through f (or F), then the constant is interpreted as 
  1214. a hexadecimal (base 16) constant.  The letters A through F represent the values 
  1215. 10 through 15 respectively.  These are hexadecimal constants: 
  1216.  
  1217.  
  1218.    0X07FFF 
  1219.    0x12345678L 
  1220.    0xFABE 
  1221.  
  1222. If a constant starts with a zero, then it is an octal constant and may contain 
  1223. only the digits 0 through 7. These are octal constants: 
  1224.  
  1225.  
  1226.    017 
  1227.    0735643L 
  1228.    0 
  1229.  
  1230. Note that the constant 0 is actually an octal constant, but is zero in decimal, 
  1231. octal and hexadecimal. 
  1232.  
  1233. The following table describes what type the compiler will give to a constant. 
  1234. The left column indicates what base (decimal, octal or hexadecimal) is used and 
  1235. what suffixes (U or L) are present.  The right column indicates the types that 
  1236. may be given to such a constant.  The type of an integer constant is the first 
  1237. type from the table in which its value can be accurately represented. 
  1238.  
  1239.  
  1240.    Constant         Type 
  1241.    ----------------------- -------------------------------------- 
  1242.    unsuffixed decimal    int, long, unsigned long 
  1243.  
  1244.    unsuffixed octal     int, unsigned int, long, unsigned long 
  1245.  
  1246.    unsuffixed hexadecimal  int, unsigned int, long, unsigned long 
  1247.  
  1248.    suffix U only      unsigned int, unsigned long 
  1249.  
  1250.    suffix L only      long, unsigned long 
  1251.  
  1252.    suffixes U and L     unsigned long 
  1253.  
  1254. The following table illustrates a number of constants and their interpretation 
  1255. and type: 
  1256.  
  1257.  
  1258.                Hexa- 
  1259.           Decimal  decimal    Watcom C/16  Watcom C/32 
  1260.    Constant    Value   Value     Type     Type 
  1261.    ----------  ----------  --------  ------------  ------------ 
  1262.        33      33     21  signed int   signed int 
  1263.       033      27     1B  signed int   signed int 
  1264.       0x33      51     33  signed int   signed int 
  1265.      33333    33333    8235  signed long   signed int 
  1266.      033333    14043    36DB  signed int   signed int 
  1267.      0xA000    40960    A000  unsigned int  signed int 
  1268.     0x33333    209715   33333  signed long   signed int 
  1269.    0x80000000  2147483648  80000000  unsigned long  unsigned int 
  1270.    2147483648  2147483648  80000000  unsigned long  unsigned int 
  1271.    4294967295  4294967295  FFFFFFFF  unsigned long  unsigned int 
  1272.  
  1273.  
  1274. ΓòÉΓòÉΓòÉ 5.2. Floating-Point Constants ΓòÉΓòÉΓòÉ
  1275.  
  1276.  
  1277. A floating-point constant may be distinguished by the presence of either a 
  1278. period, an e or E, or both.  It consists of a value part (mantissa) optionally 
  1279. followed by an exponent.  The mantissa may include a sequence of digits 
  1280. representing a whole number, followed by a period, followed by a sequence of 
  1281. digits representing a fractional part. The exponent must start with an e or E 
  1282. followed by an optional sign (+ or -), and a digit sequence representing (with 
  1283. the sign) the power of 10 by which the mantissa should be multiplied. 
  1284. Optionally, the suffix f or F may be added indicating the constant has type 
  1285. float, or the suffix l or L indicating the constant has type  long double.  If 
  1286. no suffix is present then the constant has type  double. 
  1287.  
  1288. In the mantissa, either the whole number part or the fractional part must be 
  1289. present.  If only the whole number part is present and no period is included 
  1290. then the exponent part must be present. 
  1291.  
  1292. The following table illustrates a number of floating-point constants and their 
  1293. type: 
  1294.  
  1295.  
  1296.    Constant      Value      Type 
  1297.    ----------    ------------   ---------- 
  1298.    3.14159265    3.14159265E0   double 
  1299.      11E24    1.1E25      double 
  1300.       .5L    5E-1       long double 
  1301.    7.234E-22F    7.234E-22    float 
  1302.        0.    0E0       double 
  1303.  
  1304.  
  1305. ΓòÉΓòÉΓòÉ 5.3. Character Constants ΓòÉΓòÉΓòÉ
  1306.  
  1307.  
  1308. A character constant is usually one character enclosed in single-quotes, and 
  1309. indicates a constant whose value is the representation of the character in the 
  1310. execution character set.  A character constant has type  int. 
  1311.  
  1312. The character enclosed in quotes may be any character in the source character 
  1313. set.  Certain characters in the character set may not be directly 
  1314. representable, since they may be assigned other meanings.  These characters can 
  1315. be entered using the following escape sequences: 
  1316.  
  1317.  
  1318.          Character      Escape 
  1319.    Character  Name         Sequence 
  1320.    ---------  -----------------  -------------------- 
  1321.    '      single quote     \' 
  1322.    "      double quote     " or \" 
  1323.    ?      question mark    ? or \? 
  1324.    \      backslash      \\ 
  1325.          octal value     \octal digits (max 3) 
  1326.          hexadecimal value  \xhexadecimal digits 
  1327.  
  1328. For example, 
  1329.  
  1330.  
  1331.    'a'   /* the letter a */ 
  1332.    '\''   /* a single quote */ 
  1333.    '?'   /* a question mark */ 
  1334.    '\?'   /* a question mark */ 
  1335.    '\\'   /* a backslash */ 
  1336.  
  1337. are all simple character constants. 
  1338.  
  1339. The following are some character constants containing octal escape sequences, 
  1340. made up of a \ followed by one, two or three octal digits (the digits 0 through 
  1341. 7): 
  1342.  
  1343.  
  1344.    '\0' 
  1345.    '\377' 
  1346.    '\100' 
  1347.  
  1348. If a character constant containing an octal value is found, but a non-octal 
  1349. character is also present, or if a fourth octal digit is found, it is not part 
  1350. of the octal character already specified, and constitutes a separate character. 
  1351. For example, 
  1352.  
  1353.  
  1354.    '\1000' 
  1355.    '\109' 
  1356.  
  1357. the first constant is a two-character constant, consisting of the characters 
  1358. '\100' and '0' (because an octal value consists of at most three octal digits). 
  1359. The second constant is also a two-character constant, consisting of the 
  1360. characters '\10' and '9' (because 9 is not an octal digit). 
  1361.  
  1362. If more than one octal value is to be specified in a character constant, then 
  1363. each octal value must be specified starting with \. 
  1364.  
  1365. The meaning of character constants with more than one character is 
  1366. implementation-defined. 
  1367.  
  1368. The following are some character constants containing hexadecimal escape 
  1369. sequences, made up of a \x followed by one or more hexadecimal digits (the 
  1370. digits 0 through 9, and the letters a through f and A through F).  (The values 
  1371. of these character constants are the same as the first examples of octal values 
  1372. presented above.) 
  1373.  
  1374.  
  1375.    '\x0' 
  1376.    '\xFF' 
  1377.    '\x40' 
  1378.  
  1379. If a character constant containing a hexadecimal value is found, but a 
  1380. non-hexadecimal character is also present, it is not part of the hexadecimal 
  1381. character already specified, and constitutes a separate character.  For 
  1382. example, 
  1383.  
  1384.  
  1385.    '\xFAx' 
  1386.    '\xFx' 
  1387.  
  1388. the first constant is a two-character constant, consisting of the characters 
  1389. '\xFA' and 'x' (because x is not a hexadecimal digit).  The second constant is 
  1390. also a two-character constant, consisting of the characters '\xF' and 'x'. 
  1391.  
  1392. If more hexadecimal digits are found than are required to specify one 
  1393. character, the behavior is implementation-defined.  Specifically, any sequence 
  1394. of hexadecimal characters in a hexadecimal value in a character constant is 
  1395. used to specify the value of one character.  If more than one hexadecimal value 
  1396. is to be specified in a character constant, then each hexadecimal value must be 
  1397. specified starting with \x. 
  1398.  
  1399. The meaning of character constants with more than one character is 
  1400. implementation-defined. 
  1401.  
  1402. In addition to the above escape sequences, the following escape sequences may 
  1403. be used to represent non-graphic characters: 
  1404.  
  1405.  
  1406.    Escape 
  1407.    Sequence   Meaning 
  1408.    --------   ------------------------------------- 
  1409.    \a      Causes an audible or visual alert 
  1410.    \b      Back up one character 
  1411.    \f      Move to the start of the next page 
  1412.    \n      Move to the start of the next line 
  1413.    \r      Move to the start of the current line 
  1414.    \t      Move to the next horizontal tab 
  1415.    \v      Move to the next vertical tab 
  1416.  
  1417. The following trigraph sequences may be used to represent characters not 
  1418. available on all terminals or systems: 
  1419.  
  1420.  
  1421.          Trigraph 
  1422.    Character  Sequence 
  1423.    ---------  -------- 
  1424.      [     ??( 
  1425.      ]     ??) 
  1426.      {     ??< 
  1427.      }     ??> 
  1428.      |     ??! 
  1429.      #     ??= 
  1430.      \     ??/ 
  1431.      ^     ??' 
  1432.      ~     ??- 
  1433.  
  1434.  The Watcom C/16 and C/32 compilers also allow character constants with more 
  1435. than one character.  These may be used to initialize larger types, such as 
  1436. int.  For example, the program fragment: 
  1437.  
  1438.  
  1439.     int code; 
  1440.     code = 'ab'; 
  1441.  
  1442.  assigns the constant value 'ab' to the integer object code.  The letter b is 
  1443. placed in the lowest order (least significant) portion of the integer value and 
  1444. the letter a is placed in the next highest portion. 
  1445.  
  1446.  Up to four characters may be placed in a character constant.  Successive 
  1447. characters, starting from the right-most character in the constant, are placed 
  1448. in successively higher order (more significant) bytes of the result. 
  1449.  
  1450. Note that a character constant such as 'a' is different from the corresponding 
  1451. string literal "a".  The former is of type  int and has the value of the letter 
  1452. a in the execution character set.  The latter is of type "pointer to  char" and 
  1453. its value is the address of the first character (a) of the string literal. 
  1454.  
  1455.  
  1456. ΓòÉΓòÉΓòÉ 5.3.1. Wide Character Constants ΓòÉΓòÉΓòÉ
  1457.  
  1458.  
  1459. If the value of a character constant is to be a multibyte character from an 
  1460. extended character set, then a wide character constant should be specified. 
  1461. Its form is similar to normal character constants, except that the constant is 
  1462. preceded by the character L. 
  1463.  
  1464. The type of a wide character constant is  wchar_t, which is one of the integral 
  1465. types, and is described in the header  <stddef.h>. 
  1466.  
  1467.  With Watcom C/16 and C/32,  wchar_t is defined as  unsigned short. 
  1468.  
  1469. For example, the constant L'a' is a wide character constant containing the 
  1470. letter a from the source character set, and has type  wchar_t.  In contrast, 
  1471. the constant 'a' is a character constant containing the letter a, and has type 
  1472. int. 
  1473.  
  1474. How the multibyte character maps onto the wide character value is defined by 
  1475. the  mbtowc library function. 
  1476.  
  1477. As shown above, a wide character constant may also contain a single byte 
  1478. character, since an extended character set contains the single byte characters. 
  1479. The single byte character is mapped onto the corresponding wide character code. 
  1480.  
  1481.  
  1482. ΓòÉΓòÉΓòÉ 5.4. String Literals ΓòÉΓòÉΓòÉ
  1483.  
  1484.  
  1485. A sequence of zero or more characters enclosed within double-quotes is a 
  1486. string literal. 
  1487.  
  1488. Most of the same rules for creating character constants also apply to creating 
  1489. string literals.  However, the single-quote may be entered directly or as the 
  1490. \' escape sequence.  The double-quote must be entered as the \" escape 
  1491. sequence. 
  1492.  
  1493. The value of a string literal is the sequence of characters within the quotes, 
  1494. plus a null character at the end. 
  1495.  
  1496. The type of a string literal is "array of  char". 
  1497.  
  1498. The following are examples of string literals: 
  1499.  
  1500.  
  1501.    "Hello there" 
  1502.    "\"Quotes inside string\"" 
  1503.    "G'day" 
  1504.  
  1505. If two or more string literals are adjacent, the compiler will join them 
  1506. together into one string literal.  The pair of string literals, 
  1507.  
  1508.  
  1509.    "Hello" "there" 
  1510.  
  1511. would be joined by the compiler to be, 
  1512.  
  1513.  
  1514.    "Hellothere" 
  1515.  
  1516. and is an array of 11 characters, including the single terminating null 
  1517. character. 
  1518.  
  1519. The joining of adjacent string literals occurs after the replacement of escape 
  1520. sequences.  In the examples, 
  1521.  
  1522.  
  1523.    "\xFAB\xFA" "B" 
  1524.    "\012\01" "2" 
  1525.  
  1526. the first string, after joining, consists of three characters, with the values 
  1527. '\xFAB', '\xFA' and 'B'.  The second string, after joining, also consists of 
  1528. three characters, with the values '\012', '\01' and '2'. 
  1529.  
  1530. A program should not attempt to modify a string literal, as this behavior is 
  1531. undefined.  On computers where memory can be protected, it is likely that 
  1532. string literals will be placed where the program cannot modify them.  An 
  1533. attempt to modify them will cause the program to fail.  On other computers 
  1534. without such protection, the literal can be modified, but this is generally 
  1535. considered to be a poor programming practice.  (Constants should be constant!) 
  1536.  
  1537. A string literal normally is a string.  It is not a string if one of the 
  1538. characters within double-quotes is the null character (\0).  If such a string 
  1539. literal is treated as a string, then only those characters before the first 
  1540. null character will be considered part of the string.  The characters following 
  1541. the first null character will be ignored. 
  1542.  
  1543. If a source file uses the same string literal in several places, the compiler 
  1544. may combine them so that only one instance of the string exists and each 
  1545. reference refers to that string.  In other words, the addresses of each of the 
  1546. string literals would be the same.  However, no program should rely on this 
  1547. since other compilers may make each string a separate instance. 
  1548.  
  1549.  The Watcom C/16 and C/32 compilers combine several instances of the same 
  1550. string literal in the same module into a single string literal, provided that 
  1551. they occur in declarations of constant objects or in statements other than 
  1552. declarations (eg.  assignment). 
  1553.  
  1554. If the program requires that several string literals be the same instance, then 
  1555. an object should be declared as an array of  char with its value initialized to 
  1556. the string. 
  1557.  
  1558.  
  1559. ΓòÉΓòÉΓòÉ 5.4.1. Wide String Literals ΓòÉΓòÉΓòÉ
  1560.  
  1561.  
  1562. If any of the characters in a string literal are multibyte characters from an 
  1563. extended character set, then a wide string literal should be specified.  Its 
  1564. form is similar to normal string literals, except that the string is preceded 
  1565. by the character L. 
  1566.  
  1567. The type of a wide string literal is "array of  wchar_t".  wchar_t is one of 
  1568. the integral types, and is described in the header  <stddef.h>. 
  1569.  
  1570.  With Watcom C/16 and C/32,  wchar_t is defined as  unsigned short. 
  1571.  
  1572. For example, the string literal L"ab" is a wide string literal containing the 
  1573. letters a and b.  Its type is "array [3] of  wchar_t", and the values of its 
  1574. elements are L'a', L'b' and '\0'.  In contrast, the string literal "ab" has 
  1575. type "array [3] of  char", and the values of its elements are 'a', 'b' and 
  1576. '\0'. 
  1577.  
  1578. How the multibyte characters map onto wide character values is defined by the 
  1579. mbtowc library function. 
  1580.  
  1581. As shown above, a wide string literal may also contain single byte characters, 
  1582. since the extended character set contains the single byte characters.  The 
  1583. single byte characters are mapped onto the corresponding wide character codes. 
  1584.  
  1585. Adjacent wide string literals will be concatenated by the compiler and a null 
  1586. character appended to the end.  If a string literal and a wide string literal 
  1587. are adjacent, the behavior when the compiler attempts to concatentate them is 
  1588. undefined. 
  1589.  
  1590.  
  1591. ΓòÉΓòÉΓòÉ 6. Type Conversion ΓòÉΓòÉΓòÉ
  1592.  
  1593.  
  1594. Whenever two operands are involved in an operation, some kind of  conversion of 
  1595. one or both of the operands may take place.  For example, a  short int and a 
  1596. long int cannot be directly added.  Instead, the  short int must first be 
  1597. converted to a  long int, then the two values can be added. 
  1598.  
  1599. Fortunately, C provides most conversions as implicit operations.  Simply by 
  1600. indicating that the two values are to be added, the C compiler will check their 
  1601. types and generate the appropriate conversions.  Sometimes it is necessary, 
  1602. however, to be aware of exactly how C will convert the operands. 
  1603.  
  1604. Conversion of operands always attempts to preserve the value of the operand. 
  1605. Where preservation of the value is not possible, the compiler will sign-extend 
  1606. signed quantities and discard the high bits of quantities being converted to 
  1607. smaller types. 
  1608.  
  1609. The rules of type conversions are fully discussed in the following sections. 
  1610.  
  1611.  
  1612. ΓòÉΓòÉΓòÉ 6.1. Integral Promotion ΓòÉΓòÉΓòÉ
  1613.  
  1614.  Rule: 
  1615.       A  char,  short int or  int bit-field in either of their signed or 
  1616.       unsigned forms, or an object that has an enumerated type, is always 
  1617.       converted to an  int.  If the type  int cannot contain the entire range 
  1618.       of the object being converted, then the object will be converted to an 
  1619.       unsigned int. 
  1620.  
  1621.  A  signed or  unsigned char will be converted to a  signed int without 
  1622.  changing the value. 
  1623.  
  1624.   With Watcom C/16, a  short int has the same range as  int, therefore a 
  1625.  signed short int is converted to a  signed int, and an  unsigned short int is 
  1626.  converted to an  unsigned int, without changing the value. 
  1627.  
  1628.   With Watcom C/32, a  signed or  unsigned  short int is converted to an  int 
  1629.  without changing the value. 
  1630.  
  1631.  These promotions are called the  integral promotions. 
  1632.  
  1633.  
  1634. ΓòÉΓòÉΓòÉ 6.2. Signed and Unsigned Integer Conversion ΓòÉΓòÉΓòÉ
  1635.  
  1636.  Rule: 
  1637.       If an unsigned integer is converted to an integer type of any size, then, 
  1638.       if the value can be represented in the new type, the value remains 
  1639.       unchanged. 
  1640.  
  1641.  If an unsigned integer is converted to a longer type (type with greater 
  1642.  range), then the value will not change. If it is converted to a type with a 
  1643.  smaller range, then provided the value can be represented in the smaller 
  1644.  range, the value will remain unchanged.  If the value cannot be represented, 
  1645.  then if the result type is signed, the result is implementation-defined.  If 
  1646.  the result type is  unsigned, the result is the integer modulo (1+the largest 
  1647.  unsigned number that can be stored in the shorter type). 
  1648.  
  1649.   With Watcom C/16, unsigned integers are promoted to longer types by extending 
  1650.  the high-order bits with zeros.  They are demoted to shorter types by 
  1651.  discarding the high-order portion of the larger type. 
  1652.  
  1653.  Consider the following examples of 32-bit quantities ( unsigned long int) 
  1654.  being converted to 16-bit quantities ( signed short int or  unsigned short 
  1655.  int): 
  1656.  
  1657.  
  1658.         32-bit      16-bit      signed  unsigned 
  1659.     long   representation  representation  short   short 
  1660.     ------  --------------  --------------  ------  -------- 
  1661.     65538  0x00010002    0x0002      2     2 
  1662.     100000  0x000186A0    0x86A0      -31072  34464 
  1663.  
  1664.  Rule: 
  1665.       When a signed integer is converted to an unsigned integer of equal or 
  1666.       greater length, if the value is non-negative, the value will be 
  1667.       unchanged. 
  1668.  
  1669.  A non-negative value stored in a signed integer may be converted to an equal 
  1670.  or larger integer type without affecting the value.  A negative value is first 
  1671.  converted to the signed type of the same length as the result, then (1+the 
  1672.  largest unsigned number that can be stored in the result type) is added to the 
  1673.  value to convert it to the unsigned type. 
  1674.  
  1675.   With Watcom C/16, signed integers are promoted to longer types by 
  1676.  sign-extending the value (the high bit of the shorter type is propogated 
  1677.  throughout the high bits of the longer type).  When the longer type is 
  1678.  unsigned, the sign-extended bit-pattern is then treated as an unsigned value. 
  1679.  
  1680.  Consider the following examples of 16-bit signed quantities ( signed short 
  1681.  int) being converted to 32-bit quantities ( signed long int and  unsigned long 
  1682.  int): 
  1683.  
  1684.  
  1685.     signed  16-bit     32-bit      signed  unsigned 
  1686.     short  represention  representation  long   long 
  1687.     ------  ------------  --------------  ------  ---------- 
  1688.     -2    0xFFFE     0xFFFFFFFE    -2    4294967294 
  1689.     32766  0x7FFE     0x00007FFE    32766   32766 
  1690.  
  1691.  Rule: 
  1692.       When a signed integer is converted to a longer signed integer, the value 
  1693.       will not change. 
  1694.  
  1695.  Rule: 
  1696.       When a signed integer is converted to a shorter type, the result is 
  1697.       implementation-defined. 
  1698.  
  1699.   With Watcom C/16, signed integers are converted to a shorter type by 
  1700.  preserving the low-order (least significant) portion of the larger type. 
  1701.  
  1702.  
  1703. ΓòÉΓòÉΓòÉ 6.3. Floating-Point to Integer Conversion ΓòÉΓòÉΓòÉ
  1704.  
  1705.  Rule: 
  1706.       When a floating-point type is converted to integer, the fractional part 
  1707.       is discarded.  If the value of the integer part cannot be represented in 
  1708.       the integer type, then the result is undefined. 
  1709.  
  1710.  Hence, it is valid only to convert a floating-point type to integer within the 
  1711.  range of the integer type being converted to.  Refer to the section "Integer 
  1712.  Types" for details on the range of integers. 
  1713.  
  1714.  
  1715. ΓòÉΓòÉΓòÉ 6.4. Integer to Floating-Point Conversion ΓòÉΓòÉΓòÉ
  1716.  
  1717.  Rule: 
  1718.       When the value of an integer type is converted to a floating-point type, 
  1719.       and the integer value cannot be represented exactly in the floating-point 
  1720.       type, the value will be rounded either up or down. 
  1721.  
  1722.  Rounding of floating-point numbers is implementation-defined.  The technique 
  1723.  being used by the compiler may be determined from the macro  FLT_ROUNDS found 
  1724.  in the header  <float.h>.  The following table describes the meaning of the 
  1725.  various values: 
  1726.  
  1727.  
  1728.     FLT_ROUNDS    Technique 
  1729.     ----------  ------------------------ 
  1730.       -1    indeterminable 
  1731.       0    toward zero 
  1732.       1    to nearest number 
  1733.       2    toward positive infinity 
  1734.       3    toward negative infinity 
  1735.  
  1736.   The Watcom C/16 and C/32 compilers will round to the nearest number.  (The 
  1737.  value of FLT_ROUNDS is 1.) 
  1738.  
  1739.  Rule: 
  1740.       When a floating-point value is converted to a larger floating-point type 
  1741.       ( float to  double,  float to  long double, or  double to  long double), 
  1742.       the value remains unchanged. 
  1743.  
  1744.  Rule: 
  1745.       When any floating-point type is demoted to a floating-point type with a 
  1746.       smaller range, then the result will be undefined if the value lies 
  1747.       outside the range of the smaller type.  If the value lies inside the 
  1748.       range, but cannot be represented exactly, then rounding will occur in an 
  1749.       implementation-defined manner. 
  1750.  
  1751.   The Watcom C/16 and C/32 compilers round to the nearest number.  (The value 
  1752.  of FLT_ROUNDS is 1.) 
  1753.  
  1754.  
  1755. ΓòÉΓòÉΓòÉ 6.5. Arithmetic Conversion ΓòÉΓòÉΓòÉ
  1756.  
  1757.  
  1758. Whenever two values are used with a binary operator that expects arithmetic 
  1759. types (integer or floating-point), conversions may take place implicitly.  Most 
  1760. binary operators work on two values of the same type.  If the two values have 
  1761. different types, then the type with the smaller range is always promoted to the 
  1762. type with the greater range.  Conceptually, each type is found in the table 
  1763. below and the type found lower in the table is converted to the type found 
  1764. higher in the table. 
  1765.  
  1766.  
  1767.    long double 
  1768.    double 
  1769.    float 
  1770.    unsigned long 
  1771.    long 
  1772.    unsigned int 
  1773.    int 
  1774.  
  1775. Note that any types smaller than  int have integral promotions performed on 
  1776. them to promote them to  int. 
  1777.  
  1778. The following table illustrates the result type of performing an addition on 
  1779. combinations of various types: 
  1780.  
  1781.  
  1782.    Operation            Result Type 
  1783.    --------------------------    ------------- 
  1784.    signed char + signed char    signed int 
  1785.    unsigned char + signed int    signed int 
  1786.    signed int + signed int     signed int 
  1787.    signed int + unsigned int    unsigned int 
  1788.    unsigned int + signed long    signed long 
  1789.    signed int + unsigned long    unsigned long 
  1790.    signed char + float       float 
  1791.    signed long + double       double 
  1792.    float + double          double 
  1793.    float + long double       long double 
  1794.  
  1795.  
  1796. ΓòÉΓòÉΓòÉ 6.6. Default Argument Promotion ΓòÉΓòÉΓòÉ
  1797.  
  1798.  
  1799. When a call is made to a function, the C compiler checks to see if the function 
  1800. has been defined already, or if a prototype for that function has been found. 
  1801. If so, then the arguments to the function are converted to the specified types. 
  1802. If neither is true, then the arguments to the function are promoted as follows: 
  1803.  
  1804.      all integer types have the  integral promotions performed on them, and, 
  1805.      all arguments of type  float are promoted to  double. 
  1806.  
  1807.  If the definition of the function does not have parameters with types that 
  1808.  match the promoted types, the behavior is undefined. 
  1809.  
  1810.  
  1811. ΓòÉΓòÉΓòÉ 7. Advanced Types ΓòÉΓòÉΓòÉ
  1812.  
  1813.  
  1814. The following topics are discussed: 
  1815.  
  1816.      Structures 
  1817.      Unions 
  1818.      Pointers 
  1819.      Void 
  1820.      The const and volatile Declarations 
  1821.  
  1822.  
  1823. ΓòÉΓòÉΓòÉ 7.1. Structures ΓòÉΓòÉΓòÉ
  1824.  
  1825.  
  1826. A structure is a type composed of a sequential group of members of various 
  1827. types.  Like other types, a structure is a model describing storage 
  1828. requirements and interpretations, and does not reserve any storage.  Storage is 
  1829. reserved when an object is declared to be an instance of the structure. 
  1830.  
  1831. Each of the members of a structure must have a name, with the exception of 
  1832. bit-fields. 
  1833.  
  1834.  With Watcom C/16 and C/32, a structure member may be unnamed if the member is 
  1835. a structure or union. 
  1836.  
  1837. A structure may not contain a member with an incomplete type.  In particular, 
  1838. it may not contain a member with a type of the structure being defined 
  1839. (otherwise the structure would have indeterminate size), although it may 
  1840. contain a pointer to it. 
  1841.  
  1842. The structure may be given an optional  tag with which the structure may be 
  1843. referenced elsewhere in the program.  If no tag is given, then only those 
  1844. objects listed following the definition of the structure may have the structure 
  1845. type. 
  1846.  
  1847. The  name space for structure tags is different from that of object names, 
  1848. labels and member names, so a tag may be the same identifier as one of these 
  1849. other kinds.  A structure tag may not be the same as the tag of a union or 
  1850. enumerated type, or another structure. 
  1851.  
  1852. Each structure has its own name space, so an identifier may be used as a member 
  1853. name in more than one structure.  An identifier that is an object name, 
  1854. structure tag, union tag, union member name, enumeration tag or label may also 
  1855. be used as a member name without ambiguity. 
  1856.  
  1857. Structures help to organize program data by collecting several related objects 
  1858. into one object.  They are also used for linked lists, trees and for describing 
  1859. externally-defined regions of data that the application must access. 
  1860.  
  1861. The following structure might describe a token identified by parsing a typed 
  1862. command: 
  1863.  
  1864.  
  1865.    struct tokendef { 
  1866.      int   length; 
  1867.      int   type; 
  1868.      char  text[80]; 
  1869.    }; 
  1870.  
  1871. This defines a structure containing three members, an integer containing the 
  1872. token length, another integer containing some encoding of the token type, and 
  1873. the third an array of 80 characters containing the text of the token.  The tag 
  1874. of the structure is tokendef. 
  1875.  
  1876. The above definition does not actually create an object containing the 
  1877. structure.  Creation of an instance of the structure requires a list of 
  1878. identifiers following the structure definition, or to use struct tokendef in 
  1879. place of a type for declaring an object.  For example, 
  1880.  
  1881.  
  1882.    struct tokendef { 
  1883.      int   length; 
  1884.      int   type; 
  1885.      char  text[80]; 
  1886.    } token; 
  1887.  
  1888. is equivalent to, 
  1889.  
  1890.  
  1891.    struct tokendef { 
  1892.      int   length; 
  1893.      int   type; 
  1894.      char  text[80]; 
  1895.    }; 
  1896.  
  1897.    struct tokendef token; 
  1898.  
  1899. Both create the object token as an instance of the structure tokendef.  The 
  1900. type of token is struct tokendef. 
  1901.  
  1902. References to a member of a structure are made using the  dot operator (.). 
  1903. The first operand of the .  operator is the object containing the structure. 
  1904. The second operand is the name of the member.  For example, token.length refers 
  1905. to the length member of the tokendef structure contained in token. 
  1906.  
  1907. If tokenptr is declared as, 
  1908.  
  1909.  
  1910.    struct tokendef * tokenptr; 
  1911.  
  1912. (tokenptr is a pointer to a tokendef structure), then, 
  1913.  
  1914.  
  1915.    (*tokenptr).length 
  1916.  
  1917. refers to the length member of the tokendef structure that tokenptr points to. 
  1918. Alternatively, to refer to a member of a structure, the  arrow operator (->) is 
  1919. used: 
  1920.  
  1921.    tokenptr->length 
  1922.  
  1923. is equivalent to, 
  1924.  
  1925.  
  1926.    (*tokenptr).length 
  1927.  
  1928.  If a structure contains an unnamed member which is a structure or union, then 
  1929. the members of the inner structure or union are referenced as if they were 
  1930. members of the outer structure.  For example, 
  1931.  
  1932.  
  1933.     struct outer { 
  1934.       struct inner { 
  1935.         int   a, b; 
  1936.       }; 
  1937.       int c; 
  1938.     } X; 
  1939.  
  1940.  The members of X are referenced as X.a, X.b and X.c. 
  1941.  
  1942. Each member of a structure is at a higher address than the previous member. 
  1943. Alignment of members may cause (unnamed) gaps between members, and an unnamed 
  1944. area at the end of the structure. 
  1945.  
  1946.  The Watcom C/16 and C/32 compilers provide a command-line switch and a 
  1947. #pragma to control the alignment of members of structures.  See the User's 
  1948. Guide for details. 
  1949.  
  1950.  In addition, the  _Packed keyword is provided, and if specified before the 
  1951. struct keyword, will force the structure to be packed (no alignment, no gaps) 
  1952. regardless of the setting of the command-line switch or the  #pragma 
  1953. controlling the alignment of members. 
  1954.  
  1955. A pointer to an object with a structure type, suitably cast, is also a pointer 
  1956. to the first member of the structure. 
  1957.  
  1958. A structure declaration of the form, 
  1959.  
  1960.  struct tag; 
  1961.  
  1962. can be used to declare a new structure within a block, temporarily hiding the 
  1963. old structure.  When the block ends, the previous structure's hidden 
  1964. declaration will be restored.  For example, 
  1965.  
  1966.  
  1967.    struct thing { int a,b; }; 
  1968.    /* ... */ 
  1969.      { 
  1970.        struct thing; 
  1971.        struct s1   { struct thing * thingptr; } tptr; 
  1972.        struct thing { struct s1 *   s1ptr; }   sptr; 
  1973.      } 
  1974.  
  1975. the original definition of struct thing is suppressed in order to create a new 
  1976. definition.  Failure to suppress the original definition would result in 
  1977. thingptr being a pointer to the old definition of thing rather than the new 
  1978. one. 
  1979.  
  1980. Redefining structures can be confusing and should be avoided. 
  1981.  
  1982.  
  1983. ΓòÉΓòÉΓòÉ 7.1.1. Bit-fields ΓòÉΓòÉΓòÉ
  1984.  
  1985.  
  1986. A member of a structure can be declared as a bit-field, provided the type of 
  1987. the member is  int,  unsigned int or  signed int. 
  1988.  
  1989.  In addition, the Watcom C/16 and C/32 compilers allow the types  char, 
  1990. unsigned char,  short int and  unsigned short int to be bit-fields. 
  1991.  
  1992. A bit-field declares the member to be a number of bits.  A value may be 
  1993. assigned to the bit-field in the same manner as other integral types, provided 
  1994. the value can be stored in the number of bits available.  If the value is too 
  1995. big for the bit-field, excess high bits are discarded when the value is stored. 
  1996.  
  1997. The type of the bit-field determines the treatment of the highest bit of the 
  1998. bit-field.  Signed types cause the high bit to be treated as a sign bit, while 
  1999. unsigned types do not treat it as a sign bit.  For a bit-field defined with 
  2000. type  int (and no  signed or  unsigned keyword), whether or not the high bit is 
  2001. considered a sign bit is implementation-defined. 
  2002.  
  2003.  The Watcom C/16 and C/32 compilers treat the high bit of a bit-field of type 
  2004. int as a sign bit. 
  2005.  
  2006. A bit-field is declared by following the member name by a colon and a constant 
  2007. expression which evaluates to a non-negative value that does not exceed the 
  2008. number of bits in the type. 
  2009.  
  2010. A bit-field may be declared without a name and may be used to align a structure 
  2011. to an imposed form.  Such a bit-field cannot be referenced. 
  2012.  
  2013. If two bit-fields are declared sequentially within the same structure, and they 
  2014. would both fit within the storage unit assigned to them by the compiler, then 
  2015. they are both placed within the same storage unit.  If the second bit-field 
  2016. doesn't fit, then whether it is placed in the next storage unit, or partially 
  2017. placed in the same unit as the first and spilled over into the next unit, is 
  2018. implementation-defined. 
  2019.  
  2020.  The Watcom C/16 and C/32 compilers place a bit-field in the next storage unit 
  2021. if it will not fit in the remaining portion of the previously defined 
  2022. bit-field.  Bit-fields are not allowed to straddle storage unit boundaries. 
  2023.  
  2024. An unnamed member declared as :  0 prevents the next bit-field from being 
  2025. placed in the same storage unit as the previous bit-field. 
  2026.  
  2027. The order that bit-fields are placed in the storage unit is 
  2028. implementation-defined. 
  2029.  
  2030.  The Watcom C/16 and C/32 compilers place bit-fields starting at the low-order 
  2031. end (least significant bit) of the storage unit.  If a 1-bit bit-field is 
  2032. placed alone in an  unsigned int then a value of 1 in the bit-field corresponds 
  2033. to a value of 1 in the integer. 
  2034.  
  2035. Consider the following structure definition: 
  2036.  
  2037.  
  2038.    struct list_el { 
  2039.      struct list_el * link; 
  2040.      unsigned short  elnum; 
  2041.      unsigned int   length   : 3; 
  2042.      signed int    offset   : 4; 
  2043.      int        flag    : 1; 
  2044.      char *      text; 
  2045.    }; 
  2046.  
  2047. The structure list_el contains the following members: 
  2048.  
  2049.    1. link is a pointer to a list_el structure, indicating that instances of 
  2050.       this structure will probably be used in a linked list, 
  2051.  
  2052.    2. elnum is an unsigned short integer, 
  2053.  
  2054.    3. length is an unsigned bit-field containing 3 bits, allowing values in the 
  2055.       range 0 through 7, 
  2056.  
  2057.    4. offset is a signed bit-field containing 4 bits, which will be placed in 
  2058.       the same integer with length.  Since the type is  signed int, the range 
  2059.       of values for this bit-field is -8 through 7, 
  2060.  
  2061.    5. flag is a 1-bit field, 
  2062.  
  2063.        Since the type is  int, the Watcom C/16 and C/32 compilers will treat 
  2064.       the bit as a sign bit, and the set of values for the bit-field is -1 and 
  2065.       0. 
  2066.  
  2067.    6. text is a pointer to character, possibly a string. 
  2068.  
  2069.  
  2070. ΓòÉΓòÉΓòÉ 7.2. Unions ΓòÉΓòÉΓòÉ
  2071.  
  2072.  
  2073. A union is similar to a structure, except that each member of a union is placed 
  2074. starting at the same storage location, rather than in sequentially higher 
  2075. storage locations.  (The Pascal term for a union is "variant record".) 
  2076.  
  2077. The  name space for union tags is different from that of object names, labels 
  2078. and member names, so a tag may be the same identifier as one of these other 
  2079. kinds.  The tag may not be the same identifier as the tag of a structure, 
  2080. enumeration or another union. 
  2081.  
  2082. Each union has its own name space, so an identifier may be used as a member 
  2083. name in several different unions.  An identifier that is an object name, 
  2084. structure tag, structure member name, union tag, enumeration tag or label may 
  2085. also be used as a member name without ambiguity. 
  2086.  
  2087.  With Watcom C/16 and C/32, unions, like structures, may contain unnamed 
  2088. members that are structures or unions.  References to the members of an unnamed 
  2089. structure or union are made as if the members of the inner structure or union 
  2090. were at the outer level. 
  2091.  
  2092. The size of a union is the size of the largest of the members it contains. 
  2093.  
  2094. A pointer to an object that is a union points to each of the members of the 
  2095. union.  If one or more of the members of the union is a  bit-field, then a 
  2096. pointer to the object also points to the storage unit in which the bit-field 
  2097. resides. 
  2098.  
  2099. Storing a value in one member of a union, and then referring to it via another 
  2100. member is only meaningful when the different members have the same type. 
  2101. Members of a union may themselves be structures, and if some or all of the 
  2102. members start with the same members in each structure, then references to those 
  2103. structure members may be made via any of the union members.  For example, 
  2104. consider the following structure and union definitions: 
  2105.  
  2106.  
  2107.    struct rec1 { 
  2108.      int    rectype; 
  2109.      int    v1,v2,v3; 
  2110.      char *   text; 
  2111.    }; 
  2112.  
  2113.    struct rec2 { 
  2114.      int    rectype; 
  2115.      short int flags : 8; 
  2116.      enum    {red, blue, green} hue; 
  2117.    }; 
  2118.  
  2119.    union alt_rec { 
  2120.      struct rec1  val1; 
  2121.      struct rec2  val2; 
  2122.    }; 
  2123.  
  2124. alt_rec is a union defining two members val1 and val2, which are two different 
  2125. forms of a record, namely the structures rec1 and rec2 respectively.  Each of 
  2126. the different record forms starts with the member rectype.  The following 
  2127. program fragment would be valid: 
  2128.  
  2129.  
  2130.    union alt_rec  record; 
  2131.    /* ... */ 
  2132.    record.rec1.rectype = 33; 
  2133.    DoSomething( record.rec2.rectype ); 
  2134.  
  2135. However, the following fragment would exhibit implementation-defined behavior: 
  2136.  
  2137.  
  2138.    record.rec1.v1 = 27; 
  2139.    DoSomethingElse( record.rec2.hue ); 
  2140.  
  2141. In other words, unless several members of a union are themselves structures 
  2142. where the first few members are of the same type, a program should not store 
  2143. into a union member and retrieve a value using another union member. 
  2144. Generally, a flag or other indicator is kept to describe which member of the 
  2145. union is currently the "active" member. 
  2146.  
  2147.  
  2148. ΓòÉΓòÉΓòÉ 7.3. Pointers ΓòÉΓòÉΓòÉ
  2149.  
  2150.  
  2151. A pointer to an object is equivalent to the address of the object in the memory 
  2152. of the computer. 
  2153.  
  2154. An object may be declared to be a pointer to a type of object, or it may be 
  2155. declared to be a pointer to no particular type.  The form, 
  2156.  
  2157.  type * identifier; 
  2158.  
  2159. declares the identifier to be a pointer to the given type.  If type is  void, 
  2160. then the identifier is a pointer to no particular type of object (a generic 
  2161. pointer). 
  2162.  
  2163. The following examples illustrate various pointer declarations: 
  2164.  
  2165.  
  2166.    int * intptr; 
  2167.  
  2168.   intptr is a pointer to an  int. 
  2169.  
  2170.    char * charptr; 
  2171.  
  2172.   charptr is a pointer to a char. 
  2173.  
  2174.    struct tokendef * token; 
  2175.  
  2176.   token is a pointer to the structure tokendef. 
  2177.  
  2178.    char * argv[]; 
  2179.  
  2180.   argv is an array of pointers to  char or an array of pointers to strings. 
  2181.  
  2182.    char ** strptr; 
  2183.  
  2184.   strptr is a pointer to a pointer to  char. 
  2185.  
  2186.    void * dumpbeg; 
  2187.  
  2188.   dumpbeg is a pointer, but to no particular type of object. 
  2189. Any place that a pointer may be used, the constant 0 may also be used.  This 
  2190. value is the  null pointer constant.  The value that is used internally to 
  2191. represent a null pointer is guaranteed not to be a pointer to an object.  It 
  2192. does not necessarily correspond to the integer value 0.  It merely represents a 
  2193. pointer that does not currently point at anything.  The macro  NULL, defined in 
  2194. the header  <stddef.h>, may also be used in place of 0. 
  2195.  
  2196.  
  2197. ΓòÉΓòÉΓòÉ 7.3.1. Special Pointer Types for Watcom C/16 ΓòÉΓòÉΓòÉ
  2198.  
  2199.  
  2200.  Note:  the following sections only apply to the Watcom C/16 (16-bit) compiler. 
  2201. For the Watcom C/32 compiler, see the section "Special Pointer Types for Watcom 
  2202. C/32". 
  2203.  
  2204. On the 8086, a normal pointer (16 bits) can only point to a 64K region of the 
  2205. total memory available on the machine.  This effectively limits any program to 
  2206. a maximum of 64K of executable code and 64K of data.  For many applications, 
  2207. this does not pose a limitation. 
  2208.  
  2209. Some applications need more than 64K of code or data, or both.  The Watcom C/16 
  2210. compiler provides a mechanism whereby pointers can be declared that get beyond 
  2211. the 64K limit.  This can be done either by specifying an option when compiling 
  2212. the files (see the User's Guide) or by including a special type qualifier 
  2213. keyword in the declaration of the object.  Later sections describe these 
  2214. keywords and their use. 
  2215.  
  2216. The use of the keywords may prevent the program from compiling using other C 
  2217. compilers, in particular when the program is being transported to another 
  2218. system.  However, the preprocessor can be used to eliminate the keywords on 
  2219. these other systems. 
  2220.  
  2221. Before discussing the special pointer types, it is important to understand the 
  2222. different  memory models that are available and what they mean.  The five 
  2223. memory models are referred to as: 
  2224.  
  2225.  small 
  2226.       small code (code < 64K), small data (data < 64K) 
  2227.  
  2228.  compact 
  2229.       small code (code < 64K), big data (total data > 64K, all objects < 64K) 
  2230.  
  2231.  medium 
  2232.       big code (code > 64K), small data (data < 64K) 
  2233.  
  2234.  large 
  2235.       big code (code > 64K), big data (total data > 64K, all objects < 64K) 
  2236.  
  2237.  huge 
  2238.       big code (code > 64K), huge data (total data > 64K, objects > 64K) 
  2239.  
  2240.  The following sections discuss the memory models in terms of "small" and "big" 
  2241.  code and data sizes.  The terms "small", "compact", "medium", "large" and 
  2242.  "huge" are simply concise terms used to describe the combinations of code and 
  2243.  data sizes available. 
  2244.  
  2245.  
  2246. ΓòÉΓòÉΓòÉ 7.3.1.1. The Small and Big Code Models ΓòÉΓòÉΓòÉ
  2247.  
  2248.  
  2249. Each program can use either small code (less than 64K) or big code (more than 
  2250. 64K).  Small code means that all functions (together) must fit within the 64K 
  2251. limit on code size.  It is possible to call a function using only a 16-bit 
  2252. pointer.  This is the default. 
  2253.  
  2254. Big code removes the restriction, but requires that all functions be called 
  2255. with a 32-bit pointer.  A 32-bit pointer consists of two 16-bit quantities, 
  2256. called the  segment and  offset.  (When the computer uses the segment and 
  2257. offset to refer to an actual memory location, the two values are combined to 
  2258. produce a 20-bit memory address, which allows for the addressing of 1024K of 
  2259. memory.) Because of the larger pointers, the code generated by the big code 
  2260. option takes more space and takes longer to execute. 
  2261.  
  2262. When the big code option is being used, it is possible to group functions 
  2263. together into several 64K (or smaller) regions.  Each module can be its own 
  2264. region, or several modules can be grouped.  It is possible to call other 
  2265. functions within the same group using a 16-bit value.  These functions are said 
  2266. to be  near.  Functions outside the group can still be called, but must be 
  2267. called using a 32-bit value.  These functions are said to be  far. 
  2268.  
  2269. When the big code option is given on the command line for compiling the module, 
  2270. ordinary pointers to functions will be defined automatically to be of the 
  2271. larger type, and function calls will be done using the longer (32-bit) form. 
  2272.  
  2273. It is also possible to use the small code option, and to override certain 
  2274. functions and pointers to functions as being far.  However, this method may 
  2275. lead to problems.  The Watcom C/16 compiler generates special function calls 
  2276. that the programmer doesn't see, such as checking for stack overflow when a 
  2277. function is invoked.  These calls are either near or far depending entirely on 
  2278. the memory model chosen when the module is compiled. If the small code model is 
  2279. being used, all calls will be near calls.  If, however, several code groups are 
  2280. created with far calls between them, they will all need to access the stack 
  2281. overflow checking routines.  The linker can only place these special routines 
  2282. in one of the code groups, leaving the other functions without access to them, 
  2283. causing an error. 
  2284.  
  2285. To resolve this problem, mixing code models requires that all modules be 
  2286. compiled with the big code model, overriding certain functions as being near. 
  2287. In this manner, the stack checking routines can be placed in any code group, 
  2288. which the other code groups can still access.  Alternatively, a command-line 
  2289. switch may be used to turn off stack checking, so no stack checking routines 
  2290. get called. 
  2291.  
  2292.  
  2293. ΓòÉΓòÉΓòÉ 7.3.1.2. The Small and Big Data Models ΓòÉΓòÉΓòÉ
  2294.  
  2295.  
  2296. Each program can use either small data (less than 64K) or big data (more than 
  2297. 64K).  Small data requires that all objects exist within one 64K region of 
  2298. memory.  It is possible to refer to each object using a 16-bit pointer.  This 
  2299. is the default. 
  2300.  
  2301. Big data removes the restriction, but all pointers to data objects require a 
  2302. 32-bit pointer.  As with the big code option, extra instructions are required 
  2303. to manipulate the 32-bit pointer, so the generated code will be larger and not 
  2304. as fast. 
  2305.  
  2306. With either small or big data, each object is restricted in size to a maximum 
  2307. of 64K bytes.  However, an object may be declared as huge, allowing the object 
  2308. to be bigger than 64K bytes.  Pointers to huge objects are the least efficient 
  2309. because of extra code required to handle them, especially when doing pointer 
  2310. arithmetic.  Huge objects are discussed in the section "The _ _huge Keyword". 
  2311.  
  2312. When the big data option is being used, the program still retains one region up 
  2313. to 64K in size in which objects can be referred to using 16-bit pointers, 
  2314. regardless of the code group being executed.  These objects are said to be 
  2315. near.  Objects outside this region can still be referenced, but must be 
  2316. referred to using a 32-bit value. These objects are said to be  far. 
  2317.  
  2318. When the big data option is given on the command line for compiling the module, 
  2319. ordinary pointers to objects other than functions will be defined automatically 
  2320. to be of the larger type. 
  2321.  
  2322. It is also possible to use the small data option, and to override certain 
  2323. objects as being far.  The programmer must decide which method is easier to 
  2324. use. 
  2325.  
  2326.  
  2327. ΓòÉΓòÉΓòÉ 7.3.1.3. Mixing Memory Models ΓòÉΓòÉΓòÉ
  2328.  
  2329.  
  2330. It is possible to mix small and big code and data pointers within one program. 
  2331. In fact, a programmer striving for optimum efficiency will probably mix pointer 
  2332. types.  But great care must be taken! 
  2333.  
  2334. In some applications, the programmer may want the ability to have either big 
  2335. code or big data, but won't want to pay the extra-code penalty required to 
  2336. compile everything accordingly.  In the case of big data, the programmer may 
  2337. realize that 99% of the data structures can reside within the 64K limit, and 
  2338. the remaining ones must go beyond that limit.  Similarly, it may be desirable 
  2339. to have only a few functions that don't fit within the 64K limit. 
  2340.  
  2341. When overriding the current memory model, it is very important to declare each 
  2342. type properly. 
  2343.  
  2344. The following sections describe how to override the current memory model. 
  2345.  
  2346.  
  2347. ΓòÉΓòÉΓòÉ 7.3.1.4. The _ _far Keyword for Watcom C/16 ΓòÉΓòÉΓòÉ
  2348.  
  2349.  
  2350. When the big code memory model is in effect, functions are far and pointers to 
  2351. functions are declared automatically to be pointers to far functions. 
  2352. Similarly, the big data model causes all pointers to objects (other than 
  2353. functions) to be pointers to far objects.  However, when either the small code 
  2354. or small data model is being used, the keyword  __far may be used to override 
  2355. to the big model. 
  2356.  
  2357. The  __far keyword is a type qualifier that modifies the token that follows it. 
  2358. If  __far precedes * (as in __far *), then the pointer points to something far. 
  2359. Otherwise, if  __far precedes the identifier of the object or function being 
  2360. declared (as in __far x), then the object itself is far. 
  2361.  
  2362. The keyword  __far can only be applied to function and object names and the 
  2363. indirection (pointer) symbol *.  Parameters to functions may not be declared as 
  2364. __far since they are always in the 64K data area that is near. 
  2365.  
  2366.  Watcom C/16 provides the predefined macros  far and  _far for convenience and 
  2367. compatibility with the Microsoft C compiler.  They may be used in place of 
  2368. __far. 
  2369.  
  2370. The following examples illustrate the use of the  __far keyword.  The examples 
  2371. assume that the small memory model (small code, small data) is being used. 
  2372.  
  2373.  
  2374.    int __far * ptr; 
  2375.  
  2376.   declares ptr to be a pointer to an integer.  The object ptr is near 
  2377. (addressable using only 16 bits), but the value of the pointer is the address 
  2378. of an integer which is far, and so the pointer contains 32 bits. 
  2379.  
  2380.    int * __far fptr; 
  2381.  
  2382.   also declares fptr to be a pointer to an integer.  However, the object fptr 
  2383. is far, but the integer that it points to is near. 
  2384.  
  2385.    int __far * __far ffptr; 
  2386.  
  2387.   declares ffptr to be a pointer (which is far) to an integer (which is far). 
  2388. When declaring a function, placing the keyword  __far in front of the function 
  2389. name causes the compiler to treat the function as being far.  It is important, 
  2390. if the function is called before its definition, that a  function prototype be 
  2391. included prior to any calls.  For example, the declaration, 
  2392.  
  2393.  
  2394.    void __far BubbleSort(); 
  2395.  
  2396. declares the function BubbleSort to be far, meaning that any calls to it must 
  2397. be far calls. 
  2398.  
  2399. Here are a few more examples.  These, too, assume that the small memory model 
  2400. (small code, small data) is being used. 
  2401.  
  2402.  
  2403.    struct symbol * __far FSymAlloc( void ); 
  2404.  
  2405.   declares the function FSymAlloc to be far, returning a pointer to a near 
  2406. symbol structure. 
  2407.  
  2408.    struct symbol __far * __far FFSymAlloc( void ); 
  2409.  
  2410.   declares the function FFSymAlloc to be far, returning a pointer to a far 
  2411. symbol structure. 
  2412.  
  2413.    void Indirect( float __far fn() ); 
  2414.  
  2415.   declares the function Indirect to be near, taking one parameter fn which is a 
  2416. pointer to a far function that returns a  float. 
  2417.  
  2418.    int AdjustLeft( struct symbol * __far symptr ); 
  2419.  
  2420.   is an invalid declaration, since it attempts to declare symptr to be far. 
  2421. All parameters must be near, since they reside in the 64K data area that is 
  2422. always near. 
  2423.  
  2424.  
  2425. ΓòÉΓòÉΓòÉ 7.3.1.5. The _ _near Keyword for Watcom C/16 ΓòÉΓòÉΓòÉ
  2426.  
  2427.  
  2428. When the small code memory model is in effect, functions are near, and pointers 
  2429. to functions are automatically declared to be pointers to near functions. 
  2430. Similarly, the small data model causes all pointers to objects (other than 
  2431. functions) to be pointers to near objects.  However, when either the big code 
  2432. or big data model is being used, the keyword  __near may be used to override to 
  2433. the small model. 
  2434.  
  2435. The  __near keyword is a type qualifier that modifies the token that follows 
  2436. it.  If  __near precedes * (as in __near *), then the pointer points to 
  2437. something near.  Otherwise, if  __near precedes the identifier of the object or 
  2438. function being declared (as in __near x), then the object itself is near. 
  2439.  
  2440. The keyword  __near can only be applied to function and object names and the 
  2441. indirection (pointer) symbol *. 
  2442.  
  2443.  Watcom C/16 provides the predefined macros  near and  _near for convenience 
  2444. and compatibility with the Microsoft C compiler.  They may be used in place of 
  2445. __near. 
  2446.  
  2447. The following examples illustrate the use of the  __near keyword.  These 
  2448. examples assume that the large memory module (big code, big data) is being 
  2449. used. 
  2450.  
  2451.  
  2452.    extern int __near * x; 
  2453.  
  2454.   declares the object x to be a pointer to a near integer.  (x is not 
  2455. necessarily within the 64K data area that is near, but the integer that it 
  2456. points to is.) 
  2457.  
  2458.    extern int * __near nx; 
  2459.  
  2460.   declares the object nx to be near, and is a pointer to a far integer.  (nx is 
  2461. within the 64K data area that is near, but the integer that it points to might 
  2462. not be.) 
  2463.  
  2464.    extern int __near * __near nnx; 
  2465.  
  2466.   declares the object nnx to be near, and is a pointer to a near integer.  (nnx 
  2467. and the integer that it points to are both within the 64K data area that is 
  2468. near.) 
  2469.  
  2470.    struct symbol * __near NSymAlloc( void ); 
  2471.  
  2472.   declares the function NSymAlloc to be near, and returns a pointer to a far 
  2473. symbol structure. 
  2474.  
  2475.    struct symbol __near * __near NNSymAlloc( void ); 
  2476.  
  2477.   declares the function NNSymAlloc to be near, and returns a pointer to a near 
  2478. symbol structure. 
  2479.  
  2480.  
  2481. ΓòÉΓòÉΓòÉ 7.3.1.6. The _ _huge Keyword for Watcom C/16 ΓòÉΓòÉΓòÉ
  2482.  
  2483.  
  2484. Even using the big data model, each object is restricted in size to 64K.  Some 
  2485. applications will need to get beyond this limitation.  The Watcom C/16 compiler 
  2486. provides the keyword  __huge to describe those objects that exceed 64K in size. 
  2487. The code generated for these objects is less efficient than for  __far objects. 
  2488.  
  2489. The declaration of such objects follows the same pattern as above, with the 
  2490. keyword  __huge preceding the name of the object if the object itself is bigger 
  2491. than 64K, or preceding the * if the pointer is to an object that is bigger than 
  2492. 64K. 
  2493.  
  2494. The keyword  __huge can only be applied to arrays.  Huge objects may be used in 
  2495. both the small and big data models. 
  2496.  
  2497.  Watcom C/16 provides the predefined macros  huge and  _huge for convenience 
  2498. and compatibility with the Microsoft C compiler.  They may be used in place of 
  2499. __huge. 
  2500.  
  2501. These examples illustrate the use of the  __huge keyword.  They assume that big 
  2502. code, small data (the medium memory model) is in effect. 
  2503.  
  2504.  
  2505.    int __huge iarray[50000]; 
  2506.  
  2507.   declares the object iarray to be an array of 50000 integers, for a total size 
  2508. of 100000 bytes. 
  2509.  
  2510.    int __huge * iptr; 
  2511.  
  2512.   declares iptr to be near, and a pointer to an integer that is part of a huge 
  2513. array, such as an element of iarray. 
  2514.  
  2515.  
  2516. ΓòÉΓòÉΓòÉ 7.3.2. Special Pointer Types for Watcom C/32 ΓòÉΓòÉΓòÉ
  2517.  
  2518.  
  2519. With an 80386 processor in "protect" mode, a normal pointer (32 bits) can point 
  2520. to a 4 gigabyte (4,294,967,296 byte) region of the memory available on the 
  2521. machine.  (In practice, memory limits may mean that these regions will be 
  2522. smaller than 4 gigabytes.) These regions are called segments, and there may be 
  2523. more than one segment defined for the memory.  Each 32-bit pointer is actually 
  2524. an offset within a 4 gigabyte segment, and the offsets within two different 
  2525. segments are generally not related to each other in a known manner. 
  2526.  
  2527. As an example, the screen memory may be set up so that it resides in a 
  2528. different region of the memory from the program's data.  Normal pointers (those 
  2529. within the program's data area) will not be able to access such regions. 
  2530.  
  2531. Like the 16-bit version of Watcom C (for the 8086 and 80286), Watcom C/32 uses 
  2532. the  __near and  __far keywords to describe objects that are either in the 
  2533. normal data space or elsewhere. 
  2534.  
  2535. Objects or functions that are near require a 32-bit pointer to access them. 
  2536.  
  2537. Objects or functions that are far require a 48-bit pointer to access them. 
  2538. This 48-bit pointer consists of two parts:  a  selector consisting of 16 bits, 
  2539. and an  offset consisting of 32 bits.  A selector is similar to a segment in a 
  2540. 16-bit program's far pointer, except that the numeric value of the selector 
  2541. does not directly determine the memory region.  Instead, the processor uses the 
  2542. selector value in conjunction with a "descriptor table" to determine what 
  2543. region of memory is to be accessed.  In the discussion of far pointers on the 
  2544. 80386, the terms selector and segment may be used interchangeably. 
  2545.  
  2546. Like the 16-bit compiler, the Watcom C/32 compiler supports the small, compact, 
  2547. medium and large memory models.  Throughout the discussions in the following 
  2548. sections, it is assumed that the small memory model is being used, since it is 
  2549. the most likely to be used. 
  2550.  
  2551.  
  2552. ΓòÉΓòÉΓòÉ 7.3.2.1. The _ _far Keyword for Watcom C/32 ΓòÉΓòÉΓòÉ
  2553.  
  2554.  
  2555. The  __far keyword is a type qualifier that modifies the token that follows it. 
  2556. If  __far precedes * (as in __far *), then the pointer points to something that 
  2557. is far (not in the normal data region).  Otherwise, if  __far precedes the 
  2558. identifier of the object or function being declared (as in __far x), then the 
  2559. object or function is far. 
  2560.  
  2561. The keyword  __far can only be applied to function and object names and the 
  2562. indirection (pointer) symbol *.  Parameters to functions may not be declared as 
  2563. __far, since they are always in the normal data region. 
  2564.  
  2565. These examples illustrate the use of the  __far keyword, and assume that the 
  2566. small memory model is being used. 
  2567.  
  2568.  
  2569.    int __far * ptr; 
  2570.  
  2571.   declares ptr to be a pointer to an integer.  The object ptr is near but the 
  2572. integer that it points to is far. 
  2573.  
  2574.    int * __far fptr; 
  2575.  
  2576.   also declares fptr to be a pointer to an integer.  However, the object fptr 
  2577. is far, but the integer that it points to is near. 
  2578.  
  2579.    int __far * __far ffptr; 
  2580.  
  2581.   declares ffptr to be a pointer (which is far) to an integer (which is far). 
  2582. When declaring a function, placing the keyword  __far in front of the function 
  2583. name causes the compiler to treat the function as being far.  It is important, 
  2584. if the function is called before its definition, that a  function prototype be 
  2585. included prior to any calls.  For example, the declaration, 
  2586.  
  2587.  
  2588.    extern void __far SystemService(); 
  2589.  
  2590. declares the function SystemService to be far, meaning that any calls to it 
  2591. must be far calls. 
  2592.  
  2593. Here are a few more examples: 
  2594.  
  2595.  
  2596.    extern struct systbl * __far FSysTblPtr( void ); 
  2597.  
  2598.   declares the function FSysTblPtr to be far, returning a pointer to a near 
  2599. systbl structure. 
  2600.  
  2601.    extern struct systbl __far * __far FFSysTblPtr( void ); 
  2602.  
  2603.   declares the function FFSysTblPtr to be far, returning a pointer to a far 
  2604. systbl structure. 
  2605.  
  2606.    extern void Indirect( char __far fn() ); 
  2607.  
  2608.   declares the function Indirect to be near, taking one parameter fn which is a 
  2609. pointer to a far function that returns a  char. 
  2610.  
  2611.    extern int StoreSysTbl( struct systbl * __far sysptr ); 
  2612.  
  2613.   is an invalid declaration, since it attempts to declare sysptr to be far. 
  2614. All parameters must be near, since they reside in the normal data area that is 
  2615. always near. 
  2616.  
  2617.  
  2618. ΓòÉΓòÉΓòÉ 7.3.2.2. The _ _near Keyword for Watcom C/32 ΓòÉΓòÉΓòÉ
  2619.  
  2620.  
  2621. The  __near keyword is a type qualifier that modifies the token that follows 
  2622. it.  If  __near precedes * (as in __near *), then the pointer points to 
  2623. something that is near (in the normal data region).  Otherwise, if  __near 
  2624. precedes the identifier of the object or function being declared (as in __near 
  2625. x), then the object or function is near. 
  2626.  
  2627. The keyword  __near can only be applied to function and object names and the 
  2628. indirection (pointer) symbol *. 
  2629.  
  2630. For programmers using the small memory model, the  __near keyword is not 
  2631. required, but may be useful for making the program more readable. 
  2632.  
  2633.  
  2634. ΓòÉΓòÉΓòÉ 7.3.2.3. The _ _far16 and _Seg16 Keywords ΓòÉΓòÉΓòÉ
  2635.  
  2636.  
  2637. With the 80386 processor, a far pointer consists of a 16-bit selector and a 
  2638. 32-bit offset.  Watcom C/32 also supports a special kind of far pointer which 
  2639. consists of a 16-bit selector and a 16-bit offset.  These pointers, referred to 
  2640. as  far16 pointers, allow 32-bit code to access code and data running in 16-bit 
  2641. mode. 
  2642.  
  2643. In the OS/2 operating system (version 2.0 or higher), the first 512 megabytes 
  2644. of the 4 gigabyte segment referenced by the DS register is divided into 8192 
  2645. areas of 64K bytes each.  A far16 pointer consists of a 16-bit selector 
  2646. referring to one of the 64K byte areas, and a 16-bit offset into that area. 
  2647.  
  2648. For compatibility with Microsoft C, Watcom C/32 provides the  __far16 keyword. 
  2649. A pointer declared as, 
  2650.  
  2651.  type __far16 * name; 
  2652.  
  2653. defines an object that is a far16 pointer.  If such a pointer is accessed in 
  2654. the 32-bit environment, the compiler will generate the necessary code to 
  2655. convert between the far16 pointer and a "flat" 32-bit pointer. 
  2656.  
  2657. For example, the declaration, 
  2658.  
  2659.  
  2660.    char __far16 * bufptr; 
  2661.  
  2662. declares the object bufptr to be a far16 pointer to  char. 
  2663.  
  2664. A function declared as, 
  2665.  
  2666.  type __far16 func( parm-list ); 
  2667.  
  2668. declares a 16-bit function.  Any calls to such a function from the 32-bit 
  2669. environment will cause the compiler to convert any 32-bit pointer parameters to 
  2670. far16 pointers, and any  int parameters from 32 bits to 16 bits.  (In the 
  2671. 16-bit environment, an object of type  int is only 16 bits.) Any return value 
  2672. from the function will have its return value converted in an appropriate 
  2673. manner. 
  2674.  
  2675. For example, the declaration, 
  2676.  
  2677.  
  2678.    char * __far16 Scan( char * buffer, int buflen, short err ); 
  2679.  
  2680. declares the 16-bit function Scan.  When this function is called from the 
  2681. 32-bit environment, the buffer parameter will be converted from a flat 32-bit 
  2682. pointer to a far16 pointer (which, in the 16-bit environment, would be declared 
  2683. as char __far *).  The buflen parameter will be converted from a 32-bit integer 
  2684. to a 16-bit integer.  The err parameter will be passed unchanged.  Upon 
  2685. returning, the far16 pointer (far pointer in the 16-bit environment) will be 
  2686. converted to a 32-bit pointer which describes the equivalent location in the 
  2687. 32-bit address space. 
  2688.  
  2689. For compatibility with IBM C Set/2, Watcom C/32 provides the  _Seg16 keyword. 
  2690. Note that  _Seg16 is not interchangeable with  __far16. 
  2691.  
  2692. A pointer declared as, 
  2693.  
  2694.  type * _Seg16 name; 
  2695.  
  2696. defines an object that is a far16 pointer.  Note that the  _Seg16 appears on 
  2697. the opposite side of the * than the  __far16 keyword described above. 
  2698.  
  2699. For example, 
  2700.  
  2701.  
  2702.    char * _Seg16 bufptr; 
  2703.  
  2704. declares the object bufptr to be a far16 pointer to  char (the same as above). 
  2705.  
  2706. The  _Seg16 keyword may not be used to describe a 16-bit function.  A  #pragma 
  2707. directive must be used.  See the User's Guide for details.  A function declared 
  2708. as, 
  2709.  
  2710.  type * _Seg16 func( parm-list ); 
  2711.  
  2712. declares a 32-bit function that returns a far16 pointer. 
  2713.  
  2714. For example, the declaration, 
  2715.  
  2716.  
  2717.    char * _Seg16 Scan( char * buffer, int buflen, short err ); 
  2718.  
  2719. declares the 32-bit function Scan.  No conversion of the parameter list will 
  2720. take place.  The return value is a far16 pointer. 
  2721.  
  2722.  
  2723. ΓòÉΓòÉΓòÉ 7.3.3. Based Pointers for Watcom C/16 and C/32 ΓòÉΓòÉΓòÉ
  2724.  
  2725.  
  2726. Near pointers are generally the most efficient type of pointer because they are 
  2727. small, and the compiler can assume knowledge about what segment of the 
  2728. computer's memory the pointer (offset) refers to.  Far pointers are the most 
  2729. flexible because they allow the programmer to access any part of the computer's 
  2730. memory, without limitation to a particular segment.  However, far pointers are 
  2731. bigger and slower because of the additional flexibility. 
  2732.  
  2733. Based pointers are a compromise between the efficiency of near pointers and the 
  2734. flexibility of far pointers.  With based pointers, the programmer takes 
  2735. responsibility to tell the compiler which segment a near pointer (offset) 
  2736. belongs to, but may still access segments of the computer's memory outside of 
  2737. the normal data segment (DGROUP).  The result is a pointer type which is as 
  2738. small as and almost as efficient as a near pointer, but with most of the 
  2739. flexibility of a far pointer. 
  2740.  
  2741. An object declared as a based pointer falls into one of the following 
  2742. categories: 
  2743.  
  2744.      the based pointer is in the segment described by another object, 
  2745.      the based pointer, used as a pointer to another object of the same type 
  2746.       (as in a linked list), refers to the same segment, 
  2747.      the based pointer is an offset to no particular segment, and must be 
  2748.       combined explicitly with a segment value to produce a valid pointer. 
  2749.  
  2750.  To support based pointers, the following keywords are provided: 
  2751.  
  2752.   __based 
  2753.   __segment 
  2754.   __segname 
  2755.   __self 
  2756.  The following operator is also provided: 
  2757.  
  2758.  
  2759.     :> 
  2760.  
  2761.  These keywords and operator are described in the following sections. 
  2762.  
  2763.  Two macros, defined in  <malloc.h> are also provided: 
  2764.  
  2765.  
  2766.     _NULLSEG 
  2767.     _NULLOFF 
  2768.  
  2769.  They are used in a similar manner to  NULL, but are used with objects declared 
  2770.  as  __segment and  __based respectively. 
  2771.  
  2772.  
  2773. ΓòÉΓòÉΓòÉ 7.3.3.1. Segment Constant Based Pointers and Objects ΓòÉΓòÉΓòÉ
  2774.  
  2775.  
  2776. A segment constant based pointer or object has its segment value based on a 
  2777. specific, named segment.  A segment constant based object is specified as: 
  2778.  
  2779.  type __based( __segname( "segment" ) ) object-name; 
  2780.  
  2781. and a segment constant based pointer is specified as: 
  2782.  
  2783.  type __based( __segname( "segment" ) ) * object-name; 
  2784.  
  2785. where segment is the name of the segment in which the pointer or object is 
  2786. based.  As shown above, the segment name is always specified as a string. 
  2787. There are three special segment names recognized by the compiler: 
  2788.  
  2789.  
  2790.    "_CODE" 
  2791.    "_CONST" 
  2792.    "_DATA" 
  2793.  
  2794. The "_CODE" segment is the default code segment.  The "_CONST" segment is the 
  2795. segment containing constant values.  The "_DATA" segment is the default data 
  2796. segment.  If the segment name is not one of the three recognized names, then a 
  2797. segment will be created with that name.  If a segment constant based object is 
  2798. being defined, then it will be placed in the named segment.  If a segment 
  2799. constant based pointer is being defined, then it can point at objects in the 
  2800. named segment. 
  2801.  
  2802. The following examples illustrate segment constant based pointers and objects: 
  2803.  
  2804.  
  2805.    int __based( __segname( "_CODE" ) )  ival = 3; 
  2806.    int __based( __segname( "_CODE" ) ) * iptr; 
  2807.  
  2808. ival is an object that resides in the default code segment.  iptr is an object 
  2809. that resides in the data segment (the usual place for data objects), but points 
  2810. at an integer which resides in the default code segment.  iptr is suitable for 
  2811. pointing at ival. 
  2812.  
  2813.  
  2814.    char __based( __segname( "GOODTHINGS" ) ) thing; 
  2815.  
  2816. thing is an object which resides in the segment GOODTHINGS, which will be 
  2817. created if it does not already exist.  (The creation of segments is done by the 
  2818. linker, and is a method of grouping objects and functions.  Nothing is 
  2819. implicitly created during the execution of the program.) 
  2820.  
  2821.  
  2822. ΓòÉΓòÉΓòÉ 7.3.3.2. Segment Object Based Pointers ΓòÉΓòÉΓòÉ
  2823.  
  2824.  
  2825. A segment object based pointer derives its segment value from another named 
  2826. object.  A segment object based pointer is specified as follows: 
  2827.  
  2828.  type __based( segment ) * name; 
  2829.  
  2830. where segment is an object defined as type  __segment. 
  2831.  
  2832. An object of type  __segment may contain a segment value.  Such an object is 
  2833. particularly designed for use with segment object based pointers. 
  2834.  
  2835. The following example illustrates a segment object based pointer: 
  2836.  
  2837.  
  2838.    __segment       seg; 
  2839.    char __based( seg ) * cptr; 
  2840.  
  2841. The object seg contains only a segment value.  Whenever the object cptr is used 
  2842. to point to a character, the actual pointer value will be made up of the 
  2843. segment value found in seg and the offset value found in cptr.  The object seg 
  2844. might be assigned values such as the following: 
  2845.  
  2846.      a constant value (eg.  the segment containing screen memory), 
  2847.      the result of the library function  _bheapseg, 
  2848.      the segment portion of another pointer value, by casting it to the type 
  2849.       __segment. 
  2850.  
  2851.  
  2852. ΓòÉΓòÉΓòÉ 7.3.3.3. Void Based Pointers ΓòÉΓòÉΓòÉ
  2853.  
  2854.  
  2855. A void based pointer must be explicitly combined with a segment value to 
  2856. produce a reference to a memory location.  A void based pointer does not infer 
  2857. its segment value from another object.  The :> (base) operator is used to 
  2858. combine a segment value and a void based pointer. 
  2859.  
  2860. For example, on an IBM PC or PS/2 computer, running DOS, with a color monitor, 
  2861. the screen memory begins at segment 0xB800, offset 0.  In a video text mode, to 
  2862. examine the first character currently displayed on the screen, the following 
  2863. code could be used: 
  2864.  
  2865.  
  2866.    extern void main() 
  2867.     { 
  2868.      __segment        screen; 
  2869.      char __based( void ) * scrptr; 
  2870.  
  2871.      screen = 0xB800; 
  2872.      scrptr = 0; 
  2873.      printf( "Top left character is '%c'.\n", 
  2874.          *(screen:>scrptr) ); 
  2875.     } 
  2876.  
  2877. The general form of the :> operator is: 
  2878.  
  2879.  segment :> offset 
  2880.  
  2881. where segment is an expression of type  __segment, and offset is an expression 
  2882. of type __based( void ) *. 
  2883.  
  2884.  
  2885. ΓòÉΓòÉΓòÉ 7.3.3.4. Self Based Pointers ΓòÉΓòÉΓòÉ
  2886.  
  2887.  
  2888. A self based pointer infers its segment value from itself.  It is particularly 
  2889. useful for structures such as linked lists, where all of the list elements are 
  2890. in the same segment.  A self based pointer pointing to one element may be used 
  2891. to access the next element, and the compiler will use the same segment as the 
  2892. original pointer. 
  2893.  
  2894. The following example illustrates a function which will print the values stored 
  2895. in the last two members of a linked list: 
  2896.  
  2897.  
  2898.    struct a { 
  2899.      struct a __based( __self ) * next; 
  2900.      int              number; 
  2901.    }; 
  2902.  
  2903.  
  2904.    extern void PrintLastTwo( struct a far * list ) 
  2905.     { 
  2906.      __segment         seg; 
  2907.      struct a __based( seg ) * aptr; 
  2908.  
  2909.      seg  = FP_SEG( list ); 
  2910.      aptr = FP_OFF( list ); 
  2911.      for( ; aptr != _NULLOFF; aptr = aptr->next ) { 
  2912.        if( aptr->next == _NULLOFF ) { 
  2913.          printf( "Last item is %d\n", aptr->number ); 
  2914.        } else if( aptr->next->next == _NULLOFF ) { 
  2915.          printf( "Second last item is %d\n", aptr->number ); 
  2916.        } 
  2917.      } 
  2918.     } 
  2919.  
  2920. The parameter to the function PrintLastTwo is a far pointer, pointing to a 
  2921. linked list structure anywhere in memory.  It is assumed that all members of a 
  2922. particular linked list of this type reside in the same segment of the 
  2923. computer's memory.  (Another instance of the linked list might reside entirely 
  2924. in a different segment.) The object seg is given the segment portion of the far 
  2925. pointer.  The object aptr is given the offset portion, and is described as 
  2926. being based in the segment stored in seg. 
  2927.  
  2928. The expression aptr->next refers to the next member of the structure stored in 
  2929. memory at the offset stored in aptr and the segment implied by aptr, which is 
  2930. the value stored in seg.  So far, the behavior is no different than if next had 
  2931. been declared as, 
  2932.  
  2933.  
  2934.    struct a * next; 
  2935.  
  2936. The expression aptr->next->next illustrates the difference of using a self 
  2937. based pointer.  The first part of the expression (aptr->next) occurs as 
  2938. described above.  However, using the result to point to the next member occurs 
  2939. by using the offset value found in the next member and combining it with the 
  2940. segment value of the pointer used to get to that member, which is still the 
  2941. segment implied by aptr, which is the value stored in seg.  If next had not 
  2942. been declared using __based( __self ), then the second pointing operation would 
  2943. refer to the offset value found in the next member, but with the default data 
  2944. segment (DGROUP), which may or may not be the same segment as stored in seg. 
  2945.  
  2946.  
  2947. ΓòÉΓòÉΓòÉ 7.4. Void ΓòÉΓòÉΓòÉ
  2948.  
  2949.  
  2950. The  void type has several purposes: 
  2951.  
  2952.    1. To declare an object as being a pointer to no particular type.  For 
  2953.       example, 
  2954.  
  2955.  
  2956.          void * membegin; 
  2957.  
  2958.       defines membegin as being a pointer.  It does not point to anything 
  2959.       without a cast operator.  The statement, 
  2960.  
  2961.  
  2962.          *(char *) membegin = '\0'; 
  2963.  
  2964.       will place a zero in the character at which membegin points. 
  2965.  
  2966.    2. To declare a function as not returning a value.  For example, 
  2967.  
  2968.  
  2969.          void rewind( FILE * stream ); 
  2970.  
  2971.       declares the standard library function  rewind which takes one parameter 
  2972.       and returns nothing. 
  2973.  
  2974.    3. To evaluate an expression for its side-effects, discarding the result of 
  2975.       the expression.  For example, 
  2976.  
  2977.  
  2978.          (void) getchar(); 
  2979.  
  2980.       calls the library function  getchar, which normally returns a character. 
  2981.       In this case, the character is discarded, effectively advancing one 
  2982.       character in the file without caring what character is read.  This use of 
  2983.       void is primarily for readability, because casting the expression to the 
  2984.       void type will be done automatically.  The above example could also be 
  2985.       written as, 
  2986.  
  2987.  
  2988.          getchar(); 
  2989.  
  2990.  The keyword  void is also used in one other instance.  If a function takes no 
  2991.  parameters,  void may be used in the declaration.  For example, 
  2992.  
  2993.  
  2994.     int getchar( void ); 
  2995.  
  2996.  declares the standard library function  getchar, which takes no parameters and 
  2997.  returns an integer. 
  2998.  
  2999.  No object (other than a function) may be declared with type  void. 
  3000.  
  3001.  
  3002. ΓòÉΓòÉΓòÉ 7.5. The const and volatile Declarations ΓòÉΓòÉΓòÉ
  3003.  
  3004.  
  3005. An object may be declared with the keyword  const.  Such an object may not be 
  3006. modified directly by the program.  For objects with static storage duration, 
  3007. this type qualifier describes to the compiler which objects may be placed in 
  3008. read-only memory, if the computer supports such a concept. It also provides the 
  3009. opportunity for the compiler to detect attempts to modify the object.  The 
  3010. compiler may also generate better code when it knows that an object will not be 
  3011. modified. 
  3012.  
  3013. Even though an object is declared to be constant, it is possible to modify its 
  3014. value indirectly by storing its address (using a cast) in another object 
  3015. declared to be a pointer to the same type (without the const), and then using 
  3016. the second object to modify the value to which it points.  However, this should 
  3017. be done with caution, and may fail on computers with protected memory. 
  3018.  
  3019. If the declaration of an object does not include *, that is to say it is not a 
  3020. pointer of any kind, then the keyword  const appearing anywhere in the type 
  3021. specifier (including any  typedef's) indicates that the object is constant and 
  3022. may not be changed.  If the object is a pointer and  const appears to the left 
  3023. of the *, the object is a pointer to a constant value, meaning that the value 
  3024. to which the pointer points may not be modified, although the pointer value may 
  3025. be changed.  If  const appears to the right of the *, the object is a constant 
  3026. pointer to a value, meaning that the pointer to the value may not be changed, 
  3027. although what the pointer points to may be changed.  If  const appears on both 
  3028. sides of the *, the object is a constant pointer to a constant value, meaning 
  3029. that the pointer and the object to which it points may not be changed. 
  3030.  
  3031. If the declaration of a structure, union or array includes  const, then each 
  3032. member of the type, when referred to, is treated as if  const had been 
  3033. specified. 
  3034.  
  3035. The declarations, 
  3036.  
  3037.  
  3038.    const int  baseyear = 1900; 
  3039.    const int * byptr; 
  3040.  
  3041. declare the object baseyear to be an integer whose value is constant and set to 
  3042. 1900, and the object byptr to be a pointer to a constant object of integer 
  3043. type.  If byptr was made to point to another integer that was not, in fact, 
  3044. declared to be constant, then byptr could not be used to modify that value. 
  3045. byptr may be used to get a value from an integer object, and never to change 
  3046. it.  Another way of stating it is that what byptr points to is constant, but 
  3047. byptr itself is not constant. 
  3048.  
  3049. The declarations, 
  3050.  
  3051.  
  3052.    int     baseyear; 
  3053.    int * const byptr = &baseyear; 
  3054.  
  3055. declare the object byptr as a constant pointer to an integer, in this case the 
  3056. object baseyear.  The value of baseyear may be modified via byptr, but the 
  3057. value of byptr itself may not be changed.  In this case, byptr itself is 
  3058. constant, but what byptr points to is not constant. 
  3059.  
  3060. An object may be declared with the keyword  volatile.  Such an object may be 
  3061. freely modified by the program, and its value also may be modified through 
  3062. actions outside the program.  For example, a flag may be set when a given 
  3063. interrupt occurs.  The keyword  volatile indicates to the compiler that care 
  3064. must be taken when optimizing code referring to the object, so that the meaning 
  3065. of the program is not altered.  An object that the compiler might otherwise 
  3066. have been able to keep in a register for an extended period of time will be 
  3067. forced to reside in normal storage so that an external change to it will be 
  3068. reflected in the program's behavior. 
  3069.  
  3070. If the declaration of an object does not include *, that is to say it is not a 
  3071. pointer of any kind, then the keyword  volatile appearing anywhere in the type 
  3072. specifier (including any  typedef's) indicates that the object is volatile and 
  3073. may be changed at any time without the program knowing.  If the object is a 
  3074. pointer and  volatile appears to the left of the *, the object is a pointer to 
  3075. a volatile value, meaning that the value to which the pointer points may be 
  3076. changed at any time.  If  volatile appears to the right of the *, the object is 
  3077. a volatile pointer to a value, meaning that the pointer to the value may be 
  3078. changed at any time.  If  volatile appears on both the left and the right of 
  3079. the *, the object is a volatile pointer to a volatile value, meaning that the 
  3080. pointer or the value to which it points may be changed at any time. 
  3081.  
  3082. If the declaration of a structure, union or array includes  volatile, then each 
  3083. member of the type, when referred to, is treated as if  volatile had been 
  3084. specified. 
  3085.  
  3086. The declarations, 
  3087.  
  3088.  
  3089.    volatile int  attncount; 
  3090.    volatile int * acptr; 
  3091.  
  3092. declare the object attncount to be an integer whose value may be altered at any 
  3093. time (say by an asynchronous attention handler), and the object acptr to be a 
  3094. pointer to a volatile object of integer type. 
  3095.  
  3096. If both  const and  volatile are included in the declaration of an object, then 
  3097. that object may not be modified by the program, but it may be modified through 
  3098. some external action.  An example of such an object is the clock in a computer, 
  3099. which is modified periodically (every clock "tick"), but programs are not 
  3100. allowed to change it. 
  3101.  
  3102.  
  3103. ΓòÉΓòÉΓòÉ 8. Storage Classes ΓòÉΓòÉΓòÉ
  3104.  
  3105.  
  3106. The storage class of an object describes: 
  3107.  
  3108.      the duration of the existence of the object.  An object may exist 
  3109.       throughout the execution of the program, or only during the span of time 
  3110.       that the function in which it is defined is executing.  In the latter 
  3111.       case, each time the function is called, a new instance of the object is 
  3112.       created, and that object is destroyed when the function returns. 
  3113.      the scope of the object.  An object may be declared so that it is only 
  3114.       accessible within the function in which it is defined, within the module 
  3115.       or throughout the entire program. 
  3116.  
  3117.  A storage class specifier is one of: 
  3118.  
  3119.  
  3120.     auto 
  3121.  
  3122.     register 
  3123.  
  3124.     extern 
  3125.  
  3126.     static 
  3127.  
  3128.     typedef 
  3129.  
  3130.   typedef is included in the list of storage class specifiers for convenience, 
  3131.  because the syntax of a type definition is the same as for an object 
  3132.  declaration.  A  typedef declaration does not create an object, only a synonym 
  3133.  for a type, which does not have a storage class associated with it. 
  3134.  
  3135.  Only one of these keywords (excluding  typedef) may be specified in a 
  3136.  declaration of an object. 
  3137.  
  3138.  If an object or function is declared with a storage class, but no type 
  3139.  specifier, then the type of the object or function is assumed to be  int. 
  3140.  
  3141.  While a storage class specifier may be placed following a type specifier, this 
  3142.  tends to be difficult to read.  It is recommended that the storage class (if 
  3143.  present) always be placed first in the declaration.  The ANSI C standard 
  3144.  states that the ability to place the storage class specifier other than at the 
  3145.  beginning of the declaration is an obsolescent feature. 
  3146.  
  3147.  
  3148. ΓòÉΓòÉΓòÉ 8.1. Type Definitions ΓòÉΓòÉΓòÉ
  3149.  
  3150.  
  3151. A typedef declaration introduces a synonym for another type.  It does not 
  3152. introduce a new type. 
  3153.  
  3154. The general form of a type definition is: 
  3155.  
  3156.  typedef type-information typedef-name; 
  3157.  
  3158. The typedef-name may be a comma-separated list of identifiers, all of which 
  3159. become synonyms for the type.  The names are in the same name space as ordinary 
  3160. object names, and can be redefined in inner blocks.  However, this can be 
  3161. confusing and should be avoided. 
  3162.  
  3163. The simple declaration, 
  3164.  
  3165.  
  3166.    typedef signed int COUNTER; 
  3167.  
  3168. declares the identifier COUNTER to be equivalent to the type  signed int.  A 
  3169. subsequent declaration like, 
  3170.  
  3171.  
  3172.    COUNTER ctr; 
  3173.  
  3174. declares the object ctr to be a signed integer.  If, later on, it is necessary 
  3175. to change all counters to be long signed integers, then only the  typedef would 
  3176. have to be changed, as follows: 
  3177.  
  3178.  
  3179.    typedef long signed int COUNTER; 
  3180.  
  3181. All declarations of objects of that type will use the new type. 
  3182.  
  3183. The  typedef can be used to simplify declarations elsewhere in a program.  For 
  3184. example, consider the following structure: 
  3185.  
  3186.  
  3187.    struct complex { 
  3188.      double   real; 
  3189.      double   imaginary; 
  3190.    }; 
  3191.  
  3192. To declare an object to be an instance of the structure requires the following 
  3193. declaration: 
  3194.  
  3195.  
  3196.    struct complex cnum; 
  3197.  
  3198. Now consider the following structure definition with a type definition: 
  3199.  
  3200.  
  3201.    typedef struct { 
  3202.      double  real; 
  3203.      double  imaginary; 
  3204.    } COMPLEX; 
  3205.  
  3206. In this case, the identifier COMPLEX refers to the entire structure definition, 
  3207. including the keyword  struct.  Therefore, an object can be declared as 
  3208. follows: 
  3209.  
  3210.  
  3211.    COMPLEX cnum; 
  3212.  
  3213. While this is a simple example, it illustrates a method of making object 
  3214. declarations more readable. 
  3215.  
  3216. Consider the following example, where the object fnptr is being declared as a 
  3217. pointer to a function which takes two parameters, a pointer to a structure dim3 
  3218. and an integer.  The function returns a pointer to the structure dim3.  The 
  3219. declarations could appear as follows: 
  3220.  
  3221.  
  3222.    struct dim3 { 
  3223.      int  x; 
  3224.      int  y; 
  3225.      int  z; 
  3226.    }; 
  3227.  
  3228.    struct dim3 * (*fnptr)( struct dim3 *, int ); 
  3229.  
  3230. or as: 
  3231.  
  3232.  
  3233.    typedef struct { 
  3234.      int  x; 
  3235.      int  y; 
  3236.      int  z; 
  3237.    } DIM3; 
  3238.  
  3239.    DIM3 * (*fnptr)( DIM3 *, int ); 
  3240.  
  3241. or as: 
  3242.  
  3243.  
  3244.    typedef struct { 
  3245.      int  x; 
  3246.      int  y; 
  3247.      int  z; 
  3248.    } DIM3; 
  3249.  
  3250.    typedef DIM3 * DIM3FN( DIM3 *, int ); 
  3251.  
  3252.    DIM3FN * fnptr; 
  3253.  
  3254. The last example simply declares fnptr to be a pointer to a DIM3FN, while 
  3255. DIM3FN is declared to be a function with two parameters, a pointer to a DIM3 
  3256. and an integer.  The function returns a pointer to a DIM3.  DIM3 is declared to 
  3257. be a structure of three co-ordinates. 
  3258.  
  3259.  
  3260. ΓòÉΓòÉΓòÉ 8.1.1. Compatible Types ΓòÉΓòÉΓòÉ
  3261.  
  3262.  
  3263. Some operations, such as assignment, are restricted to operating on two objects 
  3264. of the same type.  If both operands are already the same type, then no special 
  3265. conversion is required.  Otherwise, the compiler may alter automatically one or 
  3266. both operands to make them the same type.  The integral promotions and 
  3267. arithmetic conversions are examples.  Other types may require an explicit cast. 
  3268.  
  3269. The compiler decides whether or not an explicit cast is required based on the 
  3270. concept of  compatible types.  The following types are compatible: 
  3271.  
  3272.      two types that are declared exactly the same way, 
  3273.      two types that differ only in the ordering of the type specifiers, for 
  3274.       example,  unsigned long int and  int long unsigned, 
  3275.      two arrays of members of compatible type, where both arrays have the same 
  3276.       size, or where one array is declared without size information, 
  3277.      two functions that return the same type, one containing no parameter 
  3278.       information, and the other containing a fixed number of parameters (no 
  3279.       ",...") that are not affected by the default argument promotions, 
  3280.      two structures, defined in separate modules, that have the same number 
  3281.       and names of members, in the same order, with compatible types, 
  3282.      two unions, defined in separate modules, that have the same number and 
  3283.       names of members, with compatible types, 
  3284.      two enumerated types, defined in separate modules, that have the same 
  3285.       number of enumeration constants, with the same names and the same values, 
  3286.      two pointers to compatible types. 
  3287.  
  3288.  
  3289. ΓòÉΓòÉΓòÉ 8.2. Static Storage Duration ΓòÉΓòÉΓòÉ
  3290.  
  3291.  
  3292. An object with static storage duration is created and initialized only once, 
  3293. prior to the execution of the program.  Any value stored in such an object is 
  3294. retained throughout the program unless it is explicitly altered by the program 
  3295. (or it is declared with the  volatile keyword). 
  3296.  
  3297. Any object that is declared outside the scope of a function has static storage 
  3298. duration. 
  3299.  
  3300. There are three types of static objects: 
  3301.  
  3302.    1. objects whose values are only available within the function in which they 
  3303.       are defined (no linkage).  For example, 
  3304.  
  3305.  
  3306.          extern void Fn( int x ) 
  3307.          { 
  3308.            static int ObjCount; 
  3309.          /* ... */ 
  3310.          } 
  3311.  
  3312.    2. objects whose values are only available within the module in which they 
  3313.       are defined (internal linkage).  For example, 
  3314.  
  3315.  
  3316.          static int ObjCount; 
  3317.  
  3318.          extern void Fn( int x ) 
  3319.          { 
  3320.          /* ... */ 
  3321.          } 
  3322.  
  3323.    3. objects whose values are available to all components of the program 
  3324.       (external linkage).  For example, 
  3325.  
  3326.  
  3327.          extern int ObjCount = { 0 }; 
  3328.  
  3329.          extern void Fn( int x ) 
  3330.          { 
  3331.          /* ... */ 
  3332.          } 
  3333.  
  3334.  The first two types are defined with the keyword  static, while the third is 
  3335.  defined with the (optional) keyword  extern. 
  3336.  
  3337.  
  3338. ΓòÉΓòÉΓòÉ 8.2.1. The static Storage Class ΓòÉΓòÉΓòÉ
  3339.  
  3340.  
  3341. Any declaration of an object may be preceded by the keyword  static.  A 
  3342. declaration inside a function indicates to the compiler that the object has  no 
  3343. linkage, meaning that it is available only within the function.  A declaration 
  3344. not inside any function indicates to the compiler that this object has 
  3345. internal linkage, meaning that it is available in all functions within the 
  3346. module in which it is defined.  Other modules may not refer to the specific 
  3347. object.  They may have their own object defined with the same name, but this is 
  3348. a questionable programming practice and should be avoided. 
  3349.  
  3350. The value of the object will be preserved between function calls.  Any value 
  3351. placed in an object with static storage duration will remain unchanged until 
  3352. changed by a function within the same module.  It is also possible for a 
  3353. pointer to the object to be passed to a function outside the module in which 
  3354. the object is defined.  This pointer could be used to modify the value of the 
  3355. object. 
  3356.  
  3357.  
  3358. ΓòÉΓòÉΓòÉ 8.2.2. The extern Storage Class ΓòÉΓòÉΓòÉ
  3359.  
  3360.  
  3361. If an object is declared with the keyword  extern inside a function, then the 
  3362. object has  external linkage, meaning that its value is available to all 
  3363. modules, and to the function(s) containing the definition in the current 
  3364. module.  No initializer list may be specified in this case, which implies that 
  3365. the space for the object is allocated in some other module. 
  3366.  
  3367. If an object is declared outside of the definition of a function, and the 
  3368. declaration does not contain either of the keywords  static or  extern, then 
  3369. the space for the object is created at this point.  The object has  external 
  3370. linkage, meaning that it is available to other modules in the program. 
  3371.  
  3372. The following examples illustrate the creation of external objects, provided 
  3373. the declarations occur outside any function: 
  3374.  
  3375.  
  3376.    int  X; 
  3377.    float F; 
  3378.  
  3379. If the declaration for an object, outside of the definition of a function, 
  3380. contains the keyword  extern and has an initializer list, then space for the 
  3381. object is created at this point, and the object has external linkage.  If, 
  3382. however, the declaration does not include an initializer list, then the 
  3383. compiler assumes that the object is declared elsewhere.  If, during the 
  3384. remainder of the compilation of the module, no further declarations of the 
  3385. object are found, or more declarations with  extern and no initializer list are 
  3386. found, then the object must have space allocated for it in another module.  If 
  3387. a subsequent declaration in the same module does have an initializer list or 
  3388. omits the  extern keyword, then the space for the object is created at that 
  3389. point. 
  3390.  
  3391. The following examples also illustrate the creation of external objects: 
  3392.  
  3393.  
  3394.    extern LIST * ListHead = 0; 
  3395.       int   StartVal = 77; 
  3396.  
  3397. However, the next examples illustrate the tentative definition of external 
  3398. objects.  If no further definition of the object of a form shown above is 
  3399. found, then the object is found outside of the module. 
  3400.  
  3401.  
  3402.    extern LIST * ListEl; 
  3403.    extern int   Z; 
  3404.  
  3405. Another module may define its own object with the same name (provided it has 
  3406. static storage class), but it will not be able to access the external one. 
  3407. However, this can be confusing and is a questionable programming practice. 
  3408.  
  3409. Any value placed in an object declared with the  extern keyword will remain 
  3410. unchanged until changed by a function within the same or another module. 
  3411.  
  3412. A function that is declared without the keyword  static has external linkage. 
  3413.  
  3414. Suppose a module declares an object (outside of any function definition) as 
  3415. follows: 
  3416.  
  3417.  
  3418.    struct list_el * ListTop; 
  3419.  
  3420. where the structure list_el is defined elsewhere.  This declaration allocates 
  3421. space for and declares the object ListTop to be a pointer to a structure 
  3422. list_el, with external linkage.  Another module with the declaration, 
  3423.  
  3424.  
  3425.    extern struct list_el * ListTop; 
  3426.  
  3427. refers to the same object ListTop, and states that it is found outside of the 
  3428. module. 
  3429.  
  3430. Within a program, possibly consisting of more than one module, each object or 
  3431. function with external linkage must be defined (have space allocated for it) 
  3432. exactly once. 
  3433.  
  3434.  
  3435. ΓòÉΓòÉΓòÉ 8.3. Automatic Storage Duration ΓòÉΓòÉΓòÉ
  3436.  
  3437.  
  3438. The most commonly used object in a C program is one that has meaning only 
  3439. within the function in which it is defined.  The object is created when 
  3440. execution of the function is begun and destroyed when execution of the function 
  3441. is completed.  Such an object is said to have automatic storage duration.  The 
  3442. scope of the object is said to be the function in which it is defined. 
  3443.  
  3444. If such an object has the same name as another object defined outside the 
  3445. function (using  static or  extern), then the outside object is hidden from the 
  3446. function. 
  3447.  
  3448. Within a function, any object that does not have its declaration preceded by 
  3449. the keyword  static or  extern has automatic storage duration. 
  3450.  
  3451. It is possible to declare an object as automatic within any block of a 
  3452. function.  The scope of such an object is the block in which it is declared, 
  3453. including any blocks inside it.  Any outside block is unable to access such an 
  3454. object. 
  3455.  
  3456. Automatic objects may be initialized as described in the chapter 
  3457. "Initialization of Objects".  Initialization of the object only occurs when the 
  3458. block in which the object is declared is entered normally.  In particular, a 
  3459. jump into a block nested within the function will not initialize any objects 
  3460. declared in that block.  This is a questionable programming practice, and 
  3461. should be avoided. 
  3462.  
  3463. The following function checks a string to see if it contains nothing but 
  3464. digits: 
  3465.  
  3466.  
  3467.    extern int IsInt( const char * ptr ) 
  3468.    /**********************************/ 
  3469.    { 
  3470.      if( *ptr == '\0' ) return( 0 ); 
  3471.      for( ;; ) { 
  3472.        char  ch; 
  3473.  
  3474.        ch = *(ptr++); 
  3475.        if( ch == '\0' )   return( 1 ); 
  3476.        if( !isdigit( ch ) ) return( 0 ); 
  3477.      } 
  3478.    } 
  3479.  
  3480. The object ch has a scope consisting only of the  for loop.  Any statements 
  3481. before or after the loop cannot access ch. 
  3482.  
  3483.  
  3484. ΓòÉΓòÉΓòÉ 8.3.1. The auto Storage Class ΓòÉΓòÉΓòÉ
  3485.  
  3486.  
  3487. The declaration of an object in a function that does not contain the keywords 
  3488. static,  extern or  register declares an object with automatic storage 
  3489. duration.  Such an object may precede its declaration with the keyword  auto 
  3490. for readability. 
  3491.  
  3492. An object declared with no storage class specifier or with  auto is 
  3493. "addressable", which means that the address-of operator may be applied to it. 
  3494.  
  3495. The programmer should not assume any relationship between the storage locations 
  3496. of multiple  auto objects declared in a function.  If relative placement of 
  3497. objects is important, a structure should be used. 
  3498.  
  3499. The following function illustrates a use for  auto objects: 
  3500.  
  3501.  
  3502.    extern int FindSize( struct thing * thingptr ) 
  3503.    /********************************************/ 
  3504.    { 
  3505.      auto char * start; 
  3506.      auto char * finish; 
  3507.  
  3508.      FindEnds( thingptr, &start, &finish ); 
  3509.      return( finish - start + 1 ); 
  3510.    } 
  3511.  
  3512. The addresses of the automatic objects start and finish are passed to FindEnds, 
  3513. which, presumably, modifies them. 
  3514.  
  3515.  
  3516. ΓòÉΓòÉΓòÉ 8.3.2. The register Storage Class ΓòÉΓòÉΓòÉ
  3517.  
  3518.  
  3519. An object that is declared within a function, and whose declaration includes 
  3520. the keyword  register, is considered to have automatic storage duration.  The 
  3521. register keyword merely provides a hint to the compiler that this object is 
  3522. going to be heavily used, allowing the compiler to try to put it into a 
  3523. high-speed access part of the machine, such as a machine register.  The 
  3524. compiler may, however, ignore such a directive for any number of reasons, such 
  3525. as, 
  3526.  
  3527.      the compiler does not support objects in registers, 
  3528.      there are no available registers, or, 
  3529.      the compiler makes its own decisions about register usage. 
  3530.  
  3531.  Only certain types of objects may be placed in registers, although the set of 
  3532.  such types is implementation-defined. 
  3533.  
  3534.   The Watcom C/16 and C/32 compilers may place any object that is sufficiently 
  3535.  small, including a small structure, in one or more registers. 
  3536.  
  3537.   The compiler will decide which objects will be placed in registers.  The 
  3538.  register keyword is ignored, except to prevent taking the address of such an 
  3539.  object. 
  3540.  
  3541.  Objects declared with or without  register may generally be treated in the 
  3542.  same way.  An exception to this rule is that the  address-of operator (&) may 
  3543.  not be applied to a  register object, since registers are generally not within 
  3544.  the normal storage of the computer. 
  3545.  
  3546.  
  3547. ΓòÉΓòÉΓòÉ 9. Initialization of Objects ΓòÉΓòÉΓòÉ
  3548.  
  3549.  
  3550. Any definition of an object may include a value or list of values for 
  3551. initializing it, in which case the declaration is followed by an equal sign (=) 
  3552. and the initial value(s). 
  3553.  
  3554. The initial value for an object with static storage duration may be any 
  3555. expression that evaluates to a constant value, including using the address-of 
  3556. operator to take the address of a function or object with static storage 
  3557. duration. 
  3558.  
  3559. The initial value for an object with automatic storage duration may be any 
  3560. expression that would be valid as an assignment to that object, including 
  3561. references to other objects.  The evaluations of the initializations occur in 
  3562. the order in which the definitions of the objects occur. 
  3563.  
  3564.  
  3565. ΓòÉΓòÉΓòÉ 9.1. Initialization of Scalar Types ΓòÉΓòÉΓòÉ
  3566.  
  3567.  
  3568. The initial value for a scalar type (pointers, integers and floating-point 
  3569. types) may be enclosed in braces, although braces are not required. 
  3570.  
  3571. The following declarations might appear inside a function: 
  3572.  
  3573.  
  3574.    static  int  MaxRecLen  = 1000; 
  3575.    static  int  MaxMemSize = { 1000 * 8 + 10000 }; 
  3576.        float Pi     = 3.14159; 
  3577.    auto   int  x      = 3; 
  3578.    register int  y      = x * MaxRecLen; 
  3579.  
  3580.  
  3581. ΓòÉΓòÉΓòÉ 9.2. Initialization of Arrays ΓòÉΓòÉΓòÉ
  3582.  
  3583.  
  3584. For arrays of characters being initialized with a string literal, and for 
  3585. arrays of  wchar_t being initialized with a wide string literal, the braces 
  3586. around initial values are optional.  For other arrays, the braces are required. 
  3587.  
  3588. If an array of unknown size is initialized, then the size of the array is 
  3589. determined by the number of initializing values provided.  In particular, an 
  3590. array of characters of unknown size may be initialized using a string literal, 
  3591. in which case the size of the array is the number of characters in the string, 
  3592. plus one for the terminating null character.  Each character of the string is 
  3593. placed in successive elements of the array.  Consider the following array 
  3594. declarations: 
  3595.  
  3596.  
  3597.    char  StartPt[] = "Starting point..."; 
  3598.    int  Tabs[]   = { 1, 9, 17, 25, 33, 41 }; 
  3599.    float Roots[]  = { 1., 1.414, 1.732, 2., 2.236 }; 
  3600.  
  3601. The object StartPt is an array of 18 characters, Tabs is an array of 6 
  3602. integers, and Roots is an array of 5 floating-point numbers. 
  3603.  
  3604. If an array is declared to have a certain number of elements, then the maximum 
  3605. number of values in the initialization list is the number of elements in the 
  3606. array.  An exception is made for arrays of characters, where the initializer 
  3607. may be a string with the same length as the number of characters in the array. 
  3608. Each character from the string is assigned to the corresponding element of the 
  3609. array.  The null character at the end of the string literal is ignored. 
  3610.  
  3611. If there are fewer initialization values than elements of the array, then any 
  3612. elements not receiving a value from the list are assigned the value zero (for 
  3613. arithmetic types), or the null pointer constant (for pointers).  Consider the 
  3614. following examples: 
  3615.  
  3616.  
  3617.    char  Vowels1[6]  = "aeiouy"; 
  3618.    char  Vowels2[6]  = { 'a', 'e', 'i', 'o', 'u', 'y' }; 
  3619.    int  Numbers[10] = { 100, 10, 1 }; 
  3620.    float Blort[5]   = { 5.6, -2.2 }; 
  3621.  
  3622. The objects Vowels1 and Vowels2 are both arrays of six characters, and both 
  3623. contain exactly the same values in each of their corresponding elements.  The 
  3624. object Numbers is an array of 10 integers, the first three of which are 
  3625. initialized to 100, 10 and 1, and the remaining seven are set to zero.  The 
  3626. object Blort is an array of 5 floating-point numbers.  The first two elements 
  3627. are initialized to 5.6 and -2.2, and the remaining three are set to zero. 
  3628.  
  3629. If an array of more than one dimension is initialized, then each subarray may 
  3630. be initialized using a brace-enclosed list of values.  This form will work for 
  3631. an arbitrary number of dimensions.  Consider the following two-dimensional 
  3632. case: 
  3633.  
  3634.  
  3635.    int Box[3][4] = { { 11, 12, 13, 14 }, 
  3636.             { 21, 22, 23, 24 }, 
  3637.             { 31, 32, 33, 34 } }; 
  3638.  
  3639. The object Box is an array of 3 arrays of 4 integers.  There are three values 
  3640. in the initialization list, corresponding to the first dimension (3 rows). 
  3641. Each initialization value is itself a list of values corresponding to the 
  3642. second dimension (4 columns).  In other words, the first list of values { 11, 
  3643. 12, 13, 14 } is assigned to the first row of Box, the second list of values { 
  3644. 21, 22, 23, 24 } is assigned to the second row of Box, and the third list of 
  3645. values { 31, 32, 33, 34 } is assigned to the third row of Box. 
  3646.  
  3647. If all values are supplied for initializing an array, or if only elements from 
  3648. the end of the array are omitted, then the sub-levels need not be within 
  3649. braces.  For example, the following declaration of Box is the same as above: 
  3650.  
  3651.  
  3652.    int Box[3][4] = { 11, 12, 13, 14, 
  3653.             21, 22, 23, 24, 
  3654.             31, 32, 33, 34 }; 
  3655.  
  3656. The same rules about incomplete initialization lists apply to multi-dimensional 
  3657. arrays.  The following example defines a mathematical 3-by-3 identity matrix: 
  3658.  
  3659.  
  3660.    int Identity[3][3] = { { 1 }, 
  3661.               { 0, 1 }, 
  3662.               { 0, 0, 1 } }; 
  3663.  
  3664. The missing values are replaced with zeroes.  The initialization also could 
  3665. have been given as, 
  3666.  
  3667.  
  3668.    int Identity[3][3] = { { 1, 0, 0 }, 
  3669.               { 0, 1, 0 }, 
  3670.               { 0, 0, 1 } }; 
  3671.  
  3672. or as, 
  3673.  
  3674.  
  3675.    int Identity[3][3] = { 1, 0, 0, 
  3676.               0, 1, 0, 
  3677.               0, 0, 1 }; 
  3678.  
  3679.  
  3680. ΓòÉΓòÉΓòÉ 9.3. Initialization of Structures ΓòÉΓòÉΓòÉ
  3681.  
  3682.  
  3683. Structures may be initialized in a manner similar to arrays.  The initializer 
  3684. list must be specified within braces. 
  3685.  
  3686. For example, 
  3687.  
  3688.  
  3689.    struct printformat { 
  3690.      int   pagewid; 
  3691.      char   carr_ctl; 
  3692.      char *  buffer; 
  3693.    }; 
  3694.  
  3695.    char PrBuffer[256]; 
  3696.  
  3697.    struct printformat PrtFmt = { 80, ' ', PrBuffer }; 
  3698.  
  3699. Each value from the initializer list is assigned to each successive member of 
  3700. the structure.  Any unnamed gaps between members or at the end of the structure 
  3701. (caused by alignment) are ignored during initialization.  If there are more 
  3702. members of the structure than values specified by the initializer list, then 
  3703. the remaining members are initialized to zero (for arithmetic types) or the 
  3704. null pointer constant (for pointers). 
  3705.  
  3706. If a structure member is itself an array, structure or union, then the 
  3707. sub-members may be initialized using a brace-enclosed initializer list.  If 
  3708. braces are not specified, then for the purposes of initialization, the 
  3709. sub-members are treated as if they are members of the outer structure, as each 
  3710. subsequent initializer value initializes a sub-member, until no more 
  3711. sub-members are found, in which case the next member of the outer structure is 
  3712. initialized. 
  3713.  
  3714.  
  3715. ΓòÉΓòÉΓòÉ 9.4. Initialization of Unions ΓòÉΓòÉΓòÉ
  3716.  
  3717.  
  3718. Initializations of unions is the same as for structures, except that only the 
  3719. first member of the union may be initialized, using a brace-enclosed 
  3720. initializer. 
  3721.  
  3722. Consider the following example: 
  3723.  
  3724.  
  3725.    struct first3 { 
  3726.      char first, second, third; 
  3727.    }; 
  3728.  
  3729.    union ustr { 
  3730.      char      string[20]; 
  3731.      struct first3  firstthree; 
  3732.    }; 
  3733.    union ustr Str = { "Hello there" }; 
  3734.  
  3735. The object Str is declared to be a union of two types, the first of which is an 
  3736. array of 20 characters, and the second of which is a structure that allows 
  3737. direct access to the first three characters of the string contained in the 
  3738. array.  The array is initialized to the string "Hello there".  The three 
  3739. characters of struct first3 will have the characters 'H', 'e' and 'l'.  Had the 
  3740. declaration of ustr been, 
  3741.  
  3742.  
  3743.    union ustr { 
  3744.      struct first3  firstthree; 
  3745.      char      string[20]; 
  3746.    }; 
  3747.  
  3748. then the initialization could only set the first three characters. 
  3749.  
  3750.  
  3751. ΓòÉΓòÉΓòÉ 9.5. Uninitialized Objects ΓòÉΓòÉΓòÉ
  3752.  
  3753.  
  3754. An object with static storage duration, and no explicit initialization, will be 
  3755. initialized as if every member that has arithmetic type was assigned zero and 
  3756. every member that has a pointer type was assigned a null (zero) pointer. 
  3757.  
  3758. An object with automatic storage duration, and no explicit initialization, is 
  3759. not initialized.  Hence, a reference to such an automatic object that has not 
  3760. been assigned a value will yield undefined behavior.  On most systems, the 
  3761. value of the object will be arbitrary and unpredictable. 
  3762.  
  3763.  
  3764. ΓòÉΓòÉΓòÉ 10. Expressions ΓòÉΓòÉΓòÉ
  3765.  
  3766.  
  3767. An expression is a sequence of operators and operands that describes how to, 
  3768.  
  3769.      calculate a value (eg.  addition) 
  3770.      create side-effects (eg.  assignment, increment) 
  3771.  
  3772.  or both. 
  3773.  
  3774.  The order of execution of the expression is usually determined by a mixture 
  3775.  of, 
  3776.  
  3777.    1. parentheses (), which indicate to the compiler the desired grouping of 
  3778.       operations, 
  3779.  
  3780.    2. the precedence of operators, which describes the relative priority of 
  3781.       operators in the absence of parentheses, 
  3782.  
  3783.    3. the common algebraic ordering, 
  3784.  
  3785.    4. the associativity of operators. 
  3786.  
  3787.  In most other cases, the order of execution is determined by the compiler and 
  3788.  may not be relied upon.  Exceptions to this rule are described in the relevant 
  3789.  section.  Most users will find that the order of execution is well-defined and 
  3790.  intuitive.  However, when in doubt, use parentheses. 
  3791.  
  3792.  The table below summarizes the levels of precedence in expressions. 
  3793.  
  3794.  
  3795.     Expression Type     Operators 
  3796.     ----------------------  ------------------------------ 
  3797.     primary         identifier 
  3798.                 constant 
  3799.                 string 
  3800.                 (expression) 
  3801.     ----------------------  ------------------------------ 
  3802.     postfix         a[b] 
  3803.                 f() 
  3804.                 a.b   a->b 
  3805.                 a++   a-- 
  3806.     ----------------------  ------------------------------ 
  3807.     unary          sizeof u  sizeof( a ) 
  3808.                 ++a   --a 
  3809.                 &a    *a 
  3810.                 +a    -a    ~a    !a 
  3811.     ----------------------  ------------------------------ 
  3812.     cast           (type) a 
  3813.     ----------------------  ------------------------------ 
  3814.     multiplicative      a * b  a / b  a % b 
  3815.     ----------------------  ------------------------------ 
  3816.     additive         a + b  a - b 
  3817.     ----------------------  ------------------------------ 
  3818.     shift          a << b  a >> b 
  3819.     ----------------------  ------------------------------ 
  3820.     relational        a < b  a > b  a <= b  a >= b 
  3821.     ----------------------  ------------------------------ 
  3822.     equality         a == b  a != b 
  3823.     ----------------------  ------------------------------ 
  3824.     bitwise AND       a & b 
  3825.     ----------------------  ------------------------------ 
  3826.     bitwise exclusive OR   a ^ b 
  3827.     ----------------------  ------------------------------ 
  3828.     bitwise inclusive OR   a | b 
  3829.     ----------------------  ------------------------------ 
  3830.     logical AND       a && b 
  3831.     ----------------------  ------------------------------ 
  3832.     logical OR        a || b 
  3833.     ----------------------  ------------------------------ 
  3834.     conditional (*)     a ? b : c 
  3835.     ----------------------  ------------------------------ 
  3836.     assignment (*)      a = b 
  3837.                 a += b   a -= b 
  3838.                 a *= b   a /= b   a %= b 
  3839.                 a &= b   a ^= b   a |= b 
  3840.                 a <<= b   a >>= b 
  3841.     ----------------------  ------------------------------ 
  3842.     comma          a,b 
  3843.     ----------------------  ------------------------------ 
  3844.  
  3845.  (*) associates from right to left 
  3846.  
  3847.  Operations at a higher level in the table will occur before those below.  All 
  3848.  operators involving more than one operand associate from left to right, except 
  3849.  for the conditional and assignment operators, which associate from right to 
  3850.  left.  Operations at the same level, except where discussed in the relevant 
  3851.  section, may be executed in any order that the compiler chooses (subject to 
  3852.  the usual algebraic rules).  In particular, the compiler may regroup 
  3853.  sub-expressions that are both associative and commutative in order to improve 
  3854.  the efficiency of the code, provided the meaning (i.e.  types and results) of 
  3855.  the operands and result are not affected by the regrouping. 
  3856.  
  3857.  The order of any side-effects (for example, assignment, or action taken by a 
  3858.  function call) is also subject to alteration by the compiler. 
  3859.  
  3860.  An exception occurs when the operands for an operator are invalid.  For 
  3861.  example, division by zero may cause an exception.  If an exception occurs, the 
  3862.  behavior is undefined.  If an exception is a possibility, the program should 
  3863.  be prepared to handle it. 
  3864.  
  3865.  In the following sections, a formal syntax is used to describe each level in 
  3866.  the precedence table.  This syntax is used in order to completely describe the 
  3867.  relationships between the various levels. 
  3868.  
  3869.  
  3870. ΓòÉΓòÉΓòÉ 10.1. Lvalues ΓòÉΓòÉΓòÉ
  3871.  
  3872.  
  3873. In order to understand certain components of expressions, it is important to 
  3874. understand the term  lvalue. 
  3875.  
  3876. An lvalue is an expression that designates an object.  The simplest form of 
  3877. lvalue is an identifier which is an object (for example, an integer). 
  3878.  
  3879. The type of the expression may not be  void or a function.  The term lvalue is 
  3880. derived from left value, which refers to the fact that an lvalue is typically 
  3881. on the left side of an assignment expression. 
  3882.  
  3883. If ptr is a pointer to a type other than  void or a function, then both ptr and 
  3884. *ptr are lvalues. 
  3885.  
  3886. A  modifiable lvalue is an lvalue whose type is not an array or an incomplete 
  3887. type, whose declaration does not contain the keyword  const, and, if it is a 
  3888. structure or union, then none of its members contains the keyword  const. 
  3889.  
  3890.  
  3891. ΓòÉΓòÉΓòÉ 10.2. Primary Expressions ΓòÉΓòÉΓòÉ
  3892.  
  3893.  
  3894.  primary-expression: 
  3895.    identifier 
  3896.   or 
  3897.    constant 
  3898.  or 
  3899.    string-literal 
  3900.  or 
  3901.    ( expression ) 
  3902.  
  3903. A primary expression is the simplest part of an expression.  It consists of one 
  3904. of the following: 
  3905.  
  3906.  identifier 
  3907.       An identifier that designates a function is called a  function 
  3908.       designator.  An identifier that designates an object is an  lvalue. 
  3909.  
  3910.  constant 
  3911.       A constant is a primary expression whose type depends on its form.  See 
  3912.       "Constants". 
  3913.  
  3914.  string-literal 
  3915.       A string literal is a primary expression whose type is "array of  char". 
  3916.       A string literal is also an lvalue (but is not modifiable). 
  3917.  
  3918.  expression inside parentheses 
  3919.       The type and value of a parenthesized expression are the same as for the 
  3920.       expression without parentheses.  It may be an lvalue, function designator 
  3921.       or void expression. 
  3922.  
  3923.  Given these declarations, 
  3924.  
  3925.  
  3926.     int   count; 
  3927.     int *  ctrptr; 
  3928.     int   f( int ); 
  3929.     int   g( int ); 
  3930.  
  3931.  the following are all valid primary expressions: 
  3932.  
  3933.  
  3934.     count 
  3935.     3 
  3936.     3.2 
  3937.     'a' 
  3938.     "Hello there" 
  3939.     (count + 3) 
  3940.     (*(ctrptr+1)) 
  3941.     (f( ++i ) * g( j++ )) 
  3942.  
  3943.  
  3944. ΓòÉΓòÉΓòÉ 10.3. Postfix Operators ΓòÉΓòÉΓòÉ
  3945.  
  3946.  
  3947.  postfix-expression: 
  3948.    primary-expression 
  3949.  or 
  3950.    array-subscripting-expression 
  3951.  or 
  3952.    function-call-expression 
  3953.  or 
  3954.    member-designator-expression 
  3955.  or 
  3956.    post-increment-expression 
  3957.  or 
  3958.    post-decrement-expression 
  3959.  
  3960.  
  3961. ΓòÉΓòÉΓòÉ 10.3.1. Array Subscripting ΓòÉΓòÉΓòÉ
  3962.  
  3963.  
  3964.  array-subscripting-expression: 
  3965.   postfix-expression[expression] 
  3966.  
  3967. The general form for array subscripting is, 
  3968.  
  3969.  
  3970.    array[index] 
  3971.  
  3972. where array must have the type "array of type" or "pointer to type", and index 
  3973. must have an integral type.  The result has type "type". 
  3974.  
  3975. array[index] is equivalent to (*(array+index)), or the index-th element of the 
  3976. array array, where the first element is numbered zero.  Note that index is 
  3977. scaled automatically to account for the size of the elements of array. 
  3978.  
  3979. An alternate form for array subscripting is, 
  3980.  
  3981.  
  3982.    index[array] 
  3983.  
  3984. although this form is not commonly used. 
  3985.  
  3986.  
  3987. ΓòÉΓòÉΓòÉ 10.3.2. Function Calls ΓòÉΓòÉΓòÉ
  3988.  
  3989.  
  3990.  function-call-expression: 
  3991.   postfix-expression() 
  3992.  or 
  3993.  postfix-expression (argument-expression-list) 
  3994.  
  3995.  argument-expression-list: 
  3996.   one or more assignment-expressions separated by commas 
  3997.  
  3998. A postfix-expression followed by parentheses containing zero or more 
  3999. comma-separated expressions is a function-call-expression.  The 
  4000. postfix-expression denotes the function to be called, and must evaluate to a 
  4001. pointer to a function.  The simplest form of this expression is an identifier 
  4002. which is the name of a function.  For example, Fn() calls the function Fn. 
  4003.  
  4004. The expressions within the parentheses denote the arguments to the function. 
  4005. If a function prototype has been declared, then the number of arguments must 
  4006. match the parameter list in the prototype, and the arguments are converted to 
  4007. the types specified in the prototype. 
  4008.  
  4009. If the postfix-expression is simply an identifier, and no function prototype 
  4010. declaration for that identifier is in scope, then an implicit, 
  4011.  
  4012.  extern int identifier(); 
  4013.  
  4014. declaration is placed in the innermost block containing the function call. 
  4015. This declares the function as having external linkage, no information about its 
  4016. parameters is available, and the function returns an integer. 
  4017.  
  4018. The expressions are evaluated (in an undefined order) and the values assigned 
  4019. to the parameters for the function.  All arguments are passed by value, 
  4020. allowing the function to modify its parameters without affecting the arguments 
  4021. used to create the parameters.  However, an argument can be a pointer to an 
  4022. object, in which case the function may modify the object to which the pointer 
  4023. points. 
  4024.  
  4025. If a function prototype is in scope at both a call to a function and its 
  4026. definition (and if the prototypes are the same), then the compiler will ensure 
  4027. that the required number and type of parameters are present. 
  4028.  
  4029. If no function prototype is in scope at a call to a function, then the default 
  4030. argument promotions are performed.  (Integral types such as  char and  short 
  4031. int are converted to  int, while  float values are converted to  double.) When 
  4032. the function definition is encountered, if the parameter types do not match the 
  4033. default argument promotions, then the behavior is undefined.  (Usually, the 
  4034. parameters to the function will receive incorrect values.) 
  4035.  
  4036. If a function prototype has been declared at a call to a function, then each 
  4037. argument is converted, as if by assignment, to the type of the corresponding 
  4038. parameter.  When the function definition is encountered, if the types of the 
  4039. parameters do not match the types of the parameters in the function prototype, 
  4040. the behavior is undefined. 
  4041.  
  4042. If the ellipsis (,...) notation is used in a function prototype, then those 
  4043. arguments in a function call that correspond to the ellipsis have only the 
  4044. default argument promotions performed on them.  (See the chapter "Functions" 
  4045. for a complete description of the ellipsis notation.) 
  4046.  
  4047. Function calls may be recursive.  Functions may call themselves either 
  4048. directly, or via other functions. 
  4049.  
  4050. The following are some examples of function calls: 
  4051.  
  4052.  
  4053.    putchar( 'x' ); 
  4054.    chr = getchar(); 
  4055.    valid = isdigit( chr ); 
  4056.    printf( "chr = %c, valid = %2x\n", chr, valid ); 
  4057.    fnptr = &MyFunction; 
  4058.    (*fnptr)( parm1, parm2 ); 
  4059.    fnptr( parm1, parm2 ); 
  4060.  
  4061.  
  4062. ΓòÉΓòÉΓòÉ 10.3.3. Structure and Union Members ΓòÉΓòÉΓòÉ
  4063.  
  4064.  
  4065.  member-designator-expression: 
  4066.    postfix-expression . identifier 
  4067.  or 
  4068.    postfix-expression->identifier 
  4069.  
  4070. The first operand of the .  operator must be an object with a structure or 
  4071. union type.  The second operand must be the name of a member of that type.  The 
  4072. result is the value of the member, and is an lvalue if the first operand is 
  4073. also an lvalue. 
  4074.  
  4075. The first operand of the -> operator must be a pointer to an object with a 
  4076. structure or union type.  The second operand must be the name of a member of 
  4077. that type.  The result is the value of the member of the structure or union to 
  4078. which the first expression points, and is an lvalue. 
  4079.  
  4080.  
  4081. ΓòÉΓòÉΓòÉ 10.3.4. Post-Increment and Post-Decrement ΓòÉΓòÉΓòÉ
  4082.  
  4083.  
  4084.  post-increment-expression: 
  4085.    postfix-expression++ 
  4086.  
  4087.  post-decrement-expression: 
  4088.    postfix-expression-- 
  4089.  
  4090. The operand of post-increment and post-decrement must be a modifiable lvalue, 
  4091. and a scalar (not a structure, union or array). 
  4092.  
  4093. The effect of the operation is that the operand is incremented or decremented 
  4094. by 1, adjusted for the type of the operand.  For example, if the operand is 
  4095. declared to be a "pointer to type", then the increment or decrement will be by 
  4096. the value sizeof( type ). 
  4097.  
  4098. The result of both post-increment and post-decrement (if it is just a 
  4099. subexpression of a larger expression) is the original, unmodified value of the 
  4100. operand.  In other words, the original value of the operand is used in the 
  4101. expression, and then it is incremented or decremented.  Whether the operand is 
  4102. incremented immediately after use or after completion of execution of the 
  4103. expression is undefined.  Consider the statements, 
  4104.  
  4105.  
  4106.    int i = 2; 
  4107.    int j; 
  4108.  
  4109.    j = (i++) + (i++); 
  4110.  
  4111. Depending on the compiler, j may get the value 4 or 5.  If the increments are 
  4112. delayed until after the expression is evaluated, j gets the value 2 + 2.  If 
  4113. the increment of i happens immediately after its value is retrieved, then j 
  4114. gets the value 2 + 3. 
  4115.  
  4116. To avoid ambiguity, the above expression could be written as: 
  4117.  
  4118.  
  4119.    j = i + i; 
  4120.    i += 2; 
  4121.  
  4122.  
  4123. ΓòÉΓòÉΓòÉ 10.4. Unary Operators ΓòÉΓòÉΓòÉ
  4124.  
  4125.  
  4126.  unary-expression: 
  4127.    postfix-expression 
  4128.  or 
  4129.    pre-increment-expression 
  4130.  or 
  4131.    pre-decrement-expression 
  4132.  or 
  4133.    unary-operator cast-expression 
  4134.  or 
  4135.    sizeof-expression 
  4136.  
  4137.  unary-operator:  one of 
  4138.  & * + - ~ ! 
  4139.  
  4140.  
  4141. ΓòÉΓòÉΓòÉ 10.4.1. Pre-Increment and Pre-Decrement Operators ΓòÉΓòÉΓòÉ
  4142.  
  4143.  
  4144.  pre-increment-expression: 
  4145.    ++ unary-expression 
  4146.  
  4147.  pre-decrement-expression: 
  4148.    -- unary-expression 
  4149.  
  4150. The operand of the pre-increment and pre-decrement operators must be a 
  4151. modifiable lvalue, and a scalar (not a structure, union or array). 
  4152.  
  4153. The operand is incremented or decremented by 1, adjusted for the type of the 
  4154. operand.  For example, if the operand is declared to be a "pointer to type", 
  4155. then the increment or decrement will be by the value sizeof( type ). 
  4156.  
  4157. The expression ++obj is equivalent to (obj += 1), while --obj is equivalent to 
  4158. (obj -= 1). 
  4159.  
  4160.  
  4161. ΓòÉΓòÉΓòÉ 10.4.2. Address-of and Indirection Operators ΓòÉΓòÉΓòÉ
  4162.  
  4163.  
  4164.  unary-expression: 
  4165.    & cast-expression 
  4166.  or 
  4167.    * cast-expression 
  4168.  
  4169. The unary & symbol denotes the  address-of operator.  Its operand must 
  4170. designate a function or an array, or be an lvalue that designates an object 
  4171. that is not a bit-field and is not declared with the  register storage-class 
  4172. specifier.  If the type of the operand is "type", then the type of the result 
  4173. is "pointer to type" and the result is the address of the operand. 
  4174.  
  4175. If the type of the operand is "array of type", then the type of the result is 
  4176. "pointer to type" and the result is the address of the first element of the 
  4177. array. 
  4178.  
  4179. The * symbol, in its unary form, denotes the  indirection or  pointer operator. 
  4180. Its operand must be a pointer type, except that it may not be a pointer to 
  4181. void.  If the operand is a "pointer to type", then the type of the result is 
  4182. "type", and the result is the object to which the operand points. 
  4183.  
  4184. No checking is performed to ensure that the value of the pointer is valid.  If 
  4185. an invalid pointer value is used, the behavior of * is undefined. 
  4186.  
  4187. Examples: 
  4188.  
  4189.    int  counter; 
  4190.    int * ctrptr; 
  4191.    void  (*fnptr)( int, int * ); 
  4192.  
  4193.    ctrptr  = &counter; 
  4194.    *ctrptr = 3; 
  4195.  
  4196.    fnptr = FnRetVoid; 
  4197.    fnptr( *ctrptr, &counter ); 
  4198.  
  4199.  
  4200. ΓòÉΓòÉΓòÉ 10.4.3. Unary Arithmetic Operators ΓòÉΓòÉΓòÉ
  4201.  
  4202.  
  4203.  unary-expression: 
  4204.    + cast-expression 
  4205.  or 
  4206.    - cast-expression 
  4207.  or 
  4208.    ~ cast-expression 
  4209.  or 
  4210.    ! cast-expression 
  4211.  
  4212. The + symbol, in its unary form, simply returns the value of its operand.  The 
  4213. type of its operand must be an arithmetic type (character, integer or 
  4214. floating-point).  Integral promotion is performed on the operand, and the 
  4215. result has the promoted type. 
  4216.  
  4217. The - symbol, in its unary form, is the negation or  negative operator.  The 
  4218. type of its operand must be an arithmetic type (character, integer or 
  4219. floating-point).  The result is the negative of the operand.  Integral 
  4220. promotion is performed on the operand, and the result has the promoted type. 
  4221. The expression -obj is equivalent to (0-obj). 
  4222.  
  4223. The ~ symbol is the  bitwise complement, 1's complement or bitwise not 
  4224. operator.  The type of the operand must be an integral type, and integral 
  4225. promotion is performed on the operand.  The type of the result is the type of 
  4226. the promoted operand.  Each bit of the result is the complement of the 
  4227. corresponding bit in the operand, effectively turning 0 bits to 1, and 1 bits 
  4228. to 0. 
  4229.  
  4230. The !  symbol is the  logical not operator.  Its operand must be a scalar type 
  4231. (not a structure, union or array).  The result type is  int.  If the operand 
  4232. has the value zero, then the result value is 1.  If the operand has some other 
  4233. value, then the result is 0. 
  4234.  
  4235.  
  4236. ΓòÉΓòÉΓòÉ 10.4.4. The sizeof Operator ΓòÉΓòÉΓòÉ
  4237.  
  4238.  
  4239.  sizeof-expression: 
  4240.   sizeof unary-expression 
  4241.  or 
  4242.  sizeof( type-name ) 
  4243.  
  4244. The  sizeof operator gives the size (in bytes) of its operand.  The operand may 
  4245. be an expression, or a type in parentheses.  In either case, the type must not 
  4246. be a function, bit-field or incomplete type (such as  void, or an array that 
  4247. has not had its length declared). 
  4248.  
  4249. Note that an expression operand to  sizeof is not evaluated.  The expression is 
  4250. examined to determine the result type, from which the size is determined. 
  4251.  
  4252. If the operand has a character type, then the result is 1. 
  4253.  
  4254. If the type is a structure or union, then the result is the total number of 
  4255. bytes in the structure or union, including any internal or trailing padding 
  4256. included by the compiler for alignment purposes.  The size of a structure can 
  4257. be greater than the sum of the sizes of its members. 
  4258.  
  4259. If the type is an array, then the result is the total number of bytes in the 
  4260. array, unless the operand is a parameter in the function definition enclosing 
  4261. the current block, in which case the result is the size of a pointer. 
  4262.  
  4263. The type of the result of the  sizeof operator is implementation-defined, but 
  4264. it is an unsigned integer type, and is represented by  size_t in the 
  4265. <stddef.h> header. 
  4266.  
  4267.  For the Watcom C/16 and C/32 compilers, the macro  size_t is  unsigned int. 
  4268.  
  4269. Example: 
  4270.  
  4271.    struct s { 
  4272.      struct s * next; 
  4273.      int     obj1; 
  4274.      int     obj2; 
  4275.    }; 
  4276.  
  4277.  
  4278.    static struct s * SAllocAndFill( const struct s * def_s ) 
  4279.    /*******************************************************/ 
  4280.    { 
  4281.      struct s * sptr; 
  4282.  
  4283.      sptr = malloc( sizeof( struct s ) ); 
  4284.      if( sptr != NULL ) { 
  4285.        memcpy( sptr, def_s, sizeof( struct s ) ); 
  4286.      } 
  4287.      return( sptr ); 
  4288.    } 
  4289.  
  4290. The function SAllocAndFill receives a pointer to a struct s.  It allocates such 
  4291. a structure, and copies the contents of the structure pointed to by def_s into 
  4292. the allocated memory.  A pointer to the allocated structure is returned. 
  4293.  
  4294. The library function  malloc takes the number of bytes to allocate as a 
  4295. parameter and sizeof( struct s ) provides that value.  The library function 
  4296. memcpy also takes, as the third parameter, the number of bytes to copy and 
  4297. again sizeof( struct s ) provides that value. 
  4298.  
  4299.  
  4300. ΓòÉΓòÉΓòÉ 10.5. Cast Operator ΓòÉΓòÉΓòÉ
  4301.  
  4302.  
  4303.  cast-expression: 
  4304.    unary-expression 
  4305.  or 
  4306.    ( type-name ) cast-expression 
  4307.  
  4308. When an expression is preceded by a type name in parentheses, the value of the 
  4309. expression is converted to the named type.  This is called a  cast.  Both the 
  4310. type name and the operand type must be scalar (not a structure, union or 
  4311. array), unless the type name is  void.  If the type name is  void, the operand 
  4312. type must be a complete type (not an array of unknown size, or a structure or 
  4313. union that has not yet been defined). 
  4314.  
  4315. A cast does not yield an lvalue. 
  4316.  
  4317. Pointers may be freely converted from "pointer to  void" to any other pointer 
  4318. type without using an explicit cast operator.  Pointers also may be converted 
  4319. from any pointer type to "pointer to  void". 
  4320.  
  4321. A pointer may be converted to a pointer to another type.  However, the pointer 
  4322. may be invalid if the resulting pointer is not properly aligned for the type. 
  4323. Converting a pointer to a pointer to a type with less strict alignment, and 
  4324. back again, will yield the same pointer.  However, converting it to a pointer 
  4325. to a type with more strict alignment, and back again, may yield a different 
  4326. pointer.  On many computers, where alignment is not required (but may improve 
  4327. performance), conversion of pointers may take place freely. 
  4328.  
  4329.  With Watcom C/16 and C/32, alignment of integers, pointers and floating-point 
  4330. numbers is not required, so the compiler does not do any alignment.  However, 
  4331. aligning these types may make a program run slightly faster. 
  4332.  
  4333.  A command line switch may be used to force the compiler to do alignment on all 
  4334. structures. 
  4335.  
  4336. A pointer to a function may be converted to a pointer to a different type of 
  4337. function, and back again.  The resulting pointer will be the same as the 
  4338. original pointer. 
  4339.  
  4340. If a pointer is converted to a pointer to a different type of function, and a 
  4341. call is made using that pointer, the behavior is undefined. 
  4342.  
  4343. A pointer may be converted to an integral type.  The type of integer required 
  4344. to hold the value of the pointer is implementation-defined.  If the integer is 
  4345. not large enough to fully contain the value, then the behavior is undefined. 
  4346.  
  4347. An integer may be converted to a pointer.  The result is 
  4348. implementation-defined. 
  4349.  
  4350.  With Watcom C/16, for the purposes of conversion between pointers and 
  4351. integers,  __near pointers are treated as  unsigned int.  __far and  __huge 
  4352. pointers are treated as  unsigned long int, with the pointer's segment value in 
  4353. the high-order (most significant) two bytes.  All the usual integer conversion 
  4354. rules then apply.  Note that huge pointers are not normalized in any way. 
  4355.  
  4356.  With Watcom C/32, for the purposes of conversion between pointers and 
  4357. integers,  __near pointers are treated as  unsigned int.  __far16 and  _Seg16 
  4358. pointers are also treated as  unsigned int, with the pointer's segment value in 
  4359. the high-order (most significant) two bytes.  All the usual integer conversion 
  4360. rules then apply.  Note that  __far pointers may not be converted to an integer 
  4361. without losing the segment information. 
  4362.  
  4363.  
  4364. ΓòÉΓòÉΓòÉ 10.6. Multiplicative Operators ΓòÉΓòÉΓòÉ
  4365.  
  4366.  
  4367.  multiplicative-expression: 
  4368.   cast-expression 
  4369.  or 
  4370.  multiplicative-expression * cast-expression 
  4371.  or 
  4372.  multiplicative-expression / cast-expression 
  4373.  or 
  4374.  multiplicative-expression % cast-expression 
  4375.  
  4376. The * symbol, in its binary form, yields the  product of its operands.  The 
  4377. operands must have arithmetic type, and have the usual arithmetic conversions 
  4378. performed on them. 
  4379.  
  4380. The / symbol yields the  quotient from the division of the first operand by the 
  4381. second operand.  The operands must have arithmetic type, and have the usual 
  4382. arithmetic conversions performed on them.  Note that when a division by zero 
  4383. occurs, the behavior is undefined. 
  4384.  
  4385. When both operands of / are of integer type and positive value, and the 
  4386. division is inexact, the result is the largest integer less than the algebraic 
  4387. (exact) quotient.  (The result is rounded down.) 
  4388.  
  4389. When one or both operands of / is negative and the division is inexact, whether 
  4390. the compiler rounds the value up or down is implementation-defined. 
  4391.  
  4392.  The Watcom C/16 and C/32 compilers always round the result of integer division 
  4393. toward zero.  This action is also called truncation. 
  4394.  
  4395. The % symbol yields the  remainder from the division of the first operand by 
  4396. the second operand.  The operands of % must have integral type. 
  4397.  
  4398. When both operands of % are positive, the result is a positive value smaller 
  4399. than the second operand.  When one or both operands is negative, whether the 
  4400. result is positive or negative is implementation-defined. 
  4401.  
  4402.  With the Watcom C/16 and C/32 compiler, the remainder has the same sign as the 
  4403. first operand. 
  4404.  
  4405. For integral types a and b, if b is not zero, then (a/b)*b + a%b will equal a. 
  4406.  
  4407.  
  4408. ΓòÉΓòÉΓòÉ 10.7. Additive Operators ΓòÉΓòÉΓòÉ
  4409.  
  4410.  
  4411.  additive-expression: 
  4412.    multiplicative-expression 
  4413.  or 
  4414.    additive-expression + multiplicative-expression 
  4415.  or 
  4416.    additive-expression - multiplicative-expression 
  4417.  
  4418. The + symbol, in its binary form, denotes the  sum of its operands. 
  4419.  
  4420. If both operands have arithmetic type, then the usual arithmetic conversions 
  4421. are performed on them. 
  4422.  
  4423. If one of the operands is a pointer, then the other operand must have an 
  4424. integral type.  The pointer operand may not be a pointer to  void.  Before 
  4425. being added to the pointer value, the integral value is multiplied by the size 
  4426. of the object to which the pointer points.  The result type is the same as the 
  4427. pointer operand type.  If the pointer value is a pointer to a member of an 
  4428. array, then the resulting pointer will point to a member of the same array, 
  4429. provided the array is large enough.  If the resulting pointer does not point to 
  4430. a member of the array, then its use with the unary * (indirection) or -> 
  4431. (arrow) operator will yield undefined behavior. 
  4432.  
  4433. The - symbol, in its binary form, denotes the  difference resulting from the 
  4434. subtraction of the second operand from the first.  If both operands have 
  4435. arithmetic type, then the usual arithmetic conversions are performed on them. 
  4436.  
  4437. If the first operand is a pointer, then the second operand must either be a 
  4438. pointer to the same type or an integral type. 
  4439.  
  4440. In the same manner as for adding a pointer and an integral value, the integral 
  4441. value is multiplied by the size of the object to which the pointer points.  The 
  4442. pointer operand may not be a pointer to  void.  The result type is the same 
  4443. type as the pointer operand. 
  4444.  
  4445. If both operands are pointers to the same type, the difference is divided by 
  4446. the size of the type, representing the difference of the subscripts of the two 
  4447. array members (assuming the type is "array of type").  The type of the result 
  4448. is implementation-defined, and is represented by  ptrdiff_t (a signed integral 
  4449. type) defined in the  <stddef.h> header. 
  4450.  
  4451.  With Watcom C/16 and C/32,  ptrdiff_t is  int, unless the huge memory model is 
  4452. being used, in which case  ptrdiff_t is  long int. 
  4453.  
  4454.  
  4455. ΓòÉΓòÉΓòÉ 10.8. Bitwise Shift Operators ΓòÉΓòÉΓòÉ
  4456.  
  4457.  
  4458.  shift-expression: 
  4459.   additive-expression 
  4460.  or 
  4461.  shift-expression << additive-expression 
  4462.  or 
  4463.  shift-expression >> additive-expression 
  4464.  
  4465. The << symbol denotes the  left-shift operator.  Both operands must have an 
  4466. integral type, and the integral promotions are performed on them.  The type of 
  4467. the result is the type of the promoted left operand. 
  4468.  
  4469. The result of op << amt is op left-shifted amt bit positions.  Zero bits are 
  4470. filled on the right.  Effectively, the high bits shifted out of op are 
  4471. discarded, and the resulting set of bits is re-interpreted as the result. 
  4472. Another interpretation is that op is multiplied by 2 raised to the power amt. 
  4473.  
  4474. The >> symbol denotes the  right-shift operator.  Both operands must have an 
  4475. integral type, and the integral promotions are performed on them.  The type of 
  4476. the result is the type of the promoted left operand. 
  4477.  
  4478. The result of op >> amt is op right-shifted amt bit positions.  If op has an 
  4479. unsigned type, or a signed type and a non-negative value, then op is divided by 
  4480. 2 raised to the power amt.  Effectively, the low bits shifted out of op are 
  4481. discarded, zero bits are filled on the left, and the resulting set of bits is 
  4482. re-interpreted as the result. 
  4483.  
  4484. If op has a signed type and negative value, then the behavior of op >> amt is 
  4485. implementation-defined.  Usually, the high bits vacated by the right shift are 
  4486. filled with the sign bit from before the shift (arithmetic right shift), or 
  4487. with 0 (logical right shift). 
  4488.  
  4489.  With Watcom C/16 and C/32, a right shift of a negative value of a signed type 
  4490. causes the sign bit to be propogated throughout the bits vacated by the shift. 
  4491. Essentially, the vacated bits are filled with 1 bits. 
  4492.  
  4493. For both bitwise shift operators, if the number of bits to shift exceeds the 
  4494. number of bits in the type, the result is undefined. 
  4495.  
  4496.  
  4497. ΓòÉΓòÉΓòÉ 10.9. Relational Operators ΓòÉΓòÉΓòÉ
  4498.  
  4499.  
  4500.  relational-expression: 
  4501.   shift-expression 
  4502.  or 
  4503.  relational-expression < shift-expression 
  4504.  or 
  4505.  relational-expression > shift-expression 
  4506.  or 
  4507.  relational-expression <= shift-expression 
  4508.  or 
  4509.  relational-expression >= shift-expression 
  4510.  
  4511. Each of the symbols  < (less than),  > (greater than),  <= (less than or equal 
  4512. to),  >= (greater than or equal to), yields the value 1 if the relation is 
  4513. true, and 0 if the relation is false.  The result type is  int. 
  4514.  
  4515. If both operands have arithmetic type, then the usual arithmetic conversions 
  4516. are performed on them. 
  4517.  
  4518. If one of the operands is a pointer, then the other operand must be a pointer 
  4519. to a compatible type.  The result depends on where (in the address space of the 
  4520. computer) the pointers actually point. 
  4521.  
  4522. If both pointers point to members of the same array object, then the pointer 
  4523. that points to the member with a higher subscript will be greater than the 
  4524. other pointer. 
  4525.  
  4526. If both pointers point to different members within the same structure, then the 
  4527. pointer pointing to the member declared later in the structure will be greater 
  4528. than the other pointer. 
  4529.  
  4530. If both pointers point to the same union object, then they will be equal. 
  4531.  
  4532. All other comparisons yield undefined behavior.  As discussed above, the 
  4533. relationship between pointers is determined by the locations in the machine 
  4534. storage that the pointers reference.  Typically, the numeric values of the 
  4535. pointer operands are compared. 
  4536.  
  4537.  
  4538. ΓòÉΓòÉΓòÉ 10.10. Equality Operators ΓòÉΓòÉΓòÉ
  4539.  
  4540.  
  4541.  equality-expression: 
  4542.   relational-expression 
  4543.  or 
  4544.  equality-expression == relational-expression 
  4545.  or 
  4546.  equality-expression != relational-expression 
  4547.  
  4548. The symbols ==  (equal to) and !=  (not equal to) yield the value 1 if the 
  4549. relation is true, and 0 if the relation is false.  The result type is  int. 
  4550.  
  4551. If both operands have arithmetic type, then the usual arithmetic conversions 
  4552. are performed on them. 
  4553.  
  4554. If both operands are pointers to the same type and they compare equal, then 
  4555. they are pointers to the same object. 
  4556.  
  4557. If both operands are pointers and one is a pointer to  void, then the other is 
  4558. converted to a pointer to  void. 
  4559.  
  4560. If one of the operands is a pointer, the other may be a null pointer constant 
  4561. (zero). 
  4562.  
  4563. No other combinations are valid. 
  4564.  
  4565.  
  4566. ΓòÉΓòÉΓòÉ 10.11. Bitwise AND Operator ΓòÉΓòÉΓòÉ
  4567.  
  4568.  
  4569.  and-expression: 
  4570.   equality-expression 
  4571.  or 
  4572.  and-expression & equality-expression 
  4573.  
  4574. The & symbol, in its binary form, denotes the  bitwise AND operator.  Each of 
  4575. the operands must have integral type, and the usual arithmetic conversions are 
  4576. performed. 
  4577.  
  4578. The result is the bitwise AND of the two operands.  That is, the bit in the 
  4579. result is set if and only if each of the corresponding bits in the operands are 
  4580. set. 
  4581.  
  4582. The following table illustrates some bitwise AND operations: 
  4583.  
  4584.  
  4585.    Operation     Result 
  4586.    ---------------  ------ 
  4587.    0x0000 & 0x7A4C  0x0000 
  4588.    0xFFFF & 0x7A4C  0x7A4C 
  4589.    0x1001 & 0x0001  0x0001 
  4590.    0x29F4 & 0xE372  0x2170 
  4591.  
  4592.  
  4593. ΓòÉΓòÉΓòÉ 10.12. Bitwise Exclusive OR Operator ΓòÉΓòÉΓòÉ
  4594.  
  4595.  
  4596.  exclusive-or-expression: 
  4597.   and-expression 
  4598.  or 
  4599.  exclusive-or-expression ^ and-expression 
  4600.  
  4601. The ^ symbol denotes the  bitwise exclusive OR operator.  Each of the operands 
  4602. must have integral type, and the usual arithmetic conversions are performed. 
  4603.  
  4604. The result is the bitwise exclusive OR of the two operands.  That is, the bit 
  4605. in the result is set if and only if exactly one of the corresponding bits in 
  4606. the operands is set. 
  4607.  
  4608. Another interpretation is that, if one of the operands is treated as a mask, 
  4609. then every 1 bit in the mask causes the corresponding bit in the other operand 
  4610. to be complemented (0 becomes 1, 1 becomes 0) before being placed in the 
  4611. result, while every 0 bit in the mask causes the corresponding bit in the other 
  4612. operand to be placed unchanged in the result. 
  4613.  
  4614. The following table illustrates some exclusive OR operations: 
  4615.  
  4616.  
  4617.    Operation     Result 
  4618.    ---------------  ------ 
  4619.    0x0000 ^ 0x7A4C  0x7A4C 
  4620.    0xFFFF ^ 0x7A4C  0x85B3 
  4621.    0xFFFF ^ 0x85B3  0x7A4C 
  4622.    0x1001 ^ 0x0001  0x1000 
  4623.    0x29F4 ^ 0xE372  0xCA86 
  4624.  
  4625.  
  4626. ΓòÉΓòÉΓòÉ 10.13. Bitwise Inclusive OR Operator ΓòÉΓòÉΓòÉ
  4627.  
  4628.  
  4629.  inclusive-or-expression: 
  4630.   exclusive-or-expression 
  4631.  or 
  4632.  inclusive-or-expression | exclusive-or-expression 
  4633.  
  4634. The | symbol denotes the  bitwise inclusive OR operator.  Each of the operands 
  4635. must have integral type, and the usual arithmetic conversions are performed. 
  4636.  
  4637. The result is the bitwise inclusive OR of the two operands.  That is, the bit 
  4638. in the result is set if at least one of the corresponding bits in the operands 
  4639. is set. 
  4640.  
  4641. The following table illustrates some inclusive OR operations: 
  4642.  
  4643.  
  4644.    Operation     Result 
  4645.    ---------------  ------ 
  4646.    0x0000 | 0x7A4C  0x7A4C 
  4647.    0xFFFF | 0x7A4C  0xFFFF 
  4648.    0x1100 | 0x0022  0x1122 
  4649.    0x29F4 | 0xE372  0xEBF6 
  4650.  
  4651.  
  4652. ΓòÉΓòÉΓòÉ 10.14. Logical AND Operator ΓòÉΓòÉΓòÉ
  4653.  
  4654.  
  4655.  logical-and-expression: 
  4656.   inclusive-or-expression 
  4657.  or 
  4658.  logical-and-expression && inclusive-or-expression 
  4659.  
  4660. The && symbol denotes the  logical AND operator.  Each of the operands must 
  4661. have scalar type. 
  4662.  
  4663. If both of the operands are not equal to zero, then the result is 1. 
  4664. Otherwise, the result is zero.  The result type is  int. 
  4665.  
  4666. If the first operand is zero, then the second operand is not evaluated.  Any 
  4667. side effects that would have happened if the second operand had been executed 
  4668. do not happen.  Any function calls encountered in the second operand do not 
  4669. take place. 
  4670.  
  4671.  
  4672. ΓòÉΓòÉΓòÉ 10.15. Logical OR Operator ΓòÉΓòÉΓòÉ
  4673.  
  4674.  
  4675.  logical-or-expression: 
  4676.   logical-and-expression 
  4677.  or 
  4678.  logical-or-expression || logical-and-expression 
  4679.  
  4680. The || symbol denotes the  logical OR operator.  Each of the operands must have 
  4681. scalar type. 
  4682.  
  4683. If one or both of the operands is not equal to zero, then the result is 1. 
  4684. Otherwise, the result is zero (both operands are zero).  The result type is 
  4685. int. 
  4686.  
  4687. If the first operand is not zero, then the second operand is not evaluated. 
  4688. Any side effects that would have happened if the second operand had been 
  4689. executed do not happen.  Any function calls encountered in the second operand 
  4690. do not take place. 
  4691.  
  4692.  
  4693. ΓòÉΓòÉΓòÉ 10.16. Conditional Operator ΓòÉΓòÉΓòÉ
  4694.  
  4695.  
  4696.  conditional-expression: 
  4697.   logical-or-expression 
  4698.  or 
  4699.  logical-or-expression ?  expression :  conditional-expression 
  4700.  
  4701. The ?  symbol separates the first two parts of a  conditional operator, and the 
  4702. :  symbol separates the second and third parts.  The first operand must have a 
  4703. scalar type (not a structure, union or array). 
  4704.  
  4705. The first operand is evaluated.  If its value is not equal to zero, then the 
  4706. second operand is evaluated and its value is the result.  Otherwise, the third 
  4707. operand is evaluated and its value is the result. 
  4708.  
  4709. Whichever operand is evaluated, the other is not evaluated.  Any side effects 
  4710. that might have happened during the evaluation of the other operand do not 
  4711. happen. 
  4712.  
  4713. If both the second and third operands have arithmetic type, then the usual 
  4714. arithmetic conversions are performed on them, and the type of the result is the 
  4715. same type as the converted operands. 
  4716.  
  4717. If both operands have the same structure, union or pointer type, then the 
  4718. result has that type. 
  4719.  
  4720. If both operands are pointers, and one is "pointer to  void", then the result 
  4721. type is "pointer to  void". 
  4722.  
  4723. If one operand is a pointer, and the other is a null pointer constant (0), the 
  4724. result type is that of the pointer. 
  4725.  
  4726. If both operands are void expressions, then the result is a void expression. 
  4727.  
  4728. No other combinations of result types are permitted. 
  4729.  
  4730. Note that, unlike most other operators, the conditional operator associates 
  4731. from right to left.  For example, the expression, 
  4732.  
  4733.  
  4734.    a = b ? c : d ? e : f; 
  4735.  
  4736. is translated as if it had been parenthesized as follows: 
  4737.  
  4738.  
  4739.    a = b ? c : (d ? e : f); 
  4740.  
  4741. This construct is confusing, and so should probably be avoided. 
  4742.  
  4743.  
  4744. ΓòÉΓòÉΓòÉ 10.17. Assignment Operators ΓòÉΓòÉΓòÉ
  4745.  
  4746.  
  4747.  assignment-expression: 
  4748.    conditional-expression 
  4749.  or 
  4750.    simple-assignment-expression 
  4751.  or 
  4752.    compound-assignment-expression 
  4753.  
  4754. An  assignment operator stores a value in the object designated by the left 
  4755. operand.  The left operand must be a modifiable lvalue. 
  4756.  
  4757. The result type and value are those of the left operand after the assignment. 
  4758.  
  4759. Whether the left or right operand is evaluated first is undefined. 
  4760.  
  4761. Note that, unlike most other operators, the assignment operators associate from 
  4762. right to left.  For example, the expression, 
  4763.  
  4764.  
  4765.    a += b = c; 
  4766.  
  4767. is translated as if it had been bracketed as follows: 
  4768.  
  4769.  
  4770.    a += (b = c); 
  4771.  
  4772.  
  4773. ΓòÉΓòÉΓòÉ 10.17.1. Simple Assignment ΓòÉΓòÉΓòÉ
  4774.  
  4775.  
  4776.  simple-assignment-operator: 
  4777.    unary-expression = assignment-expression 
  4778.  
  4779. The = symbol denotes  simple assignment.  The value of the right operand is 
  4780. converted to the type of the left operand and replaces the value designated by 
  4781. the left operand. 
  4782.  
  4783. The two operands must obey one of the following rules, 
  4784.  
  4785.      both have arithmetic types, 
  4786.      both have the same structure or union type, or the right operand differs 
  4787.       only in the presence of the  const or  volatile keywords, 
  4788.      both are pointers to the same type, 
  4789.      both are pointers and one is a pointer to  void, 
  4790.      the left operand is a pointer, and the right is a null pointer constant 
  4791.       (0). 
  4792.  
  4793.  
  4794. ΓòÉΓòÉΓòÉ 10.17.2. Compound Assignment ΓòÉΓòÉΓòÉ
  4795.  
  4796.  
  4797.  compound-assignment-expression: 
  4798.   unary-expression assignment-operator assignment-expression 
  4799.  
  4800.  assignment-operator:  one of 
  4801.  += -= 
  4802.  *= /= %= 
  4803.  &= ^= |= 
  4804.  <<= >>= 
  4805.  
  4806. A  compound assignment operator of the form a op= b is equivalent to the simple 
  4807. assignment expression a = a op (b), except that the left operand a is evaluated 
  4808. only once. 
  4809.  
  4810. The compound assignment operator must have operands consistent with those 
  4811. allowed by the corresponding binary operator. 
  4812.  
  4813.  
  4814. ΓòÉΓòÉΓòÉ 10.18. Comma Operator ΓòÉΓòÉΓòÉ
  4815.  
  4816.  
  4817.  expression: 
  4818.    assignment-expression 
  4819.  or 
  4820.    expression, assignment-expression 
  4821.  
  4822. At the lowest precedence, the  comma operator evaluates the left operand as a 
  4823. void expression (it is evaluated and its result, if any, is discarded), and 
  4824. then evaluates the right operand.  The result has the type and value of the 
  4825. second operand. 
  4826.  
  4827. In contexts where the comma is also used as a separator (function argument 
  4828. lists and initializer lists), a comma expression must be placed in parentheses. 
  4829.  
  4830. For example, 
  4831.  
  4832.    Fn( (pi=3.14159,two_pi=2*pi) ); 
  4833.  
  4834. the function Fn has one parameter, which has the value 2 times pi. 
  4835.  
  4836.  for( i = 0, j = 0, k = 0;; i++, j++, k++ ) 
  4837.    statement; 
  4838.  
  4839. The  for statement allows three expressions.  In this example, the first 
  4840. expression initializes three objects and the third expression increments the 
  4841. three objects. 
  4842.  
  4843.  
  4844. ΓòÉΓòÉΓòÉ 10.19. Constant Expressions ΓòÉΓòÉΓòÉ
  4845.  
  4846.  
  4847. A constant expression may be specified in several places: 
  4848.  
  4849.      the size of a bit-field member of a structure, 
  4850.      the value of an enumeration constant, 
  4851.      an initializer list, 
  4852.      the number of elements in an array, 
  4853.      the value of a  case label constant, 
  4854.      with the  #if and  #elif preprocessor directives. 
  4855.  
  4856.  In most cases, a constant expression consists of a series of constant values 
  4857.  and operations that evaluate to a constant value.  Certain operations may only 
  4858.  appear within the operand of the  sizeof operator.  These include: 
  4859.  
  4860.      a function call, 
  4861.      pre- or post-increment or decrement, 
  4862.      assignment, 
  4863.      comma operator, 
  4864.      array subscripting, 
  4865.      the .  and, -> operators (structure member access), 
  4866.      the unary & (address-of) operator (see exception below), 
  4867.      the unary * (indirection) operator, 
  4868.      casts to a type other than an integer type. 
  4869.  
  4870.  In a constant expression that is an initializer, floating-point constants and 
  4871.  casts may be specified.  Objects that have static storage duration, and 
  4872.  function designators (names), may be used to provide addresses, either 
  4873.  explicitly using the unary & (address-of) operator, or implicitly by 
  4874.  specifying the identifier only. 
  4875.  
  4876.  The following examples illustrate constant expressions that may be used 
  4877.  anywhere: 
  4878.  
  4879.  
  4880.     3 
  4881.     256*3 + 27 
  4882.     OPSYS == OS_DOS /* These are macro names */ 
  4883.  
  4884.  The next set of examples are constant expressions that are only valid in an 
  4885.  initializer: 
  4886.  
  4887.  
  4888.     &SomeObject 
  4889.     SomeFunction 
  4890.     3.5 * 7.2 / 6.5 
  4891.  
  4892.  In a constant expression that is part of a  #if or  #elif preprocessor 
  4893.  directive, only integral constants and operators are permitted (and macros 
  4894.  that, when replaced, follow these same rules). 
  4895.  
  4896.  
  4897. ΓòÉΓòÉΓòÉ 11. Statements ΓòÉΓòÉΓòÉ
  4898.  
  4899.  
  4900. A statement describes what actions are to be performed.  Statements may only be 
  4901. placed inside functions.  Statements are executed in sequence, except where 
  4902. described below. 
  4903.  
  4904.  
  4905. ΓòÉΓòÉΓòÉ 11.1. Labelled Statements ΓòÉΓòÉΓòÉ
  4906.  
  4907.  
  4908. Any statement may be preceded by a  label.  Labelled statements are usually the 
  4909. target of a  goto statement, and hence occur infrequently. 
  4910.  
  4911. A label is an identifier followed by a colon.  Labels do not affect the flow of 
  4912. execution of a program. A label that is encountered during execution is 
  4913. ignored. 
  4914.  
  4915. The following example illustrates a statement with a label: 
  4916.  
  4917.  
  4918.    xyz: i = 0; 
  4919.  
  4920. Labels can only precede statements.  It follows that labels may only appear 
  4921. inside functions. 
  4922.  
  4923. A label may be defined only once within any particular function. 
  4924.  
  4925. The identifier used for a label may be the same as another identifier for an 
  4926. object, function or tag, or a label in another function.  The  name space for 
  4927. labels is separate from non-label identifiers, and each function has its own 
  4928. label name space. 
  4929.  
  4930.  
  4931. ΓòÉΓòÉΓòÉ 11.2. Compound Statements ΓòÉΓòÉΓòÉ
  4932.  
  4933.  
  4934. A  compound statement is a set of statements grouped together inside braces. 
  4935. It may have its own declarations of objects, with or without initializations, 
  4936. and may or may not have any executable statements.  A compound statement is 
  4937. also called a  block. 
  4938.  
  4939. The general form of a compound statement is: 
  4940.  
  4941.  { declaration-list statement-list } 
  4942.  
  4943. where declaration-list is a list of zero or more declarations of objects to be 
  4944. used in the block.  statement-list is a list of zero or more statements to be 
  4945. executed when the block is entered. 
  4946.  
  4947. Any declarations for objects that have automatic storage duration and 
  4948. initializers for them are evaluated in the order in which they occur. 
  4949.  
  4950. An object declared with the keyword  extern inside a block may not be 
  4951. initialized in the declaration, since the storage for that object is defined 
  4952. elsewhere. 
  4953.  
  4954. An object declared in a block, without the keyword  extern, may not be 
  4955. redeclared within the same block, except in a block contained within the 
  4956. current block. 
  4957.  
  4958.  
  4959. ΓòÉΓòÉΓòÉ 11.3. Expression Statements ΓòÉΓòÉΓòÉ
  4960.  
  4961.  
  4962. A statement that is an expression is evaluated as a void expression for its 
  4963. side effects, such as the assigning of a value with the assignment operator. 
  4964. The result of the expression is discarded.  This discarding may be made 
  4965. explicit by casting the expression as a  void. 
  4966.  
  4967. For example, the statement, 
  4968.  
  4969.  
  4970.    count = 3; 
  4971.  
  4972. consists of the expression count = 3, which has the side effect of assigning 
  4973. the value 3 to the object count.  The result of the expression is 3, with the 
  4974. type the same as the type of count.  The result is not used any further.  As 
  4975. another example, the statement, 
  4976.  
  4977.  
  4978.    (void) memcpy( dest, src, len ); 
  4979.  
  4980. indicates that, regardless of the fact that  memcpy returns a result, the 
  4981. result should be ignored.  However, it is equally valid, and quite common, to 
  4982. write, 
  4983.  
  4984.  
  4985.    memcpy( dest, src, len ); 
  4986.  
  4987. As a matter of programming style, casting an expression as  void should only be 
  4988. done when the result of the expression might normally be expected to be used 
  4989. further.  In this case, casting to  void indicates that the result was 
  4990. intentionally discarded and is not an error of omission. 
  4991.  
  4992.  
  4993. ΓòÉΓòÉΓòÉ 11.4. Null Statements ΓòÉΓòÉΓòÉ
  4994.  
  4995.  
  4996. A null statement, which is just a semi-colon, takes no action.  It is useful 
  4997. for placing a label just before a block-closing brace, or for indicating an 
  4998. empty block, such as in an iteration statement.  Consider the following 
  4999. examples of null statements: 
  5000.  
  5001.  
  5002.    { 
  5003.      gets( buffer ); 
  5004.      while( *buffer++ != '\0' ) 
  5005.        ; 
  5006.      /* ... */ 
  5007.      endblk: ; 
  5008.    } 
  5009.  
  5010. The  while iteration statement skips over characters in buffer until the null 
  5011. character is found.  The body of the iteration is empty, since the controlling 
  5012. expression does all of the work.  The endblk:  declares a label just before the 
  5013. final }, which might be used by a  goto to exit the block. 
  5014.  
  5015.  
  5016. ΓòÉΓòÉΓòÉ 11.5. Selection Statements ΓòÉΓòÉΓòÉ
  5017.  
  5018.  
  5019. A  selection statement evaluates an expression, called the  controlling 
  5020. expression, then based on the result selects from a set of statements.  These 
  5021. statements are then executed. 
  5022.  
  5023.  
  5024. ΓòÉΓòÉΓòÉ 11.5.1. The if Statement ΓòÉΓòÉΓòÉ
  5025.  
  5026.  
  5027.  if( expression ) statement 
  5028.  or 
  5029.  if( expression ) statement else statement 
  5030.  
  5031. In both cases, the type of the controlling expression (inside the parentheses) 
  5032. is a scalar type (not a structure, union or array).  If the controlling 
  5033. expression evaluates to a non-zero value, then the first statement is executed. 
  5034.  
  5035. In the second form, the  else is executed if the controlling expression 
  5036. evaluates to zero. 
  5037.  
  5038. Each statement may be a compound statement.  For example, 
  5039.  
  5040.  
  5041.    if( delay > 5 ) { 
  5042.      printf( "Waited too long\n" ); 
  5043.      ok = FALSE; 
  5044.    } else { 
  5045.      ok = TRUE; 
  5046.    } 
  5047.  
  5048. In the classic case of the dangling  else, the  else is bound to the nearest 
  5049. if that does not yet have an  else.  For example, 
  5050.  
  5051.  
  5052.    if( x > 0 ) 
  5053.      if( y > 0 ) 
  5054.        printf( "x > 0 && y > 0\n" ); 
  5055.    else 
  5056.      printf( "x <= 0\n" ); 
  5057.  
  5058. will print x <= 0 when x > 0 is true and y > 0 is false, because the  else is 
  5059. bound to the second  if, not the first.  To correct this example, it would have 
  5060. to be changed to, 
  5061.  
  5062.  
  5063.    if( x > 0 ) { 
  5064.      if( y > 0 ) 
  5065.        printf( "x > 0 && y > 0\n" ); 
  5066.    } else 
  5067.      printf( "x <= 0\n" ); 
  5068.  
  5069. This example illustrates why it is a good idea to always use braces to 
  5070. explicitly state the subject of the control structures, rather than relying on 
  5071. the fact that a single statement is also a compound statement.  A better way of 
  5072. writing the above example is, 
  5073.  
  5074.  
  5075.    if( x > 0 ) { 
  5076.      if( y > 0 ) { 
  5077.        printf( "x > 0 && y > 0\n" ); 
  5078.      } 
  5079.    } else { 
  5080.      printf( "x <= 0\n" ); 
  5081.    } 
  5082.  
  5083. where all subjects of the control structures are contained within braces, 
  5084. leaving no doubt about the meaning.  A dangling  else cannot occur if braces 
  5085. are always used. 
  5086.  
  5087. If the statements between the  if and the  else are reached via a label, the 
  5088. statements following the  else will not be executed.  However, jumping into a 
  5089. block is poor programming practice, since it makes the program difficult to 
  5090. follow. 
  5091.  
  5092.  
  5093. ΓòÉΓòÉΓòÉ 11.5.2. The switch Statement ΓòÉΓòÉΓòÉ
  5094.  
  5095.  
  5096.  switch( expression ) statement 
  5097.  
  5098. Usually, statement is a compound statement or block.  Embedded within the 
  5099. statement are  case labels and possibly a  default label, of the following 
  5100. form: 
  5101.  
  5102.  case constant-expression :  statement 
  5103.  default :  statement 
  5104.  
  5105. The controlling expression and the constant-expressions on each  case label all 
  5106. must have integral type.  No two of the  case constant-expressions may be the 
  5107. same value.  The  default label may appear at most once in any  switch block. 
  5108.  
  5109. The controlling statement is evaluated, and the integral promotion is performed 
  5110. on the result.  If the promoted value of the expression matches any of the case 
  5111. labels promoted to the same type, control is given to the statement following 
  5112. that case label.  Otherwise, control is given to the statement following the 
  5113. default label (if present).  If no default label is present, then no statements 
  5114. in the  switch block are executed. 
  5115.  
  5116. When statements within a  switch block are being executed and another  case or 
  5117. default is encountered, it is ignored and execution continues with the 
  5118. statement following the label.  The  break statement may be used to terminate 
  5119. execution of the switch block. 
  5120.  
  5121. In the following example, 
  5122.  
  5123.  
  5124.    int i; 
  5125.  
  5126.    for( i = 1; i <= 8; i++ ) { 
  5127.      printf( "%d ", i ); 
  5128.      switch( i ) { 
  5129.       case 2: 
  5130.       case 4: 
  5131.        printf( "less than 5 " ); 
  5132.       case 6: 
  5133.       case 8: 
  5134.        printf( "even\n" ); 
  5135.        break; 
  5136.       default: 
  5137.        printf( "odd\n" ); 
  5138.      } 
  5139.    } 
  5140.  
  5141. the following output is produced: 
  5142.  
  5143.  
  5144.    1 odd 
  5145.    2 less than 5 even 
  5146.    3 odd 
  5147.    4 less than 5 even 
  5148.    5 odd 
  5149.    6 even 
  5150.    7 odd 
  5151.    8 even 
  5152.  
  5153.  
  5154. ΓòÉΓòÉΓòÉ 11.6. Iteration Statements ΓòÉΓòÉΓòÉ
  5155.  
  5156.  
  5157. Iteration statements control looping.  There are three forms of iteration 
  5158. statements:  while,  do/ while and  for. 
  5159.  
  5160. The controlling expression must have a scalar type.  The loop body (often a 
  5161. compound statement or block) is executed repeatedly until the controlling 
  5162. expression is equal to zero. 
  5163.  
  5164.  
  5165. ΓòÉΓòÉΓòÉ 11.6.1. The while Statement ΓòÉΓòÉΓòÉ
  5166.  
  5167.  
  5168.  while ( expression ) statement 
  5169.  
  5170. The evaluation of the controlling expression takes place before each execution 
  5171. of the loop body (statement).  If the expression evaluates to zero the first 
  5172. time, the loop body is not executed at all. 
  5173.  
  5174. The statement may be a compound statement. 
  5175.  
  5176. For example, 
  5177.  
  5178.    char * ptr; 
  5179.    /* ... */ 
  5180.    while( *ptr != '\0' ) { 
  5181.      if( *ptr == '.' )break; 
  5182.      ++ptr; 
  5183.    } 
  5184.  
  5185. The loop will scan characters pointed at by ptr until either a null character 
  5186. or a dot is found.  If the initial value of ptr points at a null character, 
  5187. then no part of the loop body will be executed, leaving ptr pointing at the 
  5188. null character. 
  5189.  
  5190.  
  5191. ΓòÉΓòÉΓòÉ 11.6.2. The do Statement ΓòÉΓòÉΓòÉ
  5192.  
  5193.  
  5194.  do statement while ( expression ); 
  5195.  
  5196. The evaluation of the controlling expression takes place after each execution 
  5197. of the loop body (statement).  If the expression evaluates to zero the first 
  5198. time, the loop body is executed exactly once. 
  5199.  
  5200. The statement may be a compound statement. 
  5201.  
  5202. For example, 
  5203.  
  5204.    char * ptr; 
  5205.    char * endptr; 
  5206.    /* ... */ 
  5207.    endptr = ptr + strlen( ptr ); 
  5208.    do { 
  5209.      --endptr; 
  5210.    } while( endptr >= ptr  &&  *endptr == ' ' ); 
  5211.  
  5212. In this example, the loop will terminate when endptr finds a non-blank 
  5213. character starting from the right, or when endptr goes past the beginning of 
  5214. the string.  If a non-blank character is found, endptr will be left pointing at 
  5215. that character. 
  5216.  
  5217.  
  5218. ΓòÉΓòÉΓòÉ 11.6.3. The for Statement ΓòÉΓòÉΓòÉ
  5219.  
  5220.  
  5221. The statement, 
  5222.  
  5223.  for ( expr1; expr2; expr3 ) statement 
  5224.  
  5225. is almost equivalent to, 
  5226.  
  5227.  expr1; 
  5228.  while ( expr2 ) { 
  5229.  statement 
  5230.  expr3; 
  5231.  } 
  5232.  
  5233. The difference is that the  continue statement will pass control to the 
  5234. statement expr3 rather than to the end of the loop body. 
  5235.  
  5236. expr1 is an initialization expression and may be omitted. 
  5237.  
  5238. expr2 is the controlling expression, and specifies an evaluation to be made 
  5239. before each iteration of the loop body.  If the expression evaluates to zero, 
  5240. the loop body is not executed, and control is passed to the statement following 
  5241. the loop body.  If expr2 is omitted, then a non-zero (true) value is 
  5242. substituted in its place.  In this case, the statements in the loop must cause 
  5243. an explicit break from the loop. 
  5244.  
  5245. expr3 specifies an operation to be performed after each iteration.  A common 
  5246. operation would be the incrementing of a counter.  expr3 may be omitted. 
  5247.  
  5248. The statement may be a compound statement. 
  5249.  
  5250. For example, 
  5251.  
  5252.    char charvec[256]; 
  5253.    int  count; 
  5254.  
  5255.    for( count = 0; count <= 255; count++ ) { 
  5256.      charvec[count] = count; 
  5257.    } 
  5258.  
  5259. This example will initialize the character array charvec to the values from 0 
  5260. to 255. 
  5261.  
  5262. The following are examples of  for statements: 
  5263.  
  5264.  for( ;; ) 
  5265.  statement; 
  5266.  
  5267. All statements in the body of the loop will be executed until a  break or  goto 
  5268. statement is executed which passes control outside of the loop, or a  return 
  5269. statement is executed which exits the function.  This is sometimes called  loop 
  5270. forever. 
  5271.  
  5272.  for( i = 0; i <= 100; ++i ) 
  5273.  statement; 
  5274.  
  5275. The object i is given the initial value zero, and after each iteration of the 
  5276. loop is incremented by one.  The loop is executed 101 times, with i having the 
  5277. successive values 0, 1, 2 ...  99, 100, and having the value 101 after 
  5278. termination of the loop. 
  5279.  
  5280.  for( ; *bufptr != '\0'; ++bufptr ) 
  5281.  statement; 
  5282.  
  5283. The object bufptr is already initialized, and the loop will continue until 
  5284. bufptr points at a null character.  After each iteration of the loop, bufptr 
  5285. will be incremented to point at the next character. 
  5286.  
  5287.  
  5288. ΓòÉΓòÉΓòÉ 11.7. Jump Statements ΓòÉΓòÉΓòÉ
  5289.  
  5290.  
  5291. A jump statement causes execution to continue at a specific place in a program, 
  5292. without executing any other intervening statements.  There are four jump 
  5293. statements:  goto,  continue,  break and  return. 
  5294.  
  5295.  
  5296. ΓòÉΓòÉΓòÉ 11.7.1. The goto Statement ΓòÉΓòÉΓòÉ
  5297.  
  5298.  
  5299.  goto identifier; 
  5300.  
  5301. identifier is a label somewhere in the current function (including any block 
  5302. within the function).  The next statement executed will be the one following 
  5303. that label. 
  5304.  
  5305. Note:  it can be confusing to use the  goto statement excessively.  It is easy 
  5306. to create  spaghetti code, which is very difficult to understand, even by the 
  5307. person who wrote it.  It is recommended that the  goto statement be used, at 
  5308. most, to jump out of blocks, never into them. 
  5309.  
  5310.  
  5311. ΓòÉΓòÉΓòÉ 11.7.2. The continue Statement ΓòÉΓòÉΓòÉ
  5312.  
  5313.  
  5314.  
  5315.    continue; 
  5316.  
  5317. A  continue statement may only appear within a loop body, and causes a jump to 
  5318. the inner-most loop's loop-continuation statement (the end of the loop body). 
  5319.  
  5320. In a  while statement, the jump is effectively back to the  while. 
  5321.  
  5322. In a  do statement, the jump is effectively down to the  while. 
  5323.  
  5324. In a  for statement, the jump is effectively to the closing brace of the 
  5325. compound-statement that is the subject of the  for loop.  The third expression 
  5326. in the  for statement, which is often an increment or decrement, is then 
  5327. executed before control is returned to the top of the loop. 
  5328.  
  5329.  
  5330. ΓòÉΓòÉΓòÉ 11.7.3. The break Statement ΓòÉΓòÉΓòÉ
  5331.  
  5332.  
  5333.  
  5334.    break; 
  5335.  
  5336. A  break statement may only appear in an iteration (loop) body or a  switch 
  5337. statement. 
  5338.  
  5339. In a loop, a  break will cause execution to continue at the statement following 
  5340. the loop body. 
  5341.  
  5342. In a  switch statement, a  break will cause execution to continue at the 
  5343. statement following the switch.  If the loop or  switch that contains the 
  5344. break is enclosed inside another loop or  switch, only the inner-most loop or 
  5345. switch is terminated.  The  goto statement may be used to terminate more than 
  5346. one loop or  switch. 
  5347.  
  5348.  
  5349. ΓòÉΓòÉΓòÉ 11.7.4. The return Statement ΓòÉΓòÉΓòÉ
  5350.  
  5351.  
  5352.  return; 
  5353.  or 
  5354.  return expression; 
  5355.  
  5356. A popular variation of the second form is, 
  5357.  
  5358.   return( expression ); 
  5359.  
  5360. The  return statement causes execution of the current function to be 
  5361. terminated, and control is passed to the caller.  A function may contain any 
  5362. number of  return statements. 
  5363.  
  5364. If the function is declared with a return type of  void (no value is returned), 
  5365. then no  return statement within that function may return a value. 
  5366.  
  5367. If the function is declared as having a return type of other than  void, then 
  5368. any  return statement with an expression will evaluate the expression and 
  5369. convert it to the return type.  That value will be the value returned by the 
  5370. function.  If a  return is executed without an expression, and the caller uses 
  5371. the value returned by the function, the behavior is undefined since no value 
  5372. was returned.  An arbitrary value will probably be used. 
  5373.  
  5374. Reaching the closing brace } that terminates the function is equivalent to 
  5375. executing a  return statement without an expression. 
  5376.  
  5377.  
  5378. ΓòÉΓòÉΓòÉ 12. Functions ΓòÉΓòÉΓòÉ
  5379.  
  5380.  
  5381. There are two forms for defining a function.  The first form is, 
  5382.  
  5383.  storage-class return-type identifier ( parameter-type-list ) 
  5384.  { 
  5385.  declaration-list 
  5386.  
  5387.  statement-list 
  5388.  } 
  5389.  
  5390. The storage-class may be one of  extern or  static.  If storage-class is 
  5391. omitted,  extern is assumed. 
  5392.  
  5393. The return-type may be any valid type except an array.  If return-type is 
  5394. omitted,  int is assumed. 
  5395.  
  5396. The identifier is the name of the function. 
  5397.  
  5398. The parameter-type-list is either  void or empty, meaning the function takes no 
  5399. parameters, or a comma-separated list of declarations of the objects, including 
  5400. both type and parameter name (identifier).  If multiple arguments of the same 
  5401. type are specified, the type of each argument must be given individually.  The 
  5402. form, 
  5403.  
  5404.  type id1, id2 
  5405.  
  5406. is not permitted within the parameter list. 
  5407. If the parameter-type-list ends with ,...  then the function will accept a 
  5408. variable number of arguments. 
  5409.  
  5410. Any parameter declared as "array of type" is changed to "pointer to type".  Any 
  5411. parameter declared as " function" is changed to "pointer to function". 
  5412.  
  5413. The following examples illustrate several function definitions: 
  5414.  
  5415.  
  5416.    int F( void ) 
  5417.  
  5418.   The function F has no parameters, and returns an integer. 
  5419.  
  5420.    void G( int x ) 
  5421.  
  5422.   The function G has one parameter, an integer object named x, and does not 
  5423. return a value. 
  5424.  
  5425.    void * H( long int len, long int wid ) 
  5426.  
  5427.   The function H has two parameters, long integer objects named len and wid, 
  5428. and returns a pointer which does not point to any particular type of object. 
  5429.  
  5430.    void I( char * format, ... ) 
  5431.  
  5432.   The function I has one known parameter, an object named format that is a 
  5433. pointer to a character (string).  The function also accepts a variable number 
  5434. of parameters following format.  The function does not return a result. 
  5435. This form of function definition also serves as a prototype declaration for any 
  5436. calls to the function that occur later in the same module.  With the function 
  5437. prototype in scope at the time of a call to the function, the arguments are 
  5438. converted to the type of the corresponding parameter prior to the value being 
  5439. assigned.  If a call to the function is to be made prior to its definition, or 
  5440. from another module, a function prototype should be specified for it in order 
  5441. to ensure proper conversion of argument types.  Failure to do this will result 
  5442. in the default argument promotions being performed, with undefined behavior if 
  5443. the function parameter types do not match the promoted argument types. 
  5444.  
  5445. The second form of function definition is, 
  5446.  
  5447.  storage-class return-type identifier ( identifier-list ) 
  5448.  declaration-list 
  5449.  { 
  5450.  declaration-list 
  5451.  
  5452.  statement-list 
  5453.  } 
  5454.  
  5455. The storage-class, return-type and identifier parts are all the same as for the 
  5456. first form of definition.  In this form, the identifier-list is a (possibly 
  5457. empty) comma-separated list of identifiers (object names) without any type 
  5458. information.  Following the closing parenthesis, and before the opening brace 
  5459. of the body of the function, the declarations for the objects are given, using 
  5460. the normal rules.  Any object of type  int need not be explicitly declared. 
  5461.  
  5462. In the declarations of the parameter identifiers,  register is the only 
  5463. storage-class specifier that may be used. 
  5464.  
  5465. A function prototype is created from the definition after the default argument 
  5466. promotions have been performed on each parameter.  All arguments to a function 
  5467. declared in this manner will have the default argument promotions performed on 
  5468. them.  The resulting types must match the types of the declared parameters, 
  5469. after promotion.  Otherwise, the behavior is undefined. 
  5470.  
  5471. Note that it is impossible to pass an object of type  float to a function 
  5472. declared in this manner.  The argument of type  float will automatically be 
  5473. promoted to  double, and the parameter will also be promoted to  double 
  5474. (assuming that it was declared as  float).  For similar reasons, it is not 
  5475. possible to pass an object of type  char or  short int without promotion taking 
  5476. place. 
  5477.  
  5478. According to the ANSI standard for the C language, this form of function 
  5479. definition is obsolete and should not be used.  It is provided for historical 
  5480. reasons, in particular, for compatibility with older C compilers.  Using the 
  5481. first form of function definition often allows the compiler to generate better 
  5482. code. 
  5483.  
  5484. The following examples are the same as those given with the first form above, 
  5485. with the appropriate modifications: 
  5486.  
  5487.  
  5488.    int F() 
  5489.  
  5490.   The function F has no parameters, and returns an integer. 
  5491.  
  5492.    void G( x ) 
  5493.  
  5494.   The function G has one parameter, an integer object named x, and does not 
  5495. return a value.  This example could have also been written as, 
  5496.  
  5497.      void G( x ) 
  5498.        int x; 
  5499.  
  5500.   which explicitly declares x to be an integer. 
  5501.  
  5502.  
  5503.    void * H( len, wid ) 
  5504.      long int len; 
  5505.      long int wid; 
  5506.  
  5507.   The function H has two parameters, both integer objects named len and wid, 
  5508. and returns a pointer which does not point to any particular type of object. 
  5509. Any call to this function must ensure that the arguments are long integers, 
  5510. either by using an object so declared, or by explicitly casting the object to 
  5511. the type. 
  5512. The last example using the ellipsis (,...) notation is not directly 
  5513. representable using the second form of function definition.  With most 
  5514. compilers it is possible to handle variable argument lists in this form, but 
  5515. knowledge of the mechanism used to pass arguments to functions is required, and 
  5516. this mechanism may vary between different compilers. 
  5517.  
  5518.  
  5519. ΓòÉΓòÉΓòÉ 12.1. The Body of the Function ΓòÉΓòÉΓòÉ
  5520.  
  5521.  
  5522. Following the declaration of the function and the opening brace is the body of 
  5523. the function.  It consists of two portions, both of which are optional. 
  5524.  
  5525. The first portion is the declaration list for any objects needed within the 
  5526. function.  These objects may have any type and any storage class.  Objects with 
  5527. storage class  register or  auto have  automatic storage duration, meaning they 
  5528. are created when the function is called, and destroyed when the function 
  5529. returns to the caller.  (The value of the object is not preserved between calls 
  5530. to the function.) Objects with storage class  extern or  static have  static 
  5531. storage duration, meaning they are created once, before the function is ever 
  5532. called, and destroyed only when the program terminates.  Any value placed in 
  5533. such an object will remain even after the function has returned, so that the 
  5534. next time the function is called the value will still be present (unless some 
  5535. other action is taken to change it, such as using another object containing a 
  5536. pointer to the static object to modify the value). 
  5537.  
  5538. Unless an explicit  return statement is executed, the function will not return 
  5539. to the caller until the brace at the end of the function definition is 
  5540. encountered.  The return will be as if a  return statement with no expression 
  5541. was executed.  If the function is declared as returning a value, and the caller 
  5542. attempts to use the value returned in this manner, the behavior is undefined. 
  5543. The value used will be arbitrary. 
  5544.  
  5545. A function may call itself (recursion) directly, or it may call another 
  5546. function or functions which in turn call it.  Any objects declared with 
  5547. automatic storage duration are created as a new instance of the object upon 
  5548. each recursion, while objects declared with static storage duration only have 
  5549. one instance shared between the recursive instances of the function. 
  5550.  
  5551.  
  5552. ΓòÉΓòÉΓòÉ 12.2. Function Prototypes ΓòÉΓòÉΓòÉ
  5553.  
  5554.  
  5555. A function prototype is like a definition of a function, but without the body. 
  5556. A semi-colon is specified immediately following the closing right parenthesis 
  5557. of the function's declaration.  The prototype describes the name of the 
  5558. function, the types of parameters it expects (names are optional) and the type 
  5559. of the return value.  This information can be used by the C compiler to do 
  5560. proper argument type checking and conversion for calls to the function, and to 
  5561. properly handle the return value. 
  5562.  
  5563. If no function prototype has been found by the time a call to a function is 
  5564. made, all arguments have the default argument promotions performed on them, and 
  5565. the return type is assumed to be  int.  If the actual definition of the 
  5566. function does not have parameters that match the promoted types, the behavior 
  5567. is undefined.  If the return type is not  int and a return value is required, 
  5568. the behavior is undefined. 
  5569.  
  5570. The prototype for a function must match the function definition.  Each 
  5571. parameter type and the type of the return value must be the same, otherwise the 
  5572. behavior is undefined. 
  5573.  
  5574. All library functions have prototypes in one of several header files.  That 
  5575. header file should be included whenever a function described therein is used. 
  5576. Refer to the Watcom C Library Reference manual for details. 
  5577.  
  5578.  
  5579. ΓòÉΓòÉΓòÉ 12.2.1. Variable Argument Lists ΓòÉΓòÉΓòÉ
  5580.  
  5581.  
  5582. If the prototype (and definition) for a function has a parameter list that ends 
  5583. with ,...  then the function has a variable argument list or variable parameter 
  5584. list meaning that the number of arguments to the function can vary.  (The 
  5585. library function  printf is an example.) At least one argument must be provided 
  5586. before the variable portion.  This argument usually describes, in some fashion, 
  5587. how many other arguments to expect.  It may be a simple count, or may involve 
  5588. (as with  printf) an encoding of the number and types of arguments. 
  5589.  
  5590. All arguments that correspond to a variable argument list have the default 
  5591. argument promotions performed on them, since it is not possible to determine, 
  5592. at compilation time, what types will be required by the function. 
  5593.  
  5594. Since the parameters represented by the ,...  don't have names, special 
  5595. handling is required.  The C language provides a special type and three macros 
  5596. for handling variable argument lists.  To be able to use these, the header 
  5597. <stdarg.h> must be included. 
  5598.  
  5599. The type  va_list is an implementation-specific type used to store information 
  5600. about the variable list.  Within the function, an object must be declared with 
  5601. type  va_list.  This object is used by the macros and functions for processing 
  5602. the list. 
  5603.  
  5604. The macro  va_start has the form, 
  5605.  
  5606.  
  5607.    void va_start( va_list parminfo 
  5608.    , 
  5609.    lastparm 
  5610.    ); 
  5611.  
  5612. The object parminfo is set up by the macro with information describing the 
  5613. variable list.  The argument lastparm is the name (identifier) of the last 
  5614. parameter before the ,...  and must not have been declared with the storage 
  5615. class  register. 
  5616.  
  5617. The macro  va_start must be executed before any processing of the variable 
  5618. portion of the parameter list is performed. 
  5619.  
  5620.  va_start may be executed more than once, but only if an intervening  va_end is 
  5621. executed. 
  5622.  
  5623. The macro  va_arg has the form, 
  5624.  
  5625.  
  5626.    type 
  5627.    va_arg( va_list 
  5628.    parminfo 
  5629.    , 
  5630.    type 
  5631.    ); 
  5632.  
  5633. parminfo is the same object named in the call to  va_start.  type is the type 
  5634. of argument expected.  The types expected should only be those that result from 
  5635. the default argument promotions ( int and  long int and their unsigned 
  5636. varieties,  double and  long double), and those that are not subject to 
  5637. promotion (pointers, structures and unions).  The type must be determined by 
  5638. the program.  The  va_arg macro expands to an expression that has the type and 
  5639. value of the next parameter in the variable list. 
  5640.  
  5641. In the case of  printf, the parameter type expected is determined by the 
  5642. "conversion specifications" such as %s, %d and so on. 
  5643.  
  5644. The first invocation of the  va_arg macro (after executing a  va_start) returns 
  5645. the value of the parameter following lastparm (as specified in  va_start). 
  5646. Each subsequent invocation of  va_arg returns the next parameter in the list. 
  5647. At each invocation, the value of parminfo is modified (in some 
  5648. implementation-specific manner) to reflect the processing of the parameter 
  5649. list. 
  5650.  
  5651. If the type of the next parameter does not match type, or if no parameter was 
  5652. specified, the behavior is undefined. 
  5653.  
  5654. The macro  va_end has the form, 
  5655.  
  5656.  
  5657.    void va_end( va_list parminfo 
  5658.    ); 
  5659.  
  5660. parminfo is the same object named in the corresponding call to  va_start.  The 
  5661. function  va_end closes off processing of the variable argument list, which 
  5662. must be done prior to returning from the function.  If  va_end is not called 
  5663. before returning, the behavior is undefined. 
  5664.  
  5665. If  va_end is called without a corresponding call to  va_start having been 
  5666. done, the behavior is undefined. 
  5667.  
  5668. After calling  va_end and prior to returning, it is possible to call  va_start 
  5669. again and reprocess the variable list.  It will be necessary to call  va_end 
  5670. again before returning. 
  5671.  
  5672. The following function takes an arbitrary number of floating-point numbers as 
  5673. parameters along with a count, and returns the average of the numbers: 
  5674.  
  5675.  
  5676.    #include <stdarg.h> 
  5677.  
  5678.    extern double Average( int count, ... ) 
  5679.    /*************************************/ 
  5680.    { 
  5681.      double  sum = 0; 
  5682.      int   i; 
  5683.      va_list parminfo; 
  5684.  
  5685.      if( count == 0 ) { 
  5686.        return( 0.0 ); 
  5687.      } 
  5688.      va_start( parminfo, count ); 
  5689.      for( i = 0; i < count; i++ ) { 
  5690.        sum += va_arg( parminfo, double ); 
  5691.      } 
  5692.      va_end( parminfo ); 
  5693.      return( sum / count ); 
  5694.    } 
  5695.  
  5696.  
  5697. ΓòÉΓòÉΓòÉ 12.3. The Parameters to the Function main ΓòÉΓòÉΓòÉ
  5698.  
  5699.  
  5700. The function  main has a special meaning in C.  It is the function that 
  5701. receives control when a program is started.  The function main has the 
  5702. following definition: 
  5703.  
  5704.  
  5705.    extern int main( int argc, char * argv[] ) 
  5706.    /****************************************/ 
  5707.    { 
  5708.      statements 
  5709.    } 
  5710.  
  5711. The objects  argc and  argv have the following properties: 
  5712.  
  5713.      argc is the "argument count", or the number of parameters (including 
  5714.       program name) supplied to the program, and its value is greater than 
  5715.       zero, 
  5716.      argv is an array of pointers to strings containing the parameters, 
  5717.      argv[0] is the program name, if available, otherwise it is a pointer to a 
  5718.       string containing only the null character, 
  5719.      argv[argc] is a null pointer, representing the end of the argument list, 
  5720.      argv[1] through argv[argc-1] are pointers to strings representing the 
  5721.       arguments to the program.  These strings are modifiable by the program, 
  5722.       and exist throughout the execution of the program.  The strings will 
  5723.       generally be in mixed (upper and lower) case, although a system that 
  5724.       cannot provide mixed case argument strings will provide them in lower 
  5725.       case. 
  5726.  
  5727.  The translation of the arguments to the program, as provided by the operating 
  5728.  system (often from the command-line used to invoke the program), into the 
  5729.  strings contained in argv, is implementation-defined. 
  5730.  
  5731.   With Watcom C/16 and C/32, each unquoted, blank-separated token on the 
  5732.  command line is made into a string that is an element of argv.  Quoted strings 
  5733.  are maintained as one element without the quotes. 
  5734.  
  5735.   For example, the command line, 
  5736.  
  5737.  
  5738.      pgm 2+ 1 tokens "one token" 
  5739.  
  5740.   will result in argc having the value 5, and the elements of argv being the 
  5741.  strings "pgm", "2+", "1", "tokens" and "one token". 
  5742.  
  5743.  The function main may also be declared without any parameters, as, 
  5744.  
  5745.  
  5746.     extern int main( void ) 
  5747.     /*********************/ 
  5748.     { 
  5749.       statements 
  5750.     } 
  5751.  
  5752.  The return value of main is an integer, usually representing a termination 
  5753.  status.  If no return value is specified (by using a  return statement with no 
  5754.  expression or encountering the closing brace in the function), then the value 
  5755.  returned is undefined. 
  5756.  
  5757.  The  exit library function may be used to terminate the program at any point. 
  5758.  The value of the argument to  exit is returned as if main had returned the 
  5759.  value. 
  5760.  
  5761.  
  5762. ΓòÉΓòÉΓòÉ 13. The Preprocessor ΓòÉΓòÉΓòÉ
  5763.  
  5764.  
  5765. The preprocessor, as its name suggests, is that part of the C compiler which 
  5766. processes certain directives embedded in the source file(s) in advance of the 
  5767. actual compilation of the program.  Specifically, the preprocessor allows a 
  5768. source file to, 
  5769.  
  5770.      include other files (perhaps referencing externally-defined objects, or 
  5771.       containing the definitions of structures or other types which are needed 
  5772.       by more than one source file), 
  5773.      compile certain portions of the code depending on some condition (such as 
  5774.       the kind of computer for which the code is being generated), and, 
  5775.      replace macros with other text which is then compiled. 
  5776.  
  5777.  The preprocessing phase occurs after trigraphs have been converted and 
  5778.  physical lines ending with  \ have been concatenated to create longer logical 
  5779.  lines, but before escape sequences in character constants have been converted, 
  5780.  or adjacent string literals are concatenated. 
  5781.  
  5782.  Any line whose first non-blank character is a # marks the beginning of a 
  5783.  preprocessing directive.  Spaces may appear between the # and the identifier 
  5784.  for the directive.  The  #include and  #define directives are each contained 
  5785.  on one line (after concatenation of lines ending with \), while the 
  5786.  conditional compilation directives span multiple lines. 
  5787.  
  5788.  A preprocessor directive is not terminated by a semi-colon. 
  5789.  
  5790.  
  5791. ΓòÉΓòÉΓòÉ 13.1. The Null Directive ΓòÉΓòÉΓòÉ
  5792.  
  5793.  
  5794. A preprocessing directive of the form, 
  5795.  
  5796.  
  5797.    # 
  5798.  
  5799. (with no other tokens on the same line) has no effect and is discarded. 
  5800.  
  5801.  
  5802. ΓòÉΓòÉΓòÉ 13.2. Including Headers and Source Files ΓòÉΓòÉΓòÉ
  5803.  
  5804.  
  5805. A directive of the form, 
  5806.  
  5807.  
  5808.    #include <name> 
  5809.  
  5810. will search a sequence of places defined by the implementation for the  header 
  5811. identified by name.  A header declares a set of library functions and any 
  5812. necessary types or macros needed for their use.  Headers are usually provided 
  5813. by the compiler, or by a library provided for use with the compiler. 
  5814.  
  5815. name may not contain a > character.  If the header is found, the entire 
  5816. directive is replaced by the contents of the header.  If the header is not 
  5817. found, an error will occur. 
  5818.  
  5819. A directive of the form, 
  5820.  
  5821.  
  5822.    #include "name" 
  5823.  
  5824. will search for the source file identified by name.  name may not contain a " 
  5825. (double-quote) character.  If the source file identified by name is found, then 
  5826. the entire directive is replaced by the contents of the file.  Otherwise, the 
  5827. directive is processed as if the, 
  5828.  
  5829.  
  5830.    #include <name> 
  5831.  
  5832. form had been used. 
  5833.  
  5834. A third form of  #include directive is also supported.  A directive of the 
  5835. form, 
  5836.  
  5837.  
  5838.    #include tokens 
  5839.  
  5840. causes all macro substitutions (described below) to take place on tokens. 
  5841. After substitution, the directive must match either the <name> or "name" forms 
  5842. described above (including < and >, or quotes), in which case the  #include is 
  5843. processed in the corresponding manner. 
  5844.  
  5845. See the User's Guide for details about how the compiler searches for included 
  5846. files. 
  5847.  
  5848.  #include directives may be nested.  Each implementation may allow different 
  5849. depths of nesting, but all must allow at least 8 levels.  (In other words, a 
  5850. source file may include another file, which includes another file, and so on, 
  5851. up to a depth of eight files.) 
  5852.  
  5853.  The operating system may further limit the number of files that may be open at 
  5854. one time.  See the appropriate operating system manual for details. 
  5855.  
  5856.  
  5857. ΓòÉΓòÉΓòÉ 13.3. Conditionally Including Source Lines ΓòÉΓòÉΓòÉ
  5858.  
  5859.  
  5860. A directive of the form, 
  5861.  
  5862.   #if constant-expression 
  5863.  body of  #if 
  5864.   #endif 
  5865.  
  5866. evaluates the constant-expression, and if it evaluates to a non-zero value, 
  5867. then the body of the  #if is processed by the preprocessor.  Processing of the 
  5868. body ends when a corresponding  #elif,  #else, or the terminating  #endif is 
  5869. encountered. 
  5870.  
  5871. The  #if directive allows source and preprocessor lines to be conditionally 
  5872. processed by the compiler. 
  5873.  
  5874. If the constant-expression evaluates to zero, then the body of the  #if is not 
  5875. processed, and the corresponding  #elif or  #else (if present) is processed. 
  5876. If neither of these directives are present, then the preprocessor skips to the 
  5877. #endif.  Any preprocessing directives within the body of the  #if are not 
  5878. processed, but they are examined in order to determine any nested directives, 
  5879. in order to find the matching  #elif,  #else or  #endif. 
  5880.  
  5881. The constant-expression is of the same form as used in the  if statement, 
  5882. except that the values used must be integer values (including character 
  5883. constants).  No cast or  sizeof operators or enumeration constants may be used. 
  5884. Each identifier that is a macro name is replaced (as described below), and 
  5885. remaining identifiers are replaced with 0L.  All values are converted to long 
  5886. integers using the usual arithmetic conversions.  After each item has been 
  5887. converted, the evaluation of the expression takes place using the arithmetic of 
  5888. the translation environment.  Any character constants are evaluated as members 
  5889. of the source character set. 
  5890.  
  5891.  With Watcom C/16 and C/32, character constants have the same value in both the 
  5892. source and execution character sets. 
  5893.  
  5894. The unary expression, 
  5895.  
  5896.  defined identifier 
  5897.  or 
  5898.  defined( identifier ) 
  5899.  
  5900. may be used to determine if an identifier is currently defined as a macro.  Any 
  5901. macro name that is part of this unary expression is not expanded.  The above 
  5902. expressions evaluate to 1 if the named identifier is currently a macro, 
  5903. otherwise they evaluate to 0. 
  5904.  
  5905. As discussed above, if the constant-expression of the  #if evaluates to zero, 
  5906. the preprocessor looks for a corresponding  #elif.  This directive means "else 
  5907. if", and has a similar form as  #if: 
  5908.  
  5909.   #elif constant-expression 
  5910.  body of  #elif 
  5911.  
  5912. An  #elif may only be placed inside the body of an  #if.  The body of the 
  5913. #elif is processed only if the constant-expression evaluates to a non-zero 
  5914. value and the constant-expressions of the corresponding  #if and (preceding) 
  5915. #elif statements evaluated to zero.  Otherwise the body is not processed, and 
  5916. the preprocessor skips to the next corresponding  #elif or  #else, or to the 
  5917. #endif if neither of these directives is present. 
  5918.  
  5919. The  #else directive has the form, 
  5920.  
  5921.   #else 
  5922.  body of  #else 
  5923.  
  5924. The body of the  #else is processed only if the constant expressions of the 
  5925. corresponding  #if and  #elif statements evaluated to zero.  The body of the 
  5926. #else is processed until the corresponding  #endif is encountered. 
  5927.  
  5928. The form of the  #endif directive is, 
  5929.  
  5930.  
  5931.    #endif 
  5932.  
  5933. and marks the end of the  #if. 
  5934.  
  5935. The following are examples of conditional inclusion of source lines: 
  5936.  
  5937.  
  5938.    #if OPSYS == OS_CMS 
  5939.      fn_syntax = "filename filetype fm"; 
  5940.    #elif OPSYS == OS_MVS 
  5941.      fn_syntax = "'userid.library.type(membername)'"; 
  5942.    #elif OPSYS == OS_DOS  ||  OPSYS == OS_OS2 
  5943.      fn_syntax = "filename.ext"; 
  5944.    #else 
  5945.      fn_syntax = "filename"; 
  5946.    #endif 
  5947.  
  5948. The object fn_syntax is set to the appropriate filename syntax string depending 
  5949. on the value of the macro OPSYS.  If OPSYS does not match any of the stated 
  5950. values, then fn_syntax is set to the default string "filename". 
  5951.  
  5952.  
  5953.    #if HARDWARE == HW_IBM370 
  5954.      #if OPSYS == OS_CMS 
  5955.        escape_cmd = "CMS"; 
  5956.      #elif OPSYS == OS_MVS 
  5957.        escape_cmd = "TSO"; 
  5958.      #else 
  5959.        escape_cmd = "SYSTEM"; 
  5960.      #endif 
  5961.    #else 
  5962.      escape_cmd = "SYSTEM"; 
  5963.    #endif 
  5964.  
  5965. The object escape_cmd is set to an appropriate string depending on the values 
  5966. of the macros HARDWARE and OPSYS.  The indentation of the directives clearly 
  5967. illustrates the flow between various conditions and levels of directives. 
  5968.  
  5969.  
  5970. ΓòÉΓòÉΓòÉ 13.3.1. The #ifdef and #ifndef Directives ΓòÉΓòÉΓòÉ
  5971.  
  5972.  
  5973. The  #ifdef directive is used to check if an identifier is currently defined as 
  5974. a macro.  For example, the directive, 
  5975.  
  5976.  
  5977.    #ifdef xyz 
  5978.  
  5979. processes the body of the  #ifdef only if the identifier xyz is currently a 
  5980. macro.  This example is equivalent to, 
  5981.  
  5982.  
  5983.    #if defined xyz 
  5984.  
  5985. or 
  5986.  
  5987.  
  5988.    #if defined( xyz ) 
  5989.  
  5990. In a similar manner, the directive, 
  5991.  
  5992.  
  5993.    #ifndef xyz 
  5994.  
  5995. is equivalent to, 
  5996.  
  5997.  
  5998.    #if !defined xyz 
  5999.  
  6000. or 
  6001.  
  6002.  
  6003.    #if !defined( xyz ) 
  6004.  
  6005.  
  6006. ΓòÉΓòÉΓòÉ 13.4. Macro Replacement ΓòÉΓòÉΓòÉ
  6007.  
  6008.  
  6009. A directive of the form, 
  6010.  
  6011.  #define identifier replacement-list 
  6012.  
  6013. defines a  macro with the name identifier.  This particular form of macro is 
  6014. called an  object-like macro, because it is used like an object (as opposed to 
  6015. a function).  Any source line that contains a token matching the macro name has 
  6016. that token replaced by the replacement-list.  The tokens of the 
  6017. replacement-list are then rescanned for more macro replacements. 
  6018.  
  6019. For example, the macro, 
  6020.  
  6021.  
  6022.    #define TABLE_LIMIT 256 
  6023.  
  6024. defines the macro TABLE_LIMIT to be equivalent to the token 256.  This is 
  6025. sometimes called a  manifest constant, because it provides a descriptive term 
  6026. for a value that makes programs easier to read.  It is a very good idea to use 
  6027. descriptive names wherever appropriate to improve the readability of a program. 
  6028. It may also save time if the same value is used many different places, and the 
  6029. value must be changed at some point. 
  6030.  
  6031. Care must be exercised when using more complicated object-like macros. 
  6032. Consider the following example: 
  6033.  
  6034.  
  6035.    #define COUNT1 10 
  6036.    #define COUNT2 20 
  6037.    #define TOTAL_COUNT COUNT1+COUNT2 
  6038.    /* ... */ 
  6039.    memptr = malloc( TOTAL_COUNT * sizeof( int ) ); 
  6040.  
  6041. If  int is 2 bytes in size, this call to  malloc will allocate 50 bytes of 
  6042. memory, instead of the expected 60.  This occurs because TOTAL_COUNT * sizeof( 
  6043. int ) becomes 10+20 * 2 after macro replacement, and the precedence rules for 
  6044. expression evaluation cause the multiply to be done first.  To solve this 
  6045. problem, the macro for TOTAL_COUNT should be defined as: 
  6046.  
  6047.  
  6048.    #define TOTAL_COUNT (COUNT1+COUNT2) 
  6049.  
  6050. A directive of the form, 
  6051.  
  6052.   #define identifier( identifier-list ) replacement-list 
  6053.  
  6054. is called a  function-like macro, because it is used like a function call.  No 
  6055. space may appear between identifier and the left parenthesis in the macro 
  6056. definition.  Any source line(s) that contains what looks like a function call, 
  6057. where the name of the function matches a function-like macro name, and the 
  6058. number of parameters matches the number of identifiers in the identifier-list, 
  6059. has the entire function call replaced by the replacement-list, substituting the 
  6060. actual arguments of the function call for the occurrences of the identifiers in 
  6061. the replacement-list.  If the left parenthesis following the macro name was 
  6062. created as the result of a macro substitution, no further substitution will 
  6063. take place.  If the macro name appears but is not followed by a left 
  6064. parenthesis, no further substitution will take place. 
  6065.  
  6066. Consider this example: 
  6067.  
  6068.  
  6069.    #define endof( string ) \ 
  6070.      (string + strlen( string )) 
  6071.  
  6072. The \ causes the two lines to be joined together into one logical line, making 
  6073. this equivalent to, 
  6074.  
  6075.  
  6076.    #define endof( string )   (string + strlen( string )) 
  6077.  
  6078. The function-like macro endof can be used to find a pointer to the null 
  6079. character terminating a string.  The statement, 
  6080.  
  6081.  
  6082.    endptr = endof( ptr ); 
  6083.  
  6084. will have the macro replaced, so it will then be parsed as, 
  6085.  
  6086.  
  6087.    endptr = (ptr + strlen( ptr )); 
  6088.  
  6089. Note that, in this case, the argument is evaluated twice.  If StrFn( ptr ) was 
  6090. specified instead of ptr, then the function would get called twice, because the 
  6091. substitution would yield, 
  6092.  
  6093.  
  6094.    endptr = (StrFn( ptr ) + strlen( StrFn( ptr ) )); 
  6095.  
  6096. In gathering up the tokens used to identify the arguments, each sequence of 
  6097. tokens separated by a comma constitutes an argument, unless that comma happens 
  6098. to be within a matched pair of left and right parentheses.  When a right 
  6099. parenthesis is found that matches the beginning left parenthesis, and the 
  6100. number of arguments matches the number of identifiers in the macro definition, 
  6101. then the gathering of the arguments is complete and the substitution takes 
  6102. place. 
  6103.  
  6104. For example, 
  6105.  
  6106.  
  6107.    #define mymemcpy( dest, src, len ) \ 
  6108.      memcpy( dest, src, len ) 
  6109.    /* ... */ 
  6110.    mymemcpy( destptr, srcptr, (t=0, t=strlen(srcptr)) ); 
  6111.  
  6112. will, for the parameters dest, src and len, use the arguments destptr, srcptr 
  6113. and (t=0, t=strlen(srcptr)) respectively. 
  6114.  
  6115. This form of macro is also useful for "commenting out" a function call that is 
  6116. used for debugging the program.  For example, 
  6117.  
  6118.  
  6119.    #define alive( where ) printf( "Alive at" where "\n" ) 
  6120.  
  6121. could later be replaced by, 
  6122.  
  6123.  
  6124.    #define alive( where ) /* */ 
  6125.  
  6126. Alternatively, the definition, 
  6127.  
  6128.  
  6129.    #define alive( where ) 
  6130.  
  6131. may be used.  When the module or program is recompiled using this new 
  6132. definition for alive, all of the calls to  printf made as a result of the macro 
  6133. replacement will disappear, without the necessity of deleting the appropriate 
  6134. lines in each module. 
  6135.  
  6136. A directive of the form, 
  6137.  
  6138.   #undef identifier 
  6139.  
  6140. causes the macro definition for identifier to be thrown away.  No error is 
  6141. reported if no macro definition for identifier exists. 
  6142.  
  6143.  
  6144. ΓòÉΓòÉΓòÉ 13.5. Argument Substitution ΓòÉΓòÉΓòÉ
  6145.  
  6146.  
  6147. The argument substitution capabilities of the C preprocessor are very powerful, 
  6148. but can be tricky.  The following sections illustrate the capabilities, and try 
  6149. to shed light on the problems that might be encountered. 
  6150.  
  6151.  
  6152. ΓòÉΓòÉΓòÉ 13.5.1. Converting An Argument to a String ΓòÉΓòÉΓòÉ
  6153.  
  6154.  
  6155. In the replacement-string for a function-like macro, each occurrence of  # must 
  6156. be followed by a parameter to the macro.  If so, both the # and the parameter 
  6157. are replaced by a string created from the characters of the argument itself, 
  6158. with no further substitutions performed on the argument.  Each white space 
  6159. within the argument is converted to a single blank character.  If the argument 
  6160. contains a character constant or string literal, any occurrences of " 
  6161. (double-quote) are replaced by \", and any occurrences of \ (backslash) are 
  6162. replaced by \\. 
  6163.  
  6164. The following table gives a number of examples of the result of the application 
  6165. of the macro, 
  6166.  
  6167.  
  6168.    #define string( parm ) # parm 
  6169.  
  6170. as shown in the first column: 
  6171.  
  6172.  
  6173.    Argument         After Substitution 
  6174.    ---------------------  ------------------ 
  6175.    string( abc )      "abc" 
  6176.    string( "abc" )     "\"abc\"" 
  6177.    string( "abc" "def" )  "\"abc\" \"def\"" 
  6178.    string( \'/ )      "\\'/" 
  6179.    string( f(x) )      "f(x)" 
  6180.  
  6181.  
  6182. ΓòÉΓòÉΓòÉ 13.5.2. Concatenating Tokens ΓòÉΓòÉΓòÉ
  6183.  
  6184.  
  6185. In the replacement-list, if a parameter is preceded or followed by  ##, then 
  6186. the parameter is replaced by the argument itself, without examining the 
  6187. argument for any further replacements. After all such substitutions, each ## is 
  6188. removed and the tokens on either side are concatenated together.  The newly 
  6189. formed token is then examined for further macro replacement. 
  6190.  
  6191. ## may not be either the first or last token in the replacement-list. 
  6192.  
  6193. Assuming that the following macros are defined, 
  6194.  
  6195.  
  6196.    #define first   "Piece" 
  6197.    #define last    "of Earth" 
  6198.    #define firstlast "Peace on Earth" 
  6199.    #define first1   "Peas" 
  6200.  
  6201. the following table gives a number of examples of the result of the application 
  6202. of the macro, 
  6203.  
  6204.  
  6205.    #define glue( x, y ) x ## y 
  6206.  
  6207. as shown in the first column.  For the examples that span several lines, each 
  6208. successive line of the "Result" column indicates successive expansions of the 
  6209. macros. 
  6210.  
  6211.  
  6212.    Argument        After Substitution 
  6213.    -------------------  ------------------ 
  6214.    glue( 12, 34 )     1234 
  6215.  
  6216.    glue( first, 1 )    first1 
  6217.               "Peas" 
  6218.  
  6219.    glue( first, 2 )    first2 
  6220.  
  6221.    glue( first, last )  firstlast 
  6222.               "Peace on Earth" 
  6223.  
  6224.  
  6225. ΓòÉΓòÉΓòÉ 13.5.3. Simple Argument Substitution ΓòÉΓòÉΓòÉ
  6226.  
  6227.  
  6228. In the absence of either the # or ## operators, a parameter is replaced by its 
  6229. argument.  Before this happens, however, the argument is scanned again to see 
  6230. if there are any further macro substitutions to be made, applying all of the 
  6231. above rules.  The rescanning applies only to the argument, not to any other 
  6232. tokens that might be adjacent to the argument when it replaces the parameter. 
  6233. In other words, if the last token of the argument and the first token following 
  6234. in the replacement list together form a valid macro, no substitution of that 
  6235. macro will take place. 
  6236.  
  6237. Consider the following examples, with these macro definitions in place: 
  6238.  
  6239.  
  6240.    #define f(a)  a 
  6241.    #define g(x)  (1+x) 
  6242.    #define h(s,t) s t 
  6243.    #define i(y)  2-y 
  6244.    #define xyz   printf 
  6245.    #define rcrs  rcrs+2 
  6246.  
  6247.  
  6248.    Invocation        After Substitution 
  6249.    ---------------------  -------------------- 
  6250.    f(c)           c 
  6251.  
  6252.    f(f(c))         f(c) 
  6253.                c 
  6254.  
  6255.    f(g(c))         f((1+c)) 
  6256.                (1+c) 
  6257.  
  6258.    h("hello",f("there"))  h("hello","there") 
  6259.                "hello" "there" 
  6260.  
  6261.    f(xyz)("Hello\n")    f(printf)("Hello\n") 
  6262.                printf("Hello\n") 
  6263.  
  6264.  
  6265. ΓòÉΓòÉΓòÉ 13.5.4. Rescanning for Further Replacement ΓòÉΓòÉΓòÉ
  6266.  
  6267.  
  6268. After all parameters in the replacement-list have been replaced, the resulting 
  6269. set of tokens is re-examined for any further replacement.  If, during this 
  6270. scan, an apparent invocation of the macro currently being replaced is found, it 
  6271. is not replaced.  Further invocations of the macro currently being replaced are 
  6272. not eligible for replacement until a new set of tokens from the source file, 
  6273. unrelated to the tokens resulting from the current substitution, are being 
  6274. processed. 
  6275.  
  6276. Consider these examples, using the above macro definitions: 
  6277.  
  6278.  
  6279.    Invocation   After Rescanning 
  6280.    -----------  ---------------- 
  6281.    f(g)(r)    g(r) 
  6282.           (1+r) 
  6283.  
  6284.    f(f)(r)    f(r) 
  6285.  
  6286.    h(f,(b))    f (b) 
  6287.           b 
  6288.  
  6289.    i(h(i,(b)))  i(i (b)) 
  6290.           2-i (b) 
  6291.  
  6292.    i(i (b))    i(2-b) 
  6293.           2-2-b 
  6294.  
  6295.    rcrs      rcrs+2 
  6296.  
  6297. In other words, if an apparent invocation of a macro appears, and its name 
  6298. matches the macro currently being replaced, and the apparent invocation was 
  6299. manufactured by other replacements, it is not replaced.  If, however, the 
  6300. apparent invocation comes directly from an argument to the macro replacement, 
  6301. then it is replaced. 
  6302.  
  6303. After all replacements have been done, the resulting set of tokens replaces the 
  6304. invocation of the macro in the source file, and the file is then rescanned 
  6305. starting at the replacement-list.  Any further macro invocations are then 
  6306. replaced.  However, if as a result of scanning the replacement-list with 
  6307. following tokens another apparent invocation of the macro just replaced is 
  6308. found, then that macro name is not replaced.  An invocation of the macro will 
  6309. again be replaced only when a new invocation of the macro is found, unrelated 
  6310. to the just-replaced macro. 
  6311.  
  6312. If the replacement-list of tokens resembles a preprocessor directive, the 
  6313. preprocessor will not process it. 
  6314.  
  6315. A macro definition lasts until it is undefined (with  #undef) or until the end 
  6316. of the module. 
  6317.  
  6318.  
  6319. ΓòÉΓòÉΓòÉ 13.6. More Examples of Macro Replacement ΓòÉΓòÉΓòÉ
  6320.  
  6321.  
  6322. The following examples are given in the ANSI C standard, and are presented here 
  6323. as a complete guide to the way in which macros are replaced.  The expansions 
  6324. are shown in stages to better illustrate the process. 
  6325.  
  6326. The first set of examples illustrates the rules for creating string literals 
  6327. (using the # operator) and concatenating tokens (using the ## operator).  The 
  6328. following definitions are used: 
  6329.  
  6330.  
  6331.    #define str(s)    # s 
  6332.    #define xstr(s)   str(s) 
  6333.    #define debug(s, t) printf("x" # s "= %d, x" # t "= %s", x ## s, x ## t ) 
  6334.    #define INCFILE(n)  vers ## n  /* comment */ 
  6335.    #define glue(a, b)  a ## b 
  6336.    #define xglue(a, b) glue(a, b) 
  6337.    #define HIGHLOW   "hello" 
  6338.    #define LOW     LOW ", world" 
  6339.  
  6340. The following replacements are made.  The final result shows adjacent string 
  6341. literals joined together to form a single string.  This step is not actually 
  6342. part of the preprocessor stage, but is given for clarity. 
  6343.  
  6344.  
  6345.    debug( 1, 2 ); 
  6346.     printf( "x" "1" "= %d, x" "2" "= %s", x1, x2 ); 
  6347.     printf( "x1= %d, x2= %s", x1, x2 ); 
  6348.  
  6349.  
  6350.    fputs(str(strncmp("abc\0d", "abc", '\4') /* this goes away */ 
  6351.       == 0) str(: @\n), s); 
  6352.     fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": @\n", s); 
  6353.     fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: @\n", s); 
  6354.  
  6355.  
  6356.    #include xstr(INCFILE(2).h) 
  6357.     #include xstr(vers2.h) 
  6358.     #include str(vers2.h) 
  6359.     #include "vers2.h" 
  6360.     (and then the directive is replaced by the file contents) 
  6361.  
  6362.  
  6363.    glue(HIGH, LOW) 
  6364.     HIGHLOW 
  6365.     "hello" 
  6366.  
  6367.  
  6368.    xglue(HIGH, LOW) 
  6369.     xglue(HIGH, LOW ", world") 
  6370.     glue( HIGH, LOW ", world") 
  6371.     HIGHLOW ", world" 
  6372.     "hello" ", world" 
  6373.     "hello, world" 
  6374.  
  6375. The following examples illustrate the rules for redefinition and re-examination 
  6376. of macros.  The following definitions are used: 
  6377.  
  6378.  
  6379.    #define x   3 
  6380.    #define f(a) f(x * (a)) 
  6381.    #undef  x 
  6382.    #define x   2 
  6383.    #define g   f 
  6384.    #define z   z[0] 
  6385.    #define h   g(~ 
  6386.    #define m(a) a(w) 
  6387.    #define w   0,1 
  6388.    #define t(a) a 
  6389.  
  6390. The following substitutions are made: 
  6391.  
  6392.  
  6393.    f(y+1) + f(f(z)) % t(t(g)(0) + t)(1) 
  6394.    f(x * (y+1)) + ... 
  6395.    f(2 * (y+1)) + f(f(z)) % t(t(g)(0) + t)(1) 
  6396.    ...      + f(f(x * (z))) % ... 
  6397.    ...      + f(f(2 * (z))) % ... 
  6398.    ...      + f(x * (f(2 * (z)))) % ... 
  6399.    ...      + f(2 * (f(2 * (z)))) % ... 
  6400.    ...      + f(2 * (f(2 * (z[0])))) % t(t(g)(0) + t)(1) 
  6401.    ...                  % t(g(0) + t)(1) 
  6402.    ...                  % t(f(0) + t)(1) 
  6403.    ...                  % t(f(x * (0)) + t)(1) 
  6404.    ...                  % t(f(2 * (0)) + t)(1) 
  6405.    f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1) 
  6406.  
  6407. Another example: 
  6408.  
  6409.    g(2+(3,4)-w) | h 5) & m(f)^m(m) 
  6410.    f(2+(3,4)-w) | ... 
  6411.    f(2+(3,4)-0,1) | ... 
  6412.    f(x * (2+(3,4)-0,1)) | ... 
  6413.    f(2 * (2+(3,4)-0,1)) | h 5) & ... 
  6414.    ...          | g(~ 5) & ... 
  6415.    ...          | f(~ 5) & ... 
  6416.    ...          | f(x * (~ 5)) & ... 
  6417.    ...          | f(2 * (~ 5)) & m(f)^... 
  6418.    ...                 & f(w)^... 
  6419.    ...                 & f(0,1)^... 
  6420.    ...                 & f(x * (0,1))^... 
  6421.    ...                 & f(2 * (0,1))^m(m) 
  6422.    ...                        ^m(w) 
  6423.    f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1) 
  6424.  
  6425.  
  6426. ΓòÉΓòÉΓòÉ 13.7. Redefining a Macro ΓòÉΓòÉΓòÉ
  6427.  
  6428.  
  6429. Once a macro has been defined, its definition remains until it is explicitly 
  6430. undefined (using the  #undef directive), or until the compilation of the source 
  6431. file is finished.  If a macro is undefined, then it may be redefined in some 
  6432. other (or the same) way.  If, during a macro replacement, the name of a macro 
  6433. that has been defined, undefined and then defined again is encountered, the 
  6434. current (most recent) definition of the macro is used, not the one that was in 
  6435. effect when the macro being replaced was defined. 
  6436.  
  6437. Consider this example: 
  6438.  
  6439.  
  6440.    #define MAXVAL 1000 
  6441.    #define g(x)  CheckLimit( x, MAXVAL ) 
  6442.  
  6443.    #undef  MAXVAL 
  6444.    #define MAXVAL 200 
  6445.  
  6446.    g( 10 ); 
  6447.  
  6448. This macro invocation expands to, 
  6449.  
  6450.  
  6451.    CheckLimit( 10, 200 ); 
  6452.  
  6453. A macro that has been defined may be redefined (without undefining it first) 
  6454. only if the new definition has a replacement-list that is identical to the 
  6455. original definition.  Each preprocessing token in both the original and new 
  6456. replacement lists must have the same ordering and spelling, and there must be 
  6457. the same number of tokens.  The number of spaces between tokens does not 
  6458. matter, unless one definition has no spaces, and the other has spaces. 
  6459. Comments count as one space. 
  6460.  
  6461. The following examples illustrate valid redefinitions of macros: 
  6462.  
  6463.  
  6464.    #define OBJ_LIKE   (1-1) 
  6465.    #define OBJ_LIKE   /******/ (1-1) /****/ 
  6466.    #define FN_LIKE(a)  ( a ) 
  6467.    #define FN_LIKE( a ) (   /******/ \ 
  6468.               a  /******* \ 
  6469.                 */ ) 
  6470.  
  6471. The next examples illustrate invalid redefinitions of the same macros: 
  6472.  
  6473.  
  6474.    #define OBJ_LIKE   (0) 
  6475.  
  6476.   The token sequence is different. 
  6477.  
  6478.    #define OBJ_LIKE   (1 - 1) 
  6479.  
  6480.   The spacing is different (none versus one). 
  6481.  
  6482.    #define FN_LIKE(b)  ( a ) 
  6483.  
  6484.   The parameter is a different name, and is used differently. 
  6485.  
  6486.    #define FN_LIKE(b)  ( b ) 
  6487.  
  6488.   The parameter is a different name. 
  6489.  
  6490.  
  6491. ΓòÉΓòÉΓòÉ 13.8. Changing the Line Numbering and File Name ΓòÉΓòÉΓòÉ
  6492.  
  6493.  
  6494. A directive of the form, 
  6495.  
  6496.   #line number 
  6497.  
  6498. sets the line number that the compiler associates with the current line in the 
  6499. source file to the specified number. 
  6500.  
  6501. A directive of the form, 
  6502.  
  6503.   #line number string 
  6504.  
  6505. sets the line number as above and also sets the name that the compiler 
  6506. associates with the source file that is being read to the name contained in the 
  6507. string. 
  6508.  
  6509. If the directive is not recognized as one of the two forms described above, 
  6510. then macro substitution is performed (if possible) on the tokens on the line, 
  6511. and another attempt is made.  If the directive still does not match one of the 
  6512. two forms, an error is reported. 
  6513.  
  6514.  
  6515. ΓòÉΓòÉΓòÉ 13.9. Displaying a Diagnostic Message ΓòÉΓòÉΓòÉ
  6516.  
  6517.  
  6518. A directive of the form, 
  6519.  
  6520.   #error tokens 
  6521.  
  6522. causes the compiler to display a diagnostic message containing the tokens from 
  6523. the directive. 
  6524.  
  6525.  
  6526. ΓòÉΓòÉΓòÉ 13.10. Providing Other Information to the Compiler ΓòÉΓòÉΓòÉ
  6527.  
  6528.  
  6529. A directive of the form, 
  6530.  
  6531.   #pragma tokens 
  6532.  
  6533. informs the compiler about some aspect of the compilation, in an 
  6534. implementation-defined manner. 
  6535.  
  6536.  See the User's Guide for full details of the  #pragma directive. 
  6537.  
  6538.  
  6539. ΓòÉΓòÉΓòÉ 13.11. Standard Predefined Macros ΓòÉΓòÉΓòÉ
  6540.  
  6541.  
  6542. The following macro names are reserved by the compiler: 
  6543.  
  6544.  __DATE__ 
  6545.  
  6546.   The date of translation of the source file (a string literal).  The form of 
  6547. the date is "Mmm dd yyyy" where: 
  6548.  
  6549.  Mmm 
  6550.       represents the month and is one of: 
  6551.  
  6552.  
  6553.          Jan  Feb  Mar  Apr  May  Jun 
  6554.          Jul  Aug  Sep  Oct  Nov  Dec 
  6555.  
  6556.  dd 
  6557.       is the day of the month.  The first character is a blank if the day is 
  6558.       less than 10. 
  6559.  
  6560.  yyyy 
  6561.       is the year. 
  6562.  
  6563.  If the compiler cannot determine the current date, another date is provided. 
  6564.  
  6565.   With Watcom C/16 and C/32, the current date is always available. 
  6566.  
  6567.   __FILE__ 
  6568.  
  6569.  The name of the current source file (a string literal).  The name may be 
  6570.  changed using the  #line directive. 
  6571.  
  6572.   __LINE__ 
  6573.  
  6574.  The line number of the current source line (a decimal constant).  The line 
  6575.  number may be changed using the  #line directive. 
  6576.  
  6577.   __STDC__ 
  6578.  
  6579.  The decimal constant 1, meaning that the compiler is a standard-conforming 
  6580.  implementation. 
  6581.  
  6582.   __TIME__ 
  6583.  
  6584.  The time of translation of the source file (a string literal).  The form of 
  6585.  the time is "hh:mm:ss", with leading zeros provided for values less than 10. 
  6586.  If the compiler cannot determine the current time, another time is provided. 
  6587.  
  6588.   With Watcom C/16 and C/32, the current time is always available. 
  6589.  
  6590.  Any other macros predefined by the compiler will begin with an underscore (_) 
  6591.  character.  None of the predefined macros, nor the identifier defined, may be 
  6592.  undefined (with  #undef) or redefined (with  #define). 
  6593.  
  6594.  
  6595. ΓòÉΓòÉΓòÉ 13.12. Watcom C/16 and C/32 Predefined Macros ΓòÉΓòÉΓòÉ
  6596.  
  6597.  
  6598. The Watcom C/16 and C/32 compilers also provide the following predefined macros 
  6599. for describing the memory model being used: 
  6600.  
  6601.  __COMPACT__ 
  6602.  
  6603.   The compact memory model is being used. 
  6604.  __FLAT__ 
  6605.  
  6606.   The "flat" memory model is being used for the 80386 processor.  All segment 
  6607. registers refer to the same segment. 
  6608.  __HUGE__ 
  6609.  
  6610.   The huge memory model is being used. 
  6611.  __LARGE__ 
  6612.  
  6613.   The large memory model is being used. 
  6614.  __MEDIUM__ 
  6615.  
  6616.   The medium memory model is being used. 
  6617.  __SMALL__ 
  6618.  
  6619.   The small memory model is being used. 
  6620. The Watcom C/16 and C/32 compilers also provide the following macros for 
  6621. describing the target operating system: 
  6622.  
  6623.  __DOS__ 
  6624.  
  6625.   The program is being compiled for use on a DOS operating system. 
  6626.  __NETWARE_386__ 
  6627.  
  6628.   The program is being compiled for use on the Novell Netware 386 operating 
  6629. system. 
  6630.  __NT__ 
  6631.  
  6632.   The program is being compiled for use on the Windows NT operating system. 
  6633.  __OS2__ 
  6634.  
  6635.   The program is being compiled for use on the OS/2 operating system. 
  6636.  __QNX__ 
  6637.  
  6638.   The program is being compiled for use on the QNX operating system. 
  6639.  __WINDOWS__ 
  6640.  
  6641.   The program is being compiled for use with Microsoft Windows. 
  6642.  __WINDOWS_386__ 
  6643.  
  6644.   The program is being compiled for use with Microsoft Windows, using the 
  6645. Watcom 32-bit Windows interface. 
  6646. The Watcom C/16 compiler also provides the following miscellaneous macro: 
  6647.  
  6648.  __CHEAP_WINDOWS__ 
  6649.  
  6650.   The program is being compiled for use with Microsoft Windows using the "zW" 
  6651. compiler option. 
  6652.  
  6653. The Watcom C/16 and C/32 compilers also provide the following miscellaneous 
  6654. macros: 
  6655.  __CHAR_SIGNED__ 
  6656.  
  6657.   The program is being compiled using the "j" compiler option.  The default 
  6658. char type is treated as a signed quantity. 
  6659.  __FPI__ 
  6660.  
  6661.   The program is being compiled using in-line floating point instructions. 
  6662.  __INLINE_FUNCTIONS__ 
  6663.  
  6664.   The program is being compiled using the "oi" compiler option. 
  6665.  
  6666.  __WATCOMC__ 
  6667.   The compiler being used is the Watcom C/16 or Watcom C/32 compiler.  The 
  6668. value of the macro is the version number of the compiler times 100. 
  6669.  __386__ 
  6670.  
  6671.   The program is being compiled for the 80386 processor, using the Watcom C/32 
  6672. compiler. 
  6673. The Watcom C/16 and C/32 compilers also provide the following predefined macros 
  6674. for compatibility with the Microsoft C compiler, even though most of these 
  6675. macros do not begin with an underscore (_) character: 
  6676.  
  6677.  MSDOS 
  6678.  
  6679.   The program is being compiled for use on a DOS operating system. 
  6680.  _M_IX86 
  6681.  
  6682.   The program is being compiled for a specific target architecture.  The macro 
  6683. is identically equal to 100 times the architecture compiler option value (-0, 
  6684. -1, -2, -3, -4, -5, etc.).  If "-5" (Pentium instruction timings) was specified 
  6685. as a compiler option, then the value of _M_IX86 would be 500. 
  6686.  M_I86 
  6687.  
  6688.   The program is being compiled for use on the Intel 80x86 processor. 
  6689.  M_I386 
  6690.  
  6691.   The program is being compiled for use on the Intel 80386 processor. 
  6692.  M_I86CM 
  6693.  
  6694.   The compact memory model is being used. 
  6695.  M_I86HM 
  6696.  
  6697.   The huge memory model is being used. 
  6698.  M_I86LM 
  6699.  
  6700.   The large memory model is being used. 
  6701.  M_I86MM 
  6702.  
  6703.   The medium memory model is being used. 
  6704.  M_I86SM 
  6705.  
  6706.   The small memory model is being used. 
  6707.  
  6708.  NO_EXT_KEYS 
  6709.   The program is being compiled for ANSI/ISO conformance using the "za" (no 
  6710. extended keywords) compiler option. 
  6711.  
  6712.  
  6713. ΓòÉΓòÉΓòÉ 13.13. The offsetof Macro ΓòÉΓòÉΓòÉ
  6714.  
  6715.  
  6716. The macro, 
  6717.  
  6718.  offsetof( type, member ); 
  6719.  
  6720. expands to a constant expression with type  size_t.  The value of the 
  6721. expression is the offset in bytes of member from the start of the structure 
  6722. type.  member should not be a bit-field. 
  6723.  
  6724. To use this macro, include the  <stddef.h> header. 
  6725.  
  6726.  
  6727. ΓòÉΓòÉΓòÉ 13.14. The NULL Macro ΓòÉΓòÉΓòÉ
  6728.  
  6729.  
  6730. The  NULL macro expands to a  null pointer constant, which is a value that 
  6731. indicates a pointer does not currently point to anything. 
  6732.  
  6733. It is recommended that  NULL, instead of 0, be used for null pointer constants. 
  6734.  
  6735. To use this macro, include the  <stddef.h> header. 
  6736.  
  6737.  
  6738. ΓòÉΓòÉΓòÉ 14. The Order of Translation ΓòÉΓòÉΓòÉ
  6739.  
  6740.  
  6741. This chapter describes the sequence of steps that the C compiler takes in order 
  6742. to translate a set of source files.  Most programmers do not need to thoroughly 
  6743. understand these steps, as they are intuitive.  However, occasionally it will 
  6744. be necessary to examine the sequence to solve a problem in the translation 
  6745. process. 
  6746.  
  6747. Even though the steps of translation are listed as separate phases, the 
  6748. compiler may combine them together. However, this should be transparent to the 
  6749. user. 
  6750.  
  6751. The following are the phases of translation: 
  6752.  
  6753.    1. The characters of the source file(s) are mapped to the source character 
  6754.       set.  Any end-of-line markers used in the file system are translated, as 
  6755.       necessary, to new-line characters.  Any trigraphs are replaced by the 
  6756.       appropriate single character. 
  6757.  
  6758.    2. Physical source lines are joined together wherever a line is terminated 
  6759.       by a backslash (\) character.  Effectively, the \ and the new-line 
  6760.       character are deleted, creating a longer line from that record and the 
  6761.       one following. 
  6762.  
  6763.    3. The source is broken down into preprocessing tokens and sequences of 
  6764.       "white-space" (space and tab) characters (including comments).  Each 
  6765.       token is the longest sequence of characters that can be a token.  Each 
  6766.       comment is replaced by one white-space character.  The new-line 
  6767.       characters are retained at this point. 
  6768.  
  6769.    4. Preprocessing directives are executed and macro invocations are 
  6770.       substituted.  A header named in a  #include directive is processed 
  6771.       according to rules 1 to 4. 
  6772.  
  6773.    5. Members of the source character set and escape sequences in character 
  6774.       constants and string literals are converted to single characters in the 
  6775.       execution character set. 
  6776.  
  6777.    6. Adjacent character string literal tokens and adjacent wide string literal 
  6778.       tokens are concatenated. 
  6779.  
  6780.    7. White-space characters separating tokens are discarded.  Each 
  6781.       preprocessing token is converted to a token.  The tokens are translated 
  6782.       according to the syntactic and semantic rules. 
  6783.  
  6784.  The final phase usually occurs outside of the compilation phase.  In this 
  6785.  phase, often called the  linking phase, all external object definitions are 
  6786.  resolved, and an executable program image is created.  The completed image 
  6787.  contains all the information necessary to run the program in the appropriate 
  6788.  execution environment. 
  6789.  
  6790.  
  6791. ΓòÉΓòÉΓòÉ 15. Modularity ΓòÉΓòÉΓòÉ
  6792.  
  6793.  
  6794.  
  6795. For many small programs, it is possible to write a single module which contains 
  6796. all of the C source for the program.  This module can then be compiled, linked 
  6797. and run. 
  6798.  
  6799. However, for larger applications it is not possible to maintain one module with 
  6800. everything in it.  Or, if it is technically possible, compiling such a large 
  6801. module every time a change is made to the source carries too great a time 
  6802. penalty with it.  At this point, it becomes necessary to break the program into 
  6803. pieces, or modules. 
  6804.  
  6805. Dividing a program can be done quite easily.  If the only issue is to reduce 
  6806. the size of the modules that need to be compiled, then arbitrary divisions of 
  6807. the code into modules will accomplish the goal. 
  6808.  
  6809. There are other advantages, however, to planning program modularity.  Some of 
  6810. these advantages are: 
  6811.  
  6812.      recompilation time is reduced, 
  6813.      code can be grouped into logically-connected areas, making it easier to 
  6814.       find things, 
  6815.      data structures can be hidden in one module, avoiding the temptation of 
  6816.       letting an outside piece of code "peek" into a structure it really should 
  6817.       not access directly, 
  6818.      whole modules can be rewritten or redesigned without affecting other 
  6819.       modules, 
  6820.      areas of the code that depend on the hardware or operating system can be 
  6821.       isolated for easy replacement when the program is ported.  This may 
  6822.       extend to replacing the module with an assembly language equivalent for 
  6823.       increased performance. 
  6824.  
  6825.  The following sections discuss each of these points in more detail. 
  6826.  
  6827.  
  6828. ΓòÉΓòÉΓòÉ 15.1. Reducing Recompilation Time ΓòÉΓòÉΓòÉ
  6829.  
  6830.  
  6831. As discussed above, merely breaking a program into pieces will reduce the 
  6832. amount of time spent recompiling the source.  A bug is often a simple coding 
  6833. error, requiring only a one or two line change.  Recompiling only a small 
  6834. percentage of the code and relinking will be faster than recompiling 
  6835. everything. 
  6836.  
  6837. Occasionally, recompiling all of the modules will be required.  This usually 
  6838. arises when a data structure, constant, macro or other item that is used by 
  6839. several modules is changed.  With good program design, such a change would 
  6840. occur in a header file, and all modules that include that header would be 
  6841. recompiled. 
  6842.  
  6843.  
  6844. ΓòÉΓòÉΓòÉ 15.2. Grouping Code With Related Functionality ΓòÉΓòÉΓòÉ
  6845.  
  6846.  
  6847. The best way to break programs into modules is to designate each module as 
  6848. having some overall purpose.  For example, one module may deal exclusively with 
  6849. interacting with the user.  Another module may manage a table of names, while 
  6850. yet another may process some small subset of the set of actions that may be 
  6851. performed by the program. 
  6852.  
  6853. Many of the modules then become  resource managers, and every part of the code 
  6854. that needs to do something significant with that resource must act through that 
  6855. resource manager. 
  6856.  
  6857. Using the example of the names table manager, it is likely that the manager 
  6858. will need to do things like create and delete a name entry in the table.  These 
  6859. actions would translate directly to two functions with external linkage. 
  6860.  
  6861. By dividing up a program along lines of related functionality, it is usually 
  6862. easy to know where to look when a problem is being tracked. 
  6863.  
  6864. Module names that clearly state the purpose of the module also help to locate 
  6865. things. 
  6866.  
  6867.  
  6868. ΓòÉΓòÉΓòÉ 15.3. Data Hiding ΓòÉΓòÉΓòÉ
  6869.  
  6870.  
  6871. Sometimes a module is written that has exclusive ownership of a data structure, 
  6872. such as a linked list.  All other modules that wish to access the structure 
  6873. must call a function in the module that owns it.  This technique is known as 
  6874. data hiding.  The actual data is hidden in the structure, and only the 
  6875. functional interface (also called the  procedural interface) may be used to 
  6876. access it.  The functional interface is just the set of functions provided for 
  6877. accessing the structure. 
  6878.  
  6879. The main advantage of data hiding is that the data structure may be changed 
  6880. with little or no impact on other modules.  Also, access to the structure is 
  6881. controlled, leading to fewer errors because of misuse of the structure. 
  6882.  
  6883. It is possible to have different levels of data hiding.  Complete data hiding 
  6884. occurs when no outside module has access to the structure at all.  Partial data 
  6885. hiding occurs when elements of the structure can be accessed, but the overall 
  6886. structure may not be manipulated. 
  6887.  
  6888. Note that these rules work only if the programmer respects them.  The rules are 
  6889. not enforced by the compiler.  If a module includes a header that describes the 
  6890. data structures being used by another module that wants exclusive access to the 
  6891. structures, a rule is being broken.  Whether this is good or bad depends 
  6892. entirely on the judgement of the programmer. 
  6893.  
  6894.  
  6895. ΓòÉΓòÉΓòÉ 15.3.1. Complete Data Hiding ΓòÉΓòÉΓòÉ
  6896.  
  6897.  
  6898. With complete data hiding, having a pointer to an element of the structure has 
  6899. no intrinsic value except as a parameter to the functional interface.  Getting 
  6900. or setting a value in the structure requires a function call. 
  6901.  
  6902. The advantage of this technique is that the complete data structure may be 
  6903. totally redesigned without affecting other modules.  The definitions of the 
  6904. individual structures ( struct's,  union's, arrays) may be changed and no other 
  6905. module will have to be changed, or even recompiled. 
  6906.  
  6907. The main disadvantage of complete data hiding is that even simple accesses 
  6908. require a function call, which is less efficient than just referencing a 
  6909. storage location. 
  6910.  
  6911. Function-like macros may also be used to implement complete data hiding, 
  6912. avoiding the function call but hiding the true structure of the data. 
  6913. Recompilation of all modules may be required if the data structures change. 
  6914.  
  6915.  
  6916. ΓòÉΓòÉΓòÉ 15.3.2. Partial Data Hiding ΓòÉΓòÉΓòÉ
  6917.  
  6918.  
  6919. Partial data hiding occurs when the structure itself (for example, a linked 
  6920. list) is not accessible in its entirety, but elements of the structure (an 
  6921. element of the linked list) are accessible. 
  6922.  
  6923. Using the names table manager as an example, it may be necessary to call the 
  6924. names table manager to create a name entry, but once the name is created, a 
  6925. pointer to the name is returned as the return value of the create function. 
  6926. This pointer points to a structure which is defined in a header that any module 
  6927. can include.  Therefore, the contents of an element of the data structure can 
  6928. be manipulated directly. 
  6929.  
  6930. This method is more efficient than the complete data hiding technique. 
  6931. However, when the structure used for the names table is changed, all modules 
  6932. that refer to that structure must be recompiled. 
  6933.  
  6934.  
  6935. ΓòÉΓòÉΓòÉ 15.4. Rewriting and Redesigning Modules ΓòÉΓòÉΓòÉ
  6936.  
  6937.  
  6938. With modular program design and data hiding, it is often possible to completely 
  6939. replace a module without affecting others.  This is usually only possible when 
  6940. the functional interface does not change.  With partial data hiding, the actual 
  6941. types used to implement the structure would have to remain unchanged, otherwise 
  6942. at least a recompilation would be required.  Changing a  struct, for example, 
  6943. would probably require a recompilation if only the types changed, or new 
  6944. members were added.  If, however, the names of the members changed, or some 
  6945. other fundamental change occurred, then source code changes in these other 
  6946. modules would be necessary. 
  6947.  
  6948.  
  6949. ΓòÉΓòÉΓòÉ 15.5. Isolating System Dependent Code in Modules ΓòÉΓòÉΓòÉ
  6950.  
  6951.  
  6952. System dependencies are only relevant if the program being developed is to be 
  6953. run on different computers or operating systems.  Isolating system dependent 
  6954. code is discussed more thoroughly in the chapter "Writing Portable Programs". 
  6955.  
  6956. It is quite difficult, sometimes, to identify what constitutes system dependent 
  6957. code.  The first time a program is ported to a new system, a number of problem 
  6958. areas usually arise.  These areas should be carefully examined, and the code 
  6959. that is dependent on the host environment should be isolated.  Isolation may be 
  6960. accomplished by placing the code in a separate module marked as system 
  6961. dependent, or by placing macros in the code to compile differently for the 
  6962. different systems. 
  6963.  
  6964.  
  6965. ΓòÉΓòÉΓòÉ 16. Writing Portable Programs ΓòÉΓòÉΓòÉ
  6966.  
  6967.  
  6968. Portable software is software that is written in such a way that it is 
  6969. relatively easy to get the software running on a new and different computer. 
  6970. By choosing the C language, the first step has been taken to reduce the effort 
  6971. involved in porting, but there are many other things that must be done.  Some 
  6972. of these things include: 
  6973.  
  6974.      isolating the portions of the code that depend on the hardware or 
  6975.       operating system being used, 
  6976.      being aware of what features of the C language are implementation-defined 
  6977.       and avoiding them, or taking them into account, 
  6978.      being aware of the various ranges of values that may be stored in certain 
  6979.       types, and declaring objects appropriately, 
  6980.      being aware of special features available on some systems that might be 
  6981.       useful. 
  6982.  
  6983.  No programmer can seriously expect to write a large portable program the first 
  6984.  time.  The first port of the program will take a significant period of time, 
  6985.  but the final result will be a program which is much more portable than 
  6986.  before.  Generally, each subsequent port will be easier and take less time. 
  6987.  Of course, if the new target system has a new concept that was not considered 
  6988.  in the original program design (such as a totally different user-interface), 
  6989.  then porting will necessarily take longer. 
  6990.  
  6991.  
  6992. ΓòÉΓòÉΓòÉ 16.1. Isolating System Dependent Code ΓòÉΓòÉΓòÉ
  6993.  
  6994.  
  6995. The biggest problem when trying to port a program is to uncover all the places 
  6996. in the code where an assumption about the underlying hardware or operating 
  6997. system was made, and which proves to be incorrect on the new system.  Many of 
  6998. these differences are hidden in library routines, but they can still cause 
  6999. problems. 
  7000.  
  7001. Consider, for example, the issue of distinguishing between alphabetic and 
  7002. non-alphabetic characters.  The library provides the function  isalpha which 
  7003. takes a character argument and returns a non-zero value if the character is 
  7004. alphabetic, and 0 otherwise.  Suppose a programmer, writing a FORTRAN compiler, 
  7005. wanted to know if a variable name started with the letters 'I' through 'N', in 
  7006. order to determine if it should be an integer variable.  The programmer might 
  7007. write, 
  7008.  
  7009.  
  7010.    upletter = toupper( name[0] ); 
  7011.    if( upletter >= 'I'  &&  upletter <= 'N' ) { 
  7012.      /* ... */ 
  7013.    } 
  7014.  
  7015. If the program was being developed on a machine using the ASCII character set, 
  7016. this code would work fine, since the upper case letters have 26 consecutive 
  7017. values.  However, porting the program to a machine using the EBCDIC character 
  7018. set, problems may arise because between the letters 'I' and 'J' are 7 other 
  7019. characters, including '}'.  Thus, the name "}VAR" might be considered a valid 
  7020. integer variable name, which it is not.  To solve this problem, the programmer 
  7021. could write, 
  7022.  
  7023.  
  7024.    if( isalpha( name[0] ) ) { 
  7025.      upletter = toupper( name[0] ); 
  7026.      if( upletter >= 'I'  &&  upletter <= 'N' ) { 
  7027.        /* ... */ 
  7028.      } 
  7029.    } 
  7030.  
  7031. In this case, it is not necessary to isolate the code because a relatively 
  7032. simple coding change covers both cases.  But there are cases where each system 
  7033. will require a new set of functions for some aspect of the program. 
  7034.  
  7035. Consider the user interface of a program.  If the program just displays lines 
  7036. of output to a scrolling terminal, and accepts lines of input in the same way, 
  7037. the user interface probably won't need to change between systems.  But suppose 
  7038. the program has a sophisticated user interface involving full-screen 
  7039. presentation of data, windows, and menus, and uses a mouse and the keyboard for 
  7040. input.  In the absence of standards for such interfaces, it is quite likely 
  7041. that each system will require a customized set of functions.  Here is where 
  7042. program portability can become an art. 
  7043.  
  7044. An approach to this problem is to completely isolate the user interface code of 
  7045. the program.  The processing of data occurs independently of what appears on 
  7046. the screen.  At the completion of processing, a function is called which 
  7047. updates the screen.  This code may or may not be portable, depending on how 
  7048. many layers of functions are built between the physical screen and the generic 
  7049. program.  At a level fairly close to the screen hardware, a set of functions 
  7050. should be defined which perform the set of actions that the program needs.  The 
  7051. full set of functions will depend extensively on the requirements of the 
  7052. program, but they should be functions that can reasonably be expected to work 
  7053. on any system to which the program will eventually be ported. 
  7054.  
  7055. Other areas that may be system dependent include: 
  7056.  
  7057.      The behavior and capabilities of devices, including printers.  Some 
  7058.       printers support multiple fonts, expanded and compressed characters, 
  7059.       underlining, graphics, and so on.  Others support only relatively simple 
  7060.       text output. 
  7061.      Accessing memory regions outside of normally addressable storage.  A good 
  7062.       example is the Intel 80x86 family of processors.  With the Watcom C/16 
  7063.       16-bit compiler, the addressable storage is 1024 kilobytes, but a 16-bit 
  7064.       address can only address 64 kilobytes.  Special steps must be taken when 
  7065.       compiling in order to address the full storage space.  Many compilers for 
  7066.       the 8086, including Watcom C/16 and C/32, introduce new keywords that 
  7067.       describe pointer types beyond the 16-bit pointer. 
  7068.      Code that has been written in assembly language for speed.  As code 
  7069.       generation technology advances, assembly language code should become less 
  7070.       necessary. 
  7071.      Code that accesses some special feature of the system.  As an example, 
  7072.       many systems provide the ability to temporarily exit to the operating 
  7073.       system level, and later return to the program.  The method of doing this 
  7074.       varies between systems, and the requirements of the program often change 
  7075.       as well. 
  7076.      Handling the command line parameters.  While C breaks the list of 
  7077.       parameters down into strings, the interpretation of those strings may 
  7078.       vary between systems.  A program probably should attempt to conform to 
  7079.       any conventions of the system on which it is being run. 
  7080.      Handling other startup requirements.  Allocation of memory, initializing 
  7081.       devices, and so on, may be done at this point. 
  7082.  
  7083.  
  7084. ΓòÉΓòÉΓòÉ 16.2. Beware of Long External Names ΓòÉΓòÉΓòÉ
  7085.  
  7086.  
  7087. According the C Language standard, a compiler may limit external names 
  7088. (functions and global objects) to 6 significant characters.  This limitation is 
  7089. often imposed by the "linking" stage of the development process. 
  7090.  
  7091. In practice, most systems allow many more significant characters.  However, the 
  7092. developer of a portable program should be aware of the potential for porting 
  7093. the program to a system that has a small limit, and name external objects 
  7094. accordingly. 
  7095.  
  7096. If the developer must port a program with many names that are not unique within 
  7097. the limitations imposed by the target development system, the preprocessor may 
  7098. be used to provide shorter unique names for all objects.  Note that this method 
  7099. may seriously impair any symbolic debugging facilities provided by the 
  7100. development system. 
  7101.  
  7102.  
  7103. ΓòÉΓòÉΓòÉ 16.3. Avoiding Implementation-Defined Behavior ΓòÉΓòÉΓòÉ
  7104.  
  7105.  
  7106. Several aspects of the code generated by the C compiler depend on the behavior 
  7107. of the particular C compiler being used.  A portable program should avoid these 
  7108. where possible, and take them into consideration where they can't be avoided. 
  7109. It may be possible to use macros to avoid some of these issues. 
  7110.  
  7111. An important behavior that varies between systems is the number of characters 
  7112. of external objects and functions that the system recognizes.  The standard 
  7113. states that a system must recognize a minimum of 6 characters, although future 
  7114. standards may remove or extend this limit.  Most systems allow more than 6 
  7115. characters, but several recognize only 8 characters.  For true portability, a 
  7116. function or object that has external linkage should be kept unique in the first 
  7117. 6 characters.  Sometimes this requires ingenuity when thinking of names, but 
  7118. developing a system for naming objects goes a long way towards fitting within 
  7119. this restriction.  The goal, of course, is to still have meaningful object 
  7120. names.  If all systems that will eventually be used have a higher limit, then 
  7121. the programmer may decide to go past the 6 character limit.  If a port is done 
  7122. to a system with the 6 character limit, a lot of source changes may be 
  7123. required. 
  7124.  
  7125. To solve this problem, macros could be used to map the actual function names 
  7126. into more cryptic names that fit within the 6 character limit.  This technique 
  7127. may have the adverse affect of making debugging very difficult because many of 
  7128. the function and object names will not be the same as contained in the source 
  7129. code. 
  7130.  
  7131. Another implementation-defined behavior occurs with the type  char.  The 
  7132. standard does not impose a  signed or  unsigned interpretation on the type.  A 
  7133. program that uses an object of type  char that requires the values to be 
  7134. interpreted as signed or unsigned should explicitly declare the object with 
  7135. that type. 
  7136.  
  7137.  
  7138. ΓòÉΓòÉΓòÉ 16.4. Ranges of Types ΓòÉΓòÉΓòÉ
  7139.  
  7140.  
  7141. The range of an object of type  int is not specified by the standard, except to 
  7142. say that the minimum range is -32767 to 32767.  If an object is to contain an 
  7143. integer value, then thought should be given as to whether or not this range of 
  7144. values is acceptable on all systems.  If the object is a counter that will 
  7145. never go outside the range 0 to 255, then the range will be adequate.  However, 
  7146. if the object is to contain values that may exceed this range, then a  long int 
  7147. may be required. 
  7148.  
  7149. The same argument applies to objects with type  float.  It may make more sense 
  7150. to declare them with type  double. 
  7151.  
  7152. When converting floating-point numbers to integers, the rounding behavior can 
  7153. also vary between compilers and systems.  If it is important to know how the 
  7154. rounding behaves, then the program should refer to the macro FLT_ROUNDS 
  7155. (defined in the header  <float.h>), which is a value describing the type of 
  7156. rounding performed. 
  7157.  
  7158.  
  7159. ΓòÉΓòÉΓòÉ 16.5. Special Features ΓòÉΓòÉΓòÉ
  7160.  
  7161.  
  7162. Some systems provide special features that may or may not exist on other 
  7163. systems.  For example, many provide the ability to exit to the operating 
  7164. system, run some other programs, then return to the program that was running. 
  7165. Other systems may not provide this ability.  In an interactive program, this 
  7166. feature may be very useful.  By isolating the code that deals with this 
  7167. feature, a program may remain easily portable.  On the systems that don't 
  7168. support this feature, it may be necessary to provide a stub function which does 
  7169. nothing, or displays a message. 
  7170.  
  7171.  
  7172. ΓòÉΓòÉΓòÉ 16.6. Using the Preprocessor to Aid Portability ΓòÉΓòÉΓòÉ
  7173.  
  7174.  
  7175. The preprocessor is particularly useful for providing alternate code sequences 
  7176. to deal with portability issues. Conditional compilation provided by the  #if 
  7177. directive allows the insertion of differing code sequences depending on some 
  7178. criteria.  Defining a set of macros which describe the various systems, and 
  7179. another macro that selects a particular system, makes it easy to add 
  7180. system-dependent code. 
  7181.  
  7182. For example, consider the macros, 
  7183.  
  7184.  
  7185.    #define OS_DOS   0 
  7186.    #define OS_CMS   1 
  7187.    #define OS_MVS   2 
  7188.    #define OS_OS2   3 
  7189.    #define OS_QNX   4 
  7190.  
  7191.    #define HW_IBMPC  0 
  7192.    #define HW_IBM370 1 
  7193.  
  7194.    #define PR_i8086  0 
  7195.    #define PR_370   1 
  7196.  
  7197. They describe a set of operating systems (OS), hardware (HW) and processors 
  7198. (PR), which together can completely describe a computer and its operating 
  7199. system.  If the program was being ported to a IBM 370 running the MVS operating 
  7200. system, then it could include a header defining the macros above, and declare 
  7201. the macros, 
  7202.  
  7203.  
  7204.    #define OPSYS   OS_MVS 
  7205.    #define HARDWARE  HW_IBM370 
  7206.    #define PROCESSOR PR_370 
  7207.  
  7208. The following code sequence would include the call only if the program was 
  7209. being compiled for a 370 running MVS: 
  7210.  
  7211.  
  7212.    #if HARDWARE == HW_IBM370  &&  OPSYS == OS_MVS 
  7213.      DoMVSStuff( x, y ); 
  7214.    #endif 
  7215.  
  7216. In other cases, code may be conditionally compiled based only on the hardware 
  7217. regardless of the operating system, or based only on the operating system 
  7218. regardless of the hardware or processor. 
  7219.  
  7220. This technique may work well if used in moderation.  However, a module that is 
  7221. filled with these directives becomes difficult to read, and that module becomes 
  7222. a candidate for being rewritten entirely for each system. 
  7223.  
  7224.  
  7225. ΓòÉΓòÉΓòÉ 17. Avoiding Common Pitfalls ΓòÉΓòÉΓòÉ
  7226.  
  7227.  
  7228. Even though a C program is much easier to write than the corresponding assembly 
  7229. language program, there are a few areas where most programmers make mistakes, 
  7230. and spend a great deal of time staring at the code trying to figure out why the 
  7231. program doesn't work. 
  7232.  
  7233. The bugs that are the most difficult to find often occur when the compiler 
  7234. doesn't give an error or warning, but the code generated is not what the 
  7235. programmer expected.  After a great deal of looking, the programmer spots the 
  7236. error and realizes that the compiler generated the correct code, but it wasn't 
  7237. the code that was wanted. 
  7238.  
  7239. Some compilers, including Watcom C/16 and C/32, have optional checking for 
  7240. common errors built into them, providing warnings when these conditions arise. 
  7241. It is probably better to eliminate the code that causes the warning than to 
  7242. turn off the checking done by the compiler. 
  7243.  
  7244. The following sections illustrate several common pitfalls, and discuss how to 
  7245. avoid them. 
  7246.  
  7247.  
  7248. ΓòÉΓòÉΓòÉ 17.1. Assignment Instead of Comparison ΓòÉΓòÉΓòÉ
  7249.  
  7250.  
  7251. The code fragment, 
  7252.  
  7253.  
  7254.    chr = getc(); 
  7255.    if( chr = 'a' ) { 
  7256.      printf( "letter is 'a'\n" ); 
  7257.    } else { 
  7258.      printf( "letter is not 'a'\n" ); 
  7259.    } 
  7260.  
  7261. will never print the message letter is not 'a', regardless of the value of chr. 
  7262.  
  7263. The problem occurs in the second line of the example.  The statement, 
  7264.  
  7265.  
  7266.    if( chr = 'a' ) { 
  7267.  
  7268. assigns the character constant 'a' to the object chr.  If the value of chr is 
  7269. not zero, then the statement that is the subject of the  if is executed. 
  7270.  
  7271. The value of the constant 'a' is never zero, so the first part of the  if will 
  7272. always be executed.  The second part might as well not even be there! 
  7273.  
  7274. Of course, the correct way to code the second line is, 
  7275.  
  7276.  
  7277.    if( chr == 'a' ) { 
  7278.  
  7279. changing the = to ==.  This statement says to compare the value of chr against 
  7280. the constant 'a' and to execute the subject of the  if only if the values are 
  7281. the same. 
  7282.  
  7283. Using one equal sign (assignment) instead of two (comparison for equality) is a 
  7284. common errors made by programmers, often by those who are familiar with 
  7285. languages such as Pascal, where the single = means "comparison for equality". 
  7286.  
  7287.  
  7288. ΓòÉΓòÉΓòÉ 17.2. Unexpected Operator Precedence ΓòÉΓòÉΓòÉ
  7289.  
  7290.  
  7291. The code fragment, 
  7292.  
  7293.  
  7294.    if( chr = getc() != EOF ) { 
  7295.      printf( "The value of chr is %d\n", chr ); 
  7296.    } 
  7297.  
  7298. will always print 1, as long as end-of-file is not detected in  getc.  The 
  7299. intention was to assign the value from  getc to chr, then to test the value 
  7300. against EOF. 
  7301.  
  7302. The problem occurs in the first line, which says to call the library function 
  7303. getc.  The return value from  getc (an integer value representing a character, 
  7304. or EOF if end-of-file is detected), is compared against EOF, and if they are 
  7305. not equal (it's not end-of-file), then 1 is assigned to the object chr. 
  7306. Otherwise, they are equal and 0 is assigned to chr.  The value of chr is, 
  7307. therefore, always 0 or 1. 
  7308.  
  7309. The correct way to write this code fragment is, 
  7310.  
  7311.  
  7312.    if( (chr = getc()) != EOF ) { 
  7313.      printf( "The value of chr is %d\n", chr ); 
  7314.    } 
  7315.  
  7316. The extra parentheses force the assignment to occur first, and then the 
  7317. comparison for equality is done. 
  7318.  
  7319. Note:  doing assignment inside the controlling expression of loop or selection 
  7320. statements is not a good programming practice.  These expressions tend to be 
  7321. difficult to read, and problems such as using = instead of == are more 
  7322. difficult to detect when, in some cases, = is desired. 
  7323.  
  7324.  
  7325. ΓòÉΓòÉΓòÉ 17.3. Delayed Error From Included File ΓòÉΓòÉΓòÉ
  7326.  
  7327.  
  7328. Suppose the source file mytypes.h contained the line, 
  7329.  
  7330.  
  7331.    typedef int COUNTER 
  7332.  
  7333. and the main source file being compiled started with, 
  7334.  
  7335.  
  7336.    #include "mytypes.h" 
  7337.  
  7338.    extern int main( void ) 
  7339.    /*********************/ 
  7340.    { 
  7341.      COUNTER x; 
  7342.    /* ... */ 
  7343.    } 
  7344.  
  7345. Attempting to compile the main source file would report a message such as, 
  7346.  
  7347.  
  7348.    Error! Expecting ';' but found 'extern' on line 3 
  7349.  
  7350. Examining the main source file does not show any problem.  The problem actually 
  7351. occurs in the included source file, since the  typedef statement does not end 
  7352. with a semi-colon.  It is this semi-colon that the compiler is expecting to 
  7353. find.  The next token found is the extern keyword, so the error is reported in 
  7354. the main source file. 
  7355.  
  7356. When an error occurs shortly after an  #include directive, and the error is not 
  7357. readily apparent, the error may actually be caused by something in the included 
  7358. file. 
  7359.  
  7360.  
  7361. ΓòÉΓòÉΓòÉ 17.4. Extra Semi-colon in Macros ΓòÉΓòÉΓòÉ
  7362.  
  7363.  
  7364. The next code fragment illustrates a common error when using the preprocessor 
  7365. to define constants: 
  7366.  
  7367.  
  7368.    #define MAXVAL 10; 
  7369.  
  7370.    /* ... */ 
  7371.  
  7372.    if( value >= MAXVAL ) break; 
  7373.  
  7374. The compiler will report an error message like, 
  7375.  
  7376.  
  7377.    Error! Expecting ')' but found ';' on line 372 
  7378.  
  7379. The problem is easily spotted when the macro substitution is performed on line 
  7380. 372.  Using the definition for MAXVAL, the substituted version of line 372 
  7381. reads, 
  7382.  
  7383.  
  7384.    if( value >= 10; ) break; 
  7385.  
  7386. The semi-colon (;) in the definition was not treated as an end-of-statement 
  7387. indicator as expected, but was included in the definition of the macro 
  7388. (manifest constant) MAXVAL.  The substitution then results in a semi-colon 
  7389. being placed in the middle of the controlling expression, which yields the 
  7390. syntax error. 
  7391.  
  7392.  
  7393. ΓòÉΓòÉΓòÉ 17.5. The Dangling else ΓòÉΓòÉΓòÉ
  7394.  
  7395.  
  7396. In the code fragment, 
  7397.  
  7398.  
  7399.    if( value1 > 0 ) 
  7400.      if( value2 > 0 ) 
  7401.        printf( "Both values greater than zero\n" ); 
  7402.    else 
  7403.      printf( "value1 is not greater than zero\n" ); 
  7404.  
  7405. suppose value1 has the value 3, while value2 has the value -7.  This code 
  7406. fragment will cause the message, 
  7407.  
  7408.  
  7409.    value1 is not greater than zero 
  7410.  
  7411. to be displayed. 
  7412.  
  7413. The problem occurs because of the  else.  The program is indented incorrectly 
  7414. according to the syntax that the compiler will determine from the statements. 
  7415. The correct indentation should clearly show where the error lies: 
  7416.  
  7417.  
  7418.    if( value1 > 0 ) 
  7419.      if( value2 > 0 ) 
  7420.        printf( "Both values greater than zero\n" ); 
  7421.      else 
  7422.        printf( "value1 is not greater than zero\n" ); 
  7423.  
  7424. The  else belongs to the second  if, not the first.  Whenever there is more 
  7425. than one  if statement without braces and without an  else statement, the next 
  7426. else will be matched to the most recent  if statement. 
  7427.  
  7428. This code fragment clearly illustrates the usefulness of using braces to state 
  7429. program structure.  The above example would be (correctly) written as, 
  7430.  
  7431.  
  7432.    if( value1 > 0 ) { 
  7433.      if( value2 > 0 ) { 
  7434.        printf( "Both values greater than zero\n" ); 
  7435.      } 
  7436.    } else { 
  7437.      printf( "value1 is not greater than zero\n" ); 
  7438.    } 
  7439.  
  7440.  
  7441. ΓòÉΓòÉΓòÉ 17.6. Missing break in switch Statement ΓòÉΓòÉΓòÉ
  7442.  
  7443.  
  7444. In the code fragment, 
  7445.  
  7446.  
  7447.    switch( value ) { 
  7448.     case 1: 
  7449.      printf( "value is 1\n" ); 
  7450.     default: 
  7451.      printf( "value is not 1\n" ); 
  7452.    } 
  7453.  
  7454. if value is 1, the following output will appear: 
  7455.  
  7456.  
  7457.    value is 1 
  7458.    value is not 1 
  7459.  
  7460. This unexpected behavior occurs because, when value is 1, the  switch causes 
  7461. control to be passed to the case 1:  label, where the first  printf occurs. 
  7462. Then the default label is encountered.  Labels are ignored in execution, so the 
  7463. next statement executed is the second  printf. 
  7464.  
  7465. To correct this example, it should be changed to, 
  7466.  
  7467.  
  7468.    switch( value ) { 
  7469.     case 1: 
  7470.      printf( "value is 1\n" ); 
  7471.      break; 
  7472.     default: 
  7473.      printf( "value is not 1\n" ); 
  7474.    } 
  7475.  
  7476. The  break statement causes control to be passed to the statement following the 
  7477. closing brace of the  switch statement. 
  7478.  
  7479.  
  7480. ΓòÉΓòÉΓòÉ 17.7. Side-effects in Macros ΓòÉΓòÉΓòÉ
  7481.  
  7482.  
  7483. In the code fragment, 
  7484.  
  7485.  
  7486.    #define endof( ptr ) ptr + strlen( ptr ) 
  7487.    /* ... */ 
  7488.    endptr = endof( ptr++ ); 
  7489.  
  7490. the statement gets expanded to, 
  7491.  
  7492.  
  7493.    endptr = ptr++ + strlen( ptr++ ); 
  7494.  
  7495. The parameter ptr gets incremented twice, rather than once as expected. 
  7496.  
  7497. The only way to avoid this pitfall is to be aware of what macros are being 
  7498. used, and to be careful when using them.  Several library functions may be 
  7499. implemented as macros on some systems.  These functions include, 
  7500.  
  7501.  
  7502.    getc    putc 
  7503.    getchar   putchar 
  7504.  
  7505. The ANSI standard requires that documentation states which library functions 
  7506. evaluate their arguments more than once. 
  7507.  
  7508.  
  7509. ΓòÉΓòÉΓòÉ 18. Programming Style ΓòÉΓòÉΓòÉ
  7510.  
  7511.  
  7512. Programming style is as individual as a person's preference in clothing. 
  7513. Unfortunately, just as some programmers wouldn't win a fashion contest, some 
  7514. code has poor style.  This code is usually easy to spot, because it is 
  7515. difficult to understand. 
  7516.  
  7517. Good programming style can make the difference between programs that are easy 
  7518. to debug and modify, and those that you just want to avoid. 
  7519.  
  7520. There are a number of aspects to programming style.  There is no perfect style 
  7521. that is altogether superior to all others.  Each programmer must find a style 
  7522. that makes him or her comfortable.  The intention is to write code that is easy 
  7523. to read and understand, not to try to stump the next person who has to fix a 
  7524. problem in the code. 
  7525.  
  7526. Good programming style will also lead to less time spent writing a program, and 
  7527. certainly less time spent debugging or modifying it. 
  7528.  
  7529. The following sections discuss various aspects of programming style.  They 
  7530. reflect the author's own biases, but they are biases based on years of hacking 
  7531. his way through code, mostly good and some bad, and much of it his own! 
  7532.  
  7533.  
  7534. ΓòÉΓòÉΓòÉ 18.1. Consistency ΓòÉΓòÉΓòÉ
  7535.  
  7536.  
  7537. Perhaps the most important aspect of style is  consistency.  Try, as much as 
  7538. possible, to use the same rules throughout the entire program.  Having a mixed 
  7539. bag of styles within one program will confuse even the best of programmers 
  7540. trying to decipher the code. 
  7541.  
  7542. If more than one programmer is involved in the project, it may be appropriate, 
  7543. before the first line of code is written, to discuss general rules of style. 
  7544. Some rules are more important than others.  Make sure everyone understands the 
  7545. rules, and are encouraged to follow them. 
  7546.  
  7547.  
  7548. ΓòÉΓòÉΓòÉ 18.2. Case Rules for Object and Function Names ΓòÉΓòÉΓòÉ
  7549.  
  7550.  
  7551. When examining a piece of code, the scope of an object is sometimes difficult 
  7552. to determine.  One needs to examine the declarations of objects within the 
  7553. function, then those declared outside of any functions, then those declared 
  7554. included from other source files.  If no strict rules of naming objects are 
  7555. followed, each place will need to be laboriously searched each time. 
  7556.  
  7557. Using mixed case object names, with strict rules, can make the job much easier. 
  7558. It does not matter what rules are established, as long as the rules are 
  7559. consistently applied throughout the program. 
  7560.  
  7561. Consider the following sample set of rules, used throughout this book: 
  7562.  
  7563.    1. objects declared within a function with automatic storage duration are 
  7564.       entirely in lower case, 
  7565.  
  7566.  
  7567.          int     x, counter, limit; 
  7568.          float    save_global; 
  7569.          struct s * sptr; 
  7570.  
  7571.    2. objects with static storage duration (global objects) start with an upper 
  7572.       case letter, and words or word fragments also start with upper case, 
  7573.  
  7574.  
  7575.          static int    TotalCount; 
  7576.          extern float   GlobalAverage; 
  7577.          static struct s SepStruct; 
  7578.  
  7579.    3. function names start with an upper case letter, and words or word 
  7580.       fragments also start with upper case, (distinguishable from global 
  7581.       objects by the left parenthesis), 
  7582.  
  7583.  
  7584.          extern int   TrimLength( char * ptr, int len ); 
  7585.          static field * CreateField( char * name ); 
  7586.  
  7587.    4. all constants are entirely in upper case. 
  7588.  
  7589.  
  7590.          #define FIELD_LIMIT 500 
  7591.          #define BUFSIZE   32 
  7592.  
  7593.          enum { INVALID, HELP, ADD, DELETE, REPLACE }; 
  7594.  
  7595.    5. all  typedef tags are in upper case. 
  7596.  
  7597.  
  7598.          typedef struct { 
  7599.            float real; 
  7600.            float imaginary; 
  7601.          } COMPLEX; 
  7602.  
  7603.  Thus, the storage duration and scope of each identifier can be determined 
  7604.  without regard to context.  Consider this program fragment: 
  7605.  
  7606.  
  7607.       chr = ReadChar(); 
  7608.       if( chr != EOF ) { 
  7609.         GlbChr = chr; 
  7610.       } 
  7611.  
  7612.  Using the above rules, 
  7613.  
  7614.    1. ReadChar is a function, 
  7615.  
  7616.    2. chr is an object with automatic storage duration defined within the 
  7617.       current function, 
  7618.  
  7619.    3. EOF is a constant, 
  7620.  
  7621.    4. GlbChr is an object with static storage duration. 
  7622.  
  7623.  Note:  the library functions do not use mixed case names.  Also, the function 
  7624.  main does not begin with an upper case M.  Using the above coding style, 
  7625.  library functions would stand out from other functions because of the 
  7626.  letter-case difference. 
  7627.  
  7628.  
  7629. ΓòÉΓòÉΓòÉ 18.3. Choose Appropriate Names ΓòÉΓòÉΓòÉ
  7630.  
  7631.  
  7632. The naming of objects can be critical to the ease with which bugs can be found, 
  7633. or changes can be made.  Using object names such as linecount, columns and 
  7634. rownumber will make the program more readable.  Of course, short forms will 
  7635. creep into the code (few programmers like to type more than is really 
  7636. necessary), but they should be used judiciously. 
  7637.  
  7638. Consistency of naming also helps to make the code more readable.  If a 
  7639. structure is used throughout the program, and many different routines need a 
  7640. pointer to that structure, then the name of each object that points to it could 
  7641. be made the same.  Using the example of a symbol table, the object name symptr 
  7642. might be used everywhere to mean "pointer to a symbol structure".  A programmer 
  7643. seeing that object will automatically know what it is declared to be. 
  7644.  
  7645. Appropriate function names are also very important.  Names such as DoIt, while 
  7646. saving the original programmer from trying to think of a good name, make it 
  7647. more difficult for the next programmer to figure out what is going on. 
  7648.  
  7649.  
  7650. ΓòÉΓòÉΓòÉ 18.4. Indent to Emphasize Structure ΓòÉΓòÉΓòÉ
  7651.  
  7652.  
  7653. The following is a valid function: 
  7654.  
  7655.  
  7656.    static void BubbleSort( int list[], int n ) 
  7657.    /**********************************/ { int index1 
  7658.    = 0; int index2; int temp; if( n < 2 )return; do { 
  7659.    index2 = index1 + 1; do { if( list[ index1 ] > 
  7660.    list[ index2 ] ) { temp = list[ index1 ]; list[ 
  7661.    index1 ] = list[ index2 ]; list[ index2 ] = temp; 
  7662.    } } while( ++index2 < n ); } while( ++index1 < n-1 
  7663.    ); } 
  7664.  
  7665. (The compiler will know that it's valid, but the programmer would find it 
  7666. difficult to validate.) Here is the same function, but using indenting to 
  7667. clearly illustrate the function structure: 
  7668.  
  7669.  
  7670.    static void BubbleSort( int list[], int n ) 
  7671.    /*****************************************/ 
  7672.     { 
  7673.      int index1 = 0; 
  7674.      int index2; 
  7675.      int temp; 
  7676.  
  7677.      if( n < 2 )return; 
  7678.      do { 
  7679.        index2 = index1 + 1; 
  7680.        do { 
  7681.          if( list[ index1 ] > list[ index2 ] ) { 
  7682.            temp = list[ index1 ]; 
  7683.            list[ index1 ] = list[ index2 ]; 
  7684.            list[ index2 ] = temp; 
  7685.          } 
  7686.        } while( ++index2 < n ); 
  7687.      } while( ++index1 < n-1 ); 
  7688.     } 
  7689.  
  7690. Generally, it is good practice to indent each level of code by a consistent 
  7691. amount, for example 4 spaces.  Thus, the subject of an  if statement is always 
  7692. indented 4 spaces inside the  if.  In this manner, all loop and selection 
  7693. statements will stand out, making it easier to determine when the statements 
  7694. end. 
  7695.  
  7696. The following are some recommended patterns to use when indenting statements. 
  7697. These patterns have been used throughout the book. 
  7698.  
  7699.  
  7700.    int Fn( void ) 
  7701.    /************/ 
  7702.    { 
  7703.      /* indent 4 */ 
  7704.    } 
  7705.  
  7706.    if( condition ) { 
  7707.      /* indent 4 */ 
  7708.    } else { 
  7709.      /* indent 4 */ 
  7710.    } 
  7711.  
  7712.    if( condition ) { 
  7713.      /* indent 4 */ 
  7714.    } else if( condition ) { 
  7715.      /* indent 4 from first if */ 
  7716.      if( condition ) { 
  7717.        /* indent 4 from nearest if */ 
  7718.      } 
  7719.    } else { 
  7720.      /* indent 4 from first if */ 
  7721.    } 
  7722.  
  7723.    switch( condition ) { 
  7724.     case VALUE: 
  7725.      /* indent 4 from switch */ 
  7726.     case VALUE: 
  7727.     default: 
  7728.    } 
  7729.  
  7730.    do { 
  7731.      /* indent 4 */ 
  7732.    while( condition ); 
  7733.  
  7734.    while( condition ) { 
  7735.      /* indent 4 */ 
  7736.    } 
  7737.  
  7738.    for( a; b; c ) { 
  7739.      /* indent 4 */ 
  7740.    } 
  7741.  
  7742. Two other popular indenting styles are, 
  7743.  
  7744.  
  7745.    if( condition ) 
  7746.     { 
  7747.      statement 
  7748.     } 
  7749.  
  7750. and, 
  7751.  
  7752.  
  7753.    if( condition ) 
  7754.    { 
  7755.      statements 
  7756.    } 
  7757.  
  7758. It is not important which style is used.  However, a consistent style is an 
  7759. asset. 
  7760.  
  7761.  
  7762. ΓòÉΓòÉΓòÉ 18.5. Visually Align Object Declarations ΓòÉΓòÉΓòÉ
  7763.  
  7764.  
  7765. A lengthy series of object declarations can be difficult to read if care is not 
  7766. taken to improve the readability.  Consider the declarations, 
  7767.  
  7768.  
  7769.    struct flentry *flptr; 
  7770.    struct fldsym *sptr; 
  7771.    char *bufptr,*wsbuff; 
  7772.    int length; 
  7773.  
  7774. Now, consider the same declarations, but with some visual alignment done: 
  7775.  
  7776.  
  7777.    struct flentry * flptr; 
  7778.    struct fldsym  * sptr; 
  7779.    char      * bufptr; 
  7780.    char      * wsbuff; 
  7781.    int        length; 
  7782.  
  7783. It is easier to scan a list of objects when their names all begin in the same 
  7784. column. 
  7785.  
  7786.  
  7787. ΓòÉΓòÉΓòÉ 18.6. Keep Functions Small ΓòÉΓòÉΓòÉ
  7788.  
  7789.  
  7790. A function that is several hundred lines long can be difficult to comprehend, 
  7791. especially if it is being looked at on a terminal, which might only have 25 
  7792. lines.  Large functions also tend to have a lot of nesting of program 
  7793. structures, making it difficult to follow the logic. 
  7794.  
  7795. A function that fits entirely within the terminal display can be studied and 
  7796. understood more easily.  Program constructs don't get as complicated.  Large 
  7797. functions often can be broken up into smaller functions which are easier to 
  7798. maintain. 
  7799.  
  7800.  
  7801. ΓòÉΓòÉΓòÉ 18.7. Use static for Most Functions ΓòÉΓòÉΓòÉ
  7802.  
  7803.  
  7804. Most functions do not need to be called from routines outside of the current 
  7805. module.  Yet, if the keyword  static is not used in the function declaration, 
  7806. then the function is automatically given  external linkage.  This can lead to a 
  7807. proliferation of external symbols, which may cause naming conflicts.  Also, 
  7808. some linking programs may impose limitations. 
  7809.  
  7810. Only those functions that must have external linkage should be made external. 
  7811. All other definitions of functions should start with the keyword  static. 
  7812.  
  7813. It also is a good idea to start definitions for external functions with the 
  7814. keyword  extern, even though it is the default case. 
  7815.  
  7816.  
  7817. ΓòÉΓòÉΓòÉ 18.8. Group Static Objects Together ΓòÉΓòÉΓòÉ
  7818.  
  7819.  
  7820. Static objects that are declared outside of any function definition, and are 
  7821. used throughout the module, generally should be declared together, for example 
  7822. before the definition of the first function.  Placing the declarations of these 
  7823. objects near the beginning of the module makes them easier to find. 
  7824.  
  7825.  
  7826. ΓòÉΓòÉΓòÉ 18.9. Do Not Reuse the Names of Static Objects ΓòÉΓòÉΓòÉ
  7827.  
  7828.  
  7829. If an object with static storage duration exists in one module, but has 
  7830. internal linkage, then another object with the same name should not be declared 
  7831. in another module.  The programmer may confuse them. 
  7832.  
  7833. Even more importantly, if an object exists with  external linkage, a module 
  7834. should not declare another object with the same name with internal linkage. 
  7835. This second object will overshadow the first within the module, but the next 
  7836. programmer to look at the code will likely be confused. 
  7837.  
  7838.  
  7839. ΓòÉΓòÉΓòÉ 18.10. Use Included Files to Organize Structures ΓòÉΓòÉΓòÉ
  7840.  
  7841.  
  7842. Included source files can be used to organize data structures and related 
  7843. information.  They should be used when the same structure is needed in 
  7844. different modules.  They should even be considered when the structure is used 
  7845. only in one place. 
  7846.  
  7847. Generally, each included source file should contain structures and related 
  7848. information for one aspect of the program.  For example, a file that describes 
  7849. a symbol table might contain the actual structures or other types that are 
  7850. required, along with any manifest constants that are useful. 
  7851.  
  7852.  
  7853. ΓòÉΓòÉΓòÉ 18.11. Use Function Prototypes ΓòÉΓòÉΓòÉ
  7854.  
  7855.  
  7856. Function prototypes are very useful for eliminating common errors when calling 
  7857. functions.  If every function in a program is prototyped (and the prototypes 
  7858. are used), then it is difficult to pass the wrong number or types of arguments, 
  7859. or to misinterpret the return value. 
  7860.  
  7861. Using the symbol table example, the included source file that describes the 
  7862. symbol table structure and any related global objects or constant values could 
  7863. also contain the function prototypes for the functions used to access the 
  7864. table.  Another approach is to have separate source files containing the 
  7865. function prototypes, possibly using a different naming convention for the file. 
  7866. For example, 
  7867.  
  7868.  
  7869.    #include "symbols.h" 
  7870.    #include "symbols.fn" 
  7871.  
  7872. would include the structures and related values from symbols.h, and the 
  7873. function prototypes from symbols.fn. 
  7874.  
  7875.  
  7876. ΓòÉΓòÉΓòÉ 18.12. Do Not Do Too Much In One Statement ΓòÉΓòÉΓòÉ
  7877.  
  7878.  
  7879. In the same manner that a big function that does too much can be confusing, so 
  7880. too can a long statement.  Historically, a programmer might combine many 
  7881. operations into a single statement in order to get the compiler to produce 
  7882. better code.  With current compilers, splitting the statement into two or more 
  7883. simpler statements will produce equivalent code, and will make the program 
  7884. easier to understand. 
  7885.  
  7886. A common example of a statement that can be split is, 
  7887.  
  7888.  
  7889.      if( (c = getchar()) != EOF ) { 
  7890.  
  7891. Historically, this statement might have allowed the compiler to avoid storing 
  7892. the value of c and then reloading it again to compare with EOF.  However, the 
  7893. equivalent, 
  7894.  
  7895.  
  7896.      c = getchar(); 
  7897.      if( c != EOF ) { 
  7898.  
  7899. is more readable, and most compilers will produce the same code. 
  7900.  
  7901.  
  7902. ΓòÉΓòÉΓòÉ 18.13. Do Not Use goto Too Much ΓòÉΓòÉΓòÉ
  7903.  
  7904.  
  7905. The  goto statement is a very powerful tool, but it is very easy to misuse. 
  7906. Here are some general rules for the use of  goto's: 
  7907.  
  7908.      don't use them! 
  7909.  
  7910.  If that rule is not satisfactory, then these should be followed: 
  7911.  
  7912.      Never  goto a label that is above.  That is the beginning of  spaghetti 
  7913.       code.  Loop statements can always be used. 
  7914.      Never  goto the middle of a block (compound-statement).  A block should 
  7915.       always be entered by passing over the opening brace. 
  7916.      Use  goto to jump out of nested blocks, where the  break statement is not 
  7917.       appropriate. 
  7918.  
  7919.  Above all, keep the use of  goto's to a minimum. 
  7920.  
  7921.  
  7922. ΓòÉΓòÉΓòÉ 18.14. Use Comments ΓòÉΓòÉΓòÉ
  7923.  
  7924.  
  7925. Comments are crucial to good programming style.  Regardless of how well the 
  7926. program is written, some code will be difficult to understand.  Comments make 
  7927. it possible to give a full explanation for what the code is trying to do. 
  7928.  
  7929. Each function definition should begin with a short comment describing what the 
  7930. function does. 
  7931.  
  7932. Each module should begin with comments describing the purpose of the module. 
  7933. It is also a good idea to type in who wrote it, when it was written, who 
  7934. modified it and why, and when it was modified.  This last collection of 
  7935. information is commonly called an  audit trail, as it leaves a trail allowing a 
  7936. programmer to see the evolution of the module, along with who has been changing 
  7937. it. 
  7938.  
  7939. The following audit trail is from one module in an actual product: 
  7940.  
  7941.  
  7942.    /* Modified: By:       Reason: 
  7943.    * ========  ==       ====== 
  7944.    * 84/04/23  Dave McClurkin Initial implementation 
  7945.    * 84/11/08  Jim Graham   Implemented TOTAL non-combinable; 
  7946.    *               added MAXIMUM,MINIMUM,AVERAGE 
  7947.    * 84/12/12  Steve McDowell Added call to CheckBreak 
  7948.    * 85/01/12  ...       Fixed overflow problems 
  7949.    * 85/01/29  Alex Kachura  Saves value of TYP_ field 
  7950.    * 86/01/31  Steve McDowell Switched to use of numeric accumulator 
  7951.    * 86/12/10  ...       Removed some commented code 
  7952.    * 87/02/24  ...       Made all commands combinable 
  7953.    */ 
  7954.  
  7955.  
  7956. ΓòÉΓòÉΓòÉ 19. Compiler Keywords ΓòÉΓòÉΓòÉ
  7957.  
  7958.  
  7959. The following topics are discussed: 
  7960.  
  7961.      Standard Keywords 
  7962.      Watcom C/16 and C/32 Keywords 
  7963.  
  7964.  
  7965. ΓòÉΓòÉΓòÉ 19.1. Standard Keywords ΓòÉΓòÉΓòÉ
  7966.  
  7967.  
  7968. The following is the list of keywords reserved by the C language: 
  7969.  
  7970.  auto    double   int     struct 
  7971.  break    else    long    switch 
  7972.  case    enum    register  typedef 
  7973.  char    extern   return   union 
  7974.  const    float    short    unsigned 
  7975.  continue  for     signed   void 
  7976.  default   goto    sizeof   volatile 
  7977.  do     if     static   while 
  7978.  
  7979.  
  7980. ΓòÉΓòÉΓòÉ 19.2. Watcom C/16 and C/32 Keywords ΓòÉΓòÉΓòÉ
  7981.  
  7982.  
  7983. The Watcom C/16 and C/32 compilers also reserve the following keywords: 
  7984.  
  7985.  __based   __fortran  __near   __segment 
  7986.  __cdecl   __huge   _Packed   __segname 
  7987.  __export  __interrupt      __pascal 
  7988.  __self 
  7989.  __far    __loadds  __saveregs __syscall 
  7990. The Watcom C/32 compiler also reserves the following keywords: 
  7991.  
  7992.  __far16   _Seg16   __stdcall 
  7993. The keywords  __based,  __segment,  __segname and  __self are described in the 
  7994. section "Based Pointers for Watcom C/16 and C/32".  Watcom C/16 and C/32 
  7995. provide the predefined macro  _based for convenience and compatibility with the 
  7996. Microsoft C compiler.  It may be used in place of  __based.  Watcom C/16 and 
  7997. C/32 provide the predefined macro  _segment for convenience and compatibility 
  7998. with the Microsoft C compiler.  It may be used in place of  __segment.  Watcom 
  7999. C/16 and C/32 provide the predefined macro  _segname for convenience and 
  8000. compatibility with the Microsoft C compiler.  It may be used in place of 
  8001. __segname.  Watcom C/16 and C/32 provide the predefined macro  _self for 
  8002. convenience and compatibility with the Microsoft C compiler.  It may be used in 
  8003. place of  __self. 
  8004.  
  8005. The keywords  __far,  __huge and  __near are described in the sections "Special 
  8006. Pointer Types for Watcom C/16" and "Special Pointer Types for Watcom C/32". 
  8007. Watcom C/16 and C/32 provide the predefined macros  far and  _far for 
  8008. convenience and compatibility with the Microsoft C compiler.  Watcom C/16 
  8009. provides the predefined macro  SOMDLINK for convenience and compatibility with 
  8010. the Microsoft C compiler.  They may be used in place of  __far.  Watcom C/16 
  8011. and C/32 provide the predefined macros  huge and  _huge for convenience and 
  8012. compatibility with the Microsoft C compiler.  They may be used in place of 
  8013. __huge.  Watcom C/16 and C/32 provide the predefined macros  near and  _near 
  8014. for convenience and compatibility with the Microsoft C compiler.  They may be 
  8015. used in place of  __near. 
  8016.  
  8017. The keywords  __far16 and  _Seg16 are described in the section "Special Pointer 
  8018. Types for Watcom C/32".  Watcom C/16 and C/32 provide the predefined macros 
  8019. _far16 and  _Far16 for convenience and compatibility with the Microsoft C 
  8020. compiler.  It may be used in place of  __far16. 
  8021.  
  8022. The  _Packed keyword is described in the section "Structures". 
  8023.  
  8024. The  __cdecl keyword may be used with function definitions, and indicates that 
  8025. the calling convention for the function is the same as that used by Microsoft 
  8026. C.  All parameters are pushed onto the stack, instead of being passed in 
  8027. registers.  This calling convention may be controlled by a  #pragma directive. 
  8028. See the User's Guide.  Watcom C/16 and C/32 provide the predefined macros 
  8029. cdecl,  _cdecl and  _Cdecl for convenience and compatibility with the Microsoft 
  8030. C compiler.  Watcom C/16 provides the predefined macro  SOMLINK for convenience 
  8031. and compatibility with the Microsoft C compiler.  They may be used in place of 
  8032. __cdecl. 
  8033.  
  8034. The  __fortran keyword may be used with function definitions, and indicates 
  8035. that the calling convention for the function is suitable for calling a function 
  8036. written in FORTRAN.  By default, this keyword has no effect.  This calling 
  8037. convention may be controlled by a  #pragma directive.  See the User's Guide. 
  8038. Watcom C/16 and C/32 provide the predefined macros  fortran and  _fortran for 
  8039. convenience and compatibility with the Microsoft C compiler.  They may be used 
  8040. in place of  __fortran. 
  8041.  
  8042. The  __pascal keyword may be used with function definitions, and indicates that 
  8043. the calling convention for the function is suitable for calling a function 
  8044. written in Pascal.  All parameters are pushed onto the stack, but in reverse 
  8045. order to the order specified by __cdecl.  This calling convention may be 
  8046. controlled by a  #pragma directive.  See the User's Guide.  Watcom C/16 and 
  8047. C/32 provide the predefined macros  pascal and  _pascal for convenience and 
  8048. compatibility with the Microsoft C compiler.  They may be used in place of 
  8049. __pascal. 
  8050.  
  8051. The  __syscall keyword may be used with function definitions, and indicates 
  8052. that the calling convention used is compatible with OS/2 (version 2.0 or 
  8053. higher).  This calling convention may be controlled by a  #pragma directive. 
  8054. See the User's Guide.  Watcom C/16 and C/32 provide the predefined macros 
  8055. _syscall, and  _System for convenience and compatibility with the Microsoft C 
  8056. compiler.  Watcom C/32 provides the predefined macros  SOMLINK and  SOMDLINK 
  8057. for convenience and compatibility with the Microsoft C compiler.  They may be 
  8058. used in place of  __syscall. 
  8059.  
  8060. The  __stdcall keyword may be used with function definitions, and indicates 
  8061. that the calling convention used is compatible with Win32.  This calling 
  8062. convention may be controlled by a  #pragma directive.  See the User's Guide. 
  8063.  
  8064. The  __export keyword may be used with objects with static storage duration 
  8065. (global objects) and with functions, and describes that object or function as 
  8066. being a known object or entry point within a Dynamic Link Library in OS/2 or 
  8067. Microsoft Windows.  The object or function must also be declared as having 
  8068. external linkage (using the  extern keyword).  In addition, any call back 
  8069. function whose address is passed to Windows (and which Windows will "call 
  8070. back") must be defined with the __export keyword, otherwise the call will fail 
  8071. and cause unpredictable results.  The __export keyword may be omitted if the 
  8072. object or function is exported by an option specified using the linker.  See 
  8073. the Watcom Linker User's Guide.  Watcom C/16 and C/32 provide the predefined 
  8074. macro  _export for convenience and compatibility with the Microsoft C compiler. 
  8075. It may be used in place of  __export. 
  8076.  
  8077. The  __interrupt keyword may be used with function definitions for functions 
  8078. that handle computer interrupts.  All registers are saved before the function 
  8079. begins execution and restored prior to returning from the interrupt.  The 
  8080. machine language return instruction for the function is changed to iret 
  8081. (interrupt return).  Functions written using __interrupt are suitable for 
  8082. attaching to the interrupt vector using the library function  _dos_setvect. 
  8083. Watcom C/16 and C/32 provide the predefined macros  interrupt and  _interrupt 
  8084. for convenience and compatibility with the Microsoft C compiler.  They may be 
  8085. used in place of  __interrupt. 
  8086.  
  8087. The  __loadds keyword may be used with functions, and causes the compiler to 
  8088. generate code that will force the DS register to be set to the default data 
  8089. segment (DGROUP) so that near pointers will refer to that segment.  This 
  8090. keyword is normally used with functions written for Dynamic Link Libraries in 
  8091. Windows and OS/2.  Watcom C/16 and C/32 provide the predefined macro  _loadds 
  8092. for convenience and compatibility with the Microsoft C compiler.  It may be 
  8093. used in place of  __loadds. 
  8094.  
  8095. The  __saveregs keyword may be used with functions.  It is provided for 
  8096. compatibility with Microsoft C, and has no effect in Watcom C/16 and C/32. 
  8097. Watcom C/16 and C/32 provide the predefined macro  _saveregs for convenience 
  8098. and compatibility with the Microsoft C compiler.  It may be used in place of 
  8099. __saveregs. 
  8100.  
  8101.  
  8102. ΓòÉΓòÉΓòÉ 20. Trigraphs ΓòÉΓòÉΓòÉ
  8103.  
  8104.  
  8105. The following is the list of trigraphs.  In a C source file, all occurrences 
  8106. (including inside quoted strings and character constants) of any of the 
  8107. trigraph sequences below are replaced by the corresponding single character. 
  8108.  
  8109.  
  8110.          Trigraph 
  8111.    Character  Sequence 
  8112.    ---------  -------- 
  8113.      [     ??( 
  8114.      ]     ??) 
  8115.      {     ??< 
  8116.      }     ??> 
  8117.      |     ??! 
  8118.      #     ??= 
  8119.      \     ??/ 
  8120.      ^     ??' 
  8121.      ~     ??- 
  8122.  
  8123. No other trigraphs exist.  Any question mark (?) that does not belong to one of 
  8124. the trigraphs is not changed. 
  8125.  
  8126. To get a sequence of characters that would otherwise be a trigraph, place a \ 
  8127. before the second question mark.  This will cause the trigraph to be broken up 
  8128. so that it is not recognized, but later in the translation process, the \? 
  8129. will be converted to ?.  For example, ?\?= will be translated to ??=. 
  8130.  
  8131.  
  8132. ΓòÉΓòÉΓòÉ 21. Escape Sequences ΓòÉΓòÉΓòÉ
  8133.  
  8134.  
  8135. The following are the escape sequences and their meanings: 
  8136.  
  8137.  
  8138.    Escape 
  8139.    Sequence   Meaning 
  8140.    --------   ------------------------------------- 
  8141.    \a      Causes an audible or visual alert 
  8142.    \b      Back up one character 
  8143.    \f      Move to the start of the next page 
  8144.    \n      Move to the start of the next line 
  8145.    \r      Move to the start of the current line 
  8146.    \t      Move to the next horizontal tab 
  8147.    \v      Move to the next vertical tab 
  8148.  
  8149. Each escape sequence maps to a single character.  When such a character is sent 
  8150. to a display device, the action corresponding to that character is performed. 
  8151.  
  8152.  
  8153. ΓòÉΓòÉΓòÉ 22. Operator Precedence ΓòÉΓòÉΓòÉ
  8154.  
  8155.  
  8156. The table below summarizes the levels of precedence in expressions. 
  8157.  
  8158.  
  8159.    Expression Type     Operators 
  8160.    ----------------------  ------------------------------ 
  8161.    primary         identifier 
  8162.                constant 
  8163.                string 
  8164.                (expression) 
  8165.    ----------------------  ------------------------------ 
  8166.    postfix         a[b] 
  8167.                f() 
  8168.                a.b   a->b 
  8169.                a++   a-- 
  8170.    ----------------------  ------------------------------ 
  8171.    unary          sizeof u  sizeof( a ) 
  8172.                ++a   --a 
  8173.                &a    *a 
  8174.                +a    -a    ~a    !a 
  8175.    ----------------------  ------------------------------ 
  8176.    cast           (type) a 
  8177.    ----------------------  ------------------------------ 
  8178.    multiplicative      a * b  a / b  a % b 
  8179.    ----------------------  ------------------------------ 
  8180.    additive         a + b  a - b 
  8181.    ----------------------  ------------------------------ 
  8182.    shift          a << b  a >> b 
  8183.    ----------------------  ------------------------------ 
  8184.    relational        a < b  a > b  a <= b  a >= b 
  8185.    ----------------------  ------------------------------ 
  8186.    equality         a == b  a != b 
  8187.    ----------------------  ------------------------------ 
  8188.    bitwise AND       a & b 
  8189.    ----------------------  ------------------------------ 
  8190.    bitwise exclusive OR   a ^ b 
  8191.    ----------------------  ------------------------------ 
  8192.    bitwise inclusive OR   a | b 
  8193.    ----------------------  ------------------------------ 
  8194.    logical AND       a && b 
  8195.    ----------------------  ------------------------------ 
  8196.    logical OR        a || b 
  8197.    ----------------------  ------------------------------ 
  8198.    conditional (*)     a ? b : c 
  8199.    ----------------------  ------------------------------ 
  8200.    assignment (*)      a = b 
  8201.                a += b   a -= b 
  8202.                a *= b   a /= b   a %= b 
  8203.                a &= b   a ^= b   a |= b 
  8204.                a <<= b   a >>= b 
  8205.    ----------------------  ------------------------------ 
  8206.    comma          a,b 
  8207.    ----------------------  ------------------------------ 
  8208.  
  8209. (*) associates from right to left 
  8210.  
  8211. Operations at a higher level in the table will occur before those below.  All 
  8212. operators involving more than one operand associate from left to right, except 
  8213. for the conditional and assignment operators, which associate from right to 
  8214. left.  Operations at the same level, except where discussed in the relevant 
  8215. section, may be executed in any order that the compiler chooses (subject to the 
  8216. usual algebraic rules).  In particular, the compiler may regroup 
  8217. sub-expressions that are both associative and commutative in order to improve 
  8218. the efficiency of the code, provided the meaning (i.e.  types and results) of 
  8219. the operands and result are not affected by the regrouping. 
  8220.  
  8221. The order of any side-effects (for example, assignment, or action taken by a 
  8222. function call) is also subject to alteration by the compiler. 
  8223.  
  8224.  
  8225. ΓòÉΓòÉΓòÉ 23. Formal C Grammar ΓòÉΓòÉΓòÉ
  8226.  
  8227.  
  8228. This appendix presents the formal grammar of the C programming language.  The 
  8229. following notation is used: 
  8230.  
  8231. {digit}(0) 
  8232.   Zero or more occurrences of digit are allowed. 
  8233. {digit}(1) 
  8234.   One or more occurrences of digit are allowed. 
  8235. <integer-suffix > 
  8236.   integer-suffix is optional, with only one occurrence being allowed if 
  8237. present. 
  8238. A | B | C 
  8239.   Choose one of A, B or C. 
  8240.  
  8241.  
  8242. ΓòÉΓòÉΓòÉ 23.1. Lexical Grammar ΓòÉΓòÉΓòÉ
  8243.  
  8244.  
  8245. The following topics are discussed: 
  8246.  
  8247.      Tokens 
  8248.      Keywords 
  8249.      Identifiers 
  8250.      Constants 
  8251.      String Literals 
  8252.      Operators 
  8253.      Punctuators 
  8254.  
  8255.  
  8256. ΓòÉΓòÉΓòÉ 23.1.1. Definition of a Token ΓòÉΓòÉΓòÉ
  8257.  
  8258.  
  8259. token 
  8260.  
  8261.      keyword 
  8262.   or  identifier 
  8263.   or  constant 
  8264.   or  string-literal 
  8265.   or  operator 
  8266.   or  punctuator 
  8267.  
  8268.  
  8269. ΓòÉΓòÉΓòÉ 23.1.2. Definition of a Keyword ΓòÉΓòÉΓòÉ
  8270.  
  8271.  
  8272. keyword 
  8273.  
  8274.      standard-keyword 
  8275.   or  Watcom-C-keyword 
  8276.   or  Watcom-C/32-keyword 
  8277.  
  8278. standard-keyword 
  8279.  auto    double   int     struct 
  8280.  break    else    long    switch 
  8281.  case    enum    register  typedef 
  8282.  char    extern   return   union 
  8283.  const    float    short    unsigned 
  8284.  continue  for     signed   void 
  8285.  default   goto    sizeof   volatile 
  8286.  do     if     static   while 
  8287.  
  8288. Watcom-C-keyword 
  8289.  __based   __fortran  __near   __segment 
  8290.  __cdecl   __huge   _Packed   __segname 
  8291.  __export  __interrupt      __pascal 
  8292.  __self 
  8293.  __far    __loadds  __saveregs __syscall 
  8294.  
  8295. Watcom-C/32-keyword 
  8296.  __far16   _Seg16   __stdcall 
  8297.  
  8298.  
  8299. ΓòÉΓòÉΓòÉ 23.1.3. Definition of an Identifier ΓòÉΓòÉΓòÉ
  8300.  
  8301.  
  8302. identifier 
  8303.  
  8304.      nondigit {nondigit | digit}(0) 
  8305.  
  8306. nondigit 
  8307.      a | b | ...  | z | A | B | ...  | Z | _ 
  8308.  
  8309. digit 
  8310.      0 | 1 | ...  | 9 
  8311.  
  8312.  
  8313. ΓòÉΓòÉΓòÉ 23.1.4. Definition of a Constant ΓòÉΓòÉΓòÉ
  8314.  
  8315.  
  8316. constant 
  8317.  
  8318.      floating-constant 
  8319.    or  integer-constant 
  8320.    or  enumeration-constant 
  8321.    or  character-constant 
  8322.  
  8323. floating-constant 
  8324.      fractional-constant <exponent-part > <floating-suffix > 
  8325.    or  digit-sequence exponent-part <floating-suffix > 
  8326.  
  8327. exponent-part 
  8328.      e|E <+|- > digit-sequence 
  8329.  
  8330. floating-suffix 
  8331.       f | F | l | L 
  8332.  
  8333. fractional-constant 
  8334.       <digit-sequence > .  digit-sequence 
  8335.    or  digit-sequence . 
  8336.  
  8337. digit-sequence 
  8338.      {digit}(1) 
  8339.  
  8340. integer-constant 
  8341.      decimal-constant <integer-suffix > 
  8342.    or  octal-constant <integer-suffix > 
  8343.    or  hexadecimal-constant <integer-suffix > 
  8344.  
  8345. integer-suffix 
  8346.      u|U <l|L > 
  8347.    or  l|L <u|U > 
  8348.  
  8349. decimal-constant 
  8350.      nonzero-digit{digit}(0) 
  8351.  
  8352. nonzero-digit 
  8353.       1 | 2 | ...  | 9 
  8354.  
  8355. octal-constant 
  8356.      0{octal-digit}(0) 
  8357.  
  8358. octal-digit 
  8359.      0 | 1 | ...  | 7 
  8360.  
  8361. hexadecimal-constant 
  8362.      0x|0X{hexadecimal-digit}(1) 
  8363.  
  8364. hexadecimal-digit 
  8365.      0 | 1 | ...  | 9 | 
  8366.      a | b | ...  | f | A | B | ...  | F 
  8367.  
  8368. enumeration-constant 
  8369.      identifier 
  8370.  
  8371. character-constant 
  8372.      '{c-char}(1)' 
  8373.    or  L'{c-char}(1)' 
  8374.  
  8375. c-char 
  8376.      any character in the source character set except 
  8377.      the single-quote ', backslash \, or new-line character 
  8378.    or  escape-sequence 
  8379.  
  8380. escape-sequence is one of 
  8381.      \' \" \\ 
  8382.      \o \oo \ooo 
  8383.      \x{hexadecimal-digit}(1) 
  8384.      \a \b \f \n \r \t \v 
  8385.  
  8386.  
  8387. ΓòÉΓòÉΓòÉ 23.1.5. Definition of a String Literal ΓòÉΓòÉΓòÉ
  8388.  
  8389.  
  8390. string-literal 
  8391.  
  8392.      "{s-char}(0)" 
  8393.    or  L"{s-char}(0)" 
  8394.  
  8395. s-char 
  8396.      any character in the source character set except 
  8397.      the double-quote ", backslash \, or new-line character 
  8398.    or  escape-sequence 
  8399.  
  8400.  
  8401. ΓòÉΓòÉΓòÉ 23.1.6. Definition of an Operator ΓòÉΓòÉΓòÉ
  8402.  
  8403.  
  8404. operator is one of 
  8405.    [  ]  (  )  .  -> 
  8406.    ++  --  &  *  +  -  ~  !  sizeof 
  8407.    /  %  <<  >>  <  >  <=  >=  ==  !=  ^  |  &&  || 
  8408.    ?  : 
  8409.    =  *=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |= 
  8410.    ,  #  ## 
  8411.    :> 
  8412.  
  8413.  
  8414. ΓòÉΓòÉΓòÉ 23.1.7. Definition of a Punctuator ΓòÉΓòÉΓòÉ
  8415.  
  8416.  
  8417. punctuator 
  8418.      [  ]  (  )  {  }  *  ,  :  = ; ... # 
  8419.  
  8420.  
  8421. ΓòÉΓòÉΓòÉ 23.2. Phrase Structure Grammar ΓòÉΓòÉΓòÉ
  8422.  
  8423.  
  8424. The following topics are discussed: 
  8425.  
  8426.      Expressions 
  8427.      Declarations 
  8428.      Statements 
  8429.      External Definitions 
  8430.  
  8431.  
  8432. ΓòÉΓòÉΓòÉ 23.2.1. Definition of Expressions ΓòÉΓòÉΓòÉ
  8433.  
  8434.  
  8435. constant-expression 
  8436.  
  8437.      conditional-expression 
  8438.  
  8439. expression 
  8440.      assignment-expression{ , assignment-expression}(0) 
  8441.  
  8442. assignment-expression 
  8443.      conditional-expression 
  8444.    or  unary-expression assignment-operator assignment-expression 
  8445.  
  8446. assignment-operator is one of 
  8447.      =  *=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |= 
  8448.  
  8449. conditional-expression 
  8450.      logical-OR-expression <?  expression :  conditional-expression > 
  8451.  
  8452. logical-OR-expression 
  8453.      logical-AND-expression{|| logical-AND-expression}(0) 
  8454.  
  8455. logical-AND-expression 
  8456.      inclusive-OR-expression {&& inclusive-OR-expression}(0) 
  8457.  
  8458. inclusive-OR-expression 
  8459.      exclusive-OR-expression {| exclusive-OR-expression}(0) 
  8460.  
  8461. exclusive-OR-expression 
  8462.      AND-expression {^ AND-expression}(0) 
  8463.  
  8464. AND-expression 
  8465.      equality-expression {& equality-expression}(0) 
  8466.  
  8467. equality-expression 
  8468.      relational-expression {==|!= relational-expression}(0) 
  8469.  
  8470. relational-expression 
  8471.      shift-expression {<|>|<=|>= shift-expression}(0) 
  8472.  
  8473. shift-expression 
  8474.      additive-expression {<<|>> additive-expression}(0) 
  8475.  
  8476. additive-expression 
  8477.      multiplicative-expression {+|- multiplicative-expression}(0) 
  8478.  
  8479. multiplicative-expression 
  8480.      cast-expression {*|/|% cast-expression}(0) 
  8481.  
  8482. cast-expression 
  8483.      unary-expression 
  8484.    or  ( type-name ) cast-expression 
  8485.  
  8486. unary-expression 
  8487.      postfix-expression 
  8488.    or  ++ | -- | sizeof unary-expression 
  8489.    or  sizeof( type-name ) 
  8490.    or  unary-operator cast-expression 
  8491.  
  8492. unary-operator is one of 
  8493.      & * + - ~ ! 
  8494.  
  8495. postfix-expression 
  8496.      primary-expression 
  8497.    or  postfix-expression [ expression ] 
  8498.    or  postfix-expression ( <argument-expression-list > ) 
  8499.    or  postfix-expression .  identifier 
  8500.    or  postfix-expression -> identifier 
  8501.    or  postfix-expression ++ 
  8502.    or  postfix-expression -- 
  8503.  
  8504. argument-expression-list 
  8505.      assignment-expression {, assignment-expression}(0) 
  8506.  
  8507. primary-expression 
  8508.      identifier 
  8509.    or  constant 
  8510.    or  string-literal 
  8511.    or  ( expression ) 
  8512.  
  8513.  
  8514. ΓòÉΓòÉΓòÉ 23.2.2. Definition of a Declaration ΓòÉΓòÉΓòÉ
  8515.  
  8516.  
  8517. declaration 
  8518.  
  8519.      declaration-specifiers <init-declarator-list >; 
  8520.  
  8521. declaration-specifiers 
  8522.      storage-class-specifier <declaration-specifiers > 
  8523.    or  type-specifier <declaration-specifiers > 
  8524.  
  8525. init-declarator-list 
  8526.      init-declarator {, init-declarator}(0) 
  8527.  
  8528. init-declarator 
  8529.      declarator <= initializer > 
  8530.  
  8531. storage-class-specifier 
  8532.      typedef | extern | static | auto | register 
  8533.  
  8534. type-specifier 
  8535.      void | char | short | int | long | float | 
  8536.      double | signed | unsigned 
  8537.    or  struct-or-union-specifier 
  8538.    or  enum-specifier 
  8539.    or  typedef-name 
  8540.    or  type-qualifier 
  8541.  
  8542. type-qualifier 
  8543.      const | volatile 
  8544.    or  Watcom-C-type-qualifier 
  8545.    or  Watcom-C/32-type-qualifier 
  8546.  
  8547. Watcom-C-type-qualifier 
  8548.  __based   __fortran  __near   __segment 
  8549.  __cdecl   __huge   _Packed   __segname 
  8550.  __export  __interrupt      __pascal 
  8551.  __self 
  8552.  __far    __loadds  __saveregs __syscall 
  8553.  
  8554. Watcom-C/32-type-qualifier 
  8555.  __far16   _Seg16   __stdcall 
  8556.  
  8557. struct-or-union-specifier 
  8558.      struct-or-union <identifier > { struct-declaration-list } 
  8559.    or  struct-or-union identifier 
  8560.  
  8561. struct-or-union 
  8562.      struct | union 
  8563.  
  8564. struct-declaration-list 
  8565.      {struct-declaration}(1) 
  8566.  
  8567. struct-declaration 
  8568.      type-specifier-list struct-declarator-list; 
  8569.  
  8570. type-specifier-list 
  8571.      {type-specifier}(1) 
  8572.  
  8573. struct-declarator-list 
  8574.      struct-declarator {, struct-declarator}(0) 
  8575.  
  8576. struct-declarator 
  8577.      declarator 
  8578.    or   <declarator > :  constant-expression 
  8579.  
  8580. enum-specifier 
  8581.      enum <identifier > { enumerator-list } 
  8582.    or  enum identifier 
  8583.  
  8584. enumerator-list 
  8585.      enumerator {, enumerator}(0) 
  8586.  
  8587. enumerator 
  8588.      enumeration-constant <= constant-expression > 
  8589.  
  8590. declarator 
  8591.       <pointer > direct-declarator 
  8592.  
  8593. direct-declarator 
  8594.      identifier 
  8595.    or  ( declarator ) 
  8596.    or  direct-declarator [ <constant-expression > ] 
  8597.    or  direct-declarator ( parameter-type-list ) 
  8598.    or  direct-declarator ( <identifier-list > ) 
  8599.  
  8600. pointer 
  8601.      {* <type-specifier-list >}(1) 
  8602.  
  8603. parameter-type-list 
  8604.      parameter-list <, ...  > 
  8605.  
  8606. parameter-list 
  8607.      parameter-declaration {, parameter-declaration}(0) 
  8608.  
  8609. parameter-declaration 
  8610.      declaration-specifiers declarator 
  8611.    or  declaration-specifiers <abstract-declarator > 
  8612.  
  8613. identifier-list 
  8614.      identifier {, identifier}(0) 
  8615.  
  8616. type-name 
  8617.      type-specifier-list <abstract-declarator > 
  8618.  
  8619. abstract-declarator 
  8620.      pointer 
  8621.    or   <pointer > direct-abstract-declarator 
  8622.  
  8623. direct-abstract-declarator 
  8624.      ( abstract-declarator ) 
  8625.    or   <direct-abstract-declarator > [ <constant-expression > ] 
  8626.    or   <direct-abstract-declarator > ( <parameter-type-list > ) 
  8627.  
  8628. typedef-name 
  8629.      identifier 
  8630.  
  8631. initializer 
  8632.      assignment-expression 
  8633.    or  {initializer-list <, >} 
  8634.  
  8635. initializer-list 
  8636.      initializer {, initializer}(0) 
  8637.  
  8638.  
  8639. ΓòÉΓòÉΓòÉ 23.2.3. Definition of a Statement ΓòÉΓòÉΓòÉ
  8640.  
  8641.  
  8642. statement 
  8643.  
  8644.      labelled-statement 
  8645.    or  compound-statement 
  8646.    or  expression-statement 
  8647.    or  selection-statement 
  8648.    or  iteration-statement 
  8649.    or  jump-statement 
  8650.  
  8651. labelled-statement 
  8652.      identifier :  statement 
  8653.    or  case constant-expression :  statement 
  8654.    or  default :  statement 
  8655.  
  8656. compound-statement 
  8657.      { <declaration-list > <statement-list >} 
  8658.  
  8659. declaration-list 
  8660.      {declaration}(1) 
  8661.  
  8662. statement-list 
  8663.      {statement}(1) 
  8664.  
  8665. expression-statement 
  8666.       <expression >; 
  8667.  
  8668. selection-statement 
  8669.      if ( expression ) statement 
  8670.    or  if ( expression ) statement else statement 
  8671.    or  switch ( expression ) statement 
  8672.  
  8673. iteration-statement 
  8674.      while ( expression ) statement 
  8675.    or  do statement while ( expression ); 
  8676.    or  for ( <expression >; <expression >; <expression > ) statement 
  8677.  
  8678. jump-statement 
  8679.      goto identifier; 
  8680.    or  continue; 
  8681.    or  break; 
  8682.    or  return <expression >; 
  8683.  
  8684.  
  8685. ΓòÉΓòÉΓòÉ 23.2.4. External Definitions ΓòÉΓòÉΓòÉ
  8686.  
  8687.  
  8688. file 
  8689.  
  8690.      {external-definition}(1) 
  8691.  
  8692. external-definition 
  8693.      function-definition 
  8694.    or  declaration 
  8695.  
  8696. function-definition 
  8697.       <declaration-specifiers > declarator <declaration-list > 
  8698.         compound-statement 
  8699.  
  8700.  
  8701. ΓòÉΓòÉΓòÉ 23.3. Preprocessing Directives Grammar ΓòÉΓòÉΓòÉ
  8702.  
  8703.  
  8704. preprocessing-file 
  8705.  
  8706.      group 
  8707.  
  8708. group 
  8709.      {group-part}(1) 
  8710.  
  8711. group-part 
  8712.       <pp-token > new-line 
  8713.    or  if-section 
  8714.    or  control-line 
  8715.  
  8716. if-section 
  8717.      if-group {elif-group}(0) <else-group > endif-line 
  8718.  
  8719. if-group 
  8720.      # if const-expression new-line <group > 
  8721.      # ifdef identifier new-line <group > 
  8722.      # ifndef identifier new-line <group > 
  8723.  
  8724. elif-group 
  8725.      # elif constant-expression new-line <group > 
  8726.  
  8727. else-group 
  8728.      # else new-line <group > 
  8729.  
  8730. endif-line 
  8731.      # endif new-line 
  8732.  
  8733. control-line 
  8734.      # include pp-tokens new-line 
  8735.      # define identifier <pp-tokens > new-line 
  8736.      # define identifier ( <identifier-list > ) <pp-tokens > new-line 
  8737.      # undef identifier new-line 
  8738.      # line pp-tokens new-line 
  8739.      # error <pp-tokens > new-line 
  8740.      # pragma <pp-tokens > new-line 
  8741.      # new-line 
  8742.  
  8743. pp-tokens 
  8744.      {preprocessing-token}(1) 
  8745.  
  8746. preprocessing-token 
  8747.      header-name (only within a #include directive) 
  8748.    or  identifier (no keyword distinction) 
  8749.    or  constant 
  8750.    or  string-literal 
  8751.    or  operator 
  8752.    or  punctuator 
  8753.    or  each non-white-space character that cannot be one of the above 
  8754.  
  8755. header-name 
  8756.      <{h-char}(0)> 
  8757.  
  8758. h-char 
  8759.      any character in the source character set except new-line and > 
  8760.  
  8761. new-line 
  8762.      the new-line character 
  8763.  
  8764.  
  8765. ΓòÉΓòÉΓòÉ 24. Translation Limits ΓòÉΓòÉΓòÉ
  8766.  
  8767.  
  8768. All standard-conforming C compilers must be able to translate and execute a 
  8769. program that contains one instance of every one of the following limits.  Each 
  8770. limit is the minimum limit (the smallest maximum) that the compiler may impose. 
  8771.  
  8772.  The Watcom C/16 and C/32 compilers do not impose any arbitrary restrictions in 
  8773. any of these areas.  Restrictions arise solely because of memory limitations. 
  8774.  
  8775.      15 nesting levels of compound statements, iteration control structures ( 
  8776.       for,  do/ while,  while), and selection control structures ( if, 
  8777.       switch), 
  8778.      8 nesting levels of conditional inclusion ( #if), 
  8779.      12 pointer, array and function declarators (in any order) modifying an 
  8780.       arithmetic, structure, union or incomplete type in a declaration, 
  8781.      31 nesting levels of parenthesized declarators within a full declarator, 
  8782.      32 nesting levels of parenthesized expressions within a full expression, 
  8783.      31 significant initial characters in an internal identifier or a macro 
  8784.       name, 
  8785.      6 significant initial characters in an external identifier, 
  8786.      511 external identifiers in one translation unit (module), 
  8787.      127 identifiers with block scope declared in one block, 
  8788.      1024 macro identifiers simultaneously defined in one translation unit 
  8789.       (module), 
  8790.      31 parameters in one function definition, 
  8791.      31 arguments in one function call, 
  8792.      31 parameters in one macro definition, 
  8793.      31 parameters in one macro invocation, 
  8794.      509 characters in a logical (continued) source line, 
  8795.      509 characters in a character string literal or wide string literal 
  8796.       (after concatenation), 
  8797.      32767 bytes in an object, 
  8798.      8 nesting levels for  #included files, 
  8799.      257  case labels for a  switch statement (excluding those for any nested 
  8800.       switch statements), 
  8801.      127 members in a single structure or union, 
  8802.      127 enumeration constants in a single enumeration, 
  8803.      15 levels of nested structure or union definitions in a single 
  8804.       struct-declaration-list (structure or union definition). 
  8805.  
  8806.  
  8807. ΓòÉΓòÉΓòÉ 25. Macros for Numerical Limits ΓòÉΓòÉΓòÉ
  8808.  
  8809.  
  8810. Although the various numerical types may have different ranges depending on the 
  8811. implementation of the C compiler, it is still possible to write programs that 
  8812. can adapt to these changing ranges.  In most circumstances, it is clear whether 
  8813. an integer object is sufficiently large to contain all necessary values for it, 
  8814. regardless of whether or not the integer is only 16 bits. 
  8815.  
  8816. However, a programmer may want to be able to conditionally compile code based 
  8817. on information about the range of certain types.  The header  <limits.h> 
  8818. defines a set of macros that describe the range of the various integer types. 
  8819. The header  <float.h> defines another set of macros that describe the range and 
  8820. other characteristics of the various floating-point types. 
  8821.  
  8822.  
  8823. ΓòÉΓòÉΓòÉ 25.1. Numerical Limits for Integer Types ΓòÉΓòÉΓòÉ
  8824.  
  8825.  
  8826. The following macros are replaced by constant expressions that may be used in 
  8827. #if preprocessing directives.  For a compiler to conform to the C language 
  8828. standard, the magnitude of the value of the expression provided by the compiler 
  8829. must equal or exceed the ANSI value given below, and have the same sign. 
  8830. (Positive values must be greater than or equal to the ANSI value.  Negative 
  8831. values must be less than or equal to the ANSI value.) The values for the actual 
  8832. compilers are shown following the ANSI value. 
  8833.  
  8834.      the number of bits in the smallest object that is not a bit-field (byte) 
  8835.  
  8836.  
  8837.          Macro: CHAR_BIT    Value 
  8838.          ANSI       >= 8 
  8839.          Watcom C/16 and C/32   8 
  8840.      the minimum value for an object of type  signed char 
  8841.  
  8842.  
  8843.          Macro: SCHAR_MIN Value 
  8844.          ANSI  <= -127 
  8845.          Watcom C/16 and C/32  -128 
  8846.      the maximum value for an object of type  signed char 
  8847.  
  8848.  
  8849.          Macro: SCHAR_MAX Value 
  8850.          ANSI  >= 127 
  8851.          Watcom C/16 and C/32  127 
  8852.      the maximum value for an object of type  unsigned char 
  8853.  
  8854.  
  8855.          Macro: UCHAR_MAX Value 
  8856.          ANSI  >= 255 
  8857.          Watcom C/16 and C/32  255 
  8858.      the minimum value for an object of type  char 
  8859.  
  8860.       If  char is  unsigned (the default case) 
  8861.  
  8862.  
  8863.          Macro: CHAR_MIN Value 
  8864.          ANSI  0 
  8865.          Watcom C/16 and C/32  0 
  8866.  
  8867.       If  char is  signed (by using the command-line switch to force it to be 
  8868.       signed), then CHAR_MIN is equivalent to SCHAR_MIN 
  8869.  
  8870.  
  8871.          Macro: CHAR_MIN Value 
  8872.          ANSI  <= -127 
  8873.          Watcom C/16 and C/32  -128 
  8874.      the maximum value for an object of type  char 
  8875.  
  8876.       If  char is  unsigned (the default case), then CHAR_MAX is equivalent to 
  8877.       UCHAR_MAX 
  8878.  
  8879.  
  8880.          Macro: CHAR_MAX Value 
  8881.          ANSI  >= 255 
  8882.          Watcom C/16 and C/32  255 
  8883.  
  8884.       If  char is  signed (by using the command-line switch to force it to be 
  8885.       signed), then CHAR_MAX is equivalent to SCHAR_MAX 
  8886.  
  8887.  
  8888.          Macro: CHAR_MAX Value 
  8889.          ANSI  >= 127 
  8890.          Watcom C/16 and C/32  127 
  8891.      the maximum number of bytes in a multibyte character, for any supported 
  8892.       locale 
  8893.  
  8894.  
  8895.          Macro: MB_LEN_MAX Value 
  8896.          ANSI  >= 1 
  8897.          Watcom C/16 and C/32  2 
  8898.      the minimum value for an object of type  short int 
  8899.  
  8900.  
  8901.          Macro: SHRT_MIN Value 
  8902.          ANSI  <= -32767 
  8903.          Watcom C/16 and C/32  -32768 
  8904.      the maximum value for an object of type  short int 
  8905.  
  8906.  
  8907.          Macro: SHRT_MAX Value 
  8908.          ANSI  >= 32767 
  8909.          Watcom C/16 and C/32  32767 
  8910.      the maximum value for an object of type  unsigned short int 
  8911.  
  8912.  
  8913.          Macro: USHRT_MAX Value 
  8914.          ANSI  >= 65535 
  8915.          Watcom C/16 and C/32  65535 
  8916.      the minimum value for an object of type  int 
  8917.  
  8918.  
  8919.          Macro: INT_MIN Value 
  8920.          ANSI   <= -32767 
  8921.          Watcom C/16  -32768 
  8922.          Watcom C/32  -2147483648 
  8923.      the maximum value for an object of type  int 
  8924.  
  8925.  
  8926.          Macro: INT_MAX Value 
  8927.          ANSI   >= 32767 
  8928.          Watcom C/16  32767 
  8929.          Watcom C/32  2147483647 
  8930.      the maximum value for an object of type  unsigned int 
  8931.  
  8932.  
  8933.          Macro: UINT_MAX Value 
  8934.          ANSI   >= 65535 
  8935.          Watcom C/16  65535 
  8936.          Watcom C/32  4294967295 
  8937.      the minimum value for an object of type  long int 
  8938.  
  8939.  
  8940.          Macro: LONG_MIN Value 
  8941.          ANSI  <= -2147483647 
  8942.          Watcom C/16 and C/32  -2147483648 
  8943.      the maximum value for an object of type  long int 
  8944.  
  8945.  
  8946.          Macro: LONG_MAX Value 
  8947.          ANSI  >= 2147483647 
  8948.          Watcom C/16 and C/32  2147483647 
  8949.      the maximum value for an object of type  unsigned long int 
  8950.  
  8951.  
  8952.          Macro: ULONG_MAX Value 
  8953.          ANSI  >= 4294967295 
  8954.          Watcom C/16 and C/32  4294967295 
  8955.  
  8956.  
  8957. ΓòÉΓòÉΓòÉ 25.2. Numerical Limits for Floating-Point Types ΓòÉΓòÉΓòÉ
  8958.  
  8959.  
  8960. The following macros are replaced by expressions which are not necessarily 
  8961. constant.  For a compiler to conform to the C language standard, the magnitude 
  8962. of the value of the expression provided by the compiler must equal or exceed 
  8963. the ANSI value given below, and have the same sign.  (Positive values must be 
  8964. greater than or equal to the ANSI value.  Negative values must be less than or 
  8965. equal to the ANSI value.) The values for the actual compilers are shown 
  8966. following the ANSI value.  Most compilers will exceed some of these values. 
  8967.  
  8968. For those characteristics that have three different macros, the macros that 
  8969. start with FLT_ refer to type  float, DBL_ refer to type  double and LDBL_ 
  8970. refer to type  long double. 
  8971.  
  8972.      the radix (base) of representation for the exponent 
  8973.  
  8974.  
  8975.          Macro: FLT_RADIX Value 
  8976.          ANSI  >= 2 
  8977.          Watcom C/16 and C/32  2 
  8978.      the precision, or number of digits in the floating-point mantissa, 
  8979.       expressed in terms of the FLT_RADIX 
  8980.  
  8981.  
  8982.          Macro: FLT_MANT_DIG Value 
  8983.          ANSI       no value specified 
  8984.          Watcom C/16 and C/32  23 
  8985.  
  8986.  
  8987.          Macro: DBL_MANT_DIG Value 
  8988.          ANSI   no value specified 
  8989.          Watcom C/16 and C/32  52 
  8990.  
  8991.  
  8992.          Macro: LDBL_MANT_DIG Value 
  8993.          ANSI  no value specified 
  8994.          Watcom C/16 and C/32  52 
  8995.      the number of decimal digits of precision 
  8996.  
  8997.  
  8998.          Macro: FLT_DIG Value 
  8999.          ANSI  >= 6 
  9000.          Watcom C/16 and C/32  6 
  9001.  
  9002.  
  9003.          Macro: DBL_DIG Value 
  9004.          ANSI  >= 10 
  9005.          Watcom C/16 and C/32  15 
  9006.  
  9007.  
  9008.          Macro: LDBL_DIG Value 
  9009.          ANSI  >= 10 
  9010.          Watcom C/16 and C/32  15 
  9011.      the minimum negative integer n such that FLT_RADIX raised to the power n, 
  9012.       minus 1, is a normalized floating-point number, or, 
  9013.         -  the minimum exponent value in terms of FLT_RADIX, or, 
  9014.         -  the base FLT_RADIX exponent for the floating-point value that is 
  9015.            closest, but not equal, to zero 
  9016.  
  9017.          Macro: FLT_MIN_EXP Value 
  9018.          ANSI  no value specified 
  9019.          Watcom C/16 and C/32  -127 
  9020.  
  9021.  
  9022.          Macro: DBL_MIN_EXP Value 
  9023.          ANSI  no value specified 
  9024.          Watcom C/16 and C/32  -1023 
  9025.  
  9026.  
  9027.          Macro: LDBL_MIN_EXP Value 
  9028.          ANSI  no value specified 
  9029.          Watcom C/16 and C/32  -1023 
  9030.      the minimum negative integer n such that 10 raised to the power n is in 
  9031.       the range of normalized floating-point numbers, or, 
  9032.         -  the base 10 exponent for the floating-point value that is closest, 
  9033.            but not equal, to zero 
  9034.  
  9035.          Macro: FLT_MIN_10_EXP Value 
  9036.          ANSI  <= -37 
  9037.          Watcom C/16 and C/32  -38 
  9038.  
  9039.  
  9040.          Macro: DBL_MIN_10_EXP Value 
  9041.          ANSI  <= -37 
  9042.          Watcom C/16 and C/32  -307 
  9043.  
  9044.  
  9045.          Macro: LDBL_MIN_10_EXP Value 
  9046.          ANSI  <= -37 
  9047.          Watcom C/16 and C/32  -307 
  9048.      the maximum integer n such that FLT_RADIX raised to the power n, minus 1, 
  9049.       is a representable finite floating-point number, or, 
  9050.         -  the maximum exponent value in terms of FLT_RADIX, or, 
  9051.         -  the base FLT_RADIX exponent for the largest valid floating-point 
  9052.            value 
  9053.  
  9054.          Macro: FLT_MAX_EXP Value 
  9055.          ANSI  no value specified 
  9056.          Watcom C/16 and C/32  127 
  9057.  
  9058.  
  9059.          Macro: DBL_MAX_EXP Value 
  9060.          ANSI  no value specified 
  9061.          Watcom C/16 and C/32  1023 
  9062.  
  9063.  
  9064.          Macro: LDBL_MAX_EXP Value 
  9065.          ANSI  no value specified 
  9066.          Watcom C/16 and C/32  1023 
  9067.      the maximum integer n such that 10 raised to the power n is a 
  9068.       representable finite floating-point number, or, 
  9069.         -  the base 10 exponent for the largest valid floating-point value 
  9070.  
  9071.          Macro: FLT_MAX_10_EXP Value 
  9072.          ANSI  >= 37 
  9073.          Watcom C/16 and C/32  38 
  9074.  
  9075.  
  9076.          Macro: DBL_MAX_10_EXP Value 
  9077.          ANSI  >= 37 
  9078.          Watcom C/16 and C/32  308 
  9079.  
  9080.  
  9081.          Macro: LDBL_MAX_10_EXP Value 
  9082.          ANSI  >= 37 
  9083.          Watcom C/16 and C/32  308 
  9084.      the maximum representable finite floating-point number 
  9085.  
  9086.  
  9087.          Macro: FLT_MAX Value 
  9088.          ANSI  >= 1E+37 
  9089.          Watcom C/16 and C/32  3.402823466E+38 
  9090.  
  9091.  
  9092.          Macro: DBL_MAX Value 
  9093.          ANSI  >= 1E+37 
  9094.          Watcom C/16 and C/32  1.79769313486231560E+308 
  9095.  
  9096.  
  9097.          Macro: LDBL_MAX Value 
  9098.          ANSI  >= 1E+37 
  9099.          Watcom C/16 and C/32  1.79769313486231560E+308 
  9100.      the difference between 1.0 and the least value greater than 1.0 that is 
  9101.       representable in the given floating-point type, or, 
  9102.         -  the smallest number eps such that (1.0 + eps) != 1.0 
  9103.  
  9104.          Macro: FLT_EPSILON Value 
  9105.          ANSI  <= 1E-5 
  9106.          Watcom C/16 and C/32  1.192092896E-15 
  9107.  
  9108.  
  9109.          Macro: DBL_EPSILON Value 
  9110.          ANSI  <= 1E-9 
  9111.          Watcom C/16 and C/32  2.2204460492503131E-16 
  9112.  
  9113.  
  9114.          Macro: LDBL_EPSILON Value 
  9115.          ANSI  <= 1E-9 
  9116.          Watcom C/16 and C/32  2.2204460492503131E-16 
  9117.      the minimum positive normalized floating-point number 
  9118.  
  9119.  
  9120.          Macro: FLT_MIN Value 
  9121.          ANSI  <= 1E-37 
  9122.          Watcom C/16 and C/32  1.175494351E-38 
  9123.  
  9124.  
  9125.          Macro: DBL_MIN Value 
  9126.          ANSI  <= 1E-37 
  9127.          Watcom C/16 and C/32  2.22507385850720160E-308 
  9128.  
  9129.  
  9130.          Macro: LDBL_MIN Value 
  9131.          ANSI  <= 1E-37 
  9132.          Watcom C/16 and C/32  2.22507385850720160E-308 
  9133.  
  9134.  As discussed in the section "Integer to Floating-Point Conversion", the macro 
  9135.  FLT_ROUNDS is replaced by a constant expression whose value indicates what 
  9136.  kind of rounding occurs following a floating-point operation.  The following 
  9137.  table gives the value of FLT_ROUNDS and its meaning: 
  9138.  
  9139.     FLT_ROUNDS    Technique 
  9140.     ----------  ------------------------ 
  9141.       -1    indeterminable 
  9142.       0    toward zero 
  9143.       1    to nearest number 
  9144.       2    toward positive infinity 
  9145.       3    toward negative infinity 
  9146.  
  9147.  If FLT_ROUNDS has any other value, the rounding mechanism is 
  9148.  implementation-defined. 
  9149.  
  9150.   For the Watcom C/16 and C/32 compiler, the value of FLT_ROUNDS is 1, meaning 
  9151.  that floating-point values are rounded to the nearest representable number. 
  9152.  
  9153.  
  9154. ΓòÉΓòÉΓòÉ 26. Implementation-Defined Behavior ΓòÉΓòÉΓòÉ
  9155.  
  9156.  
  9157. This appendix describes the behavior of Watcom C/16 and C/32 when the standard 
  9158. describes the behavior as implementation-defined.  The term describing each 
  9159. behavior is taken directly from the ANSI/ISO C Language standard.  The numbers 
  9160. in parentheses at the end of each term refers to the section of the standard 
  9161. that discusses the behavior. 
  9162.  
  9163.  
  9164. ΓòÉΓòÉΓòÉ 26.1. Translation ΓòÉΓòÉΓòÉ
  9165.  
  9166.  
  9167. How a diagnostic is identified (5.1.1.3). 
  9168.  
  9169.   A diagnostic message appears as: 
  9170.  
  9171.    filename(line-number):  error-type!  msg-number:  msg_text 
  9172.  
  9173.   where: 
  9174.  
  9175.  filename 
  9176.       is the name of the source file where the error was detected.  If the 
  9177.       error was found in a file included from the source file specified on the 
  9178.       compiler command line, then the name of the included file will appear. 
  9179.  
  9180.  line-number 
  9181.       is the source line number in the named file where the error was detected. 
  9182.  
  9183.  error-type 
  9184.       is either the word Error for errors that prevent the compile from 
  9185.       completing successfully (no code will be generated), or Warning for 
  9186.       conditions detected by the compiler that may not do what the programmer 
  9187.       expected, but are otherwise valid.  Warnings will not prevent the 
  9188.       compiler from generating code.  The issuance of warnings may be 
  9189.       controlled by a command-line switch.  See the User's Guide for details. 
  9190.  
  9191.  msg-number 
  9192.       is the letter E (for errors) followed by a four digit error number, or 
  9193.       the letter W (for warnings) followed by a three digit warning number. 
  9194.       Each message has its own unique message number. 
  9195.  
  9196.  msg-text 
  9197.       is a descriptive message indicating the problem. 
  9198.  
  9199.  Example: 
  9200.  
  9201.  
  9202.     test.c(35): Warning! W301: No prototype found for 'GetItem' 
  9203.     test.c(57): Error! E1009: Expecting '}' but found ',' 
  9204.  
  9205.  
  9206. ΓòÉΓòÉΓòÉ 26.2. Environment ΓòÉΓòÉΓòÉ
  9207.  
  9208.  
  9209. The semantics of the arguments to main (5.1.2.2.1). 
  9210.  
  9211.   Each blank-separated token, except within quoted strings, on the command line 
  9212. is made into a string that is an element of  argv.  Quoted strings are 
  9213. maintained as one element. 
  9214.  
  9215.   For example, for the command line, 
  9216.  
  9217.  
  9218.     pgm 2+ 1 tokens "one token" 
  9219.  
  9220.   argc would have the value 5, and the five elements of argv would be, 
  9221.  
  9222.  
  9223.     pgm 
  9224.     2+ 
  9225.     1 
  9226.     tokens 
  9227.     one token 
  9228.  
  9229. What constitutes an interactive device (5.1.2.3). 
  9230.  
  9231.   For Watcom C/16 and C/32, the keyboard and the video display are considered 
  9232. interactive devices. 
  9233.  
  9234.  
  9235. ΓòÉΓòÉΓòÉ 26.3. Behaviour Concerning Identifiers ΓòÉΓòÉΓòÉ
  9236.  
  9237.  
  9238. The number of significant initial characters (beyond 31) in an identifier 
  9239. without external linkage (6.1.2). 
  9240.  
  9241.   Unlimited. 
  9242.  
  9243. The number of significant initial characters (beyond 6) in an identifier with 
  9244. external linkage (6.1.2). 
  9245.  
  9246.   The Watcom C/16 and C/32 compilers do not impose a limit.  The Watcom Linker 
  9247. limits significant characters to 40. 
  9248.  
  9249. Whether case distinctions are significant in an identifier with external 
  9250. linkage (6.1.2). 
  9251.  
  9252.   The Watcom C/16 and C/32 compilers produce object names in mixed case.  The 
  9253. Watcom Linker provides an option to respect or ignore case when resolving 
  9254. linkages.  By default, the linker ignores case.  See the Watcom Linker User's 
  9255. Guide for details. 
  9256.  
  9257.  
  9258. ΓòÉΓòÉΓòÉ 26.4. Characters ΓòÉΓòÉΓòÉ
  9259.  
  9260.  
  9261. The members of the source and execution character sets, except as explicitly 
  9262. specified in the standard (5.2.1). 
  9263.  
  9264.   The full IBM PC character set is available in both the source and execution 
  9265. character sets.  The set of values between 0x20 and 0x7F are the ASCII 
  9266. character set. 
  9267.  
  9268. The shift states used for the encoding of multibyte characters (5.2.1.2). 
  9269.  
  9270.   There are no shift states in the support for multibyte characters. 
  9271.  
  9272. The number of bits in a character in the execution character set (5.2.4.2.1). 
  9273.  
  9274.   8 
  9275.  
  9276. The mapping of members of the source character set (in character constants and 
  9277. string literals) to members of the execution character set (6.1.3.4). 
  9278.  
  9279.   Both the source and execution character sets are the full IBM PC character 
  9280. set for whichever code page is in effect.  In addition, the following table 
  9281. shows escape sequences available in the source character set, and what they 
  9282. translate to in the execution character set. 
  9283.  
  9284.  
  9285.     Escape    Hex 
  9286.     Sequence   Value  Meaning 
  9287.     --------   -----  ------- 
  9288.     \a      07    Bell or alert 
  9289.     \b      08    Backspace 
  9290.     \f      0C    Form feed 
  9291.     \n      0A    New-line 
  9292.     \r      0D    Carriage return 
  9293.     \t      09    Horizontal tab 
  9294.     \v      0B    Vertical tab 
  9295.     \'      27    Apostrophe or single quote 
  9296.     \"      22    Double quote 
  9297.     \?      3F    Question mark 
  9298.     \\      5C    Backslash 
  9299.     \ddd         Octal value 
  9300.     \xddd        Hexadecimal value 
  9301.  
  9302. The value of an integer character constant that contains a character or escape 
  9303. sequence that is not represented in the execution character set or the extended 
  9304. character set for a wide character constant (6.1.3.4). 
  9305.  
  9306.   Not possible.  Both the source and execution character sets are the IBM PC 
  9307. character set.  Thus, all characters in the source character set map directly 
  9308. to the execution character set. 
  9309.  
  9310. The value of an integer character constant that contains more than one 
  9311. character or a wide character constant that contains more than one multibyte 
  9312. character (6.1.3.4). 
  9313.  
  9314.   A multi-character constant is stored with the right-most character in the 
  9315. lowest-order (least significant) byte, and subsequent characters (moving to the 
  9316. left) being placed in higher-order (more significant) bytes.  Up to four 
  9317. characters may be placed in a character constant. 
  9318.  
  9319. The current locale used to convert multibyte characters into corresponding wide 
  9320. characters (codes) for a wide character constant (6.1.3.4). 
  9321.  
  9322.   The Watcom C/16 and C/32 compilers currently support only the "C" locale, 
  9323. using North American English, and translates code page 437 to UNICODE. 
  9324.  
  9325.   To support multibyte characters, a command line switch can be used to 
  9326. indicate which multibyte character set to use.  See the User's Guide for 
  9327. details. 
  9328.  
  9329. Whether a plain  char has the same range of values as  signed char or  unsigned 
  9330. char (6.2.1.1). 
  9331.  
  9332.   Watcom C/16 and C/32 treat  char as  unsigned, although a compiler command 
  9333. line switch can be used to make it  signed. 
  9334.  
  9335.  
  9336. ΓòÉΓòÉΓòÉ 26.5. Integers ΓòÉΓòÉΓòÉ
  9337.  
  9338.  
  9339. The representations and sets of values of the various types of integers 
  9340. (6.1.2.5). 
  9341.  
  9342.   Integers are stored using 2's complement form.  The high bit of each signed 
  9343. integer is a sign bit.  If the sign bit is 1, the value is negative. 
  9344.  
  9345.   The ranges of the various integer types are described in the section "Integer 
  9346. Types". 
  9347.  
  9348. The result of converting an integer to a shorter signed integer, or the result 
  9349. of converting an unsigned integer to a signed integer of equal length, if the 
  9350. value cannot be represented (6.2.1.2). 
  9351.  
  9352.   When converting to a shorter type, the high-order bits of the longer value 
  9353. are discarded, and the remaining bits are interpreted according to the new 
  9354. type. 
  9355.  
  9356.   For example, converting the signed long integer -15584170 (hexadecimal 
  9357. 0xFF123456) to a signed short integer yields the result 13398 (hexadecimal 
  9358. 0x3456). 
  9359.  
  9360.   When converting an unsigned integer to a signed integer of equal length, the 
  9361. bits are simply re-interpreted according to the new type. 
  9362.  
  9363.   For example, converting the unsigned short integer 65535 (hexadecimal 0xFFFF) 
  9364. to a signed short integer yields the result -1 (hexadecimal 0xFFFF). 
  9365.  
  9366. The results of bitwise operations on signed integers (6.3). 
  9367.  
  9368.   The sign bit is treated as any other bit during bitwise operations.  At the 
  9369. completion of the operation, the new bit pattern is interpreted according to 
  9370. the result type. 
  9371.  
  9372. The sign of the remainder on integer division (6.3.5). 
  9373.  
  9374.   The remainder has the same sign as the numerator (left operand). 
  9375.  
  9376. The result of a right shift of a negative-valued signed integral type (6.3.7). 
  9377.  
  9378.   A right shift of a signed integer will leave the higher, vacated bits with 
  9379. the original value of the high bit.  In other words, the sign bit is propogated 
  9380. to fill bits vacated by the shift. 
  9381.  
  9382.   For example, the result of ((short) 0x0123) >> 4 would be 0x0012.  The result 
  9383. of ((short) 0xFEFE) >> 4 will be 0xFFEF. 
  9384.  
  9385.  
  9386. ΓòÉΓòÉΓòÉ 26.6. Floating Point ΓòÉΓòÉΓòÉ
  9387.  
  9388.  
  9389. The representations and sets of values of the various types of floating-point 
  9390. numbers (6.1.2.5). 
  9391.  
  9392.   These are discussed in the section "Floating-Point Types".  The 
  9393. floating-point format used is the IEEE Standard for Binary Floating-Point 
  9394. Arithmetic as defined in the ANSI/IEEE Standard 754-1985. 
  9395.  
  9396. The direction of truncation when an integral number is converted to a 
  9397. floating-point number that cannot exactly represent the original value 
  9398. (6.2.1.3). 
  9399.  
  9400.   Truncation is only possible when converting a  long int (signed or unsigned) 
  9401. to  float.  The 24 most-significant bits (including sign bit) are used.  The 
  9402. 25th is examined, and if it is 1, the value is rounded up by adding one to the 
  9403. 24-bit value.  The remaining bits are ignored. 
  9404.  
  9405. The direction of truncation or rounding when a floating-point number is 
  9406. converted to a narrower floating-point number (6.2.1.4). 
  9407.  
  9408.   The value is rounded to the nearest value in the smaller type. 
  9409.  
  9410.  
  9411. ΓòÉΓòÉΓòÉ 26.7. Arrays and Pointers ΓòÉΓòÉΓòÉ
  9412.  
  9413.  
  9414. The type of integer required to hold the maximum size of an array - that is, 
  9415. the type of the  sizeof operator,  size_t (6.3.3.4, 7.1.1). 
  9416.  
  9417.   unsigned int 
  9418.  
  9419. The result of casting an integer to a pointer or vice versa (6.3.4). 
  9420.  
  9421.   Watcom C/16 conversion of pointer to integer: 
  9422.  
  9423.  
  9424.     Pointer   short int 
  9425.     Type     int             long int 
  9426.     ----------- --------------------------- --------------------------- 
  9427.     near     result is pointer value   result is DS register in 
  9428.                         high-order 2 bytes, pointer 
  9429.                         value in low-order 2 bytes 
  9430.     ----------- --------------------------- --------------------------- 
  9431.     far     segment is discarded, result result is segment in high- 
  9432.     huge    is pointer offset (low-order order 2 bytes, offset in 
  9433.           2 bytes of pointer)      low-order 2 bytes 
  9434.  
  9435.   Watcom C/16 conversion of integer to pointer: 
  9436.  
  9437.  
  9438.     Integer                 far pointer 
  9439.     Type     near pointer         huge pointer 
  9440.     ----------- --------------------------- --------------------------- 
  9441.     short int  result is integer value   result segment is DS 
  9442.     int                   register, offset is 
  9443.                         integer value 
  9444.     ----------- --------------------------- --------------------------- 
  9445.     long int   result is low-order 2    result segment is high- 
  9446.           byte of integer value    order 2 bytes, offset is 
  9447.                         low-order 2 bytes 
  9448.  
  9449.   Watcom C/32 conversion of pointer to integer: 
  9450.  
  9451.  
  9452.     Pointer                 int 
  9453.     Type     short            long int 
  9454.     ----------- --------------------------- --------------------------- 
  9455.     near     result is low-order 2    result is pointer value 
  9456.           byte of pointer value 
  9457.     ----------- --------------------------- --------------------------- 
  9458.     far     segment is discarded,    segment is discarded, 
  9459.     huge     result is low-order 2    result is pointer offset 
  9460.           bytes of pointer value 
  9461.  
  9462.   Watcom C/32 conversion of integer to pointer: 
  9463.  
  9464.  
  9465.     Integer                 far pointer 
  9466.     Type     near pointer         huge pointer 
  9467.     ----------- --------------------------- --------------------------- 
  9468.     short int  result is integer value,   result segment is DS 
  9469.           with zeroes for high-order  register, offset is integer 
  9470.           2 bytes           value, with zeroes for 
  9471.                         high-order 2 bytes 
  9472.     ----------- --------------------------- --------------------------- 
  9473.     int     result is integer value   result segment is DS 
  9474.     long int                 register, offset is 
  9475.                         integer value 
  9476.  
  9477. The type of integer required to hold the difference between two pointers to 
  9478. elements of the same array,  ptrdiff_t (6.3.6, 7.1.1). 
  9479.  
  9480.   If the huge memory model is being used,  ptrdiff_t has type  long int. 
  9481.  
  9482.   For all other memory models,  ptrdiff_t has type  int. 
  9483.  
  9484.   If two huge pointers are subtracted and the huge memory model is not being 
  9485. used, then the result type will be  long int even though  ptrdiff_t is  int. 
  9486.  
  9487.  
  9488. ΓòÉΓòÉΓòÉ 26.8. Registers ΓòÉΓòÉΓòÉ
  9489.  
  9490.  
  9491. The extent to which objects can actually be placed in registers by use of the 
  9492. register storage-class specifier (6.5.1). 
  9493.  
  9494.   The Watcom C/16 and C/32 compilers may place any object that is sufficiently 
  9495. small, including a small structure, in one or more registers. 
  9496.  
  9497.   The number of objects that can be placed in registers varies, and is decided 
  9498. by the compiler.  The keyword  register does not control the placement of 
  9499. objects in registers. 
  9500.  
  9501.  
  9502. ΓòÉΓòÉΓòÉ 26.9. Structures, Unions, Enumerations and Bit-Fields ΓòÉΓòÉΓòÉ
  9503.  
  9504.  
  9505. A member of a union object is accessed using a member of a different type 
  9506. (6.3.2.3). 
  9507.  
  9508.   The behavior is undefined.  Whatever bit values are present as were stored 
  9509. via one member will be extracted via another. 
  9510.  
  9511. The padding and alignment of members of structures (6.5.2.1). 
  9512.  
  9513.   The Watcom C/16 and C/32 compilers do not align structure members by default. 
  9514. A command line switch, or the pack pragma, may be used to force the compiler to 
  9515. do alignment.  See the User's Guide for details. 
  9516.  
  9517. Whether a "plain"  int bit-field is treated as a  signed int bit-field or as an 
  9518. unsigned int bit-field (6.5.2.1). 
  9519.  
  9520.   signed int 
  9521.  
  9522. The order of allocation of bit-fields within a unit (6.5.2.1). 
  9523.  
  9524.   Low-order (least significant) bit to high-order bit. 
  9525.  
  9526. Whether a bit-field can straddle a storage-unit boundary (6.5.2.1). 
  9527.  
  9528.   Bit-fields may not straddle storage-unit boundaries.  If there is 
  9529. insufficient room to store a subsequent bit-field in a storage-unit, then it 
  9530. will be placed in the next storage-unit. 
  9531.  
  9532. The integer type chosen to represent the values of an enumeration type 
  9533. (6.5.2.2). 
  9534.  
  9535.   By default, Watcom C/16 and C/32 will use the smallest integer type that can 
  9536. accommodate all values in the enumeration. 
  9537.  
  9538.   Watcom C/16 will choose the first appropriate type from the following table: 
  9539.  
  9540.  
  9541.             Smallest  Largest 
  9542.     Type       Value   Value 
  9543.     -------------  --------  -------- 
  9544.     signed char    -128    127 
  9545.     unsigned char     0    255 
  9546.     signed int    -32768   32767 
  9547.     unsigned int     0   65535 
  9548.  
  9549.   Watcom C/32 will choose the first appropriate type from the following table: 
  9550.  
  9551.  
  9552.              Smallest    Largest 
  9553.     Type        Value     Value 
  9554.     -------------  ------------  ------------ 
  9555.     signed char       -128      127 
  9556.     unsigned char       0      255 
  9557.     signed short     -32768     32767 
  9558.     unsigned short       0     65535 
  9559.     signed int    -2147483648   2147483647 
  9560.     unsigned int        0   4294967295 
  9561.  
  9562.   Both compilers have a command-line switch that force all enumerations to type 
  9563. int.  See the User's Guide for details. 
  9564.  
  9565.  
  9566. ΓòÉΓòÉΓòÉ 26.10. Qualifiers ΓòÉΓòÉΓòÉ
  9567.  
  9568.  
  9569. What constitutes an access to an object that has volatile-qualified type 
  9570. (6.5.5.3). 
  9571.  
  9572.   Any reference to a volatile object is also an access to that object. 
  9573.  
  9574.  
  9575. ΓòÉΓòÉΓòÉ 26.11. Declarators ΓòÉΓòÉΓòÉ
  9576.  
  9577.  
  9578. The maximum number of declarators that may modify an arithmetic, structure or 
  9579. union type (6.5.4). 
  9580.  
  9581.   Limited only by available memory. 
  9582.  
  9583.  
  9584. ΓòÉΓòÉΓòÉ 26.12. Behaviour Concerning Statements ΓòÉΓòÉΓòÉ
  9585.  
  9586.  
  9587. The maximum number of  case values in a  switch statement (6.6.4.2). 
  9588.  
  9589.   Limited only by available memory. 
  9590.  
  9591.  
  9592. ΓòÉΓòÉΓòÉ 26.13. Preprocessing Directives ΓòÉΓòÉΓòÉ
  9593.  
  9594.  
  9595. Whether the value of a single-character character constant in a constant 
  9596. expression that controls conditional inclusion matches the value of the same 
  9597. character constant in the execution character set.  Whether such a character 
  9598. constant may have a negative value (6.8.1). 
  9599.  
  9600.   The character sets are the same so characters will match.  Character 
  9601. constants are unsigned quantities, so no character will be negative. 
  9602.  
  9603. The method for locating includable source files (6.8.2). 
  9604.  
  9605.   See the User's Guide for full details of how included files are located. 
  9606.  
  9607. The support of quoted names for includable source files (6.8.2). 
  9608.  
  9609.   See the User's Guide for full details of how included files are located. 
  9610.  
  9611. The mapping of source file character sequences (6.8.2). 
  9612.  
  9613.   The source and execution character sets are the same.  Escape sequences are 
  9614. not supported in preprocessor directives. 
  9615.  
  9616. The behavior of each recognized  #pragma directive (6.8.6). 
  9617.  
  9618.   See the User's Guide. 
  9619.  
  9620. The definitions for  __DATE__ and  __TIME__ when respectively, the date and 
  9621. time of translation are not available (6.8.8). 
  9622.  
  9623.   The date and time are always available. 
  9624.  
  9625.  
  9626. ΓòÉΓòÉΓòÉ 26.14. Library Functions ΓòÉΓòÉΓòÉ
  9627.  
  9628.  
  9629. The null pointer constant to which the macro  NULL expands (7.1.6). 
  9630.  
  9631.   For Watcom C/16, the  NULL macro expands to 0 for the small and medium (small 
  9632. data) memory models, and to 0L for the compact, large and huge (big data) 
  9633. memory models. 
  9634.  
  9635.   For Watcom C/32, the  NULL macro expands to 0. 
  9636.  
  9637. The implementation-defined behavior of the library functions is described in 
  9638. the Watcom C Library Reference manual. 
  9639.  
  9640.  
  9641. ΓòÉΓòÉΓòÉ 27. Examples of Declarations ΓòÉΓòÉΓòÉ
  9642.  
  9643.  
  9644. This chapter presents a series of examples of declarations of objects and 
  9645. functions.  Along with each example is a description that indicates how to read 
  9646. the declaration. 
  9647.  
  9648. This chapter may be used as a "cookbook" for declarations.  Some complicated 
  9649. but commonly required declarations are given here. 
  9650.  
  9651. The first examples are very simple, and build in complexity.  Some of the 
  9652. examples given near the end of each section are unlikely to ever be required in 
  9653. a real program, but hopefully they will provide an understanding of how to read 
  9654. and write C declarations. 
  9655.  
  9656. To reduce the complexity and to better illustrate how a small difference in the 
  9657. declaration can mean a big difference in the meaning, the following rules are 
  9658. followed: 
  9659.  
  9660.    1. if an object is being declared, it is called x or X, 
  9661.  
  9662.    2. if a function is being declared, it is called F, 
  9663.  
  9664.    3. if an object is being declared, it usually has type  int, although any 
  9665.       other type may be substituted, 
  9666.  
  9667.    4. if a function is being declared, it usually returns type  int, although 
  9668.       any other type may be substituted. 
  9669.  
  9670.  Storage class specifiers ( extern,  static,  auto or  register) have purposely 
  9671.  been omitted. 
  9672.  
  9673.  
  9674. ΓòÉΓòÉΓòÉ 27.1. Object Declarations ΓòÉΓòÉΓòÉ
  9675.  
  9676.  
  9677. Here are some examples of object (variable) declarations: 
  9678.  
  9679.  
  9680.    int x; 
  9681.    2  1 
  9682.  
  9683.   (1)x is an (2)integer. 
  9684.  
  9685.    int * x; 
  9686.    3  2 1 
  9687.  
  9688.   (1)x is a (2)pointer to an (3)integer. 
  9689.  
  9690.    int ** x; 
  9691.    4  32 1 
  9692.  
  9693.   (1)x is a (2)pointer to a (3)pointer to an (4)integer. 
  9694.  
  9695.    const int x; 
  9696.     2   3  1 
  9697.  
  9698.   (1)x is a (2)constant (3)integer. 
  9699.  
  9700.    int const x; 
  9701.    3   2  1 
  9702.  
  9703.   (1)x is a (2)constant (3)integer (same as above). 
  9704.  
  9705.    const int * x; 
  9706.     3   4  2 1 
  9707.  
  9708.   (1)x is a (2)pointer to a (3)constant (4)integer.  The value of x may change, 
  9709. but the integer that it points to may not be changed.  In other words, *x 
  9710. cannot be modified. 
  9711.  
  9712.    int * const x; 
  9713.    4  3  2  1 
  9714.  
  9715.   (1)x is a (2)constant (3)pointer to an (4)integer.  The value of x may not 
  9716. change, but the integer that it points to may change.  In other words, x will 
  9717. always point at the same location, but the contents of that location may vary. 
  9718.  
  9719.    const int * const x; 
  9720.     4   5  3  2  1 
  9721.  
  9722.   (1)x is a (2)constant (3)pointer to a (4)constant (5)integer.  The value of x 
  9723. may not change, and the integer that it points to may not change.  In other 
  9724. words, x will always point at the same location, which cannot be modified via 
  9725. x. 
  9726.  
  9727.    int x[]; 
  9728.    3  12 
  9729.  
  9730.   (1)x is an (2)array of (3)integers. 
  9731.  
  9732.    int x[53]; 
  9733.    4  123 
  9734.  
  9735.   (1)x is an (2)array of (3)53 (4)integers. 
  9736.  
  9737.    int * x[]; 
  9738.    4  3 12 
  9739.  
  9740.   (1)x is an (2)array of (3)pointers to (4)integer. 
  9741.  
  9742.    int (*x)[]; 
  9743.    4  21 3 
  9744.  
  9745.   (1)x is a (2)pointer to an (3)array of (4)integers. 
  9746.  
  9747.    int * (*x)[]; 
  9748.    5  4  21 3 
  9749.  
  9750.   (1)x is a (2)pointer to an (3)array of (4)pointers to (5)integer. 
  9751.  
  9752.    int (*x)(); 
  9753.    4  21 3 
  9754.  
  9755.   (1)x is a (2)pointer to a (3)function returning an (4)integer. 
  9756.  
  9757.    int (*x[25])(); 
  9758.    6  4123  5 
  9759.  
  9760.   (1)x is an (2)array of (3)25 (4)pointers to (5)functions returning an 
  9761. (6)integer. 
  9762.  
  9763.  
  9764. ΓòÉΓòÉΓòÉ 27.2. Function Declarations ΓòÉΓòÉΓòÉ
  9765.  
  9766.  
  9767. Here are some examples of function declarations: 
  9768.  
  9769.  
  9770.    int F(); 
  9771.    3  12 
  9772.  
  9773.   (1)F is a (2)function returning an (3)integer. 
  9774.  
  9775.    int * F(); 
  9776.    4  3 12 
  9777.  
  9778.   (1)F is a (2)function returning a (3)pointer to an (4)integer. 
  9779.  
  9780.    int (*F())(); 
  9781.    5  312  4 
  9782.  
  9783.   (1)F is a (2)function returning a (3)pointer to a (4)function returning an 
  9784. (5)integer. 
  9785.  
  9786.    int * (*F())(); 
  9787.    6  5  312  4 
  9788.  
  9789.   (1)F is a (2)function returning a (3)pointer to a (4)function returning a 
  9790. (5)pointer to an (6)integer. 
  9791.  
  9792.    int (*F())[]; 
  9793.    5  312  4 
  9794.  
  9795.   (1)F is a (2)function returning a (3)pointer to an (4)array of (5)integers. 
  9796.  
  9797.    int (*(*F())[])(); 
  9798.    7  5 312  4  6 
  9799.  
  9800.   (1)F is a (2)function returning a (3)pointer to an (4)array of (5)pointers to 
  9801. (6)functions returning an (7)integer. 
  9802.  
  9803.    int * (*(*F())[])(); 
  9804.    8  7  5 312  4  6 
  9805.  
  9806.   (1)F is a (2)function returning a (3)pointer to an (4)array of (5)pointers to 
  9807. (6)functions returning a (7)pointer to an (8)integer. 
  9808.  
  9809.  
  9810. ΓòÉΓòÉΓòÉ 27.3. _ _far, _ _near and _ _huge Declarations ΓòÉΓòÉΓòÉ
  9811.  
  9812.  
  9813. The following examples illustrate the use of the  __far and  __huge keywords. 
  9814.  
  9815. The use of the  __near keyword is symmetrical with the use of the  __far 
  9816. keyword, so no examples of  __near are shown. 
  9817.  
  9818.  
  9819.    int __far X; 
  9820.    3   2  1 
  9821.  
  9822.   (1)X is a (2)far (3)integer. 
  9823.  
  9824.    int * __far x; 
  9825.    4  3  2  1 
  9826.  
  9827.   (1)x is (2)far, and is a (3)pointer to an (4)integer. 
  9828.  
  9829.    int __far * x; 
  9830.    4   2  3 1 
  9831.  
  9832.   (1)x is a (2)far (3)pointer to an (4)integer. 
  9833.  
  9834.    int __far * __far x; 
  9835.    5   3  4  2  1 
  9836.  
  9837.   (1)x is (2)far, and is a (3)far (4)pointer to an (5)integer. 
  9838.  
  9839.    int __far X[]; 
  9840.    4   2  13 
  9841.  
  9842.   (1)X is a (2)far (3)array of (4)integers. 
  9843.  
  9844.    int __huge X[]; 
  9845.    4   2   13 
  9846.  
  9847.   (1)x is a (2)huge (3)array of (4)integers (X is an array that can exceed 64K 
  9848. in size.) 
  9849.  
  9850.    int * __far X[]; 
  9851.    5  4  2  13 
  9852.  
  9853.   (1)X is a (2)far (3)array of (4)pointers to (5)integers. 
  9854.  
  9855.    int __far * X[]; 
  9856.    5   3  4 12 
  9857.  
  9858.   (1)X is an (2)array of (3)far (4)pointers to (5)integers. 
  9859.  
  9860.    int __far * __far X[]; 
  9861.    6   4  5  2  13 
  9862.  
  9863.   (1)X is a (2)far (3)array of (4)far (5)pointers to (6)integers. 
  9864.  
  9865.    int __far F(); 
  9866.    4   2  13 
  9867.  
  9868.   (1)F is a (2)far (3)function returning an (4)integer. 
  9869.  
  9870.    int * __far F(); 
  9871.    5  4  2  13 
  9872.  
  9873.   (1)F is a (2)far (3)function returning a (4)pointer to an (5)integer. 
  9874.  
  9875.    int __far * F(); 
  9876.    5   3  4 12 
  9877.  
  9878.   (1)F is a (2)function returning a (3)far (4)pointer to an (5)integer. 
  9879.  
  9880.    int __far * __far F(); 
  9881.    6   4  5  2  13 
  9882.  
  9883.   (1)F is a (2)far (3)function returning a (4)far (5)pointer to an (6)integer. 
  9884.  
  9885.    int (__far * x)(); 
  9886.    5   2  3 1 4 
  9887.  
  9888.   (1)x is a (2)far (3)pointer to a (4)function returning an (5)integer. 
  9889.  
  9890.    int __far * (* x)(); 
  9891.    6   4  5  2 1 3 
  9892.  
  9893.   (1)x is a (2)pointer to a (3)function returning a (4)far (5)pointer to an 
  9894. (6)integer. 
  9895.  
  9896.    int __far * (__far * x)(); 
  9897.    7   5  6   2  3 1 4 
  9898.  
  9899.   (1)x is a (2)far (3)pointer to a (4)function returning a (5)far (6)pointer to 
  9900. an (7)integer. 
  9901.  
  9902.  
  9903. ΓòÉΓòÉΓòÉ 27.4. _ _interrupt Declarations ΓòÉΓòÉΓòÉ
  9904.  
  9905.  
  9906. The following example illustrates the use of the  __interrupt keyword. 
  9907.  
  9908.  
  9909.    void __interrupt __far F(); 
  9910.    5     3    2  14 
  9911.  
  9912.   (1)F is a (2)far (3)interrupt (4)function returning (5)nothing. 
  9913.  
  9914.  
  9915. ΓòÉΓòÉΓòÉ 28. A Sample Program ΓòÉΓòÉΓòÉ
  9916.  
  9917.  
  9918. This chapter presents an entire C program, to illustrate many of the features 
  9919. of the language, and to illustrate elements of programming style. 
  9920.  
  9921. This program implements a memo system suitable for maintaining a set of memos, 
  9922. and displaying them on the screen.  The program allows the user to display 
  9923. memos relevant to today's date, move through the memos adding new ones and 
  9924. replacing or deleting existing ones.  The program displays help information 
  9925. whenever an invalid action is entered, or when the sole parameter to the 
  9926. program is a question mark. 
  9927.  
  9928. The program is in complete conformance to the ANSI C standard.  It should be 
  9929. able to run, without modification, on any system that provides an 
  9930. ANSI-conforming C compiler. 
  9931.  
  9932.  
  9933. ΓòÉΓòÉΓòÉ 28.1. The memos.h File ΓòÉΓòÉΓòÉ
  9934.  
  9935.  
  9936. The source file memos.h contains the structures used for storing the memos: 
  9937.  
  9938.  
  9939.    /* This structure is for an individual line in a memo. 
  9940.    */ 
  9941.    typedef struct text_line { 
  9942.      struct text_line * next; 
  9943.      char        text[1]; 
  9944.    } TEXT_LINE; 
  9945.  
  9946.    /* This structure is the head of an individual memo. 
  9947.    */ 
  9948.    typedef struct memo_el { 
  9949.      struct memo_el * prev; 
  9950.      struct memo_el * next; 
  9951.      TEXT_LINE *    text; 
  9952.      char       date[9]; 
  9953.    } MEMO_EL; 
  9954.  
  9955.  
  9956. ΓòÉΓòÉΓòÉ 28.2. The memos.c File ΓòÉΓòÉΓòÉ
  9957.  
  9958.  
  9959. The source for the program follows: 
  9960.  
  9961.  
  9962.    #include <stdio.h> 
  9963.    #include <stdlib.h> 
  9964.    #include <string.h> 
  9965.    #include <ctype.h> 
  9966.    #include <time.h> 
  9967.  
  9968.    #include "memos.h" 
  9969.  
  9970.    /* This program implements a simple memo facility. 
  9971.    * Memos may be added to a memo file, displayed 
  9972.    * on the screen, and deleted. 
  9973.    * 
  9974.    * Modified   by        reason 
  9975.    * ========   ==        ====== 
  9976.    * 87/10/02   Steve McDowell  Initial implementation. 
  9977.    * 88/09/20   Steve McDowell  Fixed up some style issues, 
  9978.    *                introduced use of TRUE and 
  9979.    *                FALSE. 
  9980.    */ 
  9981.  
  9982.    /* Define some constants to make the code more readable. 
  9983.    */ 
  9984.    #define TRUE     1 
  9985.    #define FALSE    0 
  9986.    #define NULLCHAR   '\0' 
  9987.  
  9988.    static const char FileName[] = { "memos.db" }; 
  9989.    static const char TempName[] = { "tempmemo.db" }; 
  9990.  
  9991.    static MEMO_EL *  MemoHead    = NULL; 
  9992.    static int     MemosModified = FALSE; 
  9993.    static int     QuitFlag    = TRUE; 
  9994.  
  9995.    typedef enum { 
  9996.      INVALID, 
  9997.      HELP, 
  9998.      ADD, 
  9999.      DELETE, 
  10000.      REPLACE, 
  10001.      SHOW, 
  10002.      UP, 
  10003.      DOWN, 
  10004.      TOP, 
  10005.      TODAY, 
  10006.      SAVE, 
  10007.      QUIT 
  10008.    } ACTION; 
  10009.  
  10010.    /* This table maps action keywords onto the "actions" defined 
  10011.    * above. The table also defines short forms for the keywords. 
  10012.    */ 
  10013.    typedef struct { 
  10014.      ACTION act; 
  10015.      char * keyword; 
  10016.    } ACTION_MAP; 
  10017.  
  10018.    static ACTION_MAP KeywordMap[] = { 
  10019.      HELP,   "help", 
  10020.      HELP,   "h", 
  10021.      ADD,   "add", 
  10022.      ADD,   "a", 
  10023.      DELETE,  "delete", 
  10024.      DELETE,  "del", 
  10025.      REPLACE, "replace", 
  10026.      REPLACE, "rep", 
  10027.      SHOW,   "show", 
  10028.      SHOW,   "sh", 
  10029.      UP,    "up", 
  10030.      UP,    "u", 
  10031.      DOWN,   "down", 
  10032.      DOWN,   "d", 
  10033.      DOWN,   "", 
  10034.      TOP,   "top", 
  10035.      TODAY,  "today", 
  10036.      TODAY,  "tod", 
  10037.      SAVE,   "save", 
  10038.      SAVE,   "sa", 
  10039.      QUIT,   "quit", 
  10040.      QUIT,   "q", 
  10041.  
  10042.      INVALID, "" }; 
  10043.  
  10044.    /* Maximum buffer length (maximum length of line of memo). 
  10045.    */ 
  10046.    #define MAXLEN 80 
  10047.  
  10048.    /* Function prototypes. 
  10049.    */ 
  10050.    static TEXT_LINE *  AddLine(); 
  10051.    static MEMO_EL *   AddMemo(); 
  10052.    static MEMO_EL *   DeleteMemo(); 
  10053.    static MEMO_EL *   DoActions(); 
  10054.    static MEMO_EL *   DoDownAction(); 
  10055.    static MEMO_EL *   DoUpAction(); 
  10056.    static MEMO_EL *   EnterAMemo(); 
  10057.    static ACTION    GetAction(); 
  10058.    static void *    MemoMAlloc(); 
  10059.    static ACTION    PromptAction(); 
  10060.    static ACTION    ReadAction(); 
  10061.    static MEMO_EL *   ReadAMemo(); 
  10062.    static MEMO_EL *   ShowTodaysMemos(); 
  10063.  
  10064.    extern int main( int argc, char * argv[] ) 
  10065.    /****************************************/ 
  10066.    { 
  10067.      int    index; 
  10068.      MEMO_EL * el; 
  10069.  
  10070.      printf( "Memo facility\n" ); 
  10071.  
  10072.    /* Check for a single argument that is a question mark, 
  10073.    * If found, then display the usage notes. 
  10074.    */ 
  10075.      if( argc == 2  &&  strcmp( argv[1], "?" ) == 0 ) { 
  10076.        Usage(); 
  10077.        exit( 0 ); 
  10078.      } 
  10079.      ReadMemos(); 
  10080.      MemosModified = FALSE; 
  10081.      QuitFlag    = FALSE; 
  10082.  
  10083.    /* Use the command line parameters, if any, as the first 
  10084.    * actions to be performed on the memos. 
  10085.    */ 
  10086.      el = NULL; 
  10087.      for( index = 1; index < argc; ++index ) { 
  10088.        el = DoActions( el, GetAction( argv[index] ) ); 
  10089.        if( QuitFlag ) { 
  10090.          return( FALSE ); 
  10091.        } 
  10092.      } 
  10093.      HandleMemoActions( el ); 
  10094.      return( FALSE ); 
  10095.    } 
  10096.  
  10097.    static void ReadMemos( void ) 
  10098.    /***************************/ 
  10099.  
  10100.    /* Read the memos file, building the structure to contain it. 
  10101.    */ 
  10102.    { 
  10103.      FILE *   fid; 
  10104.      MEMO_EL * new_el; 
  10105.      MEMO_EL * prev_el; 
  10106.      int    mcount; 
  10107.  
  10108.      fid = fopen( FileName, "r" ); 
  10109.  
  10110.      if( fid == NULL ) { 
  10111.        printf( "Memos file not found." 
  10112.            " Starting with no memos.\n" ); 
  10113.        return; 
  10114.      } 
  10115.  
  10116.    /* Loop reading entire memos. 
  10117.    */ 
  10118.      prev_el = NULL; 
  10119.      for( mcount = 0;; mcount++ ) { 
  10120.        new_el = ReadAMemo( fid ); 
  10121.        if( new_el == NULL ) { 
  10122.          printf( "%d memo(s) found.\n", mcount ); 
  10123.          fclose( fid ); 
  10124.          return; 
  10125.        } 
  10126.  
  10127.        if( prev_el == NULL ) { 
  10128.          MemoHead = new_el; 
  10129.          new_el->prev = NULL; 
  10130.        } else { 
  10131.          prev_el->next = new_el; 
  10132.          new_el->prev = prev_el; 
  10133.        } 
  10134.  
  10135.        new_el->next = NULL; 
  10136.        prev_el = new_el; 
  10137.      } 
  10138.    } 
  10139.    static int ReadLine( char buffer[], int len, FILE * fid ) 
  10140.    /*******************************************************/ 
  10141.  
  10142.    /* Read a line from the memos file. Handle any I/O errors and 
  10143.    * EOF. Return the length read, not counting the newline on 
  10144.    * the end. 
  10145.    */ 
  10146.    { 
  10147.      if( fgets( buffer, len, fid ) == NULL ) { 
  10148.        if( feof( fid ) ) { 
  10149.          return( EOF ); 
  10150.        } 
  10151.        perror( "Error reading memos file" ); 
  10152.        abort(); 
  10153.      } 
  10154.  
  10155.      return( strlen( buffer ) - 1 ); 
  10156.    } 
  10157.  
  10158.    static MEMO_EL * ReadAMemo( FILE * fid ) 
  10159.    /**************************************/ 
  10160.  
  10161.    /* Read one memo, creating the memo structure and filling it 
  10162.    * in. Return a pointer to the memo (NULL if none read). 
  10163.    */ 
  10164.    { 
  10165.      MEMO_EL *  el; 
  10166.      int     len; 
  10167.      TEXT_LINE * line; 
  10168.      char     buffer[MAXLEN]; 
  10169.  
  10170.      len = ReadLine( buffer, MAXLEN, fid ); 
  10171.      if( len == EOF ) { 
  10172.        return( NULL ); 
  10173.      } 
  10174.  
  10175.    /* First line must be of the form "Date:" or "Date:YY/MM/DD": 
  10176.    */ 
  10177.      if( (len != 5  &&  len != 13) 
  10178.          ||  strncmp( buffer, "Date:", 5 ) != 0 ) { 
  10179.        BadFormat(); 
  10180.      } 
  10181.  
  10182.      buffer[len] = NULLCHAR; 
  10183.      el = MemoMAlloc( sizeof( MEMO_EL ) ); 
  10184.      el->text = NULL; 
  10185.      strcpy( el->date, buffer + 5 ); 
  10186.      line = NULL; 
  10187.  
  10188.      for( ;; ) { 
  10189.        len = ReadLine( buffer, MAXLEN, fid ); 
  10190.        if( len == EOF ) { 
  10191.          BadFormat(); 
  10192.        } 
  10193.  
  10194.        buffer[len] = NULLCHAR; 
  10195.        if( strcmp( buffer, "====" ) == 0 ) { 
  10196.          return( el ); 
  10197.        } 
  10198.        line = AddLine( buffer, el, line ); 
  10199.      } 
  10200.    } 
  10201.  
  10202.    static TEXT_LINE * AddLine( char     buffer[], 
  10203.                  MEMO_EL *  el, 
  10204.                  TEXT_LINE * prevline ) 
  10205.    /************************************************/ 
  10206.  
  10207.    /* Add a line of text to the memo, taking care of all the 
  10208.    * details of modifying the structure. 
  10209.    */ 
  10210.    { 
  10211.      TEXT_LINE * line; 
  10212.  
  10213.      line = MemoMAlloc( sizeof( TEXT_LINE ) + strlen( buffer ) ); 
  10214.      strcpy( line->text, buffer ); 
  10215.      line->next = NULL; 
  10216.  
  10217.      if( prevline == NULL ) { 
  10218.        el->text = line; 
  10219.      } else { 
  10220.        prevline->next = line; 
  10221.      } 
  10222.      return( line ); 
  10223.    } 
  10224.  
  10225.    static ACTION PromptAction( void ) 
  10226.    /********************************/ 
  10227.  
  10228.    /* The user didn't specify an action on the command line, 
  10229.    * so prompt for it. 
  10230.    */ 
  10231.    { 
  10232.      ACTION act; 
  10233.  
  10234.      for( ;; ) { 
  10235.        printf( "\nEnter an action:\n" ); 
  10236.        act = ReadAction(); 
  10237.  
  10238.        if( act != INVALID ) { 
  10239.          return( act ); 
  10240.        } 
  10241.        printf( "\nThat selection was not valid.\n" ); 
  10242.        Help(); 
  10243.      } 
  10244.    } 
  10245.  
  10246.    static ACTION ReadAction( void ) 
  10247.    /******************************/ 
  10248.  
  10249.    /* Read an action from the terminal. 
  10250.    * Return the action code. 
  10251.    */ 
  10252.    { 
  10253.      char buffer[80]; 
  10254.  
  10255.      if( gets( buffer ) == NULL ) { 
  10256.        perror( "Error reading action" ); 
  10257.        abort(); 
  10258.      } 
  10259.  
  10260.      return( GetAction( buffer ) ); 
  10261.    } 
  10262.  
  10263.    static ACTION GetAction( char buffer[] ) 
  10264.    /**************************************/ 
  10265.  
  10266.    /* Given the string in the buffer, return the action that 
  10267.    * corresponds to it. 
  10268.    * The string in the buffer is first zapped into lower case 
  10269.    * so that mixed-case entries are recognized. 
  10270.    */ 
  10271.    { 
  10272.      ACTION_MAP * actmap; 
  10273.      char *    bufptr; 
  10274.  
  10275.      for( bufptr = buffer; *bufptr != NULLCHAR; ++bufptr ) { 
  10276.        *bufptr = tolower( *bufptr ); 
  10277.      } 
  10278.  
  10279.      for( actmap = KeywordMap; actmap->act != INVALID; ++actmap ) { 
  10280.        if( strcmp( buffer, actmap->keyword ) == 0 ) break; 
  10281.      } 
  10282.      return( actmap->act ); 
  10283.    } 
  10284.  
  10285.    static void HandleMemoActions( MEMO_EL * el ) 
  10286.    /*******************************************/ 
  10287.  
  10288.    /* Handle all the actions entered from the keyboard. 
  10289.    */ 
  10290.    { 
  10291.      for( ;; ) { 
  10292.        el = DoActions( el, PromptAction() ); 
  10293.        if( QuitFlag ) break; 
  10294.      } 
  10295.    } 
  10296.  
  10297.    static MEMO_EL * DoActions( MEMO_EL * el, ACTION act ) 
  10298.    /****************************************************/ 
  10299.  
  10300.    /* Perform one action on the memos. 
  10301.    */ 
  10302.    { 
  10303.      MEMO_EL * new_el; 
  10304.      MEMO_EL * prev_el; 
  10305.  
  10306.      switch( act ) { 
  10307.       case HELP: 
  10308.        Help(); 
  10309.        break; 
  10310.  
  10311.       case ADD: 
  10312.        new_el = AddMemo( el ); 
  10313.        if( new_el != NULL ) { 
  10314.          el = new_el; 
  10315.          MemosModified = TRUE; 
  10316.        } 
  10317.        break; 
  10318.  
  10319.       case DELETE: 
  10320.        el = DeleteMemo( el ); 
  10321.        MemosModified = TRUE; 
  10322.        break; 
  10323.       case REPLACE: 
  10324.        prev_el = el; 
  10325.        new_el = AddMemo( el ); 
  10326.        if( new_el != NULL ) { 
  10327.          DeleteMemo( prev_el ); 
  10328.          MemosModified = TRUE; 
  10329.        } 
  10330.        break; 
  10331.  
  10332.       case SHOW: 
  10333.        DisplayMemo( el ); 
  10334.        break; 
  10335.  
  10336.       case UP: 
  10337.        el = DoUpAction( el ); 
  10338.        break; 
  10339.  
  10340.       case DOWN: 
  10341.        el = DoDownAction( el ); 
  10342.        break; 
  10343.  
  10344.       case TOP: 
  10345.        el = NULL; 
  10346.        break; 
  10347.  
  10348.       case TODAY: 
  10349.        el = ShowTodaysMemos(); 
  10350.        break; 
  10351.  
  10352.       case SAVE: 
  10353.        if( SaveMemos() ) { 
  10354.          MemosModified = FALSE; 
  10355.        } 
  10356.        break; 
  10357.  
  10358.       case QUIT: 
  10359.        if( WantToQuit() ) { 
  10360.          QuitFlag = TRUE; 
  10361.          el = NULL; 
  10362.        } 
  10363.      } 
  10364.  
  10365.      return( el ); 
  10366.    } 
  10367.  
  10368.    static MEMO_EL * AddMemo( MEMO_EL * el ) 
  10369.    /**************************************/ 
  10370.  
  10371.    /* Add a memo following the current one. 
  10372.    */ 
  10373.    { 
  10374.      MEMO_EL * new_el; 
  10375.      MEMO_EL * next; 
  10376.  
  10377.      new_el = EnterAMemo(); 
  10378.      if( new_el == NULL ) { 
  10379.        return( NULL ); 
  10380.      } 
  10381.  
  10382.      if( el == NULL ) { 
  10383.        next = MemoHead; 
  10384.        MemoHead = new_el; 
  10385.      } else { 
  10386.        next = el->next; 
  10387.        el->next = new_el; 
  10388.      } 
  10389.  
  10390.      new_el->prev = el; 
  10391.      new_el->next = next; 
  10392.      if( next != NULL ) { 
  10393.        next->prev = new_el; 
  10394.      } 
  10395.      return( new_el ); 
  10396.    } 
  10397.  
  10398.    static MEMO_EL * EnterAMemo( void ) 
  10399.    /*********************************/ 
  10400.  
  10401.    /* Read a memo from the keyboard, creating the memo structure 
  10402.    * and filling it in. Return a pointer to the memo (NULL if 
  10403.    * none read). 
  10404.    */ 
  10405.    { 
  10406.      MEMO_EL *  el; 
  10407.      int     len; 
  10408.      TEXT_LINE * line; 
  10409.      char     buffer[MAXLEN]; 
  10410.  
  10411.      printf( "What date do you want the memo displayed" 
  10412.          " (YY/MM/DD)?\n" ); 
  10413.  
  10414.      if( gets( buffer ) == NULL ) { 
  10415.        printf( "Error reading from terminal.\n" ); 
  10416.        return( NULL ); 
  10417.      } 
  10418.  
  10419.      len = strlen( buffer ); 
  10420.  
  10421.      if( len != 0 
  10422.          &&  (len != 8 
  10423.            ||  buffer[2] != '/' 
  10424.            || buffer[5] != '/') ) { 
  10425.        printf( "Date is not valid.\n" ); 
  10426.        return( NULL ); 
  10427.      } 
  10428.  
  10429.      el = MemoMAlloc( sizeof( MEMO_EL ) ); 
  10430.      el->text = NULL; 
  10431.  
  10432.      strcpy( el->date, buffer ); 
  10433.      line = NULL; 
  10434.  
  10435.      printf( "\nEnter the text of the memo.\n" ); 
  10436.      printf( "To terminate the memo," 
  10437.          " enter a line starting with =\n" ); 
  10438.  
  10439.      for( ;; ) { 
  10440.        if( gets( buffer ) == NULL ) { 
  10441.          printf( "Error reading from terminal.\n" ); 
  10442.          return( NULL ); 
  10443.        } 
  10444.  
  10445.        if( buffer[0] == '=' ) { 
  10446.          return( el ); 
  10447.        } 
  10448.        line = AddLine( buffer, el, line ); 
  10449.      } 
  10450.    } 
  10451.  
  10452.    static MEMO_EL * DeleteMemo( MEMO_EL * el ) 
  10453.    /*****************************************/ 
  10454.  
  10455.    /* Delete the current memo. 
  10456.    * Return a pointer to another memo, usually the following one. 
  10457.    */ 
  10458.    { 
  10459.      MEMO_EL * prev; 
  10460.      MEMO_EL * next; 
  10461.      MEMO_EL * ret_el; 
  10462.  
  10463.      if( el == NULL ) { 
  10464.        return( MemoHead ); 
  10465.      } 
  10466.  
  10467.      prev = el->prev; 
  10468.      next = el->next; 
  10469.      ret_el = next; 
  10470.      if( ret_el == NULL ) { 
  10471.        ret_el = prev; 
  10472.      } 
  10473.  
  10474.    /* If it's the first memo, set a new MemoHead value. 
  10475.    */ 
  10476.      if( prev == NULL ) { 
  10477.        MemoHead = next; 
  10478.        if( next != NULL ) { 
  10479.          next->prev = NULL; 
  10480.        } 
  10481.  
  10482.      } else { 
  10483.        prev->next = next; 
  10484.        if( next != NULL ) { 
  10485.          next->prev = prev; 
  10486.        } 
  10487.      } 
  10488.  
  10489.      DisposeMemo( el ); 
  10490.      return( ret_el ); 
  10491.    } 
  10492.  
  10493.    static MEMO_EL * DoUpAction( MEMO_EL * el ) 
  10494.    /*****************************************/ 
  10495.  
  10496.    /* Perform the UP action, including displaying the memo. 
  10497.    */ 
  10498.    { 
  10499.      if( el == NULL ) { 
  10500.        DisplayTop(); 
  10501.      } else { 
  10502.        el = el->prev; 
  10503.        DisplayMemo( el ); 
  10504.      } 
  10505.      return( el ); 
  10506.    } 
  10507.    static MEMO_EL * DoDownAction( MEMO_EL * el ) 
  10508.    /*******************************************/ 
  10509.  
  10510.    /* Perform the DOWN action, including displaying the memo. 
  10511.    */ 
  10512.    { 
  10513.      MEMO_EL * next_el; 
  10514.  
  10515.      next_el = (el == NULL) ? MemoHead : el->next; 
  10516.      if( next_el == NULL ) { 
  10517.        printf( "No more memos.\n" ); 
  10518.      } else { 
  10519.        el = next_el; 
  10520.        DisplayMemo( el ); 
  10521.      } 
  10522.      return( el ); 
  10523.    } 
  10524.  
  10525.    static MEMO_EL * ShowTodaysMemos( void ) 
  10526.    /**************************************/ 
  10527.  
  10528.    /* Show all memos that either: 
  10529.    * (1) match today's date 
  10530.    * (2) don't have a date stored. 
  10531.    * Return a pointer to the last displayed memo. 
  10532.    */ 
  10533.  
  10534.    { 
  10535.      MEMO_EL * el; 
  10536.      MEMO_EL * last_el; 
  10537.      time_t   timer; 
  10538.      struct tm ltime; 
  10539.      char    date[9]; 
  10540.  
  10541.    /* Get today's time in YY/MM/DD format. 
  10542.    */ 
  10543.      time( &timer ); 
  10544.      ltime = *localtime( &timer ); 
  10545.  
  10546.      strftime( date, 9, "%y/%m/%d", <ime ); 
  10547.      last_el = NULL; 
  10548.  
  10549.      for( el = MemoHead; el != NULL; el = el->next ) { 
  10550.        if( el->date[0] == NULLCHAR 
  10551.            ||  strcmp( date, el->date ) == 0 ) { 
  10552.          DisplayMemo( el ); 
  10553.          last_el = el; 
  10554.        } 
  10555.      } 
  10556.      return( last_el ); 
  10557.    } 
  10558.  
  10559.    static void DisplayMemo( MEMO_EL * el ) 
  10560.    /*************************************/ 
  10561.  
  10562.    /* Display a memo on the screen. 
  10563.    */ 
  10564.    { 
  10565.      TEXT_LINE * tline; 
  10566.  
  10567.      if( el == NULL ) { 
  10568.        DisplayTop(); 
  10569.        return; 
  10570.      } 
  10571.  
  10572.      if( el->date[0] == NULLCHAR ) { 
  10573.        printf( "\nUndated memo\n" ); 
  10574.      } else { 
  10575.        printf( "\nDated: %s\n", el->date ); 
  10576.      } 
  10577.  
  10578.      for( tline = el->text; tline != NULL; tline = tline->next ) { 
  10579.        printf( "   %s\n", tline->text ); 
  10580.      } 
  10581.    } 
  10582.  
  10583.    static int SaveMemos( void ) 
  10584.    /**************************/ 
  10585.  
  10586.    /* Save the memos to the memos file. 
  10587.    */ 
  10588.    { 
  10589.      FILE *    fid; 
  10590.      MEMO_EL *  el; 
  10591.      TEXT_LINE * tline; 
  10592.      char     buffer[20]; 
  10593.  
  10594.      if( MemoHead == NULL ) { 
  10595.        printf( "No memos to save.\n" ); 
  10596.        return( FALSE ); 
  10597.      } 
  10598.  
  10599.    /* Open a temporary filename in case something goes wrong 
  10600.    * during the save. 
  10601.    */ 
  10602.      fid = fopen( TempName, "w" ); 
  10603.      if( fid == NULL ) { 
  10604.        printf( "Unable to open \"%s\" for writing.\n", TempName ); 
  10605.        printf( "Save not performed.\n" ); 
  10606.        return( FALSE ); 
  10607.      } 
  10608.  
  10609.      for( el = MemoHead; el != NULL; el = el->next ) { 
  10610.        sprintf( buffer, "Date:%s", el->date ); 
  10611.        if( !WriteLine( buffer, fid ) ) { 
  10612.          return( FALSE ); 
  10613.        } 
  10614.        tline = el->text; 
  10615.  
  10616.        for( ; tline != NULL; tline = tline->next ) { 
  10617.          if( !WriteLine( tline->text, fid ) ) { 
  10618.            return( FALSE ); 
  10619.          } 
  10620.        } 
  10621.  
  10622.        if( !WriteLine( "====", fid ) ) { 
  10623.          return( FALSE ); 
  10624.        } 
  10625.      } 
  10626.  
  10627.    /* Now get rid of the old file, if it's there, then rename 
  10628.    * the new one. 
  10629.    */ 
  10630.      fclose( fid ); 
  10631.      fid = fopen( FileName, "r" ); 
  10632.  
  10633.      if( fid != NULL ) { 
  10634.        fclose( fid ); 
  10635.        if( remove( FileName ) != 0 ) { 
  10636.          perror( "Can't remove old memos file" ); 
  10637.          return( FALSE ); 
  10638.        } 
  10639.      } 
  10640.  
  10641.      if( rename( TempName, FileName ) != 0 ) { 
  10642.        perror( "Can't rename new memos file" ); 
  10643.        return( FALSE ); 
  10644.      } 
  10645.      return( TRUE ); 
  10646.    } 
  10647.  
  10648.    static int WriteLine( char * text, FILE * fid ) 
  10649.    /*********************************************/ 
  10650.    { 
  10651.      if( fprintf( fid, "%s\n", text ) < 0 ) { 
  10652.        perror( "Error writing memos file" ); 
  10653.        return( FALSE ); 
  10654.      } 
  10655.      return( TRUE ); 
  10656.    } 
  10657.  
  10658.    /* Routines for displaying HELP and other simple text. 
  10659.    */ 
  10660.  
  10661.    static void Usage( void ) 
  10662.    /***********************/ 
  10663.    { 
  10664.      printf( "Usage:\n" ); 
  10665.      printf( "   memos ?\n" ); 
  10666.      printf( "     displays this text\n" ); 
  10667.  
  10668.      printf( "  or\n" ); 
  10669.  
  10670.      printf( "   memos\n" ); 
  10671.      printf( "     prompts for all actions.\n" ); 
  10672.  
  10673.      printf( "  or\n" ); 
  10674.  
  10675.      printf( "   memos action\n" ); 
  10676.      printf( "     performs the action.\n" ); 
  10677.      printf( "     More than one action may be specified.\n" ); 
  10678.      printf( "     action is one of:\n" ); 
  10679.      ShowActions(); 
  10680.    } 
  10681.  
  10682.    static void ShowActions( void ) 
  10683.    /*****************************/ 
  10684.    { 
  10685.      printf( "       Help   (display this text)\n" ); 
  10686.  
  10687.      printf( "       Add   (add new memo here)\n" ); 
  10688.  
  10689.      printf( "       DELete  (delete current memo)\n" ); 
  10690.  
  10691.      printf( "       REPlace (replace current memo)\n" ); 
  10692.  
  10693.      printf( "       SHow   (show the current memo again)\n" ); 
  10694.  
  10695.      printf( "       Up    (move up one memo)\n" ); 
  10696.      printf( "       Down   (move down one memo)\n" ); 
  10697.  
  10698.      printf( "       TOP   (move to the top of the list\n" ); 
  10699.  
  10700.      printf( "       TODay  (display today's memos)\n" ); 
  10701.  
  10702.      printf( "       SAve   (write the memos to disk)\n" ); 
  10703.    } 
  10704.  
  10705.    static void Help( void ) 
  10706.    /**********************/ 
  10707.    { 
  10708.      printf( "Choose one of:\n" ); 
  10709.      ShowActions(); 
  10710.      printf( "       Quit\n" ); 
  10711.    } 
  10712.  
  10713.    static void DisplayTop( void ) 
  10714.    /****************************/ 
  10715.    { 
  10716.      printf( "Top of memos.\n" ); 
  10717.    } 
  10718.  
  10719.    static int WantToQuit( void ) 
  10720.    /***************************/ 
  10721.  
  10722.    /* Check to see if the memos have been modified, but not saved. 
  10723.    * If so, query the user to make sure that he/she wants to quit 
  10724.    * without saving the memos. 
  10725.    */ 
  10726.    { 
  10727.      char buffer[MAXLEN]; 
  10728.  
  10729.      if( !MemosModified  ||  MemoHead == NULL ) { 
  10730.        return( TRUE ); 
  10731.      } 
  10732.  
  10733.      printf( "\nThe memos have been modified but not saved.\n" ); 
  10734.      printf( "Do you want to leave without saving them?\n" ); 
  10735.  
  10736.      gets( buffer ); 
  10737.      return( tolower( buffer[0] ) == 'y' ); 
  10738.    } 
  10739.  
  10740.    static void BadFormat( void ) 
  10741.    /***************************/ 
  10742.    { 
  10743.      printf( "Invalid format for memos file\n" ); 
  10744.      abort(); 
  10745.    } 
  10746.  
  10747.    static void * MemoMAlloc( int size ) 
  10748.    /**********************************/ 
  10749.  
  10750.    /* Allocate the specified size of memory, dealing with the 
  10751.    * case of a failure by displaying a message and quitting. 
  10752.    */ 
  10753.    { 
  10754.      register char * mem; 
  10755.  
  10756.      mem = malloc( size ); 
  10757.      if( mem == NULL ) { 
  10758.        printf( "Unable to allocate %d characters of memory\n", 
  10759.            size ); 
  10760.        abort(); 
  10761.      } 
  10762.      return( mem ); 
  10763.  
  10764.    } 
  10765.  
  10766.    static void DisposeMemo( MEMO_EL * el ) 
  10767.    /*************************************/ 
  10768.  
  10769.    /* Dispose of a memo, including its lines. 
  10770.    */ 
  10771.    { 
  10772.      TEXT_LINE * tline; 
  10773.      TEXT_LINE * next; 
  10774.  
  10775.      tline = el->text; 
  10776.      while( tline != NULL ) { 
  10777.        next = tline->next; 
  10778.        free( tline ); 
  10779.        tline = next; 
  10780.      } 
  10781.      free( el ); 
  10782.    } 
  10783.  
  10784.  
  10785. ΓòÉΓòÉΓòÉ 29. Glossary ΓòÉΓòÉΓòÉ
  10786.  
  10787.  address 
  10788.       An address is a location in a computer's memory.  Each storage location 
  10789.       (byte) has an address by which it is referenced.  A pointer is an 
  10790.       address. 
  10791.  
  10792.  aggregate 
  10793.       An aggregate type is either an array or a structure.  The term aggregate 
  10794.       refers to the fact that arrays and structures are made up of other types. 
  10795.  
  10796.  alignment 
  10797.       On some computers, objects such as integers, pointers and floating-point 
  10798.       numbers may be stored only at certain addresses (for example, only at 
  10799.       even addresses).  An attempt to reference an object that is not properly 
  10800.       aligned may cause the program to fail.  Other computers may not require 
  10801.       alignment, but may suggest it in order to increase the speed of execution 
  10802.       of programs. 
  10803.  
  10804.       C compilers align all objects that require it, including putting padding 
  10805.       characters within structures and arrays, if necessary.  However, it is 
  10806.       still possible for a program to attempt to reference an 
  10807.       improperly-aligned object. 
  10808.  
  10809.        The Watcom C/16 and C/32 compilers do not align objects by default.  A 
  10810.       command line switch, or the pack pragma, may be used to force the 
  10811.       compiler to align objects. 
  10812.  
  10813.        See the User's Guide for details. 
  10814.  
  10815.  argument 
  10816.       An argument to a function call is an expression whose value is assigned 
  10817.       to the parameter for the function.  The function may modify the 
  10818.       parameter, but the original argument is unaffected.  This method of 
  10819.       passing values to a function is often called  call by value. 
  10820.  
  10821.       The argument may be a pointer to an object, in which case the function 
  10822.       may modify the object to which the pointer points, while the argument 
  10823.       value (the pointer) is unaffected. 
  10824.  
  10825.  array 
  10826.       An array is a set of objects of the same type, grouped into adjacent 
  10827.       memory locations.  References to elements of the array are made by 
  10828.       subscripts or  indices. 
  10829.  
  10830.  assignment 
  10831.       Assignment is the storing of a value into an object, which is usually 
  10832.       done with the  = operator. 
  10833.  
  10834.  automatic storage duration 
  10835.       An object with automatic storage duration is created when the function in 
  10836.       which it is defined is invoked, and is destroyed when the function 
  10837.       returns to the caller. 
  10838.  
  10839.  bit 
  10840.       A bit is the smallest possible unit of information, representing one of 
  10841.       two values, 0 or 1.  If the bit is 0, it is said to be off.  If the bit 
  10842.       is 1, it is said to be on. 
  10843.  
  10844.       A bit is not representable by an address, but is part of a byte, which 
  10845.       does have an address. 
  10846.  
  10847.        Most processors, including the Intel 80x86 family of processors, have 8 
  10848.       bits in a byte. 
  10849.  
  10850.  bit-field 
  10851.       A bit-field is a type that contains a specified number of bits. 
  10852.  
  10853.  block 
  10854.       A block is a part of a function that begins with { and ends with } and 
  10855.       contains declarations of objects and statements that perform some action. 
  10856.       A block is also called a  compound statement. 
  10857.  
  10858.  byte 
  10859.       A byte is the smallest unit of storage representable by a unique address, 
  10860.       usually capable of holding one character of information. 
  10861.  
  10862.        Most processors, including the Intel 80x86 family of processors, have 8 
  10863.       bits in a byte. 
  10864.  
  10865.  cast 
  10866.       To cast an object is to explicitly convert it to another type. 
  10867.  
  10868.  character constant 
  10869.       A character constant is usually one character (possibly a  trigraph or 
  10870.       escape sequence) contained within single-quotes (for example, 'a', '??(' 
  10871.       and '\n'). 
  10872.  
  10873.        The Watcom C/16 and C/32 compilers allow character constants with one, 
  10874.       two, three or four characters. 
  10875.  
  10876.  comment 
  10877.       A comment is a sequence of characters, outside of a  string literal or 
  10878.       character constant, starting with /* and ending with */.  The comment is 
  10879.       only examined to find the */ that terminates it.  Hence, a comment may 
  10880.       not contain another comment. 
  10881.  
  10882.  compiler 
  10883.       A compiler is a program which reads a file containing programming 
  10884.       language statements and translates it into instructions that the computer 
  10885.       can understand. 
  10886.  
  10887.       For example, a C compiler translates statements described in this book. 
  10888.  
  10889.  compound statement 
  10890.       A compound statement is a part of a function that begins with { and ends 
  10891.       with } and contains declarations of objects and statements that perform 
  10892.       some action.  A compound statement is also called a  block. 
  10893.  
  10894.  declaration 
  10895.       A declaration describes the attributes of an object or function, such as 
  10896.       the storage duration, linkage, and type.  The space for an object is 
  10897.       reserved when its definition is found.  The declaration of a function 
  10898.       describes the function arguments and type and is also called a function 
  10899.       prototype.  The declaration of a function does not include the statements 
  10900.       to be executed when the function is called. 
  10901.  
  10902.  decrement 
  10903.       To decrement a number is to subtract (one) from it.  To decrement a 
  10904.       pointer is to decrease its value by the size of the object to which the 
  10905.       pointer points. 
  10906.  
  10907.  definition 
  10908.       A definition of an object is the same as a declaration, except that the 
  10909.       storage for the object is reserved when its definition is found.  A 
  10910.       function definition includes the statements to be executed when the 
  10911.       function is called. 
  10912.  
  10913.  exception 
  10914.       An exception occurs when an operand to an operator has an invalid value. 
  10915.       Division by zero is a common exception. 
  10916.  
  10917.  floating-point 
  10918.       A floating-point number is a member of a subset of the mathematical set 
  10919.       of real numbers, containing (possibly) a fraction and an exponent.  The 
  10920.       floating-point type is represented by one of the keywords  float,  double 
  10921.       or  long double. 
  10922.  
  10923.  function 
  10924.       A function is a collection of declarations and statements, preceded by a 
  10925.       declaration of the name of the function and the parameters to it, as well 
  10926.       as a possible return value.  The statements describe a series of steps to 
  10927.       be taken after the function is called, and before it finishes. 
  10928.  
  10929.  header 
  10930.       A header contains C source, usually function prototypes, structure and 
  10931.       union definitions, linkages to externally-defined objects and macro 
  10932.       definitions.  A header is included using the  #include preprocessor 
  10933.       directive. 
  10934.  
  10935.  identifier 
  10936.       An identifier is a sequence of characters, starting with a letter or 
  10937.       underscore, and consisting of letters, digits and underscores.  An 
  10938.       identifier is used as the name of an object, a tag, function, typedef, 
  10939.       label, macro or member of a structure or union. 
  10940.  
  10941.  implementation-defined behavior 
  10942.       Behavior that is implementation-defined depends on how a particular C 
  10943.       compiler handles a certain case.  All C compilers must document their 
  10944.       behavior in these cases. 
  10945.  
  10946.  incomplete type 
  10947.       An incomplete type is one which has been declared, but its size or 
  10948.       structure has not yet been stated.  An example is an array of items that 
  10949.       was declared without specifying how many items.  The  void type is also 
  10950.       an incomplete type, but it can never be completed. 
  10951.  
  10952.  increment 
  10953.       To increment a number is to add (one) to it.  To increment a pointer is 
  10954.       to increase its value by the size of the object to which the pointer 
  10955.       points. 
  10956.  
  10957.  index 
  10958.       An index (or subscript) is a number used to reference an element of an 
  10959.       array.  It is an integral value.  The first element of an array has the 
  10960.       index zero. 
  10961.  
  10962.  indirection 
  10963.       Indirection occurs when an object that is a pointer to an object is 
  10964.       actually used to point to it.  The unary form of the  * operator, or the 
  10965.       -> operator are used for indirection. 
  10966.  
  10967.  initialization 
  10968.       The initialization of an object is the act of giving it its first 
  10969.       (initial) value.  This may be done by giving an initialization value when 
  10970.       the object is declared, or by explicitly assigning it a value. 
  10971.  
  10972.  integer 
  10973.       An integer is a type that is a subset of the mathematical set of 
  10974.       integers.  It is represented by the keyword  int, and has a number of 
  10975.       variations including  signed char,  unsigned char,  short signed int, 
  10976.       short unsigned int,  signed int,  unsigned int,  long signed int and 
  10977.       long unsigned int. 
  10978.  
  10979.  integral promotion 
  10980.       An object or constant that is a  char,  short int,  int bit-field, or of 
  10981.       enum type, that is used in an expression, is promoted to an  int (if  int 
  10982.       is large enough to contain all possible values of the smaller type) or 
  10983.       unsigned int. 
  10984.  
  10985.  keyword 
  10986.       A keyword is an identifier that is reserved for use by the compiler.  No 
  10987.       object name or other use of an identifier may use a keyword. 
  10988.  
  10989.  label 
  10990.       A label is an identifier that corresponds to a particular statement in a 
  10991.       function. It may be used by the  goto statement.  default is a special 
  10992.       label which is used with the  switch statement. 
  10993.  
  10994.  library function 
  10995.       A library function is a function provided with the C compiler that 
  10996.       performs some commonly needed action.  The C language standard describes 
  10997.       a set of functions that all C compilers must provide.  Whether or not the 
  10998.       function actually generates a function call is implementation-defined. 
  10999.  
  11000.  line 
  11001.       A line is conceptually similar to a line as seen in a text editor.  The 
  11002.       line in a text editor may be called a physical line.  Several physical 
  11003.       lines may be joined together into one logical line (or just "line") by 
  11004.       ending all but the last line with a \ symbol.  C does not normally 
  11005.       require statements to fit onto one line, so using the \ symbol is usually 
  11006.       only necessary when defining  macros. 
  11007.  
  11008.  linkage 
  11009.       An object with external linkage may be referenced by any module in the 
  11010.       program.  An object with internal linkage may be referenced only within 
  11011.       the module in which it is defined.  An object with no linkage may only be 
  11012.       referenced within the block in which it is defined. 
  11013.  
  11014.  lint 
  11015.       lint is a utility program, often provided with the compiler, which 
  11016.       detects problems that the compiler will accept as syntactically valid, 
  11017.       but likely are not what the programmer intended. 
  11018.  
  11019.  lvalue 
  11020.       An lvalue is an expression that designates an object.  The term 
  11021.       originally comes from the assignment expression, 
  11022.  
  11023.  
  11024.          L = R 
  11025.  
  11026.       in which the left operand L to the assignment operator must be a 
  11027.       modifiable value.  The most common form of lvalue is the identifier of an 
  11028.       object. 
  11029.  
  11030.       If an expression E evaluates to a pointer to an object, then *E is an 
  11031.       lvalue that designates the object to which E points.  In particular, if E 
  11032.       is declared as a "pointer to  int", then both E and *E are lvalues having 
  11033.       the respective types "pointer to  int" and  int. 
  11034.  
  11035.  macro 
  11036.       There are two kinds of macros.  An object-like macro is an identifier 
  11037.       that is replaced by a sequence of tokens.  A function-like macro is an 
  11038.       apparent function call which is replaced by a sequence of tokens. 
  11039.  
  11040.  module 
  11041.       Referred to in the C language standard as a translation unit, a module is 
  11042.       usually a file containing C source code.  A module may include headers or 
  11043.       other source files, and have conditional compilation (preprocessing 
  11044.       directives), object declarations, and/or functions.  A module is thus 
  11045.       considered to be a C source file after the included files and conditional 
  11046.       compilation have been processed. 
  11047.  
  11048.  name space 
  11049.       A name space is a category of identifiers.  The same identifier may 
  11050.       appear in different name spaces.  For example, the identifier thing may 
  11051.       be a label, object name, tag and member of a structure or union, all at 
  11052.       the same time, since each of these has its own name space.  The syntax of 
  11053.       the use of the identifier resolves which category the identifier falls 
  11054.       into. 
  11055.  
  11056.  nesting 
  11057.       Nesting is placing something inside something else.  For example, a  for 
  11058.       statement may, as part of its body, contain another  for statement.  The 
  11059.       second  for is said to be nested inside the first.  Another form of 
  11060.       nesting occurs when source files include other files. 
  11061.  
  11062.  null pointer constant 
  11063.       The value zero, when used in a place where a pointer type is expected, is 
  11064.       considered to be a null pointer constant, which is a value that indicates 
  11065.       that the pointer does not currently point to anything.  The compiler 
  11066.       interprets the zero as a special value, and does not guarantee that the 
  11067.       actual value of the pointer will be zero. 
  11068.  
  11069.       The macro  NULL is often used to represent the null pointer constant. 
  11070.  
  11071.  null character 
  11072.       The character with all bits set to zero is used to terminate strings, and 
  11073.       is called the null character.  It is represented by the escape sequence 
  11074.       \0 in a string, or as the character constant '\0'. 
  11075.  
  11076.  object 
  11077.       An object is a collection of bytes in the storage of the computer, used 
  11078.       to represent values.  The size and meaning of the object is determined by 
  11079.       its type.  A scalar object is often referred to as a variable. 
  11080.  
  11081.  parameter 
  11082.       A parameter to a function is a "local copy" of the argument values 
  11083.       determined in the call to the function.  Any modification of a parameter 
  11084.       value does not affect the argument to the function call.  However, an 
  11085.       argument (and hence a parameter) may be a pointer to an object, in which 
  11086.       case the function may modify the object to which its parameter points. 
  11087.  
  11088.  pointer 
  11089.       An object that contains the address of another object is said to be a 
  11090.       pointer to that object. 
  11091.  
  11092.  portable 
  11093.       Portable software is written in such a way that it is relatively easy to 
  11094.       make the software run on different hardware or operating systems. 
  11095.  
  11096.  precedence 
  11097.       Precedence is the set of implicit rules for determining the order of 
  11098.       execution of an expression in the absence of parentheses. 
  11099.  
  11100.  preprocessor 
  11101.       The preprocessor: 
  11102.  
  11103.           examines tokens for macros and does appropriate substitutions if 
  11104.            necessary, 
  11105.           includes headers or other source files, and, 
  11106.           includes or excludes input lines based on  #if directives 
  11107.       before the compiler translates the source. 
  11108.  
  11109.  recursion 
  11110.       Recursion occurs when a function calls itself either directly, or by 
  11111.       calling another function which calls it.  See recursion.  (!) 
  11112.  
  11113.  register 
  11114.       A register is a special part of the computer, usually not part of the 
  11115.       addressable storage.  Registers may contain values and are generally 
  11116.       faster to use than storage. 
  11117.       The keyword  register may be used when declaring an object with automatic 
  11118.       storage duration, indicating to the compiler that this object will be 
  11119.       heavily used, and the compiler should attempt to optimize the use of this 
  11120.       object, possibly by placing it in a machine register. 
  11121.  
  11122.  return value 
  11123.       A return value is the value returned by a function via the  return 
  11124.       statement. 
  11125.  
  11126.  rounding 
  11127.       A value is rounded when the representation used to store a value is not 
  11128.       exact.  The value may be increased or decreased to the nearest value that 
  11129.       may be accurately represented. 
  11130.  
  11131.  scalar 
  11132.       A scalar is an object that is not a structure, union or array. 
  11133.       Basically, it is a single item, with type such as character, any of the 
  11134.       various integer types, or floating-point. 
  11135.  
  11136.  scope 
  11137.       The scope of an identifier identifies the part of the module that may 
  11138.       refererence it.  An object with block scope may only be referenced within 
  11139.       the block in which it is defined.  An object with file scope may be 
  11140.       referred to anywhere within the file in which it is defined. 
  11141.  
  11142.  sequence point 
  11143.       A sequence point is a point at which all side-effects from previously 
  11144.       executed statements will have been resolved, and no side-effects from 
  11145.       statements not yet executed will have occurred.  Normally, the programmer 
  11146.       will not need to worry about sequence points, as it is the compiler's job 
  11147.       to ensure that side-effects are resolved at the proper time. 
  11148.  
  11149.  side-effect 
  11150.       A side-effect modifies a value of an object, causing a change in the 
  11151.       state of the program.  The most common side-effect is assignment, whereby 
  11152.       the value of the left operand is changed. 
  11153.  
  11154.  signed 
  11155.       A signed value can represent both negative and positive values. 
  11156.  
  11157.       The keyword  signed may be used with the types  char,  short int,  int 
  11158.       and  long int. 
  11159.  
  11160.  statement 
  11161.       A statement describes the actions that are to be taken by the program. 
  11162.       (Statements are distinct from the declarations of objects.) 
  11163.  
  11164.  static storage duration 
  11165.       An object with static storage duration is created when the program is 
  11166.       invoked, and destroyed when the program exits.  Any value stored in the 
  11167.       object will remain until explicitly modified. 
  11168.  
  11169.  string 
  11170.       A string is a sequence of characters terminated by a null character.  A 
  11171.       reference to a string is made with the address of the first character. 
  11172.  
  11173.  string literal 
  11174.       A string literal is a sequence of zero or more characters enclosed within 
  11175.       double-quotes and is a constant.  Adjacent string literals are 
  11176.       concatenated into one string literal.  The value of a string literal is 
  11177.       the sequence of characters within the quotes, plus a null character (\0) 
  11178.       placed at the end. 
  11179.  
  11180.  structure 
  11181.       A structure is a type which is a set of named members of (possibly 
  11182.       different) types, which reside in memory starting at adjacent and 
  11183.       sequentially increasing storage locations. 
  11184.  
  11185.  subscript 
  11186.       A subscript (or index) is a number used to reference an element of an 
  11187.       array.  It is a non-negative integral value.  The first element of an 
  11188.       array has the subscript zero. 
  11189.  
  11190.  tag 
  11191.       A tag is an identifier which names a structure, union or enumeration.  In 
  11192.       the declaration, 
  11193.  
  11194.  
  11195.          enum nums { ZERO, ONE, TWO } value; 
  11196.  
  11197.       nums is the tag of the enumeration, while value is an object declared 
  11198.       with the enumeration type. 
  11199.  
  11200.  token 
  11201.       A token is the unit used by the preprocessor for scanning for macros, and 
  11202.       by the compiler for scanning the input source lines.  Each identifier, 
  11203.       constant and comment is one token, while other characters are each, 
  11204.       individually, one token. 
  11205.  
  11206.  type 
  11207.       The type of an object describes the size of the object, and what 
  11208.       interpretation is to be used when using the value of the object.  It may 
  11209.       include information such as whether the value is  signed or  unsigned, 
  11210.       and what range of values it may contain. 
  11211.  
  11212.  undefined behavior 
  11213.       Undefined behavior occurs when an erroneous program construct or bad data 
  11214.       is used, and the standard does not impose a behavior.  Possible actions 
  11215.       of undefined behavior include ignoring the problem, behaving in a 
  11216.       documented manner, terminating the compilation with an error, and 
  11217.       terminating the execution with an error. 
  11218.  
  11219.  union 
  11220.       A union is a type which is a set of named members of (possibly different) 
  11221.       types, which reside in memory starting at the same memory location. 
  11222.  
  11223.  unsigned 
  11224.       An unsigned value is one that can represent only non-negative values. 
  11225.  
  11226.         The keyword  unsigned may be used with the types  char,  short int, 
  11227.       int and  long int. 
  11228.  
  11229.  variable 
  11230.       A variable is generally the same thing as an object.  It is most often 
  11231.       used to refer to scalar objects. 
  11232.  
  11233.  void 
  11234.       The  void type is a special type that really indicates "no particular 
  11235.       type".  An object that is a "pointer to  void" may not be used to point 
  11236.       at anything without it first being cast to the appropriate type. 
  11237.  
  11238.       The keyword  void is also used as the type of a function that has no 
  11239.       return value, and as the parameter list of a function that requires no 
  11240.       parameters. 
  11241.