Thread: Trouble with sqrt ()

  1. #16
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    Msvc 6

  2. #17
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>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

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem
    (*) 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.
    You recall correctly.

    Some (particularly older) compilers also refuse to compile prototypes within functions.

  5. #20
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    Thanks some awesome information...

    Thanks to all of you who replied here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to sqrt
    By shamma in forum C Programming
    Replies: 1
    Last Post: 05-09-2009, 01:42 PM
  2. Having trouble with the sqrt() function.
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2005, 01:08 PM
  3. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM
  4. sqrt
    By modec in forum C Programming
    Replies: 3
    Last Post: 04-16-2003, 07:19 AM
  5. how sqrt ?
    By sambs1978 in forum C Programming
    Replies: 3
    Last Post: 09-20-2001, 08:14 AM