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
3 answers

SQL Multiple LIKE Statements

I'm currently working on a report that shows me all postcodes covered by our sales team. Each team covers over 100 postcodes. What I would like to do is create a report that brings back the clients within the postcode. Currently, my code looks like…
RustyHamster
  • 359
  • 1
  • 5
  • 19
11
votes
20 answers

Which language understands 'variable a = 0 , 20, ..., 300'?

Which language is smart so that it could understand variable a = 0 , 20, ..., 300 ? so you could easily create arrays with it giving step start var last var (or, better no last variable (a la infinite array)) and not only for numbers (but even…
Rella
  • 65,003
  • 109
  • 363
  • 636
11
votes
5 answers

Pass Ansible variables from one role (running on one host) to another role running on another host within the same playbook

I have a playbook that runs different roles on different hosts. Is it possible to pass a variable from one role running on one host to another role on another host running within the same playbook run? Or any workaround ? playbook host1 …
ady8531
  • 689
  • 5
  • 13
  • 24
11
votes
8 answers

Access variable value using string representing variable's name in C++

If the title was not clear, I will try to clarify what I am asking: Imagine I have a variable called counter, I know I can see its current value by doing something like: std::cout << counter << std::endl; However, assume I have lots of variables…
Paul Ridgway
  • 1,055
  • 2
  • 13
  • 23
11
votes
5 answers

Does Perl have PHP-like dynamic variables?

In PHP, I can write: $vname = 'phone'; $$vname = '555-1234'; print $phone; ... And the script will output "555-1234". Is there any equivalent in Perl? Is there any way to constrain $phone to the scope of the local block, as if I'd written my…
Rob Howard
  • 1,789
  • 2
  • 20
  • 38
11
votes
2 answers

Understanding python's name binding

I am trying to clarify for myself Python's rules for 'assigning' values to variables. Is the following comparison between Python and C++ valid? In C/C++ the statement int a=7 means, memory is allocated for an integer variable called a (the…
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
11
votes
2 answers

Saving a batch variable in a text file

I am trying to save a batch variable into a text file. I currently have this code: @echo off Set var=6 @echo %var%>txt.txt For /f "tokens*" %%i in (txt.txt) do @echo %%i Pause It's supposed to save the 6 into the variable var and then write the…
JoerassicPark
  • 111
  • 1
  • 1
  • 6
11
votes
3 answers

Printing Variable names and contents as debugging tool; looking for emacs/Python shortcut

I find myself adding debugging "print" statements quite often -- stuff like this: print("a_variable_name: %s" % a_variable_name) How do you all do that? Am I being neurotic in trying to find a way to optimize this? I may be working on a function…
Schof
  • 6,329
  • 5
  • 28
  • 38
11
votes
2 answers

UIViewController variables initialization

I'm studying the swift language and I have a doubt concerning the variables initialization in a UIViewController. In my DiagramViewController I have some variables: class DiagramViewController: UIViewController { var type:…
superpuccio
  • 11,674
  • 8
  • 65
  • 93
11
votes
1 answer

var in C# - Why can't it be used as a member variable?

Why is it not possible to have implicitly-typed variables at a class level within C# for when these variables are immediately assigned? ie: public class TheClass { private var aList = new List(); } Is it just something that hasn't been…
David Neale
  • 16,498
  • 6
  • 59
  • 85
11
votes
2 answers

Python regular expressions assigning to named groups

When you use variables (is that the correct word?) in python regular expressions like this: "blah (?P\w+)" ("value" would be the variable), how could you make the variable's value be the text after "blah " to the end of the line or to a certain…
None
  • 3,875
  • 7
  • 43
  • 67
11
votes
1 answer

get_the_id vs. post->ID vs. the_id / get_post_meta

I think it must be pretty basic question but I am only starting. Can someone have a look at the 3 versions of the same (?) code below and say what the difference is? All of them seem to work fine in the loop I am working on. Which should be used:…
TheElear
  • 113
  • 1
  • 5
11
votes
2 answers

align variables name in android studio

I'm trying to find a way to align all the variable names and classes names in android studio, is there a way to do this automatically? For example I want android studio to do this automatically: private void someFunction() { int …
lguenier
  • 187
  • 1
  • 10
11
votes
3 answers

PHP class constant string variable spanning over multiple lines

I want to have a string variable for a PHP class, which would be available to all methods. However, this variable is quite long, so I want to separate it into multiple lines. For example, $variable = "line 1" . "line 2" . …
ericbae
  • 9,604
  • 25
  • 75
  • 108
11
votes
2 answers

Are local variables in Fortran 77 static or stack dynamic?

For my programming languages class one hw problem asks: Are local variables in FORTRAN static or stack dynamic? Are local variables that are INITIALIZED to a default value static or stack dynamic? Show me some code with an explanation to back up…
mm2887
  • 528
  • 2
  • 5
  • 16