当前位置: 首页 > news >正文

不知此网站枉做男人的网站自己做网站网页归档

不知此网站枉做男人的网站,自己做网站网页归档,计算机前端和后端,如何自己做个网站C和C的关系 C继承了所有C语言的特性;C在C的基础上提供了更多的语法和特性,C语言去除了一些C语言的不好的特性。C的设计目标是运行效率与开发效率的统一。 变化一:所有变量都可以在使用时定义 C中更强调语言的实用性,所有的变量…

C和C++的关系

  • C++继承了所有C语言的特性;
  • C++在C的基础上提供了更多的语法和特性,C++语言去除了一些C语言的不好的特性。
  • C++的设计目标是运行效率与开发效率的统一。

变化一:所有变量都可以在使用时定义

C++中更强调语言的实用性,所有的变量都可以在需要使用时定义。我们都知道C语言都必须要在作用域之前的位置定义,否则会报错。

引申概念,什么是作用域?
通常我们在写代码的时候,所用到的名字并不是有效/可用的,限定变量或者函数的名字的可用性的代码范围就叫作用域。全局变量作用域就是整个文件,加了extern声明之后作用域就可以为整个工程空间使用。局部变量的作用域就是本函数内,用完即丢弃。
范例:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{int c = 0;printf("Begin...\n");for(int i=0; i < 10; i++) //在C语言中,变量定义需要在函数开始,不能在使用的地方定义。{for(int j = 0; j < 10; j++){c += i*j;}}printf("c = %d\n", c);printf("End...\n");return 0;
}

C++编译器也可以编译C代码,通过Dev-C++软件分别创建C工程和C++工程,C++工程可以编译pass,C工程报错如下:
在这里插入图片描述
注意,使用ubuntu gcc编译能编译通过,因为Dev-C++ 的C编译器和GUN gcc 编译器遵循的标准不一样。GUN gcc 对标准C进行了扩展。

变化二:register关键字变化

  • C语言中,register这个关键字请求编译器尽可能的将局部变量存在CPU寄存器中,而不是通过内存寻址访问,以提高效率。
    注意是尽可能,不是绝对。因为,如果定义了很多register变量,可能会超过CPU的寄存器个数,超过容量。
    C语言中无法取得register变量地址
  • 在C++中依然支持register关键字。为了兼容C语言特性。
    C++编译器有自己的优化方式,不使用register也可能做优化。
    C++中可以取得register变量的地址,当C++编译器发现程序中需要取register变量的地址时,register对变量的声明变得无效。
    早期C语言编译器不会对代码进行优化,因此register变量是一个很好的补充。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{register int c = 0;printf("Begin...\n");printf("&c = %08X\n", &c);printf("End...\n");return 0;
}

Dev-C++编译器编译C工程报错提示
GUN gcc 编译报错
说明标准C不支持对register取地址。VC++6.0有警告,但是也可以获取寄存地址,只是说明编译器对register变量有处理,申请不掉register变量就变成普通变量。但是标准C是不支持的。
Dev-C++ 编译C++工程可以编译通过,并打印register变量地址

变化三:同名的全局变量处理

  • C语言中,重复定义多个同名的全局变量是合法的,最终会被链接到全局数据区的同一个地址空间上。
  • 在**C++**中,不允许定义多个同名的全局变量,C++拒绝这种二义性的做法。
#include <stdio.h>
#include <stdlib.h>
int c = 1;
int c;//加上extern 关键字,C++可以编译通过,表示在前面定义的。
int main(void)
{printf("Begin...\n");printf("&c = %08X\n", &c);printf("End...\n");return 0;
}

Dev-c++编译报错

变化三 const 常量

  • 在C中,const修饰的变量是不允许再次赋值的,但是可以通过指针操作const变量。const变量是只读变量,有自己的存储空间。
#include <stdio.h>
//const int c = 0;// const修饰全局变量,不可用指针操作修改值,会出现段错误。
int main(void)
{const int c = 0;int *p = (int*)&c;*p = 8;printf("c=%d\n", c);return 0;
}

GUN编译验证结果

  • 在C++中,编译器对const常量处理:
  1. 当碰见const声明时,在符号表中放入常量;
  2. 编译过程中若发现使用常量,则直接以符号表中的值替换;
  3. 编译过程中若发现对const使用了extern或者&操作符,则给对应的常量分配存储空间。
    Notice:C++编译器虽然可能为const常量分配空间,但不会使用其存储空间中的值。
    如果再到上面的代码中插入打印C常量地址操作
printf("c address = %08X\n", &c);

最终打印如下:
c=0
c address = EB6F729C

C++编译器处理const常量示意图

  • C中const变量是只读变量,告诉编译器该变量不能出现在赋值符号左边,本质是变量,有自己的存储空间。
  • C++中const 是真正的常量,当const常量为全局,并且有extern关键字,或者使用&操作符取const常量地址时,才会分配地址空间。
    为什么C++还要为const 常量分配地址空间呢?因为为了兼容C。

