Wednesday, December 29, 2010

【School】 SCM weak c language tutorial --- Lesson: Statement (2) - compound statement】.

<br> have a friend on the BBS {} asked me what does this mean? .What role? .In C, there is a lot of brackets, such as {},[],() etc., really does make a number of friends they first puzzled. .VB and some other languages in the same () of the role will be different, it can be used in combination to form a number of statement blocks can be used as array indices, etc., and in the C division of the more obvious in the brackets, {} .No statement is used to combine several to form a block, this statement by the combination of a number of statements called compound statement. .Compound statement {} separated by between, and within it the statement or need a semicolon ";" end. .Nested compound statement is allowed, but also is in {} {} is in the compound statement. .Compound statement in the program is running, {} is the line followed by a single statement is executed sequentially. .The C language can be considered as a single compound statement statement that is syntactically equivalent to a single statement. .For a function, the function body is a compound statement, and maybe we will therefore know the compound statement can be used not only executable statement in the form, you can also use the variable definition statements. .Note that in the compound statement defined variables, known as local variables, the so-called local variable refers to its range only in the compound statement, the function can be considered a compound statement, so the function is defined within the range of variables only .inside the function. .Local variables and global variables on the specific use will be specified in the said function. .The following simple example with a brief description of the compound statement and the use of local variables. .# Include <at89x51.h> <BR> # include <stdio.h> void main (void) <BR> {<BR> unsigned int a, b, c, d; / / This defines the main function in the .in? SCON = 0x50; / / serial port mode 1, to allow reception of <BR> TMOD = 0x20; / / Timer 1 Timer mode 2 <BR> TH1 = 0xE8; / / 11.0592MHz 1200 baud <BR> TL1 = 0xE8 .; <BR> TI = 1; <BR> TR1 = 1; / / start the timer a = 5; <BR> b = 6; <BR> c = 7; <BR> d = 8; / / This will .entire function effectively <BR> printf ("0:% d,% d,% d,% d \ n", a, b, c, d); <BR> {/ / Compound statements 1 <BR> unsigned int a ., e; / / 1 is only valid in the compound statement <BR> a = 10, e = 100; <BR> printf ("1:% d,% d,% d,% d,% d \ n", a ., b, c, d, e); <BR> {/ / Compound statements 2 <BR> unsigned int b, f; / / 2 is only valid in the compound statement <BR> b = 11, f = 200; <BR .> printf ("2:% d,% d,% d,% d,% d,% d \ n", a, b, c, d, e, f); <BR>} / / end of compound statement 2 .<BR> printf ("1:% d,% d,% d,% d,% d \ n", a, b, c, d, e); <BR>} / / end of compound statement <BR> 1 .printf ("0:% d,% d,% d,% d \ n", a, b, c, d); while (1); <BR>} operating results: <BR> 0:5,6, .7,8 <BR> 1: 10,6,7,8,100 <BR> 2: 10,11,7,8,100,200 <BR> 1: 10,6,7,8,100 <BR> 0:5,6,7, .8 combines the results of the above description consider why this is so..

No comments:

Post a Comment