Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6808865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:00:31+00:00 2026-05-26T20:00:31+00:00

In H&S5 I encountered the most bizarre switch statement (8.7.1, p. 277) not using

  • 0

In H&S5 I encountered the “most bizarre” switch statement (8.7.1, p. 277) not using braces.
Here’s the sample:

switch (x)
    default:
    if (prime(x))
        case 2: case 3: case 5: case 7:
            process_prime(x);
    else
        case 4: case 6: case 8: case 9: case 10:
            process_composite(x);

The idea seems to be to avoid the overhead of prime(x) for the most common small numbers.

When I saw that statement, I was confused about the missing braces, but checking the official grammar (C1X pre-standard, 6.8.4, p. 147), the syntax was correct: A switch statement just has a statement after the switch expression and the closing parenthesis.

But in my programming practice I never again encountered such a curious switch statement (and I wouldn’t want to see any in code that I have to take responsibility for), but I started wondering:

Would any of you know such a switch expression, one without using braces, but still having meaning? Not just switch (i); (which is legal, but a NOP), but using at least two case labels having some sort of useful purpose?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:00:32+00:00Added an answer on May 26, 2026 at 8:00 pm

    If you use control structures in macros a switch instead of if comes handy since it has no dangling else problem.

    #define DEBUG_PRINT(...) switch (!debug_mode) case 0: fprintf(__VA_ARGS__)
    

    With that you don’t have surprises if a user of that macro puts this in an additional condition

    if (unclear) DEBUG_PRINT(stderr, "This is really %unclear\n", unclear);
    else {
     // do something reasonable here
    }
    

    Such a debug macro has the advantage of being always compiled (and then eventually optimized out). So the debug code has to remain valid through all the live time of the program.

    Also observe here that it is important that the switch doesn’t use {}, otherwise the if/else example wouldn’t work either. All this could be achieved by other means (if/else , (void)0 and do/while tricks) but this one is the most convenient I know of.

    And don’t take me wrong, I don’t say that everybody should use control structures inside macros, you certainly should know what you are doing. But there are situations where it is justified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I encountered some strange behavior which hints that I do not understand some basic
I encountered this small piece of code in this question , & wanted to
I have encountered some java script code which I believe is malicious but most
I've just encountered a weird bug/feature in table cells under Chrome & Firefox. it
I'm using SQLite & in that i'm using the following query- INSERT INTO Contact(FirstName,
I've encountered a situation where, trying not to have to modify the underlying source
just encountered the code for doing tab expansion in perl, here is the code:
I encountered a wired problem on my C++ code. I'm using TCLAP software to
I've encountered a strange bahaviour, when implementing a Facebook app using the iframe canvas
I have encountered the following statement in fortran: integer iparam(11), ipntr(14) logical select(maxncv) Double

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.