Changeset 736a38d


Ignore:
Timestamp:
Jun 12, 2024, 9:16:50 AM (3 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
c033405
Parents:
4c8f29ff
Message:

more proofreading of C background chapter

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/background.tex

    r4c8f29ff r736a38d  
    11\chapter{Background}
    22
     3\vspace*{-8pt}
     4
    35\CFA is a backwards-compatible extension of the C programming language, therefore, it must support C-style enumerations.
    4 The following covers C enumerations.
     6The following discussion covers C enumerations.
    57
    68As discussed in \VRef{s:Aliasing}, it is common for C programmers to ``believe'' there are three equivalent forms of named constants.
     
    1618\item
    1719The same explicit management is true for the @const@ declaration, and the @const@ variable cannot appear in constant-expression locations, like @case@ labels, array dimensions,\footnote{
    18 C allows variable-length array-declarations (VLA), so this case does work, but it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} immediate operands of assembler instructions, and occupy storage.
     20C allows variable-length array-declarations (VLA), so this case does work, but it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} immediate oper\-ands of assembler instructions, and occupy storage.
    1921\begin{clang}
    2022$\$$ nm test.o
     
    2224\end{clang}
    2325\item
    24 Only the @enum@ form is managed by the compiler, is part of the language type-system, works in all C constant-expression locations, and might not occupy storage..
     26Only the @enum@ form is managed by the compiler, is part of the language type-system, works in all C constant-expression locations, and normally does not occupy storage.
    2527\end{enumerate}
    2628
     
    3032
    3133C can simulate the aliasing @const@ declarations \see{\VRef{s:Aliasing}}, with static and dynamic initialization.
     34\begin{cquote}
     35\begin{tabular}{@{}l@{}l@{}}
     36\multicolumn{1}{@{}c@{}}{\textbf{static initialization}} &  \multicolumn{1}{c@{}}{\textbf{dynamic intialization}} \\
    3237\begin{clang}
    33 static const int one = 0 + 1;                   $\C{// static initialization}$
     38static const int one = 0 + 1;
    3439static const void * NIL = NULL;
    3540static const double PI = 3.14159;
    3641static const char Plus = '+';
    3742static const char * Fred = "Fred";
    38 static const int Mon = 0, Tue = Mon + 1, Wed = Tue + 1, Thu = Wed + 1, Fri = Thu + 1,
    39                                         Sat = Fri + 1, Sun = Sat + 1;
     43static const int Mon = 0, Tue = Mon + 1, Wed = Tue + 1,
     44        Thu = Wed + 1, Fri = Thu + 1, Sat = Fri + 1, Sun = Sat + 1;
     45\end{clang}
     46&
     47\begin{clang}
    4048void foo() {
    41         const int r = random() % 100;           $\C{// dynamic intialization}$
    42         int va[r];                                                      $\C{// VLA, auto scope only}$
     49        // auto scope only
     50        const int r = random() % 100;
     51        int va[r];
    4352}
     53
     54
    4455\end{clang}
    45 Statically initialized identifiers may appear in any constant-expression context, \eg @case@.
    46 Dynamically initialized identifiers may appear as array dimensions in @g++@, which allows variable-sized arrays on the stack.
     56\end{tabular}
     57\end{cquote}
     58However, statically initialized identifiers can not appear in constant-expression contexts, \eg @case@.
     59Dynamically initialized identifiers may appear in initialization and array dimensions in @g++@, which allows variable-sized arrays on the stack.
    4760Again, this form of aliasing is not an enumeration.
    4861
     
    8295The enumerators are rvalues, so assignment is disallowed.
    8396Finally, enumerators are \newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type.
    84 For unnamed enumeration this semantic is required because there is no type name for scoped qualification.
     97For unnamed enumerations, this semantic is required because there is no type name for scoped qualification.
    8598
    8699As noted, this kind of aliasing declaration is not an enumeration, even though it is declared using an @enum@ in C.
    87100While the semantics is misleading, this enumeration form matches with aggregate types:
    88101\begin{cfa}
    89 typedef struct /* unnamed */  { ... } S;
    90 struct /* unnamed */  { ... } x, y, z;  $\C{// questionable}$
     102typedef struct @/* unnamed */@  { ... } S;
     103struct @/* unnamed */@  { ... } x, y, z;        $\C{// questionable}$
    91104struct S {
    92         union /* unnamed */ {                           $\C{// unscoped fields}$
     105        union @/* unnamed */@ {                                 $\C{// unscoped fields}$
    93106                int i;  double d ;  char ch;
    94107        };
     
    132145@week = 10000;@                                         $\C{// UNDEFINED! implicit conversion to Week}$
    133146\end{clang}
    134 While converting an enumerator to underlying type is useful, the implicit conversion from the base type to an enumeration type is a common source of error.
     147While converting an enumerator to its underlying type is useful, the implicit conversion from the base type to an enumeration type is a common source of error.
    135148
    136149Enumerators can appear in @switch@ and looping statements.
     
    143156                printf( "weekend\n" );
    144157}
    145 for ( enum Week day = Mon; day <= Sun; day += 1 ) {
     158for ( enum Week day = Mon; day <= Sun; day += 1 ) { // step of 1
    146159        printf( "day %d\n", day ); // 0-6
    147160}
    148161\end{cfa}
    149 For iterating, the enumerator values \emph{must} have a consecutive ordering with a fixed step between values.
     162For iterating to make sense, the enumerator values \emph{must} have a consecutive ordering with a fixed step between values.
     163For example, a gap introduced by @Thu = 10@, results in iterating over the values 0--13, where values 3--9 are not @Week@ values.
    150164Note, it is the bidirectional conversion that allows incrementing @day@: @day@ is converted to @int@, integer @1@ is added, and the result is converted back to @Week@ for the assignment to @day@.
    151165For safety, \CC does not support the bidirectional conversion, and hence, an unsafe cast is necessary to increment @day@: @day = (Week)(day + 1)@.
    152166
    153 There is a C idiom to automatically know the number of enumerators in an enumeration.
     167There is a C idiom to automatically compute the number of enumerators in an enumeration.
    154168\begin{cfa}
    155169enum E { A, B, C, D, @N@ };  // N == 4
     
    183197
    184198\bigskip
    185 While C provides a true enumeration, it is restricted, has unsafe semantics, and does provide enumeration features in other programming languages.
     199While C provides a true enumeration, it is restricted, has unsafe semantics, and does provide useful enumeration features in other programming languages.
Note: See TracChangeset for help on using the changeset viewer.