Rules for Valid Identifiers in C:
- The identifier must begin with a letter or an underscore (_).
- It cannot start with a number or any other symbol.
- Subsequent characters may be letters, digits, or underscores.
- Special characters like
@
,#
, or spaces are not allowed.
- Special characters like
- Keywords cannot be used as identifiers.
- Keywords like
int
,float
, andwhile
have predefined meanings in C.
- Keywords like
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:
- Variable Declarations:
char a[10];
→ Declares a character arraya
with a size of 10 to store the identifier input.int flag, i=1;
→ Initializes an integerflag
to hold validity status (1 for valid, 0 for invalid) and setsi
to 1 (for looping through the string, starting from the second character).
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.
- 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 arraya
. However,gets()
is unsafe because it doesn’t check the length of the input, which can lead to buffer overflow. It’s recommended to usefgets()
in modern C programs.
- 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.
- 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 setsflag = 0
and exits the loop withbreak;
.
- The program uses a
- 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).
- After the loop, if
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.
- The program doesn’t check for underscores (
- Improvements:
- Replace
gets()
withfgets()
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).
- Replace
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.