Viariables definitions must be first in function definition?

H

Hongzheng Wang

Hi, all.

According to ISO stand, in any function definition block, the
viariables definitions statements must be the first? That is,
int func()
{
int i; /* statement 1 */
double a; /* statement 2 */
func_a(); /* statement 3 */
...

Can statements 1 and 2 be put after 3?

Thank you.

--
Hongzheng Wang
Department of Electronic Engineering
Tsinghua University
Beijing 100084, China
Tel: (+86 10) 6278 2690
 
E

E. Robert Tisdale

Hongzheng said:
According to ISO [C 89] standard,
in any function definition block,
the viariables definitions statements must be the first?

The C 99 standard lifts this restriction.
That is,

int func() {
int i; /* statement 1 */
double a; /* statement 2 */
func_a(); /* statement 3 */
...

Can statements 1 and 2 be put after 3?
Yes.

> cat func.c
int func(void) {
extern
void func_a(void);
func_a(); // statement 3
int i = 0; // statement 1
double a = 0.0;// statement 2
extern
void func_b(double);
func_b(a);
return i;
}
 
L

Liang Chen

Hongzheng Wang said:
Hi, all.

According to ISO stand, in any function definition block, the
viariables definitions statements must be the first? That is,
int func()
{
int i; /* statement 1 */
double a; /* statement 2 */
func_a(); /* statement 3 */
...

Can statements 1 and 2 be put after 3?

Thank you.

--
Hongzheng Wang
Department of Electronic Engineering
Tsinghua University
Beijing 100084, China
Tel: (+86 10) 6278 2690

Of course not, if you use a C89 complier.
 
R

Richard Bos

Hongzheng Wang said:
According to ISO stand, in any function definition block, the
viariables definitions statements must be the first? That is,
int func()
{
int i; /* statement 1 */
double a; /* statement 2 */
func_a(); /* statement 3 */
...

Can statements 1 and 2 be put after 3?

As the others said, they can under the C99 Standard, but not under the
C89 Standard. If you use a C99 compiler you are probably aware of it, so
in your case, they probably can't.
Allow me to add something, though. It's not just function definition
blocks. This is allowed in _any_ block. For example, this:


int func()
{
int i;
call_function();
{ /* Note: new block. */
double d;
call_another_function();
}
last_function();
}

is allowed. However, by the time last_function() is called, d has gone
out of scope an can no longer be used; this can be an advantage or a
disadvantage depending on what you're trying to do.

Richard
 
H

Hongzheng Wang

Thank you all!
I also think so. But the GCC's behavior confued me. I have such a
example program:

#include <stdio.h>

int main()
{
int i = 1;
printf("%d\n", i);
int j = 0;
printf("hello %d\n", j);
return 0;
}

When I compiled it using `gcc test.c -o test -std=c89 -Wall`, it did not
complained about this problem. I think I should reread the manual.

BTW: my system is debian linux box, with gcc 3.3.3

--
Hongzheng Wang
Department of Electronic Engineering
Tsinghua University
Beijing 100084, China
Tel: (+86 10) 6278 2690
 
E

E. Robert Tisdale

Hongzheng said:
Thank you all!
I also think so. But the GCC's behavior confued me.
I have such a example program:

#include <stdio.h>

int main() {
int i = 1;
printf("%d\n", i);
int j = 0;
printf("hello %d\n", j);
return 0;
}

When I compiled it using `gcc test.c -o test -std=c89 -Wall`,
it did not complained about this problem.
I think I should reread the manual.

BTW: my system is debian linux box, with gcc 3.3.3
> cat main.c
#include <stdio.h>

int main(int argc, char* argv[]) {
int i = 1;
printf("%d\n", i);
int j = 0;
printf("hello %d\n", j);
return 0;
}
> gcc -Wall -std=c89 -pedantic -o main main.c
main.c: In function `main':
main.c:6: warning: ISO C90 forbids mixed declarations and code
> gcc --version
gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
 
R

Richard Bos

Hongzheng Wang said:
#include <stdio.h>

int main()
{
int i = 1;
printf("%d\n", i);
int j = 0;
printf("hello %d\n", j);
return 0;
}

When I compiled it using `gcc test.c -o test -std=c89 -Wall`, it did not
complained about this problem.

Yes, IIRC Ganuck has this as an extension. It's not ISO C89, though.

Richard
 
M

Michael Mair

Hiho,
Yes, IIRC Ganuck has this as an extension. It's not ISO C89, though.

The option -std=c89 (better: -std=iso9899:1990) causes all ISO C89
programs (excluding the first amendment) to be compileable --
nothing more.
The option -std=c89 is the same as -ansi; the gcc manpage says:

The -ansi option does not cause non-ISO programs to be rejected
gratuitously. For that, -pedantic is required in addition to
-ansi.

gcc up to now runs with -std=gnu89 as default; as soon as ISO C99
is implemented completely, the default will be -std=gnu99, that is
C99 plus gnu extensions.


HTH
Michael
 
I

Irrwahn Grausewitz

Yes, IIRC Ganuck has this as an extension. It's not ISO C89, though.

Indeed: if gcc is invoked in conforming C89 mode [1] it emits the
required diagnostic.

[1] -W -Wall -O -std=c89 -pedantic

Regards
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top