CSS Computer Science (Optional) — Paper-I, Section-A, Topic II subtopic. Complements the User-Defined Functions page.
#include <math.h> in C/C++, import math in Python)sqrt(), pow(), abs(), ceil(), floor() — from <math.h> / <cmath>strlen(), strcpy(), strcmp(), strcat() — from <string.h>printf(), scanf(), getchar(), putchar() — from <stdio.h>rand(), malloc(), free(), exit() — from <stdlib.h>isdigit(), isalpha(), toupper(), tolower() — from <ctype.h>| Standard Library Function | User-Defined Function |
|---|---|
| Provided by the language/compiler | Written by the programmer |
| Already tested and optimized | Needs to be tested by the programmer |
| Accessed via header/import | Declared and defined in the program |
#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.
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;
}