Keywords in C

In the C programming language, especially keywords are reserved words that have a specific meaning and usage in the language. These keywords are neither used as variable names, function names, nor any other user-defined names in the program. Overall there are 32 keywords in C language.

What are the keywords in C language?

Here are the details of all special keywords used in C language with examples:

autoexternsizeof
breakfloatstatic
caseforstruct
chargotoswitch
constiftypedef
continueintunion
defaultlongunsigned
doregistervoid
doublereturnvolatile
elseshortwhile
enumsigned

auto:

The auto keyword is used to declare a variable with a local scope, For example:

auto int a = 10;

break:

The break keyword is used to terminate a loop or switch statement, For example:

for (int i = 0; i < 10; i++)
{
  if (i == 5)
  {
    break;
  }

case:

The case keyword is used to define a case in a switch statement, For example:

switch (a)
{
  case 1:
    printf("The value is 1.");
    break;
  case 2:
    printf("The value is 2.");
    break;
  default:
    printf("The value is neither 1 nor 2.");
}

char:

The char keyword is used to declare a character variable, For example:

char ch = 'A';

const:

The const keyword is used to declare a constant value that cannot be changed during program execution, For example:

const float PI = 3.14159;
printf("The value of PI is %f", PI);

continue:

The continue keyword is used to skip the current iteration of a loop and move to the next iteration, For example:

for (int i = 0; i < 10; i++)
{
  if (i == 5)
  {
    continue;
  }
  printf("%d ", i);
}

default:

The default keyword is used to define the default case in a switch statement, For example:

switch (a)
{
  case 1:
    printf("The value is 1.");
    break;
  case 2:
    printf("The value is 2.");
    break;
  default:
    printf("The value is neither 1 nor 2.");
}

do:

The do keyword is used to start a do-while loop, For example:

int i = 0;
do
{
  printf("%d ", i);
  i++;
} while (i < 10);

double:

The double keyword is used to declare a double-precision floating-point variable, For example:

double d = 3.14159;

else:

The else keyword is used in an if-else statement to specify the code to be executed when the condition is false, For example:

if (a > b)
{
  printf("a is greater than b");
}
else
{
  printf("b is greater than a");
}

enum:

The enum keyword is used to define an enumeration type, For example:

enum color {RED, GREEN, BLUE};
enum color c = RED;

extern:

The extern keyword is used to declare a variable or function that is defined in another file, For example:

extern int var;
extern void func();

float:

The float keyword is used to declare a single-precision floating-point variable, For example:

float f = 3.14;

for:

The for keyword is used to start a for loop, For example:

for (int i = 0; i < 10; i++)
{
  printf("%d ", i);
}

goto:

The goto keyword is basically used to transfer control to a labeled statement in the program. It is generally discouraged to use the goto statement in modern programming practices due to its potential to make code more difficult to understand and debug.

#include <stdio.h>

int main() {
   int count = 0;
   start: // label
   printf("%d\n", count);
   count++;
   if (count < 10) {
      goto start; // jump to label
   }
   return 0;
}

if:

The if keyword is used to start an if statement, For example:

if (a > b)
{
  printf("a is greater than b");
}

int:

The int keyword is used to declare an integer variable, For example:

int i = 10;

long:

The long keyword is used to declare a long integer variable, For example:

long l = 1000000000;

register:

The register keyword is used to declare a variable that is stored in a register instead of memory, which can result in faster access times. However, the compiler is not required to store the variable in a register, For example:

register int i = 10;

return:

The return keyword is used to return a value from a function, For example:

int add(int a, int b)
{
  return a + b;
}

short:

The short keyword is used to declare a short integer variable, For example:

short s = 1000;

signed:

The signed keyword is used to declare a signed variable, For example:

signed int i = -10;

sizeof:

The sizeof keyword is useful to determine the size of a data type in bytes, For example:

int i;
printf("The size of an integer is %lu bytes.", sizeof(i));

static:

The static keyword is useful to declare a variable that retains its value even after the function call, For example:

void func()
{
  static int i = 0;
  i++;
  printf("%d ", i);
}

struct:

The struct keyword is useful to define a structure type, For example:

struct person
{
  char name[50];
  int age;
};
struct person p;

switch:

The switch keyword is useful to start a switch statement, For example:

switch (a)
{
  case 1:
    printf("The value is 1.");
    break;
  case 2:
    printf("The value is 2.");
    break;
  default:
    printf("The value is neither 1 nor 2.");
}

typedef:

The typedef keyword is useful to create a new name for an existing data type, For example:

typedef unsigned char byte;
byte b = 'A';

union:

The union keyword is useful to define a union type, For example:

union my_union
{
  int i;
  float f;
};
union my_union u;

unsigned:

The unsigned keyword is useful to declare an unsigned variable, For example:

unsigned int i = 10;

void:

The void keyword is useful to indicate that a function does not return any value, For example:

void func()
{
  printf("This is a void function.");
}

volatile:

The volatile keyword is useful to indicate that a variable can be changed unexpectedly by external factors, For example:

volatile int i = 10;

while:

The while keyword is useful to start a while loop, For example:

int i = 0;
while (i < 10)
{
  printf("%d ", i);
  i++;
}

These are 32 keywords in C language that have a predefined meaning but they cannot be used as user-defined names in the program.

Conclusion on Keywords in C

Keywords play an important role in C programming language as they are reserved words that have a specific meaning and also cannot be used for other purposes. They are used to define the syntax and structure of the language, as well as to perform various operations and control structures in a program. Also understanding the usage of these keywords is essential for writing correct and efficient code in C. Although there are only a limited number of keywords in C, their precise use is crucial to developing robust and reliable software. Thus, programmers should be familiar with these keywords and their corresponding functions in order to take full advantage of the capabilities of the C programming language.

Categories C

Leave a Comment