C Reference function realloc()

The function realloc() reallocates a memory block with a specific new size. If you call realloc() the size of the memory block pointed to by the pointer is changed to the given size in bytes. This way you are able to expand and reduce the amount of memory you want to use (if available of course.)

It is possible that the function moves the memory block to a new location, in which way the function will return this new location. If the size of the requested block is larger then the previous block then the value of the new portion is indeterminate.

If the pointer is NULL then the function will behave exactly like the function malloc(). It will assign a new block of a size in bytes and will return a pointer to it.

If the size is 0 then the memory that was previously allocated is freed as if a call of the function free() was given. It will return a NULL pointer in that case. More »


Posted in C Reference stdlib.h Functions | 6 Comments


C Reference function malloc()

The function malloc() will allocate a block of memory that is size bytes large. If the requested memory can be allocated a pointer is returned to the beginning of the memory block.

Note: the content of the received block of memory is not initialized.

Usage of malloc():

More »


Posted in C Reference stdlib.h Functions | 5 Comments


C Reference function free()

If you have allocated a memory block with the functions malloc(), calloc() or realloc() then you need to free the previously allocated memory.

Usage of free():

void free ( void * ptr );

Parameters:

The parameter is a pointer to a memory block that was allocated with malloc(), calloc() or realloc(). If you pass a null pointer then there will no action occur. More »


Posted in C Reference stdlib.h Functions | 5 Comments


C Reference function srand() initialize random number generator

This function of stdlib will initialize the random number generator that can be used with the rand() function.

Usage of srand():

void srand ( unsigned int seed );

The function srand() is used to initialize the pseudo-random number generator by passing the argument seed.
Often the function time is used as input for the seed. More »


Posted in C Reference stdlib.h Functions | 4 Comments


C Reference function rand() generate a random number

This function of stdlib will generate a random number.

Usage of rand():

int rand (void);

Parameters:

The function rand() returns a pseudo-random integral number.
This number will be in the range 0 to RAND_MAX. The algorithm of rand() uses a seed to generate the series of numbers, this is why srand must be used to initialize the seed to some distinctive value.

The constant RAND_MAX is defined in standard library (stdlib). More »


Posted in C Reference stdlib.h Functions | 4 Comments


C Reference function strtol()

This function of stdlib will convert a string to long integer.

Usage of strtol():

long int strtol ( const char * str, char ** endptr, int base );

Parameters:

C string str interpreting its content as an integral number of the specified base, which is returned as a long int value. If endptr is not a null pointer, the function also sets the value pointed by endptr to point to the first character after the number. White-spaces are discarded until the first non-whitespace character is found. More »


Posted in C Reference stdlib.h Functions | Comments Off on C Reference function strtol()


C Reference function strtoul()

This function of stdlib will convert a string to an unsigned long integer.

Usage of strtoul():

unsigned long int strtoul ( const char * str, char ** endptr, int base );

Parameters:

C string str interpreting its content as an unsigned integral number of the specified base, which is returned as an unsigned long int value. More »


Posted in C Reference stdlib.h Functions | Comments Off on C Reference function strtoul()


C Reference function atoi() convert a string to an integer

This function of stdlib will convert a string to an integer.

Usage of atoi():

int atoi ( const char * str );

Parameters:

C string str interpreting its content as a integer. White-spaces are discarded until the first non-whitespace character is found.

Return value:

The function returns the converted integral number as an int value, if successful. If no valid conversion could be performed then an zero value is returned. If the value is out of range then INT_MAX or INT_MIN is returned. More »


Posted in C Reference stdlib.h Functions | 15 Comments


C Reference function atof() convert a string to a double

This function of stdlib will convert a string to a double.

Usage of atof():

double atof ( const char * str );

Parameters:

C string str interpreting its content as a floating point number. White-spaces are discarded until the first non-whitespace character is found. A valid floating point number for atof is formed by: plus or minus sign, sequence of digits, decimal point and optional exponent part (character ‘e’ or ‘E’) More »


Posted in C Reference stdlib.h Functions | 1 Comment


C Reference function atol()

This function of stdlib will convert a string to a long integer.

Usage of atol():

long int atol ( const char * str );

Parameters:

C string str interpreting its content as a integer. White-spaces are discarded until the first non-whitespace character is found.

Return value:

More »


Posted in C Reference stdlib.h Functions | Comments Off on C Reference function atol()