0% found this document useful (0 votes)
6 views13 pages

C Standard Library Functions Overview

The document provides a comprehensive overview of various C standard library functions, including their descriptions, syntax, and examples. It covers functions for file handling, memory management, string manipulation, and mathematical operations. Each function is presented in a structured format for easy reference.

Uploaded by

joy112833
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

C Standard Library Functions Overview

The document provides a comprehensive overview of various C standard library functions, including their descriptions, syntax, and examples. It covers functions for file handling, memory management, string manipulation, and mathematical operations. Each function is presented in a structured format for easy reference.

Uploaded by

joy112833
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Function Description Syntax Example

<stdio.h>

fclose(fp); // closes the file


if(fclose(fp)==0) printf("Closed\n");
int fclose(FILE
fclose() Closes a file // success
*stream);
fclose(stdin); // closes standard
input

while(!feof(fp)) ch = fgetc(fp); //
int feof(FILE
feof() Checks EOF read until EOF
*stream);
if(feof(fp)) printf("End reached");

if(ferror(fp)) printf("Error
Checks file int ferror(FILE
ferror() reading\n");
error *stream);
clearerr(fp); // reset error

ch = fgetc(fp); // read one char


int fgetc(FILE
fgetc() Reads a char while((ch=fgetc(fp))!=EOF)
*stream);
putchar(ch); // print file content

char fgets(buffer, 50, fp); // read 1


*fgets(char line
fgets() Reads a line
*str, int n, while(fgets(line,100,fp))
FILE *stream); printf("%s", line);

FILE
*fopen(const
char *filename,
const char fp = fopen("[Link]","r"); // read
*mode); mode
fopen() Opens a file
modes: r=read, fp = fopen("[Link]","wb");
w=write, fp = fopen("[Link]","a");
a=append,
r+=read/write,
w+=overwrite

int
fprintf(FILE fprintf(fp,"Hello %d", 5);
Writes
fprintf() *stream, const fprintf(stdout,"Value: %f\n",3.14);
formatted file
char *format, fprintf(log,"Error code: %d\n",err);
...);
Function Description Syntax Example

int fputc(int
fputc('A', fp); // write char
fputc() Writes a char char, FILE
while(i<10) fputc('x', fp);
*stream);

int fputs(const
Writes a fputs("Hello\n", fp);
fputs() char *str, FILE
string fputs(buffer, stdout);
*stream);

size_t
Reads fread(void *ptr,
fread(&data,sizeof(int),10,fp);
fread() memory size_t size,
fread(buffer,1,50,fp);
from file size_t nmemb,
FILE *stream);

int fscanf(FILE
Reads *stream, const fscanf(fp,"%d",&num);
fscanf()
formatted file char *format, fscanf(fp,"%s %f",name,&val);
...);

int fseek(FILE
*stream, long
offset, int
fseek(fp,0,SEEK_SET); // start
Moves file whence);
fseek() fseek(fp,10,SEEK_CUR); // move 10
pointer whence:
bytes forward
SEEK_SET,
SEEK_CUR,
SEEK_END

pos = ftell(fp); // get current


long ftell(FILE
ftell() Tells position position
*stream);
printf("%ld\n",ftell(fp));

size_t
fwrite(const
Writes
void *ptr, fwrite(&data,sizeof(int),10,fp);
fwrite() memory to
size_t size, fwrite(buffer,1,50,fp);
file
size_t nmemb,
FILE *stream);
Function Description Syntax Example

int getc(FILE ch = getc(fp);


getc() Reads a char *stream); // while((ch=getc(fp))!=EOF)
same as fgetc() putchar(ch);

ch = getchar(); // user input


int
getchar() Input char while((ch=getchar())!='\n')
getchar(void);
putchar(ch);

int
Output printf(const printf("Hello %d\n",5);
printf()
formatted char *format, printf("Value: %f\n",3.14);
...);

int putc(int
char, FILE putc('A',fp);
putc() Writes a char
*stream); // while(i<5) putc('x',fp);
same as fputc()

putchar('A');
int putchar(int
putchar() Output char while((ch=getchar())!='\n')
char);
putchar(ch);

int puts(const puts("Hello");


puts() Output string
char *str); puts(buffer);

int
remove(const
remove() Delete file remove("[Link]"); // deletes file
char
*filename);

int
rename(const
rename() Rename file rename("[Link]","[Link]");
char *old, const
char *new);

void
Reset file
rewind() rewind(FILE rewind(fp); // move to start
pointer
*stream);

int scanf(const
Input scanf("%d",&num);
scanf() char *format,
formatted scanf("%s %f",name,&val);
...);
Function Description Syntax Example

int
snprintf(char snprintf(buf,50,"Num: %d",10);
Format to
snprintf() *str, size_t snprintf(buf,sizeof(buf),"Hello
buffer safe
size, const char %s",name);
*format, ...);

int
Format to sprintf(char sprintf(buf,"Num: %d",10);
sprintf()
buffer *str, const char sprintf(buf,"Hello %s",name);
*format, ...);

int
sscanf(const
Read from sscanf("123 4.5","%d %f",&num,&val);
sscanf() char *str, const
buffer sscanf(buf,"%s %d",name,&num);
char *format,
...);

<stdlib.h>

printf("%d", abs(-10)); // 10
Absolute int abs(int
abs() int x = abs(5); // 5
value n);
printf("%d", abs(0)); // 0

double double d = atof("3.14"); // 3.14


String →
atof() atof(const char printf("%f", atof("0.5")); // 0.5
double
*str); double d = atof("-2.7"); // -2.7

int n = atoi("42"); // 42
int atoi(const
atoi() String → int printf("%d", atoi("-7")); // -7
char *str);
int x = atoi("0"); // 0

long l = atol("123456"); // 123456


long atol(const
atol() String → long printf("%ld", atol("-987")); // -987
char *str);
long x = atol("0"); // 0

long long ll = atoll("1234567890");


long long // 1234567890
String → long
atoll() atoll(const char printf("%lld", atoll("-987654321"));
long
*str); // -987654321
long long x = atoll("0"); // 0
Function Description Syntax Example

int *arr = (int*)calloc(5,


void sizeof(int)); // array of 5 zeros
Allocate & *calloc(size_t double *d = (double*)calloc(3,
calloc()
zero nitems, size_t sizeof(double)); // 0.0
size); char *buf = (char*)calloc(10,
sizeof(char));

div_t result = div(10,3); printf("%d


%d", [Link], [Link]); // 3 1
div_t div(int
Quotient + result = div(-7,2); printf("%d %d",
div() numer, int
remainder [Link], [Link]); // -3 -1
denom);
result = div(0,5); printf("%d %d",
[Link], [Link]); // 0 0

exit(0); // terminates program


void exit(int exit(1); // terminates with error
exit() End program
status); code 1
if(err) exit(2);

int *p = malloc(sizeof(int));
free(p);
Deallocate void free(void
free() char *buf = calloc(10,1); free(buf);
memory *ptr);
double *arr =
malloc(5*sizeof(double)); free(arr);

int *arr =
(int*)malloc(5*sizeof(int));
void
Allocate char *buf =
malloc() *malloc(size_t
memory (char*)malloc(20*sizeof(char));
size);
double *d =
(double*)malloc(3*sizeof(double));

void qsort(void
int arr[5] = {5,2,4,1,3};
*base, size_t
qsort(arr,5,sizeof(int),cmpfunc);
nitems, size_t
char *strs[] = {"c","a","b"};
qsort() Sort array size, int
qsort(strs,3,sizeof(char*),cmpfunc);
(*compar)(const
double nums[] = {3.1,2.2,1.5};
void *, const
qsort(nums,3,sizeof(double),cmpfunc);
void *));
Function Description Syntax Example

printf("%d", rand()); // random int


Random int int r = rand() % 10; // 0-9
rand()
integer rand(void); for(int i=0;i<3;i++) printf("%d ",
rand());

int *arr = malloc(2*sizeof(int)); arr


void = realloc(arr, 5*sizeof(int));
Resize *realloc(void char *buf = calloc(5,1); buf =
realloc()
memory *ptr, size_t realloc(buf,10);
new_size); double *d = malloc(3*sizeof(double));
d = realloc(d, 6*sizeof(double));

srand(time(0)); printf("%d",
void rand());
srand() Seed RNG srand(unsigned srand(123); // predictable sequence
int seed); srand((unsigned)clock()); int r =
rand();

<string.h>

char arr[] = "hello";


void
char *p = memchr(arr,'l',5); //
*memchr(const
Finds byte in points to first 'l'
memchr() void *ptr, int
memory int nums[] = {1,2,3};
value, size_t
int *n = memchr(nums,2,sizeof(nums));
num);
// points to 2

int
memcmp(const
Compare char a[] = "abc"; char b[] = "abd";
void *ptr1,
memcmp() memory int r = memcmp(a,b,3); // -1
const void
blocks int x = memcmp(a,a,3); // 0
*ptr2, size_t
num);

void
char src[] = "hi"; char dest[3];
*memcpy(void
Copy memcpy(dest,src,3); // dest="hi"
memcpy() *dest, const
memory int arr1[]={1,2}, arr2[2];
void *src,
memcpy(arr2,arr1, sizeof(arr1));
size_t num);
Function Description Syntax Example

void
Copy *memmove(void
char str[] = "12345";
memmove() memory *dest, const
memmove(str+2,str,3); // str="12123"
safely void *src,
size_t num);

char arr[5]; memset(arr,'A',5); //


void
arr="AAAAA"
*memset(void
memset() Fill memory int nums[3];
*ptr, int value,
memset(nums,0,sizeof(nums)); // nums=
size_t num);
{0,0,0}

char
Append *strcat(char char a[10]="Hi "; strcat(a,"there");
strcat()
string *dest, const // a="Hi there"
char *src);

char
First char in *strchr(const char *p = strchr("hello",'l'); //
strchr()
string char *str, int points to first 'l'
c);

int
strcmp(const
Compare strcmp("abc","abd"); // -1
strcmp() char *str1,
strings strcmp("abc","abc"); // 0
const char
*str2);

int
strcoll(const
Locale strcoll("a","b"); // -1 (depends on
strcoll() char *str1,
compare locale)
const char
*str2);

char
*strcpy(char char a[5]; strcpy(a,"hey"); //
strcpy() Copy string
*dest, const a="hey"
char *src);
Function Description Syntax Example

size_t
strcspn(const
Length to
strcspn() char *str1, strcspn("hello","l"); // 2
char
const char
*str2);

char
perror("Error"); // prints
strerror() Error msg *strerror(int
corresponding error message
errnum);

size_t
strlen() String length strlen(const strlen("hello"); // 5
char *str);

char
*strncat(char
Append n char a[10]="Hi ";
strncat() *dest, const
chars strncat(a,"there",3); // a="Hi the"
char *src,
size_t n);

int
strncmp(const
Compare n char *str1, strncmp("abc","abd",2); // 0
strncmp()
chars const char strncmp("abc","abd",3); // -1
*str2, size_t
n);

char
*strncpy(char
char a[5]; strncpy(a,"hello",3); //
strncpy() Copy n chars *dest, const
a="hel"
char *src,
size_t n);

char
*strpbrk(const
First match strpbrk("hello","aeiou"); // points
strpbrk() char *str1,
char to 'e'
const char
*str2);
Function Description Syntax Example

char
Last char in *strrchr(const strrchr("hello",'l'); // points to
strrchr()
string char *str, int last 'l'
c);

size_t
strspn(const
Length of
strspn() char *str1, strspn("123abc","123"); // 3
initial set
const char
*str2);

char
*strstr(const
Find strstr("hello world","lo"); // points
strstr() char *haystack,
substring to 'lo world'
const char
*needle);

char
Tokenize *strtok(char char str[]="a,b,c"; char
strtok()
string *str, const char *p=strtok(str,","); // p="a"
*delim);

size_t
strxfrm(char
Transform char a[10]; strxfrm(a,"abc",10); //
strxfrm() *dest, const
string locale-based transform
char *src,
size_t n);

<math.h>

y = acos(0.5); // 1.0472 radians


Arccosine of double
acos(x) y = acos(-1); // 3.1416 radians
x acos(double x);
y = acos(1); // 0 radians

y = asin(0.5); // 0.5236 radians


double
asin(x) Arcsine of x y = asin(-1); // -1.5708 radians
asin(double x);
y = asin(1); // 1.5708 radians

y = atan(1); // 0.7854 radians


Arctangent double
atan(x) y = atan(-1); // -0.7854 radians
of x atan(double x);
y = atan(0); // 0 radians
Function Description Syntax Example

theta = atan2(1,1); // 0.7854


radians
double
Angle from theta = atan2(0, -1); // 3.1416
atan2(y, x) atan2(double y,
(x, y) radians
double x);
theta = atan2(-1,1); // -0.7854
radians

y = ceil(2.3); // 3
double
ceil(x) Round up y = ceil(-2.7); // -2
ceil(double x);
y = ceil(5.0); // 5

double y = floor(2.3); // 2
floor(x) Round down floor(double y = floor(-2.7); // -3
x); y = floor(5.0); // 5

y = fabs(-2.5); // 2.5
Absolute double
fabs(x) y = fabs(0); // 0
value fabs(double x);
y = fabs(3.7); // 3.7

double y = pow(2,3); // 8
pow(x, y) x^y pow(double x, y = pow(5,0); // 1
double y); y = pow(9,0.5); // 3

y = sqrt(16); // 4
double
sqrt(x) Square root y = sqrt(2); // 1.4142
sqrt(double x);
y = sqrt(0); // 0

y = exp(1); // 2.7183
double
exp(x) e^x y = exp(0); // 1
exp(double x);
y = exp(2); // 7.3891

y = log(2.7183); // 1
double
log(x) ln(x) y = log(1); // 0
log(double x);
y = log(10); // 2.3026

double y = log10(100); // 2
log10(x) log base 10 log10(double y = log10(10); // 1
x); y = log10(1); // 0

y = sin(0); // 0
double
sin(x) Sine of x y = sin(3.1416/2); // 1
sin(double x);
y = sin(3.1416); // 0
Function Description Syntax Example

y = cos(0); // 1
double
cos(x) Cosine of x y = cos(3.1416/2); // 0
cos(double x);
y = cos(3.1416); // -1

y = tan(0); // 0
double
tan(x) Tangent of x y = tan(3.1416/4); // 1
tan(double x);
y = tan(3.1416/2); // undefined

Character
<ctype.h>
handling

isalnum('A'); // 1
Checks int isalnum(int
isalnum() isalnum('9'); // 1
alphanumeric c);
isalnum('$'); // 0

isalpha('b'); // 1
int isalpha(int
isalpha() Checks letter isalpha('G'); // 1
c);
isalpha('3'); // 0

isblank(' '); // 1
Checks space int isblank(int
isblank() isblank('\t'); // 1
or tab c);
isblank('\n'); // 0

Checks iscntrl('\n'); // 1
int iscntrl(int
iscntrl() control iscntrl('\t'); // 1
c);
character iscntrl('A'); // 0

isdigit('5'); // 1
Checks int isdigit(int
isdigit() isdigit('a'); // 0
decimal digit c);
isdigit('0'); // 1

Checks isgraph('A'); // 1
int isgraph(int
isgraph() graphical isgraph(' '); // 0
c);
char isgraph('#'); // 1

Checks islower('g'); // 1
int islower(int
islower() lowercase islower('G'); // 0
c);
letter islower('9'); // 0

Checks isprint('A'); // 1
int isprint(int
isprint() printable isprint(' '); // 1
c);
char isprint('\n'); // 0
Function Description Syntax Example

ispunct('.'); // 1
Checks int ispunct(int
ispunct() ispunct(','); // 1
punctuation c);
ispunct('A'); // 0

isspace(' '); // 1
Checks int isspace(int
isspace() isspace('\t'); // 1
whitespace c);
isspace('A'); // 0

Checks isupper('G'); // 1
int isupper(int
isupper() uppercase isupper('g'); // 0
c);
letter isupper('3'); // 0

Checks int isxdigit('A'); // 1


isxdigit() hexadecimal isxdigit(int isxdigit('f'); // 1
digit c); isxdigit('G'); // 0

tolower('G'); // 'g'
Converts to int tolower(int
tolower() tolower('g'); // 'g'
lowercase c);
tolower('9'); // '9'

toupper('g'); // 'G'
Converts to int toupper(int
toupper() toupper('G'); // 'G'
uppercase c);
toupper('1'); // '1'

<time.h>

time_t time_t t = time(NULL); // current


Get current
time() time(time_t time in seconds since Jan 1, 1970
time
*t); time(&t); // store in variable t

struct tm *lt = localtime(&t); //


struct tm convert t to local time
Convert to
localtime() *localtime(const printf("%d-%d-%d", lt->tm_mday, lt-
local time
time_t *timep); >tm_mon+1, lt->tm_year+1900); //
formatted date

struct tm *gt = gmtime(&t); // UTC


struct tm
Convert to time
gmtime() *gmtime(const
UTC printf("%d:%d:%d", gt->tm_hour, gt-
time_t *timep);
>tm_min, gt->tm_sec); // UTC time
Function Description Syntax Example

char
Time as printf("%s", ctime(&t)); // Thu Jun
ctime() *ctime(const
string 26 10:30:00 2025
time_t *timep);

char struct tm *lt = localtime(&t);


struct tm →
asctime() *asctime(const printf("%s", asctime(lt)); // Thu Jun
string
struct tm *tm); 26 10:30:00 2025

size_t
strftime(char
*s, size_t max,
const char
*format, const char buf[100];
struct tm *tm); strftime(buf, 100, "%Y-%m-%d
strftime() Format time format codes: %H:%M:%S", lt);
%Y=year, printf("%s", buf); // 2025-06-26
%m=month, 10:30:00
%d=day,
%H=hour,
%M=minute,
%S=second

double time_t t1 = time(NULL);


Time difftime(time_t time_t t2 = t1 + 3600;
difftime()
difference end, time_t printf("%f", difftime(t2, t1)); //
start); 3600.000000

struct tm lt = {0};
time_t
struct tm → lt.tm_year = 125; lt.tm_mon = 5;
mktime() mktime(struct tm
time_t lt.tm_mday = 26;
*tm);
time_t t = mktime(&lt);

clock_t start = clock();


// some computation
clock_t clock_t end = clock();
clock() CPU ticks
clock(void); printf("%f", (double)(end-
start)/CLOCKS_PER_SEC); // time in
seconds

You might also like