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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:04:34+00:00 2026-05-31T12:04:34+00:00

Code: int main() { int a=1; switch(a) { int b=20; case 1: printf(b is

  • 0

Code:

int main()  
{
  int a=1;
  switch(a)
  {
    int b=20;

    case 1:
    printf("b is %d\n",b);
    break;

    default:
    printf("b is %d\n",b);
    break;
  }
  return 0;
}

Output:
It prints some garbage value for b
when does the declaration of b takes place here
Why b is not initialized with 20 here???

  • 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-31T12:04:36+00:00Added an answer on May 31, 2026 at 12:04 pm

    Because memory will be allocated for int b but when the application is run “b = 20” will never be evaluated.

    This is because your switch-statement will jump down to either case 1:1 or default:, skipping the statement in question – thus b will be uninitialized and undefined behavior is invoked.


    The following two questions (with their accepted answers) will be of even further aid in your quest searching for answers:

    • How can a variable be used when it’s definition is bypassed? 2

    • Why can’t variables be declared in a switch statement?


    Turning your compiler warnings/errors to a higher level will hopefully provide you with this information when trying to compile your source.

    Below is what gcc says about the matter;

    foo.cpp:6:10: error: jump to case label [-fpermissive]
    foo.cpp:5:9: error:   crosses initialization of 'int b'
    

    1 since int a will always be 1 (one) it will always jump here.

    2 most relevant out of the two links, answered by me.

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

Sidebar

Related Questions

Consider this code: int main() { int e; prn(e); return 0; } void prn(double
I had this code: int foo(void){ return 1; } int main(void){ static const int
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7,
Consider the following code: int main() { int *p; ++((int){5}); //compile without err/warning &((int){5});
I have the following code int main() { int a=6; void *p; p=&a; p++;
Consider the following code: int main() { int i; volatile int* p = &i;
I have the following code: int main(int argc, char** argv) { onelog a; std::cout
Possible Duplicate: Pointer Arithmetic In C Code: int main() { int a[ ] ={0,1,2,3,4};
I have tried to use the following snippet of code: int main() { string
I got an EXC_BAD_ACCESS in main() , here is my code: int main(int argc,

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.