Computer >> Computer tutorials >  >> Programming >> C programming

Tokens in C


Tokens are the smallest elements of a program, which are meaningful to the compiler.

The following are the types of tokens: Keywords, Identifiers, Constant, Strings, Operators, etc.

Let us begin with Keywords.

Keywords

Keywords are predefined, reserved words in C and each of which is associated with specific features. These words help us to use the functionality of C language. They have special meaning to the compilers.

There are total 32 keywords in C.

autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
continueforsignedvoid
doifstaticwhile
defaultgotosizeofvolatile
constfloatshortunsigned

Identifiers

Each program element in C programming is known as an identifier. They are used for naming of variables, functions, array etc. These are user-defined names which consist of alphabets, number, underscore ‘_’. Identifier’s name should not be same or same as keywords. Keywords are not used as identifiers.

Rules for naming C identifiers −

  • It must begin with alphabets or underscore.

  • Only alphabets, numbers, underscore can be used, no other special characters, punctuations are allowed.

  • It must not contain white-space.

  • It should not be a keyword.

  • It should be up to 31 characters long.

Strings

A string is an array of characters ended with a null character(\0). This null character indicates that string has ended. Strings are always enclosed with double quotes(“ “).

Let us see how to declare String in C language −

  • char string[20] = {‘s’,’t’,’u’,’d’,’y’, ‘\0’};
  • char string[20] = “demo”;
  • char string [] = “demo”;

Here is an example of tokens in C language,

Example

#include >stdio.h>
int main() {
   // using keyword char
   char a1 = 'H';
   int b = 8;
   float d = 5.6;
   // declaration of string
   char string[200] = "demodotcom";
   if(b<10)
   printf("Character Value : %c\n",a1);
   else
   printf("Float value : %f\n",d);
   printf("String Value : %s\n", string);
   return 0;
}

Output

Character Value : H
String Value : demodotcom