Creating Your First Struct
Using the visual cue at the end of first chapter , let's create a data type to describe a chemical element:
struct chemicalElement
{
char name[3];
int atomicNum;
int valency;
double mass;
};
Note
The mass and sequence number cannot be negative, so it would be more efficient to use the
unsigned short int
data type.
Now, we can “create” a chemical element:
The code will look like this:
#include "stdio.h"
struct chemicalElement
{
char name[3];
int atomicNumber;
int valency;
double mass;
};
int main()
{
struct chemicalElement silicon = {"Si", 14, 4, 28.08};
return 0;
}
Access to struct fields will be discussed in the next chapter.
Try to describe the geometric point A
with a structure.
Swipe to start coding
- Create a structure named
Point
; - Create a field for the name of the future point;
- Create fields for the
X
andY
coordinates.
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 4.17
Creating Your First Struct
Veeg om het menu te tonen
Using the visual cue at the end of first chapter , let's create a data type to describe a chemical element:
struct chemicalElement
{
char name[3];
int atomicNum;
int valency;
double mass;
};
Note
The mass and sequence number cannot be negative, so it would be more efficient to use the
unsigned short int
data type.
Now, we can “create” a chemical element:
The code will look like this:
#include "stdio.h"
struct chemicalElement
{
char name[3];
int atomicNumber;
int valency;
double mass;
};
int main()
{
struct chemicalElement silicon = {"Si", 14, 4, 28.08};
return 0;
}
Access to struct fields will be discussed in the next chapter.
Try to describe the geometric point A
with a structure.
Swipe to start coding
- Create a structure named
Point
; - Create a field for the name of the future point;
- Create fields for the
X
andY
coordinates.
Oplossing
Bedankt voor je feedback!
Awesome!
Completion rate improved to 4.17single