The variable-declaration tag has no wiki summary.
42
votes
11answers
14k views
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 ...
28
votes
8answers
788 views
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. ...
29
votes
18answers
2k views
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 ...
16
votes
5answers
646 views
Can I declare variables of different types in the initialization of a for loop?
Why does this C++ code not compile under VS2010:
for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {}
while this one does:
short b = 0;
for ( int a = 0; a < 10; ++a, ++b ) {}
Is the ...
7
votes
1answer
598 views
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 ...
5
votes
3answers
665 views
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 ...
10
votes
8answers
2k views
Variable declaration after goto Label - C
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 <stdio.h>
int main() {
int x = 5;
goto ...
5
votes
13answers
896 views
Is there a way to define variables of two different types in a for loop initializer?
You can define 2 variables of the same type in a for loop:
int main() {
for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
But it is illegal to define ...
32
votes
5answers
3k views
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 ...
11
votes
8answers
50k views
Is it possible only to declare a variable without assigning any value in Python?
Is it possible to just declaring a variable in Python, like so?:
var
so that it's gonna be initialized to None? It seems like python allows this, but as soon as you access it, it crashes. Is this ...
8
votes
3answers
145 views
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;
...
30
votes
5answers
4k views
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;
...
2
votes
5answers
2k views
What is the difference between int* ptr and int *ptr in C? [duplicate]
Possible Duplicate:
C: is there a difference between “int* fooBar;” and “int *fooBar;”?
I am fairly new at C and I don't know the difference between the following ...
6
votes
6answers
621 views
Should variable declarations always be placed outside of a loop?
Is it better to declare a variable used in a loop outside of the loop rather then inside? Sometimes I see examples where a variable is declared inside the loop. Does this effectively cause the program ...
5
votes
1answer
595 views
Declaring Variables in @implementation
I saw an example in a book showing this code:
@implementation ViewController
{
NSString *name;
}
Why not declare this in @interface? What's the difference in declaring variables in ...