Home  >  Article  >  Backend Development  >  C program to check if a given string is a keyword?

C program to check if a given string is a keyword?

王林
王林forward
2023-09-08 14:45:041053browse

C program to check if a given string is a keyword?

Keywords are words that are predefined or reserved in the C library, have a fixed meaning, and are used to perform internal operations. The C language supports more than 64 keywords.

Each keyword exists in lowercase letters, such as auto, break, case, const, continue, int, etc.

The 32 keywords in C language can also be used in C language.

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

##This is in C The newly added 30 reserved words are not in C language.

asmdynamic_castnamespacereinterpret_castboolexplicitnewstatic_cast##catchclassconst_castdeletetryusing
false operator template
friend private this
inline public throw
mutable protected true
typeid typename using
using wchar_t

Input: str=”for”
Output: for is a keyword

Explanation

    Keywords are reserved words that cannot be used as variable names in the program.
  • There are 32 keywords in the C programming language.
  • Compares the string with each keyword, if the strings are the same, then the string is a keyword.

Example

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);
}

Output

which is a keyword

The above is the detailed content of C program to check if a given string is a keyword?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete