C program with 10 options I have a C program where the user can select from 10 different options. I've already split the core functionality of each option into different methods, but my main() function is still quite long. The rest of the code in main() involves getting user input and formatting strings to the display so that the program is clearer and looks nice.
E.g. my code is similar to:
int actionOneFunction(char *name) {
    /* Action one logic */
}

int actionTwoFunction(FILE *fptr) {
    /* Action two logic */
}

void getUserInput(char *display_msg, char *input_variable, int variable_size) {
    printf(&quot;%s&quot;, display_msg);
    fgets(input_variable, variable_size, stdin);
    strtok(input_variable, &quot;\n&quot;); /* Remove newline added by fgets */
}

void displayStartingText() {
    /* 
        A bunch of printf statements
    */
}

int main() {
    int result
    int action;

    displayStartingText();

    printf(&quot;Select the action you would like to carry out: &quot;);
    scanf(&quot;%d&quot;, &amp;action);
    getChar(); /* Remove newline generated by scanf from buffer */

    if (action &lt; 0 || action &gt; 10) {
        printf(&quot;Invalid action selected.\n&quot;);
        return 1;
    }

    if (action == 1) {
        char name[255];
        
        printf(&quot;------------\n&quot;);
        printf(&quot; Action One \n&quot;);
        printf(&quot;------------\n&quot;);

        getUserInput(&quot;Enter the name: &quot;, name, sizeof(name));
        result = actionOneFunction(name);
        if (result != 0) {
            return errno;
        }
        printf(&quot;Action one was successful!\n&quot;);
    }
    else if (action == 2) {
        FILE *fptr;
        char name[255];
        char *content;

        printf(&quot;------------\n&quot;);
        printf(&quot; Action Two \n&quot;);
        printf(&quot;------------\n&quot;);

        getUserInput(&quot;Enter the file name: &quot;, name, sizeof(name));

        fptr = openFile(name, &quot;r&quot;);
        content = actionTwoFunction(fptr);
        fclose(fptr);

        printf(&quot;Content: \n&quot;, content);
    }
    else if (action == 3) {
        /* ... */
    }

    /* 
        ... 
    */

    else if (action == 10) {
        /* ... */
    }

    return 0;

}

With all ten options and their functionality defined, my main() becomes rather long. Would it be better to put each of the if blocks into their own functions?
I can't be very detailed here because I haven't worked in C lately. But basically, yes, you should move more of the code out of the if statements into the breakout functions.
Specifically, each break-out function should have the same signature (type/arity). Based on the examples you've given so far, these appear to be quite open-ended effectful subroutines, each comparable to a mini main, so they might as well have signature ()-&gt;int. Or maybe there'll be enough structure to their behavior that you can be more specific than that, IDK.
If you're going to print out the fancy headers, abstract that away into a helper function.
Anyway, you might consider even getting rid of the if itself, in favor of a lookup-table of functions. Exactly how to implement that will depend a bit on the context; it's not something I've ever done in C.