Solution for Exercise 14-5
ex1405
#include <stdio.h> int main() { struct president { char name[40]; int year; } first = { "George Washington", 1789 }, second = { "John Adams", 1897 }; printf("The first president was %s\n",first.name); printf("He was inaugurated in %d\n",first.year); printf("The second president was %s\n",second.name); printf("He was inaugurated in %d\n",second.year); return(0); }
Output
The first president was George Washington
He was inaugurated in 1789
The second president was John Adams
He was inaugurated in 1897
Notes
* The comma is what makes the multi-declarations happen.
* The data in the braces must match in quantity and type the members of the structure.
* Many options are available to declare both structures. Above, you see an approach based on the layout of Exercise 14-4. Here is another way to make the declaration and assignment:
#include <stdio.h> int main() { struct president { char name[40]; int year; } first = { "George Washington", 1789 }, second = { "John Adams", 1897 }; printf("The first president was %s\n",first.name); printf("He was inaugurated in %d\n",first.year); printf("The second president was %s\n",second.name); printf("He was inaugurated in %d\n",second.year); return(0); }
Copyright © 1997-2024 by QPBC.
All rights reserved