Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question on ANSI C code

1 view
Skip to first unread message

Ioannis Vranos

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
I have just started to learn ANSI C using the book "The C Programming
Language" of K&R Second Edition and i have written a small program using a
function to convert temperatures from Fahrenait to Celsius. The question is
if ANSI C standard allows
to define and use a function before main(). I wrote the following code
according to my book's instructions:

#include <stdio.h>
float metatropi (int fahr, float cel);

main()
{float cel;
int fahr;
for (fahr=0; fahr<300; fahr=fahr+20)
{ printf("%d %6.1f\n", fahr, metatropi(fahr, cel));}
return 0;
}

float metatropi (int fahr, float cel)
{
cel=(5.0/9.0) * (fahr-32.0);
return cel;
}


I defined the function metatropi for conversion of fahrenait to celsius.

It compiles ok with only a warning that there is a conversion from float to int but it is ok, it works.

The question is if the following 2 codes work according to ANSI C standard:

Code 1:

#include <stdio.h>
float metatropi (int fahr, float cel);

float metatropi (int fahr, float cel)
{
cel=(5.0/9.0) * (fahr-32.0);
return cel;
}

main()
{float cel;
int fahr;
for (fahr=0; fahr<300; fahr=fahr+20)
{ printf("%d %6.1f\n", fahr, metatropi(fahr, cel));}
return 0;
}


Code 2:

#include <stdio.h>

float metatropi (int fahr, float cel)
{
cel=(5.0/9.0) * (fahr-32.0);
return cel;
}

main()
{float cel;
int fahr;
for (fahr=0; fahr<300; fahr=fahr+20)
{ printf("%d %6.1f\n", fahr, metatropi(fahr, cel));}
return 0;
}

All 3 compile ok in my C-C++ compiler but i do not know if it an accepted C style.
Any help would be appreciated!

Best regards,
Ioannis

--

* Ioannis Vranos
* Undergraduate at the Electrical Eng. Dep. of T.E.I. Pireaus
* Personal homepage: http://members.xoom.com/jvranos
* Mirror: http://www.geocities.com/capitolhill/lobby/1389


Ben Pfaff

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
"Ioannis Vranos" <jvr...@ibm.net> writes:

[...three different permutations of orderings of main(), a function,
and prototypes...]

All 3 compile ok in my C-C++ compiler but i do not know if it an accepted C style.
Any help would be appreciated!

Personally I prefer to see the main() function at the very beginning
of a program, if the program is just one file. If the program is more
than one file then I put the main function in a file by itself,
possibly also with command line parsing and miscellaneous startup
functions.

It's completely a style issue how you order your functions.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
do not ask me C questions via email; post them instead

Ioannis Vranos

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
So both 3 codes are ANSI C compliant?

--

* Ioannis Vranos
* Undergraduate at the Electrical Eng. Dep. of T.E.I. Pireaus
* Personal homepage: http://members.xoom.com/jvranos
* Mirror: http://www.geocities.com/capitolhill/lobby/1389


Ben Pfaff <pfaf...@pilot.msu.edu> wrote in message
news:8767auv...@pfaffben.user.msu.edu...

Ben Pfaff

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
"Ioannis Vranos" <jvr...@ibm.net> writes:

So both 3 codes are ANSI C compliant?

Yes, AFAICT, all 3 are ANSI C compliant.

Matthew A. Givens

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
The accepted method, and it is only a convention, is to place the prototype
declaration above the main, then the main, and then all of the other
functions you need.

Matthew in Montgomery


Ioannis Vranos wrote in message <36893...@news3.ibm.net>...

>All 3 compile ok in my C-C++ compiler but i do not know if it an accepted C style.
> Any help would be appreciated!
>

>Best regards,
>Ioannis

F. Paul Menard

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
Just my two cents....


If you are following the K & R book. I would suggest you also follow the K &
R STYLE. By style I mean the format of your code (i.e. [roper indentations
etc).

#include <stdio.h>
float metatropi (int, float ); <--- Don't need to define the actual
variables you are going to use in the function only the TYPE of the
variables.

main()
{
float cel;
int fahr;
for (fahr=0; fahr<300;

<---- Follow K & R on the placement of the braces. And indent
for readability


printf("%d %6.1f\n", fahr, metatropi(fahr, cel));
}
return 0;
}

float metatropi (int fahr, float cel)
{
cel=(5.0/9.0) * (fahr-32.0);
return cel;
}

Back in the days before C was standardized and before C accepted prototypes. You had to place you local function bodies before the main(). This cause people who write huge monolithic program source routine to have to place the main() at the bottom of a couple thousand lines of code. These are just my comments. The most important thing is the code compiles. Most people don't care about STYLE or layout. But if you ever have a job were you have to follow and make changes to someone else's code. You will appreciate when they take the extra time to provide some formatting.

FPM

>* Undergraduate at the Electrical Eng. Dep. of T.E.I. Pireaus

Erik Anderson

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
On Wed, 30 Dec 1998, F. Paul Menard wrote:
FPM> Back in the days before C was standardized and before C accepted
FPM> prototypes. You had to place you local function bodies before the main().
FPM> This cause people who write huge monolithic program source routine to have
FPM> to place the main() at the bottom of a couple thousand lines of code.
FPM> These are just my comments.

Unfortunately, your comments are incorrect. K&R allowed the programmer to
declare functions with the name and return type - also known as a "function
declarator". This in turn allowed the function body to be defined anywhere else
the coder's little heart desired.

If you think about it, how else did header files work before ANSI came along :)
Just my "Susan B. Anthony's worth" (tm) - Cheers!

Lawrence Kirby

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
In article <1Pfi2.391$bv4....@news.flash.net>
f...@flash.net "F. Paul Menard" writes:

...

>Back in the days before C was standardized and before C accepted prototypes.


> You had to place you local function bodies before the main().

No, before ANSI prototypes came along the language had (and still has)
"old style" declarations and definitions right from the early days. Maybe
you were thinking of Pascal. Here are some examples

int foo(); /* Old style function declaration */

int foo(bar, baz) /* Old style function definition */
long bar;
double baz;
{
...
}

int foo(long, double); /* ANSI prototype function declaration */

int foo(long bar, double baz) /* ANSI prototype function definition */
{
...
}


> This cause
> people who write huge monolithic program source routine to have to place the


> main() at the bottom of a couple thousand lines of code.

No, you could simply use an old style declaration.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


0 new messages