C Language Base variables

Source: Internet
Author: User

1. Basic knowledge
1.1 Definition:
Variable type variable name;

int x;
1.2 Assigning values
x = 100;

1.3 Output
printf ("%d", x);

Common format characters:
Integral type:%d,%i
Floating point type:%f
Character Type:%c

2. Scope and life cycle

2.1 Local Variables:
2.1.1 Variables defined inside a function (code block) (including formal parameters of the function)
2.1.2 Scope: Start with the line of code that defines the variable until the end of the code block
2.1.3 Life cycle: code block end will be recycled
2.1.4 Initial value Unknown

2.2 Global Variables
2.2.1 Variables defined outside the function
2.2.2 Scope: Starts from the line that defines the variable, until the end of the file (all functions are shared later)
2.2.3 life cycle: When a program starts, it allocates storage space and is destroyed when the program exits.
2.2.4 Default initial value is 0

1#include <stdio.h>2 3 //a,b,c Global Variables4 intA =Ten;//variable A initial value is ten5 intB, C = -;//The initial value of variable B is 0, and the initial value of variable C is6 7 intSumintV1,intV2)//function parameters, local variables8 {9     returnV1 +v2;Ten } One voidTest () A { -     inti =0;//Local Variables -b++; thei++; -printf"B =%d, I =%d\n", B, i); - } -  + intMain () - { +     intE//Local Variables ATest ();//B = 1, i = 1 atTest ();//B = 2, i = 1 -Test ();//B = 3, i = 1 -  -E =Ten; -     { -         { in             intf = -;//Local Variables -         } to     } +     return 0; -}

3. Memory Analysis of variables
3.1 Memory addressing from large to small, preferentially allocating memory addresses to larger bytes to the variable
3.2 variables are defined first, the larger the memory address
3.3 Get the address of the variable:& variable name
3.4 Output Address:%p
3.5 to initialize before you can use the

1 intA =Ten;2 intb = -; 3 intC;4 intD;5printf"address of a%p\n", &a);6printf"address of B%p\n", &b);7printf"C's Address%p\n", &c);//can get address8 9D = C +1;//can't use

C Language Base variables

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.