How to define variables in a switch case (without curly braces)

Source: Internet
Author: User

This weekend has been fortunate to see a few grok friends (every day/Didi/Tintin), we pulled together some of the no, very open sen!

I have a question every day, in C language, how to define a variable in the switch case? Requirements are not curly braces .

The problem is the background is that the following code is compiled, because the variable cannot be defined directly after the case.

Switch (v) {case 1:int A;      Get_value_of_xx (&a);      Set_yy (a);   Break   Case 2://do_sth break; Defalut:break;}

Every day asked me to use what special moves can resolve this compilation error? (curly braces do not count)

The answer is actually to

Case 1:

Change into

Case 1:;

Do you think it's interesting to add a semicolon? is not the best of his sister? (Anyway I was)

Every day I told a question I had never realized before, caseand Defalt are actually labels , so I suddenly understand the Duff device works (although the old long-known duff equipment, but not enlightened)

There is an introduction to the DUFF device on Wikipedia http://zh.wikipedia.org/wiki/%E8%BE%BE%E5%A4%AB%E8%AE%BE%E5%A4%87

Send (to, from, count) registers short *to, *from;register count; {Register N = (count + 7)/8;switch (count% 8) {case 0:do {*to = *from++;case 7:*to = *from++;case 6:*to = *from++;case 5 :* to = *from++;case 4:*to = *from++;case 3:*to = *from++;case 2:*to = *from++;case 1:*to = *from++;} while (--n > 0);}}


I took the C99 standard to learn a bit, the Backus paradigm of the label:

(6.8.1) Labeled-statement:

Identifier:statement

Case Constant-expression:statement

default : Statemen

As is said every day, both case and default are tags.

So what is the Backus paradigm of statement?

(6.8) Statement:

Labeled-statement

Compound-statement

Expression-statement

Selection-statement

Iteration-statement

Jump-statement

There's almost anything behind the tag, but it's not declaration expression (the one that defines the variable)

In other words, this can

Case 1:{int A;}

But you can't do that.

Case 1:int A;

because the above notation does not conform to the label. Backus Paradigm, compiler error when doing grammar analysis

But once written

Case 1:; int A;

The compilation is passed, because the semicolon, as an empty statement, fully conforms to the Backus paradigm of the label


This article only said 1/3, as to why the tag behind (immediately thereafter) can not define variables? what about C + +? let the reader think and experiment on their own, I am only responsible for throwing bricks.

Today the winter solstice, too sleepy, do not write.


How to define variables in a switch case (without curly braces)

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.