0

Just started coding and got stuck trying to do math. The code should perform an addition after user input, but I only keep getting random results like -952492524 when running the code on terminal. How is the correct way to fix this?

This is the code:

#include <stdio.h>

main()
{
    int iquantity, iprice;
    int iresult = iquantity + iprice;

    scanf("%d", &iquantity);
    scanf("%d", &iprice);

    printf("%d", &iresult);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • 3
    ... and `printf("%d", &iresult);` should be `printf("%d", iresult);` – Weather Vane Apr 17 '17 at 20:14
  • 3
    ..and it should be `int main(void)`.. And `return 0;` in the end won't harm. – Eugene Sh. Apr 17 '17 at 20:15
  • 2
    C follows a sequential language paradigm. So the statements you write will be executed on after the other. Now, how can you perform the addition before the vaues of `iquantity` and `iprice` are even input from the user? – Ajay Brahmakshatriya Apr 17 '17 at 20:16
  • Possible duplicate of [printf pointer argument type warning?](http://stackoverflow.com/questions/16224917/printf-pointer-argument-type-warning) – autistic Apr 18 '17 at 04:33
  • Why would your compiler want to *warn* you about something if it's not important or a common mistake? – autistic Apr 18 '17 at 04:34

2 Answers2

3

Your call to printf was printing the address of the variable and not its value. scanf needs the address because it will change the value of the variables; this is passing the variable by pointer. printf only needs to read the value, and so the parameters are passed by value and NOT by pointer.

This is an important concept to learn when coding in C; unlike modern languages, C does not hide variable references: one must embrace the pointer and know when to use it and when not to.

This is an excellent link to learn more on the topic. What's the difference between passing by reference vs. passing by value?

Try it like this:

#include <stdio.h>

int main(void)
{

    int iquantity, iprice;

    scanf("%d", &iquantity);

    scanf("%d", &iprice);

    int iresult = iquantity + iprice; /* after scanf, not before */

    printf("%d", iresult); /* and don't need a reference here */
}
Community
  • 1
  • 1
Display name
  • 1,228
  • 1
  • 18
  • 29
0

Since you set the value of iresult without first giving iquantity and iprice a value they are by default set to a indeterminate value from memory. You will want to set iresult after your scanf method calls.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • 1
    Not a random value. Arbitrary perhaps. – Tony Tuttle Apr 17 '17 at 20:16
  • @huck_cussler care to explain the difference? – Ajay Brahmakshatriya Apr 17 '17 at 20:18
  • 1
    @huck_cussler I'm implying it's a indeterminate value, that the value can be whatever was in memory previously. I don't mean a literal random value; I've edited to clarify. – Spencer Wieczorek Apr 17 '17 at 20:25
  • Random would be if the computer actually chose a memory address at random, as in it somehow shuffled the possible addresses up and picked one non-deterministically. Arbitrary may appear random but actually may have deterministic underpinnings. The computer is not picking an address at random, it is extremely well-defined how the computer will behave in picking the address. However, it is arbitrary in that it is not pre-defined and it almost certainly will be different from run to run. – Tony Tuttle Apr 17 '17 at 20:25