ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCSI-311Data Types
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CSI-311

    Programming Fundamentals

    Progress0 / 17 done
    Topics17
    1Overview of Computers and Programming2Overview of Languages (e.g., C Language)3Basics of Structured and Modular Programming4Basic Algorithms and Problem Solving5Development of Basic Algorithms6Analyzing Problems7Designing Solutions8Testing Designed Solutions9Fundamental Programming Constructs10Translation of Algorithms to Programs11Data Types12Control Structures13Functions14Arrays15Records16Files17Testing Programs
    6 min read
    Topic 11 of 17
    Topic Study Note Illustration
    11

    Data Types

    In C programming, data types specify the type of data that a variable can hold. Each data type requires a certain amount of memory and defines what...

    Data Types in C Language

    In C programming, data types specify the type of data that a variable can hold. Each data type requires a certain amount of memory and defines what kind of operations can be performed on the data. C provides a variety of data types for different kinds of values, from integers to floating-point numbers to characters and more.

    There are two main categories of data types in C:

    1. Primary Data Types (also called basic or primitive data types)
    2. Derived Data Types

    Let’s break them down:


    1. Primary (Basic) Data Types in C

    These are the most fundamental data types and are directly supported by C.

    a) Integer Types

    Integer types are used to store whole numbers (both positive and negative, without any decimal points).

    • int: Represents a standard integer. The size of an int can vary depending on the machine but is typically 4 bytes on most systems.

      • Example:
        int x = 100;
        
    • short: Represents a short integer. It usually requires less memory than an int (typically 2 bytes).

      • Example:
        short y = 10;
        
    • long: Represents a long integer. It usually requires more memory than an int (typically 8 bytes or 4 bytes on 32-bit systems).

      • Example:
        long z = 100000L;
        
    • long long: Represents a longer integer type (typically 8 bytes).

      • Example:
        long long a = 123456789123456789LL;
        
    • unsigned variants: These types represent non-negative integers. The unsigned keyword can be added to any of the integer types (e.g., unsigned int, unsigned short, etc.).

      • Example:
        unsigned int u = 100;
        

    Note: The size of these integer types may depend on the system architecture (e.g., 32-bit vs. 64-bit).

    b) Floating-Point Types

    Floating-point types are used to represent real numbers (numbers that have fractional parts, like 3.14 or -0.001).

    • float: Represents single-precision floating-point numbers. It typically takes 4 bytes of memory.

      • Example:
        float pi = 3.14f;
        
    • double: Represents double-precision floating-point numbers. It usually takes 8 bytes of memory and provides more precision than float.

      • Example:
        double pi = 3.14159265358979;
        
    • long double: Represents extended-precision floating-point numbers. The size can vary but is typically 10, 12, or 16 bytes, depending on the compiler and system architecture.

      • Example:
        long double pi = 3.141592653589793238L;
        

    c) Character Types

    Character types are used to store individual characters (letters, digits, symbols).

    • char: Represents a single character. It typically takes 1 byte of memory and can store characters based on the ASCII character set.

      • Example:
        char letter = 'A';
        
    • unsigned char: Represents a character, but can only store positive values (0 to 255). It still takes 1 byte of memory.

      • Example:
        unsigned char u_letter = 'B';
        
    • signed char: Represents a character that can store both positive and negative values (from -128 to 127).

      • Example:
        signed char s_letter = -65;
        

    d) Boolean Type (not a built-in data type in C, but can be simulated)

    C doesn’t have a built-in Boolean data type, but stdbool.h library defines bool as a macro for int (0 for false, non-zero for true).

    • bool: Represents a boolean value, which can be true (1) or false (0).
      • Example:
        #include <stdbool.h>
        bool isValid = true;
        

    2. Derived Data Types in C

    Derived data types are built from the primary data types. These are more complex data types used to store collections of data or references to data.

    a) Arrays

    An array is a collection of elements of the same data type stored in contiguous memory locations.

    • Syntax:
      data_type array_name[size];
      
      • Example:
        int arr[5] = {1, 2, 3, 4, 5};
        

    b) Pointers

    A pointer is a variable that holds the memory address of another variable.

    • Syntax:
      data_type *pointer_name;
      
      • Example:
        int x = 10;
        int *ptr = &x;  // Pointer to x
        

    c) Structures

    A structure (or struct) is a user-defined data type that allows grouping different types of variables under one name.

    • Syntax:
      struct structure_name {
          data_type member1;
          data_type member2;
          // other members
      };
      
      • Example:
        struct Person {
            char name[50];
            int age;
        };
        struct Person person1 = {"John Doe", 30};
        

    d) Unions

    A union is similar to a structure, but all members share the same memory location. A union allows storing different data types in the same memory location, but only one member can hold a value at a time.

    • Syntax:
      union union_name {
          data_type member1;
          data_type member2;
          // other members
      };
      
      • Example:
        union Data {
            int i;
            float f;
            char str[20];
        };
        union Data data;
        data.i = 10;  // Only one member can hold a value at a time
        

    e) Enumerations

    An enumeration (or enum) is a user-defined data type consisting of a set of named integer constants. It helps make code more readable by giving names to integral values.

    • Syntax:
      enum enum_name { constant1, constant2, constant3, ... };
      
      • Example:
        enum Day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
        enum Day today = Wednesday;
        

    3. Type Modifiers

    C also provides type modifiers that modify the size and range of the basic data types:

    • signed: Used to indicate that the data type can store both positive and negative values (default for int).
    • unsigned: Used to indicate that the data type can only store non-negative values.
    • long: Increases the size of the data type.
    • short: Reduces the size of the data type.

    Example (with modifiers):

    unsigned int u = 10;  // Non-negative integers only
    short int si = 5;     // Smaller range of integers
    long double ld = 3.14159L;  // More precision for floating-point numbers
    

    Summary of Data Types in C

    • Basic Data Types:

      • int, short, long, long long, float, double, char
    • Derived Data Types:

      • Arrays, Pointers, Structures, Unions, Enumerations
    • Type Modifiers:

      • signed, unsigned, short, long

    Each data type in C has a specific use case and is optimized for particular operations. Choosing the right data type helps in managing memory effectively and ensuring that the program runs efficiently.

    ← Previous topic 10
    Translation of Algorithms to Programs
    Next topic 12 →
    Control Structures

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    6mremaining
    1.0kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals