Sum of 2 numbers

This is a discussion on Sum of 2 numbers within the C Programming forums, part of the General Programming Boards category; Hi everyone. I am trying to do a simple program in C which reads two double numbers from the keyboard ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    114

    Sum of 2 numbers

    Hi everyone. I am trying to do a simple program in C which reads two double numbers from the keyboard and displays the sum to the user. Even though the compiling is ok, the program doesn't run as expected. The code is:

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
        double num1, num2, sum;
    
        printf("Type in the first number: ");
        scanf("%f", &num1);
        printf("Type in the second number: ");
        scanf("%f", &num2);
    
        sum = num1 + num2;
    
        printf("%f + %f = %f.", num1, num2, sum);
    
        return 0;
    
    }
    What is wrong with this code?

    Thanks in advance!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    You need a compiler which warns you when you get printf/scanf wrong.

    Eg.
    Code:
    $ gcc bar.c
    bar.c: In function ‘main’:
    bar.c:9: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    bar.c:11: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    $ gcc bar.c
    $ ./a.out 
    Type in the first number: 123
    Type in the second number: 456
    123.000000 + 456.000000 = 579.000000.$
    Change the scanf formats to "%lf"
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You might also want to include math.h although it's not strictly necessary in this case.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    Thank you very much!

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    Salem, so every time I wanna print a double variable, I need to use %lf instead of %f?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    so every time I wanna print a double variable, I need to use %lf instead of %f?
    The %lf is only required for scanf() for a double, which extracts the value from the stream, not print it. Unless you are using C99 or compiler specific extensions printf() uses %f for printing both float and double.

    Jim

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    Quote Originally Posted by jimblumberg View Post
    The %lf is only required for scanf() for a double, which extracts the value from the stream, not print it. Unless you are using C99 or compiler specific extensions printf() uses %f for printing both float and double.

    Jim
    I see. I tried using the %f for printing, and it worked, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-08-2010, 10:26 AM
  2. Can anyone help me to break numbers into an array numbers
    By s2xcracker in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2009, 05:17 PM
  3. Replies: 5
    Last Post: 12-21-2007, 12:38 PM
  4. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 06:56 AM
  5. Replies: 4
    Last Post: 03-03-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21