Auto, extern, register, static

Source: Internet
Author: User

1 Auto

2 extern

3 Register

4 static

1 Auto

Auto is used for software engineering specifications, so that the code is clear and understandable, if only for the program can run up, can not add.

Automatic variable int num, equivalent to auto int num;

automatic variable int num=10; equivalent to auto int num=10;

Automatic variables are not declared, only defined.

int num;

int num=10;

2 extern

extern global variables have different definitions and declarations.

A declaration simply declares the existence of a variable, which is defined as the entity of the global variable.

global variable declaration: extern num; equivalent to extern int num;

Global variable definition: extern num=10; equivalent to extern int num=10;

Declarations can be multiple, with only one definition.

1 #define _CRT_SECURE_NO_WARNINGS 2  3 #include <stdio.h> 4 #include <stdlib.h> 5  {     14     System ("pause"); 

Only declarations, not omitting extern, no definition, error:

1 #define_crt_secure_no_warnings2 3#include <stdio.h>4#include <stdlib.h>5 6 extern intData//there is only a declaration, there is no definition, the entity that cannot find data7 extern intdata;8 extern intdata;9 Ten Main () One { Aprintf"%d\n", data); -  -System"Pause"); the}

Severity Code description project file line suppress status
Error LNK2001 unresolved external symbol _datehellof:\users\denggelin\documents\visual Studio 2015\projects\hello\hello\hello.obj1

Only declarations, omitting extern, undefined, normal, system default initialized to 0:

1 #define_crt_secure_no_warnings2 3#include <stdio.h>4#include <stdlib.h>5 6 intData//there is only a declaration, there is no definition, the entity that cannot find data7 intdata;8 intdata;9 Ten Main () One { Aprintf"%d\n", data); -  -System"Pause"); the}

Output Result:

0
Please press any key to continue ...

Only declarations, omitting extern, undefined, array, system default initialized to 0:

1 #define_crt_secure_no_warnings2 3#include <stdio.h>4#include <stdlib.h>5 6 inta[3];7 inta[3];8 inta[3];//all default initialized to 09 Ten Main () One { Aprintf"%d\n", a[0]); -printf"%d\n", a[1]); -printf"%d\n", a[2]); the  -System"Pause"); -}

Output Result:

0
0
0
Please press any key to continue ...

Only declarations, omitting extern, undefined, struct, system default initialized to 0:

1 #define_crt_secure_no_warnings2 3#include <stdio.h>4#include <stdlib.h>5 6 structInfo7 {8     intnum;9     Charstr[ -];Ten }; One  A structinfo info1; - structinfo info1; - structinfo info1; the  -  - Main () - { +printf"%d\n", info1.num); -printf"%s\n", info1.str); +printf"%d\n", info1.num); Aprintf"%s\n", info1.str); at  -System"Pause"); -}

Output Result:

0

0

Please press any key to continue ...

3 Register

Register register variable on CPU, no memory address.

Register variable inside the CPU, fast.

So frequently used variables that need to be placed in registers can increase speed.

VC is automatically optimized, even if no register variable is declared, the VC will be automatically optimized.

GCC is not automatically optimized.

Global variables are best not to occupy registers, which can affect the speed of the program.

Static variables cannot be placed in registers.

register static int num; error

Severity Code description project file line suppress status
Error C2159 more than one storage class was specified hellof:\users\denggelin\documents\visual studio 2015\PROJECTS\HELLO\HELLO\HELLO.C17

4 static

Static statically variable, always exists, the value has not changed, the address has not changed.

Auto Auto variable, function call, it exists, when the function ends, it terminates. Addresses are the same address, but the content changes over and over again.

123

Auto, extern, register, static

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.