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 261 views
261 views asked Aug 10, 2019 by avibootz
1 answer 556 views
556 views asked Aug 7, 2019 by avibootz
1 answer 297 views
297 views asked Aug 11, 2019 by avibootz
1 answer 327 views
327 views asked Aug 11, 2019 by avibootz
2 answers 393 views
393 views asked Aug 11, 2019 by avibootz
2 answers 366 views
366 views asked Aug 10, 2019 by avibootz
2 answers 358 views
358 views asked Aug 10, 2019 by avibootz
...