Thread: Double Data Type

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    1

    Angry Double Data Type

    I am new to the C programming language, so, bare with me.

    Everytime I run this code I get a whole number and not a decimal number.

    Am I missing something?



    Code:
    #include <stdio.h>
    
    main()
    {
    
    	double  pi;
    
    
            printf("Enter the value of PI:");
    	scanf("%d", &pi);
    
    	printf("Pi is %d\n", pi);
    	fflush(stdin);
    	getchar();
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    becase you dont print it in a right format. use this

    Code:
    #include <stdio.h>
    
    int main()
    {
        double pi;
        int ch;
        
        printf("Enter the value of PI:");
        scanf("&#37;lf", &pi);
    
        printf("Pi is %lf\n", pi);
        
        while((ch=getchar()) != '\n' && ch != EOF);
        
        getchar();    
        return 0;
    }
    And dont use fflush(stdin) seeFAQ

    ssharish2005

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I wouldn't recommend using %lf to print out a double; it's valid in C99 but is not defined in C89. %f works for printing doubles in both C89 and C99 and thus will be more portable.

    %lf is still correct for doubles and scanf(), however.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM