Variables

Could someone explain what a variable is in depth, Ive read a lot of articles but none of them explained it in depth, maybe even send a link. Thanks.
There is no depth to them. The concept of a variable is very simple. What don't you understand?
What are they? I understand they’re named objects which store a value depending on their type specifier(operation also depends on type) and their size(depends on complier). What I don’t get is why they’re called objects and how they’re created within the memory location.
Last edited on
They're called "objects" because an object is a distinct thing that has an existence.

how they’re created within the memory location.

Memory is just numbers. We decide we want an int. We decide it will take up four bytes. We say "these four bytes here; they're one single int object". Done. That's all that happens. We just keep track of which lumps of memory represent our objects.
Last edited on
Makes sense, why does an object require an identifier to be considered a variable and not both a type specifier and identifier?
Where are you getting these questions?
Well from myself but it says in a book written in a book, “A variable is a named object” I didn’t get why.
Why what?

It sounds like you're asking questions like "why is an elephant called an elephant?". We have to agree common terms to be able to understand each other. We agree what a variable is so that when we talk about variables, we understand each other,. It's how language works; we have words that mean things.
My bad, why are variables referred to as “a named object” even though you can’t declare a variable without a type specifier, hopefully you understand what I’m trying and failing to say.
Well, a variable is (or if you prefer, represents, identifies) an object, and it has a name, so it's a named object.

I don't know what not being able to declare a variable without a type specifier has to do with it.
Last edited on
Maybe this might help:
Because C++ is a strongly typed language, every object must be some type.
But not every object is necessarily a named variable.
If I write my_func(42); 42 is an int, but it wouldn't be called a "variable" by most because it doesn't have a name. It's only a temporary object that will be copied into the function (where it then will have a name),
e.g. void my_func(int a) { /* ... */ };
Last edited on
The compiler reads language objects which is what represents a physical item or digital context, it translates code in other programs for further interpretation. The written program is basically an input for the compiler. The compiler is also coded as objects its self and in variables. Variable are the storage holders for specific arguments which affects how a program runs.

Variables are used to hold numbers, text, data, etc.

Variables have different types (type specifiers) for different purposes when holding data. e.g;
1
2
3
int age = 92; 
string name = "john doe";
float payCheck = 1024.37;


Variables are able to become assigned with specific property values, they can hold other variables. They help reveal results based on mathematical expressions through conditions and as logic. They can be used to keep track of a programs statistics, keep track of scores, make other code dependent on variable(s) for different scenarios.

One is able to name a variable anything they want and is usually based on the expression of the overall meaning and intention of the developed program.
a variable is like the idea in math. Consider y= f(x); x is a variable, and it can change, and as it changes, your results change (y). A variable is different from a constant which has a fixed value that can't be modified without hackery (constants live in memory too and can be hacked but the compiler won't allow you to do it directly). The idea of a constant vs variable is in the compiler, not the computer: the computer doesn't know the difference down in the bits and bytes.

Inside a computer, a variable is just a piece of memory (a register, a block of ram, etc). Inside that piece of memory are bytes. the state of those bytes represent data that you can interpret back into something meaningful to your program. Maybe it is a character in ascii or Unicode, maybe its a double or integer, maybe its class of type alien zombie. Class/struct and such type variables are a block of memory where inside that block are smaller variables of more basic types, but the idea is the same, its still memory made of bytes that contain data.

As you express your ideas in the language, you tell the computer directly or indirectly what those bytes mean. Maybe you say int x, telling the computer its an integer of a known size (int varies across systems but for your system, its a fixed size) and the cpu has a branch of circuits that do integer things (multiply, add, xor, etc). Maybe its a double and the FPU will handle it. The compiler routs the bytes to the CPU integer or FPU double areas for you, because you told it what to do when you typed it.
Last edited on
A named variable has a name that is introduced into the program by the declaration of an object (or a reference).
In the program, the name of the variable denotes the object (or reference).

1
2
int i = 9 ; // variable i denotes an object (of type int)
int& r = i ; // variable j is an alias for the object (reference to object of type int) 
Maybe a good analogy is think of a variable/object as a room.
I named the space or object that can hold people a "room" . In Spanish, that space is called "cuarto". In french, it is called "chambre". No matter the name is you are naming the same object.
A room can be single room, double room etc. because rooms have different sizes.
Last edited on
why does an object require an identifier to be considered a variable and not both a type specifier and identifier?

It does require both a type specifier and identifier:

1
2
int a;    // int is the type specifier and "a" is the name
string s; // string is the type specifier and "s" is the name 


I think the point that your book (or whatever you're reading) is really trying to make is that not all objects in a program are variables. Sometimes there are temporary objects that get created and destroyed as Ganado showed. You can also create unnamed objects on the heap with new. Invariably, when the program creates an object with new, it assigns the object's address to some pointer variable so that the program can refer to it. In that case, the pointer variable contains the address, but the object itself remains unnamed:

int *p = new int(4); // variable p contains the address of the anonymous int created by new
Topic archived. No new replies allowed.