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

Why does `int ;` compile fine in C, but not in C++?

Consider the following program (see live demo here). #include int main(void) { int ; // Missing variable name puts("Surprise"); } My compiler, gcc 4.8.1, gives the below warning: [Warning] useless type name in empty…
Destructor
  • 14,123
  • 11
  • 61
  • 126
62
votes
3 answers

C++11 - declaring non-static data members as 'auto'

Does C++11 allow declaring non-static data members as 'auto' if they are initialized in the declaration? For example: struct S { auto x = 5; // in place of 'int x = 5;', which is definitely allowed }; GCC 4.7 rejects the above code, while it…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
59
votes
4 answers

setq and defvar in Lisp

I see that the Practical Common Lisp uses (defvar *db* nil) for setting up a global variable. Isn't it OK to use setq for the same purpose? What are the advantages/disadvantages of using defvar vs. setq?
prosseek
  • 182,215
  • 215
  • 566
  • 871
49
votes
5 answers

Why doesn't C# let you declare multiple variables using var?

Given the following: // not a problem int i = 2, j = 3; so it surprises me that this: // compiler error: Implicitly-typed local variables cannot have multiple declarators var i = 2, j = 3; doesn't compile. Maybe there is something I don't…
aarona
  • 35,986
  • 41
  • 138
  • 186
45
votes
7 answers

name 'times' is used prior to global declaration - But IT IS declared

I'm coding a small program to time and show, in a ordered fashion, my Rubik's cube solvings. But Python (3) keeps bothering me about times being used prior to global declaration. But what's strange is that IT IS declared, right on the beggining, as…
41
votes
4 answers

Using a variable with the same name in different spaces

This code compiles, but I have a run time error in Visual Studio: Run-time check failure #3 - the variable 'x' is being used without being initialized... int x = 15; int main() { int x = x; return 0; } I don't understand that behavior...…
Aminos
  • 754
  • 1
  • 20
  • 40
41
votes
3 answers

C error: Expected expression before int

When I tried the following code I get the error mentioned. if(a==1) int b =10; But the following is syntactically correct if(a==1) { int b = 10; } Why is this?
38
votes
2 answers

What is the purpose of a declaration like int (x); or int (x) = 10;

If you look at the grammar for *declarator*s in §8/4 you'll notice that a noptr-declarator can be written as (ptr-declarator), that is, it can be written as (declarator-id), which validates declarations like the ones in the title. As matter of fact…
Mao
  • 1,065
  • 8
  • 12
37
votes
18 answers

Use of "var" type in variable declaration

Our internal audit suggests us to use explicit variable type declaration instead of using the keyword var. They argue that using of var "may lead to unexpected results in some cases". I am not aware of any difference between explicit type…
Petr Kozelek
  • 1,126
  • 8
  • 14
36
votes
8 answers

How can a variable be used when its definition is bypassed?

In my mind, always, definition means storage allocation. In the following code, int i allocates a 4-byte (typically) storage on program stack and bind it to i, and i = 3 assigns 3 to that storage. But because of goto, definition is bypassed which…
Eric Z
  • 14,327
  • 7
  • 45
  • 69
33
votes
1 answer

Declaring javascript variables as specific types

The title says it all, but I will provide more clarification: After seeing many samples of javascript where all variables are declared as type var, and seeing support for other datatypes, why aren't variables of a specific datatype declared as such?…
CBredlow
  • 2,790
  • 2
  • 28
  • 47
31
votes
3 answers

Possible to initialize an array after the declaration in C?

Is there a way to declare a variable like this before actually initializing it? CGFloat components[8] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15 }; I'd like it declared something like this (except this doesn't work): …
RyJ
  • 3,995
  • 6
  • 34
  • 54
31
votes
4 answers

Golang mixed assignment and declaration

I started working with go for a few weeks, and (once again) I stumbled across something that seems odd for me: // Not working a := 1 { a, b := 2, 3 } // Works a := 1 a, b := 2, 3 playground I want to assign two variables simultaneously. One is…
Procrade
  • 689
  • 1
  • 7
  • 10
31
votes
1 answer

How to declare a variable in MySQL for a normal query?

How can I declare a variable for a normal query in MySQL? e.g., declare @myVar date; set @myVar = something; select * from someTable where someColumn = @myVar; I tried and the syntax seems to be wrong...what am I missing?
EOB
  • 2,975
  • 17
  • 43
  • 70
29
votes
4 answers

Effect of declared and undeclared variables

What is the major difference between JavaScript declared and undeclared variables, since the delete operator doesn't work on declared variables? var y = 43; // declares a new variable x = 42; delete x; // returns true (x is a property…
1
2
3
44 45