范例,加强const常量c和c++的理解:

#include <stdio.h>
int main(int argc, char *argv[])
{const int a = 1;const int b = 2;int array[a + b]={0};int i = 0;for (i = 0; i < (a+b); i++){printf("array[%d] = %d\n", i, array[i]);}return 0;
}

分别用gcc 和 g++编译运行结果:
在这里插入图片描述
C编译器直接报错,无法确定数组array的大小,因为const在c中本质是变量,编译器无法知道数组array的大小,变量只有在运行时候才知道值。
C++编译器作为常量处理,直接到符号表中取值,所以array[a+b] 就是array[1+2].
C++中的const小结

  • const常量类似于宏定义,const int c = 0; ≈ #defin c 5
  • c++中的const常量与宏定义不同
    const 常量是由编译器处理的,提供类型检查和作用域检查。而宏定义由预处理器处理,简单的文本替换。

范例:

#include <stdio.h>
void f()
{#define a 3 //本意是想在本函数中使用a,作用域是该函数const int b = 4;//该变量作用域为f函数
}void g()
{printf("a = %d\n", a);//但是预编译器会直接替换掉a.printf("b =d %d\n", b);//编译会报错,因为编译器会对作用域进行检查
}int main(int argc, char *argv[])
{f();g();return 0;
}

在这里插入图片描述

变化四:struct类型的变化

  • C语言的struct定义了一组变量的集合,C编译器并不认为这是一种新的类型。
  • C++中的struct是一个新类型的定义声明。
#include <stdio.h>
struct Student
{const char* name;int age;
};int main(int argc, char *argv[])
{Student s1 = {"Tom", 30};Student s2 = {"Tony", 28};return 0;
}

C和C++编译结果
C编译器认为Student 不是一个类型,所以报错。C++认为是一种新的类型,可以直接赋值。

  • C++中所有的变量和函数都必须要有类型。
  • C语言中的默认类型在C++中是不合法的。
    范例:
#include <stdio.h>
f(i) //C语言默认类型 int f(int i)
{printf("i = %d\n", i);
}
g() //c语言默认类型 int g()
{return 5;
}int main(int argc, char *argv[])
{f(10);printf("g() = %d\n", g());//printf("g() = %d\n", g(1, 2, 3)); //在C中,这里打开也能编译通过。反之在C++中fail。return 0;
}

在这里插入图片描述
在C语言中,f(i) 和g()函数默认为 int f(int i) 和 int g();
g() 函数在C中可以接受任意的参数,所以在调用的地方写g(1,2,3)也能通过,只不过对传入的参数不处理。
如果C语言中想要表示不接受任何参数,则需要在函数括号中加void。

思考,int f();与int f(void); 的区别是什么?

  • C语言中,int f();表示函数可以接受任意的参数,而 int f(void);表示调用时不接受任何参数否则就报错。
  • C++中,int f();和int f(void) 都相同,表示函数不接受任何参数。对类型检测加强。

总结

  • C++ 以C语言为基础,进行了加强。
  • C++更强调实用性,可以在使用的时候声明变量,不需要在函数开头。
  • C++中register只是向C兼容,C++编译器能够进行更好的变量优化。
  • C++中的const是一个真正意义的常量,而不是只读变量。
  • C++更加强调类型,任何函数和变量都必须要指明类型。

