string.h Header File in C: Full Explanation & Rules | Unit 3 | BKNMU BCA



BCA Sem 1  |  Unit 3  |  Computer Concepts & C Programming

Overview of string.h Header File in C — BKNMU Junagadh

Detailed breakdown of string manipulation functions, length, copying, concatenation, comparison, and null-termination rules

BKNMU Junagadh NEP 2020 Unit 3 string.h Explained String Utilities Null Terminator
In simple words: Welcome to Unit 3! In C programming, strings are not a distinct data type like integers or floats. Instead, they are 1D character arrays terminated by a special character called the null terminator ('\0'). Since they are arrays, basic operators (like =, +, ==) cannot work directly on them. To handle common tasks like copying one string to another or comparing two strings, C provides a powerful standard library declared in #include <string.h>. This post provides a clear overview of these utilities for **BKNMU BCA Semester 1 (Unit 3)**.
📖 Core String Utilities Defined in string.h

The string.h library organizes predefined functions into four main categories, corresponding to the quadrants in your thumbnail:

strlen()

1. String Length

Calculates the total number of characters in a string, strictly **excluding** the null terminator ('\0').

strcpy()

2. String Copy

Used to copy the complete contents of one source string character array into a destination character array.

strcat()

3. String Concatenate

Used to physically join (append) the contents of one string to the end of another existing string.

strcmp()

4. String Compare

Used to check the lexicographical (alphabetical) relationship between two strings, returning integer results.

1️⃣ Master Table: Commonly Used string.h Functions

Here is a detailed breakdown of the primary functions supplied by the string.h standard library, detailing their parameters and behavior.

Function PrototypeFunctional DescriptionInput Parameters (Inputs)Return Value (Output)
size_t strlen(const char*)Calculates the number of characters in the string, not including '\0'.Pointer to the string character array.Length of the string as a size_t integer.
char* strcpy(char* dest, const char* src)Copies the source string character array content into the destination array buffer.Pointer to destination array (dest), Pointer to source string (src).Pointer to the resulting destination string.
char* strcat(char* dest, const char* src)Appends the source string contents to the end of the destination string.Pointer to destination string (dest), Pointer to source string (src).Pointer to the resulting destination string.
int strcmp(const char*, const char*)Alphabetically compares two strings. Comparison is case-sensitive (ASCII value based).Pointers to the two strings to compare.Returns 0 if strings equal; Negative if Str1 < Str2; Positive if Str1 > Str2.
char* strupr(char*)Converts all lowercase alphabets in the given string to uppercase. *(Note: This function is non-standard ANSI C, but supported in Turbo C/BKNMU exams)*Pointer to the string array.Pointer to the updated uppercase string.
ℹ️ The Null Terminator Rule: In C, every valid string character array **MUST** be terminated by the null character ('\0', ASCII 0). Without this character, string functions will continue reading memory indefinitely, causing crashes. Functions in string.h like strcpy and strcat automatically manage adding the null terminator.
💻 Code Examples & Practical Applications

1. Calculating Length using strlen():

#include <stdio.h>
#include <string.h> // Required for string functions

int main() {
    char city[20] = "Junagadh";
    int len;

    // Calculates length, excluding '\0'
    len = strlen(city);

    printf("String: %s\n", city);
    printf("Length of 'Junagadh' is: %d\n", len);

    return 0;
}

2. Using strcpy() and strcat() to Manipulate Strings:

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

int main() {
    char src[20] = "Hello ";
    char dest[40]; // Must have sufficient space
    char add[10] = "World!";

    // 1. Copy source string to destination
    strcpy(dest, src);
    printf("After strcpy: %s\n", dest); // Output: Hello 

    // 2. Concatenate (Append) third string to dest
    strcat(dest, add);
    printf("After strcat: %s\n", dest); // Output: Hello World!

    return 0;
}

3. Using strcmp() to Compare User Input:

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

int main() {
    char user_pw[20] = "bca@knmu";
    char input_pw[20];

    printf("Enter Password: ");
    scanf("%s", input_pw);

    // Compare two strings case-sensitively
    if (strcmp(user_pw, input_pw) == 0) {
        printf("Access Granted!\n");
    } else {
        printf("Access Denied.\n");
    }

    return 0;
}
🔄 Step-by-Step Flow: How String Functions Work
1

Step 1: Locate First Character Address

The function call provides the base address (pointer) of the character array starting character.

2

Step 2: Sequential Memory Scan

The predefined function scans sequentially, char by char, through memory, performing its primary task (counting length, copying, comparing, or appending).

3

Step 3: Null Terminator Detection

Crucial Step! The scan stops ONLY when the null terminator ('\0') is detected. If copying, it also copies the '\0' to the destination.

💡 Special BKNMU Exam Tip on strcmp Return Value: Remember GTU/BKNMU exam logic: strcmp() compares ASCII values.
  • Returns 0 if Str1 == Str2
  • Returns a NEGATIVE value if Str1 < Str2 (e.g., 'A' < 'B')
  • Returns a POSITIVE value if Str1 > Str2 (e.g., 'Z' > 'Y')
⚠️ Common Exam Pitfall: Destination Size Error: Forgetting destination array size is the #1 mistake! In functions like strcpy and strcat, you must ensure the destination character array has **sufficient allocated memory** to hold the resulting joined/copied string PLUS the null terminator. Overflows lead to fatal segmentation faults!
📝 Quick Revision — Key Exam Points
  • Statement Category: Standard Library Header (ansi c).
  • Valid Inclusion Scope: Include via #include <string.h> directive.
  • Primary Utilities: `strlen()`, `strcpy()`, `strcat()`, `strcmp()`, `strupr()`, `strlwr()`.
  • Data Handling Rule: Most parameters and return types use pointers (char*) and case-sensitive ASCII values.
  • Crucial Requirement: Must properly manage the null terminator ('\0') in all custom implementations.

Post a Comment

Thanks for comment.

Previous Post Next Post