Minggu, Juni 01, 2014

Define Value of Integer With Short Int Type on C Programming Language

Will be exemplified how to define a value of integer variabel with short int type on C programming language.
  1. Open Dev C++
  2. File menu > New > Source File, then will be created a new worksheet titled Untitled1
  3. File menu > Save As. On Save as type: choose C source files (*.c) and on File name, example we give name define integer value typed short int.c
  4. First, type minimal required syntax on C programming:

    main()
    {
    }


  5. Add getch() function in main() function to show program result while be Run:

    main()
    {
      getch();
    }


  6. Define name of a variable be a short int variable by using syntax

    int name_of_short_int;

    so that program syntax becomes:

    main()
    {
      int name_of_short_int;
      getch();
    }


  7. Define a value of short int variable, example we define value of the short int variable is 7

    main()
    {
      int name_of_short_int;
      name_of_short_int = 7;
      getch(); }


  8. To show integer value use printf code: %d or %u

    main()
    {
      int name_of_short_int;
      name_of_short_int = 7;
      printf("%d", name_of_short_int);
      getch();
    }


    atau

    main()
    {
      int name_of_short_int;
      name_of_short_int = 7;
      printf("%u", name_of_short_int);
      getch();
    }



%d isprintf code to show integer typed signed, while %u is print code to show integer typed unsigned.

Integer variable of short int can be classified into 2 types,
  1. Short int typed Signed
    It means is integer has sign, the sign are 2, i.e. positive sign (+) and negative sign (-), example for signed: +4789 for positive sign and -6987 for negative sign. Range of value of integer on variable typed short int is begin from -32768 s/d 32767 (minus thirty two thousand and seven hundred and sixty eight up to thirty two thousand and seven hundred and sixty seven).

    For example we define on program syntax lower limit and upper limit of short in variable typed signed:

    main()
    {
      signed short int var_short_int_signed_minimum;
      signed short int var_short_int_signed_maximum;
      var_short_int_signed_minimum = -32768;
      var_short_int_signed_maximum = 32767;
      printf("%d\n", var_short_int_signed_minimum);
      printf("%d", var_short_int_signed_maximum);
      getch();
    }


    If integer value is defined out of value range of short int variable, then its result will false when program is run, please try!, example as follows:

    main()
    {
      signed short int name_of_short_int_signed1;
      signed short int name_of_short_int_signed2;
      name_of_short_int_signed1 = -32769;
      name_of_short_int_signed2 = 32768;
      printf("%d\n", name_of_short_int_signed1);
      printf("%d", name_of_short_int_signed2);
      getch();
    }


  2. Short int typed unsigned
    It means is integer has not sign, it means can only show positive integer, as usual if integer has not sign, we interpret it as positive integer. Value range of integer on short int variable typed unsigned is 0 s/d 32767 (zero up to thirty two thousand and seven hundred and sixty seven(

    Example we define on program syntax lower limit and upper limit of short int variable typed unsigned as follows:

    main()
    {
      unsigned short int var_short_int_unsigned_minimum;
      unsigned short int var_short_int_unsigned_maximum;
      var_short_int_unsigned_minimum = 0;
      var_short_int_unsigned_maximum = 32767;
      printf("%u\n", var_short_int_unsigned_minimum);
      printf("%u", var_short_int_unsigned_maximum);
      getch();
    }


    IF integer value is defined out of value range of short int variable, then its result will false when program is run, please try, example as follows:

    main()
    {
      unsigned short int name_of_short_int_unsigned1;
      unsigned short int name_of_short_int_unsigned2;
      name_of_short_int_unsigned1 = -1;
      name_of_short_int_unsigned2 = 32768;
      printf("%u\n", name_of_short_int_unsigned1);
      printf("%u", name_of_short_int_unsigned2);
      getch();
    }

Thank you... :D

0 komentar: