[go: up one dir, main page]

0% found this document useful (0 votes)
17 views33 pages

Week 3 Tokens in C

The document provides an overview of tokens in the C programming language, defining them as the smallest elements of source code that hold specific meanings. It categorizes tokens into keywords, identifiers, operators, literals, constants, special symbols, and strings, detailing their roles and significance in C programs. Additionally, it explains the use of operators, including unary and binary operators, and their various types and functions.

Uploaded by

joshuamaingi106
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views33 pages

Week 3 Tokens in C

The document provides an overview of tokens in the C programming language, defining them as the smallest elements of source code that hold specific meanings. It categorizes tokens into keywords, identifiers, operators, literals, constants, special symbols, and strings, detailing their roles and significance in C programs. Additionally, it explains the use of operators, including unary and binary operators, and their various types and functions.

Uploaded by

joshuamaingi106
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

TOKENS IN C LANGUAGE

Introduction
As we all know, the English language comprises varied types of words like nouns,
adjectives, pronouns, articles, conjunctions, etc. These words make up the whole
language and are used according to the grammar rules.
Similarly, every programming language also has individual components that play
varied roles in a program called tokens. This lesson will discuss tokens in C, their
significance, their types, and the role they play within the C program.

Tokens in C Language
Tokens in C are the individual units of source code that make up the C programming
language. They are the smallest individual elements (or words or characters) that have a
specific meaning and purpose in any programming language.
In other words, tokens are essential components or the building blocks of C programs
and are processed by the C compiler to generate an executable program.

Use of Tokens in C
Tokens in C include identifiers, keywords, constants, operators, and punctuation
symbols. The C compiler reads the source code and breaks it down into these tokens,
which it uses to understand the program's structure and functionality. Tokens serve as
the foundation upon which C programs are built and executed.

Classification of Tokens in C Language


While there are 6 primary types of tokens in C language, we do have other
classifications as listed below
a) Keywords (Reserved words with special meaning that are used for specific
purposes only.)
b) Identifiers (Names used to identify variables, functions, classes, etc.,
components.)
c) Operators (Symbols used to perform manipulations and modifications on data.)
d) Literals
e) Special Symbols
f) Constants
g) Strings (A sequence of characters/ character array.)
h) Character Set

a) Keyword Tokens in C
Keywords are reserved (or pre-defined) words in C that have predefined meanings
and are used to show actions or operations in the code. They are an integral part of the
C language syntax and cannot be used as identifiers (variable names or function
names) in the code. There are a total of 32 keyword tokens in C language.
Some Examples of keywords include if, while, for, int, else, switch, and return. It is
essential to use these keywords properly to stick to the syntax rules and ensure that the
code is interpreted correctly by the compiler by using the preprocessor directives. The
pre - processor is automatically used by the compiler and denotes that we are using a
header file. Let's look at a simple C program example that illustrates how the if
keyword from the if-statement.

// Example of the if-keyword token in C


#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Positive number\n");}
return 0;
}

Output:
Positive number

Explanation:
In the simple C code example, we first include the input-output header file <stdio.h>.
1. Then, we begin the main() function, which is the entry point for the program's
execution.
2. In main(), we declare a variable num of integer data type and assign the value
10 to it.
3. Next, we use an if-statement, which checks if the value of the num variable is
greater than 0, i.e., num>0.
 If the condition is true, the if-block is executed. It contains a printf()
statement, which displays the string message- Positive number to the
console.
 If the condition is false, the flow moves to the next line in the program.
4. After the if-statement, the main() function terminates with a return 0 statement,
indicating successful execution.
The if keyword is reserved for if-statements in a C program. Using this keyword any
other way will lead to errors. See the table below, listing various other reserved
keyword tokens in C and their significance.

Different Reserved Keyword Tokens in C


Keyword Description
auto Specifies automatic storage class
break Terminates a loop or switch statement
case Labels a statement within a switch
char Declares a character variable
const Specifies a constant value
continue Skips an iteration of a loop
default Specifies default statement in switch
do Starts a do-while loop
Double Declares a double-precision variable
Else Executes when the if condition is false
enum Declares an enumeration type
extern Declares a variable or function
float Declares a floating-point variable
for Starts a for-loop
goto Jumps to a labeled statement
if Starts an if statement
int Declares an integer variable
long Declares a long integer variable
register Specifies register storage class
return Returns a value from a function
short Declares a short integer variable
signed Declares a signed variable
sizeof Determines the size of a data type
static Specifies static storage class
struct Declares a structure type
switch Starts a switch statement
typedef Creates a new data type
union Declares a union type
unsigned Declares an unsigned variable
void Specifies a void return type

