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 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.
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
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
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
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)
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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;
}
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |