Tokens in C Programming
Hello Everyone, In this article, we will cover the concept of Tokens in C Programming. In C programming, a token is a sequence of characters that represents a single unit of meaning in the language. Tokens can be divided into six categories: identifiers, keywords, constants, operators, special symbols, and strings. Whether you’re new to C or have some experience, this guide will help you use Tokens in C effectively. Let’s get started.
Classification of Tokens in C
In C programming, tokens can be classified into six categories:
- Identifiers: These are names given to variables, functions, or other user-defined entities in a program. Identifiers consist of a sequence of letters, digits, and underscore characters, and must start with a letter or underscore.
- Keywords: Keywords are reserved words in the C language that have a predefined meaning and cannot be used as identifiers. Examples of keywords include “if”, “else”, “for”, “while”, and “return”.
- Constants: Constants are values that remain unchanged throughout the execution of a program. They can be of different types, such as integers, floating-point numbers, characters, and strings.
- Operators: Operators are symbols that perform operations on operands. Examples of operators in C include arithmetic operators such as “+” and “-“, relational operators such as “<” and “>”, and logical operators such as “&&” and “||”.
- Special symbols: Special symbols are characters used to separate or delimit different parts of a program, such as braces “{}”, parentheses “()”, and semicolons “;”.
- Strings: Strings are sequences of characters enclosed in double quotes, used to represent text or character data in a program.
Keywords in C
Here are the keywords in the C programming language listed below:
Keyword | Description |
---|---|
auto | Defines automatic variables |
break | Breaks out of a loop or switch statement |
case | Used in switch statements to define cases |
char | Defines character data type |
const | Defines read-only variables |
continue | Skips one iteration of a loop |
default | Defines default case in switch statements |
do | Starts a do-while loop |
double | Defines double precision floating-point numbers |
else | Used in if-else statements |
enum | Defines enumerated data types |
extern | Declares a variable to be used in another file |
float | Defines floating-point numbers |
for | Starts a for loop |
goto | Jumps to a labeled statement |
if | Starts an if statement |
int | Defines integer data type |
long | Defines long integers |
register | Declares a variable to be stored in a register |
return | Returns a value from a function |
short | Defines short integers |
signed | Defines signed integers |
sizeof | Returns the size of a variable or data type |
static | Defines a static variable |
struct | Defines a structure data type |
switch | Starts a switch statement |
typedef | Defines a new data type |
union | Defines a union data type |
unsigned | Defines unsigned integers |
void | Defines a void type |
volatile | Declares a variable that can be modified by hardware or another process |
while | Starts a while loop |
Identifiers in C
In C programming, an identifier is a name given to a variable, function, or other user-defined entity in a program. Identifiers are used to provide a unique name to a particular entity in a program, which helps the compiler to distinguish between different variables and functions.
In C, an identifier can be a sequence of letters, digits, and underscore characters. However, the first character of an identifier must be a letter or an underscore. Identifiers are case sensitive, meaning that uppercase and lowercase letters are considered different.
Here are some rules for naming identifiers in C programming:
- Identifiers cannot be a keyword. For example, “int” cannot be used as an identifier.
- Identifiers can only contain letters, digits, and underscore characters.
- Identifiers must begin with a letter or an underscore.
- Identifiers cannot contain spaces or special characters.
- Identifiers have a maximum length, which varies by compiler.
- Identifiers are case sensitive.
Here are some examples of valid identifiers in C programming:
- myVariable
- _myVariable
- my_variable
- myFunction
And here are some examples of invalid identifiers:
- 1variable (cannot start with a digit)
- my variable (cannot contain spaces)
- my-variable (cannot contain special characters)
Operators in C
In C programming, an operator is a symbol that performs a specific operation on one or more operands. Operators can be classified into several types, such as arithmetic operators, relational operators, logical operators, bitwise operators, and assignment operators.
Here are some of the most common operators in C programming:
- Arithmetic operators: These are used to perform mathematical operations on numeric operands. Examples of arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
- Relational operators: These are used to compare two operands and return a Boolean value (true or false) depending on whether the comparison is true or false. Examples of relational operators include equal to (==), not equal to (!=), less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=).
- Logical operators: These are used to perform logical operations on Boolean operands. Examples of logical operators include logical AND (&&), logical OR (||), and logical NOT (!).
- Bitwise operators: These are used to perform operations on the individual bits of integer operands. Examples of bitwise operators include bitwise AND (&), bitwise OR (|), bitwise NOT (~), bitwise XOR (^), left shift (<<), and right shift (>>).
- Assignment operators: These are used to assign a value to a variable. Examples of assignment operators include simple assignment (=), addition assignment (+=), subtraction assignment (-=), multiplication assignment (*=), division assignment (/=), and modulus assignment (%=).
Operators are an essential part of C programming, and understanding their different types and meanings is crucial for writing correct and effective code.
Constants in C
In C programming, a constant is a value that remains unchanged throughout the execution of a program. Constants can be of different types, such as integers, floating-point numbers, characters, and strings.
Constants are used to store values that do not need to be changed during program execution. They are typically defined at the beginning of a program or in a header file, and their value cannot be altered during runtime. Constants can be used to improve program readability, prevent errors due to unintended modifications, and make the code more maintainable.
Here are some examples of different types of constants in C programming:
- Integer constants: These are whole numbers that can be positive, negative, or zero. For example, 5, -10, and 0 are all integer constants.
- Floating-point constants: These are real numbers that can have a fractional part. For example, 3.14, -2.5, and 0.0 are all floating-point constants.
- Character constants: These are single characters enclosed in single quotes. For example, ‘A’, ‘b’, and ‘$’ are all character constants.
- String constants: These are sequences of characters enclosed in double quotes. For example, “Hello, world!” is a string constant.
- Enumerated constants: These are user-defined constants that represent a set of integer values. For example, an enumeration type can be defined to represent the days of the week: Sunday, Monday, Tuesday, etc.
Constants are typically defined using the const keyword, which ensures that the value of the constant cannot be changed during program execution. For example, the following code defines a constant integer variable named PI: const int PI = 3.14159;
Strings in C
In C programming, a string is a sequence of characters enclosed in double quotes. Strings are used to represent text or character data in a program.
Strings are a fundamental part of many C programs and are used for a wide variety of purposes, such as storing user input, displaying output, and manipulating text. C provides a rich set of string handling functions to help programmers work with strings more easily.
Here are some examples of how strings are used in C programming:
- Declaring a string variable: A string variable is declared by specifying the data type as char and enclosing the sequence of characters in double quotes. For example:
char greeting[] = "Hello, world!";
- Accessing individual characters in a string: The individual characters in a string can be accessed using an index number starting from 0. For example:
char greeting[] = "Hello, world!";
printf("%c\\n", greeting[0]); // Output: H
- Comparing strings: Strings can be compared using the strcmp() function, which returns 0 if the strings are equal. For example:
char string1[] = "hello";
char string2[] = "world";
if (strcmp(string1, string2) == 0) {
printf("The strings are equal.\\n");
} else {
printf("The strings are not equal.\\n");
}
- Concatenating strings: Strings can be concatenated using the strcat() function, which appends one string to another. For example:
char greeting[] = "Hello, ";
char name[] = "John";
strcat(greeting, name);
printf("%s\\n", greeting); // Output: Hello, John
Characters in C
In C programming, a character is a data type that represents a single character in the ASCII or Unicode character set. Characters are typically represented using the char data type, which is a one-byte integer value that can store 256 different values.
Characters are used to represent letters, digits, punctuation marks, and other special symbols in a program. They are often used in conjunction with strings to represent text or character data.
Here are some examples of how characters are used in C programming:
- Declaring a character variable: A character variable is declared by specifying the data type as char and assigning a character value enclosed in single quotes. For example:
char myChar = 'A';
- Accessing the ASCII value of a character: The ASCII value of a character can be accessed using an integer typecast. For example:
char myChar = 'A';
int asciiValue = (int) myChar;
printf("%d\\n", asciiValue); // Output: 65
- Comparing characters: Characters can be compared using the == operator. For example:
char myChar = 'A';
if (myChar == 'A') {
printf("The character is A.\\n");
} else {
printf("The character is not A.\\n");
}
- Converting characters to uppercase or lowercase: Characters can be converted to uppercase or lowercase using the toupper() and tolower() functions. For example:
char myChar = 'a';
printf("%c\\n", toupper(myChar)); // Output: A
Special Characters in C
In C programming, special characters are characters that have a specific meaning and are used for specific purposes. These characters are used to format output, represent control characters, or perform other special functions in a program.
Here are some examples of special characters in C programming:
- Backslash (): The backslash character is used to escape special characters, such as quotation marks, in a string. For example:
printf("He said, \\"Hello!\\"\\n");
- Newline (\n): The newline character is used to insert a new line in a string or output. For example:
printf("Hello\\nworld!\\n");
- Tab (\t): The tab character is used to insert a tab in a string or output. For example:
printf("Name\\tAge\\nJohn\\t25\\n");
- Carriage return (\r): The carriage return character is used to return the cursor to the beginning of a line in a string or output. For example:
printf("Loading...\\r");
- Null (\0): The null character is used to mark the end of a string. For example:
char myString[] = "Hello\\0world";
printf("%s\\n", myString); // Output: Hello
- Alert (\a): The alert character is used to produce a beep sound. For example:
printf("\\a");
Special characters are an important aspect of C programming, as they allow programmers to format output, represent control characters, and perform other special functions in a program.
Sure, here are some additional special characters in C programming:
- Single quote (‘): The single quote character is used to represent a single quote within a character constant. For example:
char myChar = '\\'';
- Double quote (“): The double quote character is used to represent a double quote within a string constant. For example:
printf("He said, \\"Hello!\\"\\n");
- Question mark (?): The question mark character is used in the ternary operator, which is a shorthand way of writing an if-else statement. For example:
int x = 5;
int y = (x > 10) ? 1 : 0;
- Backspace (\b): The backspace character is used to move the cursor back one character in a string or output. For example:
printf("Loading...\\b\\b\\b");
- Form feed (\f): The form feed character is used to insert a page break in a string or output. For example:
printf("Name\\tAge\\fJohn\\t25\\n");
Special characters are an important aspect of C programming, and understanding their meanings and uses is crucial for writing correct and effective code.
Sure, here are some more special characters in C programming:
- Vertical tab (\v): The vertical tab character is used to insert a vertical tab in a string or output. For example:
printf("Name\\tAge\\vJohn\\t25\\n");
- Alert (\a): The alert character is used to produce a beep sound. For example:
printf("\\a");
- Hexadecimal escape sequence (\x): The hexadecimal escape sequence is used to represent a character using its ASCII code in hexadecimal format. For example:
char myChar = '\\x41'; // Represents the character 'A' in ASCII code
- Octal escape sequence (): The octal escape sequence is used to represent a character using its ASCII code in octal format. For example:
char myChar = '\\101'; // Represents the character 'A' in ASCII code
Special characters are an important aspect of C programming, and understanding their meanings and uses is crucial for writing correct and effective code.
Sure, here are some more special characters in C programming:
- Sizeof operator (\sizeof): The sizeof operator is used to determine the size of a variable or data type. For example:
int myArray[] = {1, 2, 3, 4, 5};
int size = sizeof(myArray) / sizeof(myArray[0]); // Determines the size of the array
- Line continuation (): The line continuation character is used to break a long line of code into multiple lines for readability. For example:
printf("This is a very long \\
sentence that spans \\
multiple lines.\\n");
- Unicode escape sequence (\u and \U): The Unicode escape sequence is used to represent a character using its Unicode code point. For example:
char myChar = '\\u0394'; // Represents the Greek letter delta
- Wide character (\wchar_t): The wide character data type is used to represent a single wide character, which can be used to represent characters from different character sets. For example:
wchar_t myChar = L'Δ'; // Represents the Greek letter delta
Special characters and operators are important aspects of C programming, and understanding their meanings and uses is crucial for writing correct and effective code.