Data types in C with examples


Advertisements

Share



Learn int, char, float, double basic data types, how to declare the basic data types, how to use the data types in your c program for different projects and applications.

What are data types in C?

A variable occupies some space in memory. The data type that is assigned to a variable determines how many bytes of space it will occupy in memory, the range of values that can be stored within that memory space and the set of operations that can be applied to the variable.

For example int salary statement defines a variable salary. The data type int defines that it will take(2bytes or 4 bytes) of space in memory. Storing data in a variable is called intializing the variable and data stored in a variable can be printed using its name.

Data types in C can be categorized as basic data types and derived data types. Lets dicuss all the basic data types in this article.

char, int, float, double data types

  • char data type
  • A char data type can be declared by writing char prior to the char variable name. Examples of char variables are all numeric characters A to Z or a to z. char type is 8 bit signed or unsigned number. If nothing is mentioned before the variable name then it is counted as signed char variable.

    If a character variable is to be declared as unsigned you need to write unsigned before char during declaration of the variable. Unsigned char can hold values from 0 to 255 and signed char can hold values from -128 to 127. To print a character %c notation is used to indicate just where in a line the character is to be printed. %c is called a format specifier.

  • int data type
  • An integer is a number with no fractional part. In C, an integer is never written with a decimal point. Examples are 2, –23, and 2456. Numbers such as 3.14, 0.22, and 2.000 are not integers. Integers are stored as binary numbers. The integer 7, for example, is written 111 in binary. Therefore, to store this number in an 8-bit byte, just set the first(MSB) 5 bits to 0 and the last(LSB) 3 bits to 1.

    The int type is a signed or unsigned integer. If nothing is mentioned before the variable name then it is counted as signed int variable. If an integer variable is to be declared as unsigned you need to write unsigned before int during declaration of the variable. When an integer is declared as unsigned its value during initialization has to be appended by letter U.

    An integer can be positive, negative, or zero. The range in possible values depends on the computer system. For some computers size of integer is 16bit(2 bytes) where as for some computers size of integer is 32bit(4 bytes). To print an integer %d notation is used to indicate just where in a line the integer is to be printed. %d is called a format specifier.

    The type short int or, more briefly, short may use less storage than int , thus saving space when only small numbers are needed. Like int, short is a signed type. So a short integer can be declared like short int val or short val. A short integer value can be printed using percentage hd or hi. The type long int, or long, may use more storage than int, thus enabling you to express larger integer values. Like int, long is a signed type. %li is used to print signed long integers and %lu for unsigned long integers.

    The type long long int, or long long, may use more storage than long int, thus enabling you to express larger integer values. Like int, long long is a signed type. %lli is used to print signed long long integers and %llu for unsigned long long integers.

  • Float, Double Data Type
  • Financial and mathematically oriented programs often make use of floating-point numbers. In C, such numbers are called type float, double, or long double. Data with decimal places are written by using floating point. Some floating-point numbers are 9.57, 3.16E7, 7.00, and 2e–8. Notice that adding a decimal point makes a value a floating-point value. So 7 is an integer type but 7.00 is a floating-point type. The notation 3.16E7 means to multiply 3.16 by 10 to the 7th power, that is, by 1 followed by 7 zeros. The 7 would be termed the exponent of 10. A variable of float data type is printed using %f format specifier.

    Double has 64 bit precision for floating point number (1 bit for the sign, 11 bits for the exponent and 52 bits for the value) i.e. double has 15 decimal digits of precision. Float has 32 bit precision for floating point number ( 1-bit for the sign, 8 bits for exponent, 23 bits for the value) i.e. float has 6 decimal digits of precision.

    Data types, size, specifiers

    Data Type Memory (bits) Range Format Specifier
    short int 16 -32,768 to 32,767 %hd
    unsigned short int 16 0 to 65,535 %hu
    unsigned int 32 0 to 4,294,967,295 %u
    int 32 -2,147,483,648 to 2,147,483,647 %d
    long int 64 -2,147,483,648 to 2,147,483,647 %ld
    unsigned long int 64 0 to 4,294,967,295 %lu
    long long int 64 -(2^63) to (2^63)-1 %lld
    unsigned long long int 64 0 to 18,446,744,073,709,551,615 %llu
    signed char 8 -128 to 127 %c
    unsigned char 8 0 to 255 %c
    float 32 %f
    double 64 %lf
    long double 64 %Lf

    Program using data types

    Program to demonstrate use of different data types in C
    
    #include <stdio.h>
    #include <limits.h>
    #include <float.h>
    
    #define PI 3.14
    
    void Pvariable(char ,int , unsigned int ,short ,unsigned short ,long ,unsigned long , long long , float ,double , long double );
    void print_sizeof_variable(void);
    void limitof_variable(void);
    
    int main()
    {
      printf("============ Declaring and initializing variables =============\n");
      char type = 'A'; /* declaring and initializing a character variable*/
      int numb; /* declaring an integer variable which is by default signed */
      int number = 90; /* declaring and initializing an integer variable in one statement*/
      unsigned int un = 3000000000U; /* system with 32-bit int which is unsigned meaning only 0 to certain limit it can be */
      short end = 200; /* This is decaling a 16-bit short and this is same as short int end = 200; */
      unsigned short int shrt = 56U;
      long int big = 65537L; /* declaring a long variable. This is same as long big = 65537L*/
      unsigned long int ubig = 65537UL; /* declaring a unsigned long variable.*/
      long long verybig = 123LL; /* declaring a long long variable. Writing only 123 will treat like an integer*/
      float salary = 3.45f; /* declaring a float variable*/
      double value = 123E30; /* declaring a double variable */
      long double vverybig = 123.78345L; /* declaring a long double variable */
      printf("========== Calling different functions for illustration  ====================\n");
      Pvariable(type,number,un,end,shrt,big,ubig,verybig,salary,value,vverybig);
      print_sizeof_variable();
      limitof_variable();
      return 0;
    }
    
    void Pvariable(char ch,int num, unsigned int g,short t,unsigned short unum,long op,unsigned long h, long long rt, float ld,double five, long double st)
    {
      printf("============ printing all types of variables =============\n");
      printf("Printing character variable type which is %c\n",ch);
      printf("Printing integer number number which is %d\n",num);
      printf("Printing unsigned integer number un which is %u\n",g);
      printf("Printing short integer number end which is %hd\n",t);
      printf("Printing unsigned short integer number shrt which is %hu\n",unum);
      printf("Printing long integer number big which is %li\n",op);
      printf("Printing unsigned long integer number ubig which is %lu\n",h);
      printf("Printing long long integer number vbig which is %lli\n",rt);
      printf("Printing float value salary which is %f\n",ld);
      printf("Printing double value value which is %lf\n",five);
      printf("Printing long double vverybig which is %Lf\n",st);
    }
    void print_sizeof_variable(void)
    {
      printf("====== Number of bytes occupied by a given type ============\n");
      printf("Type char has a size of %d bytes.\n", sizeof(char));
      printf("Type short has a size of %d bytes.\n", sizeof(short));
      printf("Type int has a size of %d bytes.\n", sizeof(int));
      printf("Type long has a size of %d bytes.\n", sizeof(long));
      printf("Type long long has a size of %d bytes.\n",sizeof(long long));
      printf("Type double has a size of %d bytes.\n",sizeof(double));
      printf("Type long double has a size of %d bytes.\n",sizeof(long double));
      printf("Type float has a size of %d bytes.\n",sizeof(float));
    }
    void limitof_variable(void)
    {
      printf("==========Some number limits for this system:============\n");
      printf("char stores values between min %d and max %d\n",CHAR_MIN, CHAR_MAX);
      printf("unsigned char store values from 0 to %u\n", UCHAR_MAX);
      printf("short store values from %d to %d\n", SHRT_MIN, SHRT_MAX);
      printf("unsigned short store values from 0 to %u\n", USHRT_MAX);
      printf("Biggest int: %d and smallest %d\n", INT_MAX, INT_MIN);
      printf("unsigned int store values from 0 to %u\n", UINT_MAX);
      printf("long store values from %d to %d\n", LONG_MIN, LONG_MAX);
      printf("unsigned long store values from 0 to %u\n", ULONG_MAX);
      printf("smallest non-zero value of type float is: %.3e and largest is: %.3e\n", FLT_MIN,FLT_MAX);
      printf("Smallest double is: %.3e Largest double is: %.3e\n", DBL_MIN, DBL_MAX);
      printf("Smallest long long: %lld\n", LLONG_MIN);
      printf("One byte = %d bits on this system.\n", CHAR_BIT);
      printf("Smallest normal float: %e\n", FLT_MIN);
      printf("float precision = %d digits\n", FLT_DIG);
      printf("float epsilon = %e\n", FLT_EPSILON);
    }
    

    Output:

    $ gcc prog.c
    $ ./a.out
    ============ Declaring and initializing variables =============
    ========== Calling different functions for illustration  ====================
    ============ printing all types of variables =============
    Printing character variable type which is A
    Printing integer number number which is 90
    Printing unsigned integer number un which is 3000000000
    Printing short integer number end which is 200
    Printing unsigned short integer number shrt which is 56
    Printing long integer number big which is 65537
    Printing unsigned long integer number ubig which is 65537
    Printing long long integer number vbig which is 123
    Printing float value salary which is 3.450000
    Printing double value value which is 123000000000000004979083645550592.000000
    Printing long double vverybig which is 123.783450
    ====== Number of bytes occupied by a given type ============
    Type char has a size of 1 bytes.
    Type short has a size of 2 bytes.
    Type int has a size of 4 bytes.
    Type long has a size of 4 bytes.
    Type long long has a size of 8 bytes.
    Type double has a size of 8 bytes.
    Type long double has a size of 8 bytes.
    Type float has a size of 4 bytes.
    ==========Some number limits for this system:============
    char stores values between min 0 and max 255
    unsigned char store values from 0 to 255
    short store values from -32768 to 32767
    unsigned short store values from 0 to 65535
    Biggest int: 2147483647 and smallest -2147483648
    unsigned int store values from 0 to 4294967295
    long store values from -2147483648 to 2147483647
    unsigned long store values from 0 to 4294967295
    smallest non-zero value of type float is: 1.175e-38 and largest is: 3.403e+38
    Smallest double is: 2.225e-308 Largest double is: 1.798e+308
    Smallest long long: -9223372036854775808
    One byte = 8 bits on this system.
    Smallest normal float: 1.175494e-38
    float precision = 6 digits
    float epsilon = 1.192093e-07
    $
    

    Exercises, Solutions

    Medium
    Medium
    High
    High
    Medium
    Medium
    Low
    Medium
    High
    Low

    Want to contribute a new article? Write and upload your article information .
    Share

     Articles

     C Programming

     Linux Software

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs

    Glass Elite Apple iPhone 11 Pro (Case Friendly)

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software