Standard C Episode 5

Source: Internet
Author: User

Understand the scope and visibility of variable identifiers, and the lifetime of variables.
(1) Scope and visibility of identifiers
Scope: A scope is an area in which an identifier is valid in the source program.
Visibility: A program runs to a point where the identifier that can be referenced is the one that is visible here.


(2) Variable lifetime:
The lifetime of a variable is defined as the time range from creation to destruction. Variables are "static lifetimes" and "dynamic Lifetimes" by lifetime. Static lifetime: If the lifetime of the variable is the same as the run time of the program, the variable is said to have a "static lifetime"; Dynamic lifetime: The variable lifetime starts at the creation of the claim point, ending at the end of the scope.

Note: The source program declares (defines) the variable principle: to meet the needs of the case, shorten the scope of identifiers, shorten the variable lifetime.

1 /*2 * Variable Exercise3 *4 * Understand the scope and visibility of identifiers, and the lifetime of variables. 5 *6 * Scope: The scope is an identifier that is valid in the source program. 7 * Visibility: The identifier that the program runs to a point where it can be referenced is the one that is visible. 8 *9 *Ten * Variable lifetime: The lifetime of a variable refers to the time range from creation to destruction of a variable.  One * A * variables are "static lifetime" and "dynamic lifetime" in terms of lifetime.  - * Static lifetime: If the lifetime of the variable is the same as the run time of the program, then the variable is said to have a "static lifetime"; - * Dynamic lifetime: The variable lifetime starts at the creation of the claim point, ending at the end of the scope.  the */ -  - intValue//for global variables, the compiler will automatically initialize to 0 at compile time -  + voidfunc () - { +     intvalue2 =2;//Local Variables Aprintf"%d\n", value2); at } -  - intMain () - { -Value4 =0;//local variables.  -     {     in         intValue3 =3;//local variables.  -                 //This variable definition statement defines a variable scoped to the end of the current curly brace from its definition statement.  to                 //We call this type of local variable with a "block scope", also known as a "block variable" +printf"%d\n", value3); -printf"%d\n", value4); the     } * //func (); $ //printf ("%d\n", value2);//compiler error:value2 is not definedPanax Notoginseng //printf ("%d\n", value3);//compiler error:value3 is not defined.  - return 0 the}

Variable type modifier (type-specifier)
The Auto keyword can be used to declare variables, and all local variables are automatically auto, so there is no need to specifically declare them in the source program.

The static keyword can be used to declare statically variable. The scope of a static global variable is reduced to all statements within the file that declares it. A static local variable is created at the beginning of the program until the end of the program is destroyed, and its lifetime is lengthened to the entire program run time.
The/*static type modifier modifies the lifetime of a variable by modifying the local variable, but the scope and visibility of the identifier in the source program cannot be changed.
The *static type modifier can be implemented to alter the scope of a variable when it is decorated with a global variable. */

The Register keyword can be used to declare a register variable.

The volatile keyword can declare volatile variables.

The Const keyword can be used to declare immutable variables.

Address data and pointers in the language
Pointer variables are variables that are used to hold address data.
The pointer variable declaration syntax is as follows:
An asterisk before an identifier in an INT *p_value;//declaration statement indicates that P_value is a pointer type.
int *p_value1, value1, *p_value2, temp, integer;//the representation of the identity Fu number in this declaration statement is a pointer type, and the int type is not an asterisk. That is p_value1, the type of p_value2 is int*, value1, temp, the type of integer is int

Two most basic address arithmetic operators & and *. where * is the indirect access operator,& is the accessor operator. They have a very high priority for operations.

Example exercises:

1 /*2 * Stopwatch Practice3 * Learn about library functions sleep () time ()4 *5 */6 7#include <stdio.h>8#include <time.h>9#include <unistd.h>Ten  One intMain () A { -     intStart = Time (0);//Time (0) The return value is the number of seconds between today and January 1, 1970 0:0 0 seconds (0 o'clock area) -      while(1) { theprintf"%d\r", Time (0)-start);//Note that when printf prints the input character stream, the first thing is to put the output characters into the output buffer and print to the screen when there are line breaks in the output buffer . -Fflush (stdout);//print a stream of characters in the output buffer immediately -Sleep1);//Library function Sleep Implementation let the current program sleep for one second -     } +     return 0; -}

1 /*2 * Const Type-specifier Exercise3  *4  * */5 6#include <stdio.h>7 8 intMain ()9 {Ten     Const intValue =0; Oneprintf"value is%d\n", value); A     //value = 3;//compile error, const type modifier can only put data at initialization time, once initialization is complete, no more data can be placed. This means that the variable value no longer changes.  -     //value++; -printf"value is%d\n", value); the  -     return 0; -}

1 /*2 * Static local variable exercise3  * */4 5#include <stdio.h>6 7 voidf ()8 {9     //int value = 0;Ten     StaticValue =0;//static local variables are created at the beginning of the program, and are not destroyed until the end of the entire program runs. Static local variables have a static lifetime.  One     /*The static type modifier can implement changing the lifetime of a variable when it modifies a local variable, but it cannot change the scope and visibility of the identifier in the source program.  A the *static type modifier can be implemented to alter the scope of a variable when it is decorated with a global variable. */ -printf"the value in F () is%d\n", value); -value++; the } -  - intMain () - { + f (); - f (); +     return 0; A}

1 /*2 * function returns pointer type data3  *4  *5 * Pointer variables can be used as function return values, only the address of the global variable, the address of the static local variable, or the address obtained through the parameter as the return value. It is very dangerous to use the address of ordinary variable as the return value because of the lifetime problem of ordinary local variable, so never say the address of ordinary local variable is used as the return value. 6  *7  * */8#include <stdio.h>9 int*func ()Ten { One     Static intValue =0; Aprintf"value is%d\n", value); -  -     return&value; the } -  - intMain () - { +     int*p_value =NULL; -P_value =func (); +printf"P_value = func (); *p_value is%d\n", *p_value); A*p_value =7; at func (); -printf"*p_value is%d\n .", *p_value); -     return 0; -}

Standard C Episode 5

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.