Keywords and Identifiers in C programming language are the building blocks of any C program. Identifiers are user-defined names for various program elements, while keywords are reserved words with predefined meanings in the language. This tutorial explores the concepts of identifiers and keywords in C, providing a comprehensive understanding for beginners and aspiring programmers.
Identifiers in C
In C programming language, the name of variables, functions, labels and user-defined entities are called Identifiers. Each element of a C program are given an identifier. A C identifier can be of any length, there is no limit on the length of the identifiers in C.
- int Interest;
- int getSimpleInterest(int amount);
Interest is an identifier for a variable of integer data type and getSimpleInterest is an identifier for a function.
- An identifier can be composed of alphabets, digits, and underscore only.
- The first character of an identifier must be either an alphabet or underscore.
- Identifier name is case sensitive. Home and home is recognised as two separate identifiers.
- Any special characters other than alphabets, digits, and underscore (such as :, . ,blank space, / are not allowed).
- An identifier cannot be same as a C keyword.
Examples of Valid and Invalid Identifiers
Valid Identifiers | Invalid Identifiers |
---|---|
program | 1program |
Program | Progr/am |
_program | prog ram |
prog_ram | pro,,,gram |
Best Practices for Naming Identifiers
- Use meaningful names : Choose names that reflect the purpose or content of the variable or function.
- Follow a consistent naming convention : Stick to a consistent style for naming identifiers (e.g., camelCase or snake_case).
- Avoid single-letter names : Unless used for loop counters, single-letter names can be cryptic and make the code less readable.
- Choose descriptive names : Aim for names that convey the purpose of the identifier.
// Good Naming Practices int numberOfStudents; // Descriptive and camelCase float averageSalary; // Descriptive and camelCase void calculateTotalAmount(); // Descriptive function name
Keywords in C
Keywords in C are reserved words that have predefined meanings and cannot be used as identifiers. These words are an integral part of the language and are used to define various elements such as data types, control structures, and functions. Using keywords as identifiers results in a compilation error.
- Trying to use a keyword as an identifier will generate a compilation error.
- All keywords are in lower case letters.
There are 32 keywords defined in C programming language.
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
continue | for | signed | void |
do | if | static | while |
default | goto | sizeof | volatile |
const | float | short | unsigned |
- auto : Sets a variable's automatic storage class, which means that it is made and destroyed automatically within a block.
- break : Ends the loop or switch line that is closest to it, letting program control leave the loop or switch.
- case : Part of the switch statement, defines a condition or value to be matched.
- char : Declares a a variable to hold a character. Characters are one-letter symbols that can be printed, like "a" or "3."
- const : When you declare a variable as constant, its value can't be changed after initialization.
- continue : Skips the rest of the body of the loop and moves on to the next iteration.
- default : Part of the switch statement which tells the program what to do if none of the other cases match.
- do : Starts a do-while loop. The code inside the loop is run at least once, and at the end the loop condition is evaluated.
- double : Declares a variable to store double-precision floating-point numbers.
- else : Part of the if statement, specifies the block of code to be executed if the condition is false.
- enum : Declares an enumeration type, a user-defined data type consisting of named integer constants.
- extern : Declares a variable or function as external, indicating that it is defined elsewhere in the program or in another file.
- float : Declares a variable to store single-precision floating-point numbers.
- for : Initiates a for loop, providing a concise way to express a loop with initialization, condition, and iteration expressions.
- goto : Transfers control to a labeled statement in the program. The use of goto is generally discouraged due to its impact on code readability.
- if : Introduces a conditional statement, where a block of code is executed if a specified condition is true.
- int : Declares a variable to store integers, which are whole numbers without a fractional part.
- long : Declares a variable to store long integers, providing a larger range of values compared to int.
- register : Suggests to the compiler that a variable should be stored in a register for faster access.
- return : Indicates the end of a function and returns a value to the calling function.
- short : Declares a variable to store short integers, using less memory than int.
- signed : Declares a variable as signed, meaning it can represent both positive and negative values.
- sizeof : Returns the size, in bytes, of a data type or object.
- static : Declares a variable as static, meaning it retains its value between function calls and has internal linkage.
- struct : Declares a structure, a composite data type that groups variables under a single name.
- switch : Initiates a switch statement, allowing the program to execute different blocks of code based on the value of an expression.
- typedef : Adds a new type alias to an existing data type, giving it a different name.
- unsigned : Declares a variable as unsigned, meaning it can represent only non-negative values.
- void : In a function declaration, the symbol "void" denotes a function that does not return a value or an empty parameter list in a function declaration.
- volatile : Notifies the compiler that, even though a variable seems to be unmodified, its value might change at any time.
- while : Starts a while loop in which a block of code is continually run as long as a predetermined condition is met.
Examples of Identifiers and Keywords in C
- Example 1: Identifiers
#include <stdio.h> int main() { int total_students = 100; float average_marks = 85.5; printf("Total Students: %d\n", total_students); printf("Average Marks: %.2f\n", average_marks); return 0; }
The total number of students and the average mark are represented in this example by the identifiers total_students and average_marks, respectively. - Example 2: Keywords
#include <stdio.h> int main() { int x = 5; int y = 10; if (x < y) { printf("x is less than y\n"); } else { printf("x is greater than or equal to y\n"); } return 0; }
In this example, int, if, and else are keywords. int is used to declare variables, and if and else are used for conditional statements.