0% found this document useful (0 votes)
26 views24 pages

C Programming: Tokens and Data Types

The document provides an overview of the C programming language, focusing on tokens, data types, operators, and expressions. It categorizes tokens into six types, including punctuators, keywords, strings, operators, identifiers, and constants, and explains their roles in programming. Additionally, it covers various data types such as integers, characters, floats, and doubles, along with arithmetic, relational, and logical operators used in C.

Uploaded by

sr4594245
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)
26 views24 pages

C Programming: Tokens and Data Types

The document provides an overview of the C programming language, focusing on tokens, data types, operators, and expressions. It categorizes tokens into six types, including punctuators, keywords, strings, operators, identifiers, and constants, and explains their roles in programming. Additionally, it covers various data types such as integers, characters, floats, and doubles, along with arithmetic, relational, and logical operators used in C.

Uploaded by

sr4594245
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

SWAMINARAYANASIDDHANTAINSTITUTEOFTE

CHNOLOGY, NAGPUR

COMPUTERENGINEERINGDEPARTMENT

Academic Year: 2025-2026


Year/Semester:B. TechIst Year/1thSem
Subject:Programming for Problem Solving

SWAMINARAYANASIDDHANTAINSTITUTEOFTE
CHNOLOGY,NAGPUR
UNIT-II: Types, Operators and Expressions

Tokens in C

In C programming, tokens are the smallest units in a program that have meaningful
representations. Tokens are the building blocks of a C program, and they are recognized by
the C compiler to form valid expressions and statements. Tokens can be classified into
various categories, each with specific roles in the program.

The tokens of C language can be classified into six types based on the functions they are used
to perform. The types of C tokens are as follows:

Table of Content

Punctuators
Keywords
Strings
Operators
Identifiers
Constants

1. Punctuators
The following special symbols are used in C having some special meaning and thus, cannot
be used for some other purpose. Some of these are listed below:

Brackets[]: Opening and closing brackets are used as array element references. These
indicate single and multidimensional subscripts.

Parentheses(): These special symbols are used to indicate function calls and function
parameters.
Braces{}: These opening and ending curly braces mark the start and end of a block of code
containing more than one executable statement.

Comma (, ): It is used to separate more than one statement like for separating parameters in
function calls.

Colon(: ) It is an operator that essentially invokes something called an initialization list.

Semicolon(;): It is known as a statement terminator. It indicates the end of one logical entity.
That’s why each individual statement must be ended with a semicolon.

Asterisk (*): It is used to create a pointer variable and for the multiplication of variables.
Assignment operator(=): It is used to assign values and for logical operation validation.

