UPES - School of Computer Science
Cloud & Software Operations Cluster
stdio.h, ctype.h, stdlib.h, assert.h, stdarg.h, time.h
Lecture 24
Instructor: Mohsin F. Dar
Assistant Professor
BTech First Semester - C Programming
The C Standard Library is a collection of pre-written functions, macros, and type definitions that come with every C compiler.
💡 Remember: You don't need to write these functions yourself - just include the appropriate header file!
| Header File | Purpose |
|---|---|
| stdio.h | Standard Input/Output operations |
| ctype.h | Character type testing and conversion |
| stdlib.h | General utilities: memory, conversions, etc. |
| string.h | String manipulation functions |
| math.h | Mathematical functions |
| time.h | Date and time functions |
| assert.h | Debugging assertions |
| stdarg.h | Variable argument lists |
Most commonly used header file for input and output operations.
#include <stdio.h>
int main() {
FILE *fp;
char name[50];
int age;
// Console I/O
printf("Enter name: ");
scanf("%s", name);
// File operations
fp = fopen("data.txt", "w");
if(fp != NULL) {
fprintf(fp, "Name: %s\n", name);
fclose(fp);
}
return 0;
}
Functions for testing and converting character types.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'a';
char str[] = "Hello123";
// Testing characters
if(isalpha(ch))
printf("'%c' is alphabetic\n", ch);
// Converting case
printf("Uppercase: %c\n", toupper(ch));
// Checking string characters
for(int i = 0; str[i] != '\0'; i++) {
if(isdigit(str[i]))
printf("%c is a digit\n", str[i]);
}
return 0;
}
Provides general purpose functions including memory management, conversions, and more.
#include <stdio.h>
#include <stdlib.h>
int main() {
// Dynamic memory allocation
int *arr = (int*)malloc(5 * sizeof(int));
if(arr == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
// String to integer conversion
char str[] = "123";
int num = atoi(str);
printf("Number: %d\n", num);
// Random number generation
srand(time(NULL));
printf("Random: %d\n", rand() % 100);
free(arr); // Free allocated memory
return 0;
}
Used for debugging by testing assumptions in your code.
An assertion is a statement that a condition must be true at a certain point in your program. If the condition is false, the program terminates with an error message.
⚠️ Important: Assertions are typically disabled in production code by defining NDEBUG
#include <stdio.h>
#include <assert.h>
int divide(int a, int b) {
// Assert that divisor is not zero
assert(b != 0);
return a / b;
}
int main() {
int x = 10, y = 2;
// This will work fine
printf("Result: %d\n", divide(x, y));
// This will trigger assertion failure
// printf("Result: %d\n", divide(x, 0));
return 0;
}
// Output if assertion fails:
// Assertion failed: b != 0, file test.c, line 5
Allows functions to accept a variable number of arguments.
Example: The printf() function uses stdarg.h to accept variable number of arguments!
#include <stdio.h>
#include <stdarg.h>
// Function to calculate sum of variable numbers
int sum(int count, ...) {
va_list args;
int total = 0;
va_start(args, count); // Initialize
for(int i = 0; i < count; i++) {
total += va_arg(args, int); // Get next arg
}
va_end(args); // Clean up
return total;
}
int main() {
printf("Sum of 3 numbers: %d\n", sum(3, 10, 20, 30));
printf("Sum of 5 numbers: %d\n", sum(5, 1, 2, 3, 4, 5));
return 0;
}
Functions for manipulating and formatting dates and times.
#include <stdio.h>
#include <time.h>
int main() {
// Method 1: Using clock() to measure CPU time
clock_t start = clock();
// Some operation to measure
long sum = 0;
for (int i = 0; i < 1000000; i++) {
for (int i = 0; i < 1000000; i++) {
sum += i;
}
clock_t end = clock();
double cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Sum: %ld\n", sum);
printf("CPU time used: %f seconds\n", cpu_time_used);
// Method 2: Using time() to get calendar time
time_t current_time;
time(¤t_time);
printf("\nCurrent time: %s", ctime(¤t_time));
return 0;
}
Functions for mathematical operations.
#include <stdio.h>
#include <math.h>
int main() {
double x = -5.5, y = 3.2;
double result;
// Absolute value
result = abs(x);
printf("abs(%f) = %f\n", x, result);
// Square root
result = sqrt(y);
printf("sqrt(%f) = %f\n", y, result);
// Power function
result = pow(x, y);
printf("pow(%f, %f) = %f\n", x, y, result);
// Sine function
result = sin(x);
printf("sin(%f) = %f\n", x, result);
// Cosine function
result = cos(y);
printf("cos(%f) = %f\n", y, result);
// Tangent function
result = tan(x);
printf("tan(%f) = %f\n", x, result);
// Natural logarithm
result = log(y);
printf("log(%f) = %f\n", y, result);
// Exponential function
result = exp(x);
printf("exp(%f) = %f\n", x, result);
// Floor value
result = floor(y);
printf("floor(%f) = %f\n", y, result);
// Ceiling value
result = ceil(x);
printf("ceil(%f) = %f\n", x, result);
// Round value
result = round(y);
printf("round(%f) = %f\n", y, result);
// Floating-point remainder
result = fmod(x, y);
printf("fmod(%f, %f) = %f\n", x, y, result);
// Hypotenuse function
result = hypot(x, y);
printf("hypot(%f, %f) = %f\n", x, y, result);
// Error function
result = erf(x);
printf("erf(%f) = %f\n", x, result);
// Complementary error function
result = erfc(y);
printf("erfc(%f) = %f\n", y, result);
// Logarithmic gamma function
result = lgamma(x);
printf("lgamma(%f) = %f\n", x, result);
// Gamma function
result = tgamma(y);
printf("tgamma(%f) = %f\n", y, result);
return 0;
}