您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > C_Coding_Standard
CCCCCodingCodingCodingCodingStandardStandardStandardStandardRuleRuleRuleRule1:1:1:1:AnyAnyAnyAnyvariablesvariablesvariablesvariablesmustmustmustmustbebebebeinitializedinitializedinitializedinitializedbeforebeforebeforebeforeusingusingusingusingit.it.it.it.Anyvariables,includingglobal,staticorlocalvariable,mustbeinitializedwithanexplicitvaluebeforeusingit.Reason:Reason:Reason:Reason:Thisrulerequiredtoavoidusingvariablewithunknownorrandomvalue.Category:Category:Category:Category:CompulsoryExample:Example:Example:Example:RuleRuleRuleRule2:2:2:2:Compiler/LinkerCompiler/LinkerCompiler/LinkerCompiler/Linkerdependentdependentdependentdependentfunctionsfunctionsfunctionsfunctionsshouldshouldshouldshouldbebebebeavoidedavoidedavoidedavoidedasasasaspossible.possible.possible.possible.TheCompiler/Linkerdependentfunctionswillrestricttheportabilityofthecode.Weshouldtryourbesttoavoidusingsuchfunctions,including:�Toaccessa3rdpartylibrary,weshouldmakesuretofollowthesamecompileroptionswithwhichtobuildthislib.Thecallermustfollowingthecalleeinvocationconvention.Mixedusingof__cdecl,__stdcall,__fastcalland__pascalisnotallowed.�#pragmadirectivemustbedocumentedandexplainedwhereitisused,ifsuchusagecannotbeavoided.�Usingfunctioncallingasparametershouldbeavoidedalso,itisimplementation-defined.Forexample:foo(getValue(),i++)Reason:Reason:Reason:Reason:SomebehaviorsoftheClanguagearecompiler-dependent,thereforethesemustbeunderstoodforhowthecompilerbeingused.Examplesofissueswhichneedtobeunderstoodare:StackUsage,ParameterPassingandhowdatastoredlikeLength,Alignment,Aliasing,OverlayetcTheserulescanimprovethecodeportability.Category:Category:Category:Category:OptionalExample:Example:Example:Example:NARuleRuleRuleRule3:3:3:3:IdentifiersIdentifiersIdentifiersIdentifiersininininananananinnerinnerinnerinnerscopescopescopescopeshallshallshallshallnotnotnotnotuseuseuseusethethethethesamesamesamesamenamenamenamenameasasasasananananidentifieridentifieridentifieridentifierininininananananouterouterouterouterscope,scope,scope,scope,andandandandthereforethereforethereforethereforehidehidehidehidethatthatthatthatidentifier.identifier.identifier.identifier.Theruleistodisallowthecasewhereaninnerdefinitioncanhideanouterdefinition.Iftheinnerdefinitiondoesnothidetheouterdefinition,thenthisruleisnotviolated.Reason:Reason:Reason:Reason:Hidingidentifiersdefinitioninanouterscopeleadsunexpectedmistakes,andsuchcodeissomeconfusing.Category:Category:Category:Category:CompulsoryExample:Example:Example:Example:RuleRuleRuleRule4:4:4:4:typedeftypedeftypedeftypedefnamenamenamenamemustmustmustmustbebebebeaaaauniqueuniqueuniqueuniqueidentifier.identifier.identifier.identifier.Whendefiningatypeviatypedef,thenewtypeidentifier(callingtypedefname)shouldbeuniqueattheprojectscope.Notypedefnameshouldbereusedeitherastypedefnameorforanyotherpurpose.Wherethetypedefinitionismadeinaheaderfile,andthatheaderfileisincludedinmultiplesourcefiles,thisruleisnotaviolated.Reason:Reason:Reason:Reason:Thisruleistomisusingofwrongtypedefinition.Multipletypedefwithsamenamemayleadunexpectedmistake.Category:Category:Category:Category:CompulsoryExample:Example:Example:Example:RuleRuleRuleRule5:5:5:5:signedsignedsignedsignedandandandandunsignedunsignedunsignedunsignedcharcharcharchartypetypetypetypeshallshallshallshallbebebebeusedusedusedusedonlyonlyonlyonlyforforforforthethethethestoragestoragestoragestorageandandandanduseuseuseuseofofofofnumericnumericnumericnumericvalues;values;values;values;thethethetheplainplainplainplaincharcharcharcharshallshallshallshallbebebebeusedusedusedusedforforforforcharactercharactercharactercharacterdata.data.data.data.Therearethreedistinctchartypes,(plain)char,signedcharandunsignedchar.signedcharandunsignedcharshallbeusedfornumericdataandplaincharshallbeusedforcharacterdata.Theonlypermissibleoperatorsonplainchartypesareassignmentandequalityoperators(=,==,!=).Reason:Reason:Reason:Reason:Thesignoftheplainchartypeisimplementation-definedandshouldnotbereliedupon.Category:Category:Category:Category:CompulsoryExample:Example:Example:Example:RuleRuleRuleRule6:6:6:6:BitBitBitBitfieldsfieldsfieldsfieldsshallshallshallshallonlyonlyonlyonlybebebebedefineddefineddefineddefinedtotototobebebebeofofofofunsignedunsignedunsignedunsignedtypetypetypetypeororororsignedsignedsignedsignedtype.type.type.type.Theonlyallowedtypetodefinebitfieldsaresignedorunsignedtype.Plaintype(suchasint,char,short,long)andenumtypearenotallowedReason:Reason:Reason:Reason:Theplaintypeandenumtypeareimplementation-definedandshouldnotbereliedupon.Category:Category:Category:Category:CompulsoryExample:Example:Example:Example:RuleRuleRuleRule7:7:7:7:WheneverWheneverWheneverWheneverananananobjectobjectobjectobjectororororfunctionfunctionfunctionfunctionisisisisdeclareddeclareddeclareddeclaredorororordefined,defined,defined,defined,itsitsitsitstypetypetypetypeshallshallshallshallbebebebeexplicitlyexplicitlyexplicitlyexplicitlystated.stated.stated.stated.Thetypesoftheparametersandreturnvaluesintheprototypeandthedefinitio
本文标题:C_Coding_Standard
链接地址:https://www.777doc.com/doc-5377148 .html