home *** CD-ROM | disk | FTP | other *** search
- --------------------------------------------------------------------------------
- C REFERENCE CARD LATTICE C GARY COOMBS
- --------------------------------------------------------------------------------
-
- ESCAPE CHARACTERS
- newline NL (LF) \n
- tab HT \t
- backspace BS \b
- carriage return CR \r
- form feed FF \f
- backslash \ \\
- single quote ' \'
- double quote " \"
- bit pattern ddd \ddd
- -------------------------------------------------------------------------------
- PRINTF() CONVERSIONS AS RELATED TO STRINGS
-
- |%10s| |hello, world|
- |%-10s| |hello, world|
- |%20s| | hello, world|
- |%-20s| |hello, world |
- |%20.10s| | hello, wor|
- |%-20.10s| |hello, wor|
- -------------------------------------------------------------------------------
- STORAGE CLASSES
-
- I. Keywords: auto, external, static, register
-
- II. General Comments:
-
- The storage class of a variable determines its scope and how long the
- variable persists. Storage class is determined by where the variable is defined
- and by the associated keyword. Variables defined outside a function are
- external and have global scope. Variables declared inside a function are
- automatic and local unless one of the other keywords is used. External
- variables defined above a function are known to it even if not declared
- internally.
- 'auto' and 'register' variables are created each time the function
- containing them is evoked, and they vanish each time the function finishes. The
- others last for the duration of the whole program.
-
- III. Properties:
-
- STORAGE CLASS KEYWORD DURATION SCOPE
- automatic auto temporary local
- register register temporary local
- static static persistent local
- ................................................................................
- external extern persistent global (all files)
- external static static persistent global (one file)
-
- Those above the dotted line are declared inside a function.
- Those below the line are defined outside a function.
- --------------------------------------------------------------------------------
- BASIC DATA TYPES
-
- Keywords: int, long, short, unsigned, char, float, double
-
- Signed Integers --- These can have positive or negative values.
- int: the basic integer type for a given system
- long or long int: 32 bit integer
- short or short int: 16 bit integer
-
- Unsigned Integers --- zero or positive values only
- unsigned int:, unsigned long:, unsigned short:
- char: symbols 8 bits long
-
- Floating Point --- can have positive or negative values
- float: basic floating point 32 bits
- double or long float: 64 bits
- -------------------------------------------------------------------------------
- IF STATEMENT FOR MAKING CHOICES
-
- Keywords: if, else
-
- General Comments:
-
- In each of the following forms, the statement can be either a simple
- statement or a compound statement. A "true" expression, more generally, means
- one with a nonzero value.
-
- Form 1:
-
- if ( expression )
- statement;
-
- The statement is executed if the expression is true.
-
- Form 2:
-
- if ( expression )
- statement1;
- else
- statement2;
-
- If the expression is true, statement1 is executed, else statement2 is executed.
-
- Form 3:
-
- if ( expression1 )
- statement1;
- else if ( expression2 )
- statement2;
- else
- statement3
-
- If expression1 is true, then statement1 is executed. If expression1 is false
- but expression2 is true, statement2 is executed. Otherwise, if both expressions
- are false, statement3 is executed.
- -------------------------------------------------------------------------------
- LOGICAL EXPRESSIONS
-
- expression1 && expression2 is true if and only if both expressions are true
- expression1 || expression2 is true if either one or both expressions are true
- !expression is true if the expression is false, and vice versa
-
- 6 > 2 && 3 == 3 is true
- ! ( 6 > 2 && 3 == 3 ) is false
- x != 0 && 20/x < t the second expression is evaluated only if x is
- nonzero
- -------------------------------------------------------------------------------
- THE CONDITIONAL OPERATOR
-
- ?: This operator takes three operands, each of which is an expression. They
- are arranged this way: expression1 ? expression2 : expression3. The value
- of the whole expression equals the value of expression2 if expression1 is
- true, and equals the value of expression3 otherwise.
-
- ( 5 > 3 ) ? 1 : 2 has the value 1
- ( 3 > 5 ) ? 1 : 2 has the value 2
- ( a > b ) ? a : b has the value of the larger of a or b
- -------------------------------------------------------------------------------
- MULTIPLE CHOICE WITH SWITCH
-
- Program control jumps to the statement bearing the vlaue of expression as a
- label. Program flow then proceeds through the remaining statements unless
- redirected again. Bothe expression and labels must have integer values (type
- char is included), and the labels must be constants or expressions formed solely
- from constants. If no label matches the expression value, control goes to the
- statement labeled default, if present. Otherwise control passes to the next
- statement following the switch statement.
-
- Form:
- switch (expression)
- {
- case label1 : statement
- case label2 : statement
- default : statement
- }
- Example:
- switch (letter)
- {
- case 'a' :
- case 'e' : printf("%d is a vowel\n", letter);
- case 'c' :
- case 'n' : printf("%d is in \"cane\"\n", letter);
- default : printf("Have a nice day.\n");
- }
-
- If letter has the value 'a' or 'e', all three messages are printed; 'c' and 'n'
- cause the last two to be printed. Other values print just the last message.
- -------------------------------------------------------------------------------
- THE WHILE STATEMENT
-
- Creates a loop that repeats until the test expression becomes false, or zero.
- The while statement is an entry-condition loop; the decision to go through one
- more pass of the loop is made before the loop is traversed. Thus it is possible
- that the loop is never traversed. The statement part of the form can be a
- simple statement or a compound statement.
-
- Form:
- while ( expression )
- statement
-
- The statement portion is repeated until the expression becomes false or zero.
-
- Examples:
- while ( n++ < 100 )
- printf("%d %d\n", n, 2*n+1);
-
- while ( fargo < 1000 )
- {
- fargo = fargo + step;
- step = 2 * step;
- }
- -------------------------------------------------------------------------------
- THE FOR STATEMENT
-
- The for statement uses three control expressions, separated by semicolons, to
- control a looping process. The initialize expression is executed once, before
- any of the loop statements are executed. If the test expression is true (or
- non-zero), the loop is cycled through once. Then the update expression is
- evaluated, and it is time to check the test expression again. The for statement
- is an entry-condition loop; the decision to go through one more pass of the loop
- is made before the loop is traversed. Thus it is possible that the loop is
- never traversed. The statement part of the form can be a simple statement or a
- compound statement.
-
- Form:
- for ( initialize; test; update )
- statement;
-
- The loop is repeated until test becomes false or zero.
-
- Example:
- for ( n = 0; n < 10; n++ )
- printf(" %d %d\n", n, 2*n+1 );
- -------------------------------------------------------------------------------
- THE DO WHILE STATEMENT
-
- The do while statement creates a loop that repeats until the test expression
- becomes false or zero. The do while statement is an exit-condition loop; the
- decision to go through one more pass of the loop is made after the loop is
- traversed. Thus the loop must be executed at least once. The statement part of
- the form can be a simple statement or a compound statement.
-
- Form:
- do
- statement;
- while ( expression );
-
- The statement portion is repeated until the expression becomes false or zero.
-
- Example:
- do
- scanf("%d", &number);
- while (number != 20);
- -------------------------------------------------------------------------------
- PROGRAM JUMPS
-
- I. Keywords: break, continue, goto
-
- II. General Comments:
-
- These three instructions cause program flow to jump from one location of a
- program to another location.
-
- III. break
-
- Can be used with any of the three loop forms and with the switch
- statement. It causes program control to skip over the rest of the loop or
- switch containing it and to resume with the next command following the loop or
- switch.
-
- Example:
- switch (number)
- {
- case 4 : printf("That's a good choice.\n");
- break;
- case 5 : printf("That's a fair choice.\n");
- break;
- default: printf("That's a poor choice.\n");
- }
-
- IV. continue
-
- The continue command can be used with any of the three loop forms but not
- with a switch. It causes program control to skip the remaining statements in a
- loop. For a while or for loop, the next loop cycle is started. For a do while
- loop, the exit condition is tested and then, if necessary, the next loop cycle
- is started.
-
- Example:
- while ((ch = getchar()) != EOF)
- {
- if (ch == ' ')
- continue;
- putchar(ch);
- chcount++;
- }
-
- This fragment echoes and counts nonspace characters.
-
- IV. goto
-
- A goto statement causes program control to jump to a statement bearing the
- indicated label. A colon is used to separate a labeled statement from its
- label. Label names follow the rules for variable names. The labeled statement
- can come either before or after the goto.
-
- Form:
- goto label;
- . . . .
- label : statement
-
- Example:
- top : ch = getchar();
- . . . .
- if (ch != 'y')
- goto top;
- -------------------------------------------------------------------------------
- POINTER RELATED OPERATORS
-
- I. The Address Operator:
-
- & When followed by a variable name, gives the address of that
- variable.
-
- Example:
- &nurse is the address of the variable nurse.
-
- II. The Indirection Operator:
-
- * When followed by a pointer, gives the value stored at the
- pointed-to address.
-
- Example:
- nurse = 22;
- ptr = &nurse;
- val = *ptr;
-
- The net effect is to assign the value 22 to val.
- -------------------------------------------------------------------------------
- FUNCTIONS
-
- I. Form:
-
- name(argument list)
- argument declarations
- function body
-
- The presence of the argument list and declarations is optional. Variables other
- than the arguments are declared within the body, which is bounded by braces.
-
- Example:
- diff(x,y) /* function name and argument list */
- int x,y; /* declare arguments */
- { /* begin function body */
- int x; /* declare local variable */
-
- z = x - y;
- return(z); /* return result to calling function */
- } /* end function body */
-
- II. Communicating Values:
-
- Arguments are used to convey values from the calling program to the
- function. If variables 'a' and 'b' have the values 5 and 2, then the call
-
- c = diff(a,b);
-
- transmits 5 and 2 to the variables x and y. The values 5 and 2 are called
- actual arguments, and the diff() variables x and y are called formal arguments.
- The keyword 'return' communicates one value from the function to the
- calling program. 'c' receives the value of x, which is 3
- A function ordinarily has no effect upon the variables in a calling
- program. Use pointers as arguments to directly effect variables in the calling
- program. This may be necessary if you wish to communicate more than one value
- back to the calling program.
-
- III. Function Type:
-
- Functions must have the same type as the value they return. Functions are
- assumed to be of type int. If a function is of another type, it must be
- declared so in the calling program and in the function definition.
-
- Example:
- main()
- {
- float q, x, duff(); /* declare in calling program */
- int n;
- . . . . . .
- q = duff(x,n)
- . . . . . .
- }
-
- float duff(u,k) /* declare in function definition */
- float u;
- int k;
- {
- float tor;
- . . . . . .
- return(tor); /* returns a float value */
- }
- -------------------------------------------------------------------------------
- OPERATOR PRIORITIES
-
- OPERATORS (from high to low priority)
- () { } -> .
- ! ~ ++ -- - (type) * & sizeof(all unary)
- * / %
- + -
- << >> (bit shifts)
- < <= > >=
- == !=
- & (bitwise and)
- ^ (bitwise not)
- d| (bitwise or)
- &&
- ||
- ?:
- = += -= *= /* %=
- -------------------------------------------------------------------------------
- FORMATS FOR printf()
-
- printf(controlstring, expression1, expression2, . . . );
-
- %d - print an integer
- %u - print an unsigned integer
- %f - print a floating-point number
- %e - print floating-point in exponential form
- %g - chooses %e or %f depending on number size
- %c - print a character
- %s - print a string
- %o - print unsigned octal integer
- %x - print unsigned hexadecimal integer
-
- Modifiers may be placed after the % to indicate field width, precision,
- justification, and if an int is long:
-
- %10d - print an integer in a field 10 spaces wide
- %5.2f - print 2 decimal places; field width = 5
- %-6d - print an integer left justified
- %1d - print a long integer
- -------------------------------------------------------------------------------
- FORMATS FOR scanf()
-
- The scanf() works much like printf(). Here are the main differences:
-
- 1. The arguments following the control string must be addresses. They can use
- the address operator, as in &n, or they can be array names, which are
- pointers to the first array element.
- 2. There is no %g option.
- 3. Both %e and %f work the same, accepting either format.
- 4. A %b conversion specification exists for reading short integers.
-
- Example:
- int n;
- char title[20];
- scanf("%d %d", &n, title);
- -------------------------------------------------------------------------------
- STRUCTURES
-
- A structure is a data object capable of holding more than one type of value. A
- structure template establishes the type of data the structure can hold. The
- structure definition is enclosed in braces. A structure tag gives a short-hand
- name for the form. A structure variable is one whose type is given by a
- structure template. The template nust include a tag or a variable name or both.
-
- No Tag: Here we declare a variable 'fish' to be a structure of the indicated
- form. No tag is used, and the variable name follows the structure definition:
-
- struct {
- float length;
- float weight;
- char name[40];
- } fish; /* fish as a structure variable */
-
- Tag: Alternatively, use a tag name before the opening brace, then use the tag
- name later in declaring the variable:
-
- struct critter { /* critter is the tag name */
- float length;
- float weight;
- char name[40];
- };
- struct critter fish, seal; /* declaring two structures */
- struct critter birds[5]; /* an array of 5 structures */
-
- All the structures here are of the critter form.
- -------------------------------------------------------------------------------
- STRUCTURE AND UNION OPERATORS
-
- I. The Membership Operator:
-
- This operator is used with a structure or union name to specify a member of that
- structure or union. If "name" is the name of a structure and "member" is a
- member specified by the structure template, then
-
- name.member
-
- identifies that member of the structure. The membership operator can also be
- used in the same fashion with unions.
-
- EXAMPLE:
-
- struct {
- int code;
- float cost;
- } item;
-
- item.code = 1265;
-
- This assigns a value to the "code" member of the structure "item".
-
- II. The Indirect Membership Operator: ->
-
- This operator is used with a pointer to a structure or union to identify a
- member of that structure or union. Suppose "ptrstr" is a pointer to a structure
- and that "member" is a member specified by the structure template. Then
-
- ptrstr->member
-
- identifies that member of the pointed-to structure. The indirect membership
- operator can be used in the same fashion with unions.
-
- EXAMPLE:
-
- struct {
- int code;
- float cost;
- } item, *ptrst;
-
- ptrst = &item;
- ptrst->code = 3451;
-
- This assigns a value to the "code" member of "item". The following three
- expressions are equivalent:
-
- ptrst->code item.code (*ptrst).code
- -------------------------------------------------------------------------------
- FLOW CONTROL
-
- Loops:
- while loop:
-
- while (condition)
- statement;
-
- Example:
- while (i++ < 20)
- q = 2 * q;
-
- for loop:
-
- for (initialize; test; update)
- statement;
-
- Example:
- for (n = 0; n < 10; n++)
- printf("%d %d\n", n, 2*n-1);
-
- do while loop:
-
- do
- statement
- while (condition);
-
- Example:
- do
- printf("Hello, Molly\n");
- while (i++ < 10);
-
- BRANCHING:
-
- if forms:
-
- I. if (expression)
- statement;
-
- II. if (expression)
- statement;
- else
- statement;
-
- III. if (expression)
- statement;
- else if
- statement;
- else
- statement;
-
- Example:
- if (amt > 400)
- rate = .0056;
- else
- rate = .0062;
-
- switch:
-
- switch (expression)
- {
- case label1 : statement1
- case label2 : statement2
- . . . . .
- default : statement
- }
- -------------------------------------------------------------------------------
- CODE FRAGMENT TO COPY A STRING TO AN ARRAY
-
- char name[MAX];
- int c, i;
-
- printf("Enter your name: ");
-
- for (i = 0; (c = getchar()) != '\n' && i < MAX - 1; ++i)
- name[i] = c;
- name[i] = '\0';
-
- printf("Your name is: %s", name);
- -------------------------------------------------------------------------------
- C FOOD TERMINAL INDEPENDENCE PACKAGE (TIP)
-
- Include the following header files, structure definition, and body.
-
- #include <dos.h>
- #include <tip.h>
- struct TDB tdb; /* structure needed whenever TIP is used. */
- main()
- {
- tinit(); /* initializes TIP, clears the screen, and */
- . . . . /* places the cursor at position 1,1 */
- tterm(); /* terminates TIP */
- }
-
- c = tgetc -- get a character
- tpush(c) -- push a character back
- char c;
-
- n = tputc(c,d,e) -- put a character to the screen
- n = tputs(s) -- put a character string to the screen
- int n;
- char c,d,e;
- char *s;
-
- tpos(v,h) -- move cursor to V-H coordinate
- tvpos(v) -- move cursor to V coordinate
- int v;
- int h;
-
- TO_CS /* Clear screen */
- TO_MCH /* Move cursor home */
- TO_MCU /* Move cursor up */
- TO_MCD /* Move cursor down */
- TO_MCR /* Move cursor right */
- TO_MCL /* Move cursor left */
- TO_IL /* Insert line */
- TO_DL /* Delete line */
- TO_IC /* Insert character */
- TO_DC /* Delete character */
- TO_ERL /* Erase rest of line */
- TO_ERS /* Erase rest of screen */
- TO_BPM /* Begin protect mode */
- TO_EPM /* End protect mode */
- TO_BPF /* Begin protect field */
- TO_EPF /* End protect field */
-
- TO_VN /* Set video normal */
- TO_VR /* Set video reverse */
- TO_VNU /* Set video normal, underlined */
- TO_VRU /* Set video reverse, underlined */
- TO_VNF /* Set video normal, flashing */
- TO_VRF /* Set video reverse, flashing */
-
- TO_MC /* Move cursor to V-H coordinate */
- TO_MCV /* Move cursor to V coordinate */
- TO_ESC /* Escape the next character */
-
- TI_CAN /* Cancel input line */
- TI_MCH /* Move cursor home */
- TI_MCU /* Move cursor up */
- TI_MCD /* Move cursor down */
- TI_MCR /* Move cursor right */
- TI_MCL /* Move cursor left */
- TI_IL /* Insert line */
- TI_DL /* Delete line */
- TI_IC /* Insert character */
- TI_DC /* Delete character */
- TI_ERL /* erase rest of line */
- TI_ERS /* erase rest of screen */
- TI_PGU /* page up */
- TI_PGD /* page down */
- TI_MCE /* move cursor to end */
-
- TI_F0 /* Function 0 */
- TI_F1 /* Function 1 */
- TI_F2 /* Function 2 */
- TI_F3 /* Function 3 */
- TI_F4 /* Function 4 */
- TI_F5 /* Function 5 */
- TI_F6 /* Function 6 */
- TI_F7 /* Function 7 */
- TI_F8 /* Function 8 */
- TI_F9 /* Function 9 */
- TI_FA /* Function a */
- TI_FB /* Function b */
- TI_FC /* Function c */
- TI_FD /* Function d */
- TI_FE /* Function e */
- TI_FF /* Function f */
- -------------------------------------------------------------------------------
- STANDARD FORMS
- /*******************************************************************************
- * FILENAME.C -- *
- * *
- * *
- * *
- * *
- * *
- * *
- * *
- *******************************************************************************/
- /*------------------------------------------------------------------------------
- | include files |
- ------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------
- | definitions |
- ------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------
- | function declarations |
- ------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------
- | structure declarations |
- | |
- | Value = pointer to the word being stored. |
- | NumOccur = counter of the number of times the word has occurred. |
- ------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------
- | global variable declarations |
- ------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------
- | |
- ------------------------------------------------------------------------------*/
- /*----------------------------------------------------------------------------
- | |
- ----------------------------------------------------------------------------*/
- /*--------------------------------------------------------------------------
- | |
- --------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------
- | |
- ------------------------------------------------------------------------*/
- /*----------------------------------------------------------------------
- | |
- ----------------------------------------------------------------------*/
- /*--------------------------------------------------------------------
- | |
- --------------------------------------------------------------------*/
- /* */
-
-