Asked  10 Months ago    Answers:  5   Viewed   181 times

The GNU case range extension allows case ranges in switch statements:

switch (value) {
    case 1 ... 8:
        printf("Hello, 1 to 8n");
        break;
    default:
        printf("Hello, defaultn");
        break;
}

How would you convert this to standard C (c99, or c89)? Add the individual case statements?

Edit: How would you handle very large switch statements specifically?

 Answers

4
switch(value) 
{
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
        printf("Hello, 1 to 8n"); 
        break;     
    default:         
        printf("Hello, defaultn");         
        break; 
} 

EDIT: To answer the comment.
If you have too many cases, then You might want to consider replacing the switch-case with if-else constructs. It can be much cleaner, concise & readable.

if (value >=1 && value <= 8) 
{    
    printf("Hello, 1 to 8n"); 
} 
else 
{   
    printf("Hello, defaultn"); 
}  
Monday, August 16, 2021
 
jsuggs
 
1

To write the entire contents of the scrollback buffer to a file, type

Ctrl + A and : to get to command mode, then

hardcopy -h <filename>

In older versions of screen, if you just do hardcopy -h, it just writes to the file -h. This was fixed in version 4.2.0, so hardcopy -h writes to hardcopy.N where N is the current window number.

Saturday, July 31, 2021
 
Milo
 
5
char saddr[16];
sprintf(saddr, "%d.%d.%d.%d", (unsigned char)address[0], (unsigned char)address[1], (unsigned char)address[2], (unsigned char)address[3]);

or

char saddr[16];
unsigned char *addr = (unsigned char*)address;

sprintf(saddr, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);

or, as pointed out by dreamlax:

char saddr[16];
sprintf(saddr, "%hhu.%hhu.%hhu.%hhu", address[0], address[1], address[2], address[3]);
Thursday, August 26, 2021
5

Personally, I would use the map, as its use implies a data lookup - using a switch usually indicates a difference in program behavior. Furthermore modifying the data mapping is easier with a map than with a switch.

If performance is a real issue, profiling is the only way to get a usable answer. A switch may not be faster if branch mispredictions happen often enough.

Another approach to think about this is if it wouldn't make more sense to combine the code and the associated value into a datastructure, especially if the range of codes and values is static:

struct Code { int code; int value; };

Code c = ...

std::cout << "Code " << c.code << ", value " << c.value << std::end;
Wednesday, October 6, 2021
 
truemp
 
4

For a lot of values like these, I would use a map something like this:

Dictionary<ClassStructure.Tasks_CallIDs_Strings, Task_Flag>

and retrieve values by mapping the CallIDs strings.

Edit:

As everyone can now see, the real problem of refactoring this example lies in refactoring User.TASKX. Making it a list should suffice - as it could then be indexed by the same string ClassStructure.Tasks_CallIDs_Strings

Wednesday, January 5, 2022
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :