0% found this document useful (0 votes)
73 views

Other C Functions With Funcion Pointers

The document provides an overview of various standard C functions including: - abort() terminates the current program immediately. - assert() tests for errors and exits the program if the expression is false. - atexit() registers functions to be called when the program exits. - bsearch() performs a binary search on sorted data. - exit() stops program execution and returns an exit code.

Uploaded by

Ariel Liguori
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Other C Functions With Funcion Pointers

The document provides an overview of various standard C functions including: - abort() terminates the current program immediately. - assert() tests for errors and exits the program if the expression is false. - atexit() registers functions to be called when the program exits. - bsearch() performs a binary search on sorted data. - exit() stops program execution and returns an exit code.

Uploaded by

Ariel Liguori
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

cppreference.

com > Other Standard C Functions

Other Standard C Functions


abort
Syntax:
#include <stdlib.h>
void abort( void );

The function abort() terminates the current program. Depending on the implementation,
the return value can indicate failure.

Related topics:
assert
atexit
exit

assert
Syntax:
#include <assert.h>
void assert( int exp );

The assert() macro is used to test for errors. If exp evaluates to zero, assert() writes
information to STDERR and exits the program. If the macro NDEBUG is defined, the
assert() macros will be ignored.

Related topics:
abort

atexit
Syntax:
#include <stdlib.h>
int atexit( void (*func)(void) );

The function atexit() causes the function pointed to by func to be called when the
program terminates. You can make multiple calls to atexit() (at least 32, depending on
your compiler) and those functions will be called in reverse order of their establishment.
The return value of atexit() is zero upon success, and non-zero on failure.

Related topics:
abort
exit

bsearch
Syntax:
#include <stdlib.h>
void *bsearch( const void *key, const void *buf, size_t num, size_t
size, int (*compare)(const void *, const void *) );

The bsearch() function searches buf[0] to buf[num-1] for an item that matches key,
using a binary search. The function compare should return negative if its first argument
is less than its second, zero if equal, and positive if greater. The items in the array buf
should be in ascending order. The return value of bsearch() is a pointer to the matching
item, or NULL if none is found.

Related topics:
qsort

exit
Syntax:
#include <stdlib.h>
void exit( int exit_code );

The exit() function stops the program. exit_code is passed on to be the return value of
the program, where usually zero indicates success and non-zero indicates an error.

Related topics:
abort
atexit
system

getenv
Syntax:
#include <stdlib.h>
char *getenv( const char *name );

The function getenv() returns environmental information associated with name, and is
very implementation dependent. NULL is returned if no information about name is
available.

Related topics:
system

longjmp
Syntax:
#include <setjmp.h>
void longjmp( jmp_buf envbuf, int status );

The function longjmp() causes the program to start executing code at the point of the
last call to setjmp(). envbuf is usually set through a call to setjmp(). status becomes the
return value of setjmp() and can be used to figure out where longjmp() came from.
status should not be set to zero.

Related topics:
setjmp

qsort
Syntax:
#include <stdlib.h>
void qsort( void *buf, size_t num, size_t size, int (*compare)(const
void *, const void *) );

The qsort() function sorts buf (which contains num items, each of size size) using
Quicksort. The compare function is used to compare the items in buf. compare should
return negative if the first argument is less than the second, zero if they are equal, and
positive if the first argument is greater than the second. qsort() sorts buf in ascending
order.

Related topics:
bsearch

raise
Syntax:
#include <signal.h>
int raise( int signal );

The raise() function sends the specified signal to the program. Some signals:

Signal Meaning

SIGABRT Termination error

SIGFPE Floating pointer error

SIGILL Bad instruction

SIGINT User presed CTRL-C

SIGSEGV Illegal memory access

SIGTERM Terminate program

The return value is zero upon success, nonzero on failure.

Related topics:
signal

rand
Syntax:
#include <stdlib.h>
int rand( void );

The function rand() returns a pseudorandom integer between zero and RAND_MAX.
An example:

