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

Data Types

The document provides an overview of data types in programming, covering basic and advanced types, static vs. dynamic typing, and their importance in coding. It includes examples of various data types such as integers, floats, characters, and advanced data types like arrays and structures, along with common errors related to data types. Additionally, it emphasizes real-world applications and offers practice questions for coding placements.

Uploaded by

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

Data Types

The document provides an overview of data types in programming, covering basic and advanced types, static vs. dynamic typing, and their importance in coding. It includes examples of various data types such as integers, floats, characters, and advanced data types like arrays and structures, along with common errors related to data types. Additionally, it emphasizes real-world applications and offers practice questions for coding placements.

Uploaded by

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

Understanding

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

 Note: Sizes vary by language/platform.


 Implicit Conversion: Done
automatically by the compiler.
 Explicit Conversion (Casting):
Type Done manually by the programmer.
Conversion  Example
int x = 10;
float y = x; // Implicit conversion
float z = (float)x; // Explicit conversion
 Example: Implicit Type Conversion
#include <stdio.h>
int main() {
int num = 10;
float fnum = num; // Implicit conversion
printf("Float value: %.2f", fnum);
Type }
return 0;

Conversion  Example: Explicit Type Conversion (Casting).


(Examples) #include <stdio.h>
int main() {
float num = 10.5;
int intNum = (int)num; // Explicit conversion
printf("Integer value: %d", intNum);
return 0;
}
 Examples:
 List: Ordered collection of
elements.
 Stack: LIFO structure (e.g.,
Abstract undo operations).
Data Types  Queue: FIFO structure (e.g.,
(ADTs) printer queue).
 Importance: Used in
algorithms and real-world
problem-solving.
 Example: Reverse a string
#include <stdio.h>
#include <string.h>
int main() {
Strings char str[100];
(Examples) printf("Enter a string: ");
scanf("%s", str);
printf("Reversed: %s", strrev(str));
return 0;
}
 Example: Find the largest number in
an array
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
Arrays int max = arr[0];
(Examples) for (int i = 1; i < 5; i++) {
if (arr[i] > max) max = arr[i];
}
printf("Largest number: %d", max);
return 0;
}
 Example: Define and use a structure
#include <stdio.h>
struct Student {
int id;
char name[50];
Structures };
float marks;

(Examples) int main() {


struct Student s1 = {1, "Alice", 95.5};
printf("ID: %d, Name: %s, Marks: %.2f", s1.id,
s1.name, s1.marks);
return 0;
}
 Type mismatch errors.
Common  Overflow and underflow
Errors issues.
Related to  Uninitialized variables.
Data Types  Examples and how to debug
these errors.
 Write a program to swap two
numbers without using a third
Coding variable.
Practice  Check if a given string is a
Questions palindrome.
 Use arrays to find the second
largest number in a list.
 Example: Swap two numbers without a temporary
variable.
#include <stdio.h>
int main() {
Practice int a = 10, b = 20;
Questions a = a + b;
b = a - b;
(Examples) a = a - b;
printf("a = %d, b = %d", a, b);
return 0;
}
Swap two numbers
without a temporary Count vowels in a
variable string
#include <stdio.h> #include <stdio.h>
int main() {
int main() { char str[100];

Practice int a = 10, b = 20; int count = 0;


printf("Enter a string: ");

Questions a = a + b; scanf("%s", str);


for (int i = 0; str[i] != '\0'; i++) {
(Examples) b = a - b;
if (str[i] == 'a' || str[i] == 'e' ||
str[i] == 'i' || str[i] == 'o' || str[i] ==
a = a - b; 'u')

printf("a = %d, b = count++;


%d", a, b); }
printf("Number of vowels: %d",
return 0; count);
return 0;
} }
 Data types in game development
(e.g., float for physics simulations).
Real-World  Use in web development (e.g., JSON
Applications data types).
 Role in machine learning models (e.g.,
tensor data types).
 Reviewed basic and advanced
data types.
 Discussed memory allocation
Summary
and type conversion.
 Solved practice questions for
placements.
Q&A  "Any questions or doubts?"
Assignment
s

You might also like