# Are arduino structs like old or new C?

**URL:** <https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408>\
**Category:** General Discussion\
**Created:** [July 14, 2024, 2:21am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408 "2024-07-14T02:21:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![two\_dee\_man](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/two_dee_man/32/1087410_2.png) [@two\_dee\_man](https://forum.arduino.cc/u/two_dee_man)\
**Post date:** [July 14, 2024, 2:21am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/1 "2024-07-14T02:21:35Z")

</div>

I'm new to arduino, but I'm an old hand at C and C++. Can somebody tell me if arduino structs are like new C (ie C++) where typedef isn't really needed as you can declare a struct object type by just using "struct foo {}; " and then later declare instances with just "foo fooInstance;"? Or must I mimic this by using typedef as a workaround, per old C?

Any chance there's "class" or "enum"?

Do I have to use pointers to pass structs around, or are they created equal with other variable types?

Curly brace struct initialization?

I tried to find some discussion of structs in the language reference. Oddly is wasn't there (the reference barely explains arrays). Maybe I was looking at the wrong language reference.  
Lots of garbage websites that purport to explain arduino structs are at a sub-beginner level. I hope somebody can point me to the real stuff.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 14, 2024, 2:48am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/2 "2024-07-14T02:48:29Z")

</div>

- The Arduino compiler handles C/C++.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [July 14, 2024, 3:23am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/3 "2024-07-14T03:23:38Z")

</div>

enum defaults to 16-bit values but, example

```arduino
 enum EnumName : byte {IDLE, MIDDLE, END};

```

uses half as much RAM

IIRC there is or was an **attribute** that does or did that  
edit: aha! what I typed was

```arduino
__attribute__

```

That has me wondering if the default boolean can do that?  
I still can't figure why the "optimizing" compiler defaults to 16 bits to hoid 1!

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [July 14, 2024, 3:32am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/4 "2024-07-14T03:32:01Z")

</div>

> [@two\_dee\_man](#):
>
> I tried to find some discussion of structs in the language reference.

It was written for beginners and never have we gotten a complete reference. Since it's free, we still get more than our money's worth!

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [July 14, 2024, 3:53am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/5 "2024-07-14T03:53:00Z")

</div>

> [@two\_dee\_man](#):
>
> Any chance there's "class" or "enum"?

Both are fully supported.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [July 14, 2024, 3:57am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/6 "2024-07-14T03:57:46Z")

</div>

Hello [two\_dee\_man](https://forum.arduino.cc/u/two_dee_man)

Welcome to the world's best Arduino forum ever.

Take a view to get some ideas and to gain the knowledge.

[https://www.learncpp.com/](https://www.learncpp.com/)

Have a nice day and enjoy coding in C++.

BTW enjoy coding

The struct directive was introduced in C++ in order to be able to design user-defined data types and functions.

This directive is a powerful combination in conjunction with the array instruction.

I always like to use this combination for the abstraction of the programming task.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 14, 2024, 4:35am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/7 "2024-07-14T04:35:00Z")

</div>

> [@two\_dee\_man](#):
>
> Can somebody tell me if arduino structs are like new C (ie C++) where typedef isn't really needed as you can declare a struct object type by just using "struct foo {}; " and then later declare instances with just "foo fooInstance;"?

No typedef needed.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 14, 2024, 4:41am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/8 "2024-07-14T04:41:31Z")

</div>

> [@paulpaulson](#):
>
> The struct directive was introduced in C++ in order to be able to design user-defined data types and functions.

The **struct** is not a directive; it is a keyword of C Language. It is the **class** keyword that was introduced in C++ by **Bjarne Stroustrup** ([pronounced as](https://www.google.com/search?q=bjarne+stroustrup+pronunciation&sca_esv=d29716a28124f733&sxsrf=ADLYWIJ93FEMzlL0rhZNBgnzhniAFO-YeQ%3A1720932959200&ei=X1qTZqf2C_Of4-EP_a6reA&oq=bjarne+stroustrup&gs_lp=Egxnd3Mtd2l6LXNlcnAiEWJqYXJuZSBzdHJvdXN0cnVwKgIIADIHECMYsAMYJzIKEAAYsAMY1gQYRzIKEAAYsAMY1gQYRzIKEAAYsAMY1gQYRzIKEAAYsAMY1gQYRzIKEAAYsAMY1gQYRzIKEAAYsAMY1gQYRzIKEAAYsAMY1gQYRzINEAAYgAQYsAMYQxiKBTINEAAYgAQYsAMYQxiKBTINEAAYgAQYsAMYQxiKBTINEAAYgAQYsAMYQxiKBTIOEAAYsAMY5AIY1gTYAQEyDhAAGLADGOQCGNYE2AEBMg4QABiwAxjkAhjWBNgBATIWEC4YgAQYsAMYQxjUAhjIAxiKBdgBAjITEC4YgAQYsAMYQxjIAxiKBdgBAjITEC4YgAQYsAMYQxjIAxiKBdgBAjITEC4YgAQYsAMYQxjIAxiKBdgBAkiEGlAAWABwAXgBkAEAmAEAoAEAqgEAuAEByAEAmAIBoAIVmAMAiAYBkAYTugYGCAEQARgJugYGCAIQARgIkgcBMaAHAA&sclient=gws-wiz-serp#fpstate=ive&vld=cid:188d0763,vid:KPWnwIshtEA,st:0)) in 1983.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [July 14, 2024, 4:45am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/9 "2024-07-14T04:45:49Z")

</div>

> [@paulpaulson](#):
>
> The struct directive was introduced in C++ in order to be able to design user-defined data types and functions.

C had structs from the start AFAIK!

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [July 14, 2024, 4:50am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/10 "2024-07-14T04:50:06Z")

</div>

I love this forum, you can always learn something new here.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 14, 2024, 6:26am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/11 "2024-07-14T06:26:56Z")

</div>

> [@two\_dee\_man](#):
>
> **1.** if arduino structs are like new C (ie C++) where typedef isn't really needed as you can declare a struct object type by just using "struct foo {}; " and then later declare instances with just "foo fooInstance;"? Or must I mimic this by using typedef as a workaround, per old C?
> 
> **2.** Any chance there's "class" or "enum"?
> 
> **3.** Do I have to use pointers to pass structs around, or are they created equal with other variable types?
> 
> **4.** Curly brace struct initialization?

**1.** I am telling based on K&R's Legendary "The C Programming Language" Book:

**(1)** The **struct** is a keyword. It introduces/encaptulates a data structure of conglomerate type as declared below and enclosed within pair of braces:

```arduino
struct 
{
     int x;
     int y;
     float z;
} ;

```

**(2)** An optinal name called _structure tag_ (simply tag) may follow the keyword _struct_. Given below an example; where, _point_ is a _tag_. The tag can be used subsequently as a shorthand to refer the declaration in braces of Step-1(1).

```arduino
struct point
{
     int x;
     int y;
     float 
} ;

```

==\>

```arduino
struct point pt;

```

In the above, _pt_ is a variable which is a structure of type _"struct point"_. It is an _instance_ of the structure.

The variable _pt_ has never been uttered as an _object_ in this Book of C, ; it is a concept introduced in C++ that encpsulates both "data and methods".

**(3)** In K&D's Book, I have not seen the structure declaration to contain the following _access specifiers_ which are seen in "C++ Class Declaration".

```arduino
private:
public:
protected:

```

**(4)** In C++, the _tag_ is enough to refer the structure declaration; there is no need for the tage to follow the **class** keyword. In C, the tag follows the _struct_ keyword as is seen in Step-1(2). For example:

```arduino
class Rectangle
{
     private:
          int width;
         int length;

     public:
         Rectangle(int, int);
         void calArea();
};

Rectangle rect(4, 5); //object has followed the Class Name = Calss Tag

```

.... to be continued.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 14, 2024, 6:30am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/12 "2024-07-14T06:30:49Z")

</div>

> [@GolamMostafa](#):
>
> In K&D's Book, I have not seen the strurure declaration to contain the following _access specifiers_ which are seen in "C++ Class Declaration".

Because those are C++ specific. So I'm not surprised 😉

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 14, 2024, 6:41am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/13 "2024-07-14T06:41:23Z")

</div>

> [@sterretje](#):
>
> Because those are C++ specific. So I'm not surprised 😉

And is why/how the following declartion compiled created using **struct** keyword?

```arduino
struct Rectangle
{
  private:
    int width;
    int length;

  public:
    Recatangle(int, int);
    void calArea();
};

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 14, 2024, 7:58am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/14 "2024-07-14T07:58:51Z")

</div>

You can not compare what K&R wrote in e.g. their second edition book with a modern C++.

The difference between a modern C++ struct and a class is that all members of a struct are public **by default** and all members are private **by default**. And that is how far my knowledge goes.

---

<div class="post-metadata">

**Author:** ![two\_dee\_man](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/two_dee_man/32/1087410_2.png) [@two\_dee\_man](https://forum.arduino.cc/u/two_dee_man)\
**Post date:** [July 14, 2024, 10:33am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/15 "2024-07-14T10:33:51Z")

</div>

OK, thanks for all the helpful replies. What I'm gathering from the response is that the "arduino language" is C++. Is this correct to say? Or is it some C++ subset? For example, can I do operator overloading in arduino? It's weird that there's no reference. What's the compiler -- is it just gnu g++ or some such? If so, then all my questions are thereby answered. Or is it something specifically created for arduino. I know that CS people love to make new compilers for things. Was that done here, or is it just the gnu compiler with a glitzy IDE?

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 14, 2024, 10:44am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/16 "2024-07-14T10:44:28Z")

</div>

> [@two\_dee\_man](#):
>
> What I'm gathering from the response is that the "arduino language" is C++. Is this correct to say? Or is it some C++ subset?

This might depend on the board. There are C++ things that are not supported out of the box.

> [@two\_dee\_man](#):
>
> What's the compiler -- is it just gnu g++ or some such?

Basically gnu-c / gnu-c++ tailored towards the board; e.g. for AVR based boards it's avr-gcc and avr-g++.

---

<div class="post-metadata">

**Author:** ![zwieblum](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zwieblum/32/427702_2.png) [@zwieblum](https://forum.arduino.cc/u/zwieblum)\
**Post date:** [July 14, 2024, 10:45am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/17 "2024-07-14T10:45:20Z")

</div>

e.g.: ch552duino has C backend (sdcc), not C++ 🙂

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 14, 2024, 11:10am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/18 "2024-07-14T11:10:15Z")

</div>

What are the advantags of using Arduino IDE Platform to develop applications for ATmega328P Microcontroller?

**1.** I am an Assembly Language Programmer. (I assume that) Arduino IDE Platform is not known to me. I am familiar with Microchip Studio. I can develop any application whatever I like and of course up to the level of my expertise.

**2.** Arduino IDE offers the following facilities:  
**(1)** Complex data structures of the target MCU can be handled (at almost Natural Language level) using C++ Programming Language.

**(2)** Sensors Interfacing have been made easy by the manufacturers/third parties through the supply of Library Files/Functions which are compatible with C++ Language of the Arduino IDE.

**(3)** Devices Interfacing (WiFi, Graphics Display, etc.) have been made easy by the similar scheme of Step-2(2).

**(4)** An excellent platform for learning the syntax and semantics rules of C++ Language being able to see the outcomes of experiments and imaginations at hardware levels.

**(5)** And many more...

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 14, 2024, 11:16am UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/19 "2024-07-14T11:16:48Z")

</div>

> [@zwieblum](#):
>
> e.g.: ch552duino has C backend (sdcc), not C++ 🙂

I have used sdcc (Small Device C Compiler) a lot for the 89S52 Microcntroller.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [July 14, 2024, 1:08pm UTC](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408/20 "2024-07-14T13:08:14Z")

</div>

> [@GolamMostafa](#):
>
> And is why/how the following declartion compiled created using **struct** keyword?

When compiled as C++ by default in our IDE.

[Next page](https://forum.arduino.cc/t/are-arduino-structs-like-old-or-new-c/1281408.md?page=2)
