首页 > 后端开发 > C++ > 检查给定字符串是否是关键字的C程序?

检查给定字符串是否是关键字的C程序?

王林
发布: 2023-09-08 14:45:04
转载
1106 人浏览过

检查给定字符串是否是关键字的C程序?

关键字是在C++库中预定义或保留的单词,具有固定的含义,并用于执行内部操作。C++语言支持超过64个关键字。

每个关键字都以小写字母形式存在,如auto、break、case、const、continue、int等。

C++语言中的32个关键字也可在C语言中使用。

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

这是C++中新增的30个保留字,不在C语言中。

asm dynamic_cast namespace reinterpret_cast
bool explicit new static_cast
catch false operator template
class friend private this
const_cast inline public throw
delete mutable protected true
try typeid typename using
using using wchar_t

Input: str=”for”
Output: for is a keyword
登录后复制

Explanation

  • 关键字是程序中不能用作变量名的保留字。

  • C编程语言中有32个关键字。

将字符串与每个关键字进行比较,如果字符串相同,则字符串是关键字。

Example

 示例

#include <stdio.h>
#include <string.h>
int main() {
   char keyword[32][10]={
      "auto","double","int","struct","break","else","long",
      "switch","case","enum","register","typedef","char",
      "extern","return","union","const","float","short",
      "unsigned","continue","for","signed","void","default",
      "goto","sizeof","voltile","do","if","static","while"
   } ;
   char str[]="which";
   int flag=0,i;
   for(i = 0; i < 32; i++) {
      if(strcmp(str,keyword[i])==0) {
         flag=1;
      }
   }
   if(flag==1)
      printf("%s is a keyword",str);
   else
      printf("%s is not a keyword",str);
}
登录后复制

输出

which is a keyword
登录后复制

以上是检查给定字符串是否是关键字的C程序?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
USE关键字
来自于 1970-01-01 08:00:00
0
0
0
了解“this”关键字的功能和适当用法
来自于 1970-01-01 08:00:00
0
0
0
最关键的回滚、事件没有
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板