Thread: short int vs int

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    8

    short int vs int

    Hi,

    The following program works fine if an int is declared instead of the short int for tracks ,but if I do declare /*tracks*\ as a short int then whilst giving the output
    it does not print out the name of the artist.But then again if I do declare tracks as a short int before the strings the output is fine and the program prints out the name of the artist as well.All I know is you will have to declare the variable you are planning to use at the start of the program ,Is it necessary that short int has to be declared before the strings.Here is the code I am trying to run

    Code:
    /* cd inventory*/
    #include<stdio.h>
    int main()
    {
        
        char title[61];
        char artist[61];
        short int tracks;/* if short int is declared at the top the program runs fine else the 
                           program runs fine if tracks are declared as int rather than short int here*/
        float price;
        int album;
        char type;
        
        printf("Welcome to the cd database\n");
        printf("====================================================\n");
        /* scanning the title of the track*/
        printf("Please enter the title:");
        scanf("%[^\n]",title);
        fflush(stdin);
        
        /* scanning the artist name */
        printf("\nPlease enter the name of the artist:");
        scanf("%[^\n]",artist);
        fflush(stdin);
       
        
        /* scanning the number of tracks*/
        printf("\nPlease enter the number of tracks:");
        scanf("%d",&tracks);
        fflush(stdin);
        
        /* checking if single or complete album*/
        printf("\nPlease enter 'a' if album 's' if single:");
        scanf("%c",&type);
        fflush(stdin);
        album=type=='a';/* if anything other than a is entered it assumes as s*/
        
        /* scanning the price of the album or single*/
        printf("\nPlease enter the price:");
        scanf("%f",&price);
        fflush(stdin);
        
        /*Printing the output from the database*/
        printf("The cd details you entered are\n");
        printf("======================================================\n");
        printf("Title:%s\n",title);
        printf("Arist:%s\n",artist);
        printf("Number of tracks:%d\n",tracks);
        
        if (album)
        printf("Album\n");
        else
        printf("Single\n");
        
        printf("Price :%.2f\n",price);
        printf("=======================================================\n");
        printf("Press the enter key to exit");
        getchar();
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. do not flush stdin - see FAQ
    2. use correct format for scanf when you use short. &#37;d is for int
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    Thanks for the reply.

    I did find the correct way to scan a short int ,but that still leaves me with a doubt as to why the program is not throwing the error when I declare the short int on before the strings ,the program seems to run fine even if I use the wrong scanf format for short int.

    As far as fflush goes ,I am just following what has been instructed to me which I guess is wrong I will make sure I recitfy the same.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    some compilers with compile warnings set to max will warn you about wrong scanf/printf formats used.

    But in general prototype for scanf is
    scanf(const char* , ...);
    so the regular compiler will check the first argumant type and accept anything you put after that... It does not parse the format string to check the type of the arguments passed...

    Scanf itself cannot check the argument types - it supposes that arguments provided are compatible with the format string supplied
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM