Data Types
Data Types
Data Types
Data Types:
A data type defines a collection of data values and a set of predefined operations on
those values. Computer programs produce results by manipulating data.
A data type in programming is like a container that holds certain kinds of information
and allows us to perform specific actions on that information.
Data types that are not defined in terms of other types are called primitive data
types.
All programming languages provide a set of primitive data types
To provide the structured types, the primitive data types of a language are used,
along with one or more type constructors.
Numeric Types
Integer
Integer Data Type: Integers are one of the most common primitive data types
used in programming. They represent whole numbers without any fractional or
decimal parts.
Signed and Unsigned Integers: Signed integers can represent both positive and
negative numbers, whereas unsigned integers represent only non-negative
numbers. Languages like C++ and C# offer support for unsigned integer types,
which are useful for handling binary data.
Floating-Point
Floating-Point Data Types: Floating-point data types are used to represent
real numbers in programming.
Representation in Computers: Floating-point numbers are typically stored in
binary format on most computers.
However, representing numbers like 0.1 in binary can lead to inaccuracies
because some numbers that are easy to represent in decimal notation can't be
represented exactly in binary form.
Loss of Accuracy: Another issue with floating-point types is the loss of
accuracy that can occur through arithmetic operations. Performing operations
like addition, subtraction, multiplication, and division on floating-point
numbers can introduce small errors due to the limited precision of the
representation.
IEEE Floating-Point Standard: Most modern computers use the IEEE
Floating-Point Standard 754 format to represent floating-point numbers. This
standard defines the formats for single-precision (usually called float) and
double-precision (usually called double) floating-point numbers.
Float variables are typically stored in four bytes of memory, while double
variables occupy twice as much storage.
Precision and Range: The collection of values that can be represented by a
floating-point type is defined in terms of precision and range.
Precision refers to the accuracy of the fractional part of a value, measured in
terms of the number of bits used.
Range encompasses both the range of fractions and the range of exponents,
determining the maximum and minimum values that can be represented.
Bas Sig
Precision e n Exponent Significant
Double
2 1 11 52+1
precision
Complex
Some programming languages support a complex data type—for example,
Fortran and Python.
Complex values are represented as ordered pairs of floating-point values.
In Python, the imaginary part of a complex literal is specified by following it
with a j or J—for example,
(7 + 3j)Languages that support a complex type include operations for arithmetic
on complex values.
1 -> 0001
2 -> 0010
3 -> 0011
Example: In python
# Define a character variable
char = 'A'
Boolean types
In this example, str is an array of char elements, specifically apples0, where 0 is the null
character.
Some of the most commonly used library functions for character strings in C
and C++ are strcpy, which moves strings;
strcat, which cancatenates one given string onto another;
strcmp, which lexicographically compares(by the order of their
character codes) two given strings; and
strlen, which returns the number of characters, not counting the null,
in the given string.
The parameters and return values for most of the string manipulation
functions are char pointers that point to arrays of char.
Parameters can also be string literals.
The problem is that the functions in this library that move string data do not
guard against overflowing the destination.
For example, consider the following call to strcpy: strcpy(dest, src);If
the length of dest is 20 and the length of src is 50, strcpy will write
over the 30 bytes that follow dest.
In addition to C-style strings, C++ also supports strings through its
standard class library, which is also similar to that of Java.
Because of the insecurities of the C string library, C++ programmers
should use the string class from the standard library, rather than
char arrays and the C string library.
In Java, strings are supported by the String class, whose values are
constant strings, and the StringBuffer class, whose values are changeable
and are more like arrays of single characters.
These values are specified with methods of the StringBuffer class. C#
and Ruby include string classes that are similar to those of Java.
Python includes strings as a primitive type and has operations for
substring reference, catenation, indexing to access individual characters,
as well as methods for searching and replacement.
There is also an operation for character membership in a string. So,
even though Python’s strings are primitive types, for character and
substring references, they act very much like arrays of characters.
Python strings are immutable, similar to the String class objects of
Java.
In F#, strings are a class.
Individual characters, which are represented in Unicode UTF-16, can be
accessed, but not changed.
Strings can be catenated with the + operator.
In ML, string is a primitive immutable type.
It uses ^ for its catenation operator and includes functions for
substring referencing and getting the size of a string.
Perl, JavaScript, Ruby, and PHP include built-in pattern-matching
operations.
In these languages, the pattern-matching expressions are based on
mathematical regular expressions.
They are called as regular expressions.
Consider the following pattern expression:
/[A-Za-z][A-Za-z\d]+/
|:
The vertical bar | separates two alternatives in the pattern. It functions like an OR
operator, allowing either part of the pattern to match.
\.\d+$:
Strings starting with one or more digits, followed by an optional decimal point and zero
or more digits (\d+\.?\d*).
OR
Strings starting with a decimal point, followed by one or more digits (\.\d+).
"123.45"
"0.123"
"100"
".123"
".5"
".007"
This regular expression can be useful for tasks such as validating user input to ensure it
matches a numeric format, extracting numeric values from a larger string, or searching
for numeric literals within text.