#include <stdio.h>
main()
{
int a,b,c;
printf("write two numbers");
scanf("%d%d", &a,&b);
c=a+b;
printf("the sum of two numbers is%d",&c);
}
this my used code. I am using code blocks for compiling.
#include <stdio.h>
main()
{
int a,b,c;
printf("write two numbers");
scanf("%d%d", &a,&b);
c=a+b;
printf("the sum of two numbers is%d",&c);
}
this my used code. I am using code blocks for compiling.
While using printf()
you are printing the location instead of value of c
variable. Remove &
to get the actual value of c
.
printf("the sum of two numbers is%d",c)
Look at your printf
. You are using the address-of operator (&
), This operator gives the address of its operand.
In order to print the value of c
, you need to change
printf("the sum of two numbers is%d",&c);
to
printf("The sum of two numbers is %d", c);
// ^
Update: Based on your inputs, I guess you are testing this with invalid inputs. Make sure that your inputs: a
, b
and its addition c
does not exceed valid int
value, else you are doomed.
As a side note to improve, you might want change your main()
function into
int main(void) //or int main()
This is as per what standard recommends, and is a heavily discussed topic amongst language-lawyers. Read this for more information.
Always check the return value of scanf
. Yours should be 2 if the standard input was able to be parsed into 2 int
s. Note that an int
could have a range as small as -32767 to +32767. Consider using a long
instead?
Use c
in printf
to output the int
value. Don't pass the address. Otherwise that's undefined behaviour. Some folk might suggest to you that the address of c
is being output, but the language standard says nothing of the sort, so neither should anyone else therefore.
Change the function to int main()
. That's the language standard; a return 0;
is added implicitly in that particular function if you don't supply it.
A final complexity: the behaviour of a + b
is undefined if that exceeds the minimum or maximum size of an int
. Ideally you should guard against this. But, surprisingly, this is an advanced topic! See Add integers safely, and prove the safety.
printf("the sum of two numbers is%d",&c);
As you can see that you are printing the address of the c by doing
'&c' instead of printing the actual value.
And if we see the printf
function are using the %d
format specifier to print the address of 'c' . It will give you the signed integer value. Use the %p
to print the pointer addresses.
& is the address of operator and you use for functions requiring a pointer.
The signature for scanf is:
int scanf ( const char * format, ... );
Where ... means additional arguments and pointers are required. Why pointers for scanf? Because the arguments are out args, not in args. Basically scanf needs to know the type and size of the variable. Otherwise if for example the input data is a long long but you say its an int then there will not be sufficient space reserved for the integer result.
However, the signature for printf is:
int printf ( const char * format, ... );
where ... means additional arguments containing a 'value'. For printf you pass a value of some type, it is an out argument.
So you pass a pointer as additional argument to scanf but a value to printf.
So change your code like this:
printf("the sum of two numbers is%d",c);
or
printf("the sum of two numbers is%d",a + b);
The value you saw printed was the address of the c variable. Which could be any positive integral value - and is dependent on where your computer decides to store the variable.
you see that scanf line
scanf("%d%d", &a, &b);
change to
scanf("%d %d", &a, &b);
between %d insert space char .the progam can't distinguish difference two input int.