Skip to content
Related Articles

Related Articles

C Program For Boolean to String Conversion

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Sep, 2022
View Discussion
Improve Article
Save Article

To convert boolean to string in C we will use the following 2 approaches:

  1. Using Conditional Statements
  2. Using Ternary Operator

Input: 

bool n = true 

Output: 

string true

1. Using Conditional Statements

C




// C program to demonstrate Boolean to String
// Conversion using conditional statements
#include <stdbool.h>
#include <stdio.h>
int main()
{
 
    bool n = true;
    if (n == true) {
        printf("true");
    }
    else {
        printf("false");
    }
    return 0;
}

Output

true

2. Using the ternary operator 

C




// C program to demonstrate Boolean to String
// Conversion using ternary operator
#include <stdbool.h>
#include <stdio.h>
int main()
{
 
    bool n = true;
 
    const char* s = (n == true) ? "true" : "false";
    printf("%s", s);
    return 0;
}

Output

true

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!