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

  • SEARCH
  • Home
  • 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 7022389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:34:33+00:00 2026-05-27T23:34:33+00:00

Normally when using a switch statement, you cannot define and initialize variables local to

  • 0

Normally when using a switch statement, you cannot define and initialize variables local to the compound statement, like

switch (a)
{
  int b = 5;  /* Initialization is skipped, no matter what a is */

  case 1:
    /* Do something */
    break;
  default:
   /* Do something */
   break;
}

However, since the switch statement is a statement like for or while, there is no rule against not using a compound statement, look here for examples. But this would mean, that a label may be used between the closing parenthesis after the switch keyword and the opening brace.

So in my opinion, it would be possible and allowed to use a switch statement like this:

switch (a)
  default:
{
  int b = 5;  /* Is the initialization skipped when a != 1? */
    /* Do something for the default case using 'b' */
    break;

  case 1: // if a == 1, then the initialization of b is skipped.
    /* Do something */
    break;
}

My question: Is the initialization necessarily performed in this case (a != 1)? From what I know of the standards, yes, it should be, but I cannot find it directly in any of the documents I have available. Can anyone provide a conclusive answer?

And before I get comments to that effect, yes, I know this is not a way to program in the real world. But, as always, I’m interested in the boundaries of the language specification. I’d never tolerate such a style in my programming team!

  • 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-27T23:34:34+00:00Added an answer on May 27, 2026 at 11:34 pm

    Most people think of a switch as a mutiple if, but it is technically a calculated goto. And the case <cte>: and default: are actually labels. So the rules of goto apply in these cases.

    Your both your examples are syntactically legal, but in the second one, when a==1 the b initialization will be skipped and its value will be undefined. No problem as long as you don’t use it.

    REFERENCE:

    According to C99 standard, 6.2.4.5, regarding automatic variables:

    If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block;

    So the variable is initialized each time the execution flow reaches the initialization, just as it were an assignment. And if you jump over the initialization the first time, then the variable is left uninitialized.

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

Sidebar

Related Questions

Normally when using VMWare Server 1.0.7 you are asked if you would like to
Normally you create a function using cfscript like: <cfscript> function foo() { return bar;
Normally (ie. not concurrently), putAll() cannot be more efficient than using lot's of calls
When using a switch() statement, you add break; in between separate case: declarations. But
We normally create objects using the new keyword, like: Object obj = new Object();
when I'm using a switch(in Java in this case) I normally use the default
After adding plone.app.async, I cannot start my production instances normally using 'bin/instance start'. However,
I've been using Perforce for a number of years. I'd like to switch to
First post here... I normally develop using PHP and Symfony with Propel and ActionScript
Normally we all do use using System.Linq; and using System.Data.Linq; for example on the

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.