SShortSingh.
Back to feed

How to Build a Generic min() Function in C Using _Generic

0
·2 views

C lacks built-in templates, making a truly generic minimum function difficult to implement without macros that risk side effects or undefined behavior. A common workaround is to define multiple type-specific inline functions — such as min_ll, min_ull, min_d, and min_ld — each handling a category of numeric types. A helper macro using the ## token-pasting operator can reduce the repetitive boilerplate involved in writing these functions. C11's _Generic keyword can then wrap all these functions under a single min() macro that automatically selects the correct implementation based on the operands' common type. The (I) + (J) expression inside _Generic leverages standard arithmetic conversions, allowing smaller types like char and short to be promoted without needing explicit cases for each.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Developer builds Euro Toolhub to index European software alternatives to US tools

A developer is creating Euro Toolhub, a directory focused on European software, SaaS, cloud, and AI alternatives to widely used tools. The project targets companies, agencies, developers, and privacy-conscious users who prioritize data residency, open-source options, or European legal jurisdiction. Unlike existing lists, Euro Toolhub aims to go beyond simple tool listings by offering detailed provider profiles. The platform is launching with a German-first approach before expanding across Europe. Providers can submit their tools for inclusion via the project's website.

0
ProgrammingDEV Community ·

Dinghy Slide Builder lets engineers write RevealJS presentations in YAML

Dinghy's Slide Builder is a presentation tool that lets developers author RevealJS slides using a YAML DSL, Markdown, or raw HTML stored as source files in a repository. Recognized YAML keys map to semantic HTML elements, while any other key is rendered as its matching HTML tag directly. The tool compiles presentations into a single self-contained HTML file with all assets inlined, requiring nothing beyond a browser to run. A live-reload development server speeds up the editing workflow, and the output can be shared offline or via chat without hosting. A notable exclusive feature allows Prezi-style zoom and pan across regions of an image using pixel coordinates, useful for walking through architecture diagrams without duplicating slides.

0
ProgrammingDEV Community ·

How a Deleted Gson Annotation Silently Zeroes Out All Your Data

A common Gson pitfall in Android development causes integer fields to silently default to zero when the @SerializedName annotation is removed from a Kotlin data class. Without the annotation, Gson cannot match the camelCase Kotlin property name 'baseStat' to the snake_case JSON key 'base_stat', so it leaves the field at its default value of 0 without throwing any error or warning. Gson treats missing JSON keys as acceptable, filling unmatched fields with type defaults such as 0 for integers, false for booleans, and null for objects. The library only throws exceptions for malformed JSON or type mismatches, not for absent keys. Developers are advised to always retain @SerializedName annotations when Kotlin property names differ from their corresponding JSON keys, rather than treating them as redundant code.

0
ProgrammingDEV Community ·

How to Use Attribute Capturing in Unreal Engine Execution Calculations

Attribute capturing in Unreal Engine's Gameplay Ability System allows developers to directly expose attributes from attribute sets for use in execution calculations. This technique enables complex mechanics such as damage resistance, armor reduction, and type-based advantages or weaknesses. A performance-efficient approach involves defining a static struct in the execution calculation's .cpp file using DECLARE_ATTRIBUTE_CAPTUREDEF and DEFINE_ATTRIBUTE_CAPTUREDEF macros to register attributes like Armor and ArmorPenetration. A static function returning the same struct instance avoids repeated object reconstruction, improving performance when many attributes are captured. Finally, the attributes are registered in the ExecCalc constructor by adding their capture definitions to the RelevantAttributesToCapture array.