Questions tagged [declarations]

16 questions
20
votes
3 answers

Is it good to define a variable inside a loop?

My instructor once told me that I should not define a variable inside a loop, but I honestly still do not understand why. What are the disadvantages of that? Could any body explain that to me?
20
votes
4 answers

Why do we have to mention the data type of the variable in C

Usually in C, we have to tell the computer the type of data in variable declaration. E.g. in the following program, I want to print the sum of two floating point numbers X and Y. #include main() { float X=5.2; float Y=5.1; float Z; …
17
votes
5 answers

Why is *declaration* of data and functions necessary in C language, when the definition is written at the end of the source code?

Consider the following "C" code: #include main() { printf("func:%d",Func_i()); } Func_i() { int i=3; return i; } Func_i() is defined at the end of the source code and no declaration is provide before its use in main(). At the…
10
votes
9 answers

What is the possible disadvantage of putting declarations in inner blocks, instead of at beginning of function?

At the place where I work, there are explicit guidelines for placement of declarations of variables. According to that, it is required to put them at the global level and / or at the beginning of functions, and not in inner blocks (such as a for…
TCSGrad
  • 1,362
7
votes
4 answers

Reason behind multi-line declaration style?

I'm a little curious about the reason behind the Windows header file declaration styles. When I read them, I see multi-line declarations such as the following: WINBASEAPI HANDLE WINAPI GetStdHandle( IN DWORD nStdHandle ); What is the…
user541686
  • 8,178
6
votes
4 answers

C# declaration redundancy

When declaring a member class MyClass { AnyClass> myProp = new AnyClass>(); } is quite redundant. We may find the following, more concise, less…
5
votes
3 answers

Declaring variables in Python and PHP

The question is how to cope with absence of variable declaration in Python, PHP, and the like. In most languages there is a way to let the compiler know whether I introduce a new variable or refer to an existing one: my in Perl (use strict) or…
2
votes
1 answer

How to compute whether it is guaranteed the variable is set?

Assuming declarations are expressions consider such code: if ((var x = foo()) and (var y = x)) or (var z = bar()) then println(z); end The reference to x is OK, because at this point x has to be set, but the reference to z (in println) is not. It…
2
votes
2 answers

Is there a difference between declaring variables outside or inside a loop?

Is there any difference if I were to write something like this: int row,col; for(row = 0; row < data.length; row++){ for(col = 0; col < data[row].length;col++){ //do something }//end for loop(columns) }//end for…
1
vote
2 answers

Scheme's define in Common Lisp

In Common Lisp, we have to use the let form to declare a new lexically-scoped variable. This means that the code either looks like that written in C89 (all variables declared on top of scope), or acquires unreadably deep nesting. let* is somewhat…
1
vote
3 answers

Declaring a field name starting with underscore

Before forming a class in Java or other programming languages that support OOP, should I use underscore (_) in each (local or private) field declaration. More precisely: private String _customername; Is this a correct declaration?
Hakan
  • 113
0
votes
1 answer

Is interleaving local variable declarations with assertions and function calls a bad practice?

In my experience, it is customary to place local variable declarations at the beginning of their scope. Several questions in this forum ask whether this needs to be so, and their answers tend to agree on that variables should be declared nearest to…
0
votes
4 answers

Regarding a variable in C, is an assignment always equivalent to a definition of that variable?

Is there a difference between defining a variable in C and assigning a value to a variable in C? I know that declaring a variable simply means telling the name and its type like int a. On the other hand defining a variable means telling the compiler…
yoyo_fun
  • 2,297
0
votes
3 answers

Understanding the concept of "arguments" and "parameters"

In the third last paragraph at page number 26 of the ebook "The C Programming Language" the author(s) say, "We will generally use parameter for a variable named in the parenthesized list in a function. The terms formal argument and actual argument…
0
votes
2 answers

Where are C variables declared

I see a lot of code with variables declared right after the function, but when I post something like that people end up mad and say it is better to declare them when they are used. I assume this all compiles to the same executable, so it is simply a…
1
2