Msvc 6
Msvc 6
>>Also, I was taught that its okay to declare the function prototype inside main.
Everything I've ever read declared prototypes outside main, I can't find any
evidence that your way is wrong (well maybe in the standards), but you're as
well to conform to the more standard way, which is outside main. It makes your
code clearer IMO, as inside main it looks like a function call.
>>I understand now that the format specifier for double is %e
that'll print the number in exponential format, probably not what you need.
The reason you came across %lf is because that's the format specifier for scanf
to read in a double, for printf, it's %f for both float and double
Also, about what zx-1 and I were talking about, sorry if it's a little over you
head, here's the important parts to take home:
1. C is an internationally standardised language - the standard outlines a lot
of programming practices - it tells you what will definitely work, and what might
not (on a standard conforming compiler).
2. The C standard has been through a number of different versions - the lastest
released version was agreed in 99, and there was also a standard released in
89, hence the names C89 and C99
3. C89 in widely used, and C99 is not as widely used.
4. C89 lets you get away with some things that are not necessarily a good
this (e.g. omitting main's return type, function prototyping), that C99 has
become stricter on.
Hope that clears it up
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
> Also, I was taught that its okay to declare the function prototype inside main.
It's legal, it just depends on what you're trying to convey.
A prototype inside a function isn't(*) visible to any other function (unless you prototype it there as well).
On the plus side, it's easy to see what each function depends on.
On the minus side, it's a lot more work to maintain all those duplicate prototypes in the code.
> I had read somewhere that %lf is the format specifier for double!
As Richie T explained, there are differences between the printf() and scanf() formats.
printf() is easier, because all variadic parameters undergo default promotion (all floats get passed to printf as doubles), so "%f" works equally well for floats and doubles.
scanf() on the other hand has just a bunch of pointers to storage locations, so the conversions have to be a lot more precise to make the data fit into the pointed to storage locations.
(*) IIRC, this is one thing compilers often get wrong. Some export the prototype to the whole file, and others more correctly honor the current scope.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You recall correctly.Originally Posted by Salem
Some (particularly older) compilers also refuse to compile prototypes within functions.
Thanks some awesome information...
Thanks to all of you who replied here.