Tuesday, 13 November 2018

LEARN DATA TYPES IN C WITH VIDEO

Data types in C
What is data types in C?


What is Data types?


Data types are the representation of any data means which types of data we want to store in memory. Each and Every variable occupies how many bytes in memory is defined by data types.

Syntax:  data type   identifier;

For example 

int a;





Data Types in C
Categorization of Data Type






LEARN DATA TYPE WITH VIDEO




Categorization of Data types in C


1. Primitive Data types 

It is also known as Fundamental data types. It is predefined data types and directly used by the user.
Primitive data types are:


Data Types in C
Primitive Data types 

1. Integer data types

  • int: 4 bytes of memory is needed for the 32-bit compiler.
       1 bytes = 8 bits
       4 bytes = 4*8 bits
                   = 32 bits

Signed int means positive/negative integer values.
Unsigned int means only positive integer values.

Total no. of bits = 32 bits

Range of Unsigned int: range from 0 to 2^31 i.e. 0 to 4,294,967295.

Range of signed int: range from -4294967296/2 to + 4294967295/2 i.e. -2147483648 to 2147483647.

Format Specifier

for int: %d
for unsigned: %u

  • short int:  2 bytes of memory is required for the 32-bit compiler.

       1 byte =8 bits
       2 byte = 16 bits

    • long int:  4 bytes of memory is required for the 32-bit compiler.

            1 byte =8 bits
            4 bytes = 32 bits

      2. Floating data types

      It stores decimal point values.

      a) float data types

      It needs 4-bytes of memory to store data.

      range: 1.2E - 38 to 3.4E + 38

      format specifier: %f

      b) double data types

      It needs 8-bytes of memory to store data.

      range: 2.3E - 308 to 1.7E + 308

      format specifier: %lf

      c) long double data types

      It needs 12-bytes of memory to store data.

      range: 3.4E-4932 to 1.1E+4932

      format specifier: %Lf

      Program: Find out the range of Floating data types.

      #include <stdio.h>
      #include <float.h>
      int main( )
      {
          printf ("To calculate the range of Float\n\n");

          printf ("Minimum float unsigned value: %E \n ",FLT_MIN);
          printf ("Maximum float unsigned value:%E \n", FLT_MAX);
      }


      3. Character data types

      char:

      It needs one-byte of memory to store a character.

      range: 0 to 255 (unsigned char)

      Total no. of bits: 255+1 (0) =256 bits

      signed char: -256/2 to 255/2  i.e. -128 to 127

      format specifier: %c

      4. void data types

      Void means nothing. for example:

      if we write, 
                          int add(void);
      it means its return type is an int but parameter or argument in add function is nothing.

                          void add(int a, int b);
      it means its return type is void i.e add function cannot return any value.

      Program: 

      #include <stdio.h>
      int add(int a, int b);
      int main( )
      {
          int a, b, c;
          printf ("Enter value of a and b");
          scanf ("%d %d", &a,&b);
          printf ("Entered values of a and b\n a=%d \n b=%d\n", a, b);
          c=add(a,b);
          printf ("c=%d\n", c);
          return 0;
      }
      int add(int a, int b)
      {
          int d;
          d=a+b;
          return d;
      }

      Input: a=10, b=40

      Output: c= 50

      In the above mention program, the return type is int and parameters list is int a and int b.

      Program:

      #include <stdio.h>
      void add(int a, int b);
      int main()
      {
          int a, b,c;
          printf ("Enter value of a and b");
          scanf ("%d %d", &a,&b);
          printf ("Entered values of a and b\n a=%d \n b=%d\n", a, b);
          add(a,b);

          return 0;
      }
      void add(int a, int b)
      {
          int d;
          d=a+b;
          printf ("d=%d\n", d);

      }

      Input: a=10, b=40

      Output: d= 50

      In the above mention program, the return type is void means it can't return any value.

      Size of data types is measured using the sizeof operator.

      Program:

      #include <stdio.h>
      int main( )
      {
          int a;
          float b;
          char c;
          printf ("a=%d\n b=%d\n c=%d\n", sizeof(a), sizeof(b),sizeof(c));

          double d;
          printf ("d=%d\n", sizeof(d));

          long double e;
          printf ("e=%d\n", sizeof(e));

          short int f;
          printf ("f=%d\n", sizeof(f));

          long int g;
          printf ("g=%d\n", sizeof(g));

          return 0;
      }

      Output:

      Data types in C
      Size of various data types


      2. Derived Data types

      Derived Data types are derived from primitive data types. For example Array, String, Pointer, Functions etc.


      Data Types in C
      Derived Data Types


      3. User-Defined Data types

      This type of data types is defined by the user.  For example Structure, Union, typedef etc.