Miscellaneous

Exclusive OR

The bitwise exclusive or operation may be simulated by a combination of other bitwise operations.

    /* a XOR b == NOT(a AND b) AND (a OR b)                    */

    /* a and b have every combination of opposing bits         */

    unsigned char a = 0x03;               /* binary 0000 0011  */
    unsigned char b = 0x05;               /* binary 0000 0101  */

    /* note: == has higher precedence than the bitwise ops     */

    if ((a ^ b) == (~(a & b) & (a | b)))
        printf("a XOR b == NOT(a AND b) AND (a OR b)\n");

Logical Boolean

A logical expression evaluates to zero when it is false, and one when it is true.

    /* Assign a boolean value to an index                      */

    int i;
    char c = 'x', d = 'y', e = 'z';
    char *msg[2] = {"false", "true"};

    /* note that no parentheses are needed                     */

    i = c == 'a' && d == 'b' || e == 'c';

    printf("This relation is %s\n", msg[i]);         /* false  */

    i = c == 'a' && d == 'b' || e == 'z';

    printf("This relation is %s\n", msg[i]);         /* true   */

Command-line Arguments

An environment such as UNIX and the MS DOS Window provides a command-line for the user to enter a program name for execution.  Optional fields that follow the program name are passed as data to the program.  The environment performs the following steps to accomplish this for a C program.  First, it scans the command-line and identifies the character strings delimited by blanks that represent the program name and any optional data.  Then, it assembles an array consisting of pointers to these strings, in order of their occurrence in the command-line, and an additional final null pointer.  Finally, it issues a function call to main with two arguments.  The first called argc, for argument count, is a count of all the elements in the pointer array, except for the final null pointer.  The second called argv, for argument vector, is a pointer to the pointer array.

The definition of the main function, with parameters correponding to the calling arguments is usually found as follows.

int main(int argc, char *argv[]) {}

A more precise definition of main reflecting the true character of argv is often found.

int main(int argc, char **argv) {}

Copyright © 2018 The Stevens Computing Services Company, Inc.  All rights reserved.