Useful standard functions

<ctype.h>

Characters classification

Function
Description

int isalnum(int c);

Determines if the character is alphanumeric

int isalpha(int c);

Determines if the character is alphabetic

int isblank(int c);

Determines if the character is a blank character, i.e., space or horizontal tab ()

int iscntrl(int c);

Determines if the character is a control character

int isdigit(int c);

Determines if the character is a digit

int isgraph(int c);

Determines if the character is printable (excluding space)

int islower(int c);

Determines if the character is a lowercase letter

int isprint(int c);

Determines if the character is printable (including space)

int ispunct(int c);

Determines if the character is a punctuation character

int isspace(int c);

Determines if the character is a whitespace character, i.e., space, carriage return (), newline (), horizontal tab (), vertical tab (\v), form feed (\f)

int isupper(int c);

Determines if the character is an uppercase letter

int isxdigit(int c);

Determines if the character is a hexadecimal digit

Case-mapping

Function
Description

int tolower(int c);

Converts the character to lowercase

int toupper(int c);

Converts the character to uppercase

<string.h>

Function
Description

char *strcat(char *dest, const char *src);

Appends src to the end of dest and returns dest (If dest is not large enough to hold all of src, the remaining characters are copied beyond the end of the array)

char *strncat(char *dest, const char *src, size_t n);

Appends the first n characters of src to dest

size_t strlen(const char *s);

Returns the number of characters in s, excluding the null terminator (\0)

int strcmp(const char *s1, const char *s2);

Lexicographically compares two strings, returns:

  • < 0 if s1 < s2

  • 0 if s1 == s2

  • > 0 if s1 > s2

int strncmp(const char *s1, const char *s2, size_t n);

Compares the first n characters of s1 and s2

char *strchr(const char *s, int c);

Searches for the first occurrence of character c in string s:

  • If found, returns a pointer to the character

  • Otherwise, returns NULL

char *strrchr(const char *s, int c);

Searches for the last occurrence of character c in string s

char *strtok(char *str, const char *set);

Splits the string str into tokens separated by characters from set. Each call to strtok returns a different token:

  • Returns a pointer to a token (excluding characters from set)

  • When no more tokens are found, returns NULL

char *strstr(const char *src, const char *sub);

Searches for the first occurrence of the substring sub in src:

  • If found, returns a pointer to the first character of the match

  • Otherwise, returns NULL

char *strcpy(char *s1, const char *s2);

Copies s2 (up to and including the null terminator) into s1

char *strncpy(char *s1, const char *s2, size_t n);

Copies at most n characters from s2 (ending at the null terminator, if present) into s1

<stdlib.h>

qsort

void qsort(void *base, size_t nmemb,  size_t size,  int(*compar) (const void*,const void*) );

where:

  • base – pointer to the first element of the array

  • nmemb – number of elements in the array to be sorted

  • size – size of each element (in bytes)

  • compar – pointer to a function that compares two elements and returns:

    • i < 0 if the first argument precedes the second

    • i = 0 if they are "equal"

    • i > 0 if the second argument precedes the first

<stdio.h>

A program terminates its execution normally if:

  • The return statement of the main function is reached

  • Or the exit() function is called

exit

void exit(int status)

where:

  • status indicates whether the program terminated normally or not:

    • EXIT_SUCCESS – normal termination

    • EXIT_FAILURE – termination due to an error

When the exit() function is called:

  • All open streams are flushed using fflush()

  • All open streams (standard and non-standard) are closed

  • The _exit() function is called

  • All functions registered with atexit() and on_exit() are called in the reverse order of their registration

  • The process execution is terminated

atexit

int atexit( void (*function)(void) )

Registers the function function to be called during the normal termination of the program:

  • Returns 0 on success

  • Returns -1 on failure

on_exit

int on_exit( void (*function)(int, void *) , void  *arg)

Registers the function function to be called during normal program termination

  • Returns 0 on success

  • Returns -1 on failure

<setjmp.h>

The goto statement allows jumping to a label, but only if the label is within the same function. To overcome this limitation, functions from this library are used.

setjmp

int setjmp(jmp_buf env)								

It saves the current calling environment in the env variable for later use by longjmp()

longjmp

void longjmp(jmp_buf env, int val);

It returns to the point where the setjmp() method was invoked. To do so, it must have the same env parameter as the setjmp() call.

After restoring the environment represented by the env variable, longjmp() will return from the setjmp() call.

The value returned by setjmp() is the value of longjmp() (if val = 0, it returns 1).

Last updated