C Tokens Types And Examples

In the C programming language, tokens can be categorized as follows:

  • Keywords in C
  • Identifiers in C
  • Strings in C
  • Operators in C
  • Constant in C
  • Special Characters in C

Let's understand each token one by one.

Keywords in C

Keywords in C are essentially predefined or reserved terms with specific significance, each serving a distinct purpose. These keywords, being predefined by the compiler, cannot be repurposed as variable names. Doing so would essentially alter the intended meaning of the keyword, violating language rules. C language encompasses a total of 32 keywords as listed below:

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

C Keywords Example

Let's consider an example to demonstrate the keyword in C programming.

Example

Example

#include <stdio.h>

int main() {

// Created the variable x having keyword as int and holding integer value

 int x = 10;     

    

    printf("The value of the variable is: %d", x );

    return 0; 

}

Output:

Output

The value of the variable is: 10

Explanation:

In this instance, we have declared a variable denoted as x that stores an integer value, specifically 10. The int keyword is employed to store integer values, which are exclusively for integers. Hence, we utilized the 'int' keyword for this purpose and displayed the value of x via the printf function. Likewise, appropriate keywords can be applied to the variable to accommodate decimal values and characters.

Identifiers in C

Identifiers in C are used for naming variables, functions, arrays, structures, etc. Identifiers in C are the user-defined words. It can be composed of uppercase letters, lowercase letters, underscore, or digits, but the starting letter should be either an underscore or an alphabet. Identifiers cannot be used as keywords. Rules for constructing identifiers in C are given below:

  • The first character of an identifier should be either an alphabet or an underscore, and then it can be followed by any of the character, digit, or underscore.
  • It should not begin with any numerical digit.
  • In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that identifiers are case sensitive.
  • Commas or blank spaces cannot be specified within an identifier.
  • Keywords cannot be represented as an identifier.
  • The length of the identifiers should not be more than 31 characters.
  • Identifiers should be written in such a way that it is meaningful, short, and easy to read.
  • C Identifiers Example

Let's consider an example to demonstrate the concept of Identifiers in the C programming language.

Example

Example

#include <stdio.h>

int main( ) {   //main function

//using two variables, 'a' and 'b' to hold two integer values

int a = 10;

int b = 20;

//using an integer variable 'sum' to hold the sum of 'a' and 'b'

int sum = a + b;

printf("After addition, the sum of the values is: %d ", sum );

return 0;    

}

Output:

Output

After addition, the sum of the values is: 30

Explanation:

In this instance, we've declared three variables. The initial two variables, 'a' and 'b', are assigned integer values, while the third variable, 'sum', will store the resulting sum, which must also be an integer. To add the two numbers together, we've designated 'a' and 'b' as the operands, commonly referred to as identifiers. The variable 'sum' then captures the sum of 'a' and 'b'.

Strings in C

Strings in C are consistently depicted as an array of characters with a terminating null character '\0' at the string's conclusion. This null character signifies the conclusion of the string. In C programming, strings are encapsulated within double quotation marks, whereas individual characters are encapsulated within single quotation marks. The dimension of a string corresponds to the quantity of characters present within the string.

Now, we describe the strings in different ways:

Example

char a[10] = "logic practice"; // The compiler allocates the 10 bytes to the 'a' array.

char a[] = "logic practice"; // The compiler allocates the memory at the run time.

char a[10] = {'t','p','o','i','n','t','t','e','c','h','\0'}; // String is represented in the form of characters.

C String Example

Let's consider an example to demonstrate string manipulation in the C programming language.

Example

Example

#include <stdio.h>

#include <string.h>

int main( ) {    //main function

// Initialized a string using the char[] array

char s[ 50 ]="LOGIC PRACTICETECH IS THE BEST TUTORIAL WEBSITE";

int len;

printf( "%s \n ",s );

len=strlen( s ); // Predefined string method to calculate the length

printf( "\nThe length of the string is: %d ", len );

return 0;    

}

Output:

Output

LOGIC PRACTICETECH IS THE BEST TUTORIAL WEBSITE  

The length of the string is: 41

Explanation:

In this instance, a character array of 50 units was initialized to store a string value. Subsequently, the string was displayed using '%s' format specifier. Following this, the length of the string was determined using the built-in function strlen and the resulting value was printed.

Operators in C

Operators within the C programming language are unique symbols utilized to carry out specific operations. The values that these operators work on are referred to as operands. These operators are utilized in between operands to execute specific actions. The classification of operators is based on the number of operands they work with, and they are categorized as follows:

Unary Operator

A unary operator is an operator that acts on only one operand. For instance, the increment operator (++), decrement operator (--), sizeof, and (type)* are examples of unary operators.

