TOKEN
In C programming, a token is the smallest element of a program that is meaningful to the compiler. A C program is made up of various tokens, which are the basic building blocks. There are five types of tokens in C:
A token is the smallest unit used in a C program. Each and every punctuation and word that you come across in a C program is token. A compiler breaks a C program into tokens and then proceeds ahead to the next stages used in the compilation process.
Tokens are some of the most important elements used in the C language for creating a program. One can define tokens in C as the smallest individual elements in a program that is meaningful to the functioning of a compiler.
Use of the Tokens in C
For instance, without words, you cannot create any sentence- similarly, you cannot create any program without using tokens in C language. Thus, we can also say that tokens are the building block or the very basic components used in creating any program in the C language.
Classification and Types of Tokens in C
Token are classified into five Part in C language:
- Keywords in C
- Identifiers in C
- Literals /constant
- Punctuator /Separators
- Operators in C
Keywords
In C, keywords are predefined, reserved words that have special meanings and cannot be used as identifiers (names for variables, functions, etc.). These keywords define the structure and syntax of the C language, as they are used to specify various operations, data types, and control structures.
C has total 32 reserved words.
List of all Keywords in C language:
Identifiers
C identifiers represent the name in the C program, for example, variables, functions, arrays, structures, unions, labels, etc. An identifier can be composed of letters such as uppercase, lowercase letters, underscore, digits, but the starting letter should be either an alphabet or an underscore. If the identifier is not used in the external linkage, then it is called as an internal identifier. If the identifier is used in the external linkage, then it is called as an external identifier.
Rule to Name an Identifiers in C
- An identifier can include letter (a-z or A-Z), and digits (0-9).
- An identifier cannot include special characters except the ' - ' underscore.
- Spaces are not allowed while naming an identifier.
- An identifier can only begin with an underscore or letter
- The identifier must be unique in the namespace.
- C language is case-sensitive so, 'name' and 'NAME' are different identifiers.
Literals / Constant
In C, a constant is a fixed value that cannot be altered by the program during its execution. Constants are used to represent values that remain the same throughout the program, such as numerical values, characters, or strings.
In C, Literals are the constant values that are assigned to the variables. Literals represent fixed values that cannot be modified. Literals contain memory but they do not have references as variables. Generally, both terms, constants, and literals are used interchangeably.
For example, “const int = 5;", is a constant expression and the value 5 is referred to as a constant integer literal.
Integer Constant:
In C, integer constants are numerical values that represent whole numbers (without any fractional or decimal part). These constants can be written in different number systems such as decimal, octal, and hexadecimal. Integer constants have specific rules for their representation.
There are the fixed integers value that can be represented in the given three forms.
Decimal Integer (0-9)
Octal Integer (0-7)
Hexadecimal Integer (0-9 & A-F)
Decimal Integer:
In C, a decimal constant is an integer constant that represents a whole number in the base-10 number system (also called decimal notation). These constants consist of digits 0 through 9 without any leading zeros or prefixes.
The integer constant are not sufficient to represent the quantity that vary continuously like temperature, pressure. In such cases we use the real constant also called as the floating constant as
example :99.748, -36.221
Single character constant:
In C, a single character constant is a constant that represents a single character enclosed in single quotes (' '), such as 'A', '1', or '@'. These constants are of type int in C, which means they are internally stored as integer values corresponding to their ASCII code.
Any character enclosed within single quotes (' ') is called single character constant.
e.g.: 'A' , '7' , '+' etc.
It is Having a fixed numeric values ASCII (American Standard Code for Information Interchange) value.
'0'= 48 'a'=97 'A'=65
'9' = 57 'z'=122 'Z'=90
String constant:
In C, a string constant (or string literal) is a sequence of characters enclosed in double quotes (" "). Unlike a single character constant, which holds only one character and is enclosed in single quotes (' '), a string constant can hold multiple characters, including letters, digits, special characters, and escape sequences.
The Character or set of Character enclosed within (" ") double quotes is called string constant.
e.g. its requires 1 extra byte for storage of null Character (‘\o’) which marks the end of the string.
e.g. "A" , "Name" , "P1" , "@" , "\" ,
Back slash character constant or scape sequence:
In C, escape sequences are special character sequences that allow you to represent non-printable or special characters within character and string constants. Escape sequences always begin with a backslash (\) followed by a specific character or combination of characters that defines the meaning of the escape sequence.
It consist of 2 characters including the back clash and it is generally used with the output statement.
It consist of 2 characters but it is treated as a single character constant and requires a single byte of memory for storage.
e.g. '\a' Audible alert "alert"
'\n' New line
'\t' Horizontal tab
'\\' Back slash
'\0' Null character
' \" ' Double quotes
'\b' Back space
printf("Hello \a World"); o/p HelloWorld
printf("Hello \n World) ; o/p Hello
World
printf("Hello \t World"); o/p Hello World
Printf("Hello \\ world"); o/p Hello\World
printf("He said ,\"hi\" "); o/p He said ,"hi"
printf("Hello \b World"); o/p HellWorld
Punctuators/separates:
In C programming, punctuators are symbols that have special meanings and are used to structure the program's syntax. Punctuators help to define the structure and flow of the code, separate different components, and indicate the relationships between them. They are essential for writing valid and understandable C programs.
These are also called as the separator as the separate on block of code from the other.
[ ] Brackets
{ } Braces
( ) Parentheses
; Semi colon ( Terminator)
: Colon
, Comma
Operators:
Those sings or symbols that perform certain operation are called the operators and the parameters on which the operation is performed are called the operand.
In C programming, operators are symbols that perform operations on variables and values. C has a rich set of operators, which can be categorized based on their functionality. Here’s a comprehensive overview of the different types of operators in C:
Type of operator:
1. Arithmetic operator
2. Relational operator
3. Assignment operator
4. Increment or decrement operator
5. Conditional operator
6. Logical Operator
7. Bitwise Operator
8. Special Operator
1. Arithmetic Operator:
Arithmetic operators in C are fundamental mathematical operations that allow you to perform calculations on numeric data types. C provides a set of built-in arithmetic operators to facilitate these operations. Here's a comprehensive overview of arithmetic operator in C, including the operators used, their functionality, and examples.
- These operators are used to perform basic mathematical operation. like addition, subtraction etc.
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the second operand from the first.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the first operand by the second. (Note: If both operands are integers, the result is also an integer, and any fractional part is discarded.)
- Modulus (%): Returns the remainder of the division of the first operand by the second.
Relational operators in C are used to compare two values and determine the relationship between them. These operators return a boolean value: either true (1) or false (0). Relational operators are essential for controlling the flow of programs, especially in decision-making constructs like if statements and loops.
Those operators is used to establish the relational ship b/w the operands that is they are used to compare the operands.
- Equal to (==): Checks if two values are equal.
- Not equal to (!=): Checks if two values are not equal.
- Greater than (>): Checks if the left operand is greater than the right operand.
- Less than (<): Checks if the left operand is less than the right operand.
- Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
- Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
3. Assignment operators:
Assignment operators are used for assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.
= assigns values
+= assigns sum
-= assigns difference
*= assigns product
/= assigns quotient
%= assigns reminder
Question if int A=8;
1. A+=7 A=A+7 8+7 15
2. A-=5 A=A-5 15-5 10
3. A*=7 A=A*7 10*7 70
4. A%=-11 A=A%-11 70%-11 4
5. A/=10 A=A/10 4/10 0
4. Increment or Decrement operator:
In C programming, increment and decrement operators are unary operators that are used to increase or decrease the value of a variable by one, respectively. These operators are commonly used in loops and iterations for simplifying the manipulation of loop counters and indices.
The increment operator is used to increase the value of the variable by 1. and decrement operator is used to decrease the value of the variable by 1.
i. Increment operators:
The '++' operator is used to increment the value of an integer the increment operator Can also by used in two ways.
a. Pre-increment:-
when placed before the Variable name (also Called the pre-increment operator), its value is incremented instantly. For example, ++A
b. Post increment:
when it is placed after the variable name also Called past increment operator), it Value is preserved temporary Until the execution of this Statement and it gets updated before the execution of the next Statement. For example, A++
ii. Decrement operator:
The -- operator is used to decrement the Value of an integer. Just like the increment operator, the decrement operator Can also be used in two ways.
a. Pre-decrement:
when placed before the Variable name (also called the pre-decrement Operator), its Value is decrement instantly. For example. --A
b. Post Decrement:
when it is placed after the Variable name (also called past-decrement Operator), its Value is preserved temporarily Until the execution of this Statement and it gets Updates before the execution of the next Statement for example, A--
5. Conditional operator:
The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.
In C programming, conditional operators are used to evaluate expressions and return a value based on the truthiness of a condition. The most common conditional operator is the ternary operator (?:), which provides a shorthand way to write conditional statements.
As conditional operator works on three operands, so it is also known as the ternary operator.
Syntax:- test expression 1 ? expression 2 : expression3
(condition)
6. Logical Operators:
In C programming, logical operators are used to combine or manipulate boolean expressions, typically resulting in a boolean value (true or false). They are essential for controlling the flow of execution in conditional statements and loops. The three primary logical operators in C are AND (&&), OR (||), and NOT (!).
In C programming, logical operators are used to evaluate the truth value of an expression or a condition. They allow programmers to combine multiple conditions and test them simultaneously. Logical operators are essential in programming, as they help to control program flow and determine the outcome of different operations.
These Operators are used to perform operations on the logical value . e.g. true (1) and false (0)
- Logical AND (&&) The logical AND operator returns true (non-zero) if both operands are true (non-zero). If either operand is false (zero), the result is false.
- Logical OR (||) The logical OR operator returns true (non-zero) if at least one of the operands is true (non-zero). If both operands are false (zero), the result is false.
- Logical NOT (!)The logical NOT operator negates the truth value of its operand. It returns true (non-zero) if the operand is false (zero) and returns false (zero) if the operand is true (non-zero).
7. Bit wise operator:
In C programming, bitwise operators are used to perform operations on individual bits of integer data types. These operators allow you to manipulate data at the binary level, making them useful for tasks such as low-level programming, performance optimizations, and bit manipulation. The primary bitwise operators in C include AND (&), OR (|), XOR (^), NOT (~), and shift operators (left shift << and right shift >>).
The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the calculations faster.
- Bitwise AND (&) The bitwise AND operator compares each bit of two operands and returns a new value where each bit is set to 1 only if both corresponding bits are 1.
- Bitwise OR (|) The bitwise OR operator compares each bit of two operands and returns a new value where each bit is set to 1 if at least one of the corresponding bits is 1.
- Bitwise XOR (^) The bitwise XOR operator compares each bit of two operands and returns a new value where each bit is set to 1 if the corresponding bits are different.
- Left Shift (<<) The left shift operator shifts the bits of its operand to the left by a specified number of positions. Each left shift effectively multiplies the number by 2.
- Right Shift (>>) The right shift operator shifts the bits of its operand to the right by a specified number of positions. Each right shift effectively divides the number by 2 (for positive integers).
8. Special Operator:
In C programming, special operators refer to a set of operators that do not fit into the conventional categories like arithmetic, logical, bitwise, or relational operators. They include sizeof, comma operator, pointer operator (dereference and address-of), and the member selection operators (dot and arrow). Here’s an overview of these special operators:
sizeof Operator: The sizeof operator is used to determine the size (in bytes) of a data type or variable. It can be used with any data type (including user-defined types) and is evaluated at compile time.
example:
int s;
s= sizeof (int); o/p 2
s= sizeof (float); o/p 4
s= sizeof("hello"); o/p 6