List Of Keywords In C | Properties & Application (+Code Examples)
With technological advances, understanding and using one of the most important programming languages—C—is increasingly essential. This versatile language powers everything from basic programs like word processors and calculators to more complex applications like artificial intelligence systems. Mastering C can significantly improve your programming skills, opening doors to advanced software development. In this article, we will explore the significance of keywords in C language, an integral part of the language, and provide insights into their usage.
What Are Keywords In C?
Keywords, also known as reserved words, are predefined tokens in C programming language that have special meanings and purposes. These words are reserved by the language and cannot be used as variable names or any other identifiers. It's important to note that C language is case-sensitive, meaning that keywords written in uppercase (e.g., IF) are treated differently than those written in lowercase (if).
How Many Keywords Are There In C Language?
The C programming language includes a relatively small set of keywords compared to many other languages. As per the C99 standard, there are 32 keywords in C, as listed below:
break |
case |
char |
const |
Auto |
short |
struct |
Switch |
double |
int |
else |
enum |
float |
continue |
sizeof |
Default |
extern |
for |
do |
goto |
If |
typedef |
union |
Void |
static |
signed |
long |
register |
return |
unsigned |
volatile |
while |
These keywords have specific meanings and are crucial for defining control structures, data types, function declarations, and other foundational elements in C programs. You're likely familiar with common ones like int, char, float, and return.
In the sections ahead, we will look at the properties of these keywords, a list with a description and syntax/ snippet and then at examples showcasing their implementation.
Properties Of Keywords In C
Keywords in C have certain properties that distinguish them from other elements, like variable names or user-defined identifiers:
- Reserved: Keywords are predefined by the language and cannot be used for anything other than their intended purpose, such as variable names/ identifiers or function names.
- Case-Sensitive: Keywords in C are case-sensitive, so if and IF are considered different. Using the wrong case may lead to syntax errors.
- Fixed Meaning: Each keyword has a well-defined, unchangeable meaning in the C language that applies consistently across all C programs.
- Limited Number: C has a finite number of keywords (32 in total), which simplifies the language while ensuring clarity and consistency.
- Compiler Recognition: Keywords are recognized by the compiler during the compilation process (parsing phase) and help guide it in executing the C program correctly.
- Contextual Usage: Keywords are used in specific contexts. For example, if and else are used for conditional statements, while int and float are used for declaring variable data types.
Alphabetical List Of Keywords In C
Here is a list of the 32 keywords in C, along with a snippet or example:
1. auto keyword in C: It is used to declare local automatic variables, meaning the compiler deduces the data type based on the value assigned to the variable. It’s useful when you want the compiler to infer the type automatically instead of specifying it explicitly. For example:
auto x = 5;
Here, the auto keyword tells the compiler to deduce the data type, which for the variable x is int. These variables have a local scope within a block or function.
2. break keyword in C: It creates break statements, often inside other iterative control statements or loops. When encountered, this keyword leads to immediate and premature termination of the current iteration (of the loop or switch statement, etc.) and transfers control to the next line in the code. For example:
for(int i = 0; i < 10; i++) {
if(i == 5) break;}
Here, the if-statement inside the for loop checks if the values of variable i are equal to 5 (using the relational equality operator). If the condition is true, the break statement is encountered, which terminates the iterations of the for loop and passes control to the next line in the code.
3. case keyword in C: Used inside switch statements, this keyword marks a particular case/ value to compare against the main controlling expression in the switch. (We have discussed the switch keyword in the list ahead). For example:
switch(x) {
case1:
// code
break;
case2:
// code
break;}
Here, case1, case2, ... refer to the various executable code sections, which will be executed if the case matches the value of x provided in the switch() section.
4. char keyword in C signifies one of the basic primitive data types, character. It is often used to declare variables of character type or define other data structures with character elements. For example:
char myChar = 'A';
This statement declares a char variable named myChar and initializes it with the character 'A'. A char in C is 1 byte in size and can hold values between -128 to 127 (or 0 to 255, depending on the system).
5. const keyword in C: It is used to declare constant variables in C programs. Once initialized, these variables retain their values through the program, i.e., they cannot be modified once assigned. For example:
const int PI = 3.14;
const int ARRAY_SIZE = 4;
Here, the variable PI is declared a constant, meaning its value will remain at 3.14 and cannot be altered anywhere in the code. As shown in the next line, we can use the const keyword to declare a variable to fix an array's size.
6. continue keyword in C: It is used to create the continue statement, which is used inside control statements. When encountered, it makes the control skip the remaining statements within the iteration and start the next iteration. For example:
for(int i = 0; i < 10; i++) {
if(i == 5) continue;
// code
}
Here, if the condition inside the if-statement is true, the continue keyword is encountered. It makes the program skip the remaining lines (//code) and continue to the beginning of the loop.
7. default keyword in C: It is used within a switch statement to specify the default case, which is executed if none of the other cases match the switch expression. It is like a fail-safe for all other values when none of the other cases match. For example:
switch(x) {
case 1:
// code
break;
default:
// default code
}
Here, the code inside the default case is executed when x doesn’t match the other cases, acting as a fallback.
8. do keyword in C: It is used to create a do-while loop, which executes a block of code at least once before checking the condition at the end of the loop (marked by the while keyword). For example:
do {
// code
} while(condition);
Here, the do keyword marks the beginning of the do-while loop and is followed by a set of instructions/ codes (to be executed) inside the curly braces. (We have discussed the while keyword later in the list.)
9. double keyword in C: This is used to define variables of double-precision floating-point data type. These variables occupy more memory than float and are used when more precision is needed for decimal numbers. For example:
double myDouble = 3.14159;
Here, myDouble stores a more precise floating-point value (with more decimal places) than a float would allow.
10. else keyword in C: It is used in conjunction with the if keyword to create if-else statements. It defines what happens when the condition in the if statement is false. For example:
if(condition) {
// code if the condition is true
} else {
// code if the condition is false
}
Here, the else block is executed only if the if condition is evaluated as false.
11. enum keyword in C: It is short for the enumeration type, a special user-defined data type consisting of named integer values. It is used when a variable can only take one out of a small set of predefined/ possible values. For example:
enum Days { MON, TUE, WED, THU, FRI, SAT, SUN };
Here, Days is an enumeration that represents the days of the week. Each enumerator is automatically assigned an integer value, starting from 0 (e.g., MON is 0, TUE is 1, etc.).
12. extern keyword in C: External variables (global variables with external linkage) are defined in one file and can be used by multiple other files/ programs. For example:
extern int globalVariable;
Here, globalVar is declared as an external variable, meaning its actual definition exists in another file or section of code. We use the extern keyword to declare the external variable, to let the compiler know that the variable is defined in another file.
13. float keyword in C: It defines a single-precision floating type variable, which is a variable that can store decimal numbers. It takes up less memory than a double but has lower precision. For example:
float myFloat = 2.5;
Here, myFloat stores the value 3.14 with a limited precision compared to double.
14. for keyword in C: It is used to create a for loop that repeats a block of code a specified number of times. It consists of an initialization, a loop condition, and an updation condition, all in one line. For example:
for(int i = 0; i < 10; i++) {
// code
}
Here, the for keyword marks the beginning of the loop, whose conditions are given inside the braces (). This loop runs 10 times, with i starting at 0 and incrementing its value by 1 until it reaches 9. It’s perfect for situations where the number of iterations is known beforehand.
15. goto keyword in C: It is used to create goto statements, a type of jump statement. It transfers control to a labelled part of the program, i.e., moves control jump from one place (where the condition is met) to another in code (marked by label). For example:
goto label;
// code
label:
// code after goto
Here, the goto keyword marks the beginning of the statement, which transfers execution to the code following the label. It is generally discouraged because it can make code harder to follow, but it's useful in rare cases like breaking out of nested loops.
16. if keyword in C: It is used to create an if statement, a conditional statement that executes a block of code if the condition evaluates to true. For example:
if(condition) {
// code
}
Here, the code inside the if block runs if the value of x is greater than 10. It allows programs to make decisions based on conditions. (The else keyword discussed above is used in combination with this to create if-else statements, a variation of conditional statements.)
17. int keyword in C: It is the reserved word for declaring/ defining elements that store integer values/ whole numbers. This is one of the most commonly used data types in C. For example:
int myInt = 42;
Here, we have used the int keyword before the variable name myInt to indicate that it will store integer values, 42 in this case. This type typically occupies 2 or 4 bytes, depending on the system architecture.
18. long keyword in C: It is used to declare a long integer variable, a type of integer that can hold larger values than a regular int. For example:
long myLong = 1000000L;
In this statement, we declare a long integer that can hold larger numeric values, which is useful when the standard int is not sufficient.
19. register keyword in C: The keyword suggests that a variable should be stored in the CPU's register for faster access. However, it's up to the compiler whether or not to use a register. For example:
register int counter = 0;
Here, the counter variable is suggested to be kept in a CPU register for quicker access, which is especially useful in loops.
20. return keyword in C: It is used to write return statements, which are used inside functions to define what value the function will return to the calling code. For example:
int sum(int a, int b) {
return a + b;
}
Here, we have defined a function that takes two integer parameters and calculates their sum using the addition arithmetic operator. The return statement in the function indicates that it will send the result of a + b back to wherever the sum() function was called.
21. short keyword in C: It defines a variable with a smaller range of values compared to integer data type. This is useful when you know the variable won’t need to hold large numbers. For example:
short myShort = 32767;
Here, we have declared a short integer, typically occupying 2 bytes and having a maximum value of 32,767 (for signed short).
22. signed keyword in C: It is a data type modifier that indicates that a variable can hold both positive and negative numbers. By default, integers are signed. For example:
signed int temp = -20;
Here, the temp variable can hold negative values because it’s declared as signed. Without the signed keyword, this is implied for int, but it can be specified explicitly if needed.
23. sizeof keyword in C: It represents the sizeof() operator that returns the size (in bytes) of a data type or variable. It's useful when you need to know how much memory a type occupies. For example:
int size = sizeof(int);
Here, the sizeof() operator will return the size of the int variable.
24. static keyword in C: This keyword can be used in two contexts, i.e., for creating two types of static variables.
- Local variables: A static variable inside a function retains its value between function calls.
- Global variables: A static global variable or function limits its scope to the file where it is declared.
For example:
void counter() {
static int count = 0;
count++;
printf("%d", count);
}
In the snippet above, we defined a function counter() containing a static variable count. The function increments the counter variable's value by 1 and prints it to the console. Each time counter() is called, the count variable retains its value from the previous call.
25. struct keyword in C: A structure is a type of data structure that allows you to group data of multiple types under a single name. The struct keyword is used to define a structure in C programs. For example:
struct Person {
char name[50];
int age;
};
Here, we use the struct keyword to create a structure called Person. It represents a Person with both a name (an array of characters) and an age (an integer). You can think of it as a custom data type combining multiple fields.
26. switch keyword in C: It is used to create switch statements that enable multi-way branching, allowing you to execute different blocks of code based on the value of a variable. It’s an alternative to a chain of if statements. For Example:
switch(day) {
case 1:
// code for Monday
break;
case 2:
// code for Tuesday
break;
default:
// default code
}
Here, the switch keyword marks the beginning of the statement, which checks the value of day and executes the corresponding case. The default block runs if none of the cases match.
27. typedef keyword in C: It allows us to create an alias for an existing data type. This is particularly useful for simplifying complex type declarations. For example:
typedef unsigned int uint;
Typing unsigned int every time you declare an integer variable, which can only store positive values, might be tedious. In the statement above, we used the typedef keyword to define an alias for the unsigned int type, i.e., uint, that you can use to declare variables.
28. union keyword in C: It is used to define a union, another type of data structure similar to structure. However, in the case of a union, all the members share the same memory location. This means only one member can hold a value at a time, which is useful for memory optimization. For example:
union Data {
int intValue;
float floatValue;
};
Here, the union can hold an int and a float, but only one of them can store a value at any given time.
29. unsigned keyword in C: A data type modifier that specifies that the respective variable can only hold positive values. This increases the range of possible values by not using a sign bit. For example:
unsigned int positiveNum = 50000;
Here, we have created a variable positiveNum, which can hold a larger positive value than a signed integer but cannot represent negative values.
30. void keyword in C: It can be used either with a function definition or with pointers variables.
- Functions: Used to define a function returning void, i.e., doesn’t return any value.
- Pointers: Used to define a void pointer, which is a generic pointer that can point to any data type.
For example:
void printMessage() {
printf("Hello, World!");
}
Here, as indicated by the void keyword, the function does not return a value to the calling code. Instead, it prints a simple "Hello, World!" message to the console.
31. volatile keyword in C: It tells the compiler that a variable may change at any time, preventing the compiler from optimizing its value. This is often used in embedded systems or when working with hardware. For example:
volatile int status;
This tells the compiler not to assume that status will remain constant between accesses, as it may be changed by external processes.
32. while keyword in C: It is used to create a while loop, which repeatedly executes a block of code as long as a specified condition is true. For example:
while(condition) {
// code
}
This loop keeps running as long as the condition is true. If the condition is initially false, the loop doesn't execute. (The keyword is also used in do-while loops.)
Want to become a C expert, but don't know how? Check out this amazing course.
Application Of Keywords In C
To fully harness the power of keywords in C, it is essential to understand their various applications and contexts. Let's take a closer look at some of the common ways keywords are used in C programs.
Control Structure Keywords In C
Keywords like if, else, switch, case, default, while, and do while are instrumental in defining control structures that govern the flow of execution in a C program. These structures/ decision control statements allow you to make decisions, loop through code blocks, and execute specific segments based on conditions. Look at the example C code below to see how these keywords are used.
Code Example:
Output:
The number is positive.
Good job!
While loop iteration: 0
While loop iteration: 1
While loop iteration: 2
Do-while loop iteration: 0
Do-while loop iteration: 1
Do-while loop iteration: 2
Explanation:
In the example C program above, we begin by including the essential header file for input/ output operations, i.e., <stdio.h>.
- Inside the main() function, we then initialize an integer variable num to 10, representing a numeric value.
- Then, as mentioned in the code comments, we create an if-else statement using the if and else keywords to check if the value of num is greater than 0.
- If the condition num>0 is true, the if-block executes, printing the string message- "The number is positive" to the console.
- If the condition num>0 is false, the else block executes, printing the message "The number is negative."
- Next, we initialize the character variable grade to B, representing a student's grade.
- Then, we create a switch-case statement that compares the grade variable to various cases and prints a message based on that.
- Here, we show the use of the switch, case, and break keywords.
- The switch takes the grade's value and compares it to the three cases.
- If any of the cases matches, the respective code block is executed, and the switch terminates after encountering the break statement.
- If none of the cases matches, we execute the default case and print- Needs improvement to the console using the printf() statement.
- In the next step, we initialize a counter variable i to 0 and initiate a while loop that prints the current iteration number and increments i until i is no longer less than 3. Here, the while keyword indicates the beginning of the loop.
- Similarly, we initialize another counter variable j to 0 and execute a do-while loop that prints the current iteration number and increments j while j is less than 3. Here, the do and while keywords define the loop.
- After successfully executing the code, the main function returns 0. This is the return statement for the main function.
Data Type Keywords In C
Keywords such as int, char, float, double, short, long, unsigned, and signed are used to declare variables with specific data types. These keywords provide a way to define variables that can store varied data types, such as integers, characters, or floating-point numbers. Also, they determine how memory is allocated and interpreted for each variable. The simple C code example below illustrates the same.
Code Example:
Output:
Integer: 10
Char: A
Float: 3.140000
Double: 2.718280
Short: 32767
Long: 1234567890
Unsigned: 42
Signed: -123
Explanation:
We start the simple C program example by including the standard input-output library and initiate the main() function, the program's entry point for execution.
- Inside main(), we then declare and initialize variables of different data types, such as integers, characters, floats, doubles, shorts, longs, unsigned, and signed integers.
- An integer variable named integerVariable is declared and set to the value 10.
- A character variable named charVariable is declared and set to the character A.
- A float variable named floatVariable is declared and initialized with the value 3.14.
- A double variable named doubleVariable is declared and initialized with the value 2.71828.
- A short integer variable named shortVariable is declared and initialized with the value 32767.
- A long integer variable named longVariable is declared and initialized with the value 1234567890L.
- An unsigned integer variable named unsignedVariable is declared and initialized with the value 42u.
- A signed integer variable named signedVariable is declared and initialized with the value -123.
- We then use a series of printf() statements to display the values of these variables to the console.
- Here, we use format specifiers (%d, %c, %f, %lf, %hd, %ld, %u, %d) to print the corresponding variable value correctly. We also use the newline escape sequence (\n), which shifts the cursor to the next line before printing the next output.
Function Declaration Keywords In C
Common keywords like void, return, and extern are used in function declarations. The keyword void is used to indicate that a function does not return type a value, while return is used to specify the value returned by a function. The extern variable keyword is used to declare a function body that is defined in another file. Below is a sample C program to showcase the use of the extern and void keywords.
Code Example:
Output:
Global Variable: 42
Inside displayGlobalVariable Function: 42
Explanation:
In the sample C code,
- We first use the extern keyword to declare an external variable named globalVariable, indicating to the compiler that the variable is defined elsewhere in the program.
- Next, we define the displayGlobalVariable function with a prototype, giving the compiler information about the function's signature before its actual definition later in the code. We also use the void keyword to indicate that there is no return type for this prototype.
- Then, inside the main() function, we use a printf() statement to display the value of the globalVariable.
- Since it's an external variable, its definition should ideally be in another file, which we should compile with this program. But for this example, we have given the latter in the code itself.
- We then call the displayGlobalVariable() function, which prints the value of the globalVariable variable with a descriptive message.
- After the main, we define and initialize the external variable globalVariable with the value 42. This action allocates space for the single variable in memory.
- We define the displayGlobalVariable function, which prints the value of the global variable.
Hone your coding skills with the 100-Day Coding Sprint at Unstop and top the leaderboard!
Storage Classe Keywords In C
Keywords such as auto, register, static, and extern are used to define the storage class of variables and functions. These keywords determine where and how memory is allocated for variables and how functions are accessed and linked. Below is a C code example to illustrate the use of these keywords.
Code Example:
Output:
Auto variable: 10
Register variable: 20
Static variable: 30
Explanation:
In the C program example,
- Inside the main() function, we declare and initialize an automatic variable named autoVariable with the value 10.
- It's important to note that the auto keyword is rarely used in modern C, as variables are considered automatic by default.
- Next, we declare a register variable named registerVariable and initialize it with the value 20. The register keyword suggests a preference to store the variable in a register for faster access, but modern compilers may ignore this suggestion.
- We then declare a static variable named staticVariable and initialize it with the value 30. The static keyword ensures that the variable retains its value between function calls.
- Lastly, we use a set of printf() statements to display the values of the three variables (autoVariable, registerVariable, and staticVariable).
User-Defined Type Keywords In C
Keywords like typedef, struct, enum, and union define user-defined data types. They provide a way to create custom data types, such as structures and enumerations, that can encapsulate multiple values and enhance code organization and readability. The C program sample below illustrates the use of these keywords.
Code Example:
Output:
Variable: 42
Person: Raman, Age: 25
Gender: 0
Data (int): 10
Explanation:
In the C code sample,
- As mentioned in the code comments, we use the typedef keyword to define a custom data type uint, creating an alias for unsigned int type.
- Then, we define a structure called Person with data members- name and age. The structure holds information about an individual.
- We then define an enumeration named Gender with the value MALE. This provides symbolic names for integer constants representing different genders.
- Next, we define a union named Data, capable of holding either an integer (intValue) or a floating-point number (floatValue). This allows different types to share the same memory space.
- Inside the main() function, we declare an unsigned integer variable var1, using the alias for this data type (uint), and assign it the value 42.
- We then create an instance of the Person structure named person1 and initialize its data members: name to Raman and age to 25.
- Next, we use the enumeration to declare a variable gender1 and assign it the value MALE.
- After that, we create an instance of the union, data1 and use the dot operator to access and initialize its integer value member with value 10.
- Then, we use a set of printf() statements to display the values of the variables (variable1, person1.name, person1.age, gender1, and data1.intValue).
- Finally, the program completes execution with the return 0 statement.
Difference Between Identifiers And Keywords In C
The table below highlights the primary differences between keywords and identifiers in C.
Basis | Keywords | Identifiers |
Definition | Keywords are predefined and reserved by the C language, and their meanings cannot be altered. | Identifiers are names created by the programmer for variables, functions, arrays, etc., and can be customized. |
Use | A keyword can only be used for its intended purpose, as defined by the C language. | Identifiers can be used to represent variables, functions, and other entities as needed. |
Beginning | Keywords must start with a letter and cannot contain underscores or special characters. | Identifiers can begin with a letter or an underscore (_), but not a number. |
Declaration | Keywords are fixed and cannot be declared by the programmer. | Identifiers can be declared multiple times with different values, depending on the program's needs. |
Representation | Keywords have a fixed representation and meaning throughout a program's execution. | The values or purposes represented by identifiers may change during program execution. |
Semicolon | No semicolon (;) is required after using a keyword. | A semicolon (;) is usually required at the end of a statement involving identifiers. |
Looking for guidance? Find the perfect mentor from experienced coding & software professionals here.
Conclusion
Keywords in C refer to words reserved as a part of the language’s syntax, and understanding them is essential for writing effective C programs. These reserved words have specific purposes and cannot be used for other purposes, like naming variables or functions.
Each keyword represents a predefined action or function that the programmer cannot alter. By mastering the essential C keywords, you gain a solid foundation in programming, which is invaluable whether you're developing simple applications or working on complex software solutions.
Also read- 100+ Top C Interview Questions With Answers (2023)
Frequently Asked Questions
Q. What is the main keyword in C language?
In C, the term main keyword refers to the main function, which serves as the entry point for program execution. Every C program must have a main function, as this is where the execution begins. The main function's first statement marks the starting point of the program. Below is a simple example of a C program with the main function.
Code Example:
Output:
Hello, World!
Explanation:
In this example, int main() indicates the declaration of the main function, and the program starts executing from the opening curly brace { following main. The printf() statement prints "Hello, World!" to the console, and the return 0 statement indicates a successful execution to the operating system.
Q. What are keywords also called?
In C, keywords are also commonly referred to as reserved words, which indicates that these words have special meanings in the C language and cannot be used as identifiers (such as variable names or function names). These reserved words/ pre-defined words are integral to the syntax of the C programming language, playing a crucial role in defining the structure and behavior of programs.
Q. What is a loop in C?
A loop in C is a control structure that repeats a set of instructions until a specified condition is met. Loops are used to automate repetitive tasks in a program. The main types of loops in C are:
- While loop
- For loop
- Do-while loop
- Nested loops
- Break and continue statements for controlling loop flow
Loops are crucial for controlling program execution and simplifying repetitive tasks.
Q. What do you understand by the term data type in C?
In C, a data type defines the type of data a variable can store. The primary data types in C include:
- int: Stores integer values (e.g., 32-bit integers).
- float: Used for single-precision floating-point numbers.
- char: Used to store individual characters.
- void: Specifies that a function does not return a value or that a pointer does not point to any specific data type.
There are also more specialized data types like enum for creating named integer constants.
Q. What is a variable in C?
A variable in C is a symbolic name (identifier) that references a memory location where data is stored. The value of a variable can change throughout the program’s execution. They are used to store data such as user input, intermediate results from calculations, or any other values needed for the program's operation.
By now, you must know everything about the various keywords in C. For more such informative reads, explore:
- Comma Operator In C | Code Examples For Both Separator & Operator
- 5 Types Of Literals In C & More Explained With Examples!
- Logical Operators In C Explained (Types With Examples)
- Recursion In C | Components, Working, Types & More (+Examples)
- Type Casting In C | Cast Functions, Types & More (+Code Examples)
- Ternary (Conditional) Operator In C Explained With Code Examples
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Comments
Add comment