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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:45:03+00:00 2026-06-16T06:45:03+00:00

May be possible duplicate but couldn’t have found the same. Suppose I have following

  • 0

May be possible duplicate but couldn’t have found the same.

Suppose I have following C code :

int a;
printf("Enter number :");
scanf("%d",&a);  // suppose entered only an integer
                // ignoring return value of scanf()

I got a case to check whether a is zero or non-zero.

if(a)
  printf("%d is non-zero",a);
else
  printf("%d is zero",a);

Everything is fine using if-else and I also know the other variations of if-else to achieve this . But problem comes with the switch-case as it says that we can implement everything in switch-case which we can do in if-else. But the following code fails.

switch(a)
{
 case a:
       printf("%d is non-zero",a);
       break;
 default:
       printf("%d is zero",a);
       break;
}

Also I know to reverse the case in the above code like this below will work and I will have my answer.

switch(a)
{
case 0:
    printf("%d is zero",a);
    break;
default :
    printf("%d is non-zero",a);
    break;
}

But the question is, Why ? Why if(a) is valid while case a: is not ? Is switch-case a compile time operation and if() run-time ?

  • 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-16T06:45:04+00:00Added an answer on June 16, 2026 at 6:45 am

    The reason is that switch cases can be implemented as jump tables (typically using unconditional branch instructions). So they have to be resolved at compile time.

    This makes them faster than ifs so it is better to use them when possible.

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

Sidebar

Related Questions

Hiii all, this may be a possible duplicate but I couldn't find solution for
It is quite possible that I may not have got the point, but I
Possible Duplicate: Sonar violation: Method may fail to close stream on exception I have
Possible Duplicate: What Advantages of Extension Methods have you found? All right, first of
Possible Duplicate: strange while statement behaviour? Given the following, how may I implement list
Possible Duplicate: Foreign key constraint may cause cycles or multiple cascade paths? I have
Possible Duplicate: C++ variable types limits I have a defined type that may not
I think this may be a duplicate question. But couldn't find the answer for
Possible Duplicate: Jquery event chaining Traditionally we may write: $(selector).click(function () { }); but
Possible Duplicate: PHP file cannot enter some part of code I am building a

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.