C - Write program to initialize two structures

Requirements

Write program to initialize two structures

Demo

#include <stdio.h>

int main()/*  ww  w.j  a  v  a 2 s .c  om*/
{
    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