#include <stdio.h> struct { int watts; int lumens; } bulbA; int main() { /* set values for bulbA */ bulbA.watts = 100; bulbA.lumens = 1710; /* print values of bulbA */ printf("BulbA: watts = %d\tlumens = %d\n", bulbA.watts, bulbA.lumens ); return 0; }
The following Does Not Work:
struct { int watts = 100; /* wrong */ int lumens = 1710; /* wrong */ } bulbA;
The members of a struct cannot be initialized in its definition, as the above attempts to do.
However, a struct variable like BulbA
can be initialized in its declaration:
struct { int watts; int lumens; } bulbA = {100, 1710};