2078 2

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]

C ProgrammingChapter 1: Fundamentals of C ProgrammingRules for naming valid identifiers

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:

intfloatcharif
elsewhileforreturn
voidstructswitchbreak

Rules for Naming Valid Identifiers in C

  1. Letters, digits, and underscore only: An identifier can only contain alphabets (A-Z, a-z), digits (0-9), and underscore ( _ ).

  2. Must begin with a letter or underscore: An identifier cannot start with a digit.

  3. No spaces allowed: Spaces are not permitted inside an identifier.

  4. Keywords not allowed: Reserved keywords cannot be used as identifiers.

  5. Case sensitive: Age and age are treated as different identifiers.

  6. No special characters: Characters like @, #, $, !, etc. are not allowed.

  7. Length: Typically, the first 31 characters are significant (as per ANSI C standard).

Examples:

IdentifierValid/InvalidReason
ageValidStarts with letter
_salaryValidStarts with underscore
total_marksValidLetters and underscore
var1ValidLetter followed by digit
1varInvalidStarts with digit
my varInvalidContains space
intInvalidReserved keyword
my@nameInvalidSpecial 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.