Sometimes you need a variable to hold several related chunks of data. For example, imagine that you were writing a program that computed a person’s Body Mass Index. (What is your BMI? It is your weight in kilograms divided by the square of your height in meters. A BMI under 20 suggests that you may be underweight. A BMI over 30 suggests that you may be obese. It is a very imprecise tool for measuring a person’s fitness, but it makes a fine programming example.) A person, for your purposes, consists of a float that represents height in meters and an int that represents weight in kilograms.
Now you’re going to create your own Person type. A variable of type Person will be a structure and will have two members: a float called heightInMeters and an int called weightInKilos.
Create a new project: a C Command Line Tool called BMICalc. Edit main.c to create a structure that contains the data you need for a person:
#include <stdio.h> // Here is the declaration of the struct Person struct Person { float heightInMeters; int weightInKilos; }; int main(int argc, const char * argv[]) { struct Person person; person.weightInKilos = 96; person.heightInMeters = 1.8; printf("person weighs %i kilogramsn", person.weightInKilos); printf("person is %.2f meters talln", person.heightInMeters); return 0; }
Notice that you access the members of a struct using a period.
Here’s the frame for main() after the struct’s members have been assigned values.
Most of the time, you use a structure declaration over and over again. So it’s common to create a typedef for the structure type. A typedef defines an alias for a type declaration and allows us to use it more like the usual data types. Change main.c to create and use a typedef for struct Person:
#include <stdio.h> // Here is the declaration of the type Person typedef struct { float heightInMeters; int weightInKilos; } Person; int main(int argc, const char * argv[]) { Person person; person.weightInKilos = 96; person.heightInMeters = 1.8; printf("person weighs %i kilogramsn", person.weightInKilos); printf("person is %.2f meters talln", person.heightInMeters); return 0; }
Now that you’ve created a typedef, you can pass a Person structure to another function. Add a function named bodyMassIndex() that accepts a Person as a parameter and calculates BMI. Then update main() to call it:
#include <stdio.h> typedef struct _Person { float heightInMeters; int weightInKilos; } Person; float bodyMassIndex(Person p) { return p.weightInKilos / (p.heightInMeters * p.heightInMeters); } int main(int argc, const char * argv[]) { Person person; person.weightInKilos = 96; person.heightInMeters = 1.8; float bmi = bodyMassIndex(person); printf("person has a BMI of %.2fn", bmi); return 0; }
The first structure I had to deal with as a programmer was struct tm, which the standard C library uses to hold time broken down into its components. The struct is defined:
struct tm { int tm_sec; /* seconds after the minute [0-60] */ int tm_min; /* minutes after the hour [0-59] */ int tm_hour; /* hours since midnight [0-23] */ int tm_mday; /* day of the month [1-31] */ int tm_mon; /* months since January [0-11] */ int tm_year; /* years since 1900 */ int tm_wday; /* days since Sunday [0-6] */ int tm_yday; /* days since January 1 [0-365] */ int tm_isdst; /* Daylight Savings Time flag */ long tm_gmtoff; /* offset from CUT in seconds */ char *tm_zone; /* timezone abbreviation */ };
The function time() returns the number of seconds since the first moment of 1970 in Greenwich, England. localtime_r() can read that duration and pack a struct tm with the appropriate values. (It actually takes the address of the number of seconds since 1970 and the address of an struct tm.) Thus, getting the current time as a struct tm looks like this:
long secondsSince1970 = time(NULL); printf("It has been %ld seconds since 1970n", secondsSince1970); struct tm now; localtime_r(&secondsSince1970, &now); printf("The time is %d:%d:%dn", now.tm_hour, now.tm_min, now.tm_sec);
The challenge is to write a program that will tell you what the date (4-30-2015 format is fine) will be in 4 million seconds.
(One hint: tm_mon = 0 means January, so be sure to add 1. Also, include the <time.h> header at the start of your program.)