// C program to demonstrate that direct member initialization is not possible in C #include<stdio.h> structRecord { int x = 7; # Compiler Error }; intmain() { structRecords; printf("%d", s.x); return0; }
1 2 3 4 5 6 7 8 9 10 11 12
// CPP program to initialize data member in c++ #include<iostream> usingnamespace std; structRecord { int x = 7; }; intmain() { Record s; cout << s.x << endl; return0; }
// C program with structure static member structRecord { staticint x; }; /* 6:5: error: expected specifier-qualifier-list before 'static' static int x; ^*/
1 2 3 4
// C++ program with structure static member structRecord { staticint x; };
// CPP program to initialize data member in c++ #include<iostream> usingnamespace std; structStudent { int roll; Student(int x) { roll = x; } }; intmain()// Driver Program { structStudents(2); cout << s.roll; return0; }