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

Dev Frank
5 min readFeb 5, 2024

--

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

double —

1. Size:

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

2. Declaration:

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

double double_variable;

For Example

double pi = 3.14159265359;

3. Initialization:

You can initialize a double variable at the time of declaration:

double temperature = 98.6;

4. Operations:

double variables support standard arithmetic operations, including addition, subtraction, multiplication, and division, similar to the float data type.

double x = 5.0;
double y = 2.5;
double result = x / y; // result is now 2.0

5. Formatting for Output:

When printing or displaying the value of a double, you can use the %lf format specifier in functions like printf:

printf("The value of pi is %lf\n", pi);

6. Scientific Notation:

double 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

7. Precision:

double provides 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.

8. 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);

long double —

In C programming, the long double data type represents extended-precision floating-point numbers.

It is designed to provide greater precision and a larger range compared to both float and double data types. Here’s a detailed explanation of long double:

1. 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.

2. Declaration:

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

long double long_double_variable;

For Example

long double pi = 3.14159265358979323846;

3. Initialization:

You can initialize a long double variable at the time of declaration:

long double large_number = 12345678901234567890.1234567890;

4. Operations:

long double variables support standard arithmetic operations, including addition, subtraction, multiplication, and division, similar to float and double.

long double x = 5.0;
long double y = 2.5;
long double result = x / y; // result is now 2.0

5. Formatting for Output:

When printing or displaying the value of a long double, you can use the %Lf format specifier in functions like printf:

printf("The value of pi is %Lf\n", pi);

6. Scientific Notation:

long double numbers can be expre8. ssed in scientific notation, using the e or E character to represent the exponent, similar to double.

long double scientific_number = 1.23e-4;  // Equivalent to 0.000123

7. 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.

8. 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);

void —

void data type is a special data type that indicates the absence of a specific type. Here’s a detailed explanation of the void data type:

1. No Value:

void is used to declare functions that do not return any value. For example:

void myFunction() {
// Function does not return any value
}

2. No Data Type:

When used as a function parameter, void indicates that the function does not expect any arguments.

void printMessage(void) {
printf("Hello, World!\n");
}

3. 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
}

4. Incomplete Type:

void can be used as an incomplete type when declaring pointers.

For Example:

void *ptr;

5. Return Type in Functions:

In function declarations, void indicates that the function does not return a value.

void myFunction(void);

6. Special Case in Pointers:

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));

7. Generic Programming:

In some programming paradigms, void is used in generic programming where functions or data structures can work with multiple data types.

8 .Empty Parameter List:

When used in a function declaration with an empty parameter list, it signifies that the function takes no arguments.

void myFunction(void);

bool —

In C programming, the bool data type is not a native data type like in some other programming languages. However, starting with the C99 standard, a header <stdbool.h> was introduced that includes a boolean type and boolean constants. Here’s an explanation:

1. Header Inclusion:

To use bool in C, include the <stdbool.h> header.

#include <stdbool.h>

2. Boolean Type:

The bool type is introduced, and it can have two possible values: true or false.

bool isReady = true;

3. Boolean Constants:

The header defines two constants: true and false.

#include <stdbool.h>

bool isReady = true;
bool isValid = false;

4. Operations:

The bool type is often used in conditions and logical expressions.

#include <stdbool.h>

bool isReady = true;

if (isReady) {
// Code executed if isReady is true
}

5. Functions Returning bool:

Functions can return bool values.

#include <stdbool.h>

bool isEven(int number) {
return (number % 2 == 0);
}

6. Standard Macros:

The header also defines standard macros bool, true, and false.

#include <stdbool.h>

bool status = true;

int main() {
bool flag = false;
return 0;
}

7. 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 may see boolean variables represented using int, where 0 is equivalent to false and non-zero to true.

8. Conditional Expressions:

bool is commonly used in conditional expressions to improve code readability.

#include <stdbool.h>

bool isPositive(int number) {
return (number > 0);
}

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.