C-Language-Series-#8-C-Keywords-and-Identifiers
In the journey of learning C programming, understanding the fundamental building blocks is crucial. This installment delves into two core concepts that form the vocabulary of C: keywords and identifiers. These elements are essential for writing meaningful and functional code, enabling you to communicate your instructions effectively to the compiler.
C Keywords
Keywords are predefined, reserved words in the C language, each having a specific meaning and purpose for the compiler. They are integral to the language's syntax and cannot be used for any other purpose, such as naming variables or functions.
Characteristics of C Keywords:
- They are case-sensitive and must be written in lowercase. For example,
intis a keyword, butIntis not. - Each keyword has a fixed meaning, instructing the compiler to perform a specific action or interpret data in a particular way.
- You cannot redefine a keyword.
C has a relatively small set of keywords, making it a concise language. Here's a list of the 32 keywords defined by the ANSI C standard:
auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof static
struct switch typedef union unsigned void
volatile while
Let's look at an example demonstrating the use of a few keywords:
#include <stdio.h>
int main() {
int age = 30; // 'int' is a keyword, 'age' is an identifier
if (age > 18) { // 'if' is a keyword
printf("Adult\n"); // 'printf' is an identifier (function name)
} else { // 'else' is a keyword
printf("Minor\n");
}
return 0; // 'return' is a keyword, '0' is a constant
}
In this simple program, int, if, else, and return are all keywords,
each playing a vital role in defining data types and controlling program flow.
C Identifiers
While keywords are the language's fixed vocabulary, identifiers are the names you give to various programming elements within your code. They are custom names used to identify variables, functions, arrays, structures, unions, and other user-defined items.
Rules for Naming Identifiers:
- Must start with a letter or an underscore (
_). It cannot start with a digit. - Can consist of letters (A-Z, a-z), digits (0-9), and underscores (
_). No other special characters (like@,#,$,%, etc.) are allowed. - Case-sensitive:
myVariableandmyvariableare treated as two different identifiers. - Cannot be a C keyword. You cannot use
intorforas an identifier. - No spaces allowed. If you need multiple words, use an underscore (e.g.,
total_count) or camelCase (e.g.,totalCount). - Length: While most compilers allow very long identifiers, only the first few characters (typically 31 or 63) are significant. It's good practice to keep them reasonably concise yet descriptive.
Good Practices for Identifiers:
- Descriptive Names: Choose names that clearly indicate the purpose of the variable or
function (e.g.,
studentNameinstead ofs). - Consistency: Stick to a consistent naming convention throughout your project
(e.g.,
snake_casefor variables,PascalCasefor types,camelCasefor functions). - Avoid single-letter names: Except for loop counters (
i,j,k) in small scopes, they often reduce readability.
Let's look at some examples of valid and invalid identifiers:
// Valid Identifiers:
int score;
float student_gpa;
char firstName;
void calculateTotal();
_temp_value;
// Invalid Identifiers (and why):
// int 2ndScore; // Starts with a digit
// float student-gpa; // Contains a hyphen (-)
// char for; // 'for' is a C keyword
// void my function(); // Contains a space
// int total#; // Contains a special character (#)
Keywords vs. Identifiers: A Quick Comparison
Understanding the distinction between keywords and identifiers is fundamental:
- Keywords: Predefined words with fixed meanings, reserved by the C language. You cannot change their meaning or use them for other purposes. They form the basic syntax of the language.
- Identifiers: User-defined names for variables, functions, etc. You create them, following specific rules, to make your code readable and manage its components.
Think of keywords as the grammar rules and common verbs of a language, while identifiers are the proper nouns you invent to name specific things within your story.
Conclusion
Keywords and identifiers are the bedrock of C programming. Keywords provide the structural framework and command vocabulary, while identifiers allow you to give meaningful names to the various entities in your program, enhancing readability and maintainability. Mastering their usage and adhering to their rules is a critical step in becoming a proficient C programmer. As you progress, you'll find yourself intuitively using these concepts to craft robust and efficient C applications.