Questions tagged [variable-declaration]

In computer programming, variable declaration specifies properties of a variable (such as its type).

In computer programming, variable declaration specifies properties of a variable:

  • It associates a type and an identifier (or name) with the variable.
  • It allows the compiler to decide how much storage space to allocate for storage of the value associated with the identifier and to assign an address for each variable which can be used in code generation.
663 questions
677
votes
12 answers

How can I unset a JavaScript variable?

I have a global variable in JavaScript (actually a window property, but I don't think it matters) which was already populated by a previous script, but I don't want another script that will run later to see its value or that it was even…
Guss
  • 30,470
  • 17
  • 104
  • 128
541
votes
4 answers

When to use extern in C++

I'm reading "Think in C++" and it just introduced the extern declaration. For example: extern int x; extern float y; I think I understand the meaning (declaration without definition), but I wonder when it proves useful. Can someone provide an…
Aslan986
  • 9,984
  • 11
  • 44
  • 75
503
votes
12 answers

C pointer to array/array of pointers disambiguation

What is the difference between the following declarations: int* arr1[8]; int (*arr2)[8]; int *(arr3[8]); What is the general rule for understanding more complex declarations?
George
  • 15,241
  • 22
  • 66
  • 83
389
votes
9 answers

Declaring variables inside loops, good practice or bad practice?

Question #1: Is declaring a variable inside a loop a good practice or bad practice? I've read the other threads about whether or not there is a performance issue (most said no), and that you should always declare variables as close to where they are…
JeramyRR
  • 4,273
  • 3
  • 20
  • 20
389
votes
14 answers

Is it possible only to declare a variable without assigning any value in Python?

Is it possible to declare a variable in Python, like so?: var so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why? EDIT: I want to do this for cases like…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
192
votes
5 answers

Can I simultaneously declare and assign a variable in VBA?

Can I convert the following declaration and assignment into one line: Dim clientToTest As String clientToTest = clientsToTest(i) or Dim clientString As Variant clientString = Split(clientToTest)
Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
169
votes
13 answers

Is there any overhead to declaring a variable within a loop? (C++)

I am just wondering if there would be any loss of speed or efficiency if you did something like this: int i = 0; while(i < 100) { int var = 4; i++; } which declares int var one hundred times. It seems to me like there would be, but I'm not…
user98188
159
votes
8 answers

Why do some variables declared using let inside a function become available in another function, while others result in a reference error?

I can't understand why variables act so strange when declared inside a function. In the first function I declare with let the variables b and c with the value 10: b = c = 10; In the second function I show: b + ", " + c And this shows: 10,…
bleat interteiment
  • 1,429
  • 2
  • 10
  • 15
152
votes
8 answers

C++, variable declaration in 'if' expression

What's going on here? if(int a = Func1()) { // Works. } if((int a = Func1())) { // Fails to compile. } if((int a = Func1()) && (int b = Func2())) ) { // Do stuff with a and b. // This is what I'd really like to be able to…
Neutrino
  • 8,496
  • 4
  • 57
  • 83
112
votes
7 answers

Python Variable Declaration

I want to clarify how variables are declared in Python. I have seen variable declaration as class writer: path = "" sometimes, there is no explicit declaration but just initialization using __init__: def __init__(self, name): self.name =…
bsr
  • 57,282
  • 86
  • 216
  • 316
103
votes
8 answers

What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?

Looking at an online source code I came across this at the top of several source files. var FOO = FOO || {}; FOO.Bar = …; But I have no idea what || {} does. I know {} is equal to new Object() and I think the || is for something like "if it already…
Ricardo Sanchez
  • 4,935
  • 11
  • 56
  • 86
98
votes
11 answers

Declare and assign multiple string variables at the same time

I'm declaring some strings that are empty, so it won't throw errors later on. I've read that this was the proper way: string Camnr = Klantnr = Ordernr = Bonnr = Volgnr = Omschrijving = Startdatum = Bonprioriteit = Matsoort = Dikte = Draaibaarheid =…
Mathlight
  • 6,436
  • 17
  • 62
  • 107
81
votes
7 answers

Variable declaration after goto Label

Today I found one interesting thing. I didn't know that one can't declare a variable after a goto label. Compiling the following code #include int main() { int x = 5; goto JUMP; printf("x is : %d\n",x); JUMP: int a = 0; …
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
69
votes
9 answers

Is it possible to declare a public variable in vba and assign a default value?

I want to do this but it won't compile: Public MyVariable as Integer = 123 What's the best way of achieving this?
David
  • 2,101
  • 2
  • 32
  • 41
64
votes
6 answers

C++11 auto declaration with and without pointer declarator

What's the difference between the types of bar1 and bar2? int foo = 10; auto bar1 = &foo; auto *bar2 = &foo; If both bar1 and bar2 are int*, does it makes sense to write the pointer declarator (*) in the bar2 declaration?
Henry Barker
  • 784
  • 1
  • 5
  • 9
1
2 3
44 45