今日学习——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
//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
相关文章推荐
- [C++学习] 对const变量的理解.
- 我的C++学习日记——关于const数值变量,指针和引用的理解
- C/C++中关于地址、指针和引用变量的学习笔记(六) : const和void
- c++学习之const成员变量与成员函数
- ES6语法学习阶段一:let和const 变量声明
- ECMAScript6学习笔记 ——let、const、变量解构赋值
- opencv学习(四)头文件申明/main函数格式定义/程序(变量)命名规则/printf函数
- c++学习之const成员变量与成员函数
- es6学习篇之 let和const命令 、变量的解构赋值
- ECMAScript 6学习笔记(一)let、const、变量的结构赋值
- javascript 三种变量申明方式var、let、const
- javascript 三种变量申明方式var、let、const
- c++学习之const成员变量与成员函数
- 4000 黑马程序员—C学习笔记—static和const修饰的全局与局部变量
- C++ 学习笔记(2)变量和基本类型(复合类型:引用、指针)、const、constexpr、typedef(using)、auto、decltype
- Chap 4 学习笔记-使用C#存储变量数据
- 今日学习 (使用.NET Remoting 建立分布式应用程序(二))(4月7日) ——对象生存周期管理
- Chap 4 学习笔记-使用C#存储变量数据
- C/C++变量申明
- Php 中全局变量在函数中使用时必须申明为全局