NBC 103 • Unit 316 min readHigh Exam Frequency

Strings in C: Character Arrays & Standard Library <string.h>

Unit 3: Arrays, Pointers & String ManipulationProblem Solving Using C

👨‍🏫 Professor's Mental Model: The Train with a Red Caboose Guard Van

C me string koi alag data type nahi hai, balki characters ki ek train hai jiske sabse aakhri dibbe me hamesha ek red caboose laga hota hai jise Null Character ('\0') kehte hain! Compiler ko tabhi pata chalta hai ki train kahan khatam hui jab use '\0' milta hai. Agar aap '\0' lagana bhool gaye, toh printf aage ke random garbage memory characters ko print karta chala jayega jab tak use system me koi zero na mil jaye!

Academic Lecture Notes & Solved Study Pages

Unit 3 • Core Concepts, Step-by-Step Proofs & Notebook Solutions

3 Notebook Pages
NOTEBOOK PAGE 1 OF 3

1. String Representation & The Null Terminator ('\0')

Definition: A string in C is a 1D character array terminated by a null character `\0` (ASCII numerical value 0).
Storage Requirement: A string of $N$ printable characters requires $N + 1$ bytes of memory to store the trailing `\0`!

Example: `char str[] = "HELLO";` allocates 6 bytes in RAM (`'H'`, `'E'`, `'L'`, `'L'`, `'O'`, `'\0'`).

Character Array vs String Literal:
- `char s[] = "Hello";` : Modifiable character array stored on the Stack.
- `char *s = "Hello";` : Pointer referencing a string literal located in Read-Only Data Segment (modifying `s[0] = 'M'` causes a Segmentation Fault!).
NOTEBOOK PAGE 2 OF 3

2. Safe String Input: Why fgets() Replaces gets()

`scanf("%s", str)`: Stops reading at the first whitespace. Cannot read strings containing spaces (e.g. "Dennis Ritchie").
`gets(str)`: Reads entire line including spaces until newline. HIGHLY DANGEROUS: Does not check array capacity, allowing user input to overrun stack boundaries (Buffer Overflow).
`fgets(str, sizeof(str), stdin)`: The secure industry standard. Reads up to `sizeof(str) - 1` characters, safely guaranteeing null-termination and preventing buffer overflow.
NOTEBOOK PAGE 3 OF 3

3. Standard Library Functions in <string.h>

`strlen(s)`: Returns character count excluding the terminating `\0`.
`strcpy(dest, src)`: Copies source string into destination buffer (including `\0`).
`strcat(dest, src)`: Concatenates (appends) source string to the end of destination.
`strcmp(s1, s2)`: Lexicographically compares two strings:
- Returns 0 if $s_1 == s_2$
- Returns negative (< 0) if $s_1 < s_2$ (ASCII value of first mismatch is lower)
- Returns positive (> 0) if $s_1 > s_2$
`strrev(s)`: Reverses the string in-place (non-standard in GCC, standard in Turbo C).
Memory Architecture of char greeting[] = "DEV";
IndexCharacter ElementASCII ValueMemory AddressSignificance
greeting[0]'D'680x3000First character
greeting[1]'E'690x3001Second character
greeting[2]'V'860x3002Third character
greeting[3]'\0' (Null)00x3003Mandatory String Terminator Sentinel

Interactive Tested Code Example

#include <stdio.h>

// Custom strlen implementation without <string.h>
int my_strlen(const char *s) {
    int length = 0;
    while (*s != '\0') {
        length++;
        s++; // Advance pointer
    }
    return length;
}

// Custom strcpy implementation
void my_strcpy(char *dest, const char *src) {
    while (*src != '\0') {
        *dest = *src;
        dest++;
        src++;
    }
    *dest = '\0'; // Append mandatory null terminator
}

int main(void) {
    char source[] = "BCA Scholar";
    char destination[30];

    int len = my_strlen(source);
    my_strcpy(destination, source);

    printf("Source String:      %s\n", source);
    printf("Calculated Length:  %d characters\n", len);
    printf("Copied Destination: %s\n", destination);
    return 0;
}
💡 Note:Implements classic university exam questions: writing custom my_strlen() and my_strcpy() using pure pointer navigation without relying on <string.h>.

🎯 University Exam Scoring Blueprint

  • In university exams, you will often be asked to write custom implementations of strlen(), strcpy(), and strcmp() without using <string.h>.
  • Always remember to append '\0' at the end of the destination string when writing custom string copy or concatenation functions.
  • Explain that strcmp() compares ASCII values: 'A' (65) is smaller than 'a' (97).

Top Viva Questions on Strings in C: Character Arrays & Standard Library <string.h>

2 Questions
1

What is the ASCII value of the null character '\0' versus character '0'?

2

What happens if a character array has no '\0' terminator?