fork download
  1. // assert() with printf() style messaging example by gbmhunter for www.mbedded.ninja
  2. // Designed as an example of assert() for embedded systems.
  3. // See http://w...content-available-to-author-only...d.ninja/programming/languages/c/assertions-assert
  4. #include <cassert>
  5. #include <cstdio>
  6.  
  7. #define LOG_ERROR(M, ...) fprintf(stderr, "[ERROR] (%s:%d) " M "\r\n", __FILE__, __LINE__, ##__VA_ARGS__)
  8. #define assertf(A, M, ...) if(!(A)) { LOG_ERROR(M, ##__VA_ARGS__); assert(A); }
  9.  
  10. int main() {
  11. int x = 6;
  12. assertf(x == 7, "x must equal 7. x = %i.", x);
  13. }
  14.  
Runtime error #stdin #stdout #stderr 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
[ERROR] (prog.cpp:12) x must equal 7. x = 6.
prog: prog.cpp:12: int main(): Assertion `x == 7' failed.