-1
#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.

6 Answers6

2

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)
  • Wait a minute, are you saying that you gave input as `6881188`? if yes than sorry to say this, but `int` type alone cannot not handle such value. –  Jun 06 '18 at 09:08
  • @Arvind any platform supporting 32bit `int` (nearly *everything* at this point, sans some micro-controllers), will easily take that value. – WhozCraig Jun 06 '18 at 09:11
  • i have added 1 and 1 – Akshay Mittal Jun 06 '18 at 09:11
  • @WhozCraig: OP says CodeBlocks, so I guess I and this guy assumed 16-bit `int` instead of 32-bits. – WedaPashi Jun 06 '18 at 09:13
  • @AkshayMittal Are you sure that you have compiled code after making `&c` -> `c` change? – H.S. Jun 06 '18 at 09:14
  • @WedaPashi CodeBlocks is an ide, not a compiler toolchain. Unless he's running this on DOS from a quarter-century ago, that isn't the problem (and if it was, by some magic the `printf` of an 16-bit signed int is mysteriously delivering a number larger than 32767, max signed int for 16 bit. – WhozCraig Jun 06 '18 at 09:15
  • @WhozCraig: I'd still wager at this point that the sum of hardware running 16 bit `int` is still greater than the sum of hardware running 32 bit `int`, if you'd be so kind as to excuse my clumsy language. – Bathsheba Jun 06 '18 at 09:15
  • i am a beginner.i dont know about that – Akshay Mittal Jun 06 '18 at 09:17
  • @Bathsheba Outstanding. I look forward to seeing CodeBlocks running on that sometime when I've got spare time. Had he said Turbo C I would lean a lot harder that direction. – WhozCraig Jun 06 '18 at 09:17
1

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.

WedaPashi
  • 3,002
  • 21
  • 38
  • Unless you provide us with what inputs you tested this with, no one can guarantee the behaviour. The OP says that you are using CodeBlocks IDE, I take that it could be a 16-bit `int` conforming, whereas `6881188` is a value which can not be accommodated in 16-bits. – WedaPashi Jun 06 '18 at 09:11
1
  1. Always check the return value of scanf. Yours should be 2 if the standard input was able to be parsed into 2 ints. Note that an int could have a range as small as -32767 to +32767. Consider using a long instead?

  2. 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.

  3. 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.

Bathsheba
  • 220,365
  • 33
  • 331
  • 451
0
  1. 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.

  2. 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.

Chris Walsh
  • 3,166
  • 1
  • 34
  • 54
0

& 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.

Angus Comber
  • 8,150
  • 10
  • 49
  • 92
-1

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.

Fateh Mohamed
  • 16,066
  • 4
  • 32
  • 45
rjzou
  • 1
  • 2
  • > int 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); getchar(); getchar(); return 0; } – rjzou Jun 06 '18 at 09:37
  • you must input two int between space. – rjzou Jun 06 '18 at 09:59