473,322 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Places to declare variables

Hi,

I recently noticed that my compiler (VC++7.1) allows me to declare
variables inside the expression for an if-statement, like so:

if (int i = 0)
{
i = 1;
}

Now I was wondering if this is valid code (reference to the
appropriate section/paragraph in the Standard would be nice, as I do not
really know where to look for this kind of thing) and where else I am
able to declare variables (except the obvious "for (int i ...)" case).

For the record, I am asking this out of interest, not because I want
to start declaring my variables in all kinds of funny places :)

Thanks in advance!
--
jb

(reply address in rot13, unscramble first)
Feb 9 '06 #1
6 3507

Jakob Bieling wrote:
Hi,

I recently noticed that my compiler (VC++7.1) allows me to declare
variables inside the expression for an if-statement, like so:

if (int i = 0)
{
i = 1;
}
if ((int x = calcSomething()) == y)
{
doSomethingWith(x);
}

.... code not accessing x.

You could even do something like this:

int x = 5;

if ((int x = calcSomething()) == y)
{
doSomethingWithCalced(x);
}

assert(x == 5);

.... This code runs...;

Standard defines 'x' to have scope only within the following block of
code and it overrides the upper-scope definitions.

reference to the
appropriate section/paragraph in the Standard would be nice, as I do not
really know where to look for this kind of thing


3.3.2 paragraph 4 is one such reference but only explains scope.

Feb 9 '06 #2
Jakob Bieling wrote:
Hi,

I recently noticed that my compiler (VC++7.1) allows me to declare
variables inside the expression for an if-statement, like so:

if (int i = 0)
{
i = 1;
}

Now I was wondering if this is valid code (reference to the
appropriate section/paragraph in the Standard would be nice, as I do not
really know where to look for this kind of thing) and where else I am
able to declare variables (except the obvious "for (int i ...)" case).

For the record, I am asking this out of interest, not because I want
to start declaring my variables in all kinds of funny places :)

Thanks in advance!
--
jb

(reply address in rot13, unscramble first)


That is certainly legal. I don't have the standard handy, but see
Stroustrup _TC++PL_ 3rd ed. on dynamic_cast. He does something like
this, which is where the construction is most useful:

if( Derived* pd = dynamic_cast<Derived*>( pBase ) )
{
// pd is valid; do something with it here
}
else
{
// pd is 0 since the dynamic_cast failed;
// do something else here, e.g.
pd = new Derived;
// ...
}
// pd is now out of scope

Cheers! --M

Feb 9 '06 #3

Jakob Bieling wrote:
Hi,

I recently noticed that my compiler (VC++7.1) allows me to declare
variables inside the expression for an if-statement, like so:

if (int i = 0)
{
i = 1;
}


Keep in mind though that the above code does nothing worthwhile as the
value of "int i = 0" is the assigned value; in this case 0, which is
"false". So, i is created, initialized to 0, compared to 0, and then
goes away. The compiler might even toss all that code out.

Feb 9 '06 #4
Jakob Bieling wrote:
Hi,

I recently noticed that my compiler (VC++7.1) allows me to declare
variables inside the expression for an if-statement, like so:

if (int i = 0)
{
i = 1;
}

Now I was wondering if this is valid code (reference to the
appropriate section/paragraph in the Standard would be nice, as I do not
really know where to look for this kind of thing) and where else I am
able to declare variables (except the obvious "for (int i ...)" case).


See 6.4/3.

Feb 9 '06 #5
"Jakob Bieling" <ar****************@rot13.com> schrieb im Newsbeitrag
news:ds*************@news.t-online.com...
Now I was wondering if this is valid code (reference to the appropriate
section/paragraph in the Standard would be nice, as I do not really know
where to look for this kind of thing) and where else I am able to declare
variables (except the obvious "for (int i ...)" case).

For the record, I am asking this out of interest, not because I want to
start declaring my variables in all kinds of funny places :)


There is only one funny place to declare/define a variable -- at the top of
a function body. Variables should be defined as late as possible, and that
is the time of its first use. And of cause a variable should always be
initialized immediately.

That's one of the reason why in C++ you can define a variable almost
everywhere, where you can write a statement, and even at some other places
for statements. The only thing you must not do, you must never jump over a
definition. So the following code is not valid:

switch (something)
{
case 1:
int i = 1;
...
break;
case 2:
...
}

You cannot define a variable inside a switch, but if you write clean code
and such a variable is only used in one case, you can make it valid by using
a compound statement, which restricts the scope of such a variable:

switch (something)
{
case 1:
{
int i = 1;
...
}
break;
case 2:
...
}

Regards
Heinz
Feb 9 '06 #6
Heinz Ozwirk wrote:
That's one of the reason why in C++ you can define a variable almost
everywhere, where you can write a statement, and even at some other places
for statements. The only thing you must not do, you must never jump over a
definition. So the following code is not valid:

switch (something)
{
case 1:
int i = 1;
...
break;
case 2:
...
}
That's interesting.
You cannot define a variable inside a switch, but if you write clean code
and such a variable is only used in one case, you can make it valid by using
a compound statement, which restricts the scope of such a variable:

switch (something)
{
case 1:
{
int i = 1;
...
}
break;
case 2:
...
}


I have a habit of creating scope here anyway, in much the same way that
do it for the if construct.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 10 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
3
by: Todd | last post by:
I have been a VB programmer for about 2 years. I'm a new user to VB.net, about two weeks; I'm trying to write just a small App to start with, that will do a Net send function. I would like to...
5
by: dancer | last post by:
Using ASP.net 1.1 and VB Is it possible to declare variables in their own subroutine? I had my DIM statements within a sub that worked just fine. I wanted to be able to use them in another...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.