Grouping Data: Structs

Defining a struct.

 Define a struct named PatientData that contains two integer data members named heightInches and weightPounds. Sample output for the given program:
Patient data: 63 in, 115 lbs
#include <stdio.h>

typedef struct PatientData {
 int heightInches ;
 int weightPounds;
} 
PatientData;

int main(void) {
 PatientData lunaLovegood;

lunaLovegood.heightInches = 63;
 lunaLovegood.weightPounds = 115;

printf("Patient data: %d in, %d lbs\n", lunaLovegood.heightInches, lunaLovegood.weightPounds);

return 0;
}

 

Accessing a struct’s data members.

Write a statement to print the data members of InventoryTag. End with newline. Ex: if itemID is 314 and quantityRemaining is 500, print:

Inventory ID: 314, Qty: 500

Solution

#include <stdio.h>

typedef struct InventoryTag_struct {
 int itemID;
 int quantityRemaining;
} InventoryTag;

int main(void) {
 InventoryTag redSweater;

redSweater.itemID = 314;
 redSweater.quantityRemaining = 500;

printf("Inventory ID: %d, Qty: %d\n", redSweater.itemID, redSweater.quantityRemaining);

return 0;
}