Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a !default case in C

On a micro-controller program I have a few instructions that I would like to execute for every case in a switch except the default. I do not, however, want to write a function call or use a macro for every case.

Because this is for a micro-controller running at 3-7Mhz, speed and code space are important. For example:

switch(letter)
{
case a:
    ShowApple();
    printf("You entered an english letter.");
    break;
case b:
    ShowBananna();
    printf("You entered an english letter.");
    break;
...
case z:
    ShowZebra();
    printf("You entered an english letter.");
    break;
default:
    printf("You did not enter an english letter. Silly human!");
    break;
}
like image 788
Jeremy Avatar asked Dec 07 '22 16:12

Jeremy


1 Answers

int was_default_picked;

was_default_picked = 0;
switch (letter)
{
// ...

default:
    was_default_picked = 1;
}

if (!was_default_picked)
{
    // Your logic goes here
}
like image 73
James McNellis Avatar answered Dec 22 '22 13:12

James McNellis