C Data Types

Introduction

In the previous chapter, we learned about C variables. In this chapter, we will learn about data types in C programming. Data types define the type of data that a variable can hold. Understanding data types is essential for writing effective and error-free code.

What are Data Types?

Data types are declarations for variables. This determines the type and size of data associated with variables. They help the compiler to allocate the appropriate memory space for the variables.

Basic Data Types

C provides several basic data types, which can be classified into the following categories:

  1. Integer Types
  2. Floating-Point Types
  3. Character Type
  4. Void Type

1. Integer Types

Integer types are used to store whole numbers (numbers without a fractional part).

  • int:
    • Used to store integer values.
    • Size: Typically 4 bytes.
    • Range: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned).

Example:

#include <stdio.h>

int main() {
    int age = 25; // Declaring an integer variable 'age' and assigning it a value of 25
    printf("Age: %d\n", age); // Printing the value of 'age'
    return 0; // Returning 0 to indicate successful execution
}

Output:

Age: 25
  • short int:
    • Used to store smaller integer values.
    • Size: Typically 2 bytes.
    • Range: -32,768 to 32,767 (signed), 0 to 65,535 (unsigned).

Example:

#include <stdio.h>

int main() {
    short int smallNumber = 32000; // Declaring a short integer variable 'smallNumber' and assigning it a value of 32000
    printf("Small Number: %d\n", smallNumber); // Printing the value of 'smallNumber'
    return 0; // Returning 0 to indicate successful execution
}

Output:

Small Number: 32000
  • long int:
    • Used to store larger integer values.
    • Size: Typically 4 or 8 bytes.
    • Range: -2,147,483,648 to 2,147,483,647 or more (signed), 0 to 4,294,967,295 or more (unsigned).

Example:

#include <stdio.h>

int main() {
    long int largeNumber = 1000000; // Declaring a long integer variable 'largeNumber' and assigning it a value of 1000000
    printf("Large Number: %ld\n", largeNumber); // Printing the value of 'largeNumber'
    return 0; // Returning 0 to indicate successful execution
}

Output:

Large Number: 1000000
  • long long int:
    • Used to store even larger integer values.
    • Size: Typically 8 bytes.
    • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed), 0 to 18,446,744,073,709,551,615 (unsigned).

Example:

#include <stdio.h>

int main() {
    long long int veryLargeNumber = 123456789012345; // Declaring a long long integer variable 'veryLargeNumber' and assigning it a large value
    printf("Very Large Number: %lld\n", veryLargeNumber); // Printing the value of 'veryLargeNumber'
    return 0; // Returning 0 to indicate successful execution
}

Output:

Very Large Number: 123456789012345

2. Floating-Point Types

Floating-point types are used to store real numbers (numbers with a fractional part).

  • float:
    • Used to store single-precision floating-point numbers.
    • Size: Typically 4 bytes.
    • Precision: 6 to 7 decimal digits.

Example:

#include <stdio.h>

int main() {
    float salary = 50000.50; // Declaring a float variable 'salary' and assigning it a value of 50000.50
    printf("Salary: %.2f\n", salary); // Printing the value of 'salary' with 2 decimal places
    return 0; // Returning 0 to indicate successful execution
}

Output:

Salary: 50000.50
  • double:
    • Used to store double-precision floating-point numbers.
    • Size: Typically 8 bytes.
    • Precision: 15 to 16 decimal digits.

Example:

#include <stdio.h>

int main() {
    double pi = 3.141592653589793; // Declaring a double variable 'pi' and assigning it the value of pi
    printf("Pi: %.15lf\n", pi); // Printing the value of 'pi' with 15 decimal places
    return 0; // Returning 0 to indicate successful execution
}

Output:

Pi: 3.141592653589793
  • long double:
    • Used to store extended-precision floating-point numbers.
    • Size: Typically 10, 12, or 16 bytes.
    • Precision: More than double.

Example:

#include <stdio.h>

int main() {
    long double preciseValue = 2.718281828459045235360287; // Declaring a long double variable 'preciseValue' and assigning it a very precise value
    printf("Precise Value: %.21Lf\n", preciseValue); // Printing the value of 'preciseValue' with 21 decimal places
    return 0; // Returning 0 to indicate successful execution
}

Output:

Precise Value: 2.718281828459045235360

3. Character Type

The character type is used to store individual characters.

  • char:
    • Used to store a single character.
    • Size: Typically 1 byte.
    • Range: -128 to 127 (signed), 0 to 255 (unsigned).

Example:

#include <stdio.h>

int main() {
    char grade = 'A'; // Declaring a char variable 'grade' and assigning it the value 'A'
    char symbol = '#'; // Declaring a char variable 'symbol' and assigning it the value '#'
    printf("Grade: %c\n", grade); // Printing the value of 'grade'
    printf("Symbol: %c\n", symbol); // Printing the value of 'symbol'
    return 0; // Returning 0 to indicate successful execution
}

Output:

Grade: A
Symbol: #

4. Void Type

The void type represents the absence of value. It is used in functions that do not return a value.

Example:

#include <stdio.h>

// Function that returns void (nothing)
void printMessage() {
    printf("Hello, World!\n"); // Printing a message
}

int main() {
    printMessage(); // Calling the function
    return 0; // Returning 0 to indicate successful execution
}

Output:

Hello, World!

Conclusion

Understanding data types is important for effective programming in C. Data types determine the type and size of data that can be stored in variables, and they help the compiler allocate memory efficiently. By mastering basic data types, you can write more flexible and powerful programs.

Leave a Comment

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

Scroll to Top