------------------------------------------------------------------------------
MC logo
Unsigned Type
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Enumerated Type] [Typedef] [Unsigned Type] [Conversion Games]
uns.c
/*
 * Unsigned types can be tricky, because negatives are silently translated to
 * to trash.  A slightly negative value generally becomes a very large
 * positive number.
 */
#include <stdio.h>

main()
{
        int i;
        unsigned ui, petunia;

        /* See what happens when you convert from negative. */
        i = -15;
        ui = i;
        printf("The unsigned version of -15 is %u\n", ui);
        if(ui <= 0) printf("Is it likely?\n");
        putchar('\n');

        /* The comparison u1 <= petunia is performed correctly, but
           the subtraction ui - petunia is always forced to a non-
           negative value, so ui - petunia <= 0 is true only when
           the two values are equal. */
        ui = 4;
        petunia = 0;
        for(i = 4; i--;) {
                printf("ui = %u, petunia = %u\n", ui, petunia);

                printf("ui <= petunia: ");
                if(ui <= petunia) printf("yes.\n");
                else printf("no.\n");

                printf("ui - petunia <= 0: ");
                if(ui - petunia <= 0) printf("yes.\n");
                else printf("no.\n");

                putchar('\n');
                
                --ui;
                ++petunia;
        }
}