How to toggle a boolean in C

2 Answers

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

int main(void) {
    bool x = true;
    printf("%s\n", x ? "true" : "false");
    
    x = !x;
    printf("%s\n", x ? "true" : "false");
    
    x = !x;
    printf("%s\n", x ? "true" : "false");
    
    x = !x;
    printf("%s\n", x ? "true" : "false");
    
    return 0;
}


   
   
   
/*
run:
 
true
false
true
false
   
*/

 



answered May 9, 2022 by avibootz
0 votes
#include <stdio.h>
#include <stdbool.h>

int main(void) {
    printf("%s\n", !true ? "true" : "false");
    
    printf("%s\n", !false ? "true" : "false");
    
    return 0;
}


   
   
   
/*
run:
 
false
true
   
*/

 



answered May 9, 2022 by avibootz

Related questions

2 answers 132 views
132 views asked May 9, 2022 by avibootz
2 answers 148 views
2 answers 135 views
1 answer 116 views
...