473,395 Members | 1,783 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,395 software developers and data experts.

Viariables definitions must be first in function definition?

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
Nov 14 '05 #1
8 1137

"Hongzheng Wang" <wa******@mails.tsinghua.edu.cn> wrote in message
news:ca***********@mail.cn99.com...
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.
Nov 14 '05 #2
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

Nov 14 '05 #3
Hongzheng Wang <wa******@mails.tsinghua.edu.cn> 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
Nov 14 '05 #4
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
Nov 14 '05 #5
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)
Nov 14 '05 #6
Hongzheng Wang <wa******@mails.tsinghua.edu.cn> 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
Nov 14 '05 #7
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

Nov 14 '05 #8
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Hongzheng Wang <wa******@mails.tsinghua.edu.cn> 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 (ir*******@freenet.de)
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
Nov 14 '05 #9

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

Similar topics

14
by: Joerg Schuster | last post by:
Hello, according to http://mail.python.org/pipermail/tutor/2001-July/007246.html the order of function definitions does matter in python. Does anyone know a trick to avoid this? Is there a...
7
by: Carlos Ribeiro | last post by:
I'm looking for ways to load new class definitions at runtime (I'm not talking about object instances here, so a persistent object database isn't what I am looking for ). I'm aware of a few...
2
by: Chris Gordon-Smith | last post by:
I am currently in India and have treated myself to the Indian reprint of O'Reilly's "C++ In A Nutshell". (Books in India come in at 1/3 to 1/2 of the price in Britain.) I thought that I would...
16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
5
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single...
10
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as...
7
by: G Fernandes | last post by:
Is the following an old-style(K&R) function definition or a new style(C89)? int (length, factor, multiple) { /* ... code ... */ return length; }
2
by: xllx.relient.xllx | last post by:
I have a few quetions about definitions vs declarations concerning defined types (user defined), specifically classes. From what I understand, a class contained in a header file is a definition as...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.