C strengthening Summer workshop notes 1

  • Debugging using Eclipse

  • Variable Scope:

    • Globle Scope

      1. Outside any function

      2. Can be used by lator blocks of code:

    • int g;                                                                                          int main(void){                                                                                        g=0;}

           3. Only one instance if any name can exist at the same level     

    • int a;                                                                                        int a;//is illegal
    • Scope inside blocks

      1. blocks inherit all variables from global scope

      2. May declare variables with the same name to hide(but not replace)global varibles.(if blocks are nested, variables declared in an inner block may hide variables declared in an outer block)

      3. Variables declared inside a block have a different scope from global variables, and may even be of a different type:

    • int a;
      void f(void)
      {
          float a;/* different from global `a' */
          float b;
          a = 0.0;/* sets `a' declared in this block to 0.0 */
                  /* global `a' is untouched */
          {
              int a;/* new `a' variable */
              a = 2;/* outer `a' is still set to 0.0 */
          }
          b = a;/* sets `b' to 0.0 */
      }
      /* global `a' is untouched by anything done in f() */

      4. This method of hiding variables is a potential source of bugs and some compilers will issue a warning if a variable is found to be hidden.

      5. We usually group all the local variables in the same scope.

      6. Unlike java, the scoping of a function itself is always global.


你可能感兴趣的:(C strengthening Summer workshop notes 1)