Константы

Структуры (записи)

//C has no default values for fields:
//a const field gets its value only at initialization
typedef struct {
    const int lineWidth;
    int length;
} BoldLine;

BoldLine boldLine = {.lineWidth = 10, .length = 100};
//boldLine.lineWidth = 20; //<-Error
int width = boldLine.lineWidth;
//width is 10

//a constant struct serves as the template of values
constexpr BoldLine defaultLine = {.lineWidth = 12, .length = 50};
BoldLine line = defaultLine;
//line = defaultLine; //<-Error: a const field forbids =
//line.lineWidth is 12

printf("width is %d\n", width);
printf("line.lineWidth is %d\n", line.lineWidth);