K&R2 variable definition

C

Clausfor

Hello everybody,
I cannot find where in the K&R2 it is stated that variables must be
defined at the beginning
of a block and not within other lines.
In Section 1.2 I read:
In C, all variables must be declared before they are used, usually at
the beginning of the
function before any executable statements.
USUALLY does not mean ABSOLUTELY, correct?
I know that in C99 it is possible, but I'm interested in C89.
Can anyone tell me something about?
Thank you!
Claus
 
Z

Zara

Hello everybody,
I cannot find where in the K&R2 it is stated that variables must be
defined at the beginning
of a block and not within other lines.
In Section 1.2 I read:
In C, all variables must be declared before they are used, usually at
the beginning of the
function before any executable statements.
USUALLY does not mean ABSOLUTELY, correct?
I know that in C99 it is possible, but I'm interested in C89.
Can anyone tell me something about?
Thank you!
Claus
Take a look at page 25:

"A funtion definition has this form:

return-type function-name (parameter declarations, if any)
{
declarations
statements
}
"

That's it, and it's clear

Zara
 
R

Richard Tobin

Clausfor said:
In C, all variables must be declared before they are used, usually at
the beginning of the
function before any executable statements.
USUALLY does not mean ABSOLUTELY, correct?

You can declare variable in inner blocks:

int foo(void)
{
int a;
...
{
int b;
...
}
}

-- Richard
 
C

Chris Dollin

Clausfor said:
Hello everybody,
I cannot find where in the K&R2 it is stated that variables must be
defined at the beginning
of a block and not within other lines.

Try the grammar.

(Can't cite page & name, as my copy of K&R2 is at home.)
 
R

Richard Heathfield

Clausfor said:
Hello everybody,
I cannot find where in the K&R2 it is stated that variables must be
defined at the beginning
of a block and not within other lines.

You're looking for an explicit prohibition, but you won't find one. What you
have instead is absence of permission. See page 236, grammar for
compound-statement, which shows that declarations *may* occur at the
beginning of a compound statement. Now look for other places in the grammar
where declarations *may* occur, and look at... well, at what isn't there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top