C name space and lexical scope

This is a discussion on C name space and lexical scope within the C Programming forums, part of the General Programming Boards category; Hi I want to know the relationship between "name space" and "lexical scope" (or scope) in the C language. In ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    C name space and lexical scope

    Hi
    I want to know the relationship between "name space" and "lexical scope" (or scope) in the C language. In the C standard
    Code:
    If more than one declaration of a particular identifier is visible
    at any point in a translation unit, the syntactic context
    disambiguates uses that refer to different entities.  Thus, there are
    separate name spaces for various categories of identifiers, as
    follows:
    
     * label names (disambiguated by the syntax of the label declaration
       and use);
    
     * the tags of structures, unions, and enumerations (disambiguated by
       following any/11/ of the keywords struct , union , or enum );
    
     * the members of structures or unions; each structure or union has a
       separate name space for its members (disambiguated by the type of the
       expression used to access the member via the .  or -> operator);
    
     * all other identifiers, called ordinary identifiers (declared in
       ordinary declarators or as enumeration constants).
    I'm confused about their relationships. How does C establish different name spaces?
    For example, in a translation unit, if all the tags of structures, unions, and enumerations are in the same name space, then what about the following code:
    Code:
    int
    main(void)
    {
    	struct test{
    		int i;
    		char c;
    	};
    	return 0;
    }
    
    void 
    myfunc(void)
    {
    	struct test{
    		int i;
    		char c;
    	};
    }
    the structure tag "test" is in the same name space, but in different lexical scopes. Does this mean the same idendifier can appear more than once in a name space if each has different lexical scope? Then how about this:
    Code:
    int i;
    int i;
    
    int
    main(void)
    {
    	return 0;
    }
    the variable name "i" is in the same name space, and both have file scope, which are overlapped.

    So is "lexical scope" a prerequisite when discussing "name space", or is "name space" a prerequisite when discussing "lexical scope"?
    Last edited by password636; 09-22-2009 at 09:32 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In the first case the bit you've quoted means that even if you have struct test, you could also declare an int test variable, since variable names and structure tags are in separate namespaces. The two struct definitions are in different scopes, hence both definitions are not visible at the same time, so there can't be a conflict.

    In the second case, C explicitly allows this sort of duplicate definition (that's what linkage is for). This has nothing to do with namespaces at all.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21