Monday, January 31, 2011

1.10 External Variables and Scope

  • Variables (automatic) in a function are local or private to the particular function
  • Each local variable in a function comes into existence only when the function is called, and disappeared when the function exited
  • Automatic variables do not retain their values from one call to the next, and must be explicitly set upon each entry. If they are not set, they will contain garbage
  • External variables are globally accessible, exist permanently and retain values after function returned, oppose to automatic variables
  • An external variable must be defined, exactly once, outside of any function; to set aside storage for it
  • The external variable must also be declared in each function that want to access it; this states the type of the variable. The declaration may be an explicit "extern" statement or may be implicit from context
  • If the definition of an external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function
  • Common practice is to place definition of all external variables at the beginning of source files and then omit all extern declarations
  • If a program is in several source files, and a variable is defined in file1 and used in file2 and file3, then the extern declarations are needed in file2 and file3 to connect the occurrences of the variable
  • The usual practice is to collect extern declaration of variables and functions in a separate file (header)

Thursday, January 27, 2011

1.9 Character Arrays

  • Print longest text lines:























    • The character '\0' (the null charater) value is zero
    • "hello\n" =
      
    • '/0' mark the end of string of characters.
    • %s = string form argument

    Wednesday, January 26, 2011

    1.8 Arguments - Call by Value

    • All functions arguments are passed "by value": called function is given the value of its arguments in temporary variables rather than originals
    • Called function cannot directly alter a variable in the calling function
    • Call by reference: the called routine has access to the original argument, not a local copy
    • It is possible to arrange for a function to modify a variable in a calling routin. The caller must provide the address of the variable to be set (a pointer to the variable)
    • The called function must declare the parameter to be a pointer.
    • Call by value leads to more compact programs with fewer extraneous variables, because parameters can be treated as initialized local variables in the called routine
    • To modify a variable in a calling function, the caller must provide the address of the variable to be set (pointer to the variable), and the called function must declare the parameter to be a pointer, and access the variable indirectly through it
    • In contrast, when the name of an array is used as an argument in a calling function, the value passed to the calling function is the location or address of the beginning of the array - there is no copying of array elements
    • By subscripting this value, the function can access and alter any element of the array  

      Tuesday, January 25, 2011

      1.7 Functions

      • A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation
      • Function definition:

      • Function definitions declare the parameter types and names, and the type of the result that the function returns ie. int power (int base, int n)
      • Function definitions can appear in any order, and in one source file or several, although no function can be split between files.
      • Power function exm:











      • Generally use parameter for a variable named in the parenthesized list in a function definition, and argument for the value used in a call of the function
      • The name used by function definition for its parameters are local to the function definition, and are not visible to any other function
      • The value that function definition computes is returned to main function by the return expression
      • A function need not return a value; a return statement with no expression cause control, but no useful value, to be returned to the caller
      • In main function, a return value of zero implies normal termination; non-zero values implies erroneous termination condition
      • Function prototype example: int power (int m, int n); expect two int arguments and returns an int
      • Function prototype declaration has to agree with the definition and uses of functions parameter names are optional in a function prototype.

      Monday, January 24, 2011

      1.6 Arrays


      • Array subscripts always start with zero in C (ie. ndigit[0] )
      • A subscript can be any integer expression; which include integer variable (ie. i) and integer constant
      • By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. (ASCII numeric value of '0' is 48 decimal, of '1' is 49 decimal, etc)
      • c - '0' is an integer expression with a value between 0 and 9 corresponding to the character '0' to '9' stored in c, and is thus a valid subscript for the array ndigit.

      ndigit is an array of 10 integers






















      • If else 
      • Any statement can be several statements enclosed in braces

      Thursday, January 20, 2011

      1.5 Character Input and Output

      • A text stream is a sequence of characters divided into lines
      1.51 File Copying: getchar() , putchar()
      • getchar function reads the next input character from a text stream and return that as its value
      • putchar function prints the contents of a variable as a character
      • File Copying:
       
      • EOF: End of File , Numeric Value: 0xFF

      1.52 Character Counting

      • Character Couting:

      • ++ operator : increment by 1
      • -- operator : decrement by 1
      • ++ & -- operator can be prefix or postfix; different values in expressions
      Character counting













      1.53 Line Counting
      • Counting line = counting newlines
      • A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set

      Line Counting















        1.54 Word Counting
        • Counting word = counting blank , tab or newlines

        Wednesday, January 19, 2011

        1.4 Symbolic Constants

        • A #define line gives a symbolic name or symbolic constant to be particular string of characters:
          • #define     name     replacement-text
        • Any occurrence of name will be replaced by corresponding replacement text
        • name has the same form as a variable name: sequence of letters and digits that begins with a letter
        • replacement-text can be any sequence of characters or numbers

        Tuesday, January 18, 2011