# Declare variable without initialising?

**URL:** <https://forum.arduino.cc/t/declare-variable-without-initialising/945139>\
**Category:** Programming\
**Created:** [January 9, 2022, 5:33pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139 "2022-01-09T17:33:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![BanksySan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/banksysan/32/676763_2.png) [@BanksySan](https://forum.arduino.cc/u/BanksySan)\
**Post date:** [January 9, 2022, 5:33pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/1 "2022-01-09T17:33:46Z")

</div>

I have this `CommandHandler` type that relies on the `Serial` object. I want to be able to declare the variable in global scope, but instantiate it in the `setup()` method after serial comms have been started.

 ![Variable is initialised inline.](https://europe1.discourse-cdn.com/arduino/original/4X/6/8/0/680f44599f35650a013623dadd5ba574409eb30d.png)

How do I declare the variable, but delay instantiation?

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [January 9, 2022, 5:38pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/2 "2022-01-09T17:38:36Z")

</div>

Please post code, not pictures.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 9, 2022, 5:42pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/3 "2022-01-09T17:42:18Z")

</div>

A variable is completely different from instantiating an object.

If you declare a **variable** this variable will be assigned the value zero if you don't assign any value at the line you declare the variable.

You can assign any value to a variable that fits into the variable at any place of your code

```cpp
int myIntegerVariable; // declaration of a variable without assigning a value

void setup() {
  myIntegerVariable = 123; // assigning a value to the variable

```

maybe you are confusing the term "variable" with something else?  
best regards Stefan

---

<div class="post-metadata">

**Author:** ![BanksySan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/banksysan/32/676763_2.png) [@BanksySan](https://forum.arduino.cc/u/BanksySan)\
**Post date:** [January 9, 2022, 6:01pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/4 "2022-01-09T18:01:58Z")

</div>

I'm coming from a easier languages than C. In .NET, Java or JavaScript I would call that thing a variable (or a constant if it's declared as such). In those languages, if I declare that token, without explicitly giving it a value then the value will be whatever `default` is, i.e. `0` for numbers, `false` for booleans, `null` for reference types etc.

What I want is to declare the token that I would use to access the instance of `CommandHandler` as global, but only instantiate the object in the `setup()` method.

What's the correct name for that token?

---

<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:** [January 9, 2022, 6:05pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/5 "2022-01-09T18:05:51Z")

</div>

In C/C++, declaring a variable assigns memory addresses to it. Whether it is initialized to some value depends on the circumstances.

I don't recall all the rules, but global numerical variables are initialized to zero. In functions, a declared, but uninitialized variable will usually contain garbage.

Consult the many on line references for the details.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [January 9, 2022, 6:07pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/6 "2022-01-09T18:07:00Z")

</div>

> [@BanksySan](#):
>
> I'm coming from a easier languages than C

Is such a thing possible?

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 9, 2022, 6:08pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/7 "2022-01-09T18:08:48Z")

</div>

> [@anon94801460](#):
>
> In C/C++, declaring a variable assigns memory addresses to it.

Memory is assigned when the variable is **defined**. See: [What's the difference between declaring and defining in C and C++ - Cprogramming.com](https://www.cprogramming.com/declare_vs_define.html)

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 9, 2022, 6:10pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/8 "2022-01-09T18:10:16Z")

</div>

@ [BanksySan](https://forum.arduino.cc/u/BanksySan), as already requested, post your code using Code Tags. Pictures of code are worthless.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 9, 2022, 6:18pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/9 "2022-01-09T18:18:32Z")

</div>

> [@BanksySan](#):
>
> I have this `CommandHandler` type that relies on the `Serial` object. I want to be able to declare the variable in global scope, but instantiate it in the `setup()` method after serial comms have been started.

The "Serial" object exists in global scope after you `#include <Arduino.h>`. It is not clear how you are trying to use it. Since the error is in "main.cpp" I suspect you forgot to `#include <Arduino.h>` before trying to use the names it defines.

---

<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:** [January 9, 2022, 6:21pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/10 "2022-01-09T18:21:10Z")

</div>

Good to know, thanks!

Would you mind explaining when the address and value is assigned to x in this purposely incomplete code, which compiles with the expected warning, runs and prints "x = 0"?

```cpp
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
while(!Serial);
int x;
Serial.print("x = ");
Serial.println(x);
}

void loop() {}

```

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [January 9, 2022, 6:22pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/11 "2022-01-09T18:22:44Z")

</div>

> [@anon94801460](#):
>
> runs and prints "x = 0"?

You got lucky.  
Don't rely on luck.

---

<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:** [January 9, 2022, 6:23pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/12 "2022-01-09T18:23:26Z")

</div>

I **DO NOT** rely on luck, but this VERY PURPOSEFUL example printed x = 0. I would expect it to print garbage at other times, as mentioned in my first post.

I'm awaiting an answer on when the memory address and value is assigned.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [January 9, 2022, 6:25pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/13 "2022-01-09T18:25:44Z")

</div>

Who knows what the optimiser has done?

Maybe crt0 blanked the stack area.  
Maybe X isn't even on the stack.

What if you qualify it volatile?

---

<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:** [January 9, 2022, 6:26pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/14 "2022-01-09T18:26:32Z")

</div>

Perhaps @gfvalvo will explain.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 9, 2022, 6:29pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/15 "2022-01-09T18:29:36Z")

</div>

The value of an uninitialized local variable is undefined.

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [January 9, 2022, 6:30pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/16 "2022-01-09T18:30:56Z")

</div>

> [@BanksySan](#):
>
> How do I declare the variable, but delay instantiation?

Have you tried declaring it outside of setup and assigning value to it inside setup?

---

<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:** [January 9, 2022, 6:33pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/17 "2022-01-09T18:33:51Z")

</div>

Of course, but it has an address. When did that get assigned?

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 9, 2022, 6:36pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/18 "2022-01-09T18:36:10Z")

</div>

Assuming it wasn't optimized into a register or optimized away completely, memory would have been assigned when it was defined. Here:

```cpp
int x;

```

It's never assigned a value. That's why its value is undefined.

---

<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:** [January 9, 2022, 6:38pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/19 "2022-01-09T18:38:32Z")

</div>

Cool, how does that agree with your bolded statement, and the "rules" that you linked, in reply #7?

> Memory is assigned when the variable is **defined**.

Seems to be a chicken or egg problem, which is why I am skeptical about the "rules".

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [January 9, 2022, 6:38pm UTC](https://forum.arduino.cc/t/declare-variable-without-initialising/945139/20 "2022-01-09T18:38:35Z")

</div>

> [@BanksySan](#):
>
> What I want is to declare the token that I would use to access the instance of `CommandHandler` as global, but only instantiate the object in the `setup()` method.

So you want to declare a pointer, but instantiate the object and assign to your pointer in setup? I sense XY Problem

[Next page](https://forum.arduino.cc/t/declare-variable-without-initialising/945139.md?page=2)
