易截截图软件、单文件、免安装、纯绿色、仅160KB

解析C与C++中的关键字const

 
 
在C与C++语言中都存在关键字const,很多人都对此关键字存在一个错误的认识,认为在C语言中关键字const是使变量作为一个常量,即将变量常量化,就像宏定义一样。而在C语言中的关键字const所起的作用并不是使变量常量话,而是限制变量,使变量除了被赋初值外,无法被重新赋值。
而在C++中关键字const不仅使该变量无法修改,也是使变量常量化,即将变量赋初值后可以当作常量使用,相当于进行了宏定义。
在编译器中输入以下代码,你会有更直观的体会。
 
 在C语言编译器中:
 
/* const限制的变量的值无法修改*/
#include <stdio.h>
int main(void)
{
const int a=1;
int const b=1;
a=1;
b=1;
system("pause");
return 0;

当你将这段代码编译时,编译器会报错:在mian函数中无法修改const限制的对象。
 
 
 
注:在C语言中,关键字const被放在标识符之前或之后是一样的效果。
 
 
 
/* const限制的变量无法修改*/
#include <stdio.h>
int main(void)
{
const int a=1;
switch(1)
{
case a:prinf("variable a can be used as constant");break;
default :printf("variable a cannot be used as constant");
}
system("pause");
return 0;
}

当你编译这段代码是,编译器会报错:在main函数中要求使用常量。
 
 
 
关键字const对指针的限制:
/*cosnt在*前,指针指向对象的值无法修改*/
#include <stdio.h>
int main(void)
{
int a=1,b=2;
const int * p1=&a;
int const * p2=&b;
*p1=1;
*p2=1;
system("pause");
return 0;
}

const在星号前对对指针变量进行限制时是对指针变量所指向的对象进行限制,针对上列代码就是对a,b进行限制,使a,b不能被重新赋值,因此编译时会报错:在mian函数中无法修改const限制的对象。但两个指针变量的值是可以修改的。编译下列代码:
 
/* cosnt在*前,指针变量的值可以修改*/
#include <stdio.h>
int main(void)
{
int a=1,b=2;
const int * p1=&a;
int const * p2=&b;
printf ("a=%d\nb=%d\n",*p1,*p2);
p1=&b;
if(p1==p2)
printf("the address of p1 is equal to the address of p2\n");
system("pause");
re


相关文档:

memcached 与c/c++运用

修改makefile,在LIBS里面加上-lmemcached,比如原来 gcc test.c,现在 gcc test.c -lmemcached。这个库就是libmemcached提供的。
然后添加#include<libmemcached/memcached.h>,这个文件也是libmemcached提供的。
主函数里面需要添加:
    memcached_st *memc;
    uint32_t&nbs ......

C 语言中清空输入缓冲区

C语言中有几个基本输入函数:
//获取字符系列
int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar(void);
 
//获取行系列
char *fgets(char * restrict s, int n, FILE * restrict stream);
char *gets(char *s);//可能导致溢出,用fgets代替之。
 
//格式化输入系列
int fscanf(FILE * r ......

职工工资管理系统(我的第一个C程序)


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<windows.h>
#include<malloc.h>
#include<math.h>
typedef struct worker

 int num; //编号
 char name[15]; //姓名
 char zhicheng[15];& ......

[转]C\C++语言入门篇系列

虽然学习了好多年,但需要细究某些基础知识的时候还是发现自己忘了, 从别人的文章扒过来,以备复习
参考: http://blog.csdn.net/masefee/archive/2009/12/28/5090820.aspx
============================================================
之前的定位可能主要为了研究底层及一些较复杂的问题上,而忽略了一些初学的朋友。导 ......

C测试小程序

C测试小程序
1、                 字符串类
1.1        strstr
功能:查找和获取子串
void test_strstr()
{
        char *str="Borland   Inte ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号