Curriculum
In C programming, keywords are predefined reserved words that have specific meanings and purposes within the language. These words are an integral part of C syntax and are used to perform various tasks in a program, such as declaring variables, controlling flow, and defining functions.
In this lesson, we’ll explore essential C keywords, provide examples of their usage, and present them in a table for easy reference.
Here are some common C keywords along with examples of their usage:
Keyword | Description and Usage | Example |
---|---|---|
auto |
Declare automatic variables with limited scope and storage duration | auto int x = 5; |
break |
Terminate loops or exit switch statements prematurely |
while (condition) { break; } |
case |
Define individual cases within switch statements |
switch (value) { case 1: break; } |
char |
Declare character variables for storing single characters | char ch = 'A'; |
const |
Declare constants whose values cannot be changed after initialization | const int PI = 3.14159; |
continue |
Skip the current iteration and proceed to the next one in loops | for (int i = 0; i < 10; i++) { if (i % 2 == 0) continue; } |
default |
Define the default case in switch statements |
switch (value) { default: break; } |
do |
Execute a block of code repeatedly with a do-while loop |
do { /* code */ } while (condition); |
double |
Declare double-precision floating-point variables | double pi = 3.14159265359; |
else |
Specify the code block to execute when an if condition is false |
if (condition) { /* code */ } else { /* alternative code */ } |
enum |
Define enumerated data types to create named integer constants | enum Days { MON, TUE, WED, THU, FRI, SAT, SUN }; |
extern |
Declare variables and functions defined in other source files | extern int count; |
float |
Declare single-precision floating-point variables | float temperature = 98.6; |
for |
Define a for loop for controlled iteration |
for (int i = 0; i < 10; i++) { /* code */ } |
goto |
Transfer control to a labeled statement within the same function | if (condition) goto error; |
if |
Define conditional statements to execute code based on a condition | if (x > 0) { /* code */ } |
int |
Declare integer variables for storing whole numbers | int age = 30; |
long |
Declare long integer variables for larger whole numbers | long population = 8000000; |
return |
Specify the value to be returned from functions | int add(int a, int b) { return a + b; } |
short |
Declare short integer variables | short num = 32767; |
signed |
Declare signed integer variables for both positive and negative numbers | signed int num = -42; |
sizeof |
Determine the size in bytes of data types or objects | int size = sizeof(int); |
static |
Specify internal linkage for variables and functions | static int count = 0; |
struct |
Define user-defined data structures known as structures | struct Point { int x, y; }; |
switch |
Create a switch statement for multi-case selection |
switch (choice) { case 1: /* code */ break; } |
typedef |
Create aliases or synonyms for data types | typedef int Age; |
unsigned |
Declare unsigned integer variables for non-negative whole numbers | unsigned int score = 95; |
void |
Indicate the absence of a specific data type | void myFunction() { /* code */ } |
volatile |
Indicate that a variable’s value may change unexpectedly | volatile int sensorValue; |
while |
Define a while loop for condition-based iteration |
while (condition) { /* code */ } |
C keywords are fundamental to writing C programs effectively. They provide the essential building blocks for creating variables, controlling program flow, and defining functions. By understanding the purpose and usage of these keywords, you can write clear, concise, and correct C code. As you gain experience in C programming, you’ll become more proficient in using these keywords to solve a wide range of programming challenges.