b) Identifier Tokens in C

Identifiers in C are user-defined words for naming variables, functions, arrays,


structures and other program elements. They are created by the programmer and
should follow certain naming conventions and rules. For instance, identifiers should
start with a letter or an underscore and can include letters, digits, and underscores.
Also, meaningful and descriptive identifiers help make the code more readable and
maintainable. Choosing appropriate identifiers that reflect the purpose and usage of
the associated individual elements in the code is crucial for writing high-quality C
programs. The example C program below illustrates the use of this type of tokens in C.

// Example of identifier tokens in C


#include <stdio.h>

int main() {

int age = 25;

float salary = 5000.50;

printf("Age: %d\n", age);

printf("Salary: %.2f\n", salary);

return 0;

Output:
Age: 25
Salary: 5000.50

Explanation:
In the sample C code above-

1. Inside the main() function, we initialize two variables, i.e., age (an integer with
a value of 25) and salary (a floating-point number with a value of 5000.50).
2. The names- age and salary are identifiers that describe the type of value they
store.
3. Next, we use the printf() function to display the values of age and salary to the
console.
4. Here, the %d and %.2f format specifiers indicate integer and floating-point
values. The newline escape sequence (\n) shifts the cursor to the next line.

Rules for naming identifier Tokens in C


Here are the rules one must follow for naming variables, functions, etc., identifier
tokens in C:
 Valid characters: Identifier tokens in C can consist of letters (both uppercase
and lowercase), digits, and underscores. However, special characters or blank
spaces are not allowed in identifiers. For example, myVar, count,
and total_score are valid identifiers, but my total score is an invalid identifier.
 First letter: The first letter of an identifier token in C should be a letter or an
underscore. That is, it cannot be a digit or any other special character. For
example, _myVar and score are valid identifiers, but 9count or #score are invalid
identifiers.
 Avoid using keywords: Keywords have predefined meanings in C and cannot
be used as identifiers, such as int, while, for, etc.
 Length limit: There is no specific rule on how long an identifier can be.
However, some compilers may have limitations, and identifiers over 31
characters may cause issues. Hence, identifier tokens are recommended to be
kept reasonably short and descriptive for readability and maintainability.
 Case Convention: While C is case-sensitive, following certain conventions for
readability is common. The most common convention is to use lowercase letters
for variable and function names and uppercase letters for constants. For multi-
word identifiers, you can use either lowercase letters with underscores or
capitalize the first letter of each word (e.g. camelCase).
 In identifiers, both uppercase and lowercase letters are distinct. Therefore, we
can say that identifiers are case sensitive.

c) OPERATORS TOKENS IN C
Operators are symbols that are used to perform operations, manipulations and
modifications on data. More specifically, they perform arithmetic, logical, relational,
and other operations on variables and values in the code.
 There are many types of operators in C, including arithmetic operators, logical
operators, relational/ comparison operators, assignment operators, bitwise
operators, increment/ decrement operators and conditional/ ternary operator.
 Depending upon the number of operands an operator takes, they can be
classified as unary or binary.
 The correct use of operator tokens in C is essential for performing desired
computations and manipulations on data in C programs.
 Understanding their precedence and associativity rules is also important for
writing correct and efficient code.

The operators are types of symbols that inform a compiler for performing some specific
logical or mathematical functions. The operators serve as the foundations of the
programming languages. Thus, the overall functionalities of the C programming
language remain incomplete if we do not use operators. In simpler words, the operators
are symbols that help users perform specific operations and computations- both logical
and mathematical on the operands. Thus, the operators operate on the available
operands in a program.

Use of Operators in C
The operators basically serve as symbols that operate on any value or variable. We use
it for performing various operations- such as logical, arithmetic, relational, and many
more. A programmer must use various operators for performing certain types of
mathematical operations. Thus, the primary purpose of the operators is to perform
various logical and mathematical calculations.
The programming languages like C come with some built-in functions that are rich in
nature. The use of these operators is vast. These operators act as very powerful and
useful features of all the programming languages, and the functionality of these
languages is pretty much useless without these. These make it very easy for
programmers to write the code very easily and efficiently.

