C - Write program to initialize two structures

Requirements

Write program to initialize two structures

Demo

#include <stdio.h>

int main()//  w  w w  .  j a  va2 s  .c  o  m
{
    struct president
    {
        char name[40];
        int year;
    } first = {
        "George Washington",
        1789
    };
   struct president second = {
        "John Adams",
        1797
    };

    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);
}

Result

Here is how to declare both structures:

struct president
{
        char name[40];
        int year;
} first = {
        "George Washington",
        1789
}, second = {
        "John Adams",
        1797
};

Another way to format this type of declaration is as follows, which also makes it easier to see what's going on:

struct president
{
        char name[40];
        int year;
} first = { "George Washington", 1789 },
  second = { "John Adams", 1797 };

Related Exercise