Thread: Scanning and Printing short integers

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    5

    Scanning and Printing short integers

    Hi,

    I need some very basic help with the use of short integers.

    I created the following simple program, and it's already giving me errors, probably the second printf is the culprit.

    Code:
    #include "stdafx.h"
    #include "conio.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        unsigned short int num;
        printf("Please enter a number!\n");
        scanf("%si", &num);
    
        printf("%si", num);
    
        _getch();
        return 0;
    }
    So my question is, what's the proper way of scanning and printing short integers? (I guess it doesn't matter if it's signed or unsigned)

    Also, in what interval can a short int store numbers?

    Thanks very much!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    man page scanf section 3
    A brief scan of the manual page would have told you the qualifier was 'h' and not 's'.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    I'm sorry, didn't know about that page, I'm very new to programming.

    I tried it with 'h', but it's still giving me errors. Just to clarify, after declaring a short int variable, do I put '%hi' whenever I want to scan or print it, or only for one of them?

    Thanks!

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    #include "stdafx.h"
    #include "conio.h"
    These aren't standard headers, and they aren't particularly useful for what you're trying to accomplish. You need to include the stdio.h header file in order to use printf and scanf.

    int _tmain(int argc, _TCHAR* argv[])
    I don't know why you define main like this. For most programs, you really only need to use one of the following:
    Code:
    int main(void) { /* ... */ }
    int main(int argc, char *argv[]) { /* ... */ }
    Which one you use will depend on whether you plan on using the arguments passed by the host environment.

    So my question is, what's the proper way of scanning and printing short integers? (I guess it doesn't matter if it's signed or unsigned)
    Code:
    short int a;
    
    if (scanf("%hd", &a) == 1)
        printf("%d\n", a);
    For short unsigned int, I suggest using the "%hu" format specifier. Note that the 'h' specifier isn't necessary for printf due to default argument promotions.

    Also, in what interval can a short int store numbers?
    It varies based on your C implementation. signed short has a minimum guaranteed range of -32767 to +32767 (inclusive), and unsigned short has a minimum guaranteed range of 0 to 65535. You can check their actual ranges by including the header file limits.h and checking the macros USHRT_MAX, SHRT_MAX, and SHRT_MIN. I strongly suggest assuming that each data type has their minimum guaranteed range (as specified by the standard) for the sake of writing portable code.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The use of StdAfx.h and a main with that signature simply suggest that Visual Studio is being used, but probably an old version given the conio.h.

    If the code is giving you errors, then you need to copy and paste them here so that we can see them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    Yes, it's Visual Studio 2008.

    Now it's working great, thanks very much for all the help, here's the final code:

    Code:
    #include "stdafx.h"
    #include "conio.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        unsigned short int num;
        printf("Please enter a number!\n");
        scanf("%hi", &num);
    
        printf("%hi", num);
    
        _getch();
        return 0;
    }
    I don't know why my teacher told me that there's no problem with using %si.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Code:
        scanf("%si", &num);
        printf("%si", &num);
    Would appear to 'work' for rather perverse reasons, because %s is actually for strings, not 'short' integers. If you only typed in a single letter, you'd probably be OK, but anything else would be a buffer overflow.

    printf/scanf don't care that you got the type of pointer wrong (pointer to int vs pointer to char). All they see is "pointer" and off they go, assuming you did the right thing by passing a char pointer.
    The GCC compiler will actually check printf/scanf calls, so this useful if you have a choice to use it.

    A short int is typically 2 bytes, so there isn't room for a long string, and trying it would result in a buffer overflow (and the resulting weird behaviour ranging from "it works" to "where are my install disks" and "where's that smoke coming from!?").

    > I don't know why my teacher told me that there's no problem with using %si.
    Teachers and books can be wrong (and many are). Some however throw in the occasional mistake just to see who's paying attention.
    Never rely too heavily on a single source without doing some cross-checking.
    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. scanning file and printing an array
    By Jeff Clay in forum C Programming
    Replies: 3
    Last Post: 11-26-2012, 01:12 AM
  2. scanning strings and integers from a file
    By GaelanM in forum C Programming
    Replies: 15
    Last Post: 03-31-2011, 09:38 PM
  3. scanning and printing a string
    By Prestige in forum C Programming
    Replies: 5
    Last Post: 12-16-2010, 04:35 AM
  4. Help scanning in numbers and printing format
    By ArcadeEdge in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 05:13 PM
  5. Scanning integers from a file:
    By xarmenx in forum C Programming
    Replies: 6
    Last Post: 11-29-2004, 04:57 PM