Thread: Compile error? Logic error? Syntax Error? Please help

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    2

    Question Compile error? Logic error? Syntax Error? Please help

    Can anyone please explain to me why this program won't correctly print the proper totals on screen? See my code and the included screenshot.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int total, first, second;
    
        printf("Enter 2 numbers: \n\n");
        scanf_s("%d%d", &first, &second);
        total = first + second;
        printf("%d+%d=%d\n\n", first, second, total);
        total = first * second;
        printf("%d*%d=%d\n\n", &first, &second, &total);
        total = first - second;
        printf("%d-%d=%d\n\n", &first, &second, &total);
        total = first / second;
        printf("%d/%d=%d\n\n", &first, &second, &total);
        total = first % second;
        printf("%d%%d=%d\n\n", &first, &second, &total);
    }
    Compile error? Logic error? Syntax Error? Please help-c-forum-question-1-jpgCompile error? Logic error? Syntax Error? Please help-c-forum-question-2-jpg

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There are ampersands in all of the printf() statements that are producing the wrong results.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Its because the result is too big for type 'int'. An 'int' is mostly 4 byte and can normaly have a value from –2147483648 to +2147483647.
    Edit your code to use type 'long long'. This is normaly 8 byte and can hold the value from –9223372036854755808 to +9223372036854755807.
    The type 'long long' has the format '%lld' in printf-function and familiar.

    Hints:
    The result of the division will look wrong, because a division with two integers give a integer as result. You loose the fraction.
    The formatstring from your last printf-function is not correct. Make '%d%%d' to '%d\%%d'.

    Edit:
    Your main-function returns nothing. Insert 'return 0;' as last line.
    I think the function 'scanf_s' is not standard (from Microsoft?). Use 'scanf' instead.
    Last edited by WoodSTokk; 10-10-2014 at 09:20 PM.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    2
    Quote Originally Posted by grumpy View Post
    There are ampersands in all of the printf() statements that are producing the wrong results.
    Thanks grumpy. You nailed it!

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Note that your compiler should have warned you about these issues. Make sure you always check compiler warnings, and that you have maximum warnings enabled.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. error C2143: syntax error : missing ')' before ';'
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 11:06 AM
  5. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM