Data Types
Data Types
Data Types in
Programming
Essential Concepts for Placements
• Understand the concept of
data types in programming.
• Learn about basic and
advanced data types.
Content • Explore the differences
between static and dynamic
typing.
• Apply knowledge in solving
placement-related coding
questions.
Data types define the kind
of data a variable can hold
Definition
in a programming
language.
Memory allocation
Purpose Operations allowed
Error prevention
Primitive Data Types: Basic
building blocks (e.g., int, char).
Derived Data Types:
Categories
Constructed from primitive types
of Data (e.g., arrays, pointers).
Types
Abstract Data Types (ADTs):
Data types defined by behavior
(e.g., stacks, queues).
Memory optimization and error
prevention.
Example 1: Correct usage of data
types.
Why Data int age = 25;
Types float salary = 55000.75;
Matter char grade = 'A';
printf("Age: %d, Salary: %.2f, Grade: %c",
(Examples) age, salary, grade);
Example 2: Error due to incorrect
data type.
int price = "100"; // Incorrect data type
Integer: Holds whole numbers
(e.g., int in C, int in Python).
Floating Point: For decimal
Primitive numbers (e.g., float, double).
Data Types Character: Holds a single
character (e.g., char).
Boolean: True or False values
(bool).
Use integers in a program to calculate
the sum of two numbers.
#include <stdio.h>
Integer int main() {
Data Type int num1 = 10, num2 = 20;
(Examples) int sum = num1 + num2;
printf("Sum = %d", sum);
return 0;
}
import sys
print("Max Integer:", sys.maxsize)
print("Min Integer:", -sys.maxsize - 1)
Demonstrat
e limits of
an integer
Example: Calculate the area of a
circle.
#include <stdio.h>
Floating- int main() {
Point Data float radius = 7.5;
Type float area = 3.14159 * radius *
(Examples) radius;
printf("Area = %.2f", area);
return 0;
}
Demonstrat
e precision Python Code
limits of print(0.1 + 0.2 == 0.3) # False due to floating-point
precision
floating-
point
Example: Accept and print a
character
Character #include <stdio.h>
int main() {
and
char letter;
Boolean printf("Enter a character: ");
Data Types scanf("%c", &letter);
(Examples) printf("You entered: %c", letter);
return 0;
}
#include <stdbool.h>
#include <stdio.h>
int main() {
Example: bool isAdult = true;
Boolean if (isAdult)
operations printf("You are an adult.");
return 0;
}
Static Typing Example (C++):
int a = 10;
Static vs. a = "Hello"; // Compilation error
Dynamic Dynamic Typing Example
Typing (Python):
(Examples) a = 10
a = "Hello" # No error
print(a)
Strings: Sequence of characters
(e.g., "Hello").
Arrays: Collection of elements of
Advanced the same type.
Data Types Structures: User-defined types
combining multiple fields.
Enumerations: Set of named
integral constants (enum).
Static Typing: Variable
types are known at compile
Static vs. time (e.g., C, Java).
Dynamic Dynamic Typing: Variable
Typing types are known at runtime
(e.g., Python, JavaScript).
# Dynamic Typing (Python)
x=5
x = "Hello"
Example
// Static Typing (C++)
int x = 5;
x = "Hello"; // Error
Table showing memory size and range for data types
(e.g., for C):
Data Type Size (bytes) Range
int 4 -2,147,483,648 to
2,147,483,647
Memory char 1 -128 to 127
Size and
float 4 ±3.4E–38 to
Limits ±3.4E+38