Thread: short int vs int

  1. #1
    Registered User
    Join Date
    Jul 2007

    short int vs int


    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

    /* cd inventory*/
    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");
        /* scanning the title of the track*/
        printf("Please enter the title:");
        /* scanning the artist name */
        printf("\nPlease enter the name of the artist:");
        /* scanning the number of tracks*/
        printf("\nPlease enter the number of tracks:");
        /* checking if single or complete album*/
        printf("\nPlease enter 'a' if album 's' if single:");
        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:");
        /*Printing the output from the database*/
        printf("The cd details you entered are\n");
        printf("Number of tracks:%d\n",tracks);
        if (album)
        printf("Price :%.2f\n",price);
        printf("Press the enter key to exit");

  2. #2
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    1. do not flush stdin - see FAQ
    2. use correct format for scanf when you use short. &#37;d is for int
    To be or not to be == true

  3. #3
    Registered User
    Join Date
    Jul 2007
    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
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    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
    To be or not to be == true

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, 07: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-01-2003, 11:26 PM
Website Security Test