Questions tagged [variables]

THIS IS AMBIGUOUS; USE SPECIFIC-LANGUAGE TAGS WHENEVER APPLICABLE. A variable is a named data storage location in memory. Using variables, a computer program can store numbers, text, binary data, or a combination of any of these data types. They can be passed around in the program.

A variable is a named data storage location in memory. Using variables a computer program can store numbers, textual data, , or a combination of any of these data types. The data can be passed around in a program by copying it from variable to variable or by referencing variables, i.e. defining from which variable a receiving code part is to take the contained data.

Variables which are only accessible within a certain function are termed "local variables". A "global variable", or one with indefinite scope, may be referred to anywhere in the program.

In some , variables are constrained by a specific data type. Data types may vary across languages, but share many commonalities.

Primitive data types usually include:

  • character, char, string, varchar (text)
  • byte, short, int, tinyint, integer, long (whole numbers)
  • double, decimal, float (real numbers)
  • bit, boolean (true/false)
  • date, datetime (date and time values)
  • object (any value, including composite types)
  • binary, raw, varbinary (that store stream of system data in binary form)

Composite data types consist of usually more than one of the primitive types and/or even other composite types.

# an example composite type, in pseudo code
Person(
    'First name'  : string,
    'Surname'     : string,
    'Birthday'    : date,
    'CanProgram'  : boolean
)

Some languages contain extra primitives: Tuples (Python), Linked Lists (Lisp), Hash Tables (Lisp, Perl, Python, Lua, D).

Some programming languages allow variables that store functions, that can be stored in data structures, passed as parameters to other functions, or returned as a result from other functions.

Memory allocation

The specifics of variable allocation and the representation of their values vary widely, both among programming languages and among implementations of a given language. Many language implementations allocate space for local variables (whose extent lasts for a single function call) on the stack, and their memory is automatically reclaimed when the function returns. More generally, in name binding, the name of a variable is bound to the address of some particular block (contiguous sequence) of bytes in memory, and operations on the variable manipulate that block. Referencing is more common for variables whose values have large or unknown sizes when the code is compiled. Such variables reference the location of the value instead of storing the value itself, which is allocated from a pool of memory called the heap.

More information and reference material on Wikipedia.

53839 questions
276
votes
12 answers

Pointer vs. Reference

What would be better practice when giving a function the original variable to work with: unsigned long x = 4; void func1(unsigned long& val) { val = 5; } func1(x); or: void func2(unsigned long* val) { *val =…
Jack Reza
  • 2,771
  • 3
  • 16
  • 4
274
votes
19 answers

How can I define colors as variables in CSS?

I’m working on a CSS file that is quite long. I know that the client could ask for changes to the color scheme, and was wondering: is it possible to assign colors to variables, so that I can just change a variable to have the new color applied to…
patrick
  • 7,369
  • 5
  • 20
  • 14
271
votes
5 answers

What does ${} (dollar sign and curly braces) mean in a string in JavaScript?

I haven't seen anything here or on MDN. I'm sure I'm just missing something. There's got to be some documentation on this somewhere. Functionally, it looks like it allows you to nest a variable inside a string without doing concatenation using the +…
Darren Joy
  • 2,849
  • 2
  • 12
  • 10
263
votes
7 answers

Initializing multiple variables to the same value in Java

I'm looking for a clean and efficient method of declaring multiple variables of the same type and of the same value. Right now I have: String one = "", two = "", three = "" etc... But I'm looking for something like: String one,two,three = "" Is…
user83643
  • 2,901
  • 2
  • 17
  • 13
263
votes
3 answers

Using variables inside a bash heredoc

I'm trying to interpolate variables inside of a bash heredoc: var=$1 sudo tee "/path/to/outfile" > /dev/null << "EOF" Some text that contains my $var EOF This isn't working as I'd expect ($var is treated literally, not expanded). I need to use sudo…
Jon
  • 3,280
  • 2
  • 15
  • 16
259
votes
12 answers

Why is the asterisk before the variable name, rather than after the type?

Why do most C programmers name variables like this: int *myVariable; rather than like this: int* myVariable; Both are valid. It seems to me that the asterisk is a part of the type, not a part of the variable name. Can anyone explain this logic?
WBlasko
  • 2,893
  • 2
  • 17
  • 11
252
votes
5 answers

How to split one string into multiple variables in bash shell?

I've been looking for a solution and found similar questions, only they were attempting to split sentences with spaces between them, and the answers do not work for my situation. Currently a variable is being set to something a string like…
crunchybutternut
  • 2,523
  • 2
  • 14
  • 5
250
votes
11 answers

How do I get the type of a variable?

In C++, how does one find the type of a variable?
David G
  • 94,763
  • 41
  • 167
  • 253
249
votes
20 answers

How to store standard error in a variable

Let's say I have a script like the following: useless.sh echo "This Is Error" 1>&2 echo "This Is Output" And I have another shell script: alsoUseless.sh ./useless.sh | sed 's/Output/Useless/' I want to capture "This Is Error", or any other stderr…
psycotica0
  • 3,130
  • 2
  • 19
  • 16
249
votes
8 answers

SQL variable to hold list of integers

I'm trying to debug someone else's SQL reports and have placed the underlying reports query into a query windows of SQL 2012. One of the parameters the report asks for is a list of integers. This is achieved on the report through a multi-select…
ErickTreetops
  • 3,189
  • 4
  • 27
  • 37
246
votes
18 answers

How can I print variable and string on same line in Python?

I am using python to work out how many children would be born in 5 years if a child was born every 7 seconds. The problem is on my last line. How do I get a variable to work when I'm printing text either side of it? Here is my code: currentPop =…
Bob Uni
  • 2,601
  • 5
  • 17
  • 13
244
votes
12 answers

Will using 'var' affect performance?

Earlier I asked a question about why I see so many examples use the varkeyword and got the answer that while it is only necessary for anonymous types, that it is used nonetheless to make writing code 'quicker'/easier and 'just because'. Following…
Jeff Keslinke
  • 4,278
  • 5
  • 30
  • 49
238
votes
11 answers

Is it better in C++ to pass by value or pass by reference-to-const?

Is it better in C++ to pass by value or pass by reference-to-const? I am wondering which is better practice. I realize that pass by reference-to-const should provide for better performance in the program because you are not making a copy of the…
Matt Pascoe
  • 8,651
  • 17
  • 42
  • 48
233
votes
9 answers

PHP Pass variable to next page

It seems pretty simple but I can't find a good way to do it. Say in the first page I create a variable $myVariable = "Some text"; And the form's action for that page is "Page2.php". So in Page2.php, how can I have access to that variable? I know I…
Carlo
  • 25,602
  • 32
  • 128
  • 176
232
votes
10 answers

PHP check whether property exists in object or class

I understand PHP does not have a pure object variable, but I want to check whether a property is in the given object or class. $ob = (object) array('a' => 1, 'b' => 12); or $ob = new stdClass; $ob->a = 1; $ob->b = 2; In JS, I can write this to…
Micah
  • 4,254
  • 8
  • 30
  • 38