Return home   COMPUTER
 
  C Core     C Fun     C++     Printable Versions
C Language - Functions - Extract       © Lubos Jules Stary, 03/29/2002

CONTENTS

User Input and Output
     Keyboard Input
     Screen Output

DISK I/O
     FOPEN - Open File
     Basic File Functions
     Directory and File Control Routines

String Routines
     Basic String Routines
     Other String Routines

C Function Library
     Mathematical Functions
     Time Functions
     Memory Operations
     System Controls Routines

User Input and Output

Keyboard Input

Function

Description

getchar()

Reads a character from the keyboard. Buffer + echo.

getch()

Reads a character from the keyboard. Unbuffered, no echo. (No ANSI standard)

getche()

Reads a character from the keyboard. Unbuffered, + echo. (No ANSI standard)

gets()

Reads a string from the keyboard. Buffer + echo. '\n' changes to '\0'

scanf()

Reads a string from the keyboard. Buffer + echo. Formatted input

kbhit()

Check the keyboard for key pressing. Return: not 0 == key pressed.
int _kbhit( void );

Screen Output

Function

Description

putchar()

Writes a character to the screen.

puts()

Writes a string to the screen. Adds a new line character.

printf()

Writes a string to the screen. Formatted output.

DISK I/O

FOPEN - Open File

mode

description

r

Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

w

Opens the empty file for writing. If the file doesn't exist, it's created. If the file exists, it's destroyed.

a

Opens the file for appending. If the file doesn't exist, it's created. If the file exists, new data is appended to the end of file.

r+

Opens the file for reading and writing. If the file doesn't exist, it's created. If the file exists, new data is written to the beginning of the file, overwriting existing data.

w+

Opens the file for reading and writing. If the file doesn't exist, it's created. If the file exists, it's destroyed.

a+

Opens the file for reading and appending. If the file exists, new data is appended to the end of file.

Second character:

mode

description

t

Open in text mode - default.

b

Open in binary mode.

Basic File Functions

Function

Description

fopen()

The file open. Return: NULL == error
FILE *fopen(char *filename, char *mode);

fclose()

The file close. Return: EOF == error, 0 == O.K.
int fclose(FILE *stream);

fprintf()

Write a formatted string to the file. Return: -1 == error, X == written bytes
int fprintf(FILE *stream, char *format [, argument ]...);

fscanf()

Read a formatted string from the file. Return: EOF == error, X == read bytes
int fscanf(FILE *stream, char *format [, argument ]...);

fputs()

Write a string to the file. Return: EOF == error
int fputs(char *string, FILE *stream);

fgets()

Read a string from the file. n == max. read bytes, read until newline or n-1 charcters. Return: NULL == error
char *fgets(char *string, int n, FILE *stream);

fwrite()

Write a binary data to the file. Write the buffer, size == size of 1 item, count == number of items. Return: X == number of written items
int fwrite(void *buffer, int size, int count, FILE *stream );

fread()

Read a binary data from the file. Read to the buffer, size == size of 1 item, count == number of items. Return: X == number of read items
int fread(void *buffer, int size, int count, FILE *stream );

fflush()

Writes the disk buffer to the disk. Return: EOF == error
int fflush(FILE *stream);

ftell()

Read file position indicator. Return: -1 == error, X == position
long ftell(FILE *stream)

fseek()

Move the file position indicator. offset == distance of moving, origin == relative starting point (SEEK_SET == from the beginning, SEEK_CUR == from the current position). Return: 0 == O.K.
int fseek(FILE *stream, long offset, int origin);

remove()

Delete file. Return: 0 == O.K.
int remove(char *path);

feof()

Detecting End of file. Return: 0 == O.K., nonzero == end of file
int feof(FILE *stream);

errno

Variable with stored error code from last operation.

strerror()

Get a system error message.
char *strerror(int errno);

clearerr()

Resets an error indicator for a stream.
void clearerr( FILE *stream );

Important constants (values for Windows 32 bit):

_MAX_PATH=260; /* Maximum length of full path name */
_MAX_FNAME=256; /* Maximum length of filename */
_MAX_EXT=256; /* Maximum length of extension name */
_MAX_DIR=256; /* Maximum length of directory name */

Directory and File Control Routines

direct.h, io.h

Function

Description

chdir()

Change the current working directory.

chdrive()

Change the current drive.

mkdir()

Make the new directory.

rmdir()

Remove the directory.

findfirst()

Find the file with specified attributes.

findnext()

Find the next file with specified attributes.

String Routines

Routines are defined in string.h.

Basic String Routines

Function Description

strlen()

Get the length of a string == number of chars without null character.. size_t == unsigned int.
size_t strlen(char *string);

strcpy()

Copy a string from strSource to strDest including null character. Return: strDest.
char *strcpy(char *strDest, char *strSource);

strcat()

Append a string (concatenation). Append strSource onto the end of strDest. Return: strDest.
char *strcat(char *strDest, char *strSource);

strcmp()

Compare strings. Comparing process ends by the first different chars, and compare ASCII value of these chars. Return: 0 == identical strings, <0 == string1 < string2, >0 == string1 > string2.
int strcmp(char *string1, char *string2);

strstr()

Find a first occurence of str2 in str1. Return: pointer to first char of substring, NULL == not found.
char *strstr(char *str1, char *str2);

strlwr()

Converts all the letter characters in str from uppercase to lowercase. It changes str! Return: pointer to str.
char *strlwr(char *str);

