💻 Unit 2 – Part A (2-Mark Q&A)

C Programming

⬅ Back to Unit 2

Ad Space

Part A: 2-Mark Questions

1. Define string?

In C, a string is a sequence of characters stored in a character array and always ends with a special null character '\0' that indicates the end of the string.

Strings are widely used for handling names, messages, and text data in C programs.

2. What is the difference between one dimensional and two-dimensional array?

One-Dimensional Array Two-Dimensional Array
Stores data in a single row (linear form). Stores data in rows and columns (matrix form).
Declared as: datatype arrayName[size]; Declared as: datatype arrayName[rows][cols];
Accessed using one subscript → a[i] Accessed using two subscripts → a[i][j].
Example: int marks[5]; Example: int matrix[3][3];
Suitable for storing a list of items like marks of students. Suitable for storing tabular data like a matrix or table.

3. What is an array? Write the syntax for multi-dimensional array.

An array in C is a collection of elements of the same data type stored in contiguous memory locations. Each element can be accessed using an index.

e.g. int marks[5]; // array of 5 integers

Syntax of Multi-Dimensional Array

A multi-dimensional array is an array of arrays (2D, 3D, etc.).

General Syntax:

data_type array_name[size1][size2]...[sizeN];

Example (2D array):

int matrix[3][3]; // 3x3 integer matrix

Example (3D array):

int cube[3][3][3]; // 3x3x3 integer cube

4. List out the string operations.

Common String Operations in C:

  1. String Length → strlen()
    Finds the number of characters in a string (excluding '\0').
  2. String Copy → strcpy()
    Copies one string into another.
  3. String Concatenation → strcat()
    Joins (appends) one string to the end of another.
  4. String Comparison → strcmp()
    Compares two strings (returns 0 if equal).
  5. String Reverse → strrev() (in some compilers)
    Reverses a string.
  6. String Conversion → atoi(), atof(), itoa()
    Converts strings to integers, floats, etc.

5. How to identify length of a string and give an example?

In C, the function strlen() (from <string.h>) is used to find the length of a string. It counts the number of characters excluding the null character '\0'.

Syntax:

int strlen(string_name);

Example Program:

#include <stdio.h>
#include <string.h>

int main() {
    char str[20] = "Hello World";
    int len;
    
    len = strlen(str);
    
    printf("Length of the string = %d\n", len);
    return 0;
}

Output:

Length of the string = 11

6. Write down the syntax for 2D array declaration with example.

Syntax:

data_type array_name[rows][columns];

Example:

int a[3][3];

7. Identify the main elements of an array declaration.

When declaring an array in C, the declaration contains the following main elements:

  1. Data Type
    • Specifies the type of elements the array will store.
    • Example: int, float, char.
  2. Array Name
    • The identifier used to access the array.
    • Example: marks, arr.
  3. Size (Subscript/Dimension)
    • Specifies the number of elements (for 1D) or rows & columns (for 2D).
    • Example: [5], [3][3].

8. Differentiate between one dimension array and two-dimension array.

One-Dimensional Array (1D) Two-Dimensional Array (2D)
Stores data in a linear (single row) form. Stores data in a tabular (rows & columns) form.
Declared as: datatype arrayName[size]; Declared as: datatype arrayName[rows][cols];
Accessed using one subscript → arr[i]. Accessed using two subscripts → arr[i][j].
Example: int marks[5]; Example: int matrix[3][3];
Suitable for storing a list (e.g., marks of students). Suitable for storing tables/matrices (e.g., multiplication table).

Ad Space