Minggu, 25 Oktober 2015

Data Types and Reading Input From User

C-Data Types, Reading input from user, Writing an output

Data Types
  • Data Types are used to define a variable before its use. but the value of the variable can be change anytime. the definition of variable will assign storage for the variable and define the type that will be held in the location. C-Data Types are :
  1. Char - it used to define a character or strings.
    the strorage size = 8 bytes
    the value range = -128 to 127 (signed char) or 0 to 255 (unsigned char)
  2. Short - target type will be optimized for space and will have width at least 16 bits.
    short storage size = 16 bytes
    the value range = -32768 to 32767 (signed) or 0-65535 (unsigned)
  3. Int - it used to define an integer. for most variables and countable things.
    int storage size = 16-32 bytes
    the value range = -32768 to 32767 or -2147483648 to 2147483647 (signed) or 0 to 65535 or 0 to 4294967295 (unsigned)
  4. Long - 32-64 bytes
  5. Long Long - 64 bytes
  6. float - it can be used for real number such as : 3,14. used to define floating point numbers.
    float storage size = 32 bytes
  7. double - is used to define BIG floating point numbers. it reserves twice the storage for the number.
    double storage size = 64 bytes
Reading Input From User, Writing Output
  • now i will give example how to read input from user and also writing an output for each data types. here are the examples :
  1. Char

    #include <stdio.h>
    int main ()
    {
    int letter;
    letter = 'A';
    printf("%c",letter);
    }

    you can convert the letter into a number by change the %c into %d.
    if you want to convert from number into a letter, you can change letter = 'A'; into letter = any number and let the %c in the printf. example :

    #include <stdio.h>
    int main ()
    {
    int letter;
    letter = 65;
    printf("%c",letter);
    }
  2. Int/short
    #include <stdio.h>
    int main ()
    {
    int i,j;
    int result;
    scanf("%d %d",&i,&j); // this is how to read input from user
    result = i+j+10; // this is how to write an output
    printf("the result :%d \n",result);
    }

    compare the result when you using short : //remember the possible value range data types
    #include <stdio.h>
    int main ()
    {
    int i,j;
    int result;
    scanf("%d %d",&i,&j); // this is how to read input from user
    result = i+j+10; // this is how to write an output
    printf("the result :%d \n",result);
    }
  3. Float/Double
    #include <stdio.h>
    int main ()
    {
    int a,b,c;
    float result;
    scanf("%d %d %d",&a,&b,&c); // this is how to read input from user
    result = 7.00/3.14 + (a+b+c); // this is how to write an output
    printf("result =%f",result);
    }

    compare the result when you using double.
    #include <stdio.h>
    int main ()
    {
    int a,b,c;
    double result;
    scanf("%d %d %d",&a,&b,&c); // this is how to read input from user
    result = 7.00/3.14 + (a+b+c);  // this is how to write an output
    printf("result =%f",result);
    }

    if you just want to make only 2 numbers after coma, you just need to change %f into %.2f
    if 3 numbers after coma, %.3f, and so on..

Tidak ada komentar:

Posting Komentar