In C programming, keywords are reserved words that have a predefined meaning and cannot be used as identifiers (variable names, function names, etc.). Keywords are integral to the syntax of the C language and perform specific actions. In C there are 32 keywords in C.
The C language has a set of standard keywords. Below is a list of commonly used C keywords:
int, float, double, charif, else, switch, casefor, while, do, break, continuereturn, void, struct, sizeofauto, register, static, externconst, enum, goto, volatileauto: Declares a local variable with automatic storage duration (default for local variables).break: Exits a loop or switch statement early.case: Defines a specific condition to execute within a switch statement.char: Defines a character data type.const: Declares a variable as constant, preventing modification.continue: Skips the current iteration in a loop and moves to the next one.default: Executes a block of code if no case matches in a switch.do: Runs a loop that executes at least once before checking the condition.double: Defines a double-precision floating-point data type.else: Provides an alternative block of code if the if condition is false.enum: Defines a set of named integer constants.extern: Declares a variable that is defined in another file or scope.float: Defines a floating-point data type.for: Initiates a loop that repeats until a specified condition is met.goto: Transfers control to a labeled statement within the program.if: Starts a conditional statement to execute code based on a condition.int: Defines an integer data type.long: Modifies data types to allow larger storage.register: Suggests that a variable be stored in a register for faster access.return: Exits a function and optionally returns a value.short: Modifies data types to use less storage.signed: Declares a signed data type, allowing both positive and negative values.sizeof: Returns the size of a data type or variable in bytes.static: Preserves a variable’s value between function calls or restricts scope to a file.struct: Defines a structure, grouping variables of different types.switch: Allows multiple conditional checks within one statement.typedef: Defines a new name (alias) for a data type.union: Allows storage of different data types in the same memory location.unsigned: Declares an unsigned data type, allowing only positive values.void: Specifies that a function does not return a value or is used for pointer declarations.volatile: Informs the compiler that a variable may change unexpectedly.while: Initiates a loop that repeats as long as a specified condition is true.Below are examples demonstrating the use of several common keywords in C:
int age = 25;
float height = 5.9;
char grade = 'A';
In this example, int, float, and char are data type keywords that define the type of variables.
int age = 25;: Declares an integer variable named age and assigns it the value 25.float height = 5.9;: Declares a floating-point variable named height and assigns it the value 5.9.char grade = 'A';: Declares a character variable named grade and assigns it the value 'A'.int i;
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d is odd\n", i);
}
Here, for, if, and continue are control and loop keywords that manage the flow of the program.
for (i = 0; i < 10; i++): Initializes a loop that iterates from 0 to 9, incrementing i by 1 each time.if (i % 2 == 0): Checks if i is an even number. If it is, the code will skip the remaining statements within the loop body using continue.printf("%d is odd\n", i);: Prints the value of i along with the text "is odd" to indicate that i is an odd number.static int count = 0;
extern int globalVar;
The keyword static specifies a persistent storage class, while extern is used to declare an external variable.
static int count = 0;: Declares a static integer variable count that retains its value between function calls.extern int globalVar;: Declares an external integer variable globalVar that is defined elsewhere, such as in another file.const for constants).Keywords in C programming are essential for building the language’s syntax. Knowing how to use them effectively will allow you to write clear, structured, and error-free programs. By mastering keywords, you can better utilize C’s built-in functionalities.