C Programming language-C Tokens

C Tokens

In C program, each word and punctuation are called as Tokens. Basically, tokens are the building blocks for the C code.

The compiler usually breaks the C programs into smaller units and proceeds to the various stages of compilation which are called as Tokens.

The following are the 6 tokens in C programming such as:
  • Identifiers
  • Keywords
  • Constants
  • Strings
  • Operators
  • Special Symbols
Identifiers

C identifiers are used to identify a variable, function, or any other user-defined item. C is a case-sensitive programming language.

An identifier starts with a letter A to Z, a to z or an underscore ‘_’ followed by letters or numbers, digits (0 to 9) and underscores.

Following are some of the examples of identifiers,

Example

Spark_databox               spark                _Databox               spark_123
i                spark12databox               SparkDatabox               _12spark

Note: Identifiers are not valid if it started by number.

Keywords

Keywords are predefined, reserved words in C language and each keyword has specified features.  Keywords have special meaning to the compilers. These keywords cannot be used as identifiers (variables or constants) in the program.

Example

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

Constants

Constants are similar to a variable except that their value never changes during execution of the code.
Constants in C are the fixed values that remains same during the execution of the program. The following are some of the features of Constants,
  •  Constants are also called as literals.
  • Constants always start with upper-case names.
  • Constants can be used along with any type of data types.
The constant keyword defines a constant in C.

Syntax
constant type constant_name;

Example

#include 
main() {
   const int LENGTH = 10;
   const int BREADTH = 5;
   int area = LENGTH * BREADTH;
   printf("Area of Rectangle is %d", area);
   return 0;
}

Output

Area of Rectangle is 50
Note: The const can be put either before or after the data type is possible. The following two lines of code are same.

float const VALUE = 100;
or 
const float VALUE = 100;
Constant Types in C

Constants are characterized into two types namely,
  • Numeric Constants      
  • Integer Constants
  • Real Constants
  • Character Constants
  • Single Character Constants
  • String Constants
  • Backslash Character Constants
Integer Constant

Integer Constant is  the sequence of digits. They are three types namely,
  • Decimal Integer
  • Octal Integer
  • Hexadecimal Integer

Example

Decimal Integer: 25, -40, 0, 1931.90
Octal Integer: 033, 064
Hexadecimal Integer: 0X3

Real Constant

A number contains two parts such as real and floating point constants

Example

99.25
99 – real part
25- fractional part

Single Character Constant

It contains single character enclosed with single quotes (‘ ‘). For example, the character ‘4’ is not same as an integer 4. Character constant has a specific set of integer values corresponding to the characters are known as ASCII values (American Standard Code of Information Interchange).

Example:

‘x’, ‘9’, ’,’ ,‘H’

String Constants

Strings are sequence of characters enclosed with double quotes (“ ”) which may contain numbers, letters, special characters and blank spaces. In C language, “A” and ‘A’ are different. “A” is a string whereas ‘A’ is a character.

Example:

“SparkDatabox”, “2020”, “F”

Backslash character Constant

C supports some character constant with the backslash in front of it. The following are the list of backslash characters has a specific meaning to the compiler. They are also called as “Escape Sequence”.

\a
Beep sound
\b
backspace
\f
Form feed
\r
Carriage return
\t 
Horizontal tab
\v 
Vertical tab
\’
Single quote
\”
Double quote
\\
Backslash
\0
Null

Defining Constants

In following ways, we define C constants:
  • Using #define preprocessor
  • Using const keyword
#define Preprocessor

We define constants like shown below:

#define identifier value

Example:


#include 

#define LENGTH 5
#define WIDTH  3
#define NEWLINE '\n'

int main() {
   int Area;

   Area = LENGTH * WIDTH;
   printf("Area of Rectangle : %d", Area);
   printf("%c", NEWLINE);

   return 0;
}

Output:

Area of Rectangle : 15


The const Keyword

The const prefix is used as prefix for the variables such as,
const  type variable = value;

Example:

#include 

int main() {
   const float  RADIUS = 10;
   const char NEWLINE = '\n';
   float area;

   area = 2 * 3.14 * RADIUS ;
   printf("value of area : %f", area);
   printf("%c", NEWLINE);

   return 0;
}

Output:

value of area : 62.799999

Note: For good programming practices, we always use CAPITALS to define the constants.