Unit 3 PIC
Unit 3 PIC
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.
1. Array
2. Pointers
3. Functions
ARRAYS IN C
What is Array in C?
•Fixed size
•Memory allocation issues
•Insertion and deletion issues
•Wasted space
•Limited data type support
•Lack of flexibility
Applications of Arrays
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.
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.
There are two types of arrays based on the number of dimensions it has.
They are as follows:
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-
In this method, you define an array with a fixed size and specify the initial values
at the time of declaration.
The size of the array will be automatically determined based on the number of
elements in the list.
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.
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-
datatype array_name[x][y];
Example:
Int arr[2][3];
The array may be initialized by following their declarations with a list of initial
values enclosed in braces
Memory representation of Two Dimensional array-
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:
String literals can be assigned without size. Here, the name of the string str acts as
a pointer because it is an array.
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.
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.
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').
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.
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.
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.
Example-
Printf(“%c”,tolower(‘G’));
prints g
Printf(“%c”,tolower(‘d’));
prints d
toupper()
Example-
Printf(“%c”,toupper(‘h’));
prints H
Printf(“%c”,toupper(‘d’));
prints d
isalpha()-
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