<assert.h>: for adding diagnostics that aid
program debugging.<ctype.h>: for functions that test characters for
certain properties, and function prototypes for functions that can be
used to convert lowercase letters to uppercase letters and vice versa.
islower()toupper()<errno.h>: for reporting error conditions.<float.h>: Contains the floating point size
limits of the system.<limits.h>: Contains the integral size limits of
the system.<locale.h>: enables a program to be modified for
the current locale. This enables the computer system to handle different
conventions for expressing data like dates, times, money and large
numbers throughout the world.<math.h><setjmp.h>: for functions that allow bypassing of
the usual function call and return sequence.<signal.h>: handle various conditions that may
arise during program execution.<stdarg.h>: for dealing with a list of arguments
to a function whose number and types are unknown.<stddef.h>: Contains common definitions of types
used by C for performing certain calculations.<stdio.h><stdlib.h>: for conversions of numbers to text
and text to numbers, memory allocation, random numbers, and other
utility functions.<string.h>: for string processing functions.<time.h>: and types for manipulating the time and
date.Both the following functions are contained in
stdlib.h.
void* malloc(int nrOfBytes)
NULL pointer if it was unable to allocate the
memorytype* newptr = malloc (arraySize * sizeof( int ));
newptrvoid free(ptrType* pointerToMemory)
int* myIntAddress = malloc(sizeof(int));free(myIntAddress)FILE* filePtr = fopen("abc.xyz","r");fclose(filePtr);Common errors
Every block of memory allocated with malloc() should
eventually be returned to the pool of available memory by exactly one
call to free().
free() is not called for it. The memory it occupies cannot
be reused until the program terminates.free() for a block and
then continues to use the block. This can create a conflict with re-use
of the block through another malloc() call.free() is called more than once on a
block. Leads to undefined behavior. Can corrupt the state of the memory
manager.printfscanf%d: decimal integer%f: float, double (printf)%e: float, in scientific notation%lf: double (scanf)%c: char%s: string%4d: field width (left-pads with up to 4 spaces)%-4d: field width (right-pads with up to 4 spaces)%.4f: precision (print up to 4 decimal places)\n: Newline. Position the cursor at the beginning of
the next line.\t: Horizontal tab. Move the cursor to the next tab
stop.\a: Alert. Sound the system bell.\\: Backslash. Insert a backslash character in a
string.\": Double quote. Insert a double quote character in a
string.\’: Single quote. Insert a single quote into
string.\r: Position the cursor at the beginning of the current
line.\?: Insert a question mark character%%: Percentage symbolAll of the math.h functions return type
double.
sqrt(x)exp(x)log(x): natural logarithmlog10(x)fabs(x): absolute value of xceil(x): rounds x to the smallest integer
greater than xfloor(x): rounds x to the largest integer
less than xpow(x, y): x raised to power
yfmod(x, y): remainder of x/y as a floating
point numbersin(x): where x is in radians (all trig
functions)cos(x)tan(x)stdlib.hint rand(void) returns a random integer from
0 to RAND_MAX = 32767i = 2 + rand() % 5; is a pseudorandom number from
2 to 6 (= 5+2-1).rand() generates the same sequence of numbers for the
same seedsrand() to change the seed value:// to set the seed manually
unsigned int seed = 42; // unsigned implies non-negative
srand(seed);
// to set the seed automatically
srand( time( NULL) ); // need <time.h>
// to generate random numbers
fprintf("%d", rand());Opening files
#include <stdio.h>
FILE * filePtr;
filePtr = fopen( "someFilename.xyz", "r" );
// optional: check if file was opened
if ( filePtr == NULL ) {
printf( "ERROR - file could not be opened!\n" );
}
else{
// do stuff with the file
fclose( filePtr );
} filePtr that may point to a file.someFilename.xyz and let filePtr
points to its beginning."r": File is opened to read and as text fileFile open modes
r: Open a file for reading.w: Create a file for writing. If the file already
exists, empty it.a: Append; open/create a file for writing at end of
file.r+: Open a file for update (read/write).w+: Create a file for update (read/write). If the file
already exists, empty it.a+: Append; open/create a file for update; writing is
done at the end of the file.rb: Open a file for reading in binary mode.wb: Create a file for writing in binary mode. If the
file already exists, empty it.ab: Append; open/create a file for writing at end of
file in binary mode.rb+: Open a file for update (read/write) in binary
mode.wb+: Create a file for update in binary mode. If the
file already exists, empty it.ab+: Append; open/create a file for update in binary
mode; writing is done at the end of the fileFiles and Streams
FILE
structurestdio.h)stdio.h)FILE structure
Standard library functions (in stdio.h)
int fgetc( FILE *stream );
FILE pointer as an argumentfgetc( stdin ) equivalent to
getchar()int fputc( int char, FILE *stream );char* fgets( char *str, int count, FILE *stream );
int fputs(const char *str, FILE *stream);int fscanf( FILE *stream, const char *format, ... );
int fprintf( FILE *stream, const char *format, ... );
int feof( FILE *stream );
void rewind( FILE *stream );
Example:
#include <stdio.h>
#include <stdlib.h>
int main () {
char str1[10], str2[10], str3[10];
int year;
FILE * fp;
fp = fopen ("file.txt", "w+");
fputs("We are in 2018", fp);
rewind(fp);
fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);
// str1 = "We"
// str2 = "are"
// str3 = "in"
// year = "2018"
fclose(fp);
return(0);
}fwrite: Transfer bytes from a location in memory to a
filefread: Transfer bytes from a file to a location in
memoryEg: fwrite( &myObj, sizeof( egType ), 1, fPtr );
&myObj: Location to transfer bytes fromsizeof( egType ): Number of bytes to transfer1: For arrays, number of elements to transfer. In the
eg, only one element is being transferredfPtr: File to transfer to or fromSets file position pointer to a specific position
int fseek( FILE *stream, long int offset, int whence );
fwrite( &dataVariableToWrite, sizeof( dataVariable ), 1, filePtr );
fread( &dataVariableToRead, sizeof( dataVariable ), 1, filePtr );
// here 1 is the nr of elements to read; can be something elseSEEK_SET: seek starts at beginning of fileSEEK_CUR: seek starts at current location in fileSEEK_END: seek starts at end of file