variable-declaration

Understanding variable scope in Go

萝らか妹 提交于 2019-12-01 17:51:02
I am going through the Go specification to learn the language, and these points are taken from the spec under " Declarations and scope ." Though I am able to understand points 1-4, I am confused on points 5 and 6: The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block. The scope of a type identifier declared inside a function begins at the identifier in the TypeSpec and ends at the end of the innermost containing block. This is

javascript var statement and performance

我怕爱的太早我们不能终老 提交于 2019-12-01 17:21:43
Option1 : multiple var without assignment function MyFunction() { var a = null; var b = null; .... var z = null; a = SomeValue; b = SomeValue2; .... } Option 2: one var statement, no assignment function MyFunction() { var a, b ..., z; a = SomeValue; b = SomeValue2; .... } Option 3: multiple var statements with assignment function MyFunction() { var a = SomeValue; var b = SomeValue2; .... var z = SomeValue26; } Is there any performance benefit of using a particular option? Is it true for both primitive type assignments AND object reference assignments? Thanks for your input. "premature

Asterisks in variable declarations in VB6

耗尽温柔 提交于 2019-12-01 17:14:01
问题 What's the meaning of the asterisk (*) and the number, after the variable declaration? As seen in WpName As String * 6 Public Type WayPoint WpIndex As Integer WpName As String * 6 WpLat As Double WpLon As Double WpLatDir As String * 1 WpLonDir As String * 1 End Type 回答1: The asterisk declares the variable as a fixed-length string, where the number indicates the length of the string: http://www.1sayfa.com/1024/diger/vb/ch07.htm#Heading8 The declaration of a fixed-length string variable

Can the 'auto' keyword be used as a storage class specifier in C++11?

别说谁变了你拦得住时间么 提交于 2019-12-01 15:32:43
Can the auto keyword be used as a storage class specifier in C++11? Is the following code legal in C++11? int main() { auto int x; } Prasoon Saurav No the code is ill-formed in C++11. auto in C++11 would be used to deduce the type of a variable from its initializer and it can't be used as a storage class specifier. Correct Usage int main() { auto x = 12; // x is an int auto y = 12.3; // y is a double } auto int x; is circular - you are literally declaring the type as an int . given that you had this information - there is no reason to not simply use: int x; if you wanted to declare x the type

How can I tell Visual Studio/Microsoft's C compiler to allow variable declarations after the first statement?

二次信任 提交于 2019-12-01 15:05:46
I have code that compiles on the GNUARM compiler, but Visual Studio 2010 issues errors. The issue involves declaring variables after the first statement in a C language file: main.c #include <stdio.h> #include <stdlib.h> int main(void) { int i = 6; i = i + 1; printf("Value of i is: %d\n", i); int j = i * 10; // <-- This is what Visual Studio 2010 complains about. printf("Value of j is: %d\n", j); return EXIT_SUCCESS; } The following code compiles without errors: #include <stdio.h> #include <stdlib.h> int main(void) { int i = 6; int j; // <-- Declaration is now here, valid according to K&R

Can the 'auto' keyword be used as a storage class specifier in C++11?

北慕城南 提交于 2019-12-01 14:32:38
问题 Can the auto keyword be used as a storage class specifier in C++11? Is the following code legal in C++11? int main() { auto int x; } 回答1: No the code is ill-formed in C++11. auto in C++11 would be used to deduce the type of a variable from its initializer and it can't be used as a storage class specifier. Correct Usage int main() { auto x = 12; // x is an int auto y = 12.3; // y is a double } 回答2: auto int x; is circular - you are literally declaring the type as an int . given that you had

How can I tell Visual Studio/Microsoft's C compiler to allow variable declarations after the first statement?

限于喜欢 提交于 2019-12-01 13:56:55
问题 I have code that compiles on the GNUARM compiler, but Visual Studio 2010 issues errors. The issue involves declaring variables after the first statement in a C language file: main.c #include <stdio.h> #include <stdlib.h> int main(void) { int i = 6; i = i + 1; printf("Value of i is: %d\n", i); int j = i * 10; // <-- This is what Visual Studio 2010 complains about. printf("Value of j is: %d\n", j); return EXIT_SUCCESS; } The following code compiles without errors: #include <stdio.h> #include

Is there a way to define variables of two different types in a for loop initializer?

淺唱寂寞╮ 提交于 2019-12-01 02:52:39
问题 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 variables of different types: int main() { for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) { cout << j << endl; } } Is there a way to do this? (I don't need to use i inside the loop, just j .) If you have totally hacked and obscure solution, It's OK for me. In this contrived example I know you could just use double

Does C have One Definition Rule like C++?

我的未来我决定 提交于 2019-11-30 16:35:59
问题 Recently, I found out there are some cases that will absolutely violate the ODR of C++ but will be compiled OK in C compiler. For example, this wierd scenario (with me): Source 1 int var_global=-3; Source 2 #include <stdio.h> #include <conio.h> unsigned int var_global; int main() { printf("%d \n",var_global); getch(); return 0; } I have the printed result is -3 (even though in Source 2 var_global is unsigned ) and there is no error about the redefined of var_global . I knew that C have

Function declarations precedence/overwriting variable declarations? Hoisting? Why?

孤街浪徒 提交于 2019-11-30 09:44:37
问题 Snippet 1: var a; // undefined variable named 'a' function a(foo) { // a function named 'a' var foo = "Hello World"; console.log(foo); } console.log(a); // output is: [Function: a], but why not undefined? Snippet 2: function a(foo) { // a function named 'a' var foo = "Hello World"; console.log(foo); } var a; // undefined variable named 'a' console.log(a); // output is: [Function: a], but why not undefined? I could have just shown Snippet 1 to ask this question - however I showed both just for