/* * Program to illustrate simple use of struct. * * Input: none * * Output: results of calling various functions. */ #include #include /* user-defined type for money -- dollars and cents */ typedef struct { int dollars; int cents; } money; /* print m in an appropriate format (with dollar sign, decimal point) */ void print(money m) { printf("$%d.%02d", m.dollars, m.cents); } /* add m2 to m1 */ void add_to(money *m1, money m2) { m1->dollars += m2.dollars; m1->cents += m2.cents; if (m1->cents >= 100) { m1->cents -= 100; m1->dollars += 1; } } /* main program */ int main(void) { money bank_bal = { 100, 0 }; money to_add = { 0, 50 }; printf("balance is "); print(bank_bal); printf("\n"); add_to(&bank_bal, to_add); printf("after adding, balance is "); print(bank_bal); printf("\n"); add_to(&bank_bal, to_add); printf("after adding, balance is "); print(bank_bal); printf("\n"); return EXIT_SUCCESS; }