Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,817 answers

573 users

How to create an ASSERT macro to catch assertion failure in C

2 Answers

0 votes
#include <stdio.h>

#define ASSERT(expression)                                      \
    {                                                           \
        if (expression) {                                       \
        } else {                                                \
            assertion_failure(#expression, __FILE__, __LINE__); \
        }                                                       \
    }
    
void assertion_failure(const char* expression, const char* file, int line) {
    printf("Assertion Failure: %s, in file: %s, line: %d\n", expression, file, line);
}    
    
int main(void)
{
    ASSERT(1 == 0);
    
    return 0;
}

 
 
/*
run:

Assertion Failure: 1 == 0, in file: main.c, line: 17
 
*/

 



answered May 28, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

#define ASSERT(expression, message)                                      \
    {                                                                    \
        if (expression) {                                                \
        } else {                                                         \
            assertion_failure(#expression, message, __FILE__, __LINE__); \
        }                                                                \
    }
    
void assertion_failure(const char* expression, const char* message, const char* file, int line) {
    printf("Assertion Failure: %s, message: '%s', in file: %s, line: %d\n", expression, message, file, line);
}    
    
int main(void)
{
    ASSERT(1 == 0, "Your message");
    
    return 0;
}

 
 
/*
run:

Assertion Failure: 1 == 0, message: 'Your message', in file: main.c, line: 18
 
*/

 



answered May 28, 2024 by avibootz

Related questions

6 answers 389 views
1 answer 102 views
102 views asked Sep 23, 2021 by avibootz
1 answer 118 views
1 answer 106 views
106 views asked Jan 8, 2024 by avibootz
2 answers 174 views
1 answer 119 views
...