#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
[...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
* 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...
So both 3 codes are ANSI C compliant?
Yes, AFAICT, all 3 are ANSI C compliant.
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
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
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!
...
>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
-----------------------------------------