How to convert char* to bool in C

2 Answers

0 votes
#include <stdio.h> 
#include <stdbool.h>
#include <string.h>

int main(void) 
{ 
    char *pch = "false";
	bool b = pch && stricmp(pch, "true") == 0;
	
    fputs(b ? "true" : "false", stdout);
     
    return 0; 
} 
   
   
   
/*
run:
   
false
 
*/

 



answered Aug 10, 2019 by avibootz
0 votes
#include <stdio.h> 
#include <stdbool.h>
#include <stdlib.h>

int main(void) 
{ 
    char *pch = "1";
	bool b = (char)atoi(pch);
	
    fputs(b ? "true" : "false", stdout);
     
    return 0; 
} 
   
   
   
/*
run:
   
true
 
*/

 



answered Aug 10, 2019 by avibootz

Related questions

1 answer 188 views
188 views asked Aug 10, 2019 by avibootz
1 answer 470 views
470 views asked Aug 7, 2019 by avibootz
1 answer 206 views
206 views asked Aug 11, 2019 by avibootz
1 answer 225 views
225 views asked Aug 11, 2019 by avibootz
2 answers 259 views
259 views asked Aug 11, 2019 by avibootz
2 answers 257 views
257 views asked Aug 10, 2019 by avibootz
2 answers 208 views
208 views asked Aug 10, 2019 by avibootz
...