Thread: Very simple Function using printf()

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    Very simple Function using printf()

    Hello again,

    This code uses a simple function that takes two arguments and just prints them. This is easy when using two int (%d) arguments. However, I want to take two double arguments (i.e. integers with decimal points) but once I start messing with the format specifiers the output goes mental. What am I doing wrong?
    Code:
    #include <stdio.h> 
     
     void printfunc(int, int);
     
     int main(void)
     {
         int dub1, dub2;
         
         printf("Enter two decimal integers.\n");
         scanf("%d %d", &dub1, &dub2);
         printfunc(dub1, dub2);
         printf("Cheerio for now.\n");
         
         getchar();
         getchar();
         
         return 0;
     }
     
     void printfunc(int a, int b)
     {
          printf("%d %d\n", a, b);
          }
    Any guidance most gratefully received.
    To clarify, I need to alter the function to be able to take two double arguments (e.g 1.2 and 56.3) and simply print them, i.e. void printfunc(double, double)

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    23
    I may or may not be right but you have

    scanf("%d %d", &dub1, &dub2);

    remove the & symbols and have it read

    Code:
    scanf("%d %d", dub1, dub2);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    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. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    I don't know what you are doing but I wrote the following code and it works fine.
    Code:
    #include <stdio.h> 
     
     int main(void)
     {
         double dub1, dub2;
         
         printf("Enter two decimal integers.\n");
         scanf("%lf %lf", &dub1, &dub2);
         printf("\n\n%.2lf\n%.2lf\n\n",dub1, dub2);
         printf("Cheerio for now.\n");
     
         return 0;
     }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And how much did twazzler learn about variable types from being handed a fully coded answer?

  6. #6
    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

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    I've just figured it out - I hadn't to changed my ints (dub1 and dub2) to doubles - doh

    Thanks for all your help - I'll be back!

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Hello twazzler,

    I compiled your latest listing and got this, see if it helps:
    C:\DOCUME~1\Owner>gcc -c test.c -Wall
    test.c: In function 'main':
    test.c:10:6: warning: format '%lf' expects type 'double *', but argument 2 has t
    ype 'int *'
    test.c:10:6: warning: format '%lf' expects type 'double *', but argument 3 has t
    ype 'int *'
    You should always compile with warnings turned on a high level. They catch a lot of stuff.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    That would have been a big help and saved a lot of time, yes - does Dev-C have such a setting?

    Yes, I know, I should RTFM ;-)

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well Dev-C++ uses a port of gcc, so look for something that enables -Wall. RTFM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    twazzler... you might also consider moving forward in time to a compiler from this millenium... DevC has been abandonware since the late 1990s.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Why not using Eclipse?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mahdiem View Post
    Why not using Eclipse?
    Maybe because it's not C and it appears to be Linux/MAC specific.

    There are lots of free compilers to choose from... Google is your friend.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Nah, Eclipse is a Java based IDE that runs on any platform that can run Java apps. It works with a huge number of languages, depending on what modules/plugins/compilers/interpreters you have and how you set it up. AFAIK, you can get the C compiler of your choice to use with Eclipse.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Nah, Eclipse is a Java based IDE that runs on any platform that can run Java apps. It works with a huge number of languages, depending on what modules/plugins/compilers/interpreters you have and how you set it up. AFAIK, you can get the C compiler of your choice to use with Eclipse.
    Yeah... but then there's setups like Pelles C that are an IDE and compiler all in one. I like Pelles C for three primary reasons: 1) it is C-99 standards compliant, 2) it comes with everything you need for windows development built right in and 3) It is probably the best documented compiler I've ever seen. (i.e. the help file is to die for!)

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