Reputation: 7048
From C99 Standard:
6.2.1/2
For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces.
I have a confusion with entity and identifier (which I know can be the name of an object). What exactly is an entity according to this quote with example?
Upvotes: 2
Views: 483
Reputation: 754700
Note that a single identifier can designate different entities at the same time.
#include <stdio.h>
static int x = 42; // 1
struct x { int x; }; // 2 & 3
int func(struct x x) // 4
{
if (x.x > 10) goto x; // 5
int y = x.x;
{
int x = 37; // 6
printf("%d %d\n", x, y);
}
x: // 7
return(x.x);
}
I think we can agree x
is a thoroughly overworked identifier.
x
is a static variable of type int
at file scope.x
is a structure tag.x
is a structure member.x
is a parameter/local variable of type struct x
(hiding the file scope variable).x
is a reference to a label.x
is a new local variable (hiding both the file scope variable and the parameter).x
is the definition of the label.For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces.
The structure tag x
is visible from where it is first referenced to the end of the file. It is in the tag namespace, and so does not conflict with other uses of x
. The label x
is in the label name space so it does not conflict with other uses of x
. The structure member x
is in a the 'members of struct x
' namespace and does not conflict with other uses of x
. Those are the three special name spaces.
The other references to x
are all in the 'ordinary identifiers' namespace. Typedef names are also in the ordinary namespace; that's why I didn't use a typedef
. The three different variables called x
have different scopes.
I've foregone the macro namespace; if I added #define x z
at the start, then everything I'm saying about x
would have to be said about 'z
after preprocessing'.
Upvotes: 1
Reputation:
Entities are variables, functions, types, etc. Identifiers are names of those entities. For example:
#include <stdlib.h>
#include <stdio.h>
static int foo()
{
static int x = 0; // "x" is an identifier, the variable "x" inside
// this function "foo()" is an entity.
return x++;
}
int main()
{
int x = 1; // "x" is an identifier as well, and it is the same name "x"
// as of that used inside "foo()". However, this entity
// (variable) is not the same as in "foo()", it has different
// scope and is a totally different entity.
printf("foo()+x++ = %d\n", foo()+x++);
printf("foo()+x++ = %d\n", foo()+x++);
printf("foo()+x++ = %d\n", foo()+x++);
return EXIT_SUCCESS;
}
Upvotes: 3
Reputation: 2484
Consider a Following C program
#include<stdio.h>
int main(void)
{
int var1, var2;
.....
.....
}
int newfun(void)
{
int var1;
float var2;
....
....
}
So, For each different entity (can be thought as a variable), that an identifier(variable name, above: var1, var2), can be used within a region of program text. i.e, the var1, and var2 defined in the main() function can be used within it only.
Although, the identifier in both functions main() and newfun() is same , i.e, int var1, and float var2. They are treated differently in each function.
Now, the variables (or identifiers) in main() function are of type int. They can be collectively called as an entity. But, the same variable var2 in next function is of type float, which is of an different type, and thus, of different entity.
Upvotes: 1
Reputation: 24207
The entity is the variable, I mean it implies some memory (or register) containing data, or compilation objects like types, enum, etc. that have some property like size, component fields, etc. An identifier is just a name for some entity.
Two distinguished entities can have the same name (depending on scope), but at any point of the program a name match only one entity.
Upvotes: 1