{"slug": "assert-default-params-c", "title": "assert_default_params.c", "summary": "The file `assert_default_params.c` defines a custom `assert` macro that supports optional error messages by wrapping an `assert_func` function, which prints a failure message to stderr and calls `abort()` if a condition is false. The `main` function demonstrates this by asserting that `argc` is greater than zero and that `argv[0]` is not null, with the second assertion including a custom error string. This code illustrates a technique for adding descriptive assertions in C using variadic macros.", "body_md": "assert_default_params.c\n\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\nvoid\n \nassert_func\n(\nint\n \ncond\n, \nconst\n \nchar\n \n*\nmsg\n) {\n\n \nif\n (!\ncond\n) {\n\n \nfprintf\n(\nstderr\n, \n\"assert failed: %s\\n\"\n, \nmsg\n);\n\n \nabort\n();\n\n    }\n\n}\n\n#define\n \nassert_helper\n(\nx\n, \ny\n, ...) assert_func(x, y)\n\n#define\n \nassert\n(\nx\n, ...) assert_helper((x), ## __VA_ARGS__, #x)\n\nint\n \nmain\n(\nint\n \nargc\n, \nchar\n \n*\n*\nargv\n) {\n\n \nassert\n(\nargc\n \n>\n \n0\n);\n\n \nassert\n(\nargv\n[\n0\n] \n!=\n \n0\n, \n\"argv[0] cannot be null\"\n);\n\n}", "url": "https://wpnews.pro/news/assert-default-params-c", "canonical_source": "https://gist.github.com/pervognsen/9d46bf9b9187cb7f89d64132dc586e07", "published_at": "2021-02-01 10:13:51+00:00", "updated_at": "2026-05-22 12:39:11.441614+00:00", "lang": "en", "topics": ["developer-tools"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/assert-default-params-c", "markdown": "https://wpnews.pro/news/assert-default-params-c.md", "text": "https://wpnews.pro/news/assert-default-params-c.txt", "jsonld": "https://wpnews.pro/news/assert-default-params-c.jsonld"}}