C Program to Find All The Keywords In a File


This article will guide you through a C program that can find all the keywords in a file.

In the world of programming, keywords play a crucial role in defining the syntax and structure of a programming language.

Also Read: C Program to Find the Inverse of 2×2 Matrix

In the C programming language, keywords are reserved words that have predefined meanings and cannot be used as identifiers.

As a programmer, it is essential to have a clear understanding of these keywords to write efficient and error-free code.

We will explore the implementation details and provide step-by-step instructions for executing the program.

Also Read: C Program to Copy the Contents of One File into Another File

So, let’s dive into the world of C programming and uncover the power of keywords!

How to Identify Keywords in a File?

To find keywords in a file, we need to follow a systematic approach.

The C programming language has a predefined set of keywords, and we can utilize this knowledge to our advantage.

Here’s a step-by-step breakdown of the process:

  1. Open the file: Start by opening the file that contains the code you want to analyze. You can use the fopen() function in C to open a file.
  2. Read the file: Once the file is open, read its contents character by character. You can use the fgetc() function to read characters from a file.
  3. Identify keywords: Compare each word with the list of C keywords. If a match is found, consider it a keyword and count its occurrence.
  4. Display results: Finally, display the keywords and their corresponding counts.

Now that we have a basic understanding of the process, let’s proceed with the implementation details.

Also Read: Getchar and Putchar Function in C with Example

Implementation of the C Program

To implement the program, we will use C’s standard library functions and techniques.

Here’s the C program to find all the keywords in a file:

#include <stdio.h>
#include <string.h>

int isKeyword(char word[]) {
    char keywords[32][10] = {
        "auto", "break", "case", "char", "const", "continue", "default",
        "do", "double", "else", "enum", "extern", "float", "for", "goto",
        "if", "int", "long", "register", "return", "short", "signed",
        "sizeof", "static", "struct", "switch", "typedef", "union",
        "unsigned", "void", "volatile", "while"

    for (int i = 0; i < 32; ++i) {
        if (strcmp(keywords[i], word) == 0) {
            return 1;

    return 0;

int main() {
    char filename[100];
    char word[20];
    int count = 0;

    printf("Enter the filename: ");
    scanf("%s", filename);

    FILE *file = fopen(filename, "r");

    if (file == NULL) {
        printf("Error opening file!\n");
        return 0;

    while (fscanf(file, "%s", word) != EOF) {
        if (isKeyword(word)) {
            printf("%s\n", word);


    printf("\nTotal keywords found: %d\n", count);

    return 0;

Also Read: Best 5 Programs on Fibonacci Series in C

Let’s break down the code to understand its functionality better:

  1. We include the necessary header files, stdio.h and string.h, for input/output and string operations, respectively.
  2. The isKeyword() function checks whether a given word is a keyword or not. It compares the word with the list of keywords using the strcmp() function.
  3. In the main() function, we declare variables to store the filename, word, and keyword count.
  4. The user is prompted to enter the filename of the file they want to analyze.
  5. The file is opened using the fopen() function. If the file opening fails, an error message is displayed, and the program terminates.
  6. We iterate through the file using the fscanf() function, reading each word into the word variable.
  7. If the word is a keyword (determined by the isKeyword() function), we increment the count and display the keyword.
  8. Finally, we close the file and display the total number of keywords found.

Also Read: Program To Reverse a String in C using Pointer


Q: What is the purpose of finding keywords in a file?

The purpose of finding keywords in a file is to analyze the structure and syntax of the code present in the file. It helps programmers identify the keywords used and gain insights into the programming language’s usage patterns.

Q: Can this program handle files of any size?

Yes, the program can handle files of any size. It reads the file character by character, ensuring efficient memory usage and processing.

Q: Are the keywords case-sensitive?

No, the program treats the keywords as case-insensitive. It can identify keywords regardless of their case in the file.

Q: Can I modify the program to find keywords in multiple files?

Certainly! To find keywords in multiple files, you can modify the program to accept a list of filenames as input. You can use loops or recursion to process each file one by one and accumulate the keyword counts.

Q: Are there any limitations to this program?

The program is designed to find C keywords specifically. It may not be suitable for identifying keywords in other programming languages. Additionally, if the code in the file contains preprocessor directives or comments, they will also be considered as keywords by the program.

Q: Can I use this program to find keywords in C++ code?

While the program’s core logic remains the same, C++ has additional keywords not present in the C language. To find keywords in C++ code, you will need to extend the list of keywords in the isKeyword() function to include C++ keywords as well.


Keywords are the building blocks of any programming language, and understanding them is crucial for writing effective code.

Also Read: Find the Runner Up Score | Hackerrank Solution

In this article, we explored a C program that can find all the keywords in a file. We discussed the implementation details, step-by-step instructions, and answered some common questions related to the topic.

By using this program, you can analyze your code files and gain valuable insights into the keywords used.

So, why wait? Give it a try and unlock the power of keywords in your programming journey!

Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered