Data Types in C
• Each variable in C has an associated data type.
• Each data type requires different amounts of memory and has some specific
  operations which can be performed over it.
• It specifies the type of data that the variable can store like integer, character, floating,
  double, etc.
• The data type is a collection of data with values having fixed values, meaning as well
  as its characteristics.
                              Data Types in C
                       The data types in C can be classified as follows:
Types                                        Description
                                             Arithmetic types can be further classified into
Primitive Data Types
                                             integer and floating data types.
                                             The data type has no value or operator and it
Void Types                                   does not provide a result to its caller. But void
                                             comes under Primitive data types.
                                             It is mainly used to assign names to integral
User Defined DataTypes                       constants, which make a program easy to read
                                             and maintain
                                             The data types that are derived from the
Derived types                                primitive or built-in datatypes are referred to as
                                             Derived Data Types.
Data Types in C
                                  Data Types in C
• Different data types also have different ranges up to which they can store numbers.
• These ranges may vary from compiler to compiler.
• Below is a list of ranges along with the memory requirement and format specifiers on
  the 32-bit GCC compiler.
             Data Type            Memory (bytes)   Range                 Format Specifier
             short int            2                -32,768 to 32,767     %hd
             unsigned short int   2                0 to 65,535           %hu
             unsigned int         4                0 to 4,294,967,295    %u
                                                   -2,147,483,648 to
             int                  4                                      %d
                                                   2,147,483,647
                                                   -2,147,483,648 to
             long int             4                                      %ld
                                                   2,147,483,647
             unsigned long int    4                0 to 4,294,967,295    %lu
             long long int        8                -(2^63) to (2^63)-1   %lld
                                                   0 to
                            Integer Types
 • The integer data type in C is used to store the whole numbers without decimal values.
 • Octal values, hexadecimal values, and decimal values can be stored in int data type in
   C.
 • We can determine the size of the int data type by using the sizeof operator in C.
 • Unsigned int data type in C is used to store the data values from zero to positive
   numbers but it can’t store negative values like signed int.
 • Unsigned int is larger in size than signed int and it uses “%u” as a format specifier in
   C programming language.
•Range: -2,147,483,648 to 2,147,483,647
•Size: 2 bytes or 4 bytes
•Format Specifier: %d
 Note: The size of an integer data type is compiler-dependent, when processors are
 16-bit systems, then it shows the output of int as 2 bytes. And when processors are
 32-bit then it shows 2 bytes as well as 4 bytes.
                                     Integer Types
#include <stdio.h>                                                      Output
int main()                                                              Integer value with positive data: 9
{                                                                        Integer value with negative data: -9
    int a = 9;                 // Integer value with positive data.     Integer value with an unsigned int data: 89
                                                                        Integer value with an long int data: 99998
    int b = -9;               // integer value with negative data.
    int c = 89U;       // U or u is Used for Unsigned int in C.
    long int d = 99998L; // L or l is used for long int in C.
    printf("Integer   value   with   positive data: %d\n", a);
    printf("Integer   value   with   negative data: %d\n", b);
    printf("Integer   value   with   an unsigned int data: %u\n", c);
    printf("Integer   value   with   an long int data: %ld", d);
    return 0;
}
                        Character Types
 • Character data type allows its variable to store only a single character.
 • The storage size of the character is 1.
 • It is the most basic data type in C.
 • It stores a single character and requires a single byte of memory in almost all
   compilers.
•Range: (-128 to 127) or (0 to 255)
•Size: 1 byte
•Format Specifier: %c
                                 Character Types
#include <stdio.h>
int main()                                                Output
{                                                         Value of a1: a
    char a1 = 'a';                                        Value of a1 after increment is: b
    char b1;                                              Value of b1 now is : c
    printf("Value of a1: %c\n", a1);
     a1++;
    printf("Value of a1 after increment is: %c\n", a1);
    //   a1 is assigned ASCII values
    //   which corresponds to the
    //   character 'c'
    //   a-->97 b-->98 c-->99
    //   here c will be printed
    b1   = 99;
    printf("Value of b1 now is: %c", b1);
    return 0;
}
                 Floating-Point Types
In C programming float data type is used to store floating-point values.
Float in C is used to store decimal and exponential values.
It is used to store decimal numbers (numbers with floating point values) with single
precision.
•Range: 1.2E-38 to 3.4E+38
•Size: 4 bytes
•Format Specifier: %f
                        Floating-Point Types
#include <stdio.h>
int main()                     Output
{                              9.000000
                               2.500000
      float a = 9.0f;
      float b = 2.5f;          0.000200
        // 2x10^-4
      float c = 2E-4f;
        printf("%f\n",a);
        printf("%f\n",b);
        printf("%f",c);
    return 0;
}
                           Double Types
 • A Double data type in C is used to store decimal numbers (numbers with floating point
   values) with double precision.
 • It is used to define numeric values which hold numbers with decimal values in C.
 • Double data type is basically a precision sort of data type that is capable of holding 64
   bits of decimal numbers or floating points.
 • Since double has more precision as compared to that float then it is much more
   obvious that it occupies twice the memory as occupied by the floating-point type.
 • It can easily accommodate about 16 to 17 digits after or before a decimal point.
•Range: 1.7E-308 to 1.7E+308
•Size: 8 bytes
•Format Specifier: %lf
                                Double Types
#include <stdio.h>
                                    Output
int main()                          123123123.000000
{                                   12.293123
                                    2312312312.123123
    double a = 123123123.00;
    double b = 12.293123;
    double c = 2312312312.123123;
    printf("%lf\n", a);
    printf("%lf\n", b);
    printf("%lf", c);
    return 0;
}
                         Void Data types
• The void data type in C is used to specify that no value is present.
• It does not provide a result value to its caller.
• It has no values and no operations.
• It is used to represent nothing. Void is used in multiple ways as function return type,
  function arguments as void, and pointers to void.
Syntax:
void exit(int check); // function return type void
int print(void);      // Function without any parameter can accept void.
 void *malloc( size_t size); // memory allocation function which returns a pointer to
                                        //void.
#include <stdio.h>
Void main()
{
       printf(“Hi,I will not return anything”);
  }
OUTPUT:
Hi, I will not return anything
• We can use the sizeof() operator to check the size of a variable.
• See the following C program for the usage of the various data types:
#include <stdio.h>
int main()                                                         Output
{                                                                  The size   of   int data type: 4
      int size_of_int=sizeof(int);                                 The size   of   char data type:1
     int size_of_char= sizeof(char);                               The size   of   float data type:4
      int size_of_float=sizeof(float);                             The size   of   double data type:8
    int size_of_double=sizeof(double);
      printf("The   size   of   int data type : %d\n",size_of_int );
      printf("The   size   of   char data type : %d\n",size_of_char);
      printf("The   size   of   float data type : %d\n",size_of_float);
      printf("The   size   of   double data type : %d",size_of_double);
    return 0;
}