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
9
votes
5 answers

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

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…
anpatel
  • 1,952
  • 4
  • 19
  • 36
9
votes
1 answer

php static in if statement

I have a construction like this in my config file: So why the $config contains 2 if this part of…
Aldekein
  • 3,538
  • 2
  • 29
  • 33
9
votes
4 answers

Should I use static variables in my functions to prevent recomputing values?

I've been writing C++ code for a while now, but there's something I've been wondering for some time, without being to find a clear answer. My point here is the following: let's say I have a function (could be a method, could be static, but not…
Thomas Kowalski
  • 1,995
  • 4
  • 20
  • 42
9
votes
2 answers

Understanding variable scope in Go

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…
MBB
  • 1,635
  • 3
  • 9
  • 19
9
votes
1 answer

Declaration and assignment of variables: silently dropping of assigned values

You can declare a list of variables and assign them some value in Perl 6 my ($a, $b) = 33,44 # $a will be 33 and $b 44 However, if you try to assign the value following the declaration of the variable the values will be silently dropped my ($a =…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
9
votes
3 answers

Is the second int in a multiple declaration always set to 1?

In this Code Golf post, there is the claim that “the second variable in a definition is always set to 1“, making this a well-formed line: int i=-1,c,o,w,b,e=b=w=o=c; And supposedly everything except i is set to 1 because c is automatically 1. I…
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
9
votes
1 answer

F#: Destructuring bind with a discriminated union

open System let x = (1, 2) let (p, q) = x printfn "A %A" x printfn "B %A %A" p q let y = Some(1, 2) try let None = y () with | ex -> printfn "C %A" ex let Some(r, s) = y printfn "D %A" y // printfn "E %A %A" r…
nodakai
  • 7,773
  • 3
  • 30
  • 60
9
votes
3 answers

dynamically declare/create lists in python

I am a beginner in python and met with a requirement to declare/create some lists dynamically for in python script. I need something like to create 4 list objects like depth_1,depth_2,depth_3,depth_4 on giving an input of 4.Like for (i = 1; i <=…
Cheese
  • 245
  • 2
  • 3
  • 9
8
votes
5 answers

In C#, is there way to define an enum and an instance of that enum at the same time?

Looking for a code optimization in c# that allows me to both define an enum and create a variable of that enum's type simultaniously: Before: enum State {State1, State2, State3}; State state = State.State1; After (doesn't work): enum…
Dave
  • 83
  • 1
  • 1
  • 3
8
votes
1 answer

Take let variable out of temporal dead zone

See this code: The first script attempts to let-declare foo via a destructuring assignment. However, null can't be…
Oriol
  • 274,082
  • 63
  • 437
  • 513
8
votes
2 answers

Advantage of telling the Swift Compiler an object's type, instead of inferring?

I have been doing Swift programming for a few months now and I have always been curious about this... Is there an advantage to telling the Swift compiler the type of an object in its declaration? I.e.let image: UIImage = UIImage() Compared to NOT…
justColbs
  • 1,504
  • 2
  • 18
  • 28
8
votes
3 answers

Skip variable declaration using goto?

I am reading C Programming - A Modern Approach by K.N.King to learn the C programming language and it was noted that goto statements must not skip variable-length array declarations. But now the question is: Why are goto jumps allowed to skip…
MinecraftShamrock
  • 3,504
  • 2
  • 25
  • 44
8
votes
4 answers

Is setting a boolean to false redundant?

I have read several previously asked questions and answers on this topic [or quite similar], but none of them have really addressed this point-blank. When declaring a new boolean varibale, is it redundant [e.g. unnecessary] to initialize it to…
triforceofcourage
  • 4,928
  • 2
  • 16
  • 21
8
votes
2 answers

What's the difference between `extern int (x)[]` and `extern int x[]` in C?

There is declaration extern int (x)[] at the end of this article. Are the parentheses doing anything or they are just for confusion? My guess would be that with parentheses x is an array of external integers (and that's what the article says), but…
Džuris
  • 2,115
  • 3
  • 27
  • 55
8
votes
2 answers

Using the letter L in long variable declaration

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 =…
PSR
  • 39,804
  • 41
  • 111
  • 151