tags:

views:

521

answers:

9

It has to be in a 'system' C library, not an add-on library, but it can be from any C version/compiler/system/...

Mine's strfry, from glibc, the GNU C library:

   The  strfry()  function  randomizes  the  contents  of  string by using
   rand(3) to randomly swap characters in the string.  The  result  is  an
   anagram of string.
+1  A: 

drand48 for *nix is great when you need more than 32 bits of (pseudo) randomness.

James D
A: 

G'day,

While not a C library function on its own using GNU gperf will result in generated functions that are used in C code.

Perfect hashing can speed things up tremendously.

cheers, Rob

Rob Wells
A: 

strdupa(), from the GNU C library. This function is like strdup(), but memory is allocated on the stack, like alloca() does

dmityugov
A: 

_strupr and _strlwr appear to be non standard C functions (at least I think the names with the leading underscore hint that).

And if they are - they are definitely my most-used nonstandard C function.

bernhardrusch
+2  A: 

Actually, I think strdup is actually not in the C standard... the man page claims

CONFORMING TO
   strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001.  strndup(), strdupa(),
   and strndupa() are GNU extensions.

So, strdup would have to be my vote. It's really standard non-standard.

Anders Eurenius
+2  A: 

On the humor/obscurity aspect, GNU wins with _memfrob:

DESCRIPTION
   The memfrob() function encrypts the first n bytes of the memory area s
   by exclusive-ORing each character with the number 42.  The effect can
   be reversed by using memfrob() on the encrypted memory area.

A somewhat more useful function, though only on Win32, is _heapwalk from the Microsoft C library. To dump the heap contents:

void heapdump()
{
    _HEAPINFO hinfo;
    int heapstatus;
    hinfo._pentry = NULL;
    while ((heapstatus = _heapwalk(&hinfo)) == _HEAPOK)
     printf( "%6s block at %Fp of size %4.4X\n", (hinfo._useflag == _USEDENTRY ? "USED" : "FREE" ), hinfo._pentry, hinfo._size);

    switch (heapstatus) {
    case _HEAPEMPTY: printf( "OK - empty heap\n" ); break;
    case _HEAPEND: printf( "OK - end of heap\n" ); break;
    case _HEAPBADPTR:printf( "ERROR - bad pointer to heap\n" ); break;
    case _HEAPBADBEGIN: printf( "ERROR - bad start of heap\n" ); break;
    case _HEAPBADNODE: printf( "ERROR - bad node in heap\n" ); break;
    }
}
Antti Sykäri
A: 

_stricmp, _kbhit()

Will Dean
+2  A: 

asprintf will take in a pointer to a string and allocate sufficient space in that string to handle the result of your sprintf statement. Otherwise, you're stuck guessing at the amount of memory to allocate, or using some other alternative that is either unsafe or five or six lines of kludgey code just to write a darn string.

BSD has it, GNU's stdlib has it, the standard does not.

+2  A: 

My favorite is getline() which is a GNU libc extension: it reads a whole line from a file, dynamically resizing the output to fit the whole line (only if necessary).

Basically, it avoids the common C pitfall/kludge of using fgets() with a certain length, and then finding that overly long lines completely mess up your program's input routines.

GNU libc also has the converse for output, asprintf(), which does that same thing as sprintf() but dynamically allocates enough space to hold the result.

Dan