Follow

Data Types Revisited || chapter 6

 

  1. Data types are an essential aspect of any programming language as they define the type and size of data that can be stored in a variable. In C programming language, there are two categories of data types: primitive data types and derived data types.

    1. Primitive Data Types:

    1. Primitive data types are the basic building blocks of data types in C. These include:

  • Integers: Integers are data types that represent whole numbers. They can be declared using the int keyword and can be further classified as short, long, or unsigned, depending on the size and range of the value they can store.
  • Characters: Characters are data types that represent single characters. They can be declared using the char keyword and can be further classified as signed or unsigned.
  • Floating-point numbers: Floating-point numbers are data types that represent numbers with fractional parts. They can be declared using the float and double keywords.

        1. Derived Data Types:

        1. Derived data types are created by combining primitive data types. These include:

      • Arrays: An array is a collection of similar data elements that are stored in contiguous memory locations. They can be declared using the square brackets [].
      • Pointers: A pointer is a variable that stores the memory address of another variable. They can be declared using the * operator.
      • Structures: A structure is a user-defined data type that allows us to combine data items of different data types. They can be declared using the struct keyword.
      • Union: A union is similar to a structure, but it allows us to store only one data item at a time, and all the data items share the same memory location. They can be declared using the union keyword.
      • Enumerations: An enumeration is a user-defined data type that consists of a set of named integer constants.

      1. Integers, Long and Short:

        Integers are data types that represent whole numbers. They can be declared using the keywords int, short, or long, which determine the size of the integer. Example:

      c
      int x = 10
      short y = 5
      long z = 1000;

      1. Integers, Signed and Unsigned:

      1. Integers can also be signed or unsigned. A signed integer can represent both positive and negative numbers, while an unsigned integer can only represent non-negative numbers. Example:
      c
      signed int x = -10
      unsigned int y = 10;

      1. Chars, Signed and Unsigned:

      1. Chars are data types that represent single characters. They can be declared as signed or unsigned, which determines the range of values that can be stored. Example:
      c
      signed char x = 'a';
      unsigned char y = 'b';

      1. Floats and Doubles:

      1. Floats and doubles are data types that represent floating-point numbers. Floats have a smaller range and precision than doubles, but they require less memory. Example:
      c
      float x = 3.14159
      double y = 3.141592653589793;
      1. Storage Classes in C:

        Storage classes in C define the scope and lifetime of variables. There are four storage classes in C: automatic, register, static, and external.

      2. Automatic Storage Class:

        Variables declared with the automatic storage class have a local scope and are created when the function is called. They are destroyed when the function returns. Example:

      c
      void foo()
      int x; 
      // automatic storage class // do something with x 
      }

      1. Register Storage Class:

      1. Variables declared with the register storage class are stored in the CPU register for faster access. They have a local scope and are destroyed when the function returns. Example:
      c
      void foo()
      register int x; 
      // register storage class // do something with x 
      }

      1. Static Storage Class:

      1. Variables declared with the static storage class have a global scope and retain their value between function calls. They are initialized to zero by default. Example:
      c
      void foo()
      static int x; 
      // static storage class // do something with x
      }

      1. External Storage Class:

      1. Variables declared with the extern storage class have a global scope and can be accessed from other files. They are not initialized by default. Example:
      c
      extern int x; // extern storage class
      1. Which to Use When:

        The storage class used depends on the scope, lifetime, and access requirements of the variable. Automatic storage class is the default and is used for most variables. Register storage class is used for variables that require faster access. Static and external storage classes are used for variables that retain their value between function calls and require global access.

      2. Summary:

        Data types in C define the type and size of the data stored in a variable. Integers, chars, floats, and doubles are primitive data types. Storage classes in C define the scope and lifetime of variables. Automatic, register, static, and external storage classes are available. The storage class used depends on the scope, lifetime, and access requirements of the variable.

      No comments:

      Post a Comment

      Tell us how you like it.