short int vs int

This is a discussion on short int vs int within the C Programming forums, part of the General Programming Boards category; Hi, The following program works fine if an int is declared instead of the short int for tracks ,but if ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    1. do not flush stdin - see FAQ
    2. use correct format for scanf when you use short. &#37;d is for int
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21