C Language Guide (ii) basic article

Source: Internet
Author: User
Tags arithmetic operators logical operators

Variable

    • What is a variable?

A variable is the name of the program's operable store, which is usually memory, and the variable is the identity of a piece of space in memory. A variable has a type, and the type of the variable determines the size of the memory space.

The name of a variable: a sequence consisting of letters, numbers, underscores ("_"), the first character must be a letter or an underscore.

    • Basic types of variables
Char                        -character            int                         integer            float                       single-precision floating                      -point double                      double-precision floating-point

Use sizeof to view the footprint of a storage size

int Main () {    int  i;        printf (" character-based storage space:%d bytes \ n"sizeof(char));    printf (" integer occupies storage space:%d bytes \ n"sizeof(i));     return 0 ;}

  

    • Declaration of a variable

All variables in the C language must be declared before they are used. You can declare more than one variable at a time, or you can split the declaration separately, and the declaration can also be initialized.

int A, b, c;int d = 10;char f;

  

Global variables and local variables

    • Global variables

Global variables are also called external variables, which are defined outside the body of the function.

Global variables are also divided into external global variables, static global variables.

Global variables that use the static storage type can only be used in the source program files that are defined

Global variables that use the extern storage type can be used not only in the source program files that are defined, but also by functions in other source files.

int  - ; void print () {    printf (" global variable a =%d \ n", a);} int Main () {    print ();         return 0 ;}
example-1

If the print function is defined in front of the global variable, you need to add an extern int A, otherwise you will get an error, as described below.

void print () {    externint  A;    printf (" global variable a =%d \ n", a);} int  - ; int Main () {    print ();         return 0 ;}
example-2

    • Local variables

A local variable, defined in a function, is also a local variable.

Note that the scope of a local variable is only within the function that defines the variable, such as a global variable and a local variable with the same name, and the local variable overrides the global variable within the scope of the local variable (the function).

int  - ; int Main () {    int3;    printf ("A has a value of%d \ n", a    ); return 0 ;}

Life cycle and scope

The so-called life cycle refers to the amount of time that a variable occupies memory or registers. Depending on the storage type of the variable, at compile time, the variable will be present in either the dynamic store or the static store, and its lifetime is determined by the type of storage at the time of the Declaration.

Static storage: Storing global variables and static variables, allocating storage space before executing programs, occupying a fixed storage unit

Dynamic storage: A local variable in a function, a function's return value, a parameter, etc., which are dynamically allocated during the execution of the function and automatically released when the function is finished. Because of this mechanism, the same function is called before and after, and the address to which the temporary variable is assigned may be different.

Storage type

Auto

A local variable of a non-static variable is an auto variable. Variables that describe the storage type are not atuo within the function and are considered auto types.

Register

Register storage types are used to define local variables stored in registers , and register storage types are used to improve execution efficiency.

Note: Because the number of registers is limited, register cannot be used extensively. Even with register, it does not mean that variables will be stored in registers, depending on hardware and implementation limitations.

Static

A static (static) variable whose lifetime is from the start of the program to the end of the program run, which belongs to the quiescent store.

A static global variable, which can only be used in a defined source program file, and cannot be called from a function in another source program file.

A static local variable whose lifetime is from the beginning of the program to the end of the program's run, but its scope is only within the function that defines it.

A static local variable is initialized when its function is executed, and then when the function is executed, the static local variable is no longer initialized, but the last value is preserved.

extern

The external storage type, the extern type global variable, can be used not only in the defined source program files, but also in other source file functions. If the global variable does not specify a storage type, the extern type is the default.

  

Constant

Constants are fixed values that do not change during program execution.

  

Arithmetic operators

+ Plus-minus * multiply/remove% gain + + + self-increment-self-subtract

Note: self-increment and self-reduction, a++ means that the first operation after the 1,++a represents the first self-added after operation, self-reduction of the same. Monocular operation priority is greater than binocular operation.

 

Relational operators

> greater than < less than >= equals <= less than equals = = equals = = does not equal

logical operators

&&| |!

  

Assignment operators

=

Assign the value on the right to the left

i = i + 3 equivalent to i + = 3 arithmetic operators can play this way

 

Priority level

Simply Remember

! > Arithmetic operators > Relational operators > Logical operators (! except) > Assignment operators

  

PS: Focus on the former part O (∩_∩) o~~

C Language Guide (ii) basic article

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.