Keywords in C are the building blocks of the programming language. These reserved words have predefined meanings and functionalities that the C compiler recognizes. They cannot be used as identifiers, such as variable names or function names, ensuring clarity and consistency in programming.
This post covers all the keywords in C, their significance, and key differences from identifiers. Let’s dive deeper into understanding keywords in C with examples and details.
What Are Keywords in C?
Keywords in C are predefined words with a fixed meaning that the compiler understands. These reserved words cannot be modified or used for any other purpose, ensuring clarity and avoiding ambiguity in the code.
Key Features of Keywords
- Reserved Words:
Keywords are predefined and reserved for specific tasks within the C language, ensuring consistent functionality. - Case-Sensitive:
All keywords in C are written in lowercase and are case-sensitive, distinguishing them from user-defined identifiers. - Fixed Functionality:
The meaning and purpose of keywords are fixed by the language and cannot be altered by the programmer. - Integral to Syntax:
Keywords form the foundational elements of C’s syntax, enabling control structures, data types, and program flow. - Cannot Be Used as Identifiers:
Keywords cannot be used as variable names, function names, or any user-defined labels to avoid conflicts in the program. - Universal Across Compilers:
Keywords behave uniformly across all C compilers, maintaining portability and consistency in C programs.
List of Keywords in C
A list of keywords in C or reserved words in the C programming language are mentioned below:
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 |
Specific Keywords With Descriptions
Keyword | Description |
---|---|
auto | Declares an automatic (local) variable. |
break | Exits a loop or a switch-case statement. |
case | Defines individual cases in a switch block. |
char | Declares a character data type. |
const | Declares a constant variable. |
continue | Skips the current iteration of a loop. |
default | Specifies the default case in a switch statement. |
do | Starts a do-while loop. |
double | Declares a double precision floating-point variable. |
else | Specifies an alternative block in an if statement. |
enum | Defines an enumeration type. |
extern | Declares external variables or functions. |
float | Declares a floating-point variable. |
for | Initiates a for loop. |
goto | Transfers control to a labeled statement. |
if | Introduces a conditional statement. |
int | Declares an integer data type. |
long | Declares a long integer data type. |
register | Declares a register variable for faster access. |
return | Exits from a function and optionally returns a value. |
short | Declares a short integer data type. |
signed | Specifies a signed data type. |
sizeof | Returns the size of a data type or variable in bytes. |
static | Declares a static variable or function. |
struct | Defines a structure type. |
switch | Implements a multi-way branching statement. |
typedef | Defines a new name for an existing type. |
union | Declares a union, which is a special data type. |
unsigned | Declares an unsigned data type. |
void | Specifies no return type or an empty parameter list. |
volatile | Indicates that a variable can be changed unexpectedly. |
while | Starts a while loop. |
Key Properties of Keywords in C
- Predefined and Reserved:
Keywords have fixed meanings defined by the C language and cannot be altered. - Lowercase Only:
All keywords in C are case-sensitive and must be written in lowercase. - Recognized by Compiler:
Keywords are processed uniquely by the compiler to perform specific tasks. - Not Allowed as Identifiers:
Keywords cannot be used as variable names, function names, or any other user-defined identifiers. - Essential for Syntax:
Keywords form the foundation of the C programming syntax, enabling control flow, data types, and other features. - Universal Across Compilers:
The meaning and functionality of keywords remain consistent across all C compilers.
Examples of Keywords in Use
Example 1: Control Flow Keywords
#include <stdio.h>
int main() {
int num = 10;
// if-else keyword
if (num > 5) {
printf("Number is greater than 5\n");
} else {
printf("Number is 5 or less\n");
}
// for keyword
for (int i = 0; i < 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
CExample 2: Data Type Keywords
#include <stdio.h>
int main() {
int integer = 100; // int keyword
char character = 'A'; // char keyword
float floating = 10.5; // float keyword
printf("Integer: %d\n", integer);
printf("Character: %c\n", character);
printf("Floating Point: %.2f\n", floating);
return 0;
}
CDifference Between Keywords and Identifiers
Aspect | Keywords | Identifiers |
---|---|---|
Definition | Reserved words with fixed meanings. | User-defined names for variables, functions, etc. |
Modification | Cannot be modified or redefined. | Can be defined by the programmer. |
Purpose | Performs specific functions. | Identifies variables, functions, etc. |
Case Sensitivity | Always lowercase and case-sensitive. | Case-sensitive but flexible in naming. |
Length | Predefined, usually short. | Can have any length within compiler limits. |
Compilation | Recognized and processed uniquely by the compiler. | Processed as user-defined names. |
Examples | int , while , if , return . | myVariable , calculateSum . |
Usage in Code | Mandatory for specific tasks and syntax. | Optional and used as needed by the programmer. |
Youtube Links:
Keywords in C are the backbone of the programming language, providing the essential structure and functionality needed to write efficient programs. Mastering keywords is a crucial step in becoming proficient in C programming.
The next post is: Identifiers in C. Stay tuned to explore how identifiers are used in tandem with keywords to write meaningful and functional programs.
FAQs on Keywords in C
Q1. Why are keywords reserved in C?
Keywords are reserved to prevent ambiguity and ensure that their specific meaning and functionality remain consistent across all programs.
Q2. Can keywords be redefined?
No, keywords cannot be redefined in C as they have fixed functionality.
Q3. How many keywords are in C?
There are 32 keywords in C.
Q4. Are keywords case-sensitive?
Yes, keywords in C are case-sensitive. For example, int
is valid, but Int
is not a recognized keyword.