其他分享
首页 > 其他分享> > Static、define和单目双目三目操作符

Static、define和单目双目三目操作符

作者:互联网

1.static修饰的叫静态变量。

  1. 修饰局部变量,让局部变量的生命周期变长,出了函数还在。
    #include<stdio.h>
    void ass(int s)
    {
    	int c=1;
    	s+=c;
    	c++;
    	printf("%d\n", c);
    }
    int main()
    {
    	int a = 1,b=1;
    	for (a = 1; a <= 10; a++)
    	{
    		ass(b);
    	}
    	return 0;
    }

    当我们运行这个函数的时候,会发现c的值没有变。

 因为a一出函数体便被缓冲,c的值就不见了。

#include<stdio.h>
void ass(int s)
{
	 static int c=1;
	s+=c;
	c++;
	printf("%d", c);
}
int main()
{
	int a = 1,b=1;
	for (a = 1; a <= 10; a++)
	{
		ass(b);
	}
	return 0;
}

 但加了static后,c的值即使出了函数体也可以存活。 

 2   define

define 常用来定义常量和宏(带参数)。

#define pie 3.14(#define name 常量)定义一个常量值。

#define max(x,y) (x>y?x:y)定义一个函数(#define name(参数,参数·····) (操作内容))。

 3.单目双目三目操作符

单幕操作符:只用一个变量的,如-a,a++,a--等等。

双目操作符:只用二个变量的,如a||b,a+b,a-b,a<=b等等

三目操作符:只用三个变量的,如a<b?a:b中  ? :。

标签:函数,int,static,单目,Static,三目,操作符,define
来源: https://blog.csdn.net/m0_62806610/article/details/121340435