C Program

C Program to Test Whether a Given Identifier is Valid or Not

C program to test whether a given identifier is valid or not

Rules for Valid Identifiers in C:

  1. The identifier must begin with a letter or an underscore (_).
    • It cannot start with a number or any other symbol.
  2. Subsequent characters may be letters, digits, or underscores.
    • Special characters like @, #, or spaces are not allowed.
  3. Keywords cannot be used as identifiers.
    • Keywords like int, float, and while have predefined meanings in C.

Example of Valid and Invalid Identifiers:

  • Valid: variable1, _temp, myFunction
  • Invalid: 1variable, float, my variable

#include <stdio.h> // Standard Input/Output library
#include <conio.h> // Console Input/Output library (used for `clrscr()` and `getch()`)
#include <ctype.h> // Library for character handling functions (e.g., `isalpha()`, `isdigit()`)

void main()
{
char a[10]; // Declares an array `a` of 10 characters to store the identifier.
int flag, i=1; // Declares `flag` to indicate validity and `i` to iterate through the array.

clrscr(); // Clears the screen (specific to old compilers like Turbo C, not used in modern compilers).

printf("n Enter an identifier:"); // Asks the user to input an identifier.
gets(a); // Reads the string input and stores it in `a` (Note: `gets()` is unsafe and deprecated; `fgets()` should be used instead).

// Check if the first character is a letter (using `isalpha()` function)
if(isalpha(a[0]))
flag = 1; // If the first character is an alphabet, set `flag = 1` (indicating a valid start).
else
printf("n Not a valid identifier"); // If the first character is not a letter, print an invalid message.

// Loop through the rest of the string to check validity of each character
while(a[i] != '') // Continue until the end of the string (i.e., until the null terminator `''` is found).
{
// If the current character is not a digit and not an alphabet, it's invalid
if(!isdigit(a[i]) && !isalpha(a[i]))
{
flag = 0; // Set `flag = 0` (indicating invalid) and exit the loop.
break;
}
i++; // Move to the next character in the array.
}

// If `flag` is still 1 after the loop, the identifier is valid
if(flag == 1)
printf("n Valid identifier"); // Print valid if no invalid characters were found.

getch(); // Waits for the user to press a key before the program terminates (specific to old compilers like Turbo C).
}

Detailed Explanation:

  1. Variable Declarations:
    • char a[10]; → Declares a character array a with a size of 10 to store the identifier input.
    • int flag, i=1; → Initializes an integer flag to hold validity status (1 for valid, 0 for invalid) and sets i to 1 (for looping through the string, starting from the second character).
  2. clrscr();
    • Clears the screen. This function is specific to older compilers like Turbo C. It’s not necessary in modern compilers like GCC or Visual Studio.
  3. Input from the User:
    • printf("n Enter an identifier:"); → Prompts the user to enter an identifier.
    • gets(a); → Reads the identifier entered by the user and stores it in the array a. However, gets() is unsafe because it doesn’t check the length of the input, which can lead to buffer overflow. It’s recommended to use fgets() in modern C programs.
  4. First Character Check:
    • if(isalpha(a[0])) → Checks if the first character of the identifier is an alphabet (letter). Valid identifiers must start with a letter (or underscore, though this is not considered here).
    • If the first character is a letter, it sets flag = 1, indicating that it might be valid.
    • If the first character is not a letter, the program prints “Not a valid identifier” and skips further checks.
  5. Checking the Rest of the Identifier:
    • The program uses a while loop to check the rest of the characters in the identifier, starting from the second character (i = 1).
    • Inside the loop, if(!isdigit(a[i]) && !isalpha(a[i])) checks if the current character is neither a digit nor an alphabet. If it finds any invalid character (such as special symbols or spaces), it sets flag = 0 and exits the loop with break;.
  6. Final Validation:
    • After the loop, if flag is still 1 (indicating no invalid characters were found), the program prints “Valid identifier.” Otherwise, it doesn’t print anything (since it prints “Not a valid identifier” earlier if the identifier fails at the first character).
  7. getch();
    • This function is used to hold the screen until the user presses a key. It’s specific to older compilers like Turbo C and isn’t required in modern C environments.

Key Points to Note:

  • Limitations:
    • The program doesn’t check for underscores (_), which are allowed as the first character in valid identifiers in C.
    • It doesn’t prevent the use of C keywords as identifiers, which is also a requirement in identifier validation.
  • Improvements:
    • Replace gets() with fgets() to avoid buffer overflow.
    • Modify the program to allow underscores (_) as valid first characters.
    • Add a check to ensure the identifier is not a C keyword (optional but useful).

This program provides a basic mechanism to check the validity of an identifier based on whether it starts with a letter and contains only letters and digits. However, it can be improved by addressing its limitations, such as allowing underscores and preventing the use of C keywords.

Team Educate

About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

C program to test whether a given identifier is valid or not
C Program Compiler Design

C program to implement simple code generator

ALGORITHM:1. Start2. Get address code sequence.3. Determine current location of 3 using address (for 1st operand).4. If current location not
c program
C Program

Write a C program to read name and marks of n number of students

#include <stdio.h> int main() { char name[50]; int marks, i,n; printf(“Enter number of students”); scanf(“%d”, &n); FILE *fptr; fptr=(fopen(“C:\student.txt”,”a”)); if