fanf: (dotat)
[personal profile] fanf

Here's an amusing trick that I was just discussing with Mark Wooding on IRC.

Did you know you can define functions with optional and/or named arguments in C99? It's not even completely horrible!

The main limitation is there must be at least one non-optional argument, and you need to compile with -std=c99 -Wno-override-init.

(I originally wrote that this code needs C11, but Miod Vallat pointed out it works fine in C99)

The pattern works like this, using a function called repeat() as an example.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    // Define the argument list as a structure. The dummy argument
    // at the start allows you to call the function with either
    // positional or named arguments.

    struct repeat_args {
        void *dummy;
        char *str;
        int n;
        char *sep;
    };

    // Define a wrapper macro that sets the default values and
    // hides away the rubric.

    #define repeat(...) \
            repeat((struct repeat_args){ \
                    .n = 1, .sep = " ", \
                    .dummy = NULL, __VA_ARGS__ })

    // Finally, define the function,
    // but remember to suppress macro expansion!

    char *(repeat)(struct repeat_args a) {
        if(a.n < 1)
            return(NULL);
        char *r = malloc((a.n - 1) * strlen(a.sep) +
                         a.n * strlen(a.str) + 1);
        if(r == NULL)
            return(NULL);
        strcpy(r, a.str);
        while(a.n-- > 1) { // accidentally quadratic
            strcat(r, a.sep);
            strcat(r, a.str);
        }
        return(r);
    }

    int main(void) {

        // Invoke it like this
        printf("%s\n", repeat(.str = "ho", .n = 3));

        // Or equivalently
        printf("%s\n", repeat("ho", 3, " "));

    }

I wanted to thank you.

Date: 2017-01-10 18:25 (UTC)
From: (Anonymous)
Good day!

Best resume and cover letters templates.

[url=https://www.etsy.com/shop/ResumeX]Download resume templates in Words' doc-file with high print quality and creative forms, build a polished cv and win your dream job position.[/url]

These resume templates created by professionals who realize what recruiters look for.

Where to start?

STEP 1: Idea. Start by putting together the content of your resume. Focus on work experience, summary of achievements, keywords and any other significant info that makes a powerful resume. [url=http://www.rawresume.com/]“How to Make Up a Great Resume”[/url] will take you through the whole process step by step.

STEP 2: Format chronological, functional or combination. Make sure you present your career in the style that highlights your experience, knowledge, and skills the best possible way. See [url=https://www.jmu.edu/cap/students/jobintern/resumes/format.shtml]“Choosing a Format” Guide by James Madison University[/url] to decide which format is the best for you.

STEP 3: Appearance. [url=https://www.etsy.com/shop/ResumeX] Find the most qualified template to ‘dress up’ your content[/url]. This shop has different templates to choose from.

Good bye!

Date: 2017-01-11 08:38 (UTC)
deborah_c: (Default)
From: [personal profile] deborah_c
You are an evil man. Have you been talking to Simon Tatham again? ;-)

June 2025

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated 2025-06-09 00:26
Powered by Dreamwidth Studios