Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Creating Your First Struct | Introduction to Structs
C Structs

bookCreating 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.

Task

Swipe to start coding

  1. Create a structure named Point;
  2. Create a field for the name of the future point;
  3. Create fields for the X and Y coordinates.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Summarize this chapter

Explain the code in file

Explain why file doesn't solve the task

close

Awesome!

Completion rate improved to 4.17

bookCreating Your First Struct

Swipe to show menu

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.

Task

Swipe to start coding

  1. Create a structure named Point;
  2. Create a field for the name of the future point;
  3. Create fields for the X and Y coordinates.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

close

Awesome!

Completion rate improved to 4.17
SectionΒ 1. ChapterΒ 4
single

single

some-alt