BIT102 ยท 2078 ยท 10 marks
What is an Identifier and Keyword? Explain the rules for naming valid Identifiers in C with example.Write a program to convert a given string to lowercase without using any library functions.[5+5]
What is an Identifier and Keyword? Explain the rules for naming valid Identifiers in C with example.Write a program to convert a given string to lowercase without using any library functions.[5+5]
Quick answer
An identifier is a name given to various program elements such as variables, functions, arrays, structures, and user-defined types. Identifiers are used to uniquely identify a program element within its scope.
Identifiers and Keywords in C
Identifiers
Example:
int age; // 'age' is an identifier
float salary; // 'salary' is an identifier
void display(); // 'display' is an identifier
Keywords
A keyword is a reserved word that has a predefined meaning in the C language. Keywords cannot be used as identifiers. C has 32 keywords.
Examples of Keywords:
int | float | char | if |
|---|---|---|---|
else | while | for | return |
void | struct | switch | break |
Rules for Naming Valid Identifiers in C
-
Letters, digits, and underscore only: An identifier can only contain alphabets (A-Z, a-z), digits (0-9), and underscore ( _ ).
-
Must begin with a letter or underscore: An identifier cannot start with a digit.
-
No spaces allowed: Spaces are not permitted inside an identifier.
-
Keywords not allowed: Reserved keywords cannot be used as identifiers.
-
Case sensitive:
Ageandageare treated as different identifiers. -
No special characters: Characters like
@,#,$,!, etc. are not allowed. -
Length: Typically, the first 31 characters are significant (as per ANSI C standard).
Examples:
| Identifier | Valid/Invalid | Reason |
|---|---|---|
age | Valid | Starts with letter |
_salary | Valid | Starts with underscore |
total_marks | Valid | Letters and underscore |
var1 | Valid | Letter followed by digit |
1var | Invalid | Starts with digit |
my var | Invalid | Contains space |
int | Invalid | Reserved keyword |
my@name | Invalid | Special character @ |
Program to Convert a String to Lowercase Without Library Functions
#include <stdio.h>
int main() {
char str[100];
int i;
printf("Enter a string: ");
gets(str); /* read the string */
/* Convert each character to lowercase */
for (i = 0; str[i] != '\0'; i++) {
/* Check if character is uppercase (A=65, Z=90 in ASCII) */
if (str[i] >= 'A' && str[i] <= 'Z') {
/* Add 32 to convert uppercase to lowercase */
str[i] = str[i] + 32;
}
}
printf("Lowercase string: %s\n", str);
return 0;
}
Sample Output:
Enter a string: Hello WORLD
Lowercase string: hello world
Logic Explanation:
- In ASCII, uppercase letters range from 65 ('A') to 90 ('Z').
- Lowercase letters range from 97 ('a') to 122 ('z').
- The difference between any uppercase letter and its lowercase equivalent is 32.
- So, adding 32 to an uppercase character converts it to lowercase.
- For example:
'A'(65) + 32 = 97 ='a' - Characters that are already lowercase or non-alphabetic are left unchanged.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.