strupr()

Converts all the letter characters in str from lowercase to uppercase . It changes str! Return: pointer to str.
char *strupr(char *str);

atoi()

Conversion - ASCII to integer. Spaces are ignored, conversion stops by unconvertible character encountering. Return: X == converted value, 0 == error.
int atoi(char *string);

itoa()

Conversion - integer to ASCII. radix == base of value (10,16,2). Return: pointer to str.
char *itoa(int value, char *str, int radix);

isalpha()

Returns TRUE if ch is a letter. int isalpha(int ch);

isdigit()

Returns TRUE if ch is a digit. int isdigit(int ch);

isspace()

Returns TRUE if ch is a white space character - space, tab, vertical tab, line feed, form feed, or carriage return. int isspace(int ch);

sprintf()

Write formatted data to the string. Return: X == written characters
int sprintf(char *buffer, char *format [, argument] ... );

sscanf()

Read formatted data from the string. Return: EOF == error, X == read and converted fields
int sscanf(char *buffer, char *format [, argument ] ... );

Other String Routines

Function

Description

strncpy()

Copy N characters from Source to Dest.

strncat()

Append a N characters from strSource.

strchr()

Find a first occurence of the character in a string. Return: pointer to char.

strrchr()

Find a last occurence of the character in a string. Return: pointer to char.

strcspn()

Find a first occurence of any character from string2 into string1.

strspn()

Find a first character that doesn't match a character in string2.

C Function Library

Mathematical Functions

math.h

Function

Description

abs()

Returns absolute value

modf()

Splits x into integral and fractional parts. double modf(double x, double *i)

fmod()

Returns the floating-point remainder of x/y.

srand()

Sets a random starting point.

rand()

Generates a pseudorandom number.

Time Functions

time.h

Variables:

/* MS-DOS time = number of seconds since 01.01.1970 00:00:00 */
typedef long time_t, clock_t; 
/* Formatted time */
struct tm {
    int tm_sec;     /* seconds after the minute - [0,59]  */
    int tm_min;     /* minutes after the hour - [0,59]    */
    int tm_hour;    /* hours since midnight - [0,23]  */
    int tm_mday;    /* day of the month - [1,31]      */
    int tm_mon;     /* months since January - [0,11]      */
    int tm_year;    /* years since 1900               */
    int tm_wday;    /* days since Sunday - [0,6]      */
    int tm_yday;    /* days since January 1 - [0,365] */
    int tm_isdst;   /* daylight savings time flag     */
};
Function Description

time()

Returns the system time in time_t (longint) format.
time_t time(time_t *timeptr);

localtime()

Converts the time_t format to the tm format. Return: pointer to internal tm struct.
struct tm *localtime(time_t *ptr);

mktime()

Converts the tm format to the time_t format.
time_t mktime(struct tm *ntime);

strftime()

Converts time to string. max == length of strDest string, fmt == format string, ptr == pointer to tm time. Return: strDest == output string, int value == string length / 0 - more characters then max.
size_t strftime(char *strDest, size_t max, char *fmt, struct tm *ptr);

difftime()

Calculates time difference between two dates. Return: number of seconds.
double difftime(time_t later, time_t earlier);

clock()

Returns the time amount from program starting in 1/100 sec.
clock_t clock(void);

Time format specifiers

Specifier

Description

%a

Abbreviated weekday name.

%A

Full weekday name.

%b

Abbreviated month name.

%B

Full month name.

%c

Date and time representation (for example, 10:41:50 30-Jun-91).

%d

Day of month as a decimal number 01 through 31.

%H

The hour (24-hour clock) as a decimal number 00 through 23.

%I

The hour (12-hour clock) as a decimal number 00 through 11.

%j

The day of the year as a decimal number 001 through 366.

%m

The month as a decimal number 01 through 12.

%M

The minute as a decimal number 00 through 59.

%p

AM or PM.

%S

The second as a decimal number 00 through 59.

%U

The week of the year as a decimal number 00 through 53. Sunday is considered the first day of the week.

%w

The weekday as a decimal number 0 through 6 (Sunday = 0).

%W

The week of the year as a decimal number 00 through 53. Monday is considered the first day of the week.

%x

The date representation (for example, 30-Jun-91).

%X

The time representation (for example, 10:41:50).

%y

The year, without century, as a decimal number 00 through 99.

%Y

The year, with century.

%Z

The time zone name if the information is available or blank if not.

Memory Operations
stdlib.h, malloc.h
Function Description

malloc()

Allocates num bytes of memory. Return: pointer to the first byte, NULL == error.
void *malloc(size_t num);

free()

Free the block of memory previously allocated by malloc().
void free(void *ptr);

memcpy()

Copy data from one memory block to the other. Return: *dest.
void *memcpy(void *dest, void *src, size_t count);

memchr()

Finds the first occurence of char in memory block. ch == char, count == number of bytes to be searched. Return: pointer to found char, NULL == not found.
void *memchr(void *buf, int ch, size_t count );

calloc()

Allocates the block of memory (size * num bytes), initialize it to 0. Return: pointer to the first byte, NULL == error.
void *calloc( size_t num, size_t size );

System Controls Routines

process.h

Function

Description

execl()

Execute external program.

exit()

Terminates program.

system()

Execute the operating system command.

inp()

Read one byte from I/O port.

outp()

Write one byte from I/O port.