Page 191 - C-Language
P. 191

Chapter 27: Identifier Scope




        Examples



        Block Scope


        An identifier has block scope if its corresponding declaration appears inside a block (parameter
        declaration in function definition apply). The scope ends at the end of the corresponding block.


        No different entities with the same identifier can have the same scope, but scopes may overlap. In
        case of overlapping scopes the only visible one is the one declared in the innermost scope.


         #include <stdio.h>

         void test(int bar)                   // bar has scope test function block
         {
             int foo = 5;                     // foo has scope test function block
             {
                 int bar = 10;                // bar has scope inner block, this overlaps with previous
         test:bar declaration, and it hides test:bar
                 printf("%d %d\n", foo, bar); // 5 10
             }                                // end of scope for inner bar
             printf("%d %d\n", foo, bar);     // 5 5, here bar is test:bar
         }                                    // end of scope for test:foo and test:bar

         int main(void)
         {
             int foo = 3;         // foo has scope main function block

             printf("%d\n", foo); // 3
             test(5);
             printf("%d\n", foo); // 3
             return 0;
         }                        // end of scope for main:foo


        Function Prototype Scope



         #include <stdio.h>

         /* The parameter name, apple, has function prototype scope.  These names
            are not significant outside the prototype itself.  This is demonstrated
            below. */

         int test_function(int apple);

         int main(void)
         {
             int orange = 5;

             orange = test_function(orange);
             printf("%d\r\n", orange); //orange = 6

             return 0;




        https://riptutorial.com/                                                                             167
   186   187   188   189   190   191   192   193   194   195   196