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 6219519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:47:52+00:00 2026-05-24T07:47:52+00:00

I could use something like javas finally clause in a C switch. Most of

  • 0

I could use something like javas finally clause in a C switch. Most of my cases has a shared set of functionality that I would like to put a single case. I was thinking of implementing this using goto statements, being well aware of gotos code obfuscation ability, putting the shared case at the bottom of the switch statement still seems a “cleaner” way to do this than partitioning the shared functionality into a seperate function.

Anyway, I have been trying to do it something like this:

switch( x ) {
case 0:
    printf("Case 0\n");
    goto case 2;
    break;
case 1:
    printf("Case 1\n");
    goto case 2;
    break;
case 2:
    printf("Case 2\n");
    break;
default:
    // do nothing
    break;
}

However, using gcc, this fail with the error

error: expected identifier or ‘*’ before ‘case’

Any suggestions on how to make it work? Or possibly a better approach?

  • 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-24T07:47:52+00:00Added an answer on May 24, 2026 at 7:47 am

    The “best practice” is of course to delegate the shared code to a function. But in some situations where this is overengineering, or simply not possible / desirable, then you can do:

    switch( x ) 
    {
    case 0:
        printf("Case 0\n");
        goto shared_material;
    
    case 1:
        printf("Case 1\n");
        goto shared_material; // Unnecessary, but keep it for clarity.
    
    case 2:
    shared_material:
        printf("Case 2\n");
        break;
    
    default:
        // Write a meaningful error message somewhere
        return -1;
    }
    

    I don’t find this too unreadable, and I don’t have any problem with it, provided the whole statement fits in one screen (otherwise it qualifies as spaghetti code). However, you may have to defend it in code reviews, which is one of the main reasons I would step away from such constructs and rethink the code.

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

Sidebar

Related Questions

Depends on your experience could you recommend something? I've use izpack , and it's
I could use some help creating an XSL template that will take a string
Common question but I could use an english explanation. Is it like Java where
could someone help me with the regex pattern that i could use to match
It seems like you could use a mashup of Relector and a Debugger to
In PHP if we need to match a something like, [one,two,three] , we could
I have a series of xml files that looks something like this: <ROOT> <F
I could use some help writing a regular expression. In my Django application, users
I could use the following: <fileset dir=C:\Program Files\eclipse\plugins\myplugin> <include name=extraStuff.jar /> </fileset> But that
With C# 3.0 you could use many of its features (object initializers, var variables,

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.