Is bool data type supported in embedded C?

668 Views Asked by At

The wrote the following code for avr MCU and I compiled and uploaded the code with Arduino IDE, I did not get any error and the code works but I remembered C program does not support bool data type. Will this code still be compiled with avr-gcc on Linux.


//The following code is written for ATmega328P microcontroller
// Turns ON the led on a click of a switch and Turns OFF it again another click
#include <avr/io.h>
#include <avr/interrupt.h>


#define LED_PIN   PB5
#define SW_PIN    PD2


volatile bool ledOn = false;


// Function to initialize pinmode and interrupt
void init() {
 // Set LED pin as output
  DDRB = DDRB |(1 << DDB5);
  // Set switch pin as  pull-up resistor
  PORTD = PORTD |(1 << SW_PIN);
  // Enable interrupt on the switch pin
  EIMSK = EIMSK |(1 << INT0);
  // Trigger interrupt on failing edge of the switch signal
  EICRA = (1 << ISC01);
  // Enable global interrupts
  sei();
}


// Interrupt service routine for the switch
ISR(INT0_vect) {
  // If the switch is pressed, toggle the LED state
  if ((~PIND & (1 << SW_PIN)) && !ledOn) {
   PORTB = PORTB | (1 << LED_PIN);
    ledOn = true;
  }
  else{
  PORTB = PORTB & ~(1 << LED_PIN);
    ledOn = false;
  }

}

int main(void) {
 // Initialization
  init();

 // Loop forever
 while (1) {

    // NOP
  }

  return 0;
}```
2

There are 2 best solutions below

0
Clifford On BEST ANSWER

The Arduino IDE uses C++ compilation - you are using C++ not C - even if it is otherwise valid C code, and you are not using the Arduino setup()/loop() framework or Arduino libraries..

With respect to C however, C99 defines a built-in type _Bool. It does that because of all the pre-C99 code that exists that might define types or macro aliases for Boolean in various ways. It prevents breaking existing code that the introduction of a new keywords bool, true and false might otherwise cause.

C99 also defines a header stdbool.h which provides a C++-like bool alias for _Bool, and symbols for true and false values. Including stdbool.h or cstdbool in C++ code has no effect.

The Arduino compiler is the same AVR GCC cross-compiler you are proposing to use on linux. It supports both C and C++ compilation. You can use the avr-g++ driver for C++ compilation, but its only difference to the avr-gcc driver is that it links C++ library support implicitly when invoking ld. Otherwise C or C++ compilation is determined by command line switch or by source file extension.

So your code will compile as-is with AVR GCC if you use the same C++ compilation that Arduino applies, but if you want to ensure it builds with either C or C++ compilation (or you just want to make it valid C code) you can simply add #include <stdbool.h> and it will work in either case.

3
Lundin On

Will this code still be compiled with avr-gcc on Linux

I kind of doubt you will be able to run Linux on an AVR :)

But no it will not compile with gcc under Linux since it is invalid C. From C99 to C17 versions of the standard you need to #include <stdbool.h> to use bool. C uses the keyword _Bool and bool from stdbool.h is just a macro for that.

Upcoming C23 will remove all this nonsense and then bool, true and false will become proper language keywords. So it will actually compile on Linux if you use "gcc trunk" version and compile with -std=c2x, which is about to be formally released as gcc 13 -std=c23 later this year.

The reason why it compiles on your Arduino is because you are using a C++ compiler. That's a different programming language.