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
11
votes
4 answers

Is it legal to reference a variable in its definition?

int test[2] = { 45, test[0] }; int x = (x = 111); cout << test[0] << " " << test[1] << " " << x << "\n"; // 45 45 111 Are the assignments in the first two lines legal? Visual Studio 2010 compiles and runs it without any errors or warnings but it…
0x5f3759df
  • 2,349
  • 1
  • 20
  • 25
11
votes
4 answers

How do I properly split a PATH variable in PHP?

I want to split $path = getenv('PATH'); into its components. How do I determine the separator char in an os-dependent fashion?
fabiangebert
  • 2,623
  • 2
  • 22
  • 25
11
votes
3 answers

Concatenate a dynamic variable name in Javascript

In PHP you can concatenate a variable name like this $a = 1 ${'fruit_' . $a} = 'apple'; The result will lead to the creation of variable $fruit_a How would I do this in javascript?
thank_you
  • 11,001
  • 19
  • 101
  • 185
11
votes
3 answers

Not var_dump() nor print_r() will show readable information.... but the same confusing output. Why?

I have some comments inside a drupal node, and tried var_dump() and print_r() to see the difference between them. I want to see what´s inside $content variable of $comment object. I´ve tried both and what I get is the same output! array(5) {…
Rosamunda
  • 14,620
  • 10
  • 40
  • 70
11
votes
2 answers

Batch file substring replacement using variables

I'm having trouble getting this batch file to do substring replacements when variables are used. Specifically when the !original! variable is specified; if it's a literal string it works fine. However, this will not do for my usage. setlocal…
delpium
  • 161
  • 1
  • 2
  • 7
11
votes
2 answers

How do you change makefile variables file extension?

For example, I have a variable that holds a list of dependencies BOARDS:=lance.mcm light.mcm sac.mcm I need another variable named NET such that NET:=lance.net light.net sac.net It should be set such that when I change the BOARDS variable, NET…
wonton
  • 7,568
  • 9
  • 56
  • 93
11
votes
7 answers

PHP: Session variables

I am beginning to learn php. I have a question regarding sessions. Right now, I know that session_start() creates a session variable. What I don't know is, when I access the session I created, do I need to use session_start() again? If yes... Why is…
Krimson
  • 7,386
  • 11
  • 60
  • 97
11
votes
3 answers

Why can this unbound variable work in Python (pyquery)?

The code is from the guide of pyquery from pyquery import PyQuery d = PyQuery('

Hi

Bye

') d('p').filter(lambda i: PyQuery(this).text() == 'Hi') My question is this in the 3rd line is an unbound variable and is never defined…
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
11
votes
2 answers

In ColdFusion, variables are resolved in what order?

I have little impression about variables resolve order, but I can't find it in the CFML Reference or ColdFusion Dev Guide. Can anyone help?
user133580
  • 1,479
  • 1
  • 19
  • 28
11
votes
3 answers

Why do variables passed to runnable need to be final?

If I have a variable int x = 1, say, and I declare a runnable in the main thread, and I want to pass x to the runnable's run() method, it must be declared final. Why? final int x = 0;//<----must be final... private class myRun implements Runnable…
user485498
11
votes
3 answers

C++ declaring a managed variable in a native code

I have a .NET form, and a native code in my Visual Studio. The problem is: I can't declare a global instance of my .NET form in my native code, like this: Editor^ maineditor; It gives me this problem: error C3145: 'EditorEntry' : global or static…
Miguel P
  • 1,262
  • 6
  • 23
  • 48
11
votes
5 answers

Is there a PHP equivalent of "new Array (number)" in javascript?

I was trying to convert basic Javascript function into PHP, and I saw that one of the variables was declared var Variable = new Array (13). I know that PHP variables are declared like: $variable = array() but what about the "13" in new Array(13)?…
ParaChase
  • 319
  • 6
  • 15
11
votes
6 answers

#Define VS Variable

I cannot understand what is the difference between: #define WIDTH 10 and int width = 10; What are the benefits of using the first or the second?
Rrjrjtlokrthjji
  • 602
  • 2
  • 10
  • 26
11
votes
1 answer

Union tested for current member in use

Do unions have a control structure to test which member is currently in use (or if it has any at all)? I'm asking this because undefined behavior is never a good thing to have in your program.
Alex D.
  • 427
  • 1
  • 5
  • 14
11
votes
5 answers

PYTHON: Update MULTIPLE COLUMNS with python variables

I'm trying to write a valid mysql statement that would allow me to update multiple columns in one record with values provided as python variables. My statement would look like this: db = MySQLdb.connect(host="localhost", user="user",…
elfuego1
  • 10,318
  • 9
  • 28
  • 24
1 2 3
99
100