Variable Scope 9

Source: Internet
Author: User

Variable:
Local variable: all the variables above are local variables: the variables defined in the function body are called local variables.
The parameter is also a local variable:

The only difference is that the form parameter is initialized with the value of the real parameter, while common local variables need
Hands-on assignment.


Variable declaration cycle: starts from the place where the variable is defined to the end of the function.
Scope (access scope): defines the variable and ends with the function.

Automatic Storage Period


Static Modifier
Static int A = 0;
Static local variables:
Life cycle: entire program
Scope: the access scope is the same as that of common variables.
Feature: After the function is executed, its values are not released.


Global variables:
Variables defined in the entire program:
Lifecycle: Within the lifecycle of a program.
Scope: the entire program scope can be accessed.
Difference: 1. The value of a local variable is a random number without initialization.
2. If the global variable is not initialized, the default value is zero.


1. Global variables may cause name confusion.
2. The global variables waste unnecessary memory space and increase the number of executable programs.
3. It is contrary to high cohesion and low coupling .?
We recommend that you do not use global variables.
Exercise: global variables are used to allocate a space.

Stack. (INT stack [20]; int index)
1. Push ();
2. export data to the stack: Pop ();
3. view the top element of the stack: peak ();
4. Check full (); empty ();
5.

4/block variables: variables defined in the statement block. {...}
Life cycle: defines the place where the variable ends at the end of the block.
Disappear.
Scope: block or statement block.
For ()
{
Int I = 0;
}
If ()
{
Int n = 10; Block Variable

}

Summary:
Header file commands
# Include stdio. H
Global Variables
Int data;
Custom function declaration
Int fun (void );

Int main ()
{
Int DATA = 2;
// Local variable
For ()
}
Custom Functions
Int fun (void ){
Int F; // local variable
}
Local priority principle of variables:
If the global variable is the same as the local variable or the Fast Variable.
Block variables take precedence over block variables within the block variables access range.
First, the rest are global variables. Scope rules.
Exercise:

1. Guess the number of games:
User input: 70 is too large
50 is too big
15 is too small
25 Congratulations! You guessed it.
Srand (time (0 ));
Rand (); // shengcheng yige suijishu

2. Clock
Displays the current computer time dynamically.
15:28:30 changes every second
Unsigned int sleep (unsigned int seconds) unistd. H
Sleep the current process for seconds (inaccurate)
Time ()
Time_time time (time_t * t );
Time_t size_t // define size_t
Time_t t = time (0); // t Jiu shi 1970 1 1 0 0 0 till now
Time
Int t = time (0 );
Time
T % 60
TB % 3600/60
T/3600% 24

 

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.