C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
ASSERT.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* ASSERT.C illustrates:
 *      assert
 */
 
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <assert.h>
 
#define MAXSTR 120
 
void chkstr( char *string );    /* Prototype */
 
void main()
{
    char string1[MAXSTR], string2[MAXSTR];
 
    /* Do various processes on strings and check the results. If
     * none cause errors, force an error with an empty string.
     */
    printf( "Enter a string: " );
    gets( string1 );
    chkstr( string1 );
 
    printf( "Enter another string: " );
    gets( string2 );
    chkstr( string2 );
 
    strcat( string1, string2 );
    chkstr( string1 );
    printf( "string1 + string2 = %s\n", string1 );
 
    chkstr( "" );
    printf( "You'll never get here\n" );
}
 
/* Tests a string to see if it is NULL, empty, or longer than MAXSTR. */
void chkstr( char *string )
{
    assert( string != NULL );               /* Cannot be NULL           */
    assert( *string != '\0' );              /* Cannot be empty          */
    assert( strlen( string ) < MAXSTR );    /* Length less than maximum */
}
                                    -♦-