If Operators
2002-02-18 01:50:28
Category: c:general
Description: This is a short simple example on using if() and operators together.
Author: mind
Viewed: 3200
Rating: (11 votes)


/* if example by mind@metalshell.com
 *
 * This is a short simple example on using if() and operators together
 *
 * Logical Operators:
 *
 *   ==    Is equal to
 *   !=    Does not equal
 *   >     Is greater than
 *   >=    Is greater or equal to
 *   <     Is less than
 *   <=    Is less than or equal to
 *   &&    And
 *   ||    Or
 *   !     Not
 *
 * 2/15/2002
 *
 * http://www.metalshell.com
 *
 */
 
#include <string.h>
#include <stdio.h>
 
#define str1    "test7"
#define str2    "test1"
#define str3    "test7"
 
int main()
{
        int a = 1, b = 3, c = 0, d = 10;
 
    /* not (!)
     * ! is used to check if a integer is positive or not, so if your integer is
     * 7 the statement would return false, if your integer was 0 or -1 your statement
     * would be true, other examples below.
     */
    if (!c)
        printf("c(%i) is equal to or less than 0\n", c);
    else
        printf("c(%i) is not equal to and is greater than 0\n", c);
 
    /* greater than (>) */
        if (a > c)
                printf("a(%i) is greater than c(%i)\n", a, c);
        else
                printf("a(%i) is less than c(%i)\n", a, c);
 
    /* greater than or equal to (>=) */
        if (a >= c)
                printf("a(%i) is equal to or greater than c(%i)\n", a, c);
        else
                printf("a(%i) is not equal to or is less than c(%i)\n", a, c);
 
    /* less than */
    if (a < b)
        printf("a(%i) is less than b(%i)\n", a, b);
    else
        printf("a(%i) is greater than b(%i)\n", a, b);
 
    /* less than or equal to */
        if (a <= c)
                printf("a(%i) is equal to or less than c(%i)\n", a, c);
        else
                printf("a(%i) is not equal to or is greater than c(%i)\n", a, c);
 
    /* or */
        if ((a > c) || (b < d))
                printf("a(%i)>c(%i) b(%i)<d(%i) (statement true)\n", a, c, b, d);
        else
                printf("a(%i)>c(%i) b(%i)<d(%i) (statement false)\n", a, c, b, d);
 
    /* and */
        if ((d > a) && (b > a))
                printf("d(%i) is greater than a(%i)\nb(%i) is greater than a(%i)\n", 
                       d, a, b, a);
        else
                printf("statement: d(%i) > a(%i) and b(%i) > a(%i) is false\n", 
                       d, a, b, a);
 
 
    /* here is the second not (!) example, this is a bit different but still holds the same
     * purpose.. if (a DOES_NOT(!)equal(=) b) then true is returned, false otherwise
     */
 
    /* is not equal to */
        if (a != b)
                printf("a(%i) is not equal to b(%i)\n", a, b);
        else
                printf("a(%i) is equal to b(%i)\n", a, b);
 
    /* is equal to */
        if (strlen(str1) == strlen(str2))
                printf("%s length: %d is equal to %s length: %d\n", str1, strlen(str1), 
                       str2, strlen(str2));
        else
                printf("%s length: %d is not equal to %s length: %d\n", str1, strlen(str1), 
                       str2, strlen(str2));
 
        /* strcmp() and strncmp() are string matchers basicly the same as == is to an integer
         *
         * strcmp(const char *s1, const char *s2);
         * strncmp(const char *s1, const char *s2, size_t len);
         *
         * Returns 0 on sucessful string match
         */
 
        if (!(strncmp(str1, str3, strlen(str1))))
                printf("%s and %s are the same\n", str1, str3);
        else
                printf("%s and %s are not the same\n", str1, str3);
 
        return 0;
}