Attribute keyword in C language

 

C language variables can have their own attributes, in the definition of variables can be combined with property keywords, keyword attribute specifies the variable has a special meaning. C language with auto, register, static, const, extern keyword and other properties.

auto

auto is the default property of local variables in C language, auto show will be modified variables are stored on the stack, the compiler default all local variables are auto, so the following statement i and j variables are the same.

int i; // 局部变量默认属性为auto
auto int j; // 显示声明为auto属性

 

register

register keyword requests to local variables stored in the register, just request it, the request may not be successful, must register variable CPU register acceptable value. No address register, the register can not be modified to take the address of a local variable operation. CPU registers are most scarce resource, and the life cycle of global variables is run from the end of program execution, it is not the global variable in a register, C language also provides that can not register modification global variables.

#include <stdio.h>

register int i; // error: register name not specified for ‘i’

int main(void) {
	register char c = 'a';
	printf("&c = %x\n", &c); // error: address of register variable ‘c’ requested
	return 0;
}

static

The static keyword indicates variables static properties, static modification of local variables are stored in the static area of ​​the program that is modified static local variable scope, although still within the block, but its lifecycle and has the same global variables .

The static keyword has the sense of "scope qualifier" at the same time.

static global variable modified scope exists in the file declared.

static modification of the scope of the function is declared in the document.

#include <stdio.h>

int g_v; // 全局变量,在程序的任何地方都能访问
static int g_vs; // 静态全局变量,只能在当前文件中访问

int main(void) {
	int var; // 局部变量,在栈上分配空间
	static int svar; // 静态局部变量,在静态数据区分配空间
	return 0;
}
#include <stdio.h>

int fun1() {
	int r = 0;
	r++;
	return r;
}


int fun2() {
	static int r = 0;
	r++;
	return r;
}

int main(void) {
	int i = 0;
	int j = 0;

	printf("&i = 0x%d\n", &i);
	printf("&j = 0x%d\n", &j);

	for(i; i<5; i++) {
		printf("%d  %d\n", fun1(), fun2());
	}

	return 0;
}

 operation result:

&i = 0x807994436
&j = 0x6295624
1  1
1  2
1  3
1  4
1  5

 i and j addresses big difference, because the static variables stored in a static variable region, while the presence of a local variable stack area common.

external

The role of a

extern modified variable or function that tells the compiler that the variable or function is defined elsewhere in the link back to find when the variable or function exists.

#include <stdio.h>

extern int g_v;
extern int getV();
int main(void) {
	printf("g_v = %d\n", g_v);
	printf("getV() = %d\n", getV());
	return 0;
}

int g_v; // 这个变量也可以放在其他文件中,如果g_v真的没有定义,那么在链接阶段会报hallo.c:(.text+0x6): undefined reference to `g_v'的错误

int getV() {
	return g_v;
}

The role of two

In some C ++ libraries, you'll often see the use of the following extern.

#ifdef __cplusplus
extern "C" {
#endif
 
/*...*/
 
#ifdef __cplusplus
}
#endif

Due to the different C and C ++ compiler and linker of rules, such as sign C program function is the function name, and C ++ due to the heavy load of presence, a symbol of the function name and parameter type functions are related, in C and C ++ mixed programming program, will result in the phenomenon can not link the C libraries of C ++. extern "C" {} tells the compiler to compile and link variables declared in the standard C function {}, so as to solve the problem C / C ++ mixed programming.

extern "C" {
    int fun(int a, int b) {
        return a + b;
    }
}

extern "C++" {
    int fun(int a, int b) {
        return a + b;
    }
}

Published 28 original articles · won praise 9 · views 5602

Guess you like

Origin blog.csdn.net/rookiegan/article/details/104886158