CATEGORIES OF OPERATORS
Operators can be categorized based on the number of operands it operates on i.e one (1)
operand, two (2) operands and three (3) operands.
I. Unary operators – have one (1) operand
II. Binary operators – have two (2) operands
III. Ternary operators – have three (3) operands

I. UNARY OPERATORS
These are the type of operators that act upon just a single operand for producing a new
value. All the unary operators have equal precedence, and their associativity is from
right to left. When we combine the unary operator with an operand, we get the unary
expression.

Types of Unary Operator in C


There are following types of unary operators found in the C language:
 unary minus (-)
 unary plus (+)
 decrement (- -)
 increment (++)
 NOT (!)
 sizeof ()
 Address of operator (&)

Functions of Unary Operator in C


Unary Minus
This operator changes the sign of any given argument. It means that a positive number
will become negative, and a negative number will become positive.
But the unary minus is different from that of the subtractor operator. It is because we
require two operands for subtraction.
int p = 20;
int q = -p; // q = -20

Unary Plus
This operator changes the sign of any negative argument. It means that a negative
number will become positive, and a positive number will also become positive.
But the unary plus is different from that of the addition operator. It is because we
require two operands for addition.
int p = -20;
int q = +p; // q = 20

NOT (!)
We use this operator for reversing the logical state of the available operand. It means
that the logical NOT operator will make an available condition to be false if it is already
true.
In simple words,
 If p is true, then !p will be false.
 If p is false, then !p will be true.
Increment
We use this operator to increment the overall value of any given variable by a value of
1. We can perform increment using two major ways:
 Prefix increment
 Postfix Increment
Prefix Increment
The operator in this method precedes the given operand (Example, ++p). Thus, the
value of the operand gets altered before we finally use it.
For instance,
int p = 1;
int q = ++p; // q = 2
Postfix Increment
The operator in this method follows the given operand (Example, p++). Thus, the value
of the available operand gets altered after we use it.
For instance,
int p = 1;
int q = p++; // q = 1
int r = p; // r = 2

Decrement
We use this operator to decrement the overall value of any given variable by a value of
1. We can perform decrement using two major ways:

Prefix Decrement
The operator in this method precedes the given operand (Example, --p). Thus, the value
of the operand gets altered before we finally use it.
For instance,
int p = 1;
int q = --p; // q = 0

Postfix Decrement
The operator in this method follows the given operand (Example, p--). Thus, the value
of the available operand gets altered after we use it.
For instance,
int p = 1;
int q = p--; // q = 1
int r = p; // r = 0

Address of Operator (&)


This type of operator provides the user with the address of any variable. The address of
operator is used to return the address (memory address) of any variable. The addresses
that are returned using this operator are called pointers. It is because they point towards
the variable present in the memory. For instance, &n gives an address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr.

Sizeof ()
The function of the size of operator is to return the original size of the available operand
in terms of bytes. This type of operator always precedes the given operand. Here, the
operand can either be an expression or it can also be a cast. For instance,
float n = 0;
sizeof(n);

II. BINARY OPERATORS


A binary operator operates on two operands or variables in an expression. It takes two
values, performs an operation on them, and returns a result. Binary operators are used
to perform arithmetic, logical, and comparison operations.
Examples of binary operators include addition (+), subtraction (-), multiplication (*),
division (/), and equality (==). Binary operators are commonly used in programming
to perform calculations or compare two values. The C code example below illustrates
the use of the addition binary operator token.

#include <stdio.h>
int main() {
int a = 5;
int b = 3;
int result = a + b; // Using the binary addition operator (+)
printf("The result of %d + %d is %d\n", a, b, result);
return 0;
}

Output:

The result of 5 + 3 is 8

Binary operators can further be divided into the following types:


1. Arithmetic operators
2. Relational operators
3. Assignment operators
4. Logical operators
5. Bitwise operators

1. ARITHMETIC OPERATORS:
The symbols of the arithmetic operators are:-
Operation Operator Comment Value of Value of
Sum before sum after
Multiply * sum = sum * 2 4 8
Divide / sum = sum / 2 4 2
Addition + sum = sum + 2 4 6
Subtraction - sum = sum - 2 4 2
Increment ++ ++sum 4 5
Decrement -- --sum 4 3
Modulus % sum = sum % 3 4 1

2. THE RELATIONAL OPERATORS


These allow the comparison of two or more variables.

== equal to
!= not equal
< less than
<= less than or equal to
> greater than
>= greater than or equal to

3. ASSIGNMENT OPERATORS
We use these operators to assign specific values to variables.

OPERATOR NAME USE

= Assignment Assigns value from right to left operand


+= Addition assignment Stores summed value in the left operand

-= Subtraction assignment Stores subtracted value in the left operand

Multiplication
*= Stores multiplied value in the left operand
assignment

/= Division assignment Stores quotient in the left operand

%= Modulus assignment Stores remainder in the left operand

4. LOGICAL OPERATORS
We use logical operators when we need to make decisions by testing one or more
conditions. Thus, logical operators work on Boolean values. The answers returned are
either true or false.

Symbol Name Description


&& Logical AND Logical AND requires all conditions to evaluate as
TRUE (non-zero
|| Logical OR Logical OR will be executed if any ONE of the
conditions is TRUE (non-zero).
! Logical NOT Logical NOT negates (changes from TRUE to FALSE,
vsvs) a condition.
^ Logical EOR Logical EOR will be excuted if either condition is
TRUE, but NOT if they are all true.
Bitwise
Negation

Bitwise operators in C
In C, the following 6 operators are bitwise operators (also known as bit operators as
they work at the bit-level). They are used to perform bitwise operations in C.

& bitwise AND Takes two numbers as operands and does AND on every bit of
two numbers. The result of AND is 1 only if both bits are 1.
| bitwise OR Takes two numbers as operands and does OR on every bit of
two numbers. The result of OR is 1 if any of the two bits is 1
^ bitwise XOR Takes two numbers as operands and does XOR on every bit of
two numbers. The result of XOR is 1 if the two bits are
different
<< left shift Left shifts the bits of the first operand, and the second operand
decides the number of places to shift
>> right shift Right shifts the bits of the first operand, and the second
operand decides the number of places to shift
~ bitwise NOT Takes one number and inverts all bits of it

We use the following truth table for bitwise operators


A B A & B (Bitwise AND) A | B (Bitwise OR) A ^ B (Bitwise XoR)
1 1 1 1 0
0 1 0 1 1
1 0 0 1 1
0 0 0 0 0
1 1 1 1 0

III. TERNARY OPERATORS


The conditional operator in C is kind of similar to the if-else statement as it follows the
same algorithm as of if-else statement but the conditional operator takes less space and
helps to write the if-else statements in the shortest way possible. It is also known as the
ternary operator in C as it operates on three operands.

Syntax of Conditional/Ternary Operator in C


The conditional operator can be in the form
variable = Expression1 ? Expression2 : Expression3;

Or the syntax can also be in this form


variable = (condition) ? Expression2 : Expression3;

Or syntax can also be in this form


(condition) ? (variable = Expression2) : (variable = Expression3);

Special Operators
C facilitates the usage of some special operators, which helps in reducing the hassle of
programmers. Some of them are:
• *(Pointer) => it stores the memory address of a variable.
• &(Pointer) => this points to the memory location where the computer stores the
operand.
• sizeof => this operator returns the space occupied by a particular data type in its
memory location e.g sizeof(int)
• comma (,) =>Used to link the related expressions together e.g value = (x=10, y=5)
Precedence of Operators in C
The precedence of the operators in C language helps in determining the preference in
which the operators must be evaluated in a program. Some operators display higher
precedence as compared to the others in the program. For instance, the precedence of
the multiplication operator is much higher than that of the addition operator. Let us
consider an example where a = 5 + 2 * 7; so here, the variable a gets assigned with the
value 19 and not 70. It is because the precedence of the multiplication operator (*) is
higher than that of the addition operator (+). Thus, the program would first multiply 2
with 7, produce 14, and then add it with 5 to get 19 as the final result.
The table below displays the operators according to their precedence. The ones with
higher precedence appear at the very top, and the ones with lower precedence appear at
the very bottom. In any given expression, the evaluation of the operators with higher
precedence occurs first.
Precedence Operator Description Associativity

1 () Parentheses (function call) left-to-right

[] Brackets (array subscript) left-to-right

. Member selection via object name left-to-right

-> Member selection via a pointer left-to-right

a++ , a– Postfix increment/decrement (a is left-to-right


a variable)

2 ++a , –a Prefix increment/decrement (a is right-to-left


a variable)

+,– Unary plus/minus right-to-left


!,~ Logical negation/bitwise right-to-left
complement

(type) Cast (convert value to temporary right-to-left


value of type)

* Dereference right-to-left

& Address (of operand) right-to-left

sizeof Determine size in bytes on this right-to-left


implementation

3 *,/,% Multiplication/division/modulus left-to-right

4 +,– Addition/subtraction left-to-right

5 << , >> Bitwise shift left, Bitwise shift left-to-right


right

6 < , <= Relational less than/less than or left-to-right


equal to

> , >= Relational greater than/greater left-to-right


than or equal to

7 == , != Relational is equal to/is not equal left-to-right


to

8 & Bitwise AND left-to-right

9 ^ Bitwise XOR left-to-right

10 | Bitwise OR left-to-right

11 && Logical AND left-to-right


12 || Logical OR left-to-right

13 ?: Ternary conditional right-to-left

14 = Assignment right-to-left

+= , -= Addition/subtraction assignment right-to-left

*= , /= Multiplication/division right-to-left
assignment

%= , &= Modulus/bitwise AND right-to-left


assignment

^= , |= Bitwise exclusive/inclusive OR right-to-left


assignment

<<=, >>= Bitwise shift left/right right-to-left


assignment

15 , expression separator left-to-right

Let's understand the precedence (or priority) operator with the help of programming
example.
#include <stdio.h>
int main() {
int a = 5;
a = 10,20,30;
printf("%d", a);
return 0;
}
Output:
10
Explanation:
Priority or precedence for the values assigned to any variable is given from left to right.

Example 2:
#include <stdio.h>
int main() {
int x = 5;
x = (10,20,30);
printf("%d", x);
return 0;
}
Output:
30
Explanation
Priority or precedence for the values inside brackets () assigned to any variable is given
from right to left.

d) Literal Tokens in C
Literals are used to represent constant values or fixed values that remain unchanged
during program execution. There are several types of literal tokens in C, including
numeric literals (e.g., integers or floating-point numbers), literal characters (e.g., \n for
newline), and string literals (e.g., Hello, World!).
Proper use of literals helps define the values of variables, constants, and other data
elements in the code, and their accurate representation is crucial for achieving the
desired behavior in code. Below is a sample C program that illustrates the creation of
various literal tokens in C.

#include <stdio.h>

int main() {

int integerLiteral = 42; // integer literal

float floatLiteral = 3.14; // floating-point literal

char charLiteral = 'A'; // character literal

double doubleLiteral = 2.71828; // double precision floating-point literal

const char* stringLiteral = "Hello"; // string literal

printf("Integer Literal: %d\n", integerLiteral);

printf("Float Literal: %f\n", floatLiteral);

printf("Character Literal: %c\n", charLiteral);

printf("Double Literal: %lf\n", doubleLiteral);

printf("String Literal: %s\n", stringLiteral);

return 0;
}

Output:
Integer Literal: 42
Float Literal: 3.140000
Character Literal: A
Double Literal: 2.718280
String Literal: Hello

Explanation:
In the example C code above-

1. In the main() function, we declare and initialize different types of literal tokens,
including-
 integerLiteral with an integer literal value of 42.
 floatLiteral with a floating-point literal value of 3.14.
 charLiteral with a character literal value of 'A'.
 doubleLiteral with a double-precision floating-point literal value of
2.71828.
 stringLiteral with a string literal value of "Hello".
2. We then use a set of printf() statements to display the values of these literals
with appropriate format specifiers.
3. Finally, the program returns 0, indicating successful execution, before exiting.

Types of Literals
Integer literals
Integer literals are used to represent integer values in C, which are whole numbers
without any decimal point. They can be written with or without a sign and can be
represented in different bases, such as decimal, hexadecimal, and octal. For example:
Decimal: 42, -10
Hexadecimal: 0xFF (which represents 255 in decimal)
Octal: 012 (which represents 10 in decimal)

Floating-point literals
Floating-point literals are used to represent floating-point values in C, which are
numbers with a decimal point. They can be written with or without a sign and can
have an optional exponent part. For example:
3.14, -0.5
2.0e-3 (which represents 0.002 in decimal)

Character literals
Character literals are used to represent individual characters in C, enclosed in single
quotes (' '). They can be regular characters, escape sequences representing special
characters (such as '\n' for a newline character), or octal or hexadecimal escape
sequences representing characters by their ASCII values. For example:
'a', '1', '\n' (newline character), '\x41' (which represents 'A' in ASCII)

