Keywords are reserved words in C that have special meanings defined by the language. They are part of the C syntax and cannot be used as identifiers (variable names, function names, etc.). Here’s a detailed explanation of keywords in C:
What Are Keywords?
Keywords are reserved words predefined by the C compiler that convey specific instructions. Since they are part of the language syntax, they must be used according to their intended functionality, and you cannot use them as names for variables or functions.
List of C Keywords
Below is a list of common keywords in the C language and their functions:
auto
: Used for automatic variable declarations (default behavior, rarely used explicitly).break
: Terminates the current loop or switch statement.case
: Defines a branch in aswitch
statement.char
: Declares a character data type.const
: Defines constant values that cannot be modified.continue
: Skips the current iteration of a loop and moves to the next iteration.default
: Specifies the default block of code in aswitch
statement if no case matches.do
: Starts ado-while
loop, which executes the loop body at least once.double
: Declares a double-precision floating-point number.else
: Defines the alternative block of code for anif
condition.enum
: Declares an enumeration, a user-defined type for named integer constants.extern
: Declares a global variable or function that is defined elsewhere.float
: Declares a floating-point data type.for
: Starts afor
loop, commonly used for iteration.goto
: Transfers control to a labeled statement (not recommended, as it makes code harder to read).if
: Used for conditional branching.int
: Declares an integer data type.long
: Declares a long integer, a larger version ofint
.register
: Suggests storing the variable in a CPU register for fast access (modern compilers usually ignore this).return
: Exits from a function and optionally returns a value.short
: Declares a short integer, which uses less memory thanint
.signed
: Declares a signed type, which can hold both positive and negative values.sizeof
: Returns the size of a data type or variable in bytes.static
: Defines the lifetime and visibility of a variable or function:- When used with variables, retains the value between function calls.
- When used with functions, restricts visibility to the current file.
struct
: Declares a structure, a collection of variables of different data types under a single name.switch
: A multi-way branching statement based on the value of a variable.typedef
: Defines an alias for an existing data type.union
: Declares a union, similar to astruct
, but members share the same memory space.unsigned
: Declares an unsigned type that can hold only positive values (or zero).void
: Declares a function that does not return a value or a pointer to "nothing."volatile
: Indicates that a variable’s value may be changed by something beyond the control of the code section in which it appears.while
: Starts awhile
loop, which continues as long as the given condition is true.
Examples of Commonly Used Keywords
if
,else
, andreturn
:int main() { int a = 5; if (a > 3) { return 1; } else { return 0; } }
for
Loop:for (int i = 0; i < 10; i++) { printf("%d\n", i); }
switch
Statement:int day = 3; switch (day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; default: printf("Another day"); }
Summary
- Keywords are Reserved Words: They cannot be used as variable or function names.
- Keywords Are Built-in: Each has a specific, well-defined purpose in the language.
- Understanding Their Use: Familiarizing yourself with C keywords is essential for writing clear and correct C programs.
Mastering C keywords will allow you to control the flow of your program, define data, and perform various operations effectively.