Identifiers and Keywords
Share:
In the vast world of computer programming, identifiers and keywords are basic elements that constitute the overall syntax and structure of your code. In C, these two elements play a critical role. They are essential for variable declaration, function declaration/definition, and other elements that shape your code. Hence, understanding both in a thorough manner is very crucial. This tutorial will walk you through C Identifiers and Keywords, providing thorough explanations and code snippets for better comprehension.
C Identifiers
An identifier is a name given to a unique element for identification purposes. Remember, no two distinct elements can have the same identifier. Identifiers can be used in C to name variables, functions, arrays, etc. In C, an identifier is a sequence of letters, digits, and underscores, with the condition that the sequence must begin with a letter or an underscore.
To illustrate this, below are few examples of valid and invalid identifiers in C:
Valid identifiers:
int _num;
float num1;
Invalid identifiers:
int 1num; // Identifiers cannot start with a digit.
int num-1; // Hyphen is not a valid character in an identifier.
There are a few rules for naming identifiers in C:
- They must begin with a letter or an underscore.
- They cannot begin with a digit.
- Only underscore and alphanumeric characters are allowed.
- They cannot contain spaces or special symbols like #, $, etc.
- C is case sensitive.
Keywords
Keywords in C are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are the foundation of C language syntax. As such, they cannot be used as identifiers. C has a list of 32 keywords:
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 understand few keywords with the help of examples:
- int: It is used to specify that the type of variable is an integer.
int num = 20;
- float: It is used to specify that the type of variable is a floating point.
float avg = 20.5;
- return: It is used to return a value from a function.
int add(int a, int b){
return a+b;
}
- void: It specifies that the function doesn't return a value.
void printMessage() {
printf("Hello World");
}
In conclusion, identifiers and keywords are essential building blocks in C. Identifiers represent the entities in your code, and keywords drive the structure and flow of the program. Understanding these elements is a first step towards becoming proficient in the C programming language.
0 Comment
Sign up or Log in to leave a comment