CrazyEngineers
  • Working of switch construct in c programming

    radha gogia

    Member

    Updated: Oct 26, 2024
    Views: 1.3K
    In a switch construct ,why does the compiler doesn't show any error and skips the statements written within the cases like for example I write printf("hello"); in between case 1 and case 2 ,so why is there no error in this ,and how come the compiler skips this step
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • radha gogia

    MemberFeb 20, 2015

    For example I write
    case 1: int a=9; break;
    printf("hellooo");
    case 2 : int y =98;
    printf("kill");
    break;
    So here when parsing is done so either the compiler should throw any error because of printf statement or it must print the statement which is written between the two cases but it skips it ,so why does it happen?
    Are you sure? This action cannot be undone.
    Cancel
  • shiva6

    MemberFeb 22, 2015

    Because the compiler never reaches that printf statement. When it encountered the break statement, it just skips all the remaining lines of code in that switch block and goes to the next immediate line after the switch block. That is how the break statement works.
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberFeb 24, 2015

    You have simply broken the construct here:

    case 1: int a=9; break;

    break statement breaks the switch construct thereon and comes out of that construct
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register