#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Heelpp !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


    i'm a beginner so need some help ..

    I made a program for addition of two intergers ...

    but instead of the real value like 2+2=4
    i get 2+2=2268633 something ...

    i made a few more basic ones and im facing the same problem ,,,
    is something wrong with my compiler ? or is it my mistake

    btw im writing the code here

    #include <stdio.h>
    #include <conio.h>


    int main(void)
    {
    int a;
    int b;
    int c;
    printf("Enter two numbers:");

    scanf("%d", &a);
    scanf("%d", &b);

    c = a + b;

    printf("The addition is %d" , &c);

    getch();

    }

    like for 2+2 i get 2293564
    instead of 4
    please help people !
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    > printf("The addition is %d" , &c);
    Try just
    Code:
    printf("The addition is %d" , c);
    Also, please use [code][/code] tags when posting code (see the sticky threads), and please drop the habit of excessive punctuation. It won't make anyone answer any quicker (the opposite is more likely).
    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. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    &c is the address of the variable c, whereas c would be the variable and hence the value it contains.

    In the call to printf, you want to print the value of c, not its address. Besides, if you did want to print the address, then you should have used "%p".

    When you use scanf to input values into a and b, you do still need to use &a and &b, the addresses of a and b, because scanf needs to know where to put the value. scanf needs the address, printf needs the value.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    @dwise1_aol and @salem ..
    Thank you guys ,,, It worked !
    haha ! now i guess i can continue making more programs ... this error was holding me back

    Have a nice day people

IMN logo majestic logo threadwatch logo seochat tools logo