Explain the data type in C with example

Data type in C

Data Type in C: A data types in C specify the type of data that variables can hold. There are several basic data types in C:

Integer Data type in C

Integer Types: Used to store whole numbers without decimal points. Examples include:

  • int: Typically 4 bytes on most systems. Example: int age = 30;
  • short: Typically 2 bytes. Example: short quantity = 10;
  • long: Typically 4 or 8 bytes, depending on the system. Example: long population = 1000000L;
  • char: Typically 1 byte, stores single characters. Example: char grade = 'A';

Floating Data type in C

Floating-Point Types: Used to store numbers with decimal points. Examples include:

  • float: Typically 4 bytes. Example: float pi = 3.14f;
  • double: Typically 8 bytes, provides more precision than float. Example: double price = 19.99;

Void Data type in C

Void Type: Represents the absence of type. It is often used in functions that do not return a value or take no parameters. Example:

  • void: Example of a function returning no value: void printMessage() { printf("Hello, World!\n"); }

Derived Data type in C

  • Array Types: Contiguous collection of data elements of the same type. Example: int numbers[5] = {1, 2, 3, 4, 5};
  • Pointer Types: Store memory addresses of other data types. Example: int *ptr = &age;
  • Structure Types: Store multiple variables of different types under a single name. See below structure example.
  • Union Types: Similar to structures but share the same memory location for all members. See below union example.

Structure Data Type in C

struct Person {
    char name[50];
    int age;
    float salary;
};
struct Person employee;

Union Data Type in C

union Data {
    int i;
    float f;
    char str[20];
};
union Data data;

These data types provide flexibility for storing different kinds of data efficiently in memory, depending on the requirements of the program.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *