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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:52:49+00:00 2026-05-30T05:52:49+00:00

How are statements that come before any case labelled statement in a switch-case block

  • 0

How are statements that come before any case labelled statement in a switch-case block treated.
Please explain the behavior of the following programs

prog1:

#include<stdio.h>
int main()
{
  switch(1)
  {   
    int i=0; 
    case 1:printf("%d",i);         
  }
getchar();
return 0;
}

Output: garbage value.

prog2:

#include<stdio.h>
int main()
{
  switch(1)
  {  
    printf("Inside Switch");  
    case 1:printf("Case 1\n");      
  }
  printf("Outside Switch");
  getchar();
  return 0;
}

Output:

Case 1

Outside Switch.

The statements before a case labelled statement seem unreachable according to program 2 but then why don’t i get an error for an undeclared variable i in the first program (only a warning).

Would be really helpful if someone could explain in detail that how the switch statement is treated internally.

  • 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-30T05:52:51+00:00Added an answer on May 30, 2026 at 5:52 am

    This is best explained by quotations from the c standard.
    I am quoting the relevant parts from the standard which apply to your question here.

    6.8.4.2 The switch statement

    Para 4:

    A switch statement causes control to jump to, into, or past the statement that is the
    switch body, depending on the value of a controlling expression, and on the presence of a
    default label and the values of any case labels on or in the switch body……

    Para 2:

    If a switch statement has an associated case or default label within the scope of an
    identifier with a variably modified type, the entire switch statement shall be within the
    scope of that identifier.154)

    FootNote:

    154) That is, the declaration either precedes the switch statement, or it follows the last case or default label associated with the switch that is in the block containing the declaration.

    Para 7:
    EXAMPLE In the artificial program fragment

    switch (expr)
    {
        int i = 4;
        f(i);
        case 0:
           i = 17;
           /* falls through into default code */
        default:
           printf("%d\n", i);
    }
    

    the object whose identifier is i exists with automatic storage duration (within the block) but is never initialized, and thus if the controlling expression has a nonzero value, the call to the printf function will access an indeterminate value. Similarly, the call to the function f cannot be reached.


    The above mentioned applies to both of the code examples in the Question.
    Example 1, i has an Indeterminate value since it was never initialized & hence prints garbage, While in
    Example 2, printf call is not reached because the control jumps to the matching case label.

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

Sidebar

Related Questions

I'm learning about DDD, and have come across the statement that value-objects should be
I've got the following deadlock graph that describes two sql statements that are deadlocking
I'm trying to write a simple decorator that logs a given statement before calling
I have some sql statements that calculates some numbers. It's possible (with bonus points/penalty
I have two LINQ statements that I would like to make into one, but
I have these long statements that I will refer to as x,y etc. here.
In a project of mine the SQL statements that are executed against a SQL
I'm trying to find SQL statements that timed out using the SQL 2005 profiler.
I want to execute some mysql statements that are stored in a text file
I basically have 7 select statements that I need to have the results output

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.