C - Keywords

Keywords are those predefined words that have special meaning in the compiler and they cannot be used for any other purpose. As per the C99 standard, C language has 32 keywords. Keywords cannot be used as identifiers.

The following table has the list of all keywords (reserved words) available in the C language:

autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
continueforsignedvoid
doifstaticwhile
defaultgotosizeofvolatile
constfloatshortunsigned

All the keywords in C have lowercase alphabets, although the keywords that have been newly added in C, do have uppercase alphabets in them. C is a case-sensitive language. Hence, int is a keyword but INT, or Int are not recognized as a keyword. The new keywords introduced from C99 onwards start with an underscore character. The compiler checks the source code for the correctness of the syntax of all the keywords and then translates it into the machine code.

Example of C Keywords

In the following program, we are using a keyword as an identifier i.e., as the name of the user-defined function, that will cause a compilation error.

#include <stdio.h>

void register(int, int);
int main () {

   /* variable definition: */
   int a=5, b=7;
   register(a,b);

   return 0;
}
void register(int a, int b)
{
   printf("%d", a+b);
}

Errors

main.c:3:15: error: expected identifier or '(' before 'int'
    3 | void register(int, int);
      |               ^~~
main.c: In function 'main':
main.c:8:14: error: expected ')' before ',' token
    8 |    register(a,b);
      |              ^
      |              )
main.c: At top level:
main.c:12:15: error: expected identifier or '(' before 'int'
   12 | void register(int a, int b)
      |               ^

The reason for the errors is that we are using a keyword register as the name of a user-defined function, which is not allowed.

The ANSI C version has 32 keywords. These keywords are the basic element of the program logic. These keywords can be broadly classified in following types −

  • Primary Data types
  • User defined types
  • Storage types
  • Conditionals
  • Loops and loop controls
  • Others

Let us discuss the keywords in each category.

Primary Types C Keywords

These keywords are used for variable declaration. C is a statically type language, the variable to be used must be declared. Variables in C are declared with the following keywords:

intDeclares an integer variable
longDeclares a long integer variable
shortDeclares a short integer variable
signedDeclares a signed variable
doubleDeclares a double-precision variable
charDeclares a character variable
floatDeclares a floating-point variable
unsignedDeclares an unsigned variable
voidSpecifies a void return type

User-defined Types C Keywords

C language allows you to define new data types as per requirement. The user defined type has one or more elements of primary type.

The following keywords are provided for user defined data types −

structDeclares a structure type
typedefCreates a new data type
unionDeclares a union type
enumDeclares an enumeration type

Storage Types C Keywords

The following set of keywords are called storage specifiers. They indicate the location where in the memory the variables stored. Default storage type of a variable is auto, although you can ask the compiler to form a variable with specific storage properties.

autoSpecifies automatic storage class
externDeclares a variable or function
staticSpecifies static storage class
registerSpecifies register storage class

Conditionals C Keywords

The following set of keywords help you to put conditional logic in the program. The conditional logic expressed with if and else keywords provides two alternative actions for a condition. For multi-way branching, use switch – case construct. In C, the jump operation in an assembler is implemented by the goto keyword.

gotoJumps to a labeled statement
ifStarts an if statement
elseExecutes when the if condition is false
caseLabels a statement within a switch
switchStarts a switch statement
defaultSpecifies default statement in switch

Loops and Loop Control C Keywords

Repetition or iteration is an essential aspect of the algorithm. C provides different alternatives for forming a loop, and keywords for controlling the behavior of the loop. Each of the keywords let you form a loop of different characteristics and usage.

ForStarts a for-loop
doStarts a do-while loop
whilestarts a while loop
continueSkips an iteration of a loop
breakTerminates a loop or switch statement

Other C Keywords

The following miscellaneous keywords are also extremely important:

constSpecifies a constant value
SizeofDetermines the size of a data type
Volatilecompiler that the value of the variable may change at any time

In C99 version, five more keywords were added −

  • _Bool
  • _Complex
  • _Imaginary
  • inline

In C11, seven more keywords have been added

  • _Alignas
  • _Alignof
  • _Atomic
  • _Generic
  • _Noreturn
  • _Static_assert

When the C23 standard will be released it will introduce 14 more keywords −

  • alignas
  • alignof
  • bool
  • constexpr
  • false
  • nullptr
  • static_assert
  • thread_local
  • true
  • typeof
  • typeof_unqual
  • _Decimal128

Most of the recently reserved words begin with an underscore followed by a capital letter, Since existing program source code should not have been using these identifiers.

Following points must be kept in mind when using the keywords:

  • Keywords are reserved by the programming language and have predefined meaning. They cannot be used as name of a variable or function.
  • Each keyword has to be used as per the syntax stipulated for its use. If the syntax is violated, the compiler reports compilation errors.
  • C is one of the smallest computer languages with only 32 keywords in its ANSI C version, although a few more keywords have been added afterwards.
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team