"Finding a sum of two numbers"-program

closed account (E85L1hU5)
Hello,

I've been trying to write a simple program using that minimum of knowledge that I have in C. I tried to write it intuitively, and I guess got it wrong. can anyone please check the code?

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

main()
{
int a, b, s;
s=a+b;
puts("The sum of:");
scanf("%d", &a);
puts("and");
scanf("%d", &b);
printf("is %d", &s);
}


After trying to compile the message is:
sum.c: In function ‘main’:
sum.c:11: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’

Thank you in advance!
It should be printf("is %d", s); (without &).

Also, I would calculate the sum after getting a and b from the user.
closed account (E85L1hU5)
I'll try doing so. Thank you!

PS Why without &?

PPS It worked perfectly! thanks again
Last edited on
when used like this the & operator gets the memory address of a variable, you want the value of the variable itself, which is s
Just note that this doesn't print any legit value, since you sum them up before they're given a value. The right spot to place s=a+b; shouldn't be too hard to find given the above hint.
closed account (E85L1hU5)
yup. got it! thanks.
Topic archived. No new replies allowed.