Cprogramming 简明教程

C - Identifiers

Identifier in C 有助于标识 C 代码中的 variablesconstantsfunctions 等等。C 作为一种高级计算机语言,允许您通过名称引用内存位置,而不是使用其二进制或十六进制形式的地址。

Identifier in C helps in identifying variables, constants, functions etc., in a C code. C, being a high-level computer language, allows you to refer to a memory location with a name instead of using its address in binary or hexadecimal form.

C Identifiers

标识符是用户定义的名称,用于轻松引用内存。它也用于定义程序中的各个元素,如函数、自定义类型、标签等。因此,标识符是帮助程序员更方便地使用编程元素的名称。

Identifiers are the user-defined names given to make it easy to refer to the memory. It is also used to define various elements in the program, such as the function, user-defined type, labels, etc. Identifiers are thus the names that help the programmer to use programming elements more conveniently.

当使用标识符定义变量或函数时, C compiler 会为其分配内存并将内存位置与 identifier 关联起来。因此,每当在指令中使用标识符时,C 编译器都可以访问其关联的内存位置。例如,当我们声明一个变量 age 并在图中所示的位置为其分配一个值时,编译器会为它分配一个内存位置。

When a variable or a function is defined with an identifier, the C compiler allocates it the memory and associates the memory location to the identifier. As a result, whenever the identifier is used in the instruction, C compiler can access its associated memory location. For example, when we declare a variable age and assign it a value as shown in the following figure, the compiler assigns a memory location to it.

memory

即使程序员可以使用自己选择的标识符为变量或函数等命名,但若要形成一个有效的标识符,也需要遵循某些规则。

Even if the programmer can use an identifier of his choice to name a variable or a function etc., there are certain rules to be followed to form a valid identifier.

Naming Rules of C Identifiers

以下是形成标识符所使用的规则:

Given below are the rules using which an identifier is formed −

  1. Keywords can’t be used as identifiers as they are predefined.

  2. Out of the character set that C uses, only the alphabets (upper and lowercase) and the underscore symbol (_) are allowed in the identifier. It implies that you can’t use characters like the punctuation symbols etc. as a part of the identifier.

  3. The identifier must start either with an alphabet (upper or lowercase) or an underscore. It means, a digit cannot be the first character of the identifier.

  4. The subsequent characters may be alphabets or digits or an underscore.

  5. Same identifier can’t be used as a name of two entities. An identifier can be used only once in the current scope.

根据上述规则,以下是有效和无效标识符的一些示例:

As per the above rules, some examples of the valid and invalid identifiers are as follows −

Valid C Identifiers

age, Age, AGE, average_age, __temp, address1, phone_no_personal, _my_name

Invalid C Identifiers

Average-age, my name, $age, #phone, 1mg, phy+maths

Examples of C Identifiers

以下程序显示错误:

The following program shows an error −

#include <stdio.h>

int main () {

   /* variable definition: */
   int marks = 50;
   float marks = 65.50;
   printf("%d %f", marks, marks);

   return 0;
}

Error

main.c: In function 'main':
main.c:7:10: error: conflicting types for 'marks'; have 'float'
    7 |    float marks = 65.50;
      |          ^~~~~
main.c:6:8: note: previous definition of 'marks' with type 'int'
    6 |    int marks = 50;
      |        ^~~~~
main.c:8:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]
    8 |    printf("%d %f", marks, marks);
      |            ~^      ~~~~~
      |             |      |
      |             int    double
      |            %f
  1. Identifiers are case-sensitive, as a result age is not the same as AGE.

  2. ANSI standard recognizes a length of 31 characters for an identifier. Although you can choose a name with more characters, only the first 31 will be recognized. Thus you can form a meaningful and descriptive identifier.

Scope of C Identifiers

在 C 语言中,标识符的作用域是指声明标识符并可以使用/访问标识符的位置。标识符有两个作用域:

In C language, the scope of identifiers refers to the place where an identifier is declared and can be used/accessed. There are two scopes of an identifier:

Global Identifiers

如果标识符在声明任何函数之前已经在外部声明,则称为全局(外部)标识符。

If an identifier has been declared outside before the declaration of any function, it is called as an global (external) identifier.

#include <stdio.h>

int marks= 100; // external identifier

int main() {
   printf("The value of marks is %d\n", marks);
}

Output

The value of marks is 100

这是因为 marks 在任何块之外定义,所以它是一个外部标识符。

This is because marks is defined outside of any blocks, so it is an external identifier.

Local Identifiers

另一方面,任何函数内的标识符都是局部(内部)标识符。

On the other hand, an identifier inside any function is an local (internal) identifier.

#include <stdio.h>

int main() {
   int marks= 100; // internal identifier

   printf("The value of marks is %d\n", marks);
}

Output

The value of marks is 100

这是因为 marks 在 main 函数中定义,所以它是一个内部标识符。

This is because marks is defined inside main function, so it is an internal identifier.

Examples of Different Types of C Identifiers

标识符也可以出现在函数的前向声明中。但是,函数的声明签名应与定义匹配。

Identifiers can also appear in a forward declaration of a function. However, the declaration signature of a function should match with the definition.

Example of Variable Identifier

int marks1 = 50, marks2 = 60;
float avg = (float) (marks1+marks2)/2;

Example of Function Identifier

int average(int marks1, int marks2)
{
   return (float) (marks1+marks2)/2;
}

Example of User-defined Type Identifier

struct student
{
   int rollno;
   char *name;
   int m1,m2,m3;
   float percent
};
struct student s1 = {1, "Raju", 50, 60, 70, 60.00};

Example of Typedef Identifier

struct student
{
   int rollno;
   char *name;
   int m1,m2,m3;
   float percent
};
typedef struct student STUDENT;
STUDENT s1 = {1, "Raju", 50, 60, 70, 60.00};

Example of Label Identifier

#include <stdio.h>
int main()
{
   int x=0;
   begin:
   x++;
   if (x>=10)
      goto end;
   printf("%d\n", x);
   goto begin;

   end:
      return 0;
}

Output

1
2
3
4
5
6
7
8
9

Example of Enum Identifier

#include <stdio.h>
enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun};

int main() {
   printf("The value of enum week: %d\n",Mon);
   return 0;
}

Output

The value of enum week: 10

因此,_ identifiers _ 在 _ C program _ 中无处不在。为诸如变量或函数之类的编码元素选择正确的标识符对于提高程序的可读性、调试性和文档记录非常重要。

Thus, the identifiers are found everywhere in the C program. Choosing right identifier for the coding element such as the variable or a function is important for enhancing the readability and debugging and documentation of the program.