您的位置:首页 > 其它

今日学习——const 变量申明 volatitle static

2016-09-30 13:38 162 查看
const 
//const可定义一个常变量

#include<stdio.h>

intmain()

{

  
const
4000
int num=10;

  
int n=10;

  
//num=20;     //num用const定义其值不能被修改

  
//int arr[num]; //数组[]中的值必须是常数,num是变量,并不是常量,只是num变量被加了限制,不能改值

  
int arr
;    //数组[]中的值必须是常数

  
return 0;

}

(2)//函数变量申明

#include<stdio.h>

inta;

intmain()

{

  

  
printf("%d\n",a);

  
system("pause");

  
return 0;

}

inta=10;

voidfun()

{

  
printf("hehe");

}

// extern
申明外部变量 例如:extern int a;

(3)volatile 
//关键字volatile
作用:保证内存的可见性

#include<stdio.h>

intmain()

{

  
volatile const
int num=10;

  
int *p=num;

  
*p=20;

  
printf("%d\n",num);     
//不加volatile时输出结果为,加了volatile输出结果为(直接在寄存器中读取)

  
system("pause");

  
return 0;

}

(4)static 
静态变量

#include<stdio.h>

static
int num=0;

intmain()

{

  
int i=0;

  
for(i=0; i<10; i++)

  
{

  
 int num=0;

     
num++;

     
printf("%d\n",num);

  
}

  
system("pause");

  
return 0;

}

//输出结果为十个一

//加上static int num=0;后输出结果为2 3 4 5 6 7 8 9 10

//static int num=0;做全局变量时结果为十个一

 

ac85
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: