A Fool's essay in C language (ii): global variables, precompilation, Goto

Source: Internet
Author: User
Tags goto processing instruction tag name

  Function: the reuse of code can be Implemented.       function only needs to be defined 1 times, then the code in the function can be arbitrarily called. -the Basic concept of an unknown source

  Learning and time learning, such as the wrong pen, please correct me

one, Goto Jump Statement

Goto is the role of the C language: you can jump the execution of the CPU to the other places in the current function to continue execution. because of the code back, it is easy to create a dead loop.

Attention:

1). label names are arbitrary, but conform to the naming rules and specifications of Identifiers.

Identifier: that is, the programmer can give the name of the thing collectively known as Identifiers.

Variable name, label name, function Name.

2). Goto can jump forward or jump BACKWARDS.

3). you can only jump in the current Function.

4). the code below the tag name cannot be a declared Variable.

If you have to do it, write a 1-sentence code with no Effect.

Examples are as Follows:

loop:printf (""); intUserName =0, password =0; printf ("User Name:"); scanf ("%d",&userName); printf ("password:"); scanf ("%d",&password); //If It's wrong.    if(userName! =123456|| password! =888888)    {        Gotoloop; }

  second, global variables and local variables

1. Variables: The space that is used to store data in Memory.

Local variables: variables defined inside a function, we are called local Variables.

Global variable: A variable that is set outside the Function. we are called Global Variables. directly defined below the source File.

2. The similarities and differences between global variables and local variables.

The same point: all variables are a space in memory to store Data.

Different points:

1). the location of the declaration is Different. Local variables are declared inside a function, and global variables are declared outside the Function.

2). Scope is Different.

A. Local variables. accessible only within the current Function.

B. Global Variables. starting from where the global variable is defined, the global variable can be accessed in all subsequent functions.

In general, global variables are defined at the top of The. #include下面.

3). The default value is Different.

A. Local Variables.

Declares 1 Local variables if no initial value is assigned to the local variable. there is a value in this local variable, and the value is 1 garbage. It's a random number.

B. Global Variables.

DECLARE 1 global variables, and if you do not assign an initial value to the global variable, the initial value of the global variable defaults to 0 automatically initialized to 0.

If the type of the global variable is of type char, and we are not Initialized. then the system will automatically assign a value of 1 ' + ' to this char variable

' \ s ' stands for 1 invisible Characters. The ASCII code for this character is 0.

4). the time to create and recycle is Different.

A. Local Variables.

The CPU declares the statement in memory only when it executes the sentence that declares the local variable.

It is automatically reclaimed when the scope ends.

B. Global Variables.

The program starts by creating a global variable in memory.

At the end of the program, the global variable is not recycled.

3. Characteristics of global Variables.

1). The program will be created when it is started and will not be recycled until the end of the Program.

Local Variables. each call to the function will be re-declared 1. When the function ends, the local variable is Recycled.

The global variable is accessed in that function. the access is the same as 1.

Have the ability of sharing. Declare the data as a global variable. then all functions can be accessed, and the data can be Shared.

2). When do we need to define 1 data as a global variable?

If this data wants to be accessed by multiple functions, then this data can be defined as a global variable.

4. If the function is not called, the code inside the function will not be executed.

5. You can define 1 local variables with the same name as the global Variable.

At this point, the variable is accessed from within the function, starting with the local variable that defines the same name. The access is a local variable.

It is not recommended to write so ~

  third, pre-compilation

1. pre-processing instruction/preprocessing Code.

C language code is divided into two main categories.

1). C Code.

2). Preprocess the Code.

The code that begins with # is called the preprocessing Code.

2. Handwritten 1th C program Steps.

1). write the source code in. c that conforms to the C language Specification.

2). Compile. Use the CC-C directive to compile the source code for the C language into A. o target File.

A. First check that the code in the source file conforms to the syntax specification

YES: the target file is Generated.

NO: Error.

3). link: use CC directives to deliver the target file chain to 1 Executables.

A. Add the startup code for the target File.

4). Execute the executable file.

-----> This is what the compiler does at compile time <-------------

. C source Files---execute The preprocessing code in. c files---check syntax--and compile to. o target File--chain delivered to executable File--execute.

3. pre-processing Instructions

1). Classification of pre-processing Directives.

A. The file contains Instructions. #include

B. Macro definition #define

C. Conditional compilation directives #if

2). Characteristics of pre-processing instructions.

A. All start with the # Number.

B. There is no semicolon after the preprocessing Instruction.

C. At compile time, check the syntax before.

A Fool's essay in C language (ii): global variables, precompilation, Goto

Related Article

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.