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
15
votes
4 answers

How properly declare a variable in Swift?

I found quite interesting these different ways to declare a variable in Swift: // METHOD 1 var dogName: String = "Charlie" // METHOD 2 var dogName: String { return "Charlie" } // METHOD 3 let dogName = { return "Charlie" } // METHOD 4 var…
Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
15
votes
1 answer

What are some reasons to use var instead of let in javascript?

With the new keyword let for declaration of variables in javascript ES6, I can no longer think of good reasons to use var. So far, I have been doing just that and I do not see any disadvantage of using let ALL THE TIME. What are good reasons to use…
guagay_wk
  • 26,337
  • 54
  • 186
  • 295
15
votes
2 answers

What does this declaration typedef void foo(); mean?

I don't understand the meaning of typedef void interrupt_handler();. Could someone explain it with some examples? typedef void interrupt_handler();
15
votes
4 answers

Parse a pipe-delimited string into 2, 3, 4 or 5 variables (depending on the input string)

I have a line like this in my code: list($user_id, $name, $limit, $remaining, $reset) = explode('|', $user); The last 3 parameters may or may not be there. Is there a function similar to list that will automatically ignore those last parameters if…
MikeG
  • 1,205
  • 12
  • 19
13
votes
8 answers

How to create a string-type variable in C

Question How to declare a string variable in C? Background In my quest to learn the basics of c, I am trying to port one of my oldest python programs, Bob, to C. In the program, the script asks the user for information on him or herself, and then…
xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
11
votes
2 answers

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

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?)
geza
  • 28,403
  • 6
  • 61
  • 135
11
votes
1 answer

What magic in "our" or "use vars" satisfies "use strict qw(vars)"?

I have working code, but I am trying to understand why it works. I am also trying to learn more about the internals of Perl 5 (perlbrew, perl-5.26.1, Cygwin x64). I know from perlvar and strict that use strict 'vars' works by setting flags in $^H. …
cxw
  • 16,685
  • 2
  • 45
  • 81
11
votes
2 answers

Declaration of variable causes segmentation fault

I don't understand the reason for a segmentation fault error in my program. The code is available here At line 29 I declare a PclImage variable, defined with typedef like an array of struct. The definition of PclImage type is the following (from…
Muffo
  • 1,733
  • 2
  • 19
  • 29
11
votes
2 answers

Can variables declared inside a for loop affect the performance of the loop?

I have done my homework and found repeated assurances that it makes no difference in performance whether you declare your variables inside or outside your for loop, and it actually compiles to the very same MSIL. But I have been fiddling with it…
tethered.sun
  • 149
  • 3
  • 14
11
votes
3 answers

Check if variable been initialized in PHP

I have been implementing a Wordpress plugin and I faced a problem with finding out if a variable has been declared or not. Let's say I have a model named Hello; this model has 2 variables as hello_id and hello_name. In the database we have table…
Chyngyz Sydykov
  • 430
  • 2
  • 6
  • 18
10
votes
2 answers

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

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; }
Sergii
  • 255
  • 2
  • 5
10
votes
6 answers

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 to allocate memory for a new variable each time…
Eric Anastas
  • 21,675
  • 38
  • 142
  • 236
10
votes
1 answer

Comma in variable initialization/declaration

I've stumbled upon a piece of code that look as follows: void check() { int integer = 7; //integer2 is not declared anywhere int check = integer, integer2; //after running //check = 7 …
Yoav
  • 3,326
  • 3
  • 32
  • 73
10
votes
7 answers

Java switch : variable declaration and scope

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…
9
votes
4 answers

What is the '@(' doing in this Perl code?

In this code snippet: use strict; use warnings; use Data::Dumper; my $r = [qw(testing this thing)]; print Dumper($r); foreach my $row (@({$r}) { print "$row\n"; $row .= 'mod'; } print Dumper($r); …