C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. Basic Elements of C
  5. Data types in C

Data types in C

A data types in C defines the type of data that a variable can hold.

  • It determines the size and layout of the variable’s memory, the range of values that can be stored, and the set of operations that can be performed on it.

C has several built-in data types categorized as:

  • Basic (Primitive) Data Types
  • Derived Data Types
  • User-defined Data Types

a.) int (Integer):

  • Used to store whole numbers (both positive and negative) without fractional parts.
  • Typically occupies 2 or 4 bytes depending on the system.
  • Example values: -10, 0, 100

    b.) float (Floating-point):

    • Used to store single-precision real numbers (numbers with fractional parts).
    • Usually occupies 4 bytes.
    • Example values: 3.14, -0.001, 2.5e3

    c.) double (Double Precision Floating-point):

    • Used to store double-precision real numbers (more precise than float).
    • Usually occupies 8 bytes.
    • Example values: 3.1415926535, 1.0e-10

    d.) char (Character):

    • Used to store a single character or a small integer.
    • Typically occupies 1 byte.
    • Example values: ‘A’, ‘z’, ‘0’

    Example:

    #include <stdio.h>
    
    int main() {
        int age = 30;
        float pi = 3.14159f;
        double e = 2.718281828459045;
        char grade = 'B';
    
        printf("Age: %d\n", age);
        printf("PI: %.5f\n", pi);
        printf("Euler's Number: %.15lf\n", e);
        printf("Grade: %c\n", grade);
    
        return 0;
    }

    a.) Arrays:

    • Collection of elements of the same data type stored contiguously.
    • Example: int numbers[5]; holds 5 integers.

      b.) Pointers:

      • Variables that store memory addresses.
      • Example: int *ptr;

      c.) Structures:

      • User-defined collection of variables of different types.
      • Example: struct Person { char name[50]; int age; };

      Example:

      #include <stdio.h>
      
      // Structure definition
      struct Person {
          char name[50];
          int age;
      };
      
      int main() {
          // Array of integers
          int numbers[5] = {10, 20, 30, 40, 50};
      
          // Pointer to integer
          int *ptr = &numbers[0];
      
          // Structure variable
          struct Person person1 = {"John Doe", 28};
      
          // Print array elements using pointer
          printf("Array elements:\n");
          for (int i = 0; i < 5; i++) {
              printf("%d ", *(ptr + i));
          }
          printf("\n");
      
          // Print structure members
          printf("Name: %s\n", person1.name);
          printf("Age: %d\n", person1.age);
      
          return 0;
      }

      a.) enum (Enumeration):

      • A data type consisting of named integer constants.

      Example:

      #include <stdio.h>
      
      // Enum declaration
      enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
      
      int main() {
          enum Day today = Wednesday;
      
          // Print enum value (integer)
          printf("Enum value of today: %d\n", today);
      
          // Use enum in condition
          if (today == Wednesday) {
              printf("Today is Wednesday.\n");
          }
      
          return 0;
      }

      How can we help?

      Leave a Reply

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