Choose a correct statement about C structures.a)Structure elements can...
struct book
{
int SNO=10; //not allowed
};
Choose a correct statement about C structures.a)Structure elements can...
Explanation:
Structure Elements Initialization:
In C programming, a structure is a user-defined data type that allows you to combine different data types into a single entity. Each data type inside the structure is called a member.
Initializing Structure Elements:
Structure elements can be initialized at the time of declaration using the following syntax:
struct structureName variableName = {value1, value2, ..., valueN};
Example:
```c
#include
// Define a structure
struct student {
char name[50];
int age;
float marks;
};
int main() {
// Initialize structure elements
struct student s1 = {"John", 20, 85.5};
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
```
Output:
```
Name: John
Age: 20
Marks: 85.50
```
Explanation:
In the above example, a structure named "student" is defined with three members: "name" (character array), "age" (integer), and "marks" (float). The structure variable "s1" is declared and initialized with values for each member at the time of declaration itself. The initialized values are then printed using printf statements.
Correct Statement:
The correct statement about C structures is option 'B': Structure members cannot be initialized at the time of declaration.
Explanation:
In C programming, structure members cannot be directly initialized at the time of declaration. Instead, the structure variable can be initialized with values for each member.
Example:
```c
#include
// Define a structure
struct student {
char name[50];
int age;
float marks;
};
int main() {
// Initialize structure variable
struct student s1 = {"John", 20, 85.5};
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
```
Output:
```
Name: John
Age: 20
Marks: 85.50
```
Explanation:
In the above example, the structure variable "s1" is declared and initialized with values for each member using curly braces and separating the values with commas. The initialized values are then printed using printf statements.
Therefore, the correct statement is option 'B': Structure members cannot be initialized at the time of declaration.
To make sure you are not studying endlessly, EduRev has designed Class 6 study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Class 6.