INPUT-Code:*#*include<stdio.h> int main() { short int a,b,c; scanf("%d%d",&a,&b); c=a+b; printf("%d",c); return 0; }
1 1
OUTPUT
1
please explain clearly..
INPUT-Code:*#*include<stdio.h> int main() { short int a,b,c; scanf("%d%d",&a,&b); c=a+b; printf("%d",c); return 0; }
1 1
OUTPUT
1
please explain clearly..
Run that, and see how many variables you actually stored.Code:*#*include<stdio.h> int main() { short int a,b,c, stored=0; stored = scanf("%d%d",&a,&b); c=a+b; printf("%d",c); printf("I have stored %d variables\n", stored); return 0; }
it is giving 2 variables stored..
You can't lie to scanf about the types of variables. "%d" means "int", and if you don't pass pointer-to-int, well, that's too bad. If you are passing pointer-to-short-int, you need to use the "short int" specifier, which is "%hd".
$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:5: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘short int *’
foo.c:5: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘short int *’
Try compiling with a decent compiler, then you might realise that you're trashing memory where ints were expected, but only shorts were present.
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.
can u give the representation and explain with an example how output is printing?
Why do we need an example? You've got an example in front of you. You are telling scanf to write your input into four bytes of memory, but you are only handing it two bytes to write into. Where scanf is writing the other two bytes, no one knows.
Look up the scanf() function in your C library documentation.Code:*#*include<stdio.h> <--- lose the asterisks. It's... #include <stdio.h> int main() { short int a,b,c; scanf("%d%d",&a,&b); <--- for short<space>short this should be ... scanf("%hd %hd",&a,&b); c=a+b; printf("%d",c); return 0; }
C is a real dummy... you gotta tell it everything in minute detail. Tell it the wrong thing and it will do the wrong thing.
Last edited by CommonTater; 07-15-2011 at 07:32 AM.
In all likelihood the a and b variables were adjacent on the stack.
His scanf() command was only converting the first integer due to a bad formatting string.
It was laying that first integer across both a and b setting one variable to 1 and the other to 0
So when he added them up he got 1 instead of 2
if u dont mind explain with the addresses and how it is adding .....
i am thinking like this.... a and b will stored in different addresses.what i mean is 1byte of integer value in one place and other byte will go to trash memory..like that a also..then c=2
Are you working on a compiler with 2 byte integers? If so it is absolutely time to upgrade! 16 bit code won't even run on modern 64bit operating systems... Take a look at Pelles C you're going to find it is way way way better than anything old enough to still be using 16 bit ints.
There is no such thing as "trash memory"... what happens is that 2 bytes are written... one where you want it, the other right over top of something else...
The pointers are passed just fine, but you are telling scanf to write four bytes of data there when you only own two bytes. The other two bytes will overwrite whatever's next to it. So writing four bytes to b will completely destroy a with the "top" two bytes (most likely, since the compiler is probably not going to put a and b in wildly different places in memory when they are your only variables).
> if u dont mind explain with the addresses and how it is adding .....
There is nothing to explain, you're in the realms of undefined behaviour.
We can "guess" what might be happening for your particular compiler, but it isn't useful knowledge you can take away with you and make "clever" use of it in future.
The real answer is to learn how to use scanf properly, rather than seek explanation for the chaos.
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.