《OOC》笔记(1)——C语言const、static和extern的用法

C语言中const关键字用法不少,我只喜欢两种用法。一是用于修饰函数形参,二是用于修饰全局变量和局部变量。

用const修饰的函数形参

直接修饰

一个形如

int Minus(const int a, const int b, int testCase);

的函数,const的意义是什么呢?

答:参数a被const修饰,说明在Minus函数内,编译器不允许a被别的变量(例如x)赋值(修改)。参数b同理。

如果你写了a = x; 这样的语句,编译器会警告"warning: assignment of read-only location",即"警告:赋值到只读位置"。

 int Minus(const int a, const int b, int testCase)
{
int x, y;
switch (testCase)
{
case :
a = x; //warning: assignment of read-only location
b = y; //warning: assignment of read-only location
break;
case :
a = (const int)x; //warning: assignment of read-only location
b = (const int)y; //warning: assignment of read-only location
break;
case :
x = a; //OK with compiler.
y = b; //OK with compiler.
break;
case :
x = (int)a; //OK with compiler.
y = (int)b; //OK with compiler.
break;
default:
break;
}
int result = a - b;
return result;

指针修饰

请原谅这个不靠谱的的叫法吧。总之,一个形如

int Add(const int * a, const int * b, int testCase);

的函数,const的意义是什么呢?

答:参数a是指向int的指针,a被const修饰,说明在Add函数内,a指向的内容不能被赋值(修改)。如果将a赋值给另一个int*类型的指针x,那么就可以通过x修改a指向的内容。这违反了const的作用。因此,编译器禁止将a赋值给别的变量。参数b同理。

如果你写了x = a; 这样的语句,编译器会警告"warning: assignment discards qualifiers from pointer target type",即"警告:赋值无视目标指针的修饰符"。

 int Add(const int * a, const int * b, int testCase)
{
int * x;
int * y;
switch (testCase)
{
case :
a = x; //OK with compiler.
b = y; //OK with compiler.
break;
case :
a = (const int *)x; //OK with compiler.
b = (const int *)y; //OK with compiler.
break;
case :
x = a; //warning: assignment discards qualifiers from pointer target type
y = b; //warning: assignment discards qualifiers from pointer target type
break;
case :
x = (int *)a; //OK with compiler, but const fade out.
y = (int *)b; //OK with compiler, but const fade out.
break;
case :
*a = *x; //warning: assignment of read-only location
*b = *y; //warning: assignment of read-only location
case :
*x = *a; //OK with compiler, but const fade out.
*y = *b; //OK with compiler, but const fade out.
default:
break;
}
int result = *a + *b;
return result;
}

总结以上两种情况,就是"当const修饰一个普通变量时,则这个普通变量不应被修改。当const修饰一个指针变量时,这个指针指向的内容不应被修改,也不应让其它指针指向这个内容。"

用const修饰全局变量和局部变量的思想同上。

用static修饰的函数和变量

如果在头文件x.h中声明的函数和变量如下

extern static int startPoint;
static int Add(int a, int b);

在源文件x.c中定义如下

 static int startPoint = ;
static int Move(int a)
{
return startPoint + a;
}

那么这个Move函数就只能在x.c这个源文件中使用。这相当于面向对象里的class里的私有函数了。

用static修饰的变量startPoint,也只能在x.c这个源文件中使用,这相当于面向对象里的class里的私有静态变量。

同时,这里也显示了extern用于声明全局变量的方法。首先在头文件x.h里用extern修饰该变量的声明部分,然后在源文件x.c中定义该变量。

在x.h和x.c里的示例中,我们同时用extern和static修饰了全局变量startPoint,那么这个startPoint变量就只能在源文件x.c中出现了。如果去掉static,那么startPoint就能够在所有写了"include "x.h""的源文件中使用了。

《OOC》笔记(1)——C语言const、static和extern的用法的更多相关文章

  1. 《OOC》笔记(3)——C语言变长参数va_list的用法

    <OOC>笔记(3)——C语言变长参数va_list的用法 C语言中赫赫有名的printf函数,能够接受的参数数目不固定,这就是变长参数.C#里也有params这个关键字用来实现变长参数. ...

  2. C语言-1.static 和 extern使用,2.文件,3.数据块读写

    1.static 和 extern使用, 1)修饰局部变量 static修饰局部变量特点:延长局部变量的生命周期 ,static修饰的局部变量只会被执行一次 extern不能修饰局部变量 2)修饰全局 ...

  3. static和extern的用法小结

    以前写程序是,基本不管static和extern,一个工程文件也只有一个c文件.今天尝试用多个文件来写,自然就涉及到这两个关键词的使用,自己查了些资料,并且做了些实验,总结如下. extern的用法 ...

  4. C语言 const, static, static const 的区别

    基本定义: const  就是只读的意思,只在声明中使用;static 一般有2个作用,规定作用域和存储方式. 对于局部变量, static规定其为静态存储方式, 每次调用的初始值为上一次调用的值,调 ...

  5. C语言的static和extern关键字

    我的博客:www.while0.com 如果A.c要包含B.c里的一个变量或函数,则在A.c中要用extern关键字声明.注意: ①如果是包含的B.c里的函数,则在A.c里声明的时候可以不写exter ...

  6. C语言学习及应用笔记之三:C语言const关键字及其使用

    在C语言程序中,const关键字也是经常会用到的一个关键字,那么使用const关键字的目的是什么呢?事实上,在程序中使用const关键字的主要目的就是为了向使用者传递设计者的一些意图. 事实上,无论我 ...

  7. 23 DesignPatterns学习笔记:C++语言实现 --- 2.4 Composite

    23 DesignPatterns学习笔记:C++语言实现 --- 2.4 Composite 2016-07-22 (www.cnblogs.com/icmzn) 模式理解

  8. 个人学习笔记:C语言程序结构

    个人笔记:C语言程序 函数 语句 输入输出对象 标识符 关键字 函数 一个C语言源程序,是由一个或多个函数定义顺序组成的,其中必须有一个函数名为main的主函数.C语言源程序中的函数是指完成特定数据处 ...

  9. 笔记整理--C语言

    linux下错误的捕获:errno和strerror的使用 - Google Chrome (2014/2/26 17:31:39) linux下错误的捕获:errno和strerror的使用 201 ...

