Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda 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.

Tarefa

Swipe to start coding

You are creating a structure to represent a point in a 2D space. Your goal is to store and display information about the point, including its name and coordinates.

The structure Point should contain:

  • A character array for the point’s name — it must contain only one letter (for example, "A" or "B"). To store this single letter and the null terminator \0, declare the array as name[2].
  • Two integer fields representing the x and y coordinates.

In the main function:

  • Create a variable of type Point.
  • Initialize it using curly braces {} with the name and coordinate values.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 4
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

Awesome!

Completion rate improved to 4.17

bookCreating Your First Struct

Deslize para mostrar o 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.

Tarefa

Swipe to start coding

You are creating a structure to represent a point in a 2D space. Your goal is to store and display information about the point, including its name and coordinates.

The structure Point should contain:

  • A character array for the point’s name — it must contain only one letter (for example, "A" or "B"). To store this single letter and the null terminator \0, declare the array as name[2].
  • Two integer fields representing the x and y coordinates.

In the main function:

  • Create a variable of type Point.
  • Initialize it using curly braces {} with the name and coordinate values.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 4
single

single

some-alt