"Dark Horse programmer" Local variables and global variables (C language)

Source: Internet
Author: User

depending on the scope of the variable, it can be divided intoLocal Variables:

Definition: A variable defined inside a function or code block

Scope: Start with the line that defines the variable until the end of the code block

Declaration period: Allocates storage space from the line that defines the face, which is recycled after the code block is finished

No fixed initial value

Global Variables:

Definition: A variable defined outside a function

Scope: Starts from the line that defines the variable, until the end of the file (all functions can be shared)

Declaration period: Allocates storage space from the row that defines the variable and is destroyed when the program exits

The default disposition is 0.

Code Exercise:
#include <stdio.h>    int age;                        Define global Variables Agevoid test () {Age  = 10;} int main () {  printf ("Age =%d\n", age);       Output age = 0  test ();  printf ("Age =%d\n", age);       Output Age = 10  

The first output is 0, because there is no age variable within the main function and age outside the main function is a global variable, and it is not initialized, so the default value of 0 is maintained.

The second output is ten; the test function finishes executing the age variable and is destroyed, and the global variable is accessed here

"Dark Horse programmer" Local variables and global variables (C language)

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.