SiteBanner

We're now adding two new logic tests;  ' && '  (AND)   ' || '  (OR).    Check out the 'Comparison' section of "Quick Charts" on this website.  In this example, we revisit 1) passing a pointer to a function, 2) creating a structure, 3) 'enum'  and 4) 'strstr()' - which looks for one string inside another string.

 

(right-click to download)

/* pgm19 source */

#include <stdio.h>
#include <string.h>

enum some_hair_colors {BLACK, BROWN, BLONDE, REDHEAD, WHITE, NEONPINK};
char *headhair[] = {"Black", "Brown", "Blonde", "Redhead", "White", "Neon Pink"};

struct human {
     char  fullname[35];
     char  sex;                  // 'F' or 'M' single char
     int     age;                  // = an 'enum' item, which will be an index into 'headhair[]'
     int     hair;
     float  bodytemp;
     };

void populate_guy_data(struct human *);
void populate_gal_data(struct human *);

int main(void) {
     struct human pers;

     printf("\n\n");
     populate_gal_data(&pers);
     printf("Name: %s, sex: %c, \n age: %d, hair: %s, temp: %.1f\n\n", pers.fullname, pers.sex, pers.age, headhair[pers.hair], pers.bodytemp);

     // if ( ( true ) AND ( true ) AND ( true ) AND ( ( true ) OR ( true )) )

     if ((strstr(pers.fullname, "Jack") != NULL) && (pers.sex != 'M') && (pers.age != 13) && ((pers.hair == BROWN) || (pers.hair == NEONPINK)))
          printf("Just the right person for me!\n");
     else
          printf("Eeeek! What a hideous monster. But she has nice teeth.\n\n");

     populate_guy_data(&pers);
     printf("\nName: %s, sex: %c, \n age: %d, hair: %s, temp: %.1f\n\n", pers.fullname, pers.sex, pers.age, headhair[pers.hair], pers.bodytemp);

     // if ( ( true ) AND ( true ) AND ( true ) AND ( ( true ) OR ( true )) )

     if ((strstr(pers.fullname, "Carl") != NULL) && (pers.sex == 'M') && (pers.age == 71) && ((pers.hair == BLACK) || (pers.hair == REDHEAD)))
          printf("Just the right person for me!\n");
     else
          printf("Aaakk! What low-life trash. And I can't stand his tatoos.\n\n");

     printf("\n");
     return(0);
     }

void populate_gal_data(struct human *pers) {
     pers->sex = 'F';                      pers->age = 27;
     pers->hair = NEONPINK;      pers->bodytemp = 99.2;
     strcpy(pers->fullname, "Gertrude Jack Ganinteele");
     }

void populate_guy_data(struct human *pers) {
     pers->sex = 'M';                pers->age = 71;
     pers->hair = BLONDE;     pers->bodytemp = 97.9;
     strcpy(pers->fullname, "Baldrick Gail Benson");
     }