0% found this document useful (0 votes)
5 views35 pages

Unit 3 PIC

The document discusses different types of derived data types in C including arrays, pointers, and functions. It provides details on arrays including declaration, initialization, types, memory representation, and applications of arrays in C. It also covers strings in C including definition, declaration, initialization, and common string handling functions.
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)
5 views35 pages

Unit 3 PIC

The document discusses different types of derived data types in C including arrays, pointers, and functions. It provides details on arrays including declaration, initialization, types, memory representation, and applications of arrays in C. It also covers strings in C including definition, declaration, initialization, and common string handling functions.
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/ 35

DERIVED DATA TYPES IN

C
Definition-

The derived data types are basically derived out of the fundamental data
types. A derived data type won’t typically create a new data type – but would
add various new functionalities to the existing ones instead.

Types of Derived Data Types are-

1. Array
2. Pointers
3. Functions
ARRAYS IN C

What is Array in C?

An array in C is a fixed-size collection of similar data items stored in


contiguous memory locations. It can be used to store the collection of primitive
data types such as int, char, float, etc., and also derived and user-defined data
types such as pointers, structures, etc.
Advantages of Arrays

• In an array, accessing an element is very easy by using the index number.


• The search process can be applied to an array easily.
• 2D Array is used to represent matrices.
• Arrays have low overhead.
• C provides a set of built-in functions for manipulating arrays, such as
sorting and searching.
• C supports arrays of multiple dimensions, which can be useful for
representing complex data structures like matrices.
• Arrays can be easily converted to pointers.
Disadvantages of Arrays

•Fixed size
•Memory allocation issues
•Insertion and deletion issues
•Wasted space
•Limited data type support
•Lack of flexibility
Applications of Arrays

1.Array stores data elements of the same data type.


2.Maintains multiple variable names using a single name. Arrays help to maintain
large data under a single variable name. This avoids the confusion of using multiple
variables.
3.Arrays can be used for sorting data elements. Different sorting techniques like the
Bubble sort, Insertion sort, Selection sort, etc use arrays to store and sort elements
easily.
4.Arrays can be used for performing matrix operations. Many databases, small and
large, consist of one-dimensional and two-dimensional arrays whose elements
are records.
5.Arrays can be used for CPU scheduling.
6.Lastly, arrays are also used to implement other data structures like Stacks, Queues,
Heaps, Hash tables, etc.
C Array Declaration

In C, we have to declare the array like any other variable before using it. We
can declare an array by specifying its name, the type of its elements, and the
size of its dimensions. When we declare an array in C, the compiler allocates
the memory block of the specified size to the array name.

Syntax of Array Declaration

data_type array_name [size];


or
data_type array_name [size1] [size2]...[sizeN];
Array Initialization with Declaration

In this method, we initialize the array along with its declaration. We use an initializer
list to initialize multiple elements of the array. An initializer list is the list of values
enclosed within braces { } separated b a comma.

data_type array_name [size] = {value1, value2, ... valueN};


Types of Array in C

There are two types of arrays based on the number of dimensions it has.
They are as follows:

1.One Dimensional Arrays (1D Array)


2.Multidimensional Arrays
One Dimensional Array in C

The One-dimensional arrays, also known as 1-D arrays in C are those arrays
that have only one dimension.

Syntax of 1D Array in C-

Datatype array_name [size];


Initialization of an Array

Providing a value for each element of the array is called


Initialization.
The list of values must be enclosed in curly braces

There are different types of initializing arrays-


1. Initializing an array with size and initial values
2. Initializing array without size and with initial values
3. Partial array initialization
4. Runtime initialization
1. Initializing an Array with a Specified Size and Values (Static Initialization):

In this method, you define an array with a fixed size and specify the initial values
at the time of declaration.

int myArray[5] = {10, 20, 30, 40, 50};

2. Initializing an Array without Size and with Values

The size of the array will be automatically determined based on the number of
elements in the list.

int numbers[] = {1, 2, 3, 4, 5};


3. Partial array initialization

You can partially initialize an array by providing initial values for some of its elements
while leaving others uninitialized
The remaining elements are initialized to zero or a default value.

int numbers[5] = {1, 2, 3}; // Partial array initialization

4. Runtime Initialization

Runtime initialization allows you to determine the array's size and values
dynamically while the program is running.

Int x[3];
Scanf(“%d %d %d”, &x[0], &x[1], &x[2]);
Memory representation of Single Dimensional array-

The memory representation of a one-dimensional array can be visualized as a linear


sequence of elements stored one after the other in memory. Each element of the array is
of the same data type, so they occupy a fixed amount of memory, typically based on the
data type's size.
Two-Dimensional Array in C

A two-dimensional array or 2D array in C is the simplest form of the multidimensional


array. We can visualize a two-dimensional array as an array of one-dimensional arrays
arranged one over another forming a table with ‘x’ rows and ‘y’ columns where the row
number ranges from 0 to (x-1) and the column number ranges from 0 to (y-1).
Syntax:

datatype array_name[x][y];

Example:

Int arr[2][3];

This array will contain 6 elements(2*3)


Initialization of 2D Array

The array may be initialized by following their declarations with a list of initial
values enclosed in braces
Memory representation of Two Dimensional array-

In memory, a 2-dimensional array is typically represented as a contiguous block of


memory cells. The elements of the array are stored row by row, with each row
occupying a continuous segment of memory. The memory map follows a row-major
order, meaning that the elements of each row are stored together.
STRINGS IN C
STRINGS IN C
String Definition in C

