Thread: Very simple Function using printf()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by Salem View Post
    > remove the & symbols and have it read
    You need the & in these cases.

    > but once I start messing with the format specifiers the output goes mental. What am I doing wrong?
    Well how many things did you change from 'int' to 'double'?
    And how many formats did you change from %d

    Note that to scanf a double, you need "%lf", but to print it you only need "%f"

    Post an example which doesn't work, if you're still stuck.
    Here's my code with the ints changed to doubles and %d changed to %lf (or just %f)

    Code:
    #include <stdio.h> 
     
     void printfunc(double, double);
     
     int main(void)
     {
         int dub1, dub2;
         
         printf("Enter two decimal integers.\n");
         scanf("%lf %lf", &dub1, &dub2);
         printfunc(dub1, dub2);
         printf("Cheerio for now.\n");
         
         getchar();
         getchar();
         
         return 0;
     }
     
     void printfunc(double a, double b)
     {
          printf("%f %f\n", a, b);
          }
    Adding two float values of 1.2 and 2.2 gives a printed output of 1073846681.000000 and -1717986918.000000
    Last edited by twazzler; 04-14-2011 at 12:53 PM. Reason: typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very simple printf
    By GokhanK in forum C Programming
    Replies: 3
    Last Post: 02-18-2011, 08:09 AM
  2. simple printf wont compile...
    By i_can_do_this in forum C Programming
    Replies: 9
    Last Post: 07-08-2006, 07:18 AM
  3. Simple Scanf and printf
    By xamlit in forum C Programming
    Replies: 9
    Last Post: 08-31-2005, 05:20 PM
  4. Replies: 5
    Last Post: 10-08-2004, 05:30 AM
  5. printf like function
    By argon in forum C Programming
    Replies: 2
    Last Post: 10-07-2003, 03:12 PM