String literals
String literals are used to represent sequences of characters in C, enclosed in double
quotes (" "). They are used to represent strings of characters, which are arrays of
characters terminated by a null character ('\0'). For example:
"hello", "world"
Boolean literals
Boolean literals represent boolean values, which can be either true or false. In C,
boolean values are typically represented using integer literals, where 0 represents false
and any non-zero value represents true. For example:
0 (false), 1 (true)
Binary literals
Binary literals are used to represent integer values in binary (base 2) notation and were
introduced in the C99 standard. They start with the prefix '0b' followed by a sequence
of binary digits (0 or 1). For example:
0b1010 (which represents 10 in decimal)

e) SPECIAL SYMBOLS IN C
Special symbol tokens in C include punctuation marks and other characters used to
structure and separate elements in code. Examples of special symbols include commas,
semicolons, parentheses, curly braces, and brackets.

Character Name Meaning


/* Slash star Beginning of multiline comment
*/ Star slash End of multiline comment
// Two slashes Single line comment
# Pound sign Beginning of preprocessor directive
<> Angle brackets Encloses a preprocessor directive filename
() Parenthesis Used to indicate function calls and functions
parameters
{} Curly Bracket Marks the start and end of a block of code
containing more than one executable statement
““ Quotation marks Encloses a string of characters
; Semicolon End of a programming statement, termination
[] Square bracket Used as array element reference. They indicate
single and multidimensional subscripts
: Colon Label, used for reference to jump to when need
arises
They are crucial for writing syntactically correct C programs and help organise the
code. Understanding the role and usage of these symbols is essential for maintaining
the integrity and readability of C code. Below is a C program sample that illustrates the
use of many special symbol tokens in C language.
Code Example:

// Example of special symbol tokens in C

#include <stdio.h>

int main() { //curly braces mark the enclosure of the code block for this function body

int num1, num2; // variable declaration with commas

num1 = 10; // variable assignment with assignment operator

num2 = 20;

if (num1 > num2) { // if statement with parentheses

printf("num1 is greater\n");

} else {

printf("num2 is greater\n");

};

return 0;

Output:
num2 is greater

Explanation:
In this C code sample-
1. In the main() function, we declare two integer variables, num1 and num2, in a
single line using the comma symbol between them.
2. Then, we assign value 10 to num1 and value 20 to num2 using the assignment
operator (=).
3. Next, we create an if conditional / control statement, followed by a pair of
parentheses ( ). The if-statement allows for conditional execution of code based
on a condition.
4. We have a condition num1 > num2 inside the if-statement to check whether
num1 is greater than num2. If this condition is true, the code inside the
corresponding block (curly braces{}) will be executed.
 If the condition is true, the if-block containing the printf() function prints
the message- 'num1 is greater' to the console.
 If the condition is false, the else-block containing the printf() function
prints the message 'num2 is greater' to the console.
5. The program above showcases the use of multiple special characters and strings.

Examples of Special Symbol Tokens


In the C programming language, several special symbols serve various purposes and
have special meanings. Here are some examples of special symbols in C:

 Comma (,): The comma separates multiple expressions or variables in a function


declaration or function parameters in a function call. For example:
int a, b, c; // declares three integer variables a, b, and c
printf("Hello", "World"); // calls the printf function with two string arguments

 Semicolon (;): The semicolon, also known as a statement terminator, terminates


statements in C. It indicates the end of a statement and separates multiple
statements. For example:
int a = 10; // initializes integer variable a with value 10
printf("Hello World"); // prints "Hello World" to the console
 Parentheses (()): Parentheses or simple brackets are used for grouping
expressions, passing arguments to functions, and controlling the order of
evaluation in expressions. For example:
int sum = (a + b) * c; // calculates the sum of a and b, and then multiplies it by c
printf("Value of a: %d", a); // prints the value of a using a format specifier in
parentheses

 Braces ({ }): Braces or curly brackets are used to define blocks of code, such as
function bodies, loop bodies, and conditional statements. They enclose a group
of statements and define their scope. For example:
if (a > b) { // starts a conditional statement block
printf("a is greater than b");
} // ends the conditional statement block

 Brackets ([ ]): Square Brackets are used to declare and access elements in
arrays and for specifying array sizes in function prototypes. They are also
known as opening and closing brackets. For example:
int arr[5]; // declares an integer array with size 5
printf("Value at index 2: %d", arr[2]); // accesses the value at index 2 in the
array

 Asterisk Symbol(*): Used as the multiplication operator for the multiplication


of variables and as the dereference operator for pointer variables. For example:
int a = 5 * 2; // Multiplication
int *ptr; // Pointer declaration
int b = *ptr; // Dereferencing the pointer

 Ampersand (&): Used as the address-of operator to get the dynamic memory
address of a variable. For example:
int x = 10;
int *ptr = &x; // Pointer to x

f) CONSTANT TOKENS IN C

Constant tokens in C are values that do not change during the execution of a program.
They are also known as literal values or simply literals. In other words, constant tokens
in C represent fixed values, such as numbers, characters, and strings, that remain
unchanged throughout the execution of a C program.

Code Snippet:
// Example of constants in C
int num = 10; // integer constant
float pi = 3.14; // floating-point constant
char ch = 'A'; // character constant
char str[] = "Hello"; // string literal

Primary Constant Tokens


Primary constants are fundamental values or factors that remain unchanged
throughout a process or system. They are fixed and inherent properties that define the
characteristics or behavior of a system.
Examples of primary constants include the speed of light in a vacuum (denoted as "c"
and approximately equal to 3 x 10^8 meters per second), the gravitational constant
(denoted as "G" and used to calculate gravitational forces between objects),

Secondary Constant Tokens


Secondary constants are derived or calculated values that depend on primary constants
and other variables. They are not fundamental properties but are determined based on
relationships or equations involving primary constants. Examples of secondary
constants include the Planck constant (denoted as "h" and used in quantum mechanics
to describe the relationship between energy and frequency of electromagnetic waves).

g) STRINGS IN C

String tokens in C are sequences of characters represented as arrays of characters


terminated by a null character (\0). They are used to store and manipulate text or
character data in C programs. String tokens are an important user-defined data type
widely used in various operations, such as input/output, manipulation, and storage of
textual data. String tokens in C are created using double quotes (" ").

Code example:

#include <stdio.h>

int main() {
char greeting[] = "Hello, World!"; // string token

printf("Greeting: %s\n", greeting); // printing the string

return 0;

Output:
Greeting: Hello, World!

Explanation:
In the C program sample-
1. We create a string called greeting and assign to it the character array- Hello,
World!.
2. Here, we use the double quote method, and the empty square brackets indicate
no fixed number of characters the literal can take.
3. Then, we use the printf() function to display the value of this string to the
console.

Types of String Tokens In C


In the C programming language, strings can be represented using different types of
string tokens that have some special meaning. Here are some commonly used string
token types in C:
 Character Arrays
Strings in C are often represented as a single-character array, where each
character in the array represents a character in the string. The string is
terminated by a null character ('\0'). For example:
char str[] = "Hello, World!";

 Character Pointers
C allows you to represent strings using character pointers, which point to the
first character of the string. The string is again terminated by a null character
('\0'). For example:
char *str = "Hello, World!";

 String Constants
C allows you to directly specify string constants by enclosing them in double
quotes. String constants are treated as character arrays and automatically
terminated by a null character ('\0'). For example:
"Hello, World!"

 Escape Sequences:
C provides escape sequences to represent special characters within strings.
Escape sequences start with a backslash (\) followed by a specific character
representing a particular value. Some commonly used escape sequences are:

\" (Double Quotes) \t (Horizontal Tab)

\' (Single Quotes) \r (Carriage Return)

\\ (Backslash) \b (Backspace)

\n (Newline) \f (Form Feed)


h) C CHARACTER SET

In the C programming language, the character set is based on the ASCII (American
Standard Code for Information Interchange) standard. ASCII defines a set of 128
characters, including control characters (such as newline, carriage return, and tab) and
printable characters (letters, digits, punctuation marks, and special symbols).
 The ASCII character set uses 7 bits to represent each character, allowing for a
total of 128 unique characters.
 The characters are encoded using integer values ranging from 0 to 127. For
example, the character 'A' is represented by the integer value 65, 'B' by 66, and
so on.
 In addition to the ASCII character set, the C language also supports escape
sequences that represent special characters and white space from memory, such
as '\n' for newline, '\t' for tab, and '\r' for carriage return.
 Furthermore, C provides the concept of wide characters, which are used to
handle internationalization and localization.
 Wide characters are represented by the wchar_t type and use a wider character
encoding, such as Unicode, to support a broader range of characters beyond the
ASCII set.

You might also like