Binary Operator

The binary operator is an operator applied between two operands. The following is the list of the binary operators:

  • Arithmetic Operators
  • Relational Operators
  • Shift Operators
  • Logical Operators
  • Bitwise Operators
  • Conditional Operators
  • Assignment Operator
  • Misc Operator
  • C Operators Example

Let's consider an example to demonstrate the operators in the C programming language.

Example

Example

# include <stdio.h>

# include <stdbool.h>

int main( ) {   //main function

// Created integer variables 'a' and 'b'

  int a = 24;

  int b = 8; 

 // using int c variable to sum value and used the arithmetic operator to perform addition

  int c = a + b;  

  printf("The sum of given values a and b is: %d \n ", c );

  // Calculating the subtraction value using the arithmetic operator

  c = a - b;

// Using asterisk (*) operator for performing multiplication

 printf("\nThe difference of given values a and b is: %d \n ", c );  

  c = a * b;    

  printf("\nThe product of given values a and b is: %d \n ", c );    

  // using forward slash (/) operator to perform division 

  c = a / b; 

  printf("\nThe division of given values a and b is: %d \n ", c );

  // Using double equals (==) operator to check if a and b are equal

  bool d = (a == b);    // 0 means true, 1 means false

printf("\nThe value of a and b are %d \n ", d );    

  // Using (!=) operator to check if values of a and b are not equal   

  d = (a != b);   

  printf("\nThe value of a and b are equal ? %d \n ", d );    // 0 means true, 1 means false

 

  return 0;      

}

Output:

Output

The sum of given values a and b is: 32 

The difference of given values a and b is: 16 

The product of given values a and b is: 192 

The divison of given values a and b is: 3 

The value of a and b are 0 

The value of a and b are equal ? 1

Explanation:

In this instance, we showcase the application of fundamental arithmetic and relational operators in the C programming language. The program executes addition, subtraction, multiplication, and division operations on two integers (a = 24, b = 8) and displays the outcomes using the printf function. Additionally, it verifies the equality (==) and inequality (!=) of the two values through relational operators and outputs the respective boolean outcomes (0 for false, 1 for true).

Constants in C

In the C programming language, a constant is a fixed value assigned to a variable that retains its value unchanged throughout the execution of the program. This means that the value of a constant cannot be altered during the program's runtime.

There are two methods for defining constants:

  • Utilizing the const keyword
  • Employing the #define pre-processor
  • Types of constants in C

Constant Example
Integer constant 10, 11, 34, etc.
Floating-point constant 45.6, 67.8, 11.2, etc.
Octal constant 011, 088, 022, etc.
Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.
Character constant 'a', 'b', 'c', etc.
String constant "java", "c++", ".net", etc.

C Constants Example

Let's consider an example to demonstrate the constants in C programming.

Example

Example

# include <stdio.h>

int main() {  //main function

  	//Initializing constant variable

  	const int x = 5;

 	printf("The output is : %d", x );

    return 0;

}

Output:

Output

The output is : 5

Explanation:

In this instance, we showcase the implementation of an unchanging variable utilizing the const keyword. The constant x is set to 5 and displayed using printf, guaranteeing its immutability throughout the program.

Special characters in C

Some special characters are used in C, and they have a special meaning which cannot be used for another purpose.

  • Square brackets : The opening and closing brackets represent the single and multidimensional subscripts.
  • Simple brackets : It is used in function declaration and function calling. For example, printf is a pre-defined function.
  • Curly braces { }: It is used in the opening and closing of the code. It is used in the opening and closing of the loops.
  • Comma (,): It is used for separating for more than one statement and for example, separating function parameters in a function call, separating the variable when printing the value of more than one variable using a single printf statement.
  • Hash/pre-processor (#): It is used for pre-processor directives. It basically denotes that we are using the header file.
  • Asterisk (*): This symbol is used to represenLogic Practiceers and also used as an operator for multiplication.
  • Tilde (~): It is used as a destructor to free memory.
  • Period (.): It is used to access a member of a structure or a union.
  • C Special Characters Example

Let's consider an example to demonstrate special characters in the C programming language.

Example

Example

#include <stdio.h>

int main() {  //main function

 	

  	char ch[ ] = "Logic Practice is the best tutorial site";

 	printf("The output is: %s", ch);

    return 0;

}

Output:

Output

The output is: Logic Practice is the best tutorial site

Explanation:

In this instance, we start by declaring a character array named ch and assign it a string value. Subsequently, we display the contents using the %s format specifier within the printf function.

Input Required

This code uses input(). Please provide values below: