how a declared a value in the void setup

I want to have a value that doesn't reset every loop so i can add to it every loop Does anyone know how to declare a value in the void setup. If I use the code below it says val was not declared
any help will be appreciated

void setup()
{
Serial.begin(9600);
int val;
}
void loop()
{

int val1 = (val + 1);

Serial.println (val1);
}

Does anyone know how to declare a value in the void setup.

Certainly.

void setup()
{
   int val = 37;
}

Of course, since val is local to setup, it goes out of scope when setup() ends, so it's fairly useless.

If I use the code below it says val was not declared

And, it's right. You need to read up on scope and the static keyword.

You need to either make val global, and quit creating a local variable of the same name, or make it static in loop().

Is there a way to declare the value in the Void loop but not reset everyloop

See reply #1

thanks for the help but static will not work for me because i want to be able to change the value in the loop but not have is reset at the beginning of the loop. is this possible

dansam8:
thanks for the help but static will not work for me because i want to be able to change the value in the loop but not have is reset at the beginning of the loop. is this possible

See reply #1, and yes static IS what you want. You're confusing static and const.

Variable Scope
We've gone over variables and how they're defined but now we need to talk about their scope. The scope of a variable is the context within which it is defined meaning that their existence is only defined within a block of code. They are either local or global variables. For example, a variable declared with a setup(), or a for loop, or a loop() may only be used in that block of code.
Code:

for (int i = 0; i < 5; i++) {
     serial.print(i);
}

serial.print(i); // error

i is a local variable. If you try to retrieve the value of i outside the for loop, you'd get an error of variable not defined. The same idea goes if you define a variable inside a setup() function and try to retrieve it inside a loop() function.

In many instances you need the variable to be global, meaning that you can assign or retrieve the variable from any block of code. To do this you need to define the variable outside all blocks of code.

Code:

int myPin = 5;

void setup() {
    myPin = 6; // note that I don't need to do int myPin = 6. That would re-define the variable not assign a value to it.
}

void loop() {
     serial.println(myPin);
}

myPin is a global variable and set or get from any block of code.