Boolean to string conversion means converting a Boolean value such as true or false into its corresponding text representation. In C, Boolean values are available through the <stdbool.h> header, where true represents 1 and false represents 0.
- A conditional operator or
if-elsestatement can be used to convert a Boolean value into"true"or"false". - A separate function can also be created when the conversion is needed multiple times in a program.
Examples:
Input:
value = true
Output:true
Explanation: The Boolean valuetrueis converted into the string"true".Input:
value = false
Output:false
Explanation: The Boolean valuefalseis converted into the string"false".
Approaches for Boolean to String Conversion
We can convert a Boolean value to a string using a conditional operator, an if-else statement, or a user-defined function.
1. Boolean to String Conversion Using the Conditional Operator
The conditional operator provides a simple way to choose between the strings "true" and "false" based on the Boolean value.
Algorithm:
- Declare a Boolean variable.
- Check the Boolean value using the conditional operator.
- Return
"true"if the value is true. - Otherwise, return
"false". - Print the resulting string.
#include <stdbool.h>
#include <stdio.h>
int main(){
bool value = true;
const char *result = value ? "true" : "false";
printf("%s", result);
return 0;
}
Output
trueExplanation: The variable value contains true, so the conditional operator selects the string "true" and stores its address in result.
2. Boolean to String Conversion Using if-else
We can also use an if-else statement to check the Boolean value and print the corresponding string.
Algorithm:
- Declare a Boolean variable.
- Check whether the value is true.
- If it is true, print
"true". - Otherwise, print
"false".
#include <stdbool.h>
#include <stdio.h>
int main(){
bool value = false;
if (value)
printf("true");
else
printf("false");
return 0;
}
Output
falseExplanation: The value stored in value is false, so the else block executes and prints "false".
3. Boolean to String Conversion Using a Function
A separate function can be used to convert Boolean values into strings. This approach is useful when the conversion is required at multiple places in a program.
Algorithm:
- Create a function that accepts a Boolean value.
- Return "true" when the value is true.
- Return "false" when the value is false.
- Call the function from main().
- Print the returned string.
#include <stdbool.h>
#include <stdio.h>
const char *boolToString(bool value){
return value ? "true" : "false";
}
int main(){
bool value = true;
printf("%s", boolToString(value));
return 0;
}
Output
trueExplanation: The boolToString() function checks the Boolean value and returns the corresponding string. Since value is true, the function returns "true".
4. Boolean to String Conversion Using a String Array
A string array can map Boolean values directly to "false" and "true" because Boolean values in C evaluate to 0 or 1.
Algorithm:
- Create an array containing "false" and "true".
- Declare a Boolean variable.
- Use the Boolean value as the array index.
- Print the selected string.
#include <stdbool.h>
#include <stdio.h>
int main(){
bool value = true;
const char *boolString[] = {"false", "true"};
printf("%s", boolString[value]);
return 0;
}
Output
trueExplanation: The Boolean value true corresponds to 1, so boolString[1] selects "true". If the value were false, index 0 would select "false".