Pre-processor (#): The preprocessor is a macro processor that is used automatically by the
compiler to transform your program before actual compilation.

Dot (.): Used to access members of a structure or union.

Tilde(~): Bitwise One’s Complement Operator.

#include <stdio.h>

Int main() {

// ‘\n’ is a special symbol that


// represents a newline
Printf(“Hello, \n World!”);
Return 0;
}

Output
Hello,
World!

2. Keywords

Keywords are reserved words that have predefined meanings in C. These cannot be used as
identifiers (variable names, function names, etc.). Keywords define the structure and behavior
of the program C language supports 32 keywords such as int, for, if, … etc.

Example:

#include <stdio.h>

Int main() {

// ‘int’ is a keyword used to define


// variable type
Int x = 5;
Printf(“%d”, x);
// ‘return’ is a keyword used to exit
// main function
Return 0;
}
Output
5

[Link]

Strings are nothing but an array of characters ended with a null character (‘\0’). This null
character indicates the end of the string. Strings are always enclosed in double quotes.
Whereas a character is enclosed in single quotes in C and C++.

Examples:

#include <stdio.h>

Int main() {

// “Hello, World!” is a string literal


Char str[] = “Hello, World!”;
Printf(“%s”, str);
Return 0;
}

Output
Hello, World!

4. Operators

Operators are symbols that trigger an action when applied to C variables and other objects.
The data items on which operators act are called operands.

Example:

#include <stdio.h>

Int main() {
Int a = 10, b = 5;

// ‘+’ is an arithmetic operator used


// for addition
Int sum = a + b;
Printf(“%d”, sum);
Return 0;
}

Output
15

5. Identifiers

Identifiers are names given to variables, functions, arrays, and other user-defined items. They
must begin with a letter (a-z, A-Z) or an underscore (_) and can be followed by letters, digits
(0-9), and underscores.

Example:

#include <stdio.h>

Int main() {

// ‘num’ is an identifier used to name


// a variable
Intnum = 10;
Printf(“%d”, num);
Return 0;
}

Output
10

6. Constants

Constants are fixed values used in a C program. These values do not change during the
execution of the program. Constants can be integers, floating-point numbers, characters, or
strings.

Examples:

#include <stdio.h>

Int main() {

// ‘MAX_VALUE’ is a constant that holds


// a fixed value
Constint MAX_VALUE = 100;
Printf(“%d”, MAX_VALUE);
Return 0;
}

Output
100
Data Types

Each variable in C has an associated data type. It specifies the type of data that the variable
can store like integer, character, floating, double, etc.

Example:

Int number;

The above statement declares a variable with name number that can store integer values.

C is a statically type language where each variable’s type must be specified at the declaration
and once specified, it cannot be changed.

Integer Data Type

The integer datatype in C is used to store the integer numbers (any number including positive,
negative and zero without decimal part). Octal values, hexadecimal values, and decimal
values can also be stored in int data type in C.

Range: -2,147,483,648 to 2,147,483,647


Size: 4 bytes
Format Specifier: %d

Format specifiers are the symbols that are used for printing and scanning values of given data
types.

Example:

We use int keyword to declare the integer variable:


Intval;
We can store the integer values (literals) in this variable.

#include <stdio.h>

Int main() {
Int var = 22;

Printf(“var = %d”, var);


Return 0;
}

Output
Var = 22

A variable of given data type can only contains the values of the same type. So, var can only
store numbers, not text or anything else.

The integer data type can also be used as:

Unsigned int: It can store the data values from zero to positive numbers, but it can’t store
negative values
Short int: It is lesser in size than the int by 2 bytes so can only store values from -32,768 to
32,767.
Long int: Larger version of the int datatype so can store values greater than int.

Unsigned short int: Similar in relationship with short int as unsigned int with int.

Character Data Type


Character data type allows its variable to store only a single character. The size of the
character is 1 byte. It is the most basic data type in C. It stores a single character and requires
a single byte of memory in almost all compilers.

Range: (-128 to 127) or (0 to 255)


Size: 1 byte
Format Specifier: %c

#include <stdio.h>

Int main() {
Char ch = ‘A’;

Printf(“ch = %c”, ch);


Return 0;
}
Output

Ch = A

Float Data Type


In C programming, float data type is used to store single precision floating-point values.
These values are decimal and exponential numbers.

Range: 1.2E-38 to 3.4E+38


Size: 4 bytes
Format Specifier: %f
Example:

#include <stdio.h>

Int main() {
Float val = 12.45;

Printf(“val = %f”, val);


Return 0;
}

Output
Val = 12.450000

Double Data Type

The double data type in C is used to store decimal numbers (numbers with floating point
values) with double precision. It can easily accommodate about 16 to 17 digits after or before
a decimal point.

Range: 1.7E-308 to 1.7E+308


Size: 8 bytes
Format Specifier: %lf
Example:

#include <stdio.h>

Int main() {
Double val = 1.4521;

Printf(“val = %lf”, val);


Return 0;
}

Output
Val = 1.452100
Void Data Type

The void data type in C is used to indicate the absence of a value. Variables of void data type
are not allowed. It can only be used for pointers and function return type and parameters.

Example:

Void fun(int a, int b){


// function body
}

Size of Data Types in C

The size of the data types in C is dependent on the size of the architecture, so we cannot
define the universal size of the data types. For that, the C language provides the sizeof()
operator to check the size of the data types.

Example

#include <stdio.h>

Int main(){

// Use sizeof() to know size


// the data types
Printf(“The size of int: %d\n”,
Sizeof(int));
Printf(“The size of char: %d\n”,
Sizeof(char));
Printf(“The size of float: %d\n”,
Sizeof(float));
Printf(“The size of double: %d”,
Sizeof(double));

Return 0;
}
Output
The size of int: 4
The size of char: 1
The size of float: 4
The size of double: 8
Arithmetic Operators in C

Arithmetic operators are the type of operators used to perform basic math operations like
addition, subtraction, and multiplication. Let's take a look at an example:

#include <stdio.h>
int main() {

// Calculate the area of the triangle


int sum = 10 + 20;

printf("%d", sum);
return 0;
}

Output
Find the output.

Explanation: In this code, the + operator is used for arithmetic addition, adding the
integers 10 and 20 resulting in value 30 which is stored in the variable sum.
C provides 9 arithmetic operators to work with numbers and perform different
mathematical operations. These can be classified into two types based on the number of
operands they work on:

1. Binary Arithmetic Operators

2. Unary Arithmetic Operators

1. Binary Arithmetic Operators

The binary arithmetic operators work on two operands. C provides 5 such operators for
performing arithmetic functions which are as follows:
Name Operator Arithmetic Operation Syntax

Addition + Add two operands. x+y

Subtraction - Subtract the second operand from the first operand. x-y

Multiplication * Multiply two operands. x*y

Division / Divide the first operand by the second operand. x/y

Calculate the remainder when the first operand is


% x%y
Modulus divided by the second operand.

Example

#include <stdio.h>
int main(){
int a = 10, b = 4, res;

// Addition
res = a + b;
printf("a + b is %d\n", res);

// Subtraction
res = a - b;
printf("a - b is %d\n", res);

// Multiplication
res = a * b;
printf("a * b is %d\n", res);

// Division
res = a / b;
printf("a / b is %d\n", res);

// Modulus
res = a % b;
printf("a %% b is %d\n", res);
return 0;
}
Output
a + b is 14
a - b is 6
a * b is 40
a / b is 2
a % b is 2

2. Unary Arithmetic Operators

The unary arithmetic operators work with a single operand. In C, we have four such
operators which are as follows:

Name Operator Description Syntax

Decreases the integer value of the variable by


-- --a or a--
Decrement one.

++a or
++ Increases the integer value of the variable by one.
Increment a++

Unary Plus + Returns the value of its operand. +a

Unary
- Returns the negative of the value of its operand. -a
Minus

Example

#include <stdio.h>

int main(){
int a = 10, res;

// post-incrementing a
// res is assigned 10, a is not updated yet
res = a++;
// a becomes 11 now

printf("a is %d, res is %d\n", a,res);

// post-decrement example:
// res is assigned 11, a is not updated yet
res = a--;
// a becomes 10 now
printf("a is %d, res is %d\n", a, res);

// pre-increment example:
// res is assigned 11 now since
// a is updated here itself
res = ++a;

// a and res have same values = 11


printf("a is %d, res is %d\n", a, res);

// pre-decrement example:
// res is assigned 10 only since a is updated here
// itself
res = --a;

// a and res have same values = 10


printf("a is %d, res is %d\n", a, res);

printf("+a is %d\n", +a);


printf("-a is %d", -a);

return 0;
}

Output
a is 11, res is 10
a is 10, res is 11
a is 11, res is 11
a is 10, res is 10
+a is 10
-a is -10

Explanation: In this C program, the post-increment and post-decrement operators are


demonstrated, where the value of a is updated after it is assigned to res. In contrast, the
pre-increment and pre-decrement operators update a first before assigning it to res. The
program prints the value of a and res after each operation to show how the operators affect
the values of the variables.

Relational and Logical Operators in C

Relational Operators
Relational operators are used to compare two values in C language. It checks the relationship
between two values. If relation is true, it returns 1. However, if the relation is false, it returns
0.
Here is the table of relational operators in C language
Operators Operator Name

== Equal to

> Greater than

< Less than

!= Not equal to

>= Greater than or equal to

<= Less than or equal to

Here is an example of relational operator in C language


Example
#include <stdio.h>
int main() {
int x = 10;
int y = 28;
if(x==y)
printf("Both variables are equal
");
if(x>y)
printf("x is greater than y
");
if(x<y)
printf("x is less than y
");
if(x!=y)
printf("x is not equal to y
");
if(x<=y)
printf("x is lesser or equal to y
");
if(x>=y)
printf("x is greater or equal to y
");
return 0;
}
Output

x is less than y

x is not equal to y

x is lesser or equal to y
Logical Operators
Logical operators are used to perform logical operations. It returns 0 or 1 based on the result
of condition, whether its true or false. These operators are used for decision making in C
language.

Here is the table of logical operators in C language,


Operators Meaning of Operators Results

&& Logical AND True when all operands are true

|| Logical OR True only if either one operand is true

! Logical NOT True when operand is zero

Here is an example of logical operators in C language,


Example
Live Demo
#include <stdio.h>
int main() {
int x = 10;
int y = 28;
int a = 15;
int b = 20;
if(x<y && a==b)
printf("x is less than y AND a is equal to b
");
if(x<y || a==b)
printf("x is less than y OR a is equal to b
");
if(!x)
printf("x is zero
");
return 0;
}
Output

x is less than y OR a is equal to b


Increment and Decrement Operators in C

Introduction -
Increment and decrement operators in C are essential tools for managing counters, looping
structures, and updating values efficiently. By using increment operators in C (++) and
decrement operators in C (--), you can increase or decrease variable values by one with
minimal code.

These operators, which come in both prefix (++a, --a) and postfix (a++, a--) forms, allow you
to control execution flow in loops and conditional statements seamlessly. Understanding how
increment and decrement operators in C function in various scenarios helps simplify complex
operations, making code shorter and more readable.

Increment Operators in C
Increment operators in C language are used to increase the value of a variable by one.
Represented by the symbol ++, they come in two forms: prefix (++a) and postfix (a++).

In the prefix form, the value is incremented before it’s used in an expression, while in postfix,
the value is used first and then incremented.

Increment operators are especially useful in loops and counters, where they can efficiently
control the iteration process. This operator simplifies code, making it easy to update variables
in a single step, which is commonly needed in conditions, loops, and repetitive calculations.

Decrement Operators in C
Decrement operators in C language are used to reduce the value of a variable by one.
Represented by the symbol --, they have two forms: prefix (--a) and postfix (a--).

In the prefix form, the value is decremented before it’s used in an expression, while in
postfix, the current value is used first and then decreased. Decrement operators are
particularly helpful in loops and counting operations, allowing you to manage iterations in
reverse or reduce a variable in one concise operation.

This operator streamlines code by making it easier to adjust values within loops, conditions,
and calculations where frequent reductions are needs.

Bitwise Operators in C

In C, bitwise operators are used to perform operations directly on the binary


representations of numbers. These operators work by manipulating individual bits (0s and
1s) in a number.
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.
1. The & (bitwise AND) in C 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.
2. The | (bitwise OR) in C 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.
3. The ^ (bitwise XOR) in C 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.
4. The << (left shift) in C takes two numbers, the left shifts the bits of the first operand,
and the second operand decides the number of places to shift.
5. The >> (right shift) in C takes two numbers, right shifts the bits of the first operand,
and the second operand decides the number of places to shift.
6. The ~ (bitwise NOT) in C takes one number and inverts all bits of it.
Bitwise operators allow precise manipulation of bits, giving you control over hardware
operations.
Let’s look at the truth table of the bitwise operators.

X Y X&Y X|Y X^Y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

Example of Bitwise Operators in C


The following program uses bitwise operators to perform bit operations in C.

#include <stdio.h>

int main() {

// a = 5 (00000101 in 8-bit binary)


// b = 9 (00001001 in 8-bit binary)
unsigned int a = 5, b = 9;

// The result is 00000001


printf("a&b = %u\n", a & b);

// The result is 00001101


printf("a|b = %u\n", a | b);

// The result is 00001100


printf("a^b = %u\n", a ^ b);

// The result is 11111111111111111111111111111010


// (assuming 32-bit unsigned int)
printf("~a = %u\n", a = ~a);

// The result is 00010010


printf("b<<1 = %u\n", b << 1);

// The result is 00000100


printf("b>>1 = %u\n", b >> 1);
return 0;
}
Output
a&b = 1
a|b = 13
a^b = 12

~a = 4294967290
b<<1 = 18
b>>1 = 4

Assignment Operators in C

In C, assignment operators are used to assign values to variables. The left operand is the
variable and the right operand is the value being assigned. The value on the right must match
the data type of the variable otherwise, the compiler will raise an error.

#include <stdio.h>
int main() {

// Assigning value 10 to a
// using "=" operator
int a = 10;
printf("%d", a);
return 0;
}

Output

10

Explanation: In the above example, the assignment operator (=) is used to assign the
value 10 to the variable a. The printf() function then prints the value of a, which is 10, to
the console.

Syntax
variable = value;
We can also assign the value of another variable using assignment operator.
#include <stdio.h>
int main() {
int x = 22;
// Assigning value 10 to a
// using "=" operator
int a = x;
printf("%d", a);
return 0;
}

Output
22
Compound Assignment Operators
C also provides compound assignment operators that combine an operation and assignment
in a single step. They make the code shorter and more efficient.
Here are the most commonly used compound assignment operators:
1. Addition Assignment (+=)
Adds the value of the right operand to the left operand and stores the result in the left
operand.

#include <stdio.h>
int main() {
int a = 5;

// Equivalent to a = a + 3
a += 3;
printf("%d", a);
return 0;
}

Output
a=8
. Subtraction Assignment (-=)
Subtracts the value of the right operand from the left operand and stores the result in the
left operand.

#include <stdio.h>
int main() {
int a = 10, b = 5;

// a = a - b
a -= b;
printf("%d", a);
return 0;
}

Output
5

3. Multiplication Assignment (*=)


Multiplies the value of the right operand by the left operand and stores the result in the left
operand.

#include <stdio.h>
int main() {
int a = 10, b = 5;
// a = a * b
a *= b;
printf("%d", a);
return 0;
}

Output
50
4. Division Assignment (/=)
Divides the left operand by the right operand and stores the result in the left operand.

#include <stdio.h>

int main() {
int a = 10, b = 5;

// a = a / b
a /= b;
printf("%d", a);
return 0;
}

Output
2

5. Modulus Assignment (%=)


Takes the modulus of the left operand by the right operand and stores the result in the left
operand.

#include <stdio.h>

int main() {
int a = 10, b = 5;

// a = a % b
a %= b;
printf("%d", a);
return 0;
}

Output
0

6. Bitwise AND Assignment (&=)


Performs a bitwise AND operation and assigns the result.
#include <stdio.h>

int main() {

// 60 = 0011 1100 in binary


int a = 60;

// 13 = 0000 1101 in binary


int b = 13;

// Bitwise AND Assignment


// a = a & b -> 60 & 13 = 12 (0000 1100 in binary)
a &= b;
printf("%d", a);

return 0;
}

Output
12
7. Bitwise OR Assignment (|=)
Performs a bitwise OR operation and assigns the result.

#include <stdio.h>

int main() {

// 60 = 0011 1100 in binary


int a = 60;

// 13 = 0000 1101 in binary


int b = 13;

// Bitwise OR Assignment
// a = a | b -> 60 | 13 = 61 (0011 1101 in binary)
a |= b;
printf("a |= b: %d\n", a);

return 0;
}

Output
a |= b: 61
8. Bitwise XOR Assignment (^=)
Performs a bitwise XOR operation and assigns the result.

#include <stdio.h>

int main() {
// 60 = 0011 1100 in binary

int a = 60;

// 13 = 0000 1101 in binary

int b = 13;

// a = a ^ b -> 60 ^ 13 = 49 (0011 0001 in binary)

a ^= b;

printf("%d", a);

return 0;

#include <stdio.h>

int main() {

// 60 = 0011 1100 in binary

int a = 60;

// 13 = 0000 1101 in binary

int b = 13;

// a = a ^ b -> 60 ^ 13 = 49 (0011 0001 in binary)

a ^= b;

printf("%d", a);

return 0;

}
#include <stdio.h>

int main() {

// 60 = 0011 1100 in binary

int a = 60;

// 13 = 0000 1101 in binary

int b = 13;

// Bitwise Left Shift Assignment

// a = a << 2 -> 60 << 2 = 240 (1111 0000 in binary)

a <<= 2;

printf("%d", a);

return 0;

Output
240

10. Bitwise Right Shift Assignment (>>=)


Shifts the bits of the left operand to the right by the number of positions specified by the
right operand and assigns the result.

#include <stdio.h>

int main() {

// 60 = 0011 1100 in binary


int a = 60;

// 13 = 0000 1101 in binary


int b = 13;

// Bitwise Right Shift Assignment


// a = a >> 2 -> 60 >> 2 = 15 (0000 1111 in binary)
a >>= 2;
printf("%d", a);
return 0;
}

Output
15

conditional expressions precedence and order of evaluation

You might also like