About the switch statement "think too much"

Source: Internet
Author: User

I am writing code for the project today and find that I want to use the switch statement, which is a bit confusing.

The general switch is like this.

Swith (constant)


Case 1: Statement; break;

Case 2: Statement; break;


Default: break;


Each case is a tag, and so is the Assembly layer.

... Since I have a little understanding of assembly, I have become more "think more.

I want to use switch nesting in the project, and their case will have overlapping areas. Is it feasible? I am sleepy. If the label of the case is the same, it is the same label in the Assembly layer. Will GCC not let it go? (Do not know if I have clearly stated it ...)

For example, can you directly determine whether the following code is 0 warning or 0 wrongs?

#include <stdio.h>#define A (0)#define B (1)int main(){int tmp = 0;while(!scanf("%d",&tmp)){getchar();printf("error! input again!\n");}switch(tmp){case A:switch(tmp){case A: printf("A A\n");break;case B: printf("A B\n");break;}break;case B:switch(tmp){case A: printf("B A\n");break;case B: printf("B B\n");break;}break;default:;}return 0;}

The case of the two-layer switches that I intentionally set is the same. It is confusing whether the switch jumps at the Assembly level to see ambiguity, so that it cannot be compiled.

The truth is no, and there will be no ambiguity. The case label here is given in the form of the function offset, so there will be no ambiguity.

For example, here, the switch function is the main function, and the label gives the index in the form of <main + Offset>.

About the switch statement "think too much"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.