11-Dark Horse programmer------C language Learning notes---C language variables scope and storage type

Source: Internet
Author: User

Dark Horse programmer------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Scope and storage type of five variables

The scope of the 01 variable is the area where the variable is valid, and the variable in C is divided into local variables and global variables.

02 Local Variables:

* The scope of a local variable is limited to the code block in which it is defined, and it loses its role in leaving the code block.

* The parameter is valid only within the function, it is also a local variable, and the scope is scoped to the function within which it is defined.

* When the code block is in a nested state, if the inner code block has a variable whose name is the same as the outer code block, the inner variable will mask the outer variable.

//use global variables to write a function to find the highest score, the lowest score and the average score of a class student's grade.#include <stdio.h>Doublemax=0, min= -;//Defining global VariablesDoubleAverageintm);//function DeclarationintMain () {intN; DoubleAve; printf ("Please enter the number of people: \ n");//Enter class sizescanf ("%d", &n);//number of classes acceptedAve= average (n);//calling Functionsprintf ("ave=%f,max=%f,min=%f\n", ave,max,min); }DoubleAverageintm) {    inti; Doubles,ave,sum;  for(i=1; i<=m; i++) {scanf ("%LF",&s); if(s>max) {Max=s; }        if(s<min) {min=s; } Sum= sum+s; } Ave=sum/m; returnAve; }

03 Global Variables

01 variables defined outside the function are called global variables, which are defined outside the function and do not belong to any function or code block, but to the entire source file. Its valid position starts at the point where the variable is defined and ends at the source file.

Dynamic static local variable, which exists throughout the source program, the system automatically assigns a value of 0, you can retain the last run results.

The extern external variable, in the same file, is declared in the function with extern in order for the global variable to be used in the function before the definition point.

When a source program consists of several source files, external variables defined in one source file can be declared using extern in other source files, making them available in other source files.

//write a function to find the area of a circle#include <stdio.h>DoubleAreaDoubleR) {        extern DoubleP//External Declarations    returnP*r*r;//Area Formula    }DoubleP =3.14;//defines the global variable p and initializes it from this scope down to the effectiveintMain () {DoubleR; printf ("Please enter the radius of the circle: \ n"); scanf ("%LF",&R); printf ("area=%f\n", Area (R)); }

11-Dark Horse programmer------C language Learning notes---C language variables scope and storage type

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.