limits between identifiers and variable names

Discussion in 'C Programming' started by Nnaemeka David, Oct 7, 2013.

  1. what is the distinction between an identifier and variable name in c? That is, what are the limits between both terms? Let's take the following code fragment as an example:
    #include <stdio.h>
    struct a {
    int num;
    } a;
    void main()
    {
    a.num = 10;
    printf("%d\n",a.num);
    }
    here a is both a tag and a variable name which are both identifiers and both variables!?!? Unsure of the latter!
    thanks
    Nnaemeka David, Oct 7, 2013
    #1
    1. Advertising

  2. Nnaemeka David

    Eric Sosman Guest

    On 10/7/2013 11:17 AM, Nnaemeka David wrote:
    > what is the distinction between an identifier and variable name in c? That is, what are the limits between both terms? Let's take the following code fragment as an example:
    > #include <stdio.h>
    > struct a {
    > int num;
    > } a;
    > void main()


    Aside: Don't do that. main() returns an int.

    > {
    > a.num = 10;
    > printf("%d\n",a.num);
    > }
    > here a is both a tag and a variable name which are both identifiers and both variables!?!? Unsure of the latter!
    > thanks


    You're right `a' is an identifier in each appearance, but you're
    wrong in thinking that both appearances designate variables. To
    reduce confusion, let's change your example to

    struct tag {
    int num;
    } var;

    All of `tag' and `num' and `var' are identifiers, just like `a' and
    `num' and `a' (and `main' and `printf') in your original example.
    However, in the revised example only `var' identifies a variable.
    `tag' is a part of `struct tag', the name of a type, not the name
    of a variable. And `num' identifies one of the elements of a
    `struct tag', again not a variable (`var.num' could be called a
    variable, but `num' by itself could not be).

    An identifier is a bit of source code that identifies something,
    it is a name for something. Many identifiers are names of variables,
    but programs have other things that can be named. The Standard lists
    all the things identifiers can identify (in 6.2.1 paragraph 1):

    "An identifier can denote an object; a function; a tag
    or a member of a structure, union, or enumeration; a typedef
    name; a label name; a macro name; or a macro parameter."

    Of these, only "object" is approximately equivalent to "variable;"
    the other identified things are all non-variables.

    Summary: Identifiers are names; variables ("objects") are one
    of the classes of things that have names.

    --
    Eric Sosman
    d
    Eric Sosman, Oct 7, 2013
    #2
    1. Advertising

  3. Nnaemeka David

    BartC Guest

    "Nnaemeka David" <> wrote in message
    news:...
    > what is the distinction between an identifier and variable name in c? That
    > is, what are the limits between both terms? Let's take the following code
    > fragment as an example:
    > #include <stdio.h>
    > struct a {
    > int num;
    > } a;
    > void main()
    > {
    > a.num = 10;
    > printf("%d\n",a.num);
    > }
    > here a is both a tag and a variable name which are both identifiers and
    > both variables!?!? Unsure of the latter!
    > thanks


    The identifier or name following 'struct' (its 'tag') occupies a different
    name-space compared with other kinds of names. So they can co-exist. It
    works because the struct tags always follow the word 'struct', so they can't
    get mixed up.

    That doesn't mean you can't write confusing code:

    struct a a;

    (I had thought the same applied to typedef names, in that you can do this:

    typedef int A;

    {
    A A;

    which would have been even more confusing (because of the lack of 'struct'
    to act as a cue). But it seems it needs the "{" to separate the scopes, and
    the second 'A' immediately hides the first A, so that you can't reuse the
    typedef-A name until the int-A goes out of scope. Still, it looks cool...)

    --
    Bartc
    BartC, Oct 7, 2013
    #3
  4. Nnaemeka David

    James Kuyper Guest

    On 10/07/2013 11:17 AM, Nnaemeka David wrote:
    > what is the distinction between an identifier and variable name in c? That is, what are the limits between both terms? ...


    The C standard does not define what "variable" means, despite making
    frequent use of the term. I personally interpret that term to refer to a
    "named object". This definition is consistent with every use of the term
    in the C standard. It also matches the definition that is provided by
    the C++ standard - that isn't proof that it's correct in a C context,
    but does suggest that I'm thinking along the right track.

    In C, "An identifier can denote an object; a function; a tag or a member
    of a structure, union, or enumeration; a typedef name; a label name; a
    macro name; or a macro parameter." (6.2.1p1) Notice that most of the
    things an identifier can identify are NOT objects.

    > ... Let's take the following code fragment as an example:
    > #include <stdio.h>
    > struct a {
    > int num;
    > } a;


    The first 'a' is a struct tag; the second 'a' is the name of an object.
    That's permitted in C, because those identifiers are in different name
    spaces; they identify different things. I think it's a confusing
    practice, however.

    Neither the tag 'a' nor the member name 'num' identifies an object. The
    expression a->num does designate an object, and therefore does, in a
    sense, identify it. However, as far as C is concerned, that expression
    is is not itself an identifier, though it does contain two identifiers.
    James Kuyper, Oct 7, 2013
    #4
  5. On Monday, October 7, 2013 4:17:58 PM UTC+1, Nnaemeka David wrote:
    >
    > what is the distinction between an identifier and variable name in c?
    >

    You don't really need to worry about it unless you're writing a compiler
    and come across some esoteric corner of the C standard.

    Identifiers are names that C programmers give to things, including variables
    but also functions, structures, and other objects. Normally a C identifier
    needs to start with a letter or underscore, and may contain only letters,
    underscores, and digits. identifiers are case sensitive.
    There might be some obscure difference between legal variable names and
    other legal identifiers, but, as I said, you don't really need to worry about
    it unless you're writing a compiler.
    Malcolm McLean, Oct 7, 2013
    #5
  6. Nnaemeka David

    James Kuyper Guest

    On 10/07/2013 12:17 PM, BartC wrote:
    ....
    > The identifier or name following 'struct' (its 'tag') occupies a different
    > name-space compared with other kinds of names. So they can co-exist. It
    > works because the struct tags always follow the word 'struct', so they can't
    > get mixed up.
    >
    > That doesn't mean you can't write confusing code:
    >
    > struct a a;
    >
    > (I had thought the same applied to typedef names, in that you can do this:
    >
    > typedef int A;
    >
    > {
    > A A;
    >
    > which would have been even more confusing (because of the lack of 'struct'
    > to act as a cue). But it seems it needs the "{" to separate the scopes, and
    > the second 'A' immediately hides the first A, so that you can't reuse the
    > typedef-A name until the int-A goes out of scope. Still, it looks cool...)


    "Different entities designated by the same identifier either have
    different scopes, or are in different name spaces." (6.2.1p2)
    "A typedef name shares the same name space as other identifiers declared
    in ordinary declarators." (6.7.8p3)

    Since your two uses of 'A' are in the same namespace, they cannot have
    the same scope. As you noted above, struct tags are in a different name
    space (6.2.3p1).
    James Kuyper, Oct 7, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Bos
    Replies:
    3
    Views:
    356
    CBFalconer
    Feb 6, 2004
  2. wanwan
    Replies:
    3
    Views:
    417
    Alex Martelli
    Oct 14, 2005
  3. Ivan A. Kosarev
    Replies:
    0
    Views:
    352
    Ivan A. Kosarev
    Jan 13, 2006
  4. Replies:
    1
    Views:
    338
    Roedy Green
    Apr 22, 2008
  5. News123
    Replies:
    2
    Views:
    459
    John Machin
    Nov 26, 2008
Loading...

Share This Page