DEV Community

Cover image for DATATYPES IN C (double, long double, void, bool)
Frank
Frank

Posted on

DATATYPES IN C (double, long double, void, bool)

In the previous story, we talked about some datatypes like int, char, and float. We are going to see more datatypes in this article.


double —

Image showing the text double

  • Size:
    The doubledata type typically requires 8 bytes of memory on most systems, providing more precision compared to the float data type.

  • Declaration:

double double_variable;
Enter fullscreen mode Exit fullscreen mode

Example

double pi = 3.14159265359;
Enter fullscreen mode Exit fullscreen mode
  • Initialization: You can initialize a doublevariable when declaring it.
double temperature = 98.6;
Enter fullscreen mode Exit fullscreen mode
  • Operations: doublesupports standard arithmetic operations, like addition, subtraction, multiplication, and division:
double x = 5.0;
double y = 2.5;
double result = x / y;  // result is now 2.0
Enter fullscreen mode Exit fullscreen mode
  • Output Formatting: When printing or displaying the value of a double, weuse the %lf format specifier in functions like printf:
printf("The value of pi is %lf\n", pi);
Enter fullscreen mode Exit fullscreen mode
  • Scientific Notation: Numbers can be expressed in scientific notation, using the e or E character to represent the exponent.
double scientific_number = 1.23e-4;  // Equivalent to 0.000123
Enter fullscreen mode Exit fullscreen mode
  • Precision:
    doubleprovides double-precision floating-point numbers, offering a larger range and higher precision than the float data type.
    It is suitable for applications where higher precision is required, such as scientific and engineering computations.

  • Limits:
    The <float.h> header file provides constants that represent the limits of various floating-point data types. For double, you can use DBL_MIN and DBL_MAX to get the minimum and maximum representable values.

#include <float.h>

printf("Minimum value of double: %e\n", DBL_MIN);
printf("Maximum value of double: %e\n", DBL_MAX);
Enter fullscreen mode Exit fullscreen mode

long double —

Image showing the text long double

The long double data type represents extended-precision floating-point numbers.

It offers extended-precision floating-point numbers, depending on the platform.

  • Size:
    The size of a long double variable is platform-dependent and is typically larger than that of double. It can vary, but it’s often 8 bytes or more.

  • Declaration:
    To declare a long double variable, you use the following syntax:

long double long_double_variable;
Enter fullscreen mode Exit fullscreen mode

Example

long double pi = 3.14159265358979323846;
Enter fullscreen mode Exit fullscreen mode
  • Initialization: Similar to double, you can initialize long double at the time of declaration:
long double large_number = 12345678901234567890.1234567890;
Enter fullscreen mode Exit fullscreen mode
  • Operations: long double variables support standard arithmetic operations.
long double x = 5.0;
long double y = 2.5;
long double result = x / y;  // result is now 2.0
Enter fullscreen mode Exit fullscreen mode
  • Output Formatting: When printing or displaying the value of a long double, we use the %Lf format specifier in functions like printf:
printf("The value of pi is %Lf\n", pi);
Enter fullscreen mode Exit fullscreen mode
  • Scientific Notation: As with double, long double supports scientific notation:
long double scientific_number = 1.23e-4;  // Equivalent to 0.000123
Enter fullscreen mode Exit fullscreen mode
  • Precision:
    long double provides the highest precision among floating-point data types in C. It is commonly used in applications that demand extremely high accuracy, such as scientific research and complex simulations.

  • Limits:
    The <float.h> header file provides constants that represent the limits of various floating-point data types. For long double, you can use LDBL_MIN and LDBL_MAX to get the minimum and maximum representable values.

#include <float.h>

printf("Minimum value of long double: %Le\n", LDBL_MIN);
printf("Maximum value of long double: %Le\n", LDBL_MAX);
Enter fullscreen mode Exit fullscreen mode

void —

Image showing the text void
void data type is a special data type that is used to indicate the absence of a specific type.

  • No Value: Use void for functions that don’t return a value.
void myFunction() {
    // Function does not return any value
}
Enter fullscreen mode Exit fullscreen mode
  • No Arguments: When used as a function parameter, voidindicates that the function does not expect any arguments.
void printMessage(void) {
    printf("Hello, World!\n");
}
Enter fullscreen mode Exit fullscreen mode
  • Pointer to void (Generic Pointer): A pointer of type void* (generic pointer) can point to objects of any data type. It is often used in functions that need to handle various data types.
void printValue(void *ptr, char type) {
    // Function to print values of different types using a generic pointer
}
Enter fullscreen mode Exit fullscreen mode
  • Incomplete Type: voidcan be used as an incomplete type when declaring pointers.
void *ptr;
Enter fullscreen mode Exit fullscreen mode
  • Return Type in Functions: In function declarations, void indicates that the function does not return a value.
void myFunction(void);
Enter fullscreen mode Exit fullscreen mode
  • Dynamic Memory: void* is commonly used for allocating memory dynamically (with malloc, calloc, etc.) because it can be cast to any pointer type.
void *ptr = malloc(sizeof(int));
Enter fullscreen mode Exit fullscreen mode
  • Generic Programming: In some programming paradigms, void is used in generic programming where functions or data structures can work with multiple data types.

bool —

Image showing the text bool

The bool type was introduced in C99. Before that, C did not have a native boolean type. However, starting with the C99 standard, a header was introduced that includes a boolean type and boolean constants.

  • Header Inclusion: To use boolin C, include the <stdbool.h> header.
#include <stdbool.h>
Enter fullscreen mode Exit fullscreen mode
  • Boolean Type: The bool type can take values trueor false.
bool isReady = true;
bool isValid = false;
Enter fullscreen mode Exit fullscreen mode
  • Boolean Constants: The header defines two constants: true and false.
#include <stdbool.h>

bool isReady = true;
bool isValid = false;
Enter fullscreen mode Exit fullscreen mode
  • Operations: The booltype is often used in conditions and logical expressions.
#include <stdbool.h>

bool isReady = true;

if (isReady) {
    // Code executed if isReady is true
}
Enter fullscreen mode Exit fullscreen mode
  • Functions Returning bool:
#include <stdbool.h>

bool isEven(int number) {
    return (number % 2 == 0);
}
Enter fullscreen mode Exit fullscreen mode
  • Compatibility:
    While C99 and later standards have native support for bool, some older compilers or projects might not fully support it. In such cases, you - boolean variables represented using int, where 0 is equivalent to falseand non-zero to true.

  • Conditional Expressions:
    boolis commonly used in conditional expressions to improve code readability.

#include <stdbool.h>

bool isPositive(int number) {
    return (number > 0);
}
Enter fullscreen mode Exit fullscreen mode

Understanding the different data types in C, like double, long double, void, and bool, is crucial for writing efficient and accurate code. Each datatype has its unique properties and is suited for specific tasks—whether it's handling precise calculations with double and long double, defining functions with no return type using void, or managing logic with bool. Mastering these concepts will allow you to build more robust and flexible programs, especially as you tackle more complex problems in scientific computing, system programming, or everyday application development.


Further Resources

To continue your journey in mastering C and its data types, here are some valuable resources:

Books:
The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie (also known as K&R) — A classic text that introduces C with clarity and depth.

Online Tutorials:
Learn-C.org — An interactive platform to practice C programming.
Video Resources:

YouTube Channel: freeCodeCamp — Provides free tutorials on C programming, among many other languages.


By diving into these resources, you’ll deepen your understanding of C and strengthen your programming skills. Keep experimenting and coding!

Top comments (0)