CSS Prepare

optional · CSS study notes

II.13 Standard Library Functions

⬆️ P-I.II. Programming Fundamentals · Question Bank · Chapter II (2016–2026)

🧠

Remember: Header → arguments → result → conditions.

Start here — what you should be able to do

  • Name the correct header and result for common library calls.
  • Explain domain, capacity and error handling.
  • Trace the recorded Java wrapper-method question.

Reading order: foundation checks → full notes and examples → exam traps → self-test without notes → past-paper answers. All sections below remain part of the topic; code examples are kept in full.

Foundation checks

Library contracts

FunctionCheck before callingRemember
sqrt(x)x ≥ 0 for a real result<math.h> in C; <cmath> and std::sqrt in C++.
strlen(s)Valid null-terminated stringCounts characters before the terminator.
strcpy / strcatSufficient writable destination spaceCapacity must include \0; overlap is not supported.
strcmp(a,b)Valid stringsResult is negative, zero or positive—not necessarily −1/0/+1.
isdigit / toupperEOF or an unsigned-char-representable valueConvert a possibly signed char to unsigned char first.
mallocValid requested sizeCheck failure; release with free.

Headers versus libraries: headers supply declarations/types/templates; object/library code supplies compiled definitions where needed. Inclusion is not the same as linking.

1. What Are Standard Library Functions

  • Pre-written, pre-tested functions bundled with a programming language's standard library
  • Save programmers from re-implementing common operations (math, string handling, I/O)
  • Accessed by including/importing the relevant header or module (e.g. #include <math.h> in C/C++, import math in Python)

2. Common Categories (C/C++ examples)

  • Math functions: sqrt(), pow(), abs(), ceil(), floor() — from <math.h> / <cmath>
  • String functions: strlen(), strcpy(), strcmp(), strcat() — from <string.h>
  • I/O functions: printf(), scanf(), getchar(), putchar() — from <stdio.h>
  • Utility functions: rand(), malloc(), free(), exit() — from <stdlib.h>
  • Character functions: isdigit(), isalpha(), toupper(), tolower() — from <ctype.h>

3. Standard Library vs User-Defined Functions

Standard Library FunctionUser-Defined Function
Provided by the language/compilerWritten by the programmer
Already tested and optimizedNeeds to be tested by the programmer
Accessed via header/importDeclared and defined in the program

4. Worked Example

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

int main() {
    double result = sqrt(25.0);      // math.h -> 5.0
    char name[20] = "Ali";
    strcat(name, " Khan");             // string.h -> "Ali Khan"
    printf("%.1f %s", result, name);  // stdio.h
    return 0;
}

This single program pulls standard library functions from three different headers — sqrt() from <math.h>, strcat() from <string.h>, and printf() from <stdio.h> — without the programmer having to implement any of them.

5. 💻 Code Examples: C & C++

The same task (compute a square root, build/modify a name) using C's traditional headers vs C++'s standard library equivalents:

C:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main(void) {
    double result = sqrt(25.0);           // <math.h>
    char name[20] = "ali";
    strcat(name, " khan");                  // <string.h>
    name[0] = toupper(name[0]);             // <ctype.h>
    int *arr = malloc(3 * sizeof(int));     // <stdlib.h>
    printf("%.1f %s\n", result, name);
    free(arr);                               // <stdlib.h>
    return 0;
}

C++:

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    double result = sqrt(25.0);            // <cmath>
    string name = "ali";
    name += " khan";                          // std::string's own operator+=, not strcat
    name[0] = toupper(name[0]);               // <cctype>
    sort(name.begin(), name.end());           // <algorithm> -- another standard library function
    cout << result << " " << name << endl;
    return 0;
}
  • C relies on header-provided functions (strcat, malloc/free for manual memory management); C++ often replaces them with safer class methods (std::string) and RAII, though the classic C headers (<cstring>, <cstdlib>) still work unchanged in C++

6. MCQ Traps

  • Standard library functions still need to be declared via header inclusion, not automatically available
  • strcpy() and = are not interchangeable for strings in C — arrays can't be assigned directly with =
  • malloc()/free() belong to <stdlib.h>, not <string.h>

7. Self-Test Questions — with answers

Q1. Which header declares sqrt in C?

Answer

  • <math.h>.
  • In C++, use <cmath> with std::sqrt.
  • On some C toolchains, linking the mathematics library is a separate build step.

Q2. Library function versus user-defined function?

Answer

  • A standard library supplies a standardised operation.
  • A user-defined function implements application-specific logic.
  • Both still have arguments, return values and preconditions that must be used correctly.

Q3. Which header supplies isdigit and toupper?

Answer

  • C: <ctype.h>.
  • C++: <cctype> with the appropriate std-qualified functions.
  • Pass EOF or an unsigned-char-representable value.

Q4. What does strcat do and which header declares it?

Answer

  • <string.h> in C; <cstring> in C++.
  • It appends the source string to the destination, including a final null terminator.
  • The destination must have sufficient space and must not overlap the source.

Q5. Why include a header if the function is already compiled?

Answer

  • The compiler needs its declaration and type information at the call site.
  • The linker connects the call to a compiled definition when required.
  • Some library components are inline or templates and need definitions available in headers.
  1. Name the header file needed to use sqrt() in C.
  2. Differentiate a standard library function from a user-defined function.
  3. Which header provides isdigit() and toupper()?
  4. Which header must be included to use strcat(), and what does it do?
  5. Why must a header be included even though the function itself is already compiled into the standard library?

8. 📜 Past-Paper Questions & MCQs (CSS 2016–2026)

2024 · Paper-I · Q5(a) · 6 marks

Recall cue: intValue truncates 3.5 to 3; compareTo reports that 3.5 is less than 4.5.

Q. Show the output of the Java code: Double x = 3.5; System.out.println(x.intValue()); System.out.println(x.compareTo(4.5));

Answer — key points

  • x.intValue() converts the Double to an int by dropping the fractional part → 3.
  • x.compareTo(4.5) returns a negative number when x is less than the argument → -1.
  • Output:
3
-1

Recall check and coverage

  • Explain the three learning outcomes at the top without looking.
  • Reproduce one example or trace and test a boundary case.
  • Answer each self-test, then compare with the key.
  • Attempt each recorded past-paper task on this page.

Recorded written references: 2024 · Paper-I · Q5(a) · 6 marks

Evidence limit: this page preserves the question records found in your Notion notes. Disputed labels and questions not checked against original PDFs remain flagged in the Question Bank; this is not a claim of exhaustive original-paper verification.