Thread: C - float and double data-type question..

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool C - float and double data-type question..

    hi again every1, can any1 please help me with this one? thanks.. ^.^

    ..i made a c program that should output the square root of a number....

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    
    int main()
    {
    	double x=100;
    
    	clrscr();
    
    	printf("Square root of 100 is equals to: %lf", sqrt(x));
    
    	getchar();
    
    	return 0;
    }
    ...but every time it does, in this example, it shows 10.000000 (..which is the square root of 100..)

    what i want is to make the program output 10 (..with no decimals..) instead of 10.000000...




    ...another program i made that uses the pow() function:


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    
    int main()
    {
    	double x=10;
    
    	clrscr();
    
    	printf("10 raised to 2 is equals to: %lf", pow(x, 2));
    
    	getchar();
    
    	return 0;
    }
    ...also has the same problem, it shows 100.000000 instead of just 100





    ....about the float and double data-type, is it correct that the %lf parameter in the printf() function is for double, and %f for float? im just confused... thanks again for your help and advise... ^.^

    ...and uhmmm btw, about the title for this post i made, if you found it inappropriate, please, accept my apologies...

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The manual explains all, http://www.cplusplus.com/reference/c...io/printf.html

    Look a few tables down at "precision". That way it saves you asking about every possible combination of printf().
    Last edited by zacs7; 07-13-2008 at 05:51 AM.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    The only thing u have to change is to declare x as an int instead of double(and also in printf &#37;d instead of %lf).double & float gives u the output in real form not in integer form.
    also %lf is used for double and %f for float.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    &#37;f is used whether or not it's a double or float for printf.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    26
    Quote Originally Posted by ShadeS_07 View Post
    what i want is to make the program output 10 (..with no decimals..) instead of 10.000000...
    Have you tried:

    printf("Square root of 100 is equals to: %2.0f", sqrt(x));

    ?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And don't forget about the &#37;g format specifier, which also trims trailing zeroes automatically. (It also will change from "normal" format to "exponential" format automatically too.)

  7. #7
    Registered User graydot's Avatar
    Join Date
    Jul 2008
    Posts
    6
    printf("Square root of 100 is equals to: &#37;d", sqrt(x));
    gives output as 0, however
    printf("Square root of 100 is equals to: %d", (int)sqrt(x));
    gives the correct output of 10.
    I wonder why the first doesn't work.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because sqrt(double) returns double, not int. In other words, you are lying to printf about what you're trying to print, so you get undefined result.
    The second explicitly tells the compiler you want the result to be truncated to an integer and an integer is what printf expects, since you told it so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User graydot's Avatar
    Join Date
    Jul 2008
    Posts
    6
    ah. that explains it. thanks a lot.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool ThankYou! ^.^

    thankyou so much for all your help and advise, i really appreciate it... ^.^


    ..uhmm, one more question please... what does %2.0f do? what does it mean? thnkz... ^.^

    Quote Originally Posted by Mole42 View Post
    Have you tried:

    printf("Square root of 100 is equals to: %2.0f", sqrt(x));

    ?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ShadeS_07 View Post
    thankyou so much for all your help and advise, i really appreciate it... ^.^


    ..uhmm, one more question please... what does %2.0f do? what does it mean? thnkz... ^.^
    Did you read the link on printf formatting supplied above? It means that you want 2 digits and 0 decimals. You could for example use "%8.3f", and you would use 8 positions for the whole number [or more if it's a bigger number than can fit in that space], and out of those 3 would be decimal places.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  5. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM