Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Viariables definitions must be first in function definition?

Reply
Thread Tools

Viariables definitions must be first in function definition?

 
 
Hongzheng Wang
Guest
Posts: n/a
 
      06-09-2004
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
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-09-2004
Hongzheng Wang wrote:

> 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;
}

> gcc -Wall -std=c99 -pedantic -c func.c

 
Reply With Quote
 
 
 
 
Liang Chen
Guest
Posts: n/a
 
      06-09-2004

"Hongzheng Wang" <> wrote in message
news:ca5v1p$12ig$...
> 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.


 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-09-2004
Hongzheng Wang <> wrote:

> 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
 
Reply With Quote
 
Hongzheng Wang
Guest
Posts: n/a
 
      06-09-2004
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
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-09-2004
Hongzheng Wang wrote:

> 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)
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-09-2004
Hongzheng Wang <> wrote:

> #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
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      06-09-2004
Hiho,

>>#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.


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

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      06-09-2004
(Richard Bos) wrote:
>Hongzheng Wang <> wrote:
>
>> #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.


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

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

Regards
--
Irrwahn Grausewitz ()
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function definitions borophyll@gmail.com C Programming 8 10-08-2007 02:45 AM
Find function definition in a C file for a function with its name and starting line no. as input jeniffer Perl Misc 7 03-26-2006 10:53 AM
Conditional function definitions Olov Johansson Javascript 8 01-21-2006 09:43 AM
member function definition inside and outside class..function already defined error.. ypjofficial@indiatimes.com C++ 1 01-09-2006 08:49 AM
Fun with function definitions Anders Höckersten Ruby 3 10-21-2005 10:22 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57