PSC 5 marks
PSC 5 marks
Ans.An array is a data structure that stores a collection of elements, typically of the same
data type, in a contiguous block of memory. Each element in an array is identified by an
index or position, and arrays allow efficient access to these elements.
# Example of an array in Python (List)
numbers = [1, 2, 3, 4, 5]
Advantages of Arrays:
1. Efficient Access:
Arrays allow for fast access to elements using their indices.
2. Contiguous Memory Allocation:
Since arrays are stored in contiguous memory locations.
3. Easy to Use:
Arrays are simple and easy to implement and use.
4. Fixed Size:
Once an array is defined, its size is fixed.
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
return 0;
}
Example output:Enter a string: Hello
Reversed string: olleH
3.How do you find the length of a string in C? Which library function is used?
Ans.#include <stdio.h>
#include <string.h> // Include the string.h library for strlen()
int main() {
char str[100];
return 0;
}
5.Explain how the strcpy() function works internally. Provide an implementation example.
Ans.The strcpy() function in C is used to copy one string (character array) to another. It
works by copying each character from the source string to the destination string, including
the null terminator ('\0'), which marks the end of the string.
How strcpy() Works Internally:
1. Source and Destination:strcpy() takes two arguments: the destination string and the
source string. It copies the characters from the source string to the destination string.
2. Character-by-Character Copy:The function begins by copying characters from the source
array to the destination array, one by one, until it encounters the null terminator ('\0').
3. Null Terminator:After copying the last character from the source string, the null terminator
('\0') is also copied to the destination string, marking the end of the string.
4. No Bound Checking:strcpy() does not check whether the destination array is large enough
to hold the copied string, which can lead to buffer overflows if the destination is not large
enough.
6.Write a short note on storage classes.
Ans.In C, storage classes define the scope, visibility, and lifetime of variables or functions.
They are used to specify where a variable is stored, how long it persists, and where it can be
accessed. The four primary storage classes in C are:
1. auto:Default Storage Class: By default, local variables in C are of type auto. This means
that they are created when the block or function in which they are declared is entered, and
destroyed when the block or function is exited.
7.Explain the declaration and initialization of one dimensional and two-dimensional array
with an example.
Ans.. One-Dimensional Array:A one-dimensional array is a list of variables of the same type
that are stored in contiguous memory locations. It is declared using the syntax:
type array_name[size];
Declaration: Specifies the type of elements and the number of elements in the array.
Initialization: You can initialize the array either at the time of declaration or later in the code.
Example:
#include <stdio.h>
int main() {
// Declaration and initialization at the same time
int arr[5] = {1, 2, 3, 4, 5}; // Array of 5 integers
return 0;
}
Initialization: {1, 2, 3, 4, 5} initializes the array with values. If not initialized, the array contains
garbage values.
Output:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
2. Two-Dimensional Array:
A two-dimensional array can be thought of as an array of arrays. It is useful for representing
data in rows and columns (like a matrix).
type array_name[row_size][col_size];
int main() {
// Declaration and initialization at the same time
int matrix[3][3] = {
{1, 2, 3}, // First row
{4, 5, 6}, // Second row
{7, 8, 9} // Third row
};
return 0;
}
Initialization: { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } initializes the array with values.
Output:
matrix[0][0] = 1
matrix[0][1] = 2
matrix[0][2] = 3
matrix[1][0] = 4
matrix[1][1] = 5
matrix[1][2] = 6
matrix[2][0] = 7
matrix[2][1] = 8
matrix[2][2] = 9
Key Points:
Two-Dimensional Array: A collection of arrays, where each array represents a row. You
access an element using two indices: one for the row and one for the column.