0% found this document useful (0 votes)
27 views

FOC-unit 4

Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

FOC-unit 4

Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Data types

In computer programming, a data type is a way to classify different types of data. Text
data, also known as string data, refers to any sequence of characters, including letters, numbers,
and symbols. This type of data is typically used to store words, phrases, sentences, or paragraphs.
Examples of text data include names, addresses, and descriptions. Numeric data, on the other
hand, refers to numerical values such as integers and floating-point numbers. This type of data is
typically used to store numbers, calculations, or measurements. Examples of numeric data
include ages, prices, and quantities. In summary, text data is any sequence of characters, and
numeric data is any numerical value.

Data Types

1.Numeric Data Types


2.Non numeric Data Types

1. Numeric Data Type:


 Numeric data can be further divided into different subtypes based on the range and
precision of the values they can represent. Some common numeric data types include
integers (e.g., 1, -5, 100), floating-point numbers (e.g., 3.14, -0.5, 2.0), and decimal
numbers for precise decimal calculations.
 Arithmetic operations such as addition, subtraction, multiplication, and division can be
performed on numeric data.
 Comparison operations like greater than (>), less than (<), equal to (==), etc., are used to
compare numeric values.
 Some programming languages provide functions or libraries for performing mathematical
operations and calculations on numeric data, such as trigonometric functions, logarithmic
functions, or rounding functions.
 Numeric data types have limitations on the range of values they can represent and the
precision they can maintain. It's important to consider these limitations while working
with numeric data to avoid unexpected results or errors.

2. Non-numeric Data Types

Character Data Type:

 Character data is represented using a data type called a string. In most programming
languages, strings are enclosed within quotation marks (single or double).
 Strings can contain any combination of characters, including letters, numbers, symbols,
and white spaces.
 Character data is generally used for storing and manipulating textual information, such as
names, addresses, messages, or any other form of human-readable data.
 String data is typically treated as a sequence of individual characters, allowing operations
like concatenation (joining two strings together) or extracting specific characters or
substrings.

 Character data is often represented using Unicode encoding, which allows for the
representation of characters from various writing systems and languages.
 Strings can be manipulated using various operations such as concatenation (combining two or
more strings), substring extraction (extracting a part of a string), length calculation, and
searching for specific patterns or characters.
 Some programming languages provide additional functionality for text data, such as string
formatting, string interpolation, or regular expressions for pattern matching.
 Character data is typically enclosed in quotation marks, but the specific syntax may vary
