C Tokens

Tokens are the smallest element, which are meaningful to a compiler. There are six types of tokens in C programming, namely Keywords, Identifiers, Constants, Strings, Special Symbols and Operators.

Keywords

Keywords are reserved words in C, where each keyword has a specific purpose. These words cannot be assigned with new meaning. In C, there are total 32 keywords.

auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned

Identifiers

Identifiers as the name suggests, identifies a program element such as variables, functions or arrays. Once declared, identifiers can be reused later in the program.

There are certain rules that should be followed while naming an identifier.

  • It must begin with an alphabet or underscore.
  • Only alphabets, numbers and underscores are allowed.
  • It should not be a keyword.
  • It should not contain white spaces.
  • It should not be more than 31 characters long.

The following code shows examples of valid and invalid identifiers.

#include<stdio.h>
int main(void)
{ 
// Valid Identifiers
    int _var1;
    char my_var;
    int arr[100];

// Invalid Identifiers
    int auto;
    char 1st_day;
    double #coder;
    
    return 0;
}

If you try to run a program containing invalid identifiers, then the compiler may throw an error or the program may behave abnormally. In the above example valid identifiers were _var1, my_var, arr (variable name) and main (method name).

Constants

In C, constants can be a number, character or string, however, they cannot be modified. They are used in places where a fixed value is required such as mathematical constants like π.


Types of C Constants

  • Integer Constant: 100, -3, 0
  • Floating-point Constant: 3.14, 5.3323
  • Character Constant: 'A', 'c', '7'
  • Enumeration Constant: set of named integer constants - Dice{1,2,3,4,5,6}

There are two ways to create a constant in C, using the #define preprocessor directive or using the const keyword.

The following code shows various ways of creating constants in C.

#include<stdio.h>

// Constant using #define
#define PI 3.1416

// Enumeration Constant
enum week{
Monday, 
Tuesday, 
Wednesday, 
Thursday, 
Friday, 
Saturday, 
Sunday
}day;

int main(void)
{ 
// Integer Constants
    const int MAX_DEGREE = 360;
    
// Floating-point Constant
    const float e = 2.718;
    
// Character Constant
    const char YES_RESPONSE = 'y';
    
// String Constants (character constant array)
    static const char GREET[] = "Hello";

    return 0;
}

Strings

C allows creation of strings using array of characters. Each string terminates with a null character '\0' which denotes end of the string. C strings are enclosed within double quotes "" whereas a character is enclosed within single quotes ''. Example,

#include<stdio.h>
int main(void)
{ 
// Null character has to be included
    char arr[5] = {'c','o','o','l','\0'};
    
// Null character is automatically inserted
    char arr2[5] = "cool";
    
// Memory size allocated at run time
    char arr3[] = "cool";
    
    return 0;
}

Note, Strings which are not initialized and has a local scope will contain garbage values.

Special Symbols

C special symbol has a defined meaning and cannot be changed. The special symbols are listed below.

  • Square Brackets [] : Used for array subscripts.
  • Parenthesis () : Special symbol denoting function calls and function parameter.
  • Braces {} : Denotes block of code
  • Semi-colon ; : Denotes end of the line
  • Comma , : Separates two or more statements.
  • Pre processor # : It is a macro processor which is processed before compilation of the program.
  • Assignment = : It is used to assign values.
  • Asterisk * : Used to create pointers.

Trending

C Programming

Remember to follow community guidelines and don't go off topic.