Thread: question on short...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    10

    question on short...

    Code:
    *#*include<stdio.h>
    int main()
    {
    short int a,b,c;
    scanf("%d%d",&a,&b);
    c=a+b;
    printf("%d",c);
    return 0;
    }
    INPUT-
    1 1

    OUTPUT
    1
    please explain clearly..

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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;
    }
    Run that, and see how many variables you actually stored.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    it is giving 2 variables stored..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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".

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    $ 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.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    can u give the representation and explain with an example how output is printing?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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;
    }
    Look up the scanf() function in your C library documentation.

    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.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    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.
    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

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    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

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by raju.b41 View Post
    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...

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by raju.b41 View Post
    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
    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).

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A short question!
    By C_programmer.C in forum C Programming
    Replies: 11
    Last Post: 08-28-2010, 03:17 PM
  2. Short question concerning 'if'
    By Rahiiyja in forum C++ Programming
    Replies: 4
    Last Post: 07-08-2007, 01:31 PM
  3. a short question
    By peterx in forum C Programming
    Replies: 8
    Last Post: 10-12-2005, 12:45 AM
  4. short question on c++
    By war-totem in forum C++ Programming
    Replies: 5
    Last Post: 06-02-2005, 06:37 AM
  5. 'short' question
    By sand_man in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 05:04 PM

Tags for this Thread