srand( time(NULL) );
for( i = 0; i < 10; i++ )
printf( "Random number #%d: %d\n", i, rand() );
Related topics:
srand

setjmp
Syntax:
#include <setjmp.h>
int setjmp( jmp_buf envbuf );

The setjmp() function saves the system stack in envbuf for use by a later call to
longjmp(). When you first call setjmp(), its return value is zero. Later, when you call
longjmp(), the second argument of longjmp() is what the return value of setjmp() will
be. Confused? Read about longjmp().

Related topics:
longjmp

signal
Syntax:
#include <signal.h>
void ( *signal( int signal, void (* func) (int)) ) (int);

The signal() function sets func to be called when signal is recieved by your program.
func can be a custom signal handler, or one of these macros (defined in signal.h):

Macro Explanation

SIG_DFL default signal handling

SIG_IGN ignore the signal

Some basic signals that you can attach a signal handler to are:
Signal Description

SIGTERM Generic stop signal that can be caught.

SIGINT Interrupt program, normally ctrl-c.

SIGQUIT Interrupt program, similar to SIGINT.

SIGKILL Stops the program. Cannot be caught.

SIGHUP Reports a disconnected terminal.

The return value of signal() is the address of the previously defined function for this
signal, or SIG_ERR is there is an error.

Example code:

The following example uses the signal() function to call an arbitrary number of
functions when the user aborts the program. The functions are stored in a vector, and a
single "clean-up" function calls each function in that vector of functions when the
program is aborted:

void f1() {
cout << "calling f1()..." << endl;
}
void f2() {
cout << "calling f2()..." << endl;
}
typedef void(*endFunc)(void);
vector<endFunc> endFuncs;
void cleanUp( int dummy ) {
for( unsigned int i = 0; i < endFuncs.size(); i++ ) {
endFunc f = endFuncs.at(i);
(*f)();
}
exit(-1);
}
int main() {
// connect various signals to our clean-up function
signal( SIGTERM, cleanUp );
signal( SIGINT, cleanUp );
signal( SIGQUIT, cleanUp );
signal( SIGHUP, cleanUp );
// add two specific clean-up functions to a list of functions
endFuncs.push_back( f1 );
endFuncs.push_back( f2 );
// loop until the user breaks
while( 1 );
return 0;
}
Related topics:
raise
srand
Syntax:
#include <stdlib.h>
void srand( unsigned seed );

The function srand() is used to seed the random sequence generated by rand(). For any
given seed, rand() will generate a specific "random" sequence over and over again.

srand( time(NULL) );
for( i = 0; i < 10; i++ )
printf( "Random number #%d: %d\n", i, rand() );
Related topics:
rand
(Standard C Date & Time) time

system
Syntax:
#include <stdlib.h>
int system( const char *command );

The system() function runs the given command as a system call. The return value is
usually zero if the command executed without errors. If command is NULL, system()
will test to see if there is a command interpreter available. Non-zero will be returned if
there is a command interpreter available, zero if not.

Related topics:
exit
getenv

va_arg
Syntax:
#include <stdarg.h>
type va_arg( va_list argptr, type );
void va_end( va_list argptr );
void va_start( va_list argptr, last_parm );

The va_arg() macros are used to pass a variable number of arguments to a function.

1. First, you must have a call to va_start() passing a valid va_list and the
mandatory first argument of the function. This first argument describes the
number of parameters being passed.
2. Next, you call va_arg() passing the va_list and the type of the argument to be
returned. The return value of va_arg() is the current parameter.
3. Repeat calls to va_arg() for however many arguments you have.
4. Finally, a call to va_end() passing the va_list is necessary for proper cleanup.

For example:
int sum( int, ... );
int main( void ) {
int answer = sum( 4, 4, 3, 2, 1 );
printf( "The answer is %d\n", answer );
return( 0 );
}
int sum( int num, ... ) {
int answer = 0;
va_list argptr;
va_start( argptr, num );
for( ; num > 0; num-- )
answer += va_arg( argptr, int );
va_end( argptr );
return( answer );
}

This code displays 10, which is 4+3+2+1.

You might also like