A string is a group of characters, such as letters, numbers, symbols, and punctuation


marks, that are arranged in a linear fashion. A string in C is a group of characters that is
finished with the NULL character "0."

C String Declaration Syntax

Declaring a string in C is as simple as declaring a one-dimensional array. Below is


the basic syntax for declaring a string.

char string_name[size];

In the above syntax string_name is any name given to the string variable and size is
used to define the length of the string, i.e the number of characters strings will store.
C String Initialization
We can initialize a C string in 4 different ways which are as follows:

1. Assigning a String Literal without Size

String literals can be assigned without size. Here, the name of the string str acts as
a pointer because it is an array.

char str[] = “Hello";

2. Assigning a String Literal with a Predefined Size

String literals can be assigned with a predefined size. But we should always account
for one extra space which will be assigned to the null character. If we want to store a
string of size n then we should always declare a string with a size equal to or greater
than n+1.

char str[50] = “Hello";


3. Assigning Character by Character with Size
We can also assign a string character by character. But we should remember to set the
end character as ‘\0’ which is a null character.

char str[11] = { ‘H','e',’l',’l',’o',’w','o','r',’l',’d','\0'};

4. Assigning Character by Character without size


We can assign character by character without size with the NULL character at the end.
The size of the string is determined by the compiler automatically.

char str[] = { ‘H','e',’l',’l',’o',’w','o','r',’l',’d','\0'};


String Handling Functions

The C string functions are built-in functions that can be used for various operations
and manipulations on strings. These string functions can be used to perform tasks
such as string copy, concatenation, comparison, length, etc.

1. Strlen - Returns the length of string name.


2. Strcmp - Compares the first string with the second string. If strings are
the same it returns 0.
3. Strcpy - Copies the contents of string second string to first string .
4. Strcat - Concat one string with another string and the result is stored in
the first string.
1. strlen()-

The strlen() function in C is used to find the length of a string. The strlen() function in C
returns an integer with the length of the string excluding the NULL character. The
strlen() function counts the alphabets, whitespaces, special symbols, and numbers until
it encounters the NULL character in the string.

Int len;
char str[]="Hello";
len=strlen(str);

Output-
5
2. strcmp()

The strcmp() function is part of the standard C library ( string. h ). Its primary
purpose is to compare the characters of the two strings in sequence until it finds a
mismatch or until the end of the strings is reached (that is, the null character '\0').

char str1[] = "Tom!";


char str2[] = "Tom!";
int result = strcmp(str1, str2);

Output-
0 (True)
3. strcpy()

strcpy is a C standard library function that copies a string from one location to another. It
is defined in the string. h header file. The function takes two arguments: a destination
buffer where the copied string will be stored, and a source string that will be copied.

char source[] = "C Program";


char destination[50];
strcpy(destination, source);

Output-
C Program (in destination)
4. strcat()

The strcat() function which defined under the string. h header file in c concatenates
two strings and returns a string in which the two strings are appended. The function
takes the source and destination strings as parameters and returns the destination
string where the destination and source strings are appended.

char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};


char ch2[10]={'c', '\0'};
strcat(ch,ch2);

Output-
helloc
Character Handling Functions

Character functions are those that enable you to manipulate character data. Just as
character datatypes are sometimes called string datatypes, character functions are
sometimes called string functions.

1. toascii(A) The ASCII code value of A, in the range of 0 through 127


2. tolower(HELLO) The lowercase of character HELLO
3. toupper(hello) The uppercase of character hello
4. isalpha(ch) An upper- or lowercase letter of the alphabet ch
5. Isnumeric The IsNumeric() function checks whether an expression is a
valid number. This function returns a Boolean value.
ASCII-

American Standard Code for Information Interchange (ASCII) is a character encoding


standard that assigns a unique numerical value to all characters including special
symbols. In C programming, the ASCII value of the character is stored instead of the
character itself. For example, the ASCII value of ‘A’ is 65.

Each character or special character is represented by some ASCII code.


Each ASCII code occupies 7 bits in memory.
Each character variable is assigned an ASCII value ranging from 0 to 127.

ASCII value of uppercase alphabets – 65 to 90.


ASCII value of lowercase alphabets – 97 to
122.
tolower()-

In C, the tolower() function is used to convert uppercase letters to lowercase. When an


uppercase letter is passed into the tolower() function, it converts it into lowercase.
However, when a lowercase letter is passed into the tolower() function, it returns the same
letter

Example-

Printf(“%c”,tolower(‘G’));
prints g

Printf(“%c”,tolower(‘d’));
prints d
toupper()

The toupper() function is used to convert lowercase alphabet to uppercase. i.e. If


the character passed is a lowercase alphabet then the toupper() function converts a
lowercase alphabet to an uppercase alphabet.

Example-

Printf(“%c”,toupper(‘h’));
prints H

Printf(“%c”,toupper(‘d’));
prints d
isalpha()-

In C programming, isalpha() function checks whether a character is an alphabet


(a to z and A-Z) or not.
If a character passed to isalpha() is an alphabet, it returns a non-zero integer, if
not it returns 0.

Example-

Printf(“%c”,isalpha(‘D’));
returns True

Printf(“%c”,isalpha(‘$’));
returns 0
isnumeric()-

the function checks whether the input character is a digit or not. Here the
character is not a digit. So the isnumeric() function returns zero (0) or false
value.

Example-

Printf(“%c”,isnumeric(‘8’));
returns True

Printf(“%c”,isnumeric(‘a’));
returns 0

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy