- Reserved: The C language reserves keywords are those keywords that cannot be used as identifiers in programs. Using a keyword as a variable name or other identifier will cause a compilation error .
- Predefined Meaning: Each keyword has a specific meaning that is assigned by the C language. These meanings are built into the C language's grammar and syntax and the compiler interprets them accordingly.
- Specific Use: Keywords are designed for specific purposes and contexts within the C language. They define control structures, data types, flow control, and other language constructs. Attempting to use a keyword outside of its intended purpose will result in a compilation error.
- Standardized: C language keywords are standardized across different compilers and implementations. It ensures the consistency and portability of C programs across different platforms and environments.
A compilation of 32 keywords in the C programming language is presented here:
| 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 |
Here is a brief explanation of each keyword in the C language along with their syntax and an example:
- auto: This keyword declares an automatic variable with a local scope .
Syntax:
It has the following syntax:
auto data_typevariable_name;
Example:
auto int count;
Example
#include <stdio.h>
int main() {
auto int count = 10;
printf("Count: %d\n", count);
{
auto int count = 5;
printf("Inner Count: %d\n", count);
}
printf("Count: %d\n", count);
return 0;
}
Output:
Count: 10
Inner Count: 5
Count: 10
- break statement: This statement is employed to end the execution of a loop or switch statement.
Syntax:
It has the following syntax:
break;
Example
#include <stdio.h>
int main() {
for (inti = 0; i< 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
Output:
0 1 2 3 4
- case: This is employed within a switch statement to specify various scenarios.
Syntax:
It has the following syntax:
case constant_expression:
Example
#include <stdio.h>
int main() {
int choice = 2;
switch (choice) {
case 1:
printf("You chose option 1.\n");
break;
case 2:
printf("You chose option 2.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Output:
You chose option 2.
- char: This term is employed to define a data type specifically for characters.
Syntax:
It has the following syntax:
char variable_name;
Example:
char grade = 'A';
Example
#include <stdio.h>
int main() {
char grade = 'A';
printf("Grade: %c\n", grade);
return 0;
}
Output:
Grade: A
The
- const keyword is employed to define constants that are immutable and cannot be altered.
Syntax:
It has the following syntax:
const data_type constant_name = value;
Example
#include <stdio.h>
int main() {
constint MAX_SIZE = 100;
printf("Max Size: %d\n", MAX_SIZE);
return 0;
}
Output:
Max Size: 100
Syntax:
It has the following syntax:
continue;
Example
#include <stdio.h>
int main() {
for (inti = 0; i< 10; i++) {
if (i == 5) {
continue;
}
printf("%d ", i);
}
return 0;
}
Output:
0 1 2 3 4 6 7 8 9
- default: This is employed within a switch statement as the default option if none of the other cases match.
Syntax:
It has the following syntax:
efault:
Example
#include <stdio.h>
int main() {
int choice = 3;
switch (choice) {
case 1:
printf("You chose option 1.\n");
break;
case 2:
printf("You chose option 2.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Output:
Invalid choice.ax: default:
The
- statement is employed to establish a do-while loop, enabling the repetition of a code block until a specific condition is satisfied.
Syntax:
It has the following syntax:
do {
// code to be executed
} while (condition);
Example
#include <stdio.h>
int main() {
inti = 0;
do {
printf("%d ", i);
i++;
} while (i< 5);
return 0;
}
Output:
0 1 2 3 4
- double: This term is employed to define a data type representing double-precision floating-point numbers.
Syntax:
It has the following syntax:
double variable_name;
Example:
double pi = 3.14159;
Example
#include <stdio.h>
int main() {
double pi = 3.14159;
printf("Pi: %lf\n", pi);
return 0;
}
Output:
Pi: 3.141590
In programming, the keyword "else" is employed within an if statement to indicate the set of instructions to run when the specified condition evaluates to false.
Syntax:
It has the following syntax:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Example
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are not an adult.\n");
}
return 0;
}
Output:
You are an adult.
An
- enum is employed to specify an enumeration, which represents a collection of named values.
Syntax:
It has the following syntax:
enum enum_name {
value1,
value2,
//...
};
Example
#include <stdio.h>
enum Days {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main() {
enum Days today = Tuesday;
printf("Today is day number %d\n", today);
return 0;
}
Output:
Today is day number 1
- extern: This keyword is employed to announce a variable or function that is implemented in a different file or outside the current scope.
Syntax:
It has the following syntax:
extern data_type variable_name;
Example
#include <stdio.h>
extern int global_variable;
int main() {
global_variable = 10;
printf("Global variable: %d\n", global_variable);
return 0;
}
int global_variable;
Output:
Global variable: 10
- float: This term is employed to define a data type representing single-precision floating-point numbers.
Syntax:
It has the following syntax:
float variable_name;
Example:
float weight = 65.5;
Example
#include <stdio.h>
int main() {
float weight = 65.5;
printf("Weight: %.2f\n", weight);
return 0;
}
Output:
Weight: 65.50
- for loop: This statement is employed to generate a loop that iterates over a block of code as long as a particular condition is met.
Syntax:
It has the following syntax:
for (initialization; condition; increment/decrement) {
// code to be executed
}
Example:
for (int i = 0; i< 5; i++) {
printf("%d ", i);
}
#include <stdio.h>
int main() {
for (inti = 0; i< 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:
0 1 2 3 4
- goto: It is employed to direct the flow of execution to a specified label within the current function.
Syntax:
It has the following syntax:
goto label_name;
Example
#include <stdio.h>
int main() {
for (inti = 0; i< 10; i++) {
if (i == 5) {
goto end;
}
printf("%d ", i);
}
end:
printf("Loop ended.\n");
return 0;
}
Output:
0 1 2 3 4
Loop ended.
- if: This keyword is employed to establish an if statement that executes a specific action depending on a condition.
Syntax:
It has the following syntax:
if (condition) {
// code to be executed if the condition is true
}
Example
#include <stdio.h>
int main() {
int x = 5;
if (x > 0) {
printf("x is a positive number.\n");
}
return 0;
}
Output:
x is a positive number.
The keyword
- int is utilized for defining a data type that stores whole numbers without decimal points.
Syntax:
It has the following syntax:
int variable_name;
Example:
int number = 10;
Example
#include <stdio.h>
int main() {
int number = 10;
printf("Number: %d\n", number);
return 0;
}
Output:
Number: 10
- long: This term is employed to define a data type representing a large integer value.
Syntax:
It has the following syntax:
long variable_name;
Example:
long population = 1000000;
Example
#include <stdio.h>
int main() {
long population = 1000000;
printf("Population: %ld\n", population);
return 0;
}
Output:
Population: 1000000
- register: This keyword is employed to define a register variable, indicating to the compiler to store the variable in a register for quicker retrieval.
Syntax:
It has the following syntax:
register data_type variable_name;
Example:
register int x = 5;
Example
#include <stdio.h>
int main() {
register intreg_var = 5;
printf("Register Variable: %d\n", reg_var);
return 0;
}
Output:
Register Variable: 5
Syntax:
It has the following syntax:
return expression;
Example
#include <stdio.h>
int square(intnum) {
return num * num;
}
intmain() {
int result = square(5);
printf("Square: %d\n", result);
return 0;
}
Output:
Square: 25
- short: This term is employed to specify a data type representing a short integer.
Syntax:
It has the following syntax:
short variable_name;
Example
#include <stdio.h>
int main() {
short temperature = -10;
printf("Temperature: %d\n", temperature);
return 0;
}
Output:
Copy code
Temperature: -10
- signed: This keyword is employed to define a data type as signed, allowing it to store values that can be either positive or negative.
Syntax:
It has the following syntax:
signed data_type variable_name;
Example:
signed int balance = -100;
Example
#include <stdio.h>
int main() {
signed int balance = -100;
printf("Balance: %d\n", balance);
return 0;
}
Output:
Balance: -100
- sizeof: This function is employed to calculate the byte size of a specific data type or variable.
Syntax:
It has the following syntax:
sizeof(data_type); or sizeof(variable);
Example
#include <stdio.h>
int main() {
int size = sizeof(int);
printf("Size of int: %d bytes\n", size);
return 0;
}
Output:
Size of int: 4 bytes
- static: This keyword is employed to define a variable or function that maintains its value or scope beyond the block where it was initially declared.
Syntax:
It has the following syntax:
static data_type variable_name;
static return_type function_name(arguments);
Example
#include <stdio.h>
void increment() {
static int count = 0;
count++;
printf("Count: %d\n", count);
}
int main() {
increment();
increment();
return 0;
}
Output:
Count: 1
Count: 2
A
- struct is a fundamental element in programming that allows for the creation of a custom data type known as a structure. This structure can store various variables of diverse data types within it.
Syntax:
It has the following syntax:
struct struct_name {
data_type member1;
data_type member2;
//...
};
Example
#include <stdio.h>
#include <string.h>
struct Person {
char name[20];
int age;
};
int main() {
struct Person student;
strcpy(student.name, "John");
student.age = 20;
printf("Name: %s, Age: %d\n", student.name, student.age);
return 0;
}
Output:
Name: John, Age: 20
- switch: This is employed to establish a switch statement, enabling various execution paths to be taken depending on different cases.
Syntax:
It has the following syntax:
switch (expression) {
case constant1:
// code to be executed if expression matches constant1
break;
case constant2:
// code to be executed if expression matches constant2
break;
//...
default:
// code to be executed if expression does not match any constant
}
Example
#include <stdio.h>
int main() {
int choice = 2;
switch (choice) {
case 1:
printf("You chose option 1.\n");
break;
case 2:
printf("You chose option 2.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Output:
You chose option 2.
- typedef keyword is employed to establish an alternative name (alias) for a preexisting data type.
Syntax:
It has the following syntax:
typedef existing_data_type new_data_type;
Example
#include <stdio.h>
typedef int marks;
int main() {
marks math_marks = 95;
printf("Math Marks: %d\n", math_marks);
return 0;
}
Math Marks: 95
A
- union is employed to declare a custom data type known as a union. This union has the capability to store various data types, although only one member can be stored at any given time.
Example
#include <stdio.h>
union Number {
int integer;
float floating_point;
};
int main() {
union Number num;
num.integer = 10;
printf("Integer: %d\n", num.integer);
return 0;
}
Output:
Integer: 10
- unsigned: This keyword is employed to define a data type that can hold only non-negative values.
Syntax:
It has the following syntax:
unsigned data_type variable_name;
Example
#include <stdio.h>
int main() {
unsigned int count = 100;
printf("Count: %u\n", count);
return 0;
}
Output:
Count: 100
- void: This term is employed to signify the lack of a particular type or to declare functions that do not provide a return value.
Syntax:
It has the following syntax:
For function return type: void function_name(arguments);
As a data type: void variable_name;
Example
#include <stdio.h>
void printMessage() {
printf("Hello, World!\n");
}
int main() {
printMessage();
return 0;
}
Output:
Hello, World!
The
- volatile keyword is employed to define a variable that is susceptible to external modifications and should not be optimized by the compiler.
Syntax:
It has the following syntax:
volatile data_type variable_name;
Example:
volatile int sensor_reading;
// Access and modify the sensor_reading variable in an interrupt service routine
Example
#include <stdio.h>
int main() {
volatile int sensor_reading = 0;
// Simulating sensor reading update
for (inti = 0; i< 10; i++) {
sensor_reading = i;
printf("Sensor Reading: %d\n", sensor_reading);
}
return 0;
}
Output:
Sensor Reading: 0
Sensor Reading: 1
Sensor Reading: 2
Sensor Reading: 3
Sensor Reading: 4
Sensor Reading: 5
Sensor Reading: 6
Sensor Reading: 7
Sensor Reading: 8
Sensor Reading: 9
- while loop is employed to iterate over a block of code as long as a certain condition remains true.
Syntax:
It has the following syntax:
while (condition) {
// code to be executed
}
Example
#include <stdio.h>
int main() {
inti = 0;
while (i< 5) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
Output:
0 1 2 3 4