随机推荐

  1. System.Security.SecurityException The source was not found, but some or all event logs could not be searched.Inaccessible logs Security.

    An exception occurred during the Install phase. System.Security.SecurityException The source was not ...

  2. 解决SQL server 2014 修改表中的字段,无法保存的问题。

    修改PROJECT表中的字段,保存时,弹出上面的窗体,无法保存. 解决方法为:[工具]->[选项]->[设计器]中,去掉“阻止保存要求重新创建表的更改”前的勾选.

  3. MVC文件上传和下载

    1.单个文件上传 HTML写法:form表单中加enctype="multipart/form-data" <form aciont="" method= ...

  4. 深入浅出UML类图(一)

    在UML 2.0的13种图形中,类图是使用频率最高的UML图之一.Martin Fowler在其著作<UML Distilled: A Brief Guide to the Standard O ...

  5. JS-concat

    var arr1 = [ 1,2,3 ];var arr2 = [ 4,5,6 ];var arr3 = [ 7,8,9 ];alert( arr1.concat( arr2, arr3 ) );

  6. 在app中打开appStore中其他app

    var str = "https://itunes.apple.com/cn/app/zhang-jiange-hao-tou-zi-ke/id402382976?mt=8"//这 ...

  7. Cobbler学习之二--Cobbler的Web管理和维护

    Cobbler的Web管理模块和命令行模块是可以分开工作的,没有依赖关系. 1 WebUI的功能 查看所有的对象和配置文件 添加或者删除system,distro, profile 执行“cobble ...

  8. (转)Linux下root密码丢失和运行级别错误的解决办法

    我们知道,root用户在Linux中是相当重要的,其地位如同Windows中的Adminstrator 有了root权限我们还能修改其他用户的密码,可是,如果root用户的密码丢失该怎么办? 不用担心 ...

  9. input文本框去除单击时的边框的方法

    前端开发写的input文本框标签后单击时可以看到有边框,去除边框的方法: input{     outline:medium; }

  10. Android中的PopupWindow

    1.功能 PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的,可以设置显示位置. 2.需求 弹出软键盘,实现键盘功能从而 ...