收藏 分销(赏)

编程语言C与C++的不兼容性.pdf

上传人:曲**** 文档编号:12502448 上传时间:2025-10-21 格式:PDF 页数:52 大小:3.52MB 下载积分:12 金币
下载 相关 举报
编程语言C与C++的不兼容性.pdf_第1页
第1页 / 共52页
编程语言C与C++的不兼容性.pdf_第2页
第2页 / 共52页


点击查看更多>>
资源描述
Generated by Foxit PDF Creator Foxit Software For evaluation only.Incompatibilities Btwn ISO C and By David R.Tribbl Revision 1.0,2001-08-05 ContentsIntroduction C+versus C Changes to C99 versus C+98 Aggregate 工nitializrs CommentsConditional xprssion dclarations Digraph punctuation tokens Implicit function dclarations Implicit variable dclarations 工ntrmixd dderations and statmnts C99 versus C+98Altrnat punctuation token spllings Array parameter qualifiers Boolan typ Character literals clog identifier Comma operator results Complx floatingpoint type Compound literals const linkag Dsignatd initializers Duplicate typedefs Dynamic sizof valuation Empty paramtr listsEmpty preprocessor function macro arguments Enumration constantsEnumration declarations with trailing commaISO C+ISO C与工SO C+间的不兼容 原著 David R.Tribble Cao_julians 译 Leidan 校(VI.0)1.0 版,2001-08-05 内容引言C+与 CC99的改动与C+98 聚集的初始化子 注释 条件表达式声明 连字符标记 隐含函数声明 隐含变量声明 交叉的声明和语句C99 与 C+98可选的符号标记拼写数组参数限定符布尔类型字面字符clog标识符逗号操作符结果复数浮点类型复合字面字常量链接 指定的初始化 重复的类型定义 动态sizeof运算 空预处理函数宏实参 空预处理函数宏实参 枚举常量 带后缀逗号枚举声明Generated by Foxit PDF Creator Foxit Software For evaluation only.Enumeration typsFlxibl array mmbrs Function nam mangling Function pointersHxadcimal floating-point literals IEC 60559 arithmtic support 工nlin functions Integer types headers Library function prototypes Library hadr fils long long integer typeNstd structure tagsNon-prototyp function dclarations Old-styl casts One dfinition rule _Pragma keyword Prdfind idntifirsRsrvd keywords in C99 Rsrvd keywords in C+restrict keyword Returning void static linkage String initializrs String literals ar const Structures dclard in function prototyps Hyp一gnric math functions Typedefs vrsus type tagsVariable-argument function dclaratorsVariabl一argumnt preprocessor function macros Variable-length arrays Void pointer assignments Wide character type枚举类型灵活数组成员(FAMs)函数名分解 函数指针十六进制浮点数字面值 IEC 60559算术支持 inline函数integer类型头文件 库函数原型库头文件long long int 类型 嵌套的结构标记 非原型的函数声明 旧式风格的类型转换 一次定义规则_Pragma关键字 予定义的标识符 C99中的保留字 C+中的保留字 Restrict关键字 返回 void 静态链接 字符串初始化 字符串是常量在函数原型中声明的结构 泛型数学函数typedef与类型标记 可变参数函数声明 可变参数予处理函数宏 可变长度数组 void指针赋值 宽字符类型Generated by Foxit PDF Creator Foxit Software For evaluation only.IntroductionTh C programming language Ngan to be standardizd som tim around 1985 by th ANSI X3J9 committ.Several yars of effort wnt by,and in 198 9 ANSI approved th nw standard.An ISO committ ratifid it a yar latr in 19 90 after adding an amndmnt dealing with internationalization issues.Th 1989 C standard is known officially as ANSI/ISO 9899-1989,Programming Languages C,and this document refers to the 198 9 C standard as C89.The 1990 ISO revision of the standard is known officially as ISO/IEC 9899-1990,Programming Languages-C,which is rfrrd to in this documnt as C90.The nxt version of th C standard was ratified by ISO in 1999.Officially know as ISO/IEC 9899-1999,Programming Languags C,it is rfrrd to in this document as”C99”.引言大约1985年由ANSI X3J9委员会对C程序设计语言 进行标准化。多年努力后,1989年ANS工核定了新标准。一年后也就是1990年ISO委员会在追加了某些更正后以 国际化版本形式批准了它。1989年C标准被官方称作 ANSI/ISO 9899-1989,C程序设计语言,本文中涉及到 1989年C标准的称之为C89ol990年该标准的工SO修订 版被官方称作工SO/工EC9899-1999,C程序设计语言,本 文中称之为C90.C标准的下一个版本199 9年由工SO批准。官方称作 ISO/IEC9899-1999,C程序设计语言,本文中称之为 C99.The C+programming languag was based on th C programming languag as it xistd shortly aftr th ANSI C standardization ffort had begun.Around 1995 an ISO committee was formd to standardiz C+,and the new standard was ratified in 1998,which is officially known as ISO/IEC 14882-1998f Programming Languags-C+.It is referred to in this documnt as C+98”or simply as C+”.Though th two languags shar a common hritag,and though the dsigners involvd in th standardization procsss for ach language tried to kp thm as compatible as possible,som incompatibilities unavoidably aros.Once the programmr is aware of these potential problem spots,thy are asy,for th most part,to avoid whn writing C cod.C+程序设计语言是基于C程序设计语言的,在致力 于ANSI C标准后不久就产生了 C+。大约1995年成立 了标准C+的工S。委员会,新标准在1998年被批准,官 方称作工SO/工EC14882-1998,C+程序设计语言。本文 称N 为 C+9 8 或 C+。尽管这两种语言有着相同的传统,尽管这两个语言标 准化过程的设计者都尽可能保持它们的兼容性,然而某些 不兼容还是无可奈何地出现。但是程序员知道了这些潜在 的问题,他们在写C代码时会很容易避开它们。Generated by Foxit PDF Creator Foxit Software For evaluation only.Whn we say that C is incompatibl with C+with rspct to a spcific languag Mature,w man that a C program that mploys that fatur ithr is not valid C+cod and thus will not compil as a C+program,or that it will compil as a C+program but will exhibit diffrnt behavior than the sam program compiled as a C program.In othr words,an incompatible C fatur is valid as C cod but not as C+cod.All incompatibilities of this kind ar addrssd in this document.Avoiding ths kinds of incompatibilities allows the programmer to write correct C code that is intndd to interact with,or be compiled as,C+code.Another form of incompatibl fatur is one that is valid whn used in a C+program but is invalid in a C program.W call this an incompatible C+Mature.Hug portions of th C+languag fall into this catgory(e.g.,classs,tmplats,exceptions,rfrances,mmbr functions,anonymous unions,tc.),so vry fw of ths kinds of incompatibilities ar addressed in this documnt.Yet another form of incompatible fatur occurs whn a C+program uses a Mature that has the sam name as a C90 datura but which has a different usage or meaning in C.This document covrs ths kinds of incompatibilities.当我们考虑某个特定的语言特点时说C与C+不兼容,意味着采用这个特点的c代码不是一个有效的C+代码、并且不能作为C+程序进行编译,或者作为C+程序编译 会出现与作为c程序编译不同的效果。换句话说,作为C 有效的不兼容的特性,对C+无效。在本文中强调所有这 类不兼容性。特别是当它与C+代码相互作用时,或当它 被作为C+代码编译时,避免这类不兼容性可使程序员写 出正确的C代码,。另一形式的不兼容性是用于C+程序中是正确的,而 在c程序中是无效的。我们称之为C+不兼容性。C+程 序设计语言中的大部分(如类,模板,异常,引用,成员 函数,匿名共用体等等)具有这类性质,所以本文很少强 调这类不兼容性。然而还有另一形式的不兼容性,当C+程序中使用一个特性,它在c程序中有相同的名字但有不 同的用途或不同的意义。本文会涉及到这类不兼容性。This document lists only th incompatibilities between C99 and C+98.(Incompatibilities between C90 and C+have bn documntd Iswhr;sAppndix B of Stroustrup STR,for xampl.)Naw additions to th C99 standard library ar also not addrssd in this document uniss thy specifically introduce C+incompatibilities.C+vrsus CAs discussd in th Introduction,no attempt is made in this documnt to covr incompatibl C+Matures,i.,daturas of the C+language or library that ar not supported in C.Hug portions of C+and its library fall into this category.A partial list of ths daturas includes:本文只列出了 C99和C+98之间的不兼容性。(C90 和C+的不兼容性在别的文章中提及过,如见 Stroustrup 的附录 B)本文也不强调新加入的C99标准库,除非它们特别地 引入了 C+的不兼容性。C+与 C正如在引言中讨论的,本文不想讨论C+的不兼容性,即在C中不支持的C+语言或C+库中那部分特性。C+语言或库中大部分属于这一类。这些性质包括在下表中:Generated by Foxit PDF Creator Foxit Software For evaluation only.anonymous unions classes constructors and dstructors xcptions and try/catch blocks xtrnal function linkages(e.g.,xtrn C)function overloading mmbr functions namespaces new and delete operators and functions oprator overloading rfrnc types standard tmplat library(STL)template classs template functionsChanges to C99 versus C+98Th following items ar incompatibilities btwn C90 and C+98,but have since been changd in C99 so that they no longr cause problms btwn the two languags.Aggrgat工nitializrsC90 rquires automatic and rgistr variables of aggrgat type(structr array,or union)to have initializers containing only constant expressions.(Many compilers do not adhr to this restriction,however.)C99 removes that restriction,allowing non-constant xprssions to b used in such initializrs.C+allows non-constant xprssions to be used in initializrs for automatic and register variabls.(It also allows arbitrary non-constant xprssions to b usd to initializ static and xtrnal variabls.)匿名共用体类构造函数和析构函数异常和抛出/捕捉块外部函数链接(如xtrn“C部函数重载成员函数名字空间nw和delete操作符和函数操作符重载引用类型标准模板库(STL)模板类模板函数C99的改动与C+98下述项目在C90和C+98间是不兼容的,但从C99 开始它们不再会在两种语言间引起问题。聚集的初始化C90要求聚集类型(结构、数组或共用体)的自动变 量和寄存器变量只能用常量表达式初始化。(然而,许多编 译器并不坚持这个限制)C99撤消了这一限制,允许非常量表达式用于此类初 始化。C+允许非常量表达式用于自动变量和寄存器变量的 初始化。(它也允许任意的非常量表达式用于静态变量和外 部变量的初始化。)Generated by Foxit PDF Creator Foxit Software For evaluation only.For xampl:/C and C+cod void foo(int i)例如/C and C+cod void foo(int i)floatx=/.(float)i;/Valid C90,C99,andC+intm3=2,3);/./ValidC90,C99,andC+intg2=o.i;/Invalid C90)C99:6.7.8C+98:3.7.2,8.5,8.5.1floatx=(float)i;/C90,C99,and C+有效intm3=1,2,3);/C90,C99,and C+有效intg2=o,/C90无效i);Commnts注释C+recognizes/comments as well as/*/commnts.C+识另U/注释和/*/注释。C90 only rcognizs th/*.*/form of commnts.The II、form usually produces a syntax rror in C90,but thr ar rare cass that may compile erroneously without warning:C90仅识别/*。*/形式的注释。C90对/形式通常 产生一个语法错误,但很少会出现无警告而错误编译的。i=(x/*y*/z+r w);C99 recognizes both forms of comments.C99:5.1.1.2,6.4.9C+98:2.1,2.7Conditional xprssion dclarationsC+allows local variabl dclarations within conditional xprssions(which appar within for,if,whil,and switch statmnts).The scope of the variables dclard in this context xtnds to th end of the statmnt containing the conditional expression.For example:i=(x/*y*/z+,w);C99识别两种形式的注释条件表达式声明C+允许局部变量声明出现在条件表达式中(出现在 for,if,while,和switch语句中)。在这个上下文中 声明的变量其作用域扩展到包含这个条件表达式的语句尾 部。例如:Generated by Foxit PDF Creator Foxit Software For evaluation only.for(int i=0;i SIZE;i+)a i=i+1;C90 dos not allow this fatur.C99 allows this fatur,but only within for statmnts.C99:6.8.5 C+98:3.3.2,6.4,6.5for(int i=0;i SIZE;i+)a i=i+1;C90不允许这个性质。C99允许这个性质,但仅仅在这个for语句中。Digraph punctuation tokensC+rcognizs two-character punctuation tokens,calld digraphs,which ar not rcognizd by C90.Th digraphs and thir quivalnt tokens ar:)%:*%:%:#C99 recognizes th same set of digraphs.The following program is valid in both C99 and C+:%:includ stdio.h%:ifndef BUFSIZE%:dfin BUFSIZE 512%:endif void copy(char dr const char sr int len)%whil(1n=0)%:1n:=s%C99:6.4.6连字符标记C+识别两个字符的标记,称为连字符,C90不能识 别。连字符与其等价的符号如下 )%:*%:%:#C99能识别这个连字符集合。下述程序在C99和C+中都是有效的:%:include%:ifndf BUFSIZE%:define BUFSIZE 512%:endifvoid copy(char df const char s:,int 1n)=0)%d=s;%Generated by Foxit PDF Creator Foxit Software For evaluation only.C+98:2.5,2.12Implicit function declarationsC90 allows a function to b implicitly dclard at th point of its first use(call)r assigning it a return typ of int by default.For example:/*No previous dclaration of bar()is in scop*/void foo(void)(bar();/*Implicit declaration:xtrn int bar()*/a隐含函数声明C90允许一个函数在它的第一个使用(调用)点隐含 声明,缺省地指定为int返回类型。例如:/*在这个作用域内没有事先声明bar()*/*隐含声明:外部的int bar()*/C+dos not allow implicit function declarations.It is invalid to call function that dos not hav a previous dclaration in scop.C99 no longer allows functions to b implicitly dclard.The cod above is invalid in both C99 and C+.C+不允许隐含函数声明。在作用域内没有事先声明 就调用函数是无效的。C99不再允许隐含声明函数。上述代码在C99和C+中都是无效的。C99:6.5.2.2C+98:5.2.2Implicit variabl declarationsC90 allows the dclaration of a variabl,function argument,or structure mmbr to omit th type spcifir,implicitly(faulting its type to int.C99 does not allow this omission,and nithr dos C+.The following cod is valid in C90,but invalid in C99 and C+:static sizes=0;/*Implicit int,rror*/struct info const char*name;const sz;/*Implicit intr error*/);隐含变量声明C90允许变量、函数形参或结构体成员省略类型指定 符,隐含它(们)的类型是int。C99不允许这种省略,C+也不允许。下述代码在C90中有效,但在C99和C+中无效:static sizs=0;/*隐含 int,错误*/struct info(const char*name;const sz;/*隐含 int,错误*/);Generated by Foxit PDF Creator Foxit Software For evaluation only.static foo(rgistr i)/*Implicit ints,rror*/(auto j=3;/*Implicit int,rror*/return(i+j);C99:6.7,6.7.2C+98:1,7.1.5static foo(rgistr i)/*隐含 int,错误*/(auto j=3;/*隐含 int,错误*/return(i+j);工ntrmixd declarations and statementsC90 syntax spcifis that all the dclarations within a block must appar bfor the first statement in th block.交叉的声明和语句C90语法限定一个块中的所有声明必须早于块中的第 一个语句。C+dos not hav this restriction,allowing statmnts and dclarations to appar in any order within a block.C99 also rmovs this rstriction,allowing intermixed statements and declarations.void prefind(void)(int i;for(i=0;i SZ;i+)if(find(arri)brak;const char*s;/*Invalid C90,valid C99 and C+*/s=arri;prepend(s);C99:6.8.2C+98:6,6.3,6.7C+没有这个限制,允许一个块中的语句和声明以任 何顺序出现。C99也撤消了这个限制,允许交叉的语句和声明。void prefind(void)(int i;for(i=0;i SZ;i+)if(find(arri)brak;const char*s;/*工C90中无效,C99和C+中有效*/s=arr i;prpnd(s);Generated by Foxit PDF Creator Foxit Software For evaluation only.C99 vrsus C+98C99 与 C+98Th following items comprise th diffrncs btwn C99 and C+98.Some of th s incompatibilities xist d b tw n C8 9 and C+98 and remain unchanged btwn C99 and C+98,whil others ar new daturas that wr introduced into C99 that are incompatibl with C+98.Not that features that ar specific to C+and which are not 1gal C(.g.,class mmbr function declarations)are not included in this section;only language features that ar common to both C and C+ar discussd.Most of the features are valid as C but invalid as C+.下述项目包括了 C99和C+98间的差别。有一些不 兼容性在C89和C+98中就存在,并保留到C99和 C+98,然而其它一些新引入C99的性质也与C+98不 兼容。注意一些在C中不合法的C+性质(如类成员函数 声明)并不包括在这里;只讨论C和C+共有的语法性质。这些性质大部分在C中是有效的,但在C+中是无效的。Som of ths faturs ar likely to b implmntd as xtnsions by many C+compilers in ordr to b more compatibl with C compilers.Altrnat punctuation tokn spellingsC+provids the following keywords as synonyms for punctuation tokens:8.nd&and_eq&=bitand&bitor|compl not!not_q!=or|or_q|=xor A xorqq A=Ths keywords ar also recognized by th C+preprocessor.C90 dos not have these built-in keywords,but it does provide a standard header fil that contains dfinitions for th sam words as macros,bhaving almost like built-in keywords.某些性质或许是由许多C+编译器实现的扩展,为与 C编译器更多地兼容。可选的符号标记拼写C+提供下述关键字作为符号标记的同义词。and&and_eq&=bitand&bitor|compl not!not_ q!=or|or_eq|=xor A xorqq A=C+预处理器也识别这些关键字。C90没有这些内建的关键字,但提供了一个标准的 is0646.h头文件,包含了这些词的宏定义,表现为 有这些内建关键字。Generated by Foxit PDF Creator Foxit Software For evaluation only.C+rquires implmntations to provid an mpty hadr.Including it in a C+program has no ffct on the program.Howvr,C cod that do s not includ the h ad r is free to use th s words as idntifirs and macro nairas,which may caus incompatibilities when such cod is compile1 as C+.num oper nop,and,or,q,ne;xtrn int instr(num opr op,struct compl*c);Th rcommndd practice for cod intndd to b compild as both C and C+is to use ths idntifirs only for these special manings,and only after including./Propr hadr inclusion allows for the us of*and*t al#ifndef _cplusplus include int foo(float a,float b,float c)(return(a b and b=c);C99:7.9C+98:2.5,2.11Array parameter qualifiersC99 provids nw declaration syntax for function paramtrs of array types,allowing type qualifiers(the cv-qualifiers const and volatil,and restrict)to be includd within th first set of brackets of an array declarator.The qualifier modifies th type of th array paramter itself.For xampl,the following dclarations ar semantically idntical:extern void foo(int strconst);xtrn void foo(int*const str);In both declarations,paramtr str is a const pointr to an int object.C+需要提供一个空的1$。646.4头文件来实现。在C+程序中包含它没有效果。然而,C代码中不包含这 个头文件去作为标识符和宏名字使用,将在C+中编译时 引起不兼容性。enum opr nop,and,or,eqA ne;extern int instr(num oper op,struct compl*c);仅在这些特定意义上使用这些标识符,而且仅在包含 了is0646.h头文件之后,这个被推荐的实践有意于代 码象C和C+一样被编译,。/正确的头文件包含允许-nd,等的使用#ifndf cplusplus#includ ndifint foo(float a,float b,float c)(return(a b and b=c);数组参数限定符C99为数组类型的函数参数提供了新的声明语法,允 许类型限定符(cv限定符const,volatile和 restrict)被包括在数组声明符的第一组括号内。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服