C语言中什么是标识符及其规则?

标识符是 C 编程语言中使用的标记之一。它是一个名称,用于标识变量、常量、函数、数组以及用户定义的数据。

我们不能使用关键字作为标识符,因为关键字是为特殊用途而保留的。一旦声明,我们就可以在后面的程序语句中使用标识符来引用关联的值。

特殊类型的标识符称为语句标签,可以在 goto 语句中使用。

规则

命名标识符的规则如下 -

  • 标识符名称是唯一的。

  • 不能使用关键字作为标识符。

  • 标识符必须以字母或下划线 (_) 开头。

  • 它不应包含空格。

  • 不允许有特殊字符。

  • 标识符只能由字母、数字或下划线组成。

  • 只有 31 个字符是有意义的。

  • 它们区分大小写。

示例

以下是用于识别哪些术语被称为标识符的 C 程序-

/* C Program to Count Vowels and Consonants in a String */
#include <stdio.h>
int main(){
   char str[100];
   int i, vowels, consonants;
   i = vowels = consonants = 0;
   printf("\nEnter any String : ");
   gets(str);
   while (str[i] != '\0'){
      if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u'
      || str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U'){
      vowels++;
   }
   else
      consonants++;
      i++;
   }
   printf("\n no of Vowels in the given String = %d", vowels);
   printf("\n no of Consonants in the given String = %d", consonants);
   return 0;
}
输出结果

执行上述程序时,会产生以下结果 -

Enter any String : nhooo.com
no of Vowels in the given String = 6
no of Consonants in the given String = 9

在上面的程序中,标识符是 -

Str, i, 元音, 辅音