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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:56:16+00:00 2026-06-14T13:56:16+00:00

Why does this code fragment run fine void foo(int i) { switch(i) { case

  • 0

Why does this code fragment run fine

void foo(int i)
{
    switch(i) {
    case 1:
    {
        X x1;
        break;
    }
    case 2:
        X x2;
        break;
    }
}

whereas the following gives compilation error (initialization of ‘x1’ is skipped by ‘case’ label)?

void foo(int i)
{
    switch(i) {
    case 1:
        X x1;
        break;
    case 2:
        X x2;
        break;
    }
}

I understand that using braces introduces a new scope, hence storage will not be allocated for x1 till we hit its opening brace. But x2 is still initialized inside a case label without enclosing braces. Should this not be an error too?

I think initialization of x2 can be conditionally skipped in both the code fragments

  • 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-06-14T13:56:17+00:00Added an answer on June 14, 2026 at 1:56 pm

    1: Valid

     case 1:
            {
            X x1;
            break;
            }
    

    If it doesn’t hit the condition, x1 can’t be used by any further statements, so there can’t be a runtime error with this. x1 doesn’t try to exist outside braces.


    2: Invalid

     switch(i) {
        case 1:
            X x1; //don't break
            i = 2;
            ...
            ...
            ...
         case 2:
            x1.someOperation()
    
     }
    

    In the above, if i was 2 initially, you’d hit x1.someOperation() before X x1 which would construct the object.

    If it was allowed to compile, it would throw a runtime error or not, depending upon whether the case:1 was executed before 2, (and the object was constructed). Hence, it is disallowed by the compiler.


    The same is allowed with Plain Old Data types which cannot have a user-defined constructor.

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

Sidebar

Related Questions

What exactly does this code fragment do? #include <stdio.h> List *makeList(int n) { List
I have the following java code fragment while (condition1){ switch (someinteger){ case 1: if(condition2)
Does this code cause a memory leak: int main(){ int * a = new
Why does this code int (*g)(int); int (*h)(char); h = g; In C, give
The following code fragment does nothing, but illustrates the problem. It was extracted from
I have seen the following code in a website... what does this mean?.Can i
Is this code fragment OK or does it result in undefined behavior? std::string s;
I have the following code fragment that I use to scale images. This is
The following fragment of CL code does not work as I expected with CCL
Does this code always evaluate to false? Both variables are two's complement signed ints.

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.