Followers

Friday, November 17, 2023

User defined data types used in C language

 

User-defined data types in C allow programmers to create their own data types based on the existing basic and derived data types. These types help in organizing and structuring data in a way that is meaningful for the problem being solved. The primary user-defined data types in C are structures, unions, enumerations, and typedefs.

  1. 1. Structures:

    • Structures allow users to define a composite data type that groups variables of different data types under a single name.
    • Each variable inside the structure is called a member.
    • Structs provide a way to represent complex entities with multiple attributes.
    struct Person {
    char name[50];
    int age;
    float height;
    };
    struct Person john; // Creating an instance of the structure
  2. 2. Unions:

    • Unions are similar to structures but share the same memory location for all their members.
    • Only one member of a union can be accessed at a time.
    • Useful when different data types need to share the same memory space.
    union Data {
    int i;
    float f;
    char str[20];
    };
    union Data myData; // Creating an instance of the union
  3. 3. Enumerations (Enums):

    • Enums provide a way to create named integer constants.
    • They offer a readable and symbolic representation for a set of related values.
    • Enums make the code more self-documenting.
    enum Color { RED, GREEN, BLUE };
    enum Color selectedColor = GREEN;
  4. 4. Typedef:

    • typedef is used to create an alias for an existing data type.
    • It allows programmers to define custom names for existing data types, making the code more readable and portable.
    typedef unsigned int uint; // uint is now an alias for unsigned
    int
    uint x = 42;

These user-defined data types help improve code organization, readability, and maintainability. They allow programmers to model real-world entities more effectively and create abstractions that make code easier to understand and reason about.

Basic data types used in c Language.


C language provides several basic data types that are used to define variables. Here are some of the basic data types in C:
  1. 1. int: Integer data type is used to store whole numbers. It can be both positive and negative.

    int x = 10;
  2. 2. float: Float data type is used to store floating-point numbers (real numbers).

    float pi = 3.14;
  3. 3. double: Double data type is used to store double-precision floating-point numbers. It provides more precision than the float data type.

    double bigNumber = 123456789.987654321;
  4. 4. char: Char data type is used to store a single character.

    char grade = 'A';
  5. 5. short: Short data type is used to store small integers.

    short smallNumber = 5;
  6. 6. long: Long data type is used to store large integers.

    long bigInteger = 1234567890;
  7. 7. long long: Long long data type is used to store very large integers.

    long long veryBigInteger = 1234567890123456789LL;
  8. 8. _Bool: Bool data type is used to represent boolean values. It can have values either true (1) or false (0).

    _Bool isTrue = 1;
  9. 9. void: Void is a special data type that indicates the absence of a specific type.

    void myFunction() {
    // some code
    }
  10. These data types can be used to declare variables, and the choice of a data type depends on the kind of data we want to store and the range of values it needs to cover. The size of these data types may vary depending on the compiler and the platform.