between programming languages. For example, in Python, strings can be enclosed in
single quotes (''), double quotes ("") or triple quotes (""" or ''').
 Some programming languages provide built-in functions or methods for working with
strings, such as searching for a particular substring, replacing characters, or converting
case (e.g., uppercase to lowercase)

 Character data is often stored as an array or sequence of characters, where each character is
represented by its corresponding Unicode or ASCII code.
 Many programming languages provide various string manipulation functions, such as
converting case (uppercase to lowercase or vice versa), trimming white spaces, splitting strings
into substrings, or joining multiple strings together.
 String interpolation is a common feature in some programming languages, allowing you to
embed variables or expressions within a string using placeholders. The placeholders are then
replaced with their corresponding values during runtime.
 Some languages support regular expressions, which are powerful tools for pattern matching
and manipulating text data. Regular expressions enable tasks like searching for specific patterns,
extracting data based on predefined patterns, or replacing parts of a string.

 Character data is often used in input/output operations, file handling, user interfaces, and text
processing tasks like parsing or tokenizing.

A character set is a collection of characters along with an encoding scheme to convert them to
their binary representations.

There are two popular character sets used frequently in programming languages:

 ASCII
o Originally required 7 bits of memory to store, but modern extensions of ASCII
require 1 full byte (8 bits) of storage space
o Has a base set of 128 characters, including all of the English letters (upper and
lower case), digits 0-9, some common symbols, and several unprintable characters
like tab, return, backspace, etc... The extended versions of ASCII add several
more printable characters.
 Unicode
o Requires 2 bytes (16 bits) of storage space

o Consists of 65,536 characters, including characters and symbols from many


different languages
o ASCII is a subset of Unicode. That is to say, if the letter 'A', for example, is
encoded to a decimal value of 65 in ASCII, then the Unicode version of 'A' will
also be encoded to the same value.
o Java uses Unicode

ARRAYS

Introduction:

So far we have used only single variable name for storing one data item. If we need to store
multiple copies of the same data then it is very difficult for the user. To overcome the difficulty a
new data structure is used called arrays.

 An array is a linear and homogeneous data structure

 An array permits homogeneous data. It means that similar types of elements are stored

contiguously in the memory under one variable name.

 An array can be declared of any standard or custom data type.

An array is a group of similar elements or data items of the same type collected at contiguous
memory locations. In simple words, we can say that in computer programming, arrays are
generally used to organize the same type of data.

Representation of an Array:

Arrays can be represented in several ways, depending on the different languages.


Arrays always store the same type of values. In the above example:

 int is a type of data value.


 Data items stored in an array are known as elements.
 The location or placing of each element has an index value.

Important: Array can store only the same type of data items.

Declaration Syntax of Array:

VariableType VariableName[Sequence of Elements];

Example 1: For integral value

int A[10];

Here 10 means, this array A can have 10 integer elements.

2 5 8 44 21 11 7 9 3 1

Example 2: For character value

char B[10];
This array B can have 10 character elements.

f d a b n j l s e y

Initialization of an Array:

If an array is described inside a function, the elements will have garbage value. And in case an
array is static or global, its elements will be initialized automatically to 0.

We can say that we can simply initialize elements of an array at the time of declaration and for
that, we have to use the proper syntax:

Syntax: datatype Array_Name[size] = { value1, value2, value3, ….. valueN };

Types of Arrays:

There are two types of arrays:

 One-Dimensional Arrays
 Multi-Dimensional Arrays

One -Dimensional Arrays

A one-dimensional array is a kind of linear array. It involves single sub-scripting. The []


(brackets) is used for the subscript of the array and to declare and access the elements from the
array.

Syntax: DataType ArrayName [size];

For example: int a[10];

Multi-Dimensional Arrays

In multi-dimensional arrays, we have two categories:

 Two-Dimensional Arrays
 Three-Dimensional Arrays

1. Two-Dimensional Arrays

An array involving two subscripts [] [] is known as a two-dimensional array. They are also
known as the array of the array. Two-dimensional arrays are divided into rows and columns and
are able to handle the data of the table.

Syntax: DataType ArrayName[row_size][column_size];


For Example: int arr[5][5];

2. Three-Dimensional Arrays

When we require to create two or more tables of the elements to declare the array elements, then
in such a situation we use three-dimensional arrays.

Syntax: DataType ArrayName[size1][size2][size3];

For Example: int a[5][5][5];

Advantages of Array

 It is a better version of storing the data of the same size and same type.
 It enables us to collect the number of elements in it.
 Arrays have a safer cache positioning that improves performance.
 Arrays can represent multiple data items of the same type using a single name.

Disadvantages Of Array:

 In an array, it is essential to identify the number of elements to be stored.


 It is a static structure. It means that in an array, the memory size is fixed.
 When it comes to insertion and deletion, it is a bit difficult because the elements are
stored sequentially and the shifting operation is expensive.

Character Arrays

 In a character array, each element stores one character.


e.g. char c[5];
c[0]='H'; c[1]='e'; c[2]=c[3]='l'; c[4]='o';

c[0]=72; c[1]=101; c[2]=c[3]=108; c[4]=111;

c[0] H

c[1] e

c[2] l

c[3] l

c[4] o
Character Arrays – Initialization
 Like other type one-dimensional arrays, the character array can be initialized in the same
way.
e.g. char c[5] = { 'H', 'e', 'l', 'l', 'o' };

 If the number of initializers is less than the declared size, the remaining elements
are initialized to null character ('\0').
e.g. char c[6] = { 'H', 'e', 'l', 'l', 'o' };

c[0] H

c[1] e

c[2] l

c[3] l

c[4] o

c[5] \0

 The character array can be used as other type one-dimensional arrays.

main()

{ char c[10] = { 'I', 'V', 'a', 'm', 'V', 'h', 'a', 'p', 'p', 'y' };

int i;

for ( i = 0; i < 10; i++ )

printf ( "%c", c[i] );

 A string is a character array.


 char c[10] = { 'I', 'V', 'a', 'm', 'V', 'h', 'a', 'p', 'p', 'y' };
The actual length of the string is not always equal to the size of the character
array.

 In fact, we often concern the actual length of a string rather than the size of a
character array.
char c[100] = { 'I', 'V', 'a', 'm', 'V', 'h', 'a', 'p', 'p', 'y' };
The size of the character array is 100, but the actual length of the string is only 10.

 The array size is often much larger than the size of the string stored in the array. In order
to represent the end of the string, the null character '\0' is used as the "end-of-string"
marker.
char c[5] = { 'B', 'o ', 'y' };

c[0] B

c[1] o

c[2] y

c[3] \0

c[4] \0
 Pay attention to the distinction of the null character '\0', and the blank space character
'V', and the figure character '0'.

 The ASCII value of '\0' is 0. It can't be displayed and acts as the "end-of-string"
marker.

 The ASCII value of 'V' is 32. It can be outputted as a blank and occupy one place.

 The ASCII value of '0' is 48. It can be outputted as a figure.

 When a string constant is stored in memory, it also uses the null character '\0' as the
"end-of-string" marker.

 For example: When the string "Hello" is stored in memory, it requires 6 bytes
storage, and the last byte is used to store the null character '\0'.

Strings – Initialization

 Using characters.
char c[5] = { 'G', 'o ', 'o', 'd' };
 Using string.
char c[5] = { "Good" };
char c[5] = "Good";
char c[ ] = "Good";

c[0] G

c[1] o

c[2] o

c[3] d

c[4] \0
 The initialization of table of strings.
char fruit[ ][7] = { "Apple", "Orange", "Grape", "Pear", "Peach"};

You might also like