0% found this document useful (0 votes)
6 views5 pages

PSC 5 marks

Uploaded by

hwoeou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views5 pages

PSC 5 marks

Uploaded by

hwoeou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

1.What is array? Give example and advantages of array.

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.

2.What is String? Write a program in C to reverse the input string.


Ans.A string is a sequence of characters used to represent text. In most programming
languages, strings are either represented as an array of characters or a special data type.
Here is a simple C program to reverse an input string:
#include <stdio.h>
#include <string.h>

int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

int length = strlen(str) - 1; // exclude newline character


printf("Reversed string: ");

for(int i = length; i >= 0; i--) {


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

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];

// Take input string from the user


printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

// Remove the newline character if fgets is used


str[strcspn(str, "\n")] = '\0';

// Find the length of the string


int length = strlen(str);

// Output the length


printf("Length of the string: %d\n", length);

return 0;
}

4.Explain the difference between character array and string in C.

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.

Scope: Local to the function or block in which they are defined.


Lifetime: The lifetime is limited to the duration of the function or block.
Example:void func() {
auto int x = 10; // 'auto' is optional in local variables
}
2. register:
Storage Location: It suggests that a variable should be stored in a CPU register (if available),
rather than in memory, to speed up access.
Scope: Local to the function or block in which it is defined.
Lifetime: The lifetime is still limited to the function or block, but its storage is optimized for
faster access.
Limitations: You cannot take the address of a register variable using the address-of operator
(&).
Example:void func() {
register int count = 0; // Try to store in a register
}
3. static:
Persistent Storage: A static variable retains its value between function calls and is not
reinitialized each time the function is called. It persists for the entire program's lifetime.
Scope: For local variables, the scope is limited to the function or block where it is declared,
but its value persists across function calls. For global variables or functions, the scope is
limited to the file, making it private to that file.
Lifetime: The variable exists for the duration of the program.
Example:void func() {

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

// Accessing and printing array elements


for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}

return 0;
}

Declaration: int arr[5]; creates an array of 5 integers.

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).

The syntax for declaring a 2D array is:

type array_name[row_size][col_size];

Declaration: Specifies the type of elements, number of rows, and columns.

Initialization: A two-dimensional array can be initialized using nested curly braces.


Example:
#include <stdio.h>

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
};

// Accessing and printing array elements


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("matrix[%d][%d] = %d\n", i, j, matrix[i][j]);
}
}

return 0;
}

Declaration: int matrix[3][3]; creates a 2D array with 3 rows and 3 columns.

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:

One-Dimensional Array: A simple list of values, indexed from 0 to size-1.

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.

Initialization:If an array is not explicitly initialized, it may contain garbage values.


Arrays can be initialized in multiple ways (e.g., manually, or using loops).

Example of Partial Initialization:


For both one-dimensional and two-dimensional arrays, you can initialize only part of the
array and leave the rest to be initialized to zero (for integer arrays) or null (for character
arrays).
Example:int arr[5] = {1, 2}; // The array will be {1, 2, 0, 0, 0}

You might also like