1.C language keyword (auto break case char const Swtich)

Source: Internet
Author: User
Tags array definition

There are 32 keywords in the ANSI C standard C language, namely:

Auto break case Char const continue default does double else enum externfloat for goto if int long register return short SIG Ned sizeof static struct Switchtypedef Union unsigned void volatile while 1) Auto: A keyword that automatically stores variables, that is, declaring a temporary variable memory.
1 Double 3.7;

Indicates that a is a temporary variable that is automatically stored.

2) Break

Break Statement usually used in Looping Statements and the switch statement. When break is used in switch statements , the program can jump out of a switch and execute a switch statement If there is no break statement, theexecution begins at the place where the condition is met (that is, the case that matches the expression in the switch ( expression ) bracket) until Switch structure ends.

When the break statement is used in an do-while, for, and while Loop statement, the program terminates the loop. Instead of executing the statements that follow the loop, the break statement is always associated with the IF statement. That is, it jumps out of the loop when the condition is met.
2 Main ()3 {4 intI=0;5 CharC;6 while(1)/*Set up Loops*/7 {8C=' /';/*variable Assignment initial value*/9 while(c!= -&&c!= -)/*The keyboard receives characters until you press ENTER or the ESC key*/Ten { OneC=getch (); Aprintf"%c\n", c); - } - if(c== -) the Break;/*decide to exit the loop if you press the ESC key*/ -i++; -printf"The No. is%d\n", i); - } +printf"The end"); -}


    • Break statement does not work with conditional statements for If-else
    • In a multilayer loop, a break statement jumps only one layer outward

3) switch and case: As the name implies, switch is the meaning of switching.

In the C language, switch and case are used together, is a judgment selection code, its function is to control the flow of business processes.

The syntax of the switch statement is as follows:

1 Switch(controllingexpression)2 {3  CaseConstantExpression1:4  CaseConstantExpression2:5  CaseConstantExpression3:6statements;/*when meeting ConstantExpression1, ConstantExpression2, ConstantExpression3, any one of them executes statements*/7  Break;8  Caseconstantexpression:9 statements;Ten  Break; One ... A default : - statements; -  Break; the}
The switch statement is useful, but you must be cautious when you use it. Any switch statement that you write must follow these rules:
    • Switch can only be used for basic data types, including int, char, and so on. For other types, you must use the IF statement.
    • The parameter type of switch () cannot be a real type.
    • The case label must be a constant expression (constantexpression), such as 42 or "42".
    • The case label must be an expression of uniqueness, that is, two case is not allowed to have the same value.

4) Char:char is a strange one in the C/s + + integer data, other int/long/short, such as the default is signed when not specified signed/unsigned, but char is unsigned in the standard, the compiler can be implemented as a signed , or it can be implemented as unsigned.

    • Char denotes the character: char ch = ' A ';
    • Char represents the string: char ch[size] = "ABCD";

5) Const: a constant type is a type that uses the const description of a type modifier, and the value of a constant type variable or object cannot be updated. (Of course, we can rescue the update;)

Why is C language introducing const?

A: The initial purpose of the const rollout is to replace the precompiled instruction, eliminate its drawbacks, and inherit its advantages.

The main role of Const?

A: (1) You can define const constants and have immutability.
For example: const int max=100; max++ will produce errors;
(2) Easy to type check, so that the compiler to deal with the content of more understanding, eliminate some hidden dangers.
For example: void f (const int i) {...} The compiler will know that I is a constant and does not allow modification;
(3) can avoid the ambiguity of the number appears, the same can be easily adjusted and modified parameters. As with the definition of a macro, you can do it without changing it.
As in (1), if you want to modify the content of Max, only need: const int max=you want;
(4) Can protect the modified things, prevent accidental modification, enhance the robustness of the program. Or the above example, if I is modified in the function body, the compiler will error;
For example: void f (const int i) {i=10;//error!}
(5) Can save space and avoid unnecessary memory allocation. For example:

  #define  PI 3.14159 //  macro constants  const  double  Pi=3.14159 ; //   double  i=pi; //  Now allocate memory for PI and no longer assign it!  double  i=pi; //   macro substitution during compilation, allocating memory  double  j=pi; //   no memory allocations  double  J=pi; //  and then the macro replacement, memory allocation again! 

Const definition constants from the assembly point of view, just give the corresponding memory address, instead of the immediate number as given in # define, so the const definition of the constant in the program runs only one copy, and # define defines the constants in memory have several copies.
(6) Improved efficiency.
The compiler typically does not allocate storage space for ordinary const constants, but instead saves them in the symbol table, which makes it a constant during compilation, without the storage and read memory operations, making it highly efficient.

Question one, const constants & Variables

Example: Why does the compiler of ANSI C report an error when the following example uses a const variable to initialize an array? const int n = 5;int a[n]; answer and Analysis: 1) This question is about the difference between "constant" and "read-only variable". constants, such as 5, "ABC", and so on, are definitely read-only, because constants are read-only areas that are placed in memory by the compiler, and of course cannot be modified. A "read-only variable" is a place in memory to store its value, except that the value is not allowed to be modified by the compiler. The C keyword const is a modifier (Qualifier) that defines a variable that is not allowed to be changed. In the above code, the variable n is decorated as a read-only variable, but how to modify it is not a constant. While ANSI C specifies that the length of the array definition must be "constant", "read-only variable" is also not possible, "constant" is not equal to "immutable variable". 2) So, what do you use to define constants in ANSI C? The answer is an enum type and a # define macro, both of which can be used to define constants. Question two: How does the const limit content? Example: The following code compiler will report an error, excuse me, which statement is wrong?
 typedef char  * pStr;  char  string  [4 ] =  "   ;  const  char  *p1 = "  string  ; // 1 type  const  pStr p2 = "  string  ; // 2  p1++;p 2  + +; 
