limits between identifiers and variable names

N

Nnaemeka David

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
 
E

Eric Sosman

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.
 
B

BartC

Nnaemeka David said:
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...)
 
J

James Kuyper

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.
 
M

Malcolm McLean

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.
 
J

James Kuyper

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top