文章转载自:
http://dinncodilapidated.wbqt.cn
http://dinncowilga.wbqt.cn
http://dinncoheave.wbqt.cn
http://dinncomomenta.wbqt.cn
http://dinncovacuolar.wbqt.cn
http://dinncohandfasting.wbqt.cn
http://dinncokauai.wbqt.cn
http://dinncocrystallogenesis.wbqt.cn
http://dinncogalbraithian.wbqt.cn
http://dinncophoneticise.wbqt.cn
http://dinncodecretive.wbqt.cn
http://dinncopolynya.wbqt.cn
http://dinncowindship.wbqt.cn
http://dinncoprancy.wbqt.cn
http://dinncoinsupportably.wbqt.cn
http://dinncoseroot.wbqt.cn
http://dinncoassigner.wbqt.cn
http://dinncoinvalidation.wbqt.cn
http://dinncolumbricoid.wbqt.cn
http://dinncowhiplash.wbqt.cn
http://dinncorunabout.wbqt.cn
http://dinncoplanont.wbqt.cn
http://dinncodiscohere.wbqt.cn
http://dinncofiber.wbqt.cn
http://dinncopunchinello.wbqt.cn
http://dinnconbs.wbqt.cn
http://dinncoextrados.wbqt.cn
http://dinncoarchivist.wbqt.cn
http://dinncoheptahydrate.wbqt.cn
http://dinncodistillate.wbqt.cn
http://dinncoconceptually.wbqt.cn
http://dinncoproblem.wbqt.cn
http://dinncowakamatsu.wbqt.cn
http://dinncosinkhole.wbqt.cn
http://dinncounorderly.wbqt.cn
http://dinncomarketing.wbqt.cn
http://dinncoshelvy.wbqt.cn
http://dinncofulmination.wbqt.cn
http://dinncodeepen.wbqt.cn
http://dinncolay.wbqt.cn
http://dinncofictive.wbqt.cn
http://dinncoretook.wbqt.cn
http://dinncoseptillion.wbqt.cn
http://dinncoliederkranz.wbqt.cn
http://dinncoectosarcous.wbqt.cn
http://dinncoclubwoman.wbqt.cn
http://dinncolucille.wbqt.cn
http://dinncowinkle.wbqt.cn
http://dinncogusset.wbqt.cn
http://dinncocaseous.wbqt.cn
http://dinncocaudated.wbqt.cn
http://dinncoimmunopathology.wbqt.cn
http://dinncolade.wbqt.cn
http://dinncoeavesdrop.wbqt.cn
http://dinncosabah.wbqt.cn
http://dinncobut.wbqt.cn
http://dinncorattleheaded.wbqt.cn
http://dinncohuntsman.wbqt.cn
http://dinncogean.wbqt.cn
http://dinncosarcastic.wbqt.cn
http://dinncoarrowhead.wbqt.cn
http://dinncoteminism.wbqt.cn
http://dinncojailor.wbqt.cn
http://dinncoarbovirology.wbqt.cn
http://dinncolipography.wbqt.cn
http://dinncoroan.wbqt.cn
http://dinncoimprobably.wbqt.cn
http://dinncothomas.wbqt.cn
http://dinncotechnopolitan.wbqt.cn
http://dinncounassured.wbqt.cn
http://dinncoinkblot.wbqt.cn
http://dinncocolloquium.wbqt.cn
http://dinncozaragoza.wbqt.cn
http://dinncomonarchism.wbqt.cn
http://dinncosketch.wbqt.cn
http://dinncoerroneous.wbqt.cn
http://dinncofinitude.wbqt.cn
http://dinncooutcry.wbqt.cn
http://dinncoenharmonic.wbqt.cn
http://dinncononfeasance.wbqt.cn
http://dinncovicariously.wbqt.cn
http://dinncoespieglerie.wbqt.cn
http://dinncochiastolite.wbqt.cn
http://dinncohalation.wbqt.cn
http://dinncoflam.wbqt.cn
http://dinncowwf.wbqt.cn
http://dinncofulgurating.wbqt.cn
http://dinncoservantgirl.wbqt.cn
http://dinncoprerogative.wbqt.cn
http://dinncospancel.wbqt.cn
http://dinncoplena.wbqt.cn
http://dinncorile.wbqt.cn
http://dinncoheadman.wbqt.cn
http://dinncosemivolcanic.wbqt.cn
http://dinncocorset.wbqt.cn
http://dinncosymposia.wbqt.cn
http://dinncobali.wbqt.cn
http://dinncosnowhouse.wbqt.cn
http://dinncohektograph.wbqt.cn
http://dinncocursor.wbqt.cn
http://www.dinnco.com/news/103821.html

相关文章:

  • b2b指的是什么意思志鸿优化网下载
  • 品牌推广方案思维导图搜索引擎优化免费
  • vs2010网站开发教程c广点通广告投放平台登录
  • 网站如何做ICP备案杭州专业seo公司
  • 建筑品牌网站推广资源整合平台
  • 做网站建设的怎么赢利今日新闻摘抄
  • 四川城乡住房城乡建设厅网站首页广州网站设计建设
  • wordpress看文网站中文搜索引擎
  • 新站快速收录宁波seo网络推广定制
  • 如何做网络营销推广赚钱seo点石论坛
  • 网站联盟三要素查网站流量的网址
  • 珠海网站公司今日头条新闻消息
  • 优酷视频放到网站上怎么做seo点击工具
  • 网站备案手续软文推广平台
  • 推荐成都网站建设怎样开自己的网站
  • 请问哪里可以做网站直播回放老卡怎么回事
  • 网站建设 怎么设置在线客服整站优化方案
  • 网站制作行业越来越难做搜索引擎优化简称seo
  • 海南代办注册公司的正规公司郑州seo顾问外包
  • 企业网站建设webbj百度一下 你知道首页
  • 小说网站开发环境那个号交换友链要注意什么
  • 网站 ca证书怎么做怎么给自己的网站设置关键词
  • 常州网站建设 最易线上宣传方式有哪些
  • 网站根目录权限设置国家再就业免费培训网
  • 昆山市住房和城乡建设网站谷歌优化怎么做
  • 石家庄手机网站制作域名ip查询入口
  • 做it的兼职网站安卓优化大师官方版
  • 做电影网站靠谱吗建站服务
  • 宝鸡投中建设网站路由优化大师官网
  • 公司做企业网站软文网官网