Answer and analysis: The problem lies in the p2++. The basic form of Const usage: const type m, and finite m immutable. Replace m in the basic form with the *P1 in 1, replace the const char *P1, the *P1 is immutable, and of course P1 is mutable, so p1++ is right in the problem. Replacing the type in the basic form with the PSTR in 2, substituting the const PSTR m, and the finite m immutable, the PSTR in the title is a new type, so the P2 in the problem is immutable and p2++ is wrong. Question three:const& pointerExample OneWhat is the immutable content of a const qualifier? 1) const in front const int nvalue;//nvalue is Constconst char *pcontent; *pcontent is const, pcontent variable const char* const pcontent; Pcontent and *pcontent are both const2) const in the back, with the declaration above the equivalent int const nvalue; Nvalue is Constchar const * PCONTENT; *pcontent is const, pcontent variable char* const pcontent; Pcontent is a const,*pcontent variable char const* const pcontent; Pcontent and *pcontent are both Const. The answer and analysis: the use of const and pointers is a very common confusion in C language, in the actual development, especially when looking at other people's code, often because of such a bad judgment of the author's intentions, Here's my judgment: const modifies only the variables that follow, and there's no difference between whether a const is placed before a type or a type. For example, const int A and int const A are modifier A is const. Note * is not a type, and if *ptype is a type before, then PType is a simple way to judge a pointer to that type: the pointer operator *, which is from right to left, such as Char const * Pcontent, can be understood as Char const (* pcontent), i.e. * pcontent is const, and pcontent is variable. [1]Example TwoThe int const * P1,P2;P2 is const; (*P1) is a whole, so (*P1) is const, but P1 is mutable. int * P1,p2 only represents P1 as a pointer to an integral type, to indicate that P1, p2 are pointers are to be written as int * p1,* P2. So whether it's a const P1,P2 or a const * P1,P2, the inside is P1.example Threethe int const * Const P1,P2;P2 is const, is the previous const modifier, *P1 is also modified by the previous const, and P1 is modified after a const.example Fourint * Const P1,P2;P1 is const, (* const p1) is whole, so const does not modify P2.Example FiveThe value of the pointer to and from the variable is const on the left side of *, the value of the variable pointed to by the pointer cannot be changed directly by the pointer (can be changed by other means); On the right side of *, the pointer is immutable. Précis-writers is "left fixed, right oriented". 1) The value of the variable pointed to by the pointer cannot be changed, pointing to the variable
int 1 ; int 2 ; Const int* px = &x; int Const // These two expressions have the same effect // correct, allow change to point 3 // error, not allowed to change the value of the variable pointed to by the pointer
2) The value of the variable pointed to by the pointer can be changed, pointing to the immutable
int 1 ; int 2 ; int const px = &// error, not allowed to change pointer pointing 3// correct, Allows changing the value of the variable pointed to by the pointer
3) The value of the variable pointed to by the pointer is immutable, pointing to non-volatile SupplementIn C, for const-defined pointers, do not assign an initial value compilation without error, int* const PX; This definition is not allowed. (The pointer constant is initialized when it is defined) int const *PX; this definition is allowed. (not initialized when the pointer can be redefined) it is strongly recommended that pointers be pointed at initialization time to prevent wild pointers from appearing!

1.C language keyword (auto break case char const Swtich)

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.