variable-declaration

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

爷,独闯天下 提交于 2019-11-29 16:45:21
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 completeness purposes. I have wrote some brief comments in them as well. My question is, in both cases

Using a variable with the same name in different spaces

柔情痞子 提交于 2019-11-29 13:06:57
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... in the error box when I click continue the program resumes and x has a corrupted content (like -8556328 instead of 15 ). Why does this code work without a problem, and the int array is well declared? const int x = 5; int main() { int x[x] = {1,2,3,4}; return 0; } x is defined at the left of = . so in x[x] , [x] refer to the global one, whereas in x = x; , x

Using the letter L in long variable declaration

*爱你&永不变心* 提交于 2019-11-29 10:28:36
long l2 = 32; When I use the above statement, I don't get an error (I did not used l at the end), but when I use the below statement, I get this error: The literal 3244444444 of type int is out of range long l2 = 3244444444; If I use long l2 = 3244444444l; , then there's no error. What is the reason for this? Using l is not mandatory for long variables. 3244444444 is interpreted as a literal integer but can't fit in a 32-bit int variable. It needs to be a literal long value , so it needs an l or L at the end: long l2 = 3244444444l; // or 3244444444L More info: Primitive Data Types ,

Is it well-formed, if I redefine a variable as auto, and the deduced type is the same? [duplicate]

徘徊边缘 提交于 2019-11-29 09:29:15
This question already has an answer here: Does a declaration using “auto” match an extern declaration that uses a concrete type specifier? 3 answers Look at this snippet: int a; extern int b; auto b = a; Is it well-formed? Clang successfully compiles it, but GCC and MSVC don't. (This issue has come up when I answered How to declare and define a static member with deduced type? ) Tl;DR; clang is correct, the logic is that this is allowed by [dcl.spec.auto] and to restrict this for deduced return types [dcl.spec.auto]p11 was added otherwise there is no restriction and therefore this is not

C error: Expected expression before int

馋奶兔 提交于 2019-11-28 17:00:45
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? sheu This is actually a fairly interesting question. It's not as simple as it looks at first. For reference, I'm going to be basing this off of the latest C11 language grammar defined in N1570 I guess the counter-intuitive part of the question is: if this is correct C: if (a == 1) { int b = 10; } then why is this not also correct C? if (a == 1) int b = 10; I mean, a one-line conditional if statement should be fine either with or without

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

南楼画角 提交于 2019-11-28 15:10:14
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 this: value for index in sequence: if value == None and conditionMet: value = index break Duplicate Uninitialised value in python (by same author) Are there any declaration keywords in Python? (by the same author) Related Python: variable scope and function calls Other languages have "variables" Why not just do this: var = None Python is dynamic, so you don

Python - Difference between variable value declaration on Fibonacci function

痞子三分冷 提交于 2019-11-28 14:17:32
I'm kind of beginner in python. I was looking at one the types to make a fibonacci function, def fib(n): a=0 b=1 while a<n: print a a,b=b,a+b and I saw the a,b=b,a+b declaration. So, I thought a=b and b=a+b were the same to a,b=a,b+a, so I changed the function for it to be like this: def fib(n): a=0 b=1 while a<n: print a a=b b=a+b and I thought it would be right, but when I executed the program, I got a different output. Can someone explain to me the difference between those two types of declaration? Thanks, anyway. b, a+b creates a tuple containing those two values. Then a, b = ... unpacks

Java switch : variable declaration and scope

北战南征 提交于 2019-11-28 12:13:19
How does the Java compiler handle the following switch block ? What is the scope of the 'b' variable ? Note that the 'b' variable is declared only in the first branch of the switch statement. Attempting to declare it in the second branch as well results in a "duplicate local variable" compilation error. int a = 3; switch( a ) { case 0: int b = 1; System.out.println("case 0: b = " + b); break; case 1: // the following line does not compile: b may not have been initialized // System.out.println("case 1 before: b = " + b); b = 2; System.out.println("case 1 after: b = " + b); break; default: b = 7

Why is no ReferenceError being thrown if a variable is used before it’s declared?

本小妞迷上赌 提交于 2019-11-28 11:35:51
I’m trying to wrap my head around the behavior of reference errors thrown in JavaScript. In the following example, a ReferenceError is thrown at the second line, and execution breaks: var obj = {}; obj.func1 = func2; alert('Completed'); Whereas in this example, the code completes successfully, though obj.func1 remains undefined : var obj = {}; obj.func1 = func2; var func2 = function() { alert('func2'); }; alert('Completed'); My assumption was that an error would be thrown at the second line just the same, and when that wasn’t the case, I’d have expected obj.func1 to properly reference func2 ,

What is the difference between int* ptr and int *ptr in C? [duplicate]

冷暖自知 提交于 2019-11-28 11:27:51
This question already has an answer here: Difference between int* p and int *p declaration 4 answers difference between int* i and int *i 8 answers I am fairly new at C and I don't know the difference between the following two variable declarations: int* ptr; int *ptr; I think that in the declaration int* ptr; , ptr 's value cannot be changed whereas it can be changed for the declaration, int *ptr; I am not sure if that is it though. What is the concept behind the two declarations? To the compiler, there is no difference between the two declarations. To the human reader, the former may imply