c.texi 424 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112
  1. \input texinfo
  2. @c Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
  3. @c (The work of Trevis Rothwell and Nelson Beebe has been assigned or
  4. @c licensed to the FSF.)
  5. @c move alignment later?
  6. @setfilename ./c
  7. @settitle GNU C Language Manual
  8. @documentencoding UTF-8
  9. @c Merge variable index into the function index.
  10. @synindex vr fn
  11. @copying
  12. Copyright @copyright{} 2022 Richard Stallman and Free Software Foundation, Inc.
  13. (The work of Trevis Rothwell and Nelson Beebe has been assigned or
  14. licensed to the FSF.)
  15. @quotation
  16. Permission is granted to copy, distribute and/or modify this document
  17. under the terms of the GNU Free Documentation License, Version 1.3 or
  18. any later version published by the Free Software Foundation; with the
  19. Invariant Sections being ``GNU General Public License,'' with the
  20. Front-Cover Texts being ``A GNU Manual,'' and with the Back-Cover
  21. Texts as in (a) below. A copy of the license is included in the
  22. section entitled ``GNU Free Documentation License.''
  23. (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
  24. modify this GNU manual.''
  25. @end quotation
  26. @end copying
  27. @dircategory Programming
  28. @direntry
  29. * C: (c). GNU C Language Intro and Reference Manual
  30. @end direntry
  31. @titlepage
  32. @sp 6
  33. @center @titlefont{GNU C Language Introduction}
  34. @center @titlefont{and Reference Manual}
  35. @sp 4
  36. @c @center @value{EDITION} Edition
  37. @sp 5
  38. @center Richard Stallman
  39. @center and
  40. @center Trevis Rothwell
  41. @center plus Nelson Beebe
  42. @center on floating point
  43. @page
  44. @vskip 0pt plus 1filll
  45. @insertcopying
  46. @sp 2
  47. @ignore
  48. WILL BE Published by the Free Software Foundation @*
  49. 51 Franklin Street, Fifth Floor @*
  50. Boston, MA 02110-1301 USA @*
  51. ISBN ?-??????-??-?
  52. @end ignore
  53. @ignore
  54. @sp 1
  55. Cover art by J. Random Artist
  56. @end ignore
  57. @end titlepage
  58. @summarycontents
  59. @contents
  60. @node Top
  61. @ifnottex
  62. @top GNU C Manual
  63. @end ifnottex
  64. @iftex
  65. @top Preface
  66. @end iftex
  67. This manual explains the C language for use with the GNU Compiler
  68. Collection (GCC) on the GNU/Linux system and other systems. We refer
  69. to this dialect as GNU C. If you already know C, you can use this as
  70. a reference manual.
  71. If you understand basic concepts of programming but know nothing about
  72. C, you can read this manual sequentially from the beginning to learn
  73. the C language.
  74. If you are a beginner in programming, we recommend you first learn a
  75. language with automatic garbage collection and no explicit pointers,
  76. rather than starting with C@. Good choices include Lisp, Scheme,
  77. Python and Java. C's explicit pointers mean that programmers must be
  78. careful to avoid certain kinds of errors.
  79. C is a venerable language; it was first used in 1973. The GNU C
  80. Compiler, which was subsequently extended into the GNU Compiler
  81. Collection, was first released in 1987. Other important languages
  82. were designed based on C: once you know C, it gives you a useful base
  83. for learning C@t{++}, C#, Java, Scala, D, Go, and more.
  84. The special advantage of C is that it is fairly simple while allowing
  85. close access to the computer's hardware, which previously required
  86. writing in assembler language to describe the individual machine
  87. instructions. Some have called C a ``high-level assembler language''
  88. because of its explicit pointers and lack of automatic management of
  89. storage. As one wag put it, ``C combines the power of assembler
  90. language with the convenience of assembler language.'' However, C is
  91. far more portable, and much easier to read and write, than assembler
  92. language.
  93. This manual describes the GNU C language supported by the GNU Compiler
  94. Collection, as of roughly 2017. Please inform us of any changes
  95. needed to match the current version of GNU C.
  96. When a construct may be absent or work differently in other C
  97. compilers, we say so. When it is not part of ISO standard C, we say
  98. it is a ``GNU C extension,'' because it is useful to know that.
  99. However, standards and other dialects are secondary topics for this
  100. manual. For simplicity's sake, we keep those notes short, unless it
  101. is vital to say more.
  102. Likewise, we hardly mention C@t{++} or other languages that the GNU
  103. Compiler Collection supports. We hope this manual will serve as a
  104. base for writing manuals for those languages, but languages so
  105. different can't share one common manual.
  106. Some aspects of the meaning of C programs depend on the target
  107. platform: which computer, and which operating system, the compiled
  108. code will run on. Where this is the case, we say so.
  109. The C language provides no built-in facilities for performing such
  110. common operations as input/output, memory management, string
  111. manipulation, and the like. Instead, these facilities are provided by
  112. functions defined in the standard library, which is automatically
  113. available in every C program. @xref{Top, The GNU C Library, , libc,
  114. The GNU C Library Reference Manual}.
  115. GNU/Linux systems use the GNU C Library to do this job. It is itself
  116. a C program, so once you know C you can read its source code and see
  117. how its library functions do their jobs. Some fraction of the
  118. functions are implemented as @dfn{system calls}, which means they
  119. contain a special instruction that asks the system kernel (Linux) to
  120. do a specific task. To understand how those are implemented, you'd
  121. need to read Linux source code instead. Whether a library function is
  122. a system call is an internal implementation detail that makes no
  123. difference for how to call the function.
  124. This manual incorporates the former GNU C Preprocessor Manual, which
  125. was among the earliest GNU manuals. It also uses some text from the
  126. earlier GNU C Manual that was written by Trevis Rothwell and James
  127. Youngman.
  128. GNU C has many obscure features, each one either for historical
  129. compatibility or meant for very special situations. We have left them
  130. to a companion manual, the GNU C Obscurities Manual, which will be
  131. published digitally later.
  132. Please report errors and suggestions to c-manual@@gnu.org.
  133. @menu
  134. * The First Example:: Getting started with basic C code.
  135. * Complete Program:: A whole example program
  136. that can be compiled and run.
  137. * Storage:: Basic layout of storage; bytes.
  138. * Beyond Integers:: Exploring different numeric types.
  139. * Lexical Syntax:: The various lexical components of C programs.
  140. * Arithmetic:: Numeric computations.
  141. * Assignment Expressions:: Storing values in variables.
  142. * Execution Control Expressions:: Expressions combining values in various ways.
  143. * Binary Operator Grammar:: An overview of operator precedence.
  144. * Order of Execution:: The order of program execution.
  145. * Primitive Types:: More details about primitive data types.
  146. * Constants:: Explicit constant values:
  147. details and examples.
  148. * Type Size:: The memory space occupied by a type.
  149. * Pointers:: Creating and manipulating memory pointers.
  150. * Structures:: Compound data types built
  151. by grouping other types.
  152. * Arrays:: Creating and manipulating arrays.
  153. * Enumeration Types:: Sets of integers with named values.
  154. * Defining Typedef Names:: Using @code{typedef} to define type names.
  155. * Statements:: Controlling program flow.
  156. * Variables:: Details about declaring, initializing,
  157. and using variables.
  158. * Type Qualifiers:: Mark variables for certain intended uses.
  159. * Functions:: Declaring, defining, and calling functions.
  160. * Compatible Types:: How to tell if two types are compatible
  161. with each other.
  162. * Type Conversions:: Converting between types.
  163. * Scope:: Different categories of identifier scope.
  164. * Preprocessing:: Using the GNU C preprocessor.
  165. * Integers in Depth:: How integer numbers are represented.
  166. * Floating Point in Depth:: How floating-point numbers are represented.
  167. * Compilation:: How to compile multi-file programs.
  168. * Directing Compilation:: Operations that affect compilation
  169. but don't change the program.
  170. Appendices
  171. * Type Alignment:: Where in memory a type can validly start.
  172. * Aliasing:: Accessing the same data in two types.
  173. * Digraphs:: Two-character aliases for some characters.
  174. * Attributes:: Specifying additional information
  175. in a declaration.
  176. * Signals:: Fatal errors triggered in various scenarios.
  177. * GNU Free Documentation License:: The license for this manual.
  178. * Symbol Index:: Keyword and symbol index.
  179. * Concept Index:: Detailed topical index.
  180. @detailmenu
  181. --- The Detailed Node Listing ---
  182. * Recursive Fibonacci:: Writing a simple function recursively.
  183. * Stack:: Each function call uses space in the stack.
  184. * Iterative Fibonacci:: Writing the same function iteratively.
  185. * Complete Example:: Turn the simple function into a full program.
  186. * Complete Explanation:: Explanation of each part of the example.
  187. * Complete Line-by-Line:: Explaining each line of the example.
  188. * Compile Example:: Using GCC to compile the example.
  189. * Float Example:: A function that uses floating-point numbers.
  190. * Array Example:: A function that works with arrays.
  191. * Array Example Call:: How to call that function.
  192. * Array Example Variations:: Different ways to write the call example.
  193. Lexical Syntax
  194. * English:: Write programs in English!
  195. * Characters:: The characters allowed in C programs.
  196. * Whitespace:: The particulars of whitespace characters.
  197. * Comments:: How to include comments in C code.
  198. * Identifiers:: How to form identifiers (names).
  199. * Operators/Punctuation:: Characters used as operators or punctuation.
  200. * Line Continuation:: Splitting one line into multiple lines.
  201. * Digraphs:: Two-character substitutes for some characters.
  202. Arithmetic
  203. * Basic Arithmetic:: Addition, subtraction, multiplication,
  204. and division.
  205. * Integer Arithmetic:: How C performs arithmetic with integer values.
  206. * Integer Overflow:: When an integer value exceeds the range
  207. of its type.
  208. * Mixed Mode:: Calculating with both integer values
  209. and floating-point values.
  210. * Division and Remainder:: How integer division works.
  211. * Numeric Comparisons:: Comparing numeric values for
  212. equality or order.
  213. * Shift Operations:: Shift integer bits left or right.
  214. * Bitwise Operations:: Bitwise conjunction, disjunction, negation.
  215. Assignment Expressions
  216. * Simple Assignment:: The basics of storing a value.
  217. * Lvalues:: Expressions into which a value can be stored.
  218. * Modifying Assignment:: Shorthand for changing an lvalue's contents.
  219. * Increment/Decrement:: Shorthand for incrementing and decrementing
  220. an lvalue's contents.
  221. * Postincrement/Postdecrement:: Accessing then incrementing or decrementing.
  222. * Assignment in Subexpressions:: How to avoid ambiguity.
  223. * Write Assignments Separately:: Write assignments as separate statements.
  224. Execution Control Expressions
  225. * Logical Operators:: Logical conjunction, disjunction, negation.
  226. * Logicals and Comparison:: Logical operators with comparison operators.
  227. * Logicals and Assignments:: Assignments with logical operators.
  228. * Conditional Expression:: An if/else construct inside expressions.
  229. * Comma Operator:: Build a sequence of subexpressions.
  230. Order of Execution
  231. * Reordering of Operands:: Operations in C are not necessarily computed
  232. in the order they are written.
  233. * Associativity and Ordering:: Some associative operations are performed
  234. in a particular order; others are not.
  235. * Sequence Points:: Some guarantees about the order of operations.
  236. * Postincrement and Ordering:: Ambiguous execution order with postincrement.
  237. * Ordering of Operands:: Evaluation order of operands
  238. and function arguments.
  239. * Optimization and Ordering:: Compiler optimizations can reorder operations
  240. only if it has no impact on program results.
  241. Primitive Data Types
  242. * Integer Types:: Description of integer types.
  243. * Floating-Point Data Types:: Description of floating-point types.
  244. * Complex Data Types:: Description of complex number types.
  245. * The Void Type:: A type indicating no value at all.
  246. * Other Data Types:: A brief summary of other types.
  247. Constants
  248. * Integer Constants:: Literal integer values.
  249. * Integer Const Type:: Types of literal integer values.
  250. * Floating Constants:: Literal floating-point values.
  251. * Imaginary Constants:: Literal imaginary number values.
  252. * Invalid Numbers:: Avoiding preprocessing number misconceptions.
  253. * Character Constants:: Literal character values.
  254. * Unicode Character Codes:: Unicode characters represented
  255. in either UTF-16 or UTF-32.
  256. * Wide Character Constants:: Literal characters values larger than 8 bits.
  257. * String Constants:: Literal string values.
  258. * UTF-8 String Constants:: Literal UTF-8 string values.
  259. * Wide String Constants:: Literal string values made up of
  260. 16- or 32-bit characters.
  261. Pointers
  262. * Address of Data:: Using the ``address-of'' operator.
  263. * Pointer Types:: For each type, there is a pointer type.
  264. * Pointer Declarations:: Declaring variables with pointer types.
  265. * Pointer Type Designators:: Designators for pointer types.
  266. * Pointer Dereference:: Accessing what a pointer points at.
  267. * Null Pointers:: Pointers which do not point to any object.
  268. * Invalid Dereference:: Dereferencing null or invalid pointers.
  269. * Void Pointers:: Totally generic pointers, can cast to any.
  270. * Pointer Comparison:: Comparing memory address values.
  271. * Pointer Arithmetic:: Computing memory address values.
  272. * Pointers and Arrays:: Using pointer syntax instead of array syntax.
  273. * Low-Level Pointer Arithmetic:: More about computing memory address values.
  274. * Pointer Increment/Decrement:: Incrementing and decrementing pointers.
  275. * Pointer Arithmetic Drawbacks:: A common pointer bug to watch out for.
  276. * Pointer-Integer Conversion:: Converting pointer types to integer types.
  277. * Printing Pointers:: Using @code{printf} for a pointer's value.
  278. Structures
  279. * Referencing Fields:: Accessing field values in a structure object.
  280. * Dynamic Memory Allocation:: Allocating space for objects
  281. while the program is running.
  282. * Field Offset:: Memory layout of fields within a structure.
  283. * Structure Layout:: Planning the memory layout of fields.
  284. * Packed Structures:: Packing structure fields as close as possible.
  285. * Bit Fields:: Dividing integer fields
  286. into fields with fewer bits.
  287. * Bit Field Packing:: How bit fields pack together in integers.
  288. * const Fields:: Making structure fields immutable.
  289. * Zero Length:: Zero-length array as a variable-length object.
  290. * Flexible Array Fields:: Another approach to variable-length objects.
  291. * Overlaying Structures:: Casting one structure type
  292. over an object of another structure type.
  293. * Structure Assignment:: Assigning values to structure objects.
  294. * Unions:: Viewing the same object in different types.
  295. * Packing With Unions:: Using a union type to pack various types into
  296. the same memory space.
  297. * Cast to Union:: Casting a value one of the union's alternative
  298. types to the type of the union itself.
  299. * Structure Constructors:: Building new structure objects.
  300. * Unnamed Types as Fields:: Fields' types do not always need names.
  301. * Incomplete Types:: Types which have not been fully defined.
  302. * Intertwined Incomplete Types:: Defining mutually-recursive structure types.
  303. * Type Tags:: Scope of structure and union type tags.
  304. Arrays
  305. * Accessing Array Elements:: How to access individual elements of an array.
  306. * Declaring an Array:: How to name and reserve space for a new array.
  307. * Strings:: A string in C is a special case of array.
  308. * Incomplete Array Types:: Naming, but not allocating, a new array.
  309. * Limitations of C Arrays:: Arrays are not first-class objects.
  310. * Multidimensional Arrays:: Arrays of arrays.
  311. * Constructing Array Values:: Assigning values to an entire array at once.
  312. * Arrays of Variable Length:: Declaring arrays of non-constant size.
  313. Statements
  314. * Expression Statement:: Evaluate an expression, as a statement,
  315. usually done for a side effect.
  316. * if Statement:: Basic conditional execution.
  317. * if-else Statement:: Multiple branches for conditional execution.
  318. * Blocks:: Grouping multiple statements together.
  319. * return Statement:: Return a value from a function.
  320. * Loop Statements:: Repeatedly executing a statement or block.
  321. * switch Statement:: Multi-way conditional choices.
  322. * switch Example:: A plausible example of using @code{switch}.
  323. * Duffs Device:: A special way to use @code{switch}.
  324. * Case Ranges:: Ranges of values for @code{switch} cases.
  325. * Null Statement:: A statement that does nothing.
  326. * goto Statement:: Jump to another point in the source code,
  327. identified by a label.
  328. * Local Labels:: Labels with limited scope.
  329. * Labels as Values:: Getting the address of a label.
  330. * Statement Exprs:: A series of statements used as an expression.
  331. Variables
  332. * Variable Declarations:: Name a variable and and reserve space for it.
  333. * Initializers:: Assigning initial values to variables.
  334. * Designated Inits:: Assigning initial values to array elements
  335. at particular array indices.
  336. * Auto Type:: Obtaining the type of a variable.
  337. * Local Variables:: Variables declared in function definitions.
  338. * File-Scope Variables:: Variables declared outside of
  339. function definitions.
  340. * Static Local Variables:: Variables declared within functions,
  341. but with permanent storage allocation.
  342. * Extern Declarations:: Declaring a variable
  343. which is allocated somewhere else.
  344. * Allocating File-Scope:: When is space allocated
  345. for file-scope variables?
  346. * auto and register:: Historically used storage directions.
  347. * Omitting Types:: The bad practice of declaring variables
  348. with implicit type.
  349. Type Qualifiers
  350. * const:: Variables whose values don't change.
  351. * volatile:: Variables whose values may be accessed
  352. or changed outside of the control of
  353. this program.
  354. * restrict Pointers:: Restricted pointers for code optimization.
  355. * restrict Pointer Example:: Example of how that works.
  356. Functions
  357. * Function Definitions:: Writing the body of a function.
  358. * Function Declarations:: Declaring the interface of a function.
  359. * Function Calls:: Using functions.
  360. * Function Call Semantics:: Call-by-value argument passing.
  361. * Function Pointers:: Using references to functions.
  362. * The main Function:: Where execution of a GNU C program begins.
  363. Type Conversions
  364. * Explicit Type Conversion:: Casting a value from one type to another.
  365. * Assignment Type Conversions:: Automatic conversion by assignment operation.
  366. * Argument Promotions:: Automatic conversion of function parameters.
  367. * Operand Promotions:: Automatic conversion of arithmetic operands.
  368. * Common Type:: When operand types differ, which one is used?
  369. Scope
  370. * Scope:: Different categories of identifier scope.
  371. Preprocessing
  372. * Preproc Overview:: Introduction to the C preprocessor.
  373. * Directives:: The form of preprocessor directives.
  374. * Preprocessing Tokens:: The lexical elements of preprocessing.
  375. * Header Files:: Including one source file in another.
  376. * Macros:: Macro expansion by the preprocessor.
  377. * Conditionals:: Controlling whether to compile some lines
  378. or ignore them.
  379. * Diagnostics:: Reporting warnings and errors.
  380. * Line Control:: Reporting source line numbers.
  381. * Null Directive:: A preprocessing no-op.
  382. Integers in Depth
  383. * Integer Representations:: How integer values appear in memory.
  384. * Maximum and Minimum Values:: Value ranges of integer types.
  385. Floating Point in Depth
  386. * Floating Representations:: How floating-point values appear in memory.
  387. * Floating Type Specs:: Precise details of memory representations.
  388. * Special Float Values:: Infinity, Not a Number, and Subnormal Numbers.
  389. * Invalid Optimizations:: Don't mess up non-numbers and signed zeros.
  390. * Exception Flags:: Handling certain conditions in floating point.
  391. * Exact Floating-Point:: Not all floating calculations lose precision.
  392. * Rounding:: When a floating result can't be represented
  393. exactly in the floating-point type in use.
  394. * Rounding Issues:: Avoid magnifying rounding errors.
  395. * Significance Loss:: Subtracting numbers that are almost equal.
  396. * Fused Multiply-Add:: Taking advantage of a special floating-point
  397. instruction for faster execution.
  398. * Error Recovery:: Determining rounding errors.
  399. * Exact Floating Constants:: Precisely specified floating-point numbers.
  400. * Handling Infinity:: When floating calculation is out of range.
  401. * Handling NaN:: What floating calculation is undefined.
  402. * Signed Zeros:: Positive zero vs. negative zero.
  403. * Scaling by the Base:: A useful exact floating-point operation.
  404. * Rounding Control:: Specifying some rounding behaviors.
  405. * Machine Epsilon:: The smallest number you can add to 1.0
  406. and get a sum which is larger than 1.0.
  407. * Complex Arithmetic:: Details of arithmetic with complex numbers.
  408. * Round-Trip Base Conversion:: What happens between base-2 and base-10.
  409. * Further Reading:: References for floating-point numbers.
  410. Directing Compilation
  411. * Pragmas:: Controlling compilation of some constructs.
  412. * Static Assertions:: Compile-time tests for conditions.
  413. @end detailmenu
  414. @end menu
  415. @node The First Example
  416. @chapter The First Example
  417. This chapter presents the source code for a very simple C program and
  418. uses it to explain a few features of the language. If you already
  419. know the basic points of C presented in this chapter, you can skim it
  420. or skip it.
  421. We present examples of C source code (other than comments) using a
  422. fixed-width typeface, since that's the way they look when you edit
  423. them in an editor such as GNU Emacs.
  424. @menu
  425. * Recursive Fibonacci:: Writing a simple function recursively.
  426. * Stack:: Each function call uses space in the stack.
  427. * Iterative Fibonacci:: Writing the same function iteratively.
  428. @end menu
  429. @node Recursive Fibonacci
  430. @section Example: Recursive Fibonacci
  431. @cindex recursive Fibonacci function
  432. @cindex Fibonacci function, recursive
  433. To introduce the most basic features of C, let's look at code for a
  434. simple mathematical function that does calculations on integers. This
  435. function calculates the @var{n}th number in the Fibonacci series, in
  436. which each number is the sum of the previous two: 1, 1, 2, 3, 5, 8,
  437. 13, 21, 34, 55, @dots{}.
  438. @example
  439. int
  440. fib (int n)
  441. @{
  442. if (n <= 2) /* @r{This avoids infinite recursion.} */
  443. return 1;
  444. else
  445. return fib (n - 1) + fib (n - 2);
  446. @}
  447. @end example
  448. This very simple program illustrates several features of C:
  449. @itemize @bullet
  450. @item
  451. A function definition, whose first two lines constitute the function
  452. header. @xref{Function Definitions}.
  453. @item
  454. A function parameter @code{n}, referred to as the variable @code{n}
  455. inside the function body. @xref{Function Parameter Variables}.
  456. A function definition uses parameters to refer to the argument
  457. values provided in a call to that function.
  458. @item
  459. Arithmetic. C programs add with @samp{+} and subtract with
  460. @samp{-}. @xref{Arithmetic}.
  461. @item
  462. Numeric comparisons. The operator @samp{<=} tests for ``less than or
  463. equal.'' @xref{Numeric Comparisons}.
  464. @item
  465. Integer constants written in base 10.
  466. @xref{Integer Constants}.
  467. @item
  468. A function call. The function call @code{fib (n - 1)} calls the
  469. function @code{fib}, passing as its argument the value @code{n - 1}.
  470. @xref{Function Calls}.
  471. @item
  472. A comment, which starts with @samp{/*} and ends with @samp{*/}. The
  473. comment has no effect on the execution of the program. Its purpose is
  474. to provide explanations to people reading the source code. Including
  475. comments in the code is tremendously important---they provide
  476. background information so others can understand the code more quickly.
  477. @xref{Comments}.
  478. In this manual, we present comment text in the variable-width typeface
  479. used for the text of the chapters, not in the fixed-width typeface
  480. used for the rest of the code. That is to make comments easier to
  481. read. This distinction of typeface does not exist in a real file of C
  482. source code.
  483. @item
  484. Two kinds of statements, the @code{return} statement and the
  485. @code{if}@dots{}@code{else} statement. @xref{Statements}.
  486. @item
  487. Recursion. The function @code{fib} calls itself; that is called a
  488. @dfn{recursive call}. These are valid in C, and quite common.
  489. The @code{fib} function would not be useful if it didn't return.
  490. Thus, recursive definitions, to be of any use, must avoid
  491. @dfn{infinite recursion}.
  492. This function definition prevents infinite recursion by specially
  493. handling the case where @code{n} is two or less. Thus the maximum
  494. depth of recursive calls is less than @code{n}.
  495. @end itemize
  496. @menu
  497. * Function Header:: The function's name and how it is called.
  498. * Function Body:: Declarations and statements that implement the function.
  499. @end menu
  500. @node Function Header
  501. @subsection Function Header
  502. @cindex function header
  503. In our example, the first two lines of the function definition are the
  504. @dfn{header}. Its purpose is to state the function's name and say how
  505. it is called:
  506. @example
  507. int
  508. fib (int n)
  509. @end example
  510. @noindent
  511. says that the function returns an integer (type @code{int}), its name is
  512. @code{fib}, and it takes one argument named @code{n} which is also an
  513. integer. (Data types will be explained later, in @ref{Primitive Types}.)
  514. @node Function Body
  515. @subsection Function Body
  516. @cindex function body
  517. @cindex recursion
  518. The rest of the function definition is called the @dfn{function body}.
  519. Like every function body, this one starts with @samp{@{}, ends with
  520. @samp{@}}, and contains zero or more @dfn{statements} and
  521. @dfn{declarations}. Statements specify actions to take, whereas
  522. declarations define names of variables, functions, and so on. Each
  523. statement and each declaration ends with a semicolon (@samp{;}).
  524. Statements and declarations often contain @dfn{expressions}; an
  525. expression is a construct whose execution produces a @dfn{value} of
  526. some data type, but may also take actions through ``side effects''
  527. that alter subsequent execution. A statement, by contrast, does not
  528. have a value; it affects further execution of the program only through
  529. the actions it takes.
  530. This function body contains no declarations, and just one statement,
  531. but that one is a complex statement in that it contains nested
  532. statements. This function uses two kinds of statements:
  533. @table @code
  534. @item return
  535. The @code{return} statement makes the function return immediately.
  536. It looks like this:
  537. @example
  538. return @var{value};
  539. @end example
  540. Its meaning is to compute the expression @var{value} and exit the
  541. function, making it return whatever value that expression produced.
  542. For instance,
  543. @example
  544. return 1;
  545. @end example
  546. @noindent
  547. returns the integer 1 from the function, and
  548. @example
  549. return fib (n - 1) + fib (n - 2);
  550. @end example
  551. @noindent
  552. returns a value computed by performing two function calls
  553. as specified and adding their results.
  554. @item @code{if}@dots{}@code{else}
  555. The @code{if}@dots{}@code{else} statement is a @dfn{conditional}.
  556. Each time it executes, it chooses one of its two substatements to execute
  557. and ignores the other. It looks like this:
  558. @example
  559. if (@var{condition})
  560. @var{if-true-statement}
  561. else
  562. @var{if-false-statement}
  563. @end example
  564. Its meaning is to compute the expression @var{condition} and, if it's
  565. ``true,'' execute @var{if-true-statement}. Otherwise, execute
  566. @var{if-false-statement}. @xref{if-else Statement}.
  567. Inside the @code{if}@dots{}@code{else} statement, @var{condition} is
  568. simply an expression. It's considered ``true'' if its value is
  569. nonzero. (A comparison operation, such as @code{n <= 2}, produces the
  570. value 1 if it's ``true'' and 0 if it's ``false.'' @xref{Numeric
  571. Comparisons}.) Thus,
  572. @example
  573. if (n <= 2)
  574. return 1;
  575. else
  576. return fib (n - 1) + fib (n - 2);
  577. @end example
  578. @noindent
  579. first tests whether the value of @code{n} is less than or equal to 2.
  580. If so, the expression @code{n <= 2} has the value 1. So execution
  581. continues with the statement
  582. @example
  583. return 1;
  584. @end example
  585. @noindent
  586. Otherwise, execution continues with this statement:
  587. @example
  588. return fib (n - 1) + fib (n - 2);
  589. @end example
  590. Each of these statements ends the execution of the function and
  591. provides a value for it to return. @xref{return Statement}.
  592. @end table
  593. Calculating @code{fib} using ordinary integers in C works only for
  594. @var{n} < 47, because the value of @code{fib (47)} is too large to fit
  595. in type @code{int}. The addition operation that tries to add
  596. @code{fib (46)} and @code{fib (45)} cannot deliver the correct result.
  597. This occurrence is called @dfn{integer overflow}.
  598. Overflow can manifest itself in various ways, but one thing that can't
  599. possibly happen is to produce the correct value, since that can't fit
  600. in the space for the value. @xref{Integer Overflow}.
  601. @xref{Functions}, for a full explanation about functions.
  602. @node Stack
  603. @section The Stack, And Stack Overflow
  604. @cindex stack
  605. @cindex stack frame
  606. @cindex stack overflow
  607. @cindex recursion, drawbacks of
  608. @cindex stack frame
  609. Recursion has a drawback: there are limits to how many nested levels of
  610. function calls a program can make. In C, each function call allocates a block
  611. of memory which it uses until the call returns. C allocates these
  612. blocks consecutively within a large area of memory known as the
  613. @dfn{stack}, so we refer to the blocks as @dfn{stack frames}.
  614. The size of the stack is limited; if the program tries to use too
  615. much, that causes the program to fail because the stack is full. This
  616. is called @dfn{stack overflow}.
  617. @cindex crash
  618. @cindex segmentation fault
  619. Stack overflow on GNU/Linux typically manifests itself as the
  620. @dfn{signal} named @code{SIGSEGV}, also known as a ``segmentation
  621. fault.'' By default, this signal terminates the program immediately,
  622. rather than letting the program try to recover, or reach an expected
  623. ending point. (We commonly say in this case that the program
  624. ``crashes''). @xref{Signals}.
  625. It is inconvenient to observe a crash by passing too large
  626. an argument to recursive Fibonacci, because the program would run a
  627. long time before it crashes. This algorithm is simple but
  628. ridiculously slow: in calculating @code{fib (@var{n})}, the number of
  629. (recursive) calls @code{fib (1)} or @code{fib (2)} that it makes equals
  630. the final result.
  631. However, you can observe stack overflow very quickly if you use
  632. this function instead:
  633. @example
  634. int
  635. fill_stack (int n)
  636. @{
  637. if (n <= 1) /* @r{This limits the depth of recursion.} */
  638. return 1;
  639. else
  640. return fill_stack (n - 1);
  641. @}
  642. @end example
  643. Under gNewSense GNU/Linux on the Lemote Yeeloong, without optimization
  644. and using the default configuration, an experiment showed there is
  645. enough stack space to do 261906 nested calls to that function. One
  646. more, and the stack overflows and the program crashes. On another
  647. platform, with a different configuration, or with a different
  648. function, the limit might be bigger or smaller.
  649. @node Iterative Fibonacci
  650. @section Example: Iterative Fibonacci
  651. @cindex iterative Fibonacci function
  652. @cindex Fibonacci function, iterative
  653. Here's a much faster algorithm for computing the same Fibonacci
  654. series. It is faster for two reasons. First, it uses @dfn{iteration}
  655. (that is, repetition or looping) rather than recursion, so it doesn't
  656. take time for a large number of function calls. But mainly, it is
  657. faster because the number of repetitions is small---only @code{@var{n}}.
  658. @c If you change this, change the duplicate in node Example of for.
  659. @example
  660. int
  661. fib (int n)
  662. @{
  663. int last = 1; /* @r{Initial value is @code{fib (1)}.} */
  664. int prev = 0; /* @r{Initial value controls @code{fib (2)}.} */
  665. int i;
  666. for (i = 1; i < n; ++i)
  667. /* @r{If @code{n} is 1 or less, the loop runs zero times,} */
  668. /* @r{since @code{i < n} is false the first time.} */
  669. @{
  670. /* @r{Now @code{last} is @code{fib (@code{i})}}
  671. @r{and @code{prev} is @code{fib (@code{i} @minus{} 1)}.} */
  672. /* @r{Compute @code{fib (@code{i} + 1)}.} */
  673. int next = prev + last;
  674. /* @r{Shift the values down.} */
  675. prev = last;
  676. last = next;
  677. /* @r{Now @code{last} is @code{fib (@code{i} + 1)}}
  678. @r{and @code{prev} is @code{fib (@code{i})}.}
  679. @r{But that won't stay true for long,}
  680. @r{because we are about to increment @code{i}.} */
  681. @}
  682. return last;
  683. @}
  684. @end example
  685. This definition computes @code{fib (@var{n})} in a time proportional
  686. to @code{@var{n}}. The comments in the definition explain how it works: it
  687. advances through the series, always keeps the last two values in
  688. @code{last} and @code{prev}, and adds them to get the next value.
  689. Here are the additional C features that this definition uses:
  690. @table @asis
  691. @item Internal blocks
  692. Within a function, wherever a statement is called for, you can write a
  693. @dfn{block}. It looks like @code{@{ @r{@dots{}} @}} and contains zero or
  694. more statements and declarations. (You can also use additional
  695. blocks as statements in a block.)
  696. The function body also counts as a block, which is why it can contain
  697. statements and declarations.
  698. @xref{Blocks}.
  699. @item Declarations of local variables
  700. This function body contains declarations as well as statements. There
  701. are three declarations directly in the function body, as well as a
  702. fourth declaration in an internal block. Each starts with @code{int}
  703. because it declares a variable whose type is integer. One declaration
  704. can declare several variables, but each of these declarations is
  705. simple and declares just one variable.
  706. Variables declared inside a block (either a function body or an
  707. internal block) are @dfn{local variables}. These variables exist only
  708. within that block; their names are not defined outside the block, and
  709. exiting the block deallocates their storage. This example declares
  710. four local variables: @code{last}, @code{prev}, @code{i}, and
  711. @code{next}.
  712. The most basic local variable declaration looks like this:
  713. @example
  714. @var{type} @var{variablename};
  715. @end example
  716. For instance,
  717. @example
  718. int i;
  719. @end example
  720. @noindent
  721. declares the local variable @code{i} as an integer.
  722. @xref{Variable Declarations}.
  723. @item Initializers
  724. When you declare a variable, you can also specify its initial value,
  725. like this:
  726. @example
  727. @var{type} @var{variablename} = @var{value};
  728. @end example
  729. For instance,
  730. @example
  731. int last = 1;
  732. @end example
  733. @noindent
  734. declares the local variable @code{last} as an integer (type
  735. @code{int}) and starts it off with the value 1. @xref{Initializers}.
  736. @item Assignment
  737. Assignment: a specific kind of expression, written with the @samp{=}
  738. operator, that stores a new value in a variable or other place. Thus,
  739. @example
  740. @var{variable} = @var{value}
  741. @end example
  742. @noindent
  743. is an expression that computes @code{@var{value}} and stores the value in
  744. @code{@var{variable}}. @xref{Assignment Expressions}.
  745. @item Expression statements
  746. An expression statement is an expression followed by a semicolon.
  747. That computes the value of the expression, then ignores the value.
  748. An expression statement is useful when the expression changes some
  749. data or has other side effects---for instance, with function calls, or
  750. with assignments as in this example. @xref{Expression Statement}.
  751. Using an expression with no side effects in an expression statement is
  752. pointless except in very special cases. For instance, the expression
  753. statement @code{x;} would examine the value of @code{x} and ignore it.
  754. That is not useful.
  755. @item Increment operator
  756. The increment operator is @samp{++}. @code{++i} is an
  757. expression that is short for @code{i = i + 1}.
  758. @xref{Increment/Decrement}.
  759. @item @code{for} statements
  760. A @code{for} statement is a clean way of executing a statement
  761. repeatedly---a @dfn{loop} (@pxref{Loop Statements}). Specifically,
  762. @example
  763. for (i = 1; i < n; ++i)
  764. @var{body}
  765. @end example
  766. @noindent
  767. means to start by doing @code{i = 1} (set @code{i} to one) to prepare
  768. for the loop. The loop itself consists of
  769. @itemize @bullet
  770. @item
  771. Testing @code{i < n} and exiting the loop if that's false.
  772. @item
  773. Executing @var{body}.
  774. @item
  775. Advancing the loop (executing @code{++i}, which increments @code{i}).
  776. @end itemize
  777. The net result is to execute @var{body} with 1 in @code{i},
  778. then with 2 in @code{i}, and so on, stopping just before the repetition
  779. where @code{i} would equal @code{n}. If @code{n} is less than 1,
  780. the loop will execute the body zero times.
  781. The body of the @code{for} statement must be one and only one
  782. statement. You can't write two statements in a row there; if you try
  783. to, only the first of them will be treated as part of the loop.
  784. The way to put multiple statements in such a place is to group them
  785. with a block, and that's what we do in this example.
  786. @end table
  787. @node Complete Program
  788. @chapter A Complete Program
  789. @cindex complete example program
  790. @cindex example program, complete
  791. It's all very well to write a Fibonacci function, but you cannot run
  792. it by itself. It is a useful program, but it is not a complete
  793. program.
  794. In this chapter we present a complete program that contains the
  795. @code{fib} function. This example shows how to make the program
  796. start, how to make it finish, how to do computation, and how to print
  797. a result.
  798. @menu
  799. * Complete Example:: Turn the simple function into a full program.
  800. * Complete Explanation:: Explanation of each part of the example.
  801. * Complete Line-by-Line:: Explaining each line of the example.
  802. * Compile Example:: Using GCC to compile the example.
  803. @end menu
  804. @node Complete Example
  805. @section Complete Program Example
  806. Here is the complete program that uses the simple, recursive version
  807. of the @code{fib} function (@pxref{Recursive Fibonacci}):
  808. @example
  809. #include <stdio.h>
  810. int
  811. fib (int n)
  812. @{
  813. if (n <= 2) /* @r{This avoids infinite recursion.} */
  814. return 1;
  815. else
  816. return fib (n - 1) + fib (n - 2);
  817. @}
  818. int
  819. main (void)
  820. @{
  821. printf ("Fibonacci series item %d is %d\n",
  822. 20, fib (20));
  823. return 0;
  824. @}
  825. @end example
  826. @noindent
  827. This program prints a message that shows the value of @code{fib (20)}.
  828. Now for an explanation of what that code means.
  829. @node Complete Explanation
  830. @section Complete Program Explanation
  831. @ifnottex
  832. Here's the explanation of the code of the example in the
  833. previous section.
  834. @end ifnottex
  835. This sample program prints a message that shows the value of @code{fib
  836. (20)}, and exits with code 0 (which stands for successful execution).
  837. Every C program is started by running the function named @code{main}.
  838. Therefore, the example program defines a function named @code{main} to
  839. provide a way to start it. Whatever that function does is what the
  840. program does. @xref{The main Function}.
  841. The @code{main} function is the first one called when the program
  842. runs, but it doesn't come first in the example code. The order of the
  843. function definitions in the source code makes no difference to the
  844. program's meaning.
  845. The initial call to @code{main} always passes certain arguments, but
  846. @code{main} does not have to pay attention to them. To ignore those
  847. arguments, define @code{main} with @code{void} as the parameter list.
  848. (@code{void} as a function's parameter list normally means ``call with
  849. no arguments,'' but @code{main} is a special case.)
  850. The function @code{main} returns 0 because that is
  851. the conventional way for @code{main} to indicate successful execution.
  852. It could instead return a positive integer to indicate failure, and
  853. some utility programs have specific conventions for the meaning of
  854. certain numeric @dfn{failure codes}. @xref{Values from main}.
  855. @cindex @code{printf}
  856. The simplest way to print text in C is by calling the @code{printf}
  857. function, so here we explain very briefly what that function does.
  858. For a full explanation of @code{printf} and the other standard I/O
  859. functions, see @ref{I/O on Streams, The GNU C Library, , libc, The GNU
  860. C Library Reference Manual}.
  861. @cindex standard output
  862. The first argument to @code{printf} is a @dfn{string constant}
  863. (@pxref{String Constants}) that is a template for output. The
  864. function @code{printf} copies most of that string directly as output,
  865. including the newline character at the end of the string, which is
  866. written as @samp{\n}. The output goes to the program's @dfn{standard
  867. output} destination, which in the usual case is the terminal.
  868. @samp{%} in the template introduces a code that substitutes other text
  869. into the output. Specifically, @samp{%d} means to take the next
  870. argument to @code{printf} and substitute it into the text as a decimal
  871. number. (The argument for @samp{%d} must be of type @code{int}; if it
  872. isn't, @code{printf} will malfunction.) So the output is a line that
  873. looks like this:
  874. @example
  875. Fibonacci series item 20 is 6765
  876. @end example
  877. This program does not contain a definition for @code{printf} because
  878. it is defined by the C library, which makes it available in all C
  879. programs. However, each program does need to @dfn{declare}
  880. @code{printf} so it will be called correctly. The @code{#include}
  881. line takes care of that; it includes a @dfn{header file} called
  882. @file{stdio.h} into the program's code. That file is provided by the
  883. operating system and it contains declarations for the many standard
  884. input/output functions in the C library, one of which is
  885. @code{printf}.
  886. Don't worry about header files for now; we'll explain them later in
  887. @ref{Header Files}.
  888. The first argument of @code{printf} does not have to be a string
  889. constant; it can be any string (@pxref{Strings}). However, using a
  890. constant is the most common case.
  891. @node Complete Line-by-Line
  892. @section Complete Program, Line by Line
  893. Here's the same example, explained line by line.
  894. @strong{Beginners, do you find this helpful or not?
  895. Would you prefer a different layout for the example?
  896. Please tell rms@@gnu.org.}
  897. @example
  898. #include <stdio.h> /* @r{Include declaration of usual} */
  899. /* @r{I/O functions such as @code{printf}.} */
  900. /* @r{Most programs need these.} */
  901. int /* @r{This function returns an @code{int}.} */
  902. fib (int n) /* @r{Its name is @code{fib};} */
  903. /* @r{its argument is called @code{n}.} */
  904. @{ /* @r{Start of function body.} */
  905. /* @r{This stops the recursion from being infinite.} */
  906. if (n <= 2) /* @r{If @code{n} is 1 or 2,} */
  907. return 1; /* @r{make @code{fib} return 1.} */
  908. else /* @r{otherwise, add the two previous} */
  909. /* @r{Fibonacci numbers.} */
  910. return fib (n - 1) + fib (n - 2);
  911. @}
  912. int /* @r{This function returns an @code{int}.} */
  913. main (void) /* @r{Start here; ignore arguments.} */
  914. @{ /* @r{Print message with numbers in it.} */
  915. printf ("Fibonacci series item %d is %d\n",
  916. 20, fib (20));
  917. return 0; /* @r{Terminate program, report success.} */
  918. @}
  919. @end example
  920. @node Compile Example
  921. @section Compiling the Example Program
  922. @cindex compiling
  923. @cindex executable file
  924. To run a C program requires converting the source code into an
  925. @dfn{executable file}. This is called @dfn{compiling} the program,
  926. and the command to do that using GNU C is @command{gcc}.
  927. This example program consists of a single source file. If we
  928. call that file @file{fib1.c}, the complete command to compile it is
  929. this:
  930. @example
  931. gcc -g -O -o fib1 fib1.c
  932. @end example
  933. @noindent
  934. Here, @option{-g} says to generate debugging information, @option{-O}
  935. says to optimize at the basic level, and @option{-o fib1} says to put
  936. the executable program in the file @file{fib1}.
  937. To run the program, use its file name as a shell command.
  938. For instance,
  939. @example
  940. ./fib1
  941. @end example
  942. @noindent
  943. However, unless you are sure the program is correct, you should
  944. expect to need to debug it. So use this command,
  945. @example
  946. gdb fib1
  947. @end example
  948. @noindent
  949. which starts the GDB debugger (@pxref{Sample Session, Sample Session,
  950. A Sample GDB Session, gdb, Debugging with GDB}) so you can run and
  951. debug the executable program @code{fib1}.
  952. Richard Stallman's advice, from personal experience, is to turn to the
  953. debugger as soon as you can reproduce the problem. Don't try to avoid
  954. it by using other methods instead---occasionally they are shortcuts,
  955. but usually they waste an unbounded amount of time. With the
  956. debugger, you will surely find the bug in a reasonable time; overall,
  957. you will get your work done faster. The sooner you get serious and
  958. start the debugger, the sooner you are likely to find the bug.
  959. @xref{Compilation}, for an introduction to compiling more complex
  960. programs which consist of more than one source file.
  961. @node Storage
  962. @chapter Storage and Data
  963. @cindex bytes
  964. @cindex storage organization
  965. @cindex memory organization
  966. Storage in C programs is made up of units called @dfn{bytes}. On
  967. nearly all computers, a byte consists of 8 bits, but there are a few
  968. peculiar computers (mostly ``embedded controllers'' for very small
  969. systems) where a byte is longer than that. This manual does not try
  970. to explain the peculiarity of those computers; we assume that a byte
  971. is 8 bits.
  972. Every C data type is made up of a certain number of bytes; that number
  973. is the data type's @dfn{size}. @xref{Type Size}, for details. The
  974. types @code{signed char} and @code{unsigned char} are one byte long;
  975. use those types to operate on data byte by byte. @xref{Signed and
  976. Unsigned Types}. You can refer to a series of consecutive bytes as an
  977. array of @code{char} elements; that's what an ASCII string looks like
  978. in memory. @xref{String Constants}.
  979. @node Beyond Integers
  980. @chapter Beyond Integers
  981. So far we've presented programs that operate on integers. In this
  982. chapter we'll present examples of handling non-integral numbers and
  983. arrays of numbers.
  984. @menu
  985. * Float Example:: A function that uses floating-point numbers.
  986. * Array Example:: A function that works with arrays.
  987. * Array Example Call:: How to call that function.
  988. * Array Example Variations:: Different ways to write the call example.
  989. @end menu
  990. @node Float Example
  991. @section An Example with Non-Integer Numbers
  992. @cindex floating point example
  993. Here's a function that operates on and returns @dfn{floating point}
  994. numbers that don't have to be integers. Floating point represents a
  995. number as a fraction together with a power of 2. (For more detail,
  996. @pxref{Floating-Point Data Types}.) This example calculates the
  997. average of three floating point numbers that are passed to it as
  998. arguments:
  999. @example
  1000. double
  1001. average_of_three (double a, double b, double c)
  1002. @{
  1003. return (a + b + c) / 3;
  1004. @}
  1005. @end example
  1006. The values of the parameter @var{a}, @var{b} and @var{c} do not have to be
  1007. integers, and even when they happen to be integers, most likely their
  1008. average is not an integer.
  1009. @code{double} is the usual data type in C for calculations on
  1010. floating-point numbers.
  1011. To print a @code{double} with @code{printf}, we must use @samp{%f}
  1012. instead of @samp{%d}:
  1013. @example
  1014. printf ("Average is %f\n",
  1015. average_of_three (1.1, 9.8, 3.62));
  1016. @end example
  1017. The code that calls @code{printf} must pass a @code{double} for
  1018. printing with @samp{%f} and an @code{int} for printing with @samp{%d}.
  1019. If the argument has the wrong type, @code{printf} will produce garbage
  1020. output.
  1021. Here's a complete program that computes the average of three
  1022. specific numbers and prints the result:
  1023. @example
  1024. double
  1025. average_of_three (double a, double b, double c)
  1026. @{
  1027. return (a + b + c) / 3;
  1028. @}
  1029. int
  1030. main (void)
  1031. @{
  1032. printf ("Average is %f\n",
  1033. average_of_three (1.1, 9.8, 3.62));
  1034. return 0;
  1035. @}
  1036. @end example
  1037. From now on we will not present examples of calls to @code{main}.
  1038. Instead we encourage you to write them for yourself when you want
  1039. to test executing some code.
  1040. @node Array Example
  1041. @section An Example with Arrays
  1042. @cindex array example
  1043. A function to take the average of three numbers is very specific and
  1044. limited. A more general function would take the average of any number
  1045. of numbers. That requires passing the numbers in an array. An array
  1046. is an object in memory that contains a series of values of the same
  1047. data type. This chapter presents the basic concepts and use of arrays
  1048. through an example; for the full explanation, see @ref{Arrays}.
  1049. Here's a function definition to take the average of several
  1050. floating-point numbers, passed as type @code{double}. The first
  1051. parameter, @code{length}, specifies how many numbers are passed. The
  1052. second parameter, @code{input_data}, is an array that holds those
  1053. numbers.
  1054. @example
  1055. double
  1056. avg_of_double (int length, double input_data[])
  1057. @{
  1058. double sum = 0;
  1059. int i;
  1060. for (i = 0; i < length; i++)
  1061. sum = sum + input_data[i];
  1062. return sum / length;
  1063. @}
  1064. @end example
  1065. This introduces the expression to refer to an element of an array:
  1066. @code{input_data[i]} means the element at index @code{i} in
  1067. @code{input_data}. The index of the element can be any expression
  1068. with an integer value; in this case, the expression is @code{i}.
  1069. @xref{Accessing Array Elements}.
  1070. @cindex zero-origin indexing
  1071. The lowest valid index in an array is 0, @emph{not} 1, and the highest
  1072. valid index is one less than the number of elements. (This is known
  1073. as @dfn{zero-origin indexing}.)
  1074. This example also introduces the way to declare that a function
  1075. parameter is an array. Such declarations are modeled after the syntax
  1076. for an element of the array. Just as @code{double foo} declares that
  1077. @code{foo} is of type @code{double}, @code{double input_data[]}
  1078. declares that each element of @code{input_data} is of type
  1079. @code{double}. Therefore, @code{input_data} itself has type ``array
  1080. of @code{double}.''
  1081. When declaring an array parameter, it's not necessary to say how long
  1082. the array is. In this case, the parameter @code{input_data} has no
  1083. length information. That's why the function needs another parameter,
  1084. @code{length}, for the caller to provide that information to the
  1085. function @code{avg_of_double}.
  1086. @node Array Example Call
  1087. @section Calling the Array Example
  1088. To call the function @code{avg_of_double} requires making an
  1089. array and then passing it as an argument. Here is an example.
  1090. @example
  1091. @{
  1092. /* @r{The array of values to average.} */
  1093. double nums_to_average[5];
  1094. /* @r{The average, once we compute it.} */
  1095. double average;
  1096. /* @r{Fill in elements of @code{nums_to_average}.} */
  1097. nums_to_average[0] = 58.7;
  1098. nums_to_average[1] = 5.1;
  1099. nums_to_average[2] = 7.7;
  1100. nums_to_average[3] = 105.2;
  1101. nums_to_average[4] = -3.14159;
  1102. average = avg_of_double (5, nums_to_average);
  1103. /* @r{@dots{}now make use of @code{average}@dots{}} */
  1104. @}
  1105. @end example
  1106. This shows an array subscripting expression again, this time
  1107. on the left side of an assignment, storing a value into an
  1108. element of an array.
  1109. It also shows how to declare a local variable that is an array:
  1110. @code{double nums_to_average[5];}. Since this declaration allocates the
  1111. space for the array, it needs to know the array's length. You can
  1112. specify the length with any expression whose value is an integer, but
  1113. in this declaration the length is a constant, the integer 5.
  1114. The name of the array, when used by itself as an expression, stands
  1115. for the address of the array's data, and that's what gets passed to
  1116. the function @code{avg_of_double} in @code{avg_of_double (5,
  1117. nums_to_average)}.
  1118. We can make the code easier to maintain by avoiding the need to write
  1119. 5, the array length, when calling @code{avg_of_double}. That way, if
  1120. we change the array to include more elements, we won't have to change
  1121. that call. One way to do this is with the @code{sizeof} operator:
  1122. @example
  1123. average = avg_of_double ((sizeof (nums_to_average)
  1124. / sizeof (nums_to_average[0])),
  1125. nums_to_average);
  1126. @end example
  1127. This computes the number of elements in @code{nums_to_average} by dividing
  1128. its total size by the size of one element. @xref{Type Size}, for more
  1129. details of using @code{sizeof}.
  1130. We don't show in this example what happens after storing the result of
  1131. @code{avg_of_double} in the variable @code{average}. Presumably
  1132. more code would follow that uses that result somehow. (Why compute
  1133. the average and not use it?) But that isn't part of this topic.
  1134. @node Array Example Variations
  1135. @section Variations for Array Example
  1136. The code to call @code{avg_of_double} has two declarations that
  1137. start with the same data type:
  1138. @example
  1139. /* @r{The array of values to average.} */
  1140. double nums_to_average[5];
  1141. /* @r{The average, once we compute it.} */
  1142. double average;
  1143. @end example
  1144. In C, you can combine the two, like this:
  1145. @example
  1146. double nums_to_average[5], average;
  1147. @end example
  1148. This declares @code{nums_to_average} so each of its elements is a
  1149. @code{double}, and @code{average} so that it simply is a
  1150. @code{double}.
  1151. However, while you @emph{can} combine them, that doesn't mean you
  1152. @emph{should}. If it is useful to write comments about the variables,
  1153. and usually it is, then it's clearer to keep the declarations separate
  1154. so you can put a comment on each one.
  1155. We set all of the elements of the array @code{nums_to_average} with
  1156. assignments, but it is more convenient to use an initializer in the
  1157. declaration:
  1158. @example
  1159. @{
  1160. /* @r{The array of values to average.} */
  1161. double nums_to_average[]
  1162. = @{ 58.7, 5.1, 7.7, 105.2, -3.14159 @};
  1163. /* @r{The average, once we compute it.} */
  1164. average = avg_of_double ((sizeof (nums_to_average)
  1165. / sizeof (nums_to_average[0])),
  1166. nums_to_average);
  1167. /* @r{@dots{}now make use of @code{average}@dots{}} */
  1168. @}
  1169. @end example
  1170. The array initializer is a comma-separated list of values, delimited
  1171. by braces. @xref{Initializers}.
  1172. Note that the declaration does not specify a size for
  1173. @code{nums_to_average}, so the size is determined from the
  1174. initializer. There are five values in the initializer, so
  1175. @code{nums_to_average} gets length 5. If we add another element to
  1176. the initializer, @code{nums_to_average} will have six elements.
  1177. Because the code computes the number of elements from the size of
  1178. the array, using @code{sizeof}, the program will operate on all the
  1179. elements in the initializer, regardless of how many those are.
  1180. @node Lexical Syntax
  1181. @chapter Lexical Syntax
  1182. @cindex lexical syntax
  1183. @cindex token
  1184. To start the full description of the C language, we explain the
  1185. lexical syntax and lexical units of C code. The lexical units of a
  1186. programming language are known as @dfn{tokens}. This chapter covers
  1187. all the tokens of C except for constants, which are covered in a later
  1188. chapter (@pxref{Constants}). One vital kind of token is the
  1189. @dfn{identifier} (@pxref{Identifiers}), which is used for names of any
  1190. kind.
  1191. @menu
  1192. * English:: Write programs in English!
  1193. * Characters:: The characters allowed in C programs.
  1194. * Whitespace:: The particulars of whitespace characters.
  1195. * Comments:: How to include comments in C code.
  1196. * Identifiers:: How to form identifiers (names).
  1197. * Operators/Punctuation:: Characters used as operators or punctuation.
  1198. * Line Continuation:: Splitting one line into multiple lines.
  1199. @end menu
  1200. @node English
  1201. @section Write Programs in English!
  1202. In principle, you can write the function and variable names in a
  1203. program, and the comments, in any human language. C allows any kinds
  1204. of characters in comments, and you can put non-ASCII characters into
  1205. identifiers with a special prefix. However, to enable programmers in
  1206. all countries to understand and develop the program, it is best given
  1207. today's circumstances to write identifiers and comments in
  1208. English.
  1209. English is the one language that programmers in all countries
  1210. generally study. If a program's names are in English, most
  1211. programmers in Bangladesh, Belgium, Bolivia, Brazil, and Bulgaria can
  1212. understand them. Most programmers in those countries can speak
  1213. English, or at least read it, but they do not read each other's
  1214. languages at all. In India, with so many languages, two programmers
  1215. may have no common language other than English.
  1216. If you don't feel confident in writing English, do the best you can,
  1217. and follow each English comment with a version in a language you
  1218. write better; add a note asking others to translate that to English.
  1219. Someone will eventually do that.
  1220. The program's user interface is a different matter. We don't need to
  1221. choose one language for that; it is easy to support multiple languages
  1222. and let each user choose the language to use. This requires writing
  1223. the program to support localization of its interface. (The
  1224. @code{gettext} package exists to support this; @pxref{Message
  1225. Translation, The GNU C Library, , libc, The GNU C Library Reference
  1226. Manual}.) Then a community-based translation effort can provide
  1227. support for all the languages users want to use.
  1228. @node Characters
  1229. @section Characters
  1230. @cindex character set
  1231. @cindex Unicode
  1232. @c ??? How to express ¶?
  1233. GNU C source files are usually written in the
  1234. @url{https://en.wikipedia.org/wiki/ASCII,,ASCII} character set, which
  1235. was defined in the 1960s for English. However, they can also include
  1236. Unicode characters represented in the
  1237. @url{https://en.wikipedia.org/wiki/UTF-8,,UTF-8} multibyte encoding.
  1238. This makes it possible to represent accented letters such as @samp{á},
  1239. as well as other scripts such as Arabic, Chinese, Cyrillic, Hebrew,
  1240. Japanese, and Korean.@footnote{On some obscure systems, GNU C uses
  1241. UTF-EBCDIC instead of UTF-8, but that is not worth describing in this
  1242. manual.}
  1243. In C source code, non-ASCII characters are valid in comments, in wide
  1244. character constants (@pxref{Wide Character Constants}), and in string
  1245. constants (@pxref{String Constants}).
  1246. @c ??? valid in identifiers?
  1247. Another way to specify non-ASCII characters in constants (character or
  1248. string) and identifiers is with an escape sequence starting with
  1249. backslash, specifying the intended Unicode character. (@xref{Unicode
  1250. Character Codes}.) This specifies non-ASCII characters without
  1251. putting a real non-ASCII character in the source file itself.
  1252. C accepts two-character aliases called @dfn{digraphs} for certain
  1253. characters. @xref{Digraphs}.
  1254. @node Whitespace
  1255. @section Whitespace
  1256. @cindex whitespace characters in source files
  1257. @cindex space character in source
  1258. @cindex tab character in source
  1259. @cindex formfeed in source
  1260. @cindex linefeed in source
  1261. @cindex newline in source
  1262. @cindex carriage return in source
  1263. @cindex vertical tab in source
  1264. Whitespace means characters that exist in a file but appear blank in a
  1265. printed listing of a file (or traditionally did appear blank, several
  1266. decades ago). The C language requires whitespace in order to separate
  1267. two consecutive identifiers, or to separate an identifier from a
  1268. numeric constant. Other than that, and a few special situations
  1269. described later, whitespace is optional; you can put it in when you
  1270. wish, to make the code easier to read.
  1271. Space and tab in C code are treated as whitespace characters. So are
  1272. line breaks. You can represent a line break with the newline
  1273. character (also called @dfn{linefeed} or LF), CR (carriage return), or
  1274. the CRLF sequence (two characters: carriage return followed by a
  1275. newline character).
  1276. The @dfn{formfeed} character, Control-L, was traditionally used to
  1277. divide a file into pages. It is still used this way in source code,
  1278. and the tools that generate nice printouts of source code still start
  1279. a new page after each ``formfeed'' character. Dividing code into
  1280. pages separated by formfeed characters is a good way to break it up
  1281. into comprehensible pieces and show other programmers where they start
  1282. and end.
  1283. The @dfn{vertical tab} character, Control-K, was traditionally used to
  1284. make printing advance down to the next section of a page. We know of
  1285. no particular reason to use it in source code, but it is still
  1286. accepted as whitespace in C.
  1287. Comments are also syntactically equivalent to whitespace.
  1288. @ifinfo
  1289. @xref{Comments}.
  1290. @end ifinfo
  1291. @node Comments
  1292. @section Comments
  1293. @cindex comments
  1294. A comment encapsulates text that has no effect on the program's
  1295. execution or meaning.
  1296. The purpose of comments is to explain the code to people that read it.
  1297. Writing good comments for your code is tremendously important---they
  1298. should provide background information that helps programmers
  1299. understand the reasons why the code is written the way it is. You,
  1300. returning to the code six months from now, will need the help of these
  1301. comments to remember why you wrote it this way.
  1302. Outdated comments that become incorrect are counterproductive, so part
  1303. of the software developer's responsibility is to update comments as
  1304. needed to correspond with changes to the program code.
  1305. C allows two kinds of comment syntax, the traditional style and the
  1306. C@t{++} style. A traditional C comment starts with @samp{/*} and ends
  1307. with @samp{*/}. For instance,
  1308. @example
  1309. /* @r{This is a comment in traditional C syntax.} */
  1310. @end example
  1311. A traditional comment can contain @samp{/*}, but these delimiters do
  1312. not nest as pairs. The first @samp{*/} ends the comment regardless of
  1313. whether it contains @samp{/*} sequences.
  1314. @example
  1315. /* @r{This} /* @r{is a comment} */ But this is not! */
  1316. @end example
  1317. A @dfn{line comment} starts with @samp{//} and ends at the end of the line.
  1318. For instance,
  1319. @example
  1320. // @r{This is a comment in C@t{++} style.}
  1321. @end example
  1322. Line comments do nest, in effect, because @samp{//} inside a line
  1323. comment is part of that comment:
  1324. @example
  1325. // @r{this whole line is} // @r{one comment}
  1326. This is code, not comment.
  1327. @end example
  1328. It is safe to put line comments inside block comments, or vice versa.
  1329. @example
  1330. @group
  1331. /* @r{traditional comment}
  1332. // @r{contains line comment}
  1333. @r{more traditional comment}
  1334. */ text here is not a comment
  1335. // @r{line comment} /* @r{contains traditional comment} */
  1336. @end group
  1337. @end example
  1338. But beware of commenting out one end of a traditional comment with a line
  1339. comment. The delimiter @samp{/*} doesn't start a comment if it occurs
  1340. inside an already-started comment.
  1341. @example
  1342. @group
  1343. // @r{line comment} /* @r{That would ordinarily begin a block comment.}
  1344. Oops! The line comment has ended;
  1345. this isn't a comment any more. */
  1346. @end group
  1347. @end example
  1348. Comments are not recognized within string constants. @t{@w{"/* blah
  1349. */"}} is the string constant @samp{@w{/* blah */}}, not an empty
  1350. string.
  1351. In this manual we show the text in comments in a variable-width font,
  1352. for readability, but this font distinction does not exist in source
  1353. files.
  1354. A comment is syntactically equivalent to whitespace, so it always
  1355. separates tokens. Thus,
  1356. @example
  1357. @group
  1358. int/* @r{comment} */foo;
  1359. @r{is equivalent to}
  1360. int foo;
  1361. @end group
  1362. @end example
  1363. @noindent
  1364. but clean code always uses real whitespace to separate the comment
  1365. visually from surrounding code.
  1366. @node Identifiers
  1367. @section Identifiers
  1368. @cindex identifiers
  1369. An @dfn{identifier} (name) in C is a sequence of letters and digits,
  1370. as well as @samp{_}, that does not start with a digit. Most compilers
  1371. also allow @samp{$}. An identifier can be as long as you like; for
  1372. example,
  1373. @example
  1374. int anti_dis_establishment_arian_ism;
  1375. @end example
  1376. @cindex case of letters in identifiers
  1377. Letters in identifiers are case-sensitive in C; thus, @code{a}
  1378. and @code{A} are two different identifiers.
  1379. @cindex keyword
  1380. @cindex reserved words
  1381. Identifiers in C are used as variable names, function names, typedef
  1382. names, enumeration constants, type tags, field names, and labels.
  1383. Certain identifiers in C are @dfn{keywords}, which means they have
  1384. specific syntactic meanings. Keywords in C are @dfn{reserved words},
  1385. meaning you cannot use them in any other way. For instance, you can't
  1386. define a variable or function named @code{return} or @code{if}.
  1387. You can also include other characters, even non-ASCII characters, in
  1388. identifiers by writing their Unicode character names, which start with
  1389. @samp{\u} or @samp{\U}, in the identifier name. @xref{Unicode
  1390. Character Codes}. However, it is usually a bad idea to use non-ASCII
  1391. characters in identifiers, and when they are written in English, they
  1392. never need non-ASCII characters. @xref{English}.
  1393. Whitespace is required to separate two consecutive identifiers, or to
  1394. separate an identifier from a preceding or following numeric
  1395. constant.
  1396. @node Operators/Punctuation
  1397. @section Operators and Punctuation
  1398. @cindex operators
  1399. @cindex punctuation
  1400. Here we describe the lexical syntax of operators and punctuation in C.
  1401. The specific operators of C and their meanings are presented in
  1402. subsequent chapters.
  1403. Most operators in C consist of one or two characters that can't be
  1404. used in identifiers. The characters used for operators in C are
  1405. @samp{!~^&|*/%+-=<>,.?:}.
  1406. Some operators are a single character. For instance, @samp{-} is the
  1407. operator for negation (with one operand) and the operator for
  1408. subtraction (with two operands).
  1409. Some operators are two characters. For example, @samp{++} is the
  1410. increment operator. Recognition of multicharacter operators works by
  1411. grouping together as many consecutive characters as can constitute one
  1412. operator.
  1413. For instance, the character sequence @samp{++} is always interpreted
  1414. as the increment operator; therefore, if we want to write two
  1415. consecutive instances of the operator @samp{+}, we must separate them
  1416. with a space so that they do not combine as one token. Applying the
  1417. same rule, @code{a+++++b} is always tokenized as @code{@w{a++ ++ +
  1418. b}}, not as @code{@w{a++ + ++b}}, even though the latter could be part
  1419. of a valid C program and the former could not (since @code{a++}
  1420. is not an lvalue and thus can't be the operand of @code{++}).
  1421. A few C operators are keywords rather than special characters. They
  1422. include @code{sizeof} (@pxref{Type Size}) and @code{_Alignof}
  1423. (@pxref{Type Alignment}).
  1424. The characters @samp{;@{@}[]()} are used for punctuation and grouping.
  1425. Semicolon (@samp{;}) ends a statement. Braces (@samp{@{} and
  1426. @samp{@}}) begin and end a block at the statement level
  1427. (@pxref{Blocks}), and surround the initializer (@pxref{Initializers})
  1428. for a variable with multiple elements or components (such as arrays or
  1429. structures).
  1430. Square brackets (@samp{[} and @samp{]}) do array indexing, as in
  1431. @code{array[5]}.
  1432. Parentheses are used in expressions for explicit nesting of
  1433. expressions (@pxref{Basic Arithmetic}), around the parameter
  1434. declarations in a function declaration or definition, and around the
  1435. arguments in a function call, as in @code{printf ("Foo %d\n", i)}
  1436. (@pxref{Function Calls}). Several kinds of statements also use
  1437. parentheses as part of their syntax---for instance, @code{if}
  1438. statements, @code{for} statements, @code{while} statements, and
  1439. @code{switch} statements. @xref{if Statement}, and following
  1440. sections.
  1441. Parentheses are also required around the operand of the operator
  1442. keywords @code{sizeof} and @code{_Alignof} when the operand is a data
  1443. type rather than a value. @xref{Type Size}.
  1444. @node Line Continuation
  1445. @section Line Continuation
  1446. @cindex line continuation
  1447. @cindex continuation of lines
  1448. The sequence of a backslash and a newline is ignored absolutely
  1449. anywhere in a C program. This makes it possible to split a single
  1450. source line into multiple lines in the source file. GNU C tolerates
  1451. and ignores other whitespace between the backslash and the newline.
  1452. In particular, it always ignores a CR (carriage return) character
  1453. there, in case some text editor decided to end the line with the CRLF
  1454. sequence.
  1455. The main use of line continuation in C is for macro definitions that
  1456. would be inconveniently long for a single line (@pxref{Macros}).
  1457. It is possible to continue a line comment onto another line with
  1458. backslash-newline. You can put backslash-newline in the middle of an
  1459. identifier, even a keyword, or an operator. You can even split
  1460. @samp{/*}, @samp{*/}, and @samp{//} onto multiple lines with
  1461. backslash-newline. Here's an ugly example:
  1462. @example
  1463. @group
  1464. /\
  1465. *
  1466. */ fo\
  1467. o +\
  1468. = 1\
  1469. 0;
  1470. @end group
  1471. @end example
  1472. @noindent
  1473. That's equivalent to @samp{/* */ foo += 10;}.
  1474. Don't do those things in real programs, since they make code hard to
  1475. read.
  1476. @strong{Note:} For the sake of using certain tools on the source code, it is
  1477. wise to end every source file with a newline character which is not
  1478. preceded by a backslash, so that it really ends the last line.
  1479. @node Arithmetic
  1480. @chapter Arithmetic
  1481. @cindex arithmetic operators
  1482. @cindex operators, arithmetic
  1483. @c ??? Duplication with other sections -- get rid of that?
  1484. Arithmetic operators in C attempt to be as similar as possible to the
  1485. abstract arithmetic operations, but it is impossible to do this
  1486. perfectly. Numbers in a computer have a finite range of possible
  1487. values, and non-integer values have a limit on their possible
  1488. accuracy. Nonetheless, except when results are out of range, you will
  1489. encounter no surprises in using @samp{+} for addition, @samp{-} for
  1490. subtraction, and @samp{*} for multiplication.
  1491. Each C operator has a @dfn{precedence}, which is its rank in the
  1492. grammatical order of the various operators. The operators with the
  1493. highest precedence grab adjoining operands first; these expressions
  1494. then become operands for operators of lower precedence. We give some
  1495. information about precedence of operators in this chapter where we
  1496. describe the operators; for the full explanation, see @ref{Binary
  1497. Operator Grammar}.
  1498. The arithmetic operators always @dfn{promote} their operands before
  1499. operating on them. This means converting narrow integer data types to
  1500. a wider data type (@pxref{Operand Promotions}). If you are just
  1501. learning C, don't worry about this yet.
  1502. Given two operands that have different types, most arithmetic
  1503. operations convert them both to their @dfn{common type}. For
  1504. instance, if one is @code{int} and the other is @code{double}, the
  1505. common type is @code{double}. (That's because @code{double} can
  1506. represent all the values that an @code{int} can hold, but not vice
  1507. versa.) For the full details, see @ref{Common Type}.
  1508. @menu
  1509. * Basic Arithmetic:: Addition, subtraction, multiplication,
  1510. and division.
  1511. * Integer Arithmetic:: How C performs arithmetic with integer values.
  1512. * Integer Overflow:: When an integer value exceeds the range
  1513. of its type.
  1514. * Mixed Mode:: Calculating with both integer values
  1515. and floating-point values.
  1516. * Division and Remainder:: How integer division works.
  1517. * Numeric Comparisons:: Comparing numeric values for equality or order.
  1518. * Shift Operations:: Shift integer bits left or right.
  1519. * Bitwise Operations:: Bitwise conjunction, disjunction, negation.
  1520. @end menu
  1521. @node Basic Arithmetic
  1522. @section Basic Arithmetic
  1523. @cindex addition operator
  1524. @cindex subtraction operator
  1525. @cindex multiplication operator
  1526. @cindex division operator
  1527. @cindex negation operator
  1528. @cindex operator, addition
  1529. @cindex operator, subtraction
  1530. @cindex operator, multiplication
  1531. @cindex operator, division
  1532. @cindex operator, negation
  1533. Basic arithmetic in C is done with the usual binary operators of
  1534. algebra: addition (@samp{+}), subtraction (@samp{-}), multiplication
  1535. (@samp{*}) and division (@samp{/}). The unary operator @samp{-} is
  1536. used to change the sign of a number. The unary @code{+} operator also
  1537. exists; it yields its operand unaltered.
  1538. @samp{/} is the division operator, but dividing integers may not give
  1539. the result you expect. Its value is an integer, which is not equal to
  1540. the mathematical quotient when that is a fraction. Use @samp{%} to
  1541. get the corresponding integer remainder when necessary.
  1542. @xref{Division and Remainder}. Floating point division yields value
  1543. as close as possible to the mathematical quotient.
  1544. These operators use algebraic syntax with the usual algebraic
  1545. precedence rule (@pxref{Binary Operator Grammar}) that multiplication
  1546. and division are done before addition and subtraction, but you can use
  1547. parentheses to explicitly specify how the operators nest. They are
  1548. left-associative (@pxref{Associativity and Ordering}). Thus,
  1549. @example
  1550. -a + b - c + d * e / f
  1551. @end example
  1552. @noindent
  1553. is equivalent to
  1554. @example
  1555. (((-a) + b) - c) + ((d * e) / f)
  1556. @end example
  1557. @node Integer Arithmetic
  1558. @section Integer Arithmetic
  1559. @cindex integer arithmetic
  1560. Each of the basic arithmetic operations in C has two variants for
  1561. integers: @dfn{signed} and @dfn{unsigned}. The choice is determined
  1562. by the data types of their operands.
  1563. Each integer data type in C is either @dfn{signed} or @dfn{unsigned}.
  1564. A signed type can hold a range of positive and negative numbers, with
  1565. zero near the middle of the range. An unsigned type can hold only
  1566. nonnegative numbers; its range starts with zero and runs upward.
  1567. The most basic integer types are @code{int}, which normally can hold
  1568. numbers from @minus{}2,147,483,648 to 2,147,483,647, and @code{unsigned
  1569. int}, which normally can hold numbers from 0 to 4,294,967,295. (This
  1570. assumes @code{int} is 32 bits wide, always true for GNU C on real
  1571. computers but not always on embedded controllers.) @xref{Integer
  1572. Types}, for full information about integer types.
  1573. When a basic arithmetic operation is given two signed operands, it
  1574. does signed arithmetic. Given two unsigned operands, it does
  1575. unsigned arithmetic.
  1576. If one operand is @code{unsigned int} and the other is @code{int}, the
  1577. operator treats them both as unsigned. More generally, the common
  1578. type of the operands determines whether the operation is signed or
  1579. not. @xref{Common Type}.
  1580. Printing the results of unsigned arithmetic with @code{printf} using
  1581. @samp{%d} can produce surprising results for values far away from
  1582. zero. Even though the rules above say that the computation was done
  1583. with unsigned arithmetic, the printed result may appear to be signed!
  1584. The explanation is that the bit pattern resulting from addition,
  1585. subtraction or multiplication is actually the same for signed and
  1586. unsigned operations. The difference is only in the data type of the
  1587. result, which affects the @emph{interpretation} of the result bit pattern,
  1588. and whether the arithmetic operation can overflow (see the next section).
  1589. But @samp{%d} doesn't know its argument's data type. It sees only the
  1590. value's bit pattern, and it is defined to interpret that as
  1591. @code{signed int}. To print it as unsigned requires using @samp{%u}
  1592. instead of @samp{%d}. @xref{Formatted Output, The GNU C Library, ,
  1593. libc, The GNU C Library Reference Manual}.
  1594. Arithmetic in C never operates directly on narrow integer types (those
  1595. with fewer bits than @code{int}; @ref{Narrow Integers}). Instead it
  1596. ``promotes'' them to @code{int}. @xref{Operand Promotions}.
  1597. @node Integer Overflow
  1598. @section Integer Overflow
  1599. @cindex integer overflow
  1600. @cindex overflow, integer
  1601. When the mathematical value of an arithmetic operation doesn't fit in
  1602. the range of the data type in use, that's called @dfn{overflow}.
  1603. When it happens in integer arithmetic, it is @dfn{integer overflow}.
  1604. Integer overflow happens only in arithmetic operations. Type conversion
  1605. operations, by definition, do not cause overflow, not even when the
  1606. result can't fit in its new type. @xref{Integer Conversion}.
  1607. Signed numbers use two's-complement representation, in which the most
  1608. negative number lacks a positive counterpart (@pxref{Integers in
  1609. Depth}). Thus, the unary @samp{-} operator on a signed integer can
  1610. overflow.
  1611. @menu
  1612. * Unsigned Overflow:: Overflow in unsigned integer arithmetic.
  1613. * Signed Overflow:: Overflow in signed integer arithmetic.
  1614. @end menu
  1615. @node Unsigned Overflow
  1616. @subsection Overflow with Unsigned Integers
  1617. Unsigned arithmetic in C ignores overflow; it produces the true result
  1618. modulo the @var{n}th power of 2, where @var{n} is the number of bits
  1619. in the data type. We say it ``truncates'' the true result to the
  1620. lowest @var{n} bits.
  1621. A true result that is negative, when taken modulo the @var{n}th power
  1622. of 2, yields a positive number. For instance,
  1623. @example
  1624. unsigned int x = 1;
  1625. unsigned int y;
  1626. y = -x;
  1627. @end example
  1628. @noindent
  1629. causes overflow because the negative number @minus{}1 can't be stored
  1630. in an unsigned type. The actual result, which is @minus{}1 modulo the
  1631. @var{n}th power of 2, is one less than the @var{n}th power of 2. That
  1632. is the largest value that the unsigned data type can store. For a
  1633. 32-bit @code{unsigned int}, the value is 4,294,967,295. @xref{Maximum
  1634. and Minimum Values}.
  1635. Adding that number to itself, as here,
  1636. @example
  1637. unsigned int z;
  1638. z = y + y;
  1639. @end example
  1640. @noindent
  1641. ought to yield 8,489,934,590; however, that is again too large to fit,
  1642. so overflow truncates the value to 4,294,967,294. If that were a
  1643. signed integer, it would mean @minus{}2, which (not by coincidence)
  1644. equals @minus{}1 + @minus{}1.
  1645. @node Signed Overflow
  1646. @subsection Overflow with Signed Integers
  1647. @cindex compiler options for integer overflow
  1648. @cindex integer overflow, compiler options
  1649. @cindex overflow, compiler options
  1650. For signed integers, the result of overflow in C is @emph{in
  1651. principle} undefined, meaning that anything whatsoever could happen.
  1652. Therefore, C compilers can do optimizations that treat the overflow
  1653. case with total unconcern. (Since the result of overflow is undefined
  1654. in principle, one cannot claim that these optimizations are
  1655. erroneous.)
  1656. @strong{Watch out:} These optimizations can do surprising things. For
  1657. instance,
  1658. @example
  1659. int i;
  1660. @r{@dots{}}
  1661. if (i < i + 1)
  1662. x = 5;
  1663. @end example
  1664. @noindent
  1665. could be optimized to do the assignment unconditionally, because the
  1666. @code{if}-condition is always true if @code{i + 1} does not overflow.
  1667. GCC offers compiler options to control handling signed integer
  1668. overflow. These options operate per module; that is, each module
  1669. behaves according to the options it was compiled with.
  1670. These two options specify particular ways to handle signed integer
  1671. overflow, other than the default way:
  1672. @table @option
  1673. @item -fwrapv
  1674. Make signed integer operations well-defined, like unsigned integer
  1675. operations: they produce the @var{n} low-order bits of the true
  1676. result. The highest of those @var{n} bits is the sign bit of the
  1677. result. With @option{-fwrapv}, these out-of-range operations are not
  1678. considered overflow, so (strictly speaking) integer overflow never
  1679. happens.
  1680. The option @option{-fwrapv} enables some optimizations based on the
  1681. defined values of out-of-range results. In GCC 8, it disables
  1682. optimizations that are based on assuming signed integer operations
  1683. will not overflow.
  1684. @item -ftrapv
  1685. Generate a signal @code{SIGFPE} when signed integer overflow occurs.
  1686. This terminates the program unless the program handles the signal.
  1687. @xref{Signals}.
  1688. @end table
  1689. One other option is useful for finding where overflow occurs:
  1690. @ignore
  1691. @item -fno-strict-overflow
  1692. Disable optimizations that are based on assuming signed integer
  1693. operations will not overflow.
  1694. @end ignore
  1695. @table @option
  1696. @item -fsanitize=signed-integer-overflow
  1697. Output a warning message at run time when signed integer overflow
  1698. occurs. This checks the @samp{+}, @samp{*}, and @samp{-} operators.
  1699. This takes priority over @option{-ftrapv}.
  1700. @end table
  1701. @node Mixed Mode
  1702. @section Mixed-Mode Arithmetic
  1703. Mixing integers and floating-point numbers in a basic arithmetic
  1704. operation converts the integers automatically to floating point.
  1705. In most cases, this gives exactly the desired results.
  1706. But sometimes it matters precisely where the conversion occurs.
  1707. If @code{i} and @code{j} are integers, @code{(i + j) * 2.0} adds them
  1708. as an integer, then converts the sum to floating point for the
  1709. multiplication. If the addition causes an overflow, that is not
  1710. equivalent to converting each integer to floating point and then
  1711. adding the two floating point numbers. You can get the latter result
  1712. by explicitly converting the integers, as in @code{((double) i +
  1713. (double) j) * 2.0}. @xref{Explicit Type Conversion}.
  1714. @c Eggert's report
  1715. Adding or multiplying several values, including some integers and some
  1716. floating point, performs the operations left to right. Thus, @code{3.0 +
  1717. i + j} converts @code{i} to floating point, then adds 3.0, then
  1718. converts @code{j} to floating point and adds that. You can specify a
  1719. different order using parentheses: @code{3.0 + (i + j)} adds @code{i}
  1720. and @code{j} first and then adds that sum (converted to floating
  1721. point) to 3.0. In this respect, C differs from other languages, such
  1722. as Fortran.
  1723. @node Division and Remainder
  1724. @section Division and Remainder
  1725. @cindex remainder operator
  1726. @cindex modulus
  1727. @cindex operator, remainder
  1728. Division of integers in C rounds the result to an integer. The result
  1729. is always rounded towards zero.
  1730. @example
  1731. 16 / 3 @result{} 5
  1732. -16 / 3 @result{} -5
  1733. 16 / -3 @result{} -5
  1734. -16 / -3 @result{} 5
  1735. @end example
  1736. @noindent
  1737. To get the corresponding remainder, use the @samp{%} operator:
  1738. @example
  1739. 16 % 3 @result{} 1
  1740. -16 % 3 @result{} -1
  1741. 16 % -3 @result{} 1
  1742. -16 % -3 @result{} -1
  1743. @end example
  1744. @noindent
  1745. @samp{%} has the same operator precedence as @samp{/} and @samp{*}.
  1746. From the rounded quotient and the remainder, you can reconstruct
  1747. the dividend, like this:
  1748. @example
  1749. int
  1750. original_dividend (int divisor, int quotient, int remainder)
  1751. @{
  1752. return divisor * quotient + remainder;
  1753. @}
  1754. @end example
  1755. To do unrounded division, use floating point. If only one operand is
  1756. floating point, @samp{/} converts the other operand to floating
  1757. point.
  1758. @example
  1759. 16.0 / 3 @result{} 5.333333333333333
  1760. 16 / 3.0 @result{} 5.333333333333333
  1761. 16.0 / 3.0 @result{} 5.333333333333333
  1762. 16 / 3 @result{} 5
  1763. @end example
  1764. The remainder operator @samp{%} is not allowed for floating-point
  1765. operands, because it is not needed. The concept of remainder makes
  1766. sense for integers because the result of division of integers has to
  1767. be an integer. For floating point, the result of division is a
  1768. floating-point number, in other words a fraction, which will differ
  1769. from the exact result only by a very small amount.
  1770. There are functions in the standard C library to calculate remainders
  1771. from integral-values division of floating-point numbers.
  1772. @xref{Remainder Functions, The GNU C Library, , libc, The GNU C Library
  1773. Reference Manual}.
  1774. Integer division overflows in one specific case: dividing the smallest
  1775. negative value for the data type (@pxref{Maximum and Minimum Values})
  1776. by @minus{}1. That's because the correct result, which is the
  1777. corresponding positive number, does not fit (@pxref{Integer Overflow})
  1778. in the same number of bits. On some computers now in use, this always
  1779. causes a signal @code{SIGFPE} (@pxref{Signals}), the same behavior
  1780. that the option @option{-ftrapv} specifies (@pxref{Signed Overflow}).
  1781. Division by zero leads to unpredictable results---depending on the
  1782. type of computer, it might cause a signal @code{SIGFPE}, or it might
  1783. produce a numeric result.
  1784. @cindex division by zero
  1785. @cindex zero, division by
  1786. @strong{Watch out:} Make sure the program does not divide by zero. If
  1787. you can't prove that the divisor is not zero, test whether it is zero,
  1788. and skip the division if so.
  1789. @node Numeric Comparisons
  1790. @section Numeric Comparisons
  1791. @cindex numeric comparisons
  1792. @cindex comparisons
  1793. @cindex operators, comparison
  1794. @cindex equal operator
  1795. @cindex not-equal operator
  1796. @cindex less-than operator
  1797. @cindex greater-than operator
  1798. @cindex less-or-equal operator
  1799. @cindex greater-or-equal operator
  1800. @cindex operator, equal
  1801. @cindex operator, not-equal
  1802. @cindex operator, less-than
  1803. @cindex operator, greater-than
  1804. @cindex operator, less-or-equal
  1805. @cindex operator, greater-or-equal
  1806. @cindex truth value
  1807. There are two kinds of comparison operators: @dfn{equality} and
  1808. @dfn{ordering}. Equality comparisons test whether two expressions
  1809. have the same value. The result is a @dfn{truth value}: a number that
  1810. is 1 for ``true'' and 0 for ``false.''
  1811. @example
  1812. a == b /* @r{Test for equal.} */
  1813. a != b /* @r{Test for not equal.} */
  1814. @end example
  1815. The equality comparison is written @code{==} because plain @code{=}
  1816. is the assignment operator.
  1817. Ordering comparisons test which operand is greater or less. Their
  1818. results are truth values. These are the ordering comparisons of C:
  1819. @example
  1820. a < b /* @r{Test for less-than.} */
  1821. a > b /* @r{Test for greater-than.} */
  1822. a <= b /* @r{Test for less-than-or-equal.} */
  1823. a >= b /* @r{Test for greater-than-or-equal.} */
  1824. @end example
  1825. For any integers @code{a} and @code{b}, exactly one of the comparisons
  1826. @code{a < b}, @code{a == b} and @code{a > b} is true, just as in
  1827. mathematics. However, if @code{a} and @code{b} are special floating
  1828. point values (not ordinary numbers), all three can be false.
  1829. @xref{Special Float Values}, and @ref{Invalid Optimizations}.
  1830. @node Shift Operations
  1831. @section Shift Operations
  1832. @cindex shift operators
  1833. @cindex operators, shift
  1834. @cindex operators, shift
  1835. @cindex shift count
  1836. @dfn{Shifting} an integer means moving the bit values to the left or
  1837. right within the bits of the data type. Shifting is defined only for
  1838. integers. Here's the way to write it:
  1839. @example
  1840. /* @r{Left shift.} */
  1841. 5 << 2 @result{} 20
  1842. /* @r{Right shift.} */
  1843. 5 >> 2 @result{} 1
  1844. @end example
  1845. @noindent
  1846. The left operand is the value to be shifted, and the right operand
  1847. says how many bits to shift it (the @dfn{shift count}). The left
  1848. operand is promoted (@pxref{Operand Promotions}), so shifting never
  1849. operates on a narrow integer type; it's always either @code{int} or
  1850. wider. The result of the shift operation has the same type as the
  1851. promoted left operand.
  1852. @menu
  1853. * Bits Shifted In:: How shifting makes new bits to shift in.
  1854. * Shift Caveats:: Caveats of shift operations.
  1855. * Shift Hacks:: Clever tricks with shift operations.
  1856. @end menu
  1857. @node Bits Shifted In
  1858. @subsection Shifting Makes New Bits
  1859. A shift operation shifts towards one end of the number and has to
  1860. generate new bits at the other end.
  1861. Shifting left one bit must generate a new least significant bit. It
  1862. always brings in zero there. It is equivalent to multiplying by the
  1863. appropriate power of 2. For example,
  1864. @example
  1865. 5 << 3 @r{is equivalent to} 5 * 2*2*2
  1866. -10 << 4 @r{is equivalent to} -10 * 2*2*2*2
  1867. @end example
  1868. The meaning of shifting right depends on whether the data type is
  1869. signed or unsigned (@pxref{Signed and Unsigned Types}). For a signed
  1870. data type, it performs ``arithmetic shift,'' which keeps the number's
  1871. sign unchanged by duplicating the sign bit. For an unsigned data
  1872. type, it performs ``logical shift,'' which always shifts in zeros at
  1873. the most significant bit.
  1874. In both cases, shifting right one bit is division by two, rounding
  1875. towards negative infinity. For example,
  1876. @example
  1877. (unsigned) 19 >> 2 @result{} 4
  1878. (unsigned) 20 >> 2 @result{} 5
  1879. (unsigned) 21 >> 2 @result{} 5
  1880. @end example
  1881. For negative left operand @code{a}, @code{a >> 1} is not equivalent to
  1882. @code{a / 2}. They both divide by 2, but @samp{/} rounds toward
  1883. zero.
  1884. The shift count must be zero or greater. Shifting by a negative
  1885. number of bits gives machine-dependent results.
  1886. @node Shift Caveats
  1887. @subsection Caveats for Shift Operations
  1888. @strong{Warning:} If the shift count is greater than or equal to the
  1889. width in bits of the promoted first operand, the results are
  1890. machine-dependent. Logically speaking, the ``correct'' value would be
  1891. either @minus{}1 (for right shift of a negative number) or 0 (in all other
  1892. cases), but the actual result is whatever the machine's shift
  1893. instruction does in that case. So unless you can prove that the
  1894. second operand is not too large, write code to check it at run time.
  1895. @strong{Warning:} Never rely on how the shift operators relate in
  1896. precedence to other arithmetic binary operators. Programmers don't
  1897. remember these precedences, and won't understand the code. Always use
  1898. parentheses to explicitly specify the nesting, like this:
  1899. @example
  1900. a + (b << 5) /* @r{Shift first, then add.} */
  1901. (a + b) << 5 /* @r{Add first, then shift.} */
  1902. @end example
  1903. Note: according to the C standard, shifting of signed values isn't
  1904. guaranteed to work properly when the value shifted is negative, or
  1905. becomes negative during the operation of shifting left. However, only
  1906. pedants have a reason to be concerned about this; only computers with
  1907. strange shift instructions could plausibly do this wrong. In GNU C,
  1908. the operation always works as expected,
  1909. @node Shift Hacks
  1910. @subsection Shift Hacks
  1911. You can use the shift operators for various useful hacks. For
  1912. example, given a date specified by day of the month @code{d}, month
  1913. @code{m}, and year @code{y}, you can store the entire date in a single
  1914. integer @code{date}:
  1915. @example
  1916. unsigned int d = 12;
  1917. unsigned int m = 6;
  1918. unsigned int y = 1983;
  1919. unsigned int date = ((y << 4) + m) << 5) + d;
  1920. @end example
  1921. @noindent
  1922. To extract the original day, month, and year out of
  1923. @code{date}, use a combination of shift and remainder.
  1924. @example
  1925. d = date % 32;
  1926. m = (date >> 5) % 16;
  1927. y = date >> 9;
  1928. @end example
  1929. @code{-1 << LOWBITS} is a clever way to make an integer whose
  1930. @code{LOWBITS} lowest bits are all 0 and the rest are all 1.
  1931. @code{-(1 << LOWBITS)} is equivalent to that, due to associativity of
  1932. multiplication, since negating a value is equivalent to multiplying it
  1933. by @minus{}1.
  1934. @node Bitwise Operations
  1935. @section Bitwise Operations
  1936. @cindex bitwise operators
  1937. @cindex operators, bitwise
  1938. @cindex negation, bitwise
  1939. @cindex conjunction, bitwise
  1940. @cindex disjunction, bitwise
  1941. Bitwise operators operate on integers, treating each bit independently.
  1942. They are not allowed for floating-point types.
  1943. The examples in this section use binary constants, starting with
  1944. @samp{0b} (@pxref{Integer Constants}). They stand for 32-bit integers
  1945. of type @code{int}.
  1946. @table @code
  1947. @item ~@code{a}
  1948. Unary operator for bitwise negation; this changes each bit of
  1949. @code{a} from 1 to 0 or from 0 to 1.
  1950. @example
  1951. ~0b10101000 @result{} 0b11111111111111111111111101010111
  1952. ~0 @result{} 0b11111111111111111111111111111111
  1953. ~0b11111111111111111111111111111111 @result{} 0
  1954. ~ (-1) @result{} 0
  1955. @end example
  1956. It is useful to remember that @code{~@var{x} + 1} equals
  1957. @code{-@var{x}}, for integers, and @code{~@var{x}} equals
  1958. @code{-@var{x} - 1}. The last example above shows this with @minus{}1
  1959. as @var{x}.
  1960. @item @code{a} & @code{b}
  1961. Binary operator for bitwise ``and'' or ``conjunction.'' Each bit in
  1962. the result is 1 if that bit is 1 in both @code{a} and @code{b}.
  1963. @example
  1964. 0b10101010 & 0b11001100 @result{} 0b10001000
  1965. @end example
  1966. @item @code{a} | @code{b}
  1967. Binary operator for bitwise ``or'' (``inclusive or'' or
  1968. ``disjunction''). Each bit in the result is 1 if that bit is 1 in
  1969. either @code{a} or @code{b}.
  1970. @example
  1971. 0b10101010 | 0b11001100 @result{} 0b11101110
  1972. @end example
  1973. @item @code{a} ^ @code{b}
  1974. Binary operator for bitwise ``xor'' (``exclusive or''). Each bit in
  1975. the result is 1 if that bit is 1 in exactly one of @code{a} and @code{b}.
  1976. @example
  1977. 0b10101010 ^ 0b11001100 @result{} 0b01100110
  1978. @end example
  1979. @end table
  1980. To understand the effect of these operators on signed integers, keep
  1981. in mind that all modern computers use two's-complement representation
  1982. (@pxref{Integer Representations}) for negative integers. This means
  1983. that the highest bit of the number indicates the sign; it is 1 for a
  1984. negative number and 0 for a positive number. In a negative number,
  1985. the value in the other bits @emph{increases} as the number gets closer
  1986. to zero, so that @code{0b111@r{@dots{}}111} is @minus{}1 and
  1987. @code{0b100@r{@dots{}}000} is the most negative possible integer.
  1988. @strong{Warning:} C defines a precedence ordering for the bitwise
  1989. binary operators, but you should never rely on it. You should
  1990. never rely on how bitwise binary operators relate in precedence to the
  1991. arithmetic and shift binary operators. Other programmers don't
  1992. remember this precedence ordering, so always use parentheses to
  1993. explicitly specify the nesting.
  1994. For example, suppose @code{offset} is an integer that specifies
  1995. the offset within shared memory of a table, except that its bottom few
  1996. bits (@code{LOWBITS} says how many) are special flags. Here's
  1997. how to get just that offset and add it to the base address.
  1998. @example
  1999. shared_mem_base + (offset & (-1 << LOWBITS))
  2000. @end example
  2001. Thanks to the outer set of parentheses, we don't need to know whether
  2002. @samp{&} has higher precedence than @samp{+}. Thanks to the inner
  2003. set, we don't need to know whether @samp{&} has higher precedence than
  2004. @samp{<<}. But we can rely on all unary operators to have higher
  2005. precedence than any binary operator, so we don't need parentheses
  2006. around the left operand of @samp{<<}.
  2007. @node Assignment Expressions
  2008. @chapter Assignment Expressions
  2009. @cindex assignment expressions
  2010. @cindex operators, assignment
  2011. As a general concept in programming, an @dfn{assignment} is a
  2012. construct that stores a new value into a place where values can be
  2013. stored---for instance, in a variable. Such places are called
  2014. @dfn{lvalues} (@pxref{Lvalues}) because they are locations that hold a value.
  2015. An assignment in C is an expression because it has a value; we call
  2016. it an @dfn{assignment expression}. A simple assignment looks like
  2017. @example
  2018. @var{lvalue} = @var{value-to-store}
  2019. @end example
  2020. @noindent
  2021. We say it assigns the value of the expression @var{value-to-store} to
  2022. the location @var{lvalue}, or that it stores @var{value-to-store}
  2023. there. You can think of the ``l'' in ``lvalue'' as standing for
  2024. ``left,'' since that's what you put on the left side of the assignment
  2025. operator.
  2026. However, that's not the only way to use an lvalue, and not all lvalues
  2027. can be assigned to. To use the lvalue in the left side of an
  2028. assignment, it has to be @dfn{modifiable}. In C, that means it was
  2029. not declared with the type qualifier @code{const} (@pxref{const}).
  2030. The value of the assignment expression is that of @var{lvalue} after
  2031. the new value is stored in it. This means you can use an assignment
  2032. inside other expressions. Assignment operators are right-associative
  2033. so that
  2034. @example
  2035. x = y = z = 0;
  2036. @end example
  2037. @noindent
  2038. is equivalent to
  2039. @example
  2040. x = (y = (z = 0));
  2041. @end example
  2042. This is the only useful way for them to associate;
  2043. the other way,
  2044. @example
  2045. ((x = y) = z) = 0;
  2046. @end example
  2047. @noindent
  2048. would be invalid since an assignment expression such as @code{x = y}
  2049. is not valid as an lvalue.
  2050. @strong{Warning:} Write parentheses around an assignment if you nest
  2051. it inside another expression, unless that is a conditional expression,
  2052. or comma-separated series, or another assignment.
  2053. @menu
  2054. * Simple Assignment:: The basics of storing a value.
  2055. * Lvalues:: Expressions into which a value can be stored.
  2056. * Modifying Assignment:: Shorthand for changing an lvalue's contents.
  2057. * Increment/Decrement:: Shorthand for incrementing and decrementing
  2058. an lvalue's contents.
  2059. * Postincrement/Postdecrement:: Accessing then incrementing or decrementing.
  2060. * Assignment in Subexpressions:: How to avoid ambiguity.
  2061. * Write Assignments Separately:: Write assignments as separate statements.
  2062. @end menu
  2063. @node Simple Assignment
  2064. @section Simple Assignment
  2065. @cindex simple assignment
  2066. @cindex assignment, simple
  2067. A @dfn{simple assignment expression} computes the value of the right
  2068. operand and stores it into the lvalue on the left. Here is a simple
  2069. assignment expression that stores 5 in @code{i}:
  2070. @example
  2071. i = 5
  2072. @end example
  2073. @noindent
  2074. We say that this is an @dfn{assignment to} the variable @code{i} and
  2075. that it @dfn{assigns} @code{i} the value 5. It has no semicolon
  2076. because it is an expression (so it has a value). Adding a semicolon
  2077. at the end would make it a statement (@pxref{Expression Statement}).
  2078. Here is another example of a simple assignment expression. Its
  2079. operands are not simple, but the kind of assignment done here is
  2080. simple assignment.
  2081. @example
  2082. x[foo ()] = y + 6
  2083. @end example
  2084. A simple assignment with two different numeric data types converts the
  2085. right operand value to the lvalue's type, if possible. It can convert
  2086. any numeric type to any other numeric type.
  2087. Simple assignment is also allowed on some non-numeric types: pointers
  2088. (@pxref{Pointers}), structures (@pxref{Structure Assignment}), and
  2089. unions (@pxref{Unions}).
  2090. @strong{Warning:} Assignment is not allowed on arrays because
  2091. there are no array values in C; C variables can be arrays, but these
  2092. arrays cannot be manipulated as wholes. @xref{Limitations of C
  2093. Arrays}.
  2094. @xref{Assignment Type Conversions}, for the complete rules about data
  2095. types used in assignments.
  2096. @node Lvalues
  2097. @section Lvalues
  2098. @cindex lvalues
  2099. An expression that identifies a memory space that holds a value is
  2100. called an @dfn{lvalue}, because it is a location that can hold a value.
  2101. The standard kinds of lvalues are:
  2102. @itemize @bullet
  2103. @item
  2104. A variable.
  2105. @item
  2106. A pointer-dereference expression (@pxref{Pointer Dereference}) using
  2107. unary @samp{*}.
  2108. @item
  2109. A structure field reference (@pxref{Structures}) using @samp{.}, if
  2110. the structure value is an lvalue.
  2111. @item
  2112. A structure field reference using @samp{->}. This is always an lvalue
  2113. since @samp{->} implies pointer dereference.
  2114. @item
  2115. A union alternative reference (@pxref{Unions}), on the same conditions
  2116. as for structure fields.
  2117. @item
  2118. An array-element reference using @samp{[@r{@dots{}}]}, if the array
  2119. is an lvalue.
  2120. @end itemize
  2121. If an expression's outermost operation is any other operator, that
  2122. expression is not an lvalue. Thus, the variable @code{x} is an
  2123. lvalue, but @code{x + 0} is not, even though these two expressions
  2124. compute the same value (assuming @code{x} is a number).
  2125. An array can be an lvalue (the rules above determine whether it is
  2126. one), but using the array in an expression converts it automatically
  2127. to a pointer to the first element. The result of this conversion is
  2128. not an lvalue. Thus, if the variable @code{a} is an array, you can't
  2129. use @code{a} by itself as the left operand of an assignment. But you
  2130. can assign to an element of @code{a}, such as @code{a[0]}. That is an
  2131. lvalue since @code{a} is an lvalue.
  2132. @node Modifying Assignment
  2133. @section Modifying Assignment
  2134. @cindex modifying assignment
  2135. @cindex assignment, modifying
  2136. You can abbreviate the common construct
  2137. @example
  2138. @var{lvalue} = @var{lvalue} + @var{expression}
  2139. @end example
  2140. @noindent
  2141. as
  2142. @example
  2143. @var{lvalue} += @var{expression}
  2144. @end example
  2145. This is known as a @dfn{modifying assignment}. For instance,
  2146. @example
  2147. i = i + 5;
  2148. i += 5;
  2149. @end example
  2150. @noindent
  2151. shows two statements that are equivalent. The first uses
  2152. simple assignment; the second uses modifying assignment.
  2153. Modifying assignment works with any binary arithmetic operator. For
  2154. instance, you can subtract something from an lvalue like this,
  2155. @example
  2156. @var{lvalue} -= @var{expression}
  2157. @end example
  2158. @noindent
  2159. or multiply it by a certain amount like this,
  2160. @example
  2161. @var{lvalue} *= @var{expression}
  2162. @end example
  2163. @noindent
  2164. or shift it by a certain amount like this.
  2165. @example
  2166. @var{lvalue} <<= @var{expression}
  2167. @var{lvalue} >>= @var{expression}
  2168. @end example
  2169. In most cases, this feature adds no power to the language, but it
  2170. provides substantial convenience. Also, when @var{lvalue} contains
  2171. code that has side effects, the simple assignment performs those side
  2172. effects twice, while the modifying assignment performs them once. For
  2173. instance,
  2174. @example
  2175. x[foo ()] = x[foo ()] + 5;
  2176. @end example
  2177. @noindent
  2178. calls @code{foo} twice, and it could return different values each
  2179. time. If @code{foo ()} returns 1 the first time and 3 the second
  2180. time, then the effect could be to add @code{x[3]} and 5 and store the
  2181. result in @code{x[1]}, or to add @code{x[1]} and 5 and store the
  2182. result in @code{x[3]}. We don't know which of the two it will do,
  2183. because C does not specify which call to @code{foo} is computed first.
  2184. Such a statement is not well defined, and shouldn't be used.
  2185. By contrast,
  2186. @example
  2187. x[foo ()] += 5;
  2188. @end example
  2189. @noindent
  2190. is well defined: it calls @code{foo} only once to determine which
  2191. element of @code{x} to adjust, and it adjusts that element by adding 5
  2192. to it.
  2193. @node Increment/Decrement
  2194. @section Increment and Decrement Operators
  2195. @cindex increment operator
  2196. @cindex decrement operator
  2197. @cindex operator, increment
  2198. @cindex operator, decrement
  2199. @cindex preincrement expression
  2200. @cindex predecrement expression
  2201. The operators @samp{++} and @samp{--} are the @dfn{increment} and
  2202. @dfn{decrement} operators. When used on a numeric value, they add or
  2203. subtract 1. We don't consider them assignments, but they are
  2204. equivalent to assignments.
  2205. Using @samp{++} or @samp{--} as a prefix, before an lvalue, is called
  2206. @dfn{preincrement} or @dfn{predecrement}. This adds or subtracts 1
  2207. and the result becomes the expression's value. For instance,
  2208. @example
  2209. #include <stdio.h> /* @r{Declares @code{printf}.} */
  2210. int
  2211. main (void)
  2212. @{
  2213. int i = 5;
  2214. printf ("%d\n", i);
  2215. printf ("%d\n", ++i);
  2216. printf ("%d\n", i);
  2217. return 0;
  2218. @}
  2219. @end example
  2220. @noindent
  2221. prints lines containing 5, 6, and 6 again. The expression @code{++i}
  2222. increments @code{i} from 5 to 6, and has the value 6, so the output
  2223. from @code{printf} on that line says @samp{6}.
  2224. Using @samp{--} instead, for predecrement,
  2225. @example
  2226. #include <stdio.h> /* @r{Declares @code{printf}.} */
  2227. int
  2228. main (void)
  2229. @{
  2230. int i = 5;
  2231. printf ("%d\n", i);
  2232. printf ("%d\n", --i);
  2233. printf ("%d\n", i);
  2234. return 0;
  2235. @}
  2236. @end example
  2237. @noindent
  2238. prints three lines that contain (respectively) @samp{5}, @samp{4}, and
  2239. again @samp{4}.
  2240. @node Postincrement/Postdecrement
  2241. @section Postincrement and Postdecrement
  2242. @cindex postincrement expression
  2243. @cindex postdecrement expression
  2244. @cindex operator, postincrement
  2245. @cindex operator, postdecrement
  2246. Using @samp{++} or @samp{--} @emph{after} an lvalue does something
  2247. peculiar: it gets the value directly out of the lvalue and @emph{then}
  2248. increments or decrements it. Thus, the value of @code{i++} is the same
  2249. as the value of @code{i}, but @code{i++} also increments @code{i} ``a
  2250. little later.'' This is called @dfn{postincrement} or
  2251. @dfn{postdecrement}.
  2252. For example,
  2253. @example
  2254. #include <stdio.h> /* @r{Declares @code{printf}.} */
  2255. int
  2256. main (void)
  2257. @{
  2258. int i = 5;
  2259. printf ("%d\n", i);
  2260. printf ("%d\n", i++);
  2261. printf ("%d\n", i);
  2262. return 0;
  2263. @}
  2264. @end example
  2265. @noindent
  2266. prints lines containing 5, again 5, and 6. The expression @code{i++}
  2267. has the value 5, which is the value of @code{i} at the time,
  2268. but it increments @code{i} from 5 to 6 just a little later.
  2269. How much later is ``just a little later''? That is flexible. The
  2270. increment has to happen by the next @dfn{sequence point}. In simple cases,
  2271. that means by the end of the statement. @xref{Sequence Points}.
  2272. If a unary operator precedes a postincrement or postincrement expression,
  2273. the increment nests inside:
  2274. @example
  2275. -a++ @r{is equivalent to} -(a++)
  2276. @end example
  2277. That's the only order that makes sense; @code{-a} is not an lvalue, so
  2278. it can't be incremented.
  2279. The most common use of postincrement is with arrays. Here's
  2280. an example of using postincrement to access one element of an
  2281. array and advance the index for the next access. Compare
  2282. this with the example @code{avg_of_double}, which is almost
  2283. the same but doesn't use postincrement (@pxref{Array Example}).
  2284. @example
  2285. double
  2286. avg_of_double_alt (int length, double input_data[])
  2287. @{
  2288. double sum = 0;
  2289. int i;
  2290. /* @r{Fetch each element and add it into @code{sum}.} */
  2291. for (i = 0; i < length;)
  2292. /* @r{Use the index @code{i}, then increment it.} */
  2293. sum += input_data[i++];
  2294. return sum / length;
  2295. @}
  2296. @end example
  2297. @node Assignment in Subexpressions
  2298. @section Pitfall: Assignment in Subexpressions
  2299. @cindex assignment in subexpressions
  2300. @cindex subexpressions, assignment in
  2301. In C, the order of computing parts of an expression is not fixed.
  2302. Aside from a few special cases, the operations can be computed in any
  2303. order. If one part of the expression has an assignment to @code{x}
  2304. and another part of the expression uses @code{x}, the result is
  2305. unpredictable because that use might be computed before or after the
  2306. assignment.
  2307. Here's an example of ambiguous code:
  2308. @example
  2309. x = 20;
  2310. printf ("%d %d\n", x, x = 4);
  2311. @end example
  2312. @noindent
  2313. If the second argument, @code{x}, is computed before the third argument,
  2314. @code{x = 4}, the second argument's value will be 20. If they are
  2315. computed in the other order, the second argument's value will be 4.
  2316. Here's one way to make that code unambiguous:
  2317. @example
  2318. y = 20;
  2319. printf ("%d %d\n", y, x = 4);
  2320. @end example
  2321. Here's another way, with the other meaning:
  2322. @example
  2323. x = 4;
  2324. printf ("%d %d\n", x, x);
  2325. @end example
  2326. This issue applies to all kinds of assignments, and to the increment
  2327. and decrement operators, which are equivalent to assignments.
  2328. @xref{Order of Execution}, for more information about this.
  2329. However, it can be useful to write assignments inside an
  2330. @code{if}-condition or @code{while}-test along with logical operators.
  2331. @xref{Logicals and Assignments}.
  2332. @node Write Assignments Separately
  2333. @section Write Assignments in Separate Statements
  2334. It is often convenient to write an assignment inside an
  2335. @code{if}-condition, but that can reduce the readability of the
  2336. program. Here's an example of what to avoid:
  2337. @example
  2338. if (x = advance (x))
  2339. @r{@dots{}}
  2340. @end example
  2341. The idea here is to advance @code{x} and test if the value is nonzero.
  2342. However, readers might miss the fact that it uses @samp{=} and not
  2343. @samp{==}. In fact, writing @samp{=} where @samp{==} was intended
  2344. inside a condition is a common error, so GNU C can give warnings when
  2345. @samp{=} appears in a way that suggests it's an error.
  2346. It is much clearer to write the assignment as a separate statement, like this:
  2347. @example
  2348. x = advance (x);
  2349. if (x != 0)
  2350. @r{@dots{}}
  2351. @end example
  2352. @noindent
  2353. This makes it unmistakably clear that @code{x} is assigned a new value.
  2354. Another method is to use the comma operator (@pxref{Comma Operator}),
  2355. like this:
  2356. @example
  2357. if (x = advance (x), x != 0)
  2358. @r{@dots{}}
  2359. @end example
  2360. @noindent
  2361. However, putting the assignment in a separate statement is usually clearer
  2362. unless the assignment is very short, because it reduces nesting.
  2363. @node Execution Control Expressions
  2364. @chapter Execution Control Expressions
  2365. @cindex execution control expressions
  2366. @cindex expressions, execution control
  2367. This chapter describes the C operators that combine expressions to
  2368. control which of those expressions execute, or in which order.
  2369. @menu
  2370. * Logical Operators:: Logical conjunction, disjunction, negation.
  2371. * Logicals and Comparison:: Logical operators with comparison operators.
  2372. * Logicals and Assignments:: Assignments with logical operators.
  2373. * Conditional Expression:: An if/else construct inside expressions.
  2374. * Comma Operator:: Build a sequence of subexpressions.
  2375. @end menu
  2376. @node Logical Operators
  2377. @section Logical Operators
  2378. @cindex logical operators
  2379. @cindex operators, logical
  2380. @cindex conjunction operator
  2381. @cindex disjunction operator
  2382. @cindex negation operator, logical
  2383. The @dfn{logical operators} combine truth values, which are normally
  2384. represented in C as numbers. Any expression with a numeric value is a
  2385. valid truth value: zero means false, and any other value means true.
  2386. A pointer type is also meaningful as a truth value; a null pointer
  2387. (which is zero) means false, and a non-null pointer means true
  2388. (@pxref{Pointer Types}). The value of a logical operator is always 1
  2389. or 0 and has type @code{int} (@pxref{Integer Types}).
  2390. The logical operators are used mainly in the condition of an @code{if}
  2391. statement, or in the end test in a @code{for} statement or
  2392. @code{while} statement (@pxref{Statements}). However, they are valid
  2393. in any context where an integer-valued expression is allowed.
  2394. @table @samp
  2395. @item ! @var{exp}
  2396. Unary operator for logical ``not.'' The value is 1 (true) if
  2397. @var{exp} is 0 (false), and 0 (false) if @var{exp} is nonzero (true).
  2398. @strong{Warning:} if @code{exp} is anything but an lvalue or a
  2399. function call, you should write parentheses around it.
  2400. @item @var{left} && @var{right}
  2401. The logical ``and'' binary operator computes @var{left} and, if necessary,
  2402. @var{right}. If both of the operands are true, the @samp{&&} expression
  2403. gives the value 1 (which is true). Otherwise, the @samp{&&} expression
  2404. gives the value 0 (false). If @var{left} yields a false value,
  2405. that determines the overall result, so @var{right} is not computed.
  2406. @item @var{left} || @var{right}
  2407. The logical ``or'' binary operator computes @var{left} and, if necessary,
  2408. @var{right}. If at least one of the operands is true, the @samp{||} expression
  2409. gives the value 1 (which is true). Otherwise, the @samp{||} expression
  2410. gives the value 0 (false). If @var{left} yields a true value,
  2411. that determines the overall result, so @var{right} is not computed.
  2412. @end table
  2413. @strong{Warning:} never rely on the relative precedence of @samp{&&}
  2414. and @samp{||}. When you use them together, always use parentheses to
  2415. specify explicitly how they nest, as shown here:
  2416. @example
  2417. if ((r != 0 && x % r == 0)
  2418. ||
  2419. (s != 0 && x % s == 0))
  2420. @end example
  2421. @node Logicals and Comparison
  2422. @section Logical Operators and Comparisons
  2423. The most common thing to use inside the logical operators is a
  2424. comparison. Conveniently, @samp{&&} and @samp{||} have lower
  2425. precedence than comparison operators and arithmetic operators, so we
  2426. can write expressions like this without parentheses and get the
  2427. nesting that is natural: two comparison operations that must both be
  2428. true.
  2429. @example
  2430. if (r != 0 && x % r == 0)
  2431. @end example
  2432. @noindent
  2433. This example also shows how it is useful that @samp{&&} guarantees to
  2434. skip the right operand if the left one turns out false. Because of
  2435. that, this code never tries to divide by zero.
  2436. This is equivalent:
  2437. @example
  2438. if (r && x % r == 0)
  2439. @end example
  2440. @noindent
  2441. A truth value is simply a number, so using @code{r} as a truth value
  2442. tests whether it is nonzero. But @code{r}'s meaning as en expression
  2443. is not a truth value---it is a number to divide by. So it is better
  2444. style to write the explicit @code{!= 0}.
  2445. Here's another equivalent way to write it:
  2446. @example
  2447. if (!(r == 0) && x % r == 0)
  2448. @end example
  2449. @noindent
  2450. This illustrates the unary @samp{!} operator, and the need to
  2451. write parentheses around its operand.
  2452. @node Logicals and Assignments
  2453. @section Logical Operators and Assignments
  2454. There are cases where assignments nested inside the condition can
  2455. actually make a program @emph{easier} to read. Here is an example
  2456. using a hypothetical type @code{list} which represents a list; it
  2457. tests whether the list has at least two links, using hypothetical
  2458. functions, @code{nonempty} which is true if the argument is a nonempty
  2459. list, and @code{list_next} which advances from one list link to the
  2460. next. We assume that a list is never a null pointer, so that the
  2461. assignment expressions are always ``true.''
  2462. @example
  2463. if (nonempty (list)
  2464. && (temp1 = list_next (list))
  2465. && nonempty (temp1)
  2466. && (temp2 = list_next (temp1)))
  2467. @r{@dots{}} /* @r{use @code{temp1} and @code{temp2}} */
  2468. @end example
  2469. @noindent
  2470. Here we take advantage of the @samp{&&} operator to avoid executing
  2471. the rest of the code if a call to @code{nonempty} returns ``false.'' The
  2472. only natural place to put the assignments is among those calls.
  2473. It would be possible to rewrite this as several statements, but that
  2474. could make it much more cumbersome. On the other hand, when the test
  2475. is even more complex than this one, splitting it into multiple
  2476. statements might be necessary for clarity.
  2477. If an empty list is a null pointer, we can dispense with calling
  2478. @code{nonempty}:
  2479. @example
  2480. if ((temp1 = list_next (list))
  2481. && (temp2 = list_next (temp1)))
  2482. @r{@dots{}}
  2483. @end example
  2484. @node Conditional Expression
  2485. @section Conditional Expression
  2486. @cindex conditional expression
  2487. @cindex expression, conditional
  2488. C has a conditional expression that selects one of two expressions
  2489. to compute and get the value from. It looks like this:
  2490. @example
  2491. @var{condition} ? @var{iftrue} : @var{iffalse}
  2492. @end example
  2493. @menu
  2494. * Conditional Rules:: Rules for the conditional operator.
  2495. * Conditional Branches:: About the two branches in a conditional.
  2496. @end menu
  2497. @node Conditional Rules
  2498. @subsection Rules for the Conditional Operator
  2499. The first operand, @var{condition}, should be a value that can be
  2500. compared with zero---a number or a pointer. If it is true (nonzero),
  2501. then the conditional expression computes @var{iftrue} and its value
  2502. becomes the value of the conditional expression. Otherwise the
  2503. conditional expression computes @var{iffalse} and its value becomes
  2504. the value of the conditional expression. The conditional expression
  2505. always computes just one of @var{iftrue} and @var{iffalse}, never both
  2506. of them.
  2507. Here's an example: the absolute value of a number @code{x}
  2508. can be written as @code{(x >= 0 ? x : -x)}.
  2509. @strong{Warning:} The conditional expression operators have rather low
  2510. syntactic precedence. Except when the conditional expression is used
  2511. as an argument in a function call, write parentheses around it. For
  2512. clarity, always write parentheses around it if it extends across more
  2513. than one line.
  2514. Assignment operators and the comma operator (@pxref{Comma Operator})
  2515. have lower precedence than conditional expression operators, so write
  2516. parentheses around those when they appear inside a conditional
  2517. expression. @xref{Order of Execution}.
  2518. @node Conditional Branches
  2519. @subsection Conditional Operator Branches
  2520. @cindex branches of conditional expression
  2521. We call @var{iftrue} and @var{iffalse} the @dfn{branches} of the
  2522. conditional.
  2523. The two branches should normally have the same type, but a few
  2524. exceptions are allowed. If they are both numeric types, the
  2525. conditional converts both to their common type (@pxref{Common Type}).
  2526. With pointers (@pxref{Pointers}), the two values can be pointers to
  2527. nearly compatible types (@pxref{Compatible Types}). In this case, the
  2528. result type is a similar pointer whose target type combines all the
  2529. type qualifiers (@pxref{Type Qualifiers}) of both branches.
  2530. If one branch has type @code{void *} and the other is a pointer to an
  2531. object (not to a function), the conditional converts the @code{void *}
  2532. branch to the type of the other.
  2533. If one branch is an integer constant with value zero and the other is
  2534. a pointer, the conditional converts zero to the pointer's type.
  2535. In GNU C, you can omit @var{iftrue} in a conditional expression. In
  2536. that case, if @var{condition} is nonzero, its value becomes the value of
  2537. the conditional expression, after conversion to the common type.
  2538. Thus,
  2539. @example
  2540. x ? : y
  2541. @end example
  2542. @noindent
  2543. has the value of @code{x} if that is nonzero; otherwise, the value of
  2544. @code{y}.
  2545. @cindex side effect in ?:
  2546. @cindex ?: side effect
  2547. Omitting @var{iftrue} is useful when @var{condition} has side effects.
  2548. In that case, writing that expression twice would carry out the side
  2549. effects twice, but writing it once does them just once. For example,
  2550. if we suppose that the function @code{next_element} advances a pointer
  2551. variable to point to the next element in a list and returns the new
  2552. pointer,
  2553. @example
  2554. next_element () ? : default_pointer
  2555. @end example
  2556. @noindent
  2557. is a way to advance the pointer and use its new value if it isn't
  2558. null, but use @code{default_pointer} if that is null. We cannot do
  2559. it this way,
  2560. @example
  2561. next_element () ? next_element () : default_pointer
  2562. @end example
  2563. @noindent
  2564. because that would advance the pointer a second time.
  2565. @node Comma Operator
  2566. @section Comma Operator
  2567. @cindex comma operator
  2568. @cindex operator, comma
  2569. The comma operator stands for sequential execution of expressions.
  2570. The value of the comma expression comes from the last expression in
  2571. the sequence; the previous expressions are computed only for their
  2572. side effects. It looks like this:
  2573. @example
  2574. @var{exp1}, @var{exp2} @r{@dots{}}
  2575. @end example
  2576. @noindent
  2577. You can bundle any number of expressions together this way, by putting
  2578. commas between them.
  2579. @menu
  2580. * Uses of Comma:: When to use the comma operator.
  2581. * Clean Comma:: Clean use of the comma operator.
  2582. * Avoid Comma:: When to not use the comma operator.
  2583. @end menu
  2584. @node Uses of Comma
  2585. @subsection The Uses of the Comma Operator
  2586. With commas, you can put several expressions into a place that
  2587. requires just one expression---for example, in the header of a
  2588. @code{for} statement. This statement
  2589. @example
  2590. for (i = 0, j = 10, k = 20; i < n; i++)
  2591. @end example
  2592. @noindent
  2593. contains three assignment expressions, to initialize @code{i}, @code{j}
  2594. and @code{k}. The syntax of @code{for} requires just one expression
  2595. for initialization; to include three assignments, we use commas to
  2596. bundle them into a single larger expression, @code{i = 0, j = 10, k =
  2597. 20}. This technique is also useful in the loop-advance expression,
  2598. the last of the three inside the @code{for} parentheses.
  2599. In the @code{for} statement and the @code{while} statement
  2600. (@pxref{Loop Statements}), a comma provides a way to perform some side
  2601. effect before the loop-exit test. For example,
  2602. @example
  2603. while (printf ("At the test, x = %d\n", x), x != 0)
  2604. @end example
  2605. @node Clean Comma
  2606. @subsection Clean Use of the Comma Operator
  2607. Always write parentheses around a series of comma operators, except
  2608. when it is at top level in an expression statement, or within the
  2609. parentheses of an @code{if}, @code{for}, @code{while}, or @code{switch}
  2610. statement (@pxref{Statements}). For instance, in
  2611. @example
  2612. for (i = 0, j = 10, k = 20; i < n; i++)
  2613. @end example
  2614. @noindent
  2615. the commas between the assignments are clear because they are between
  2616. a parenthesis and a semicolon.
  2617. The arguments in a function call are also separated by commas, but that is
  2618. not an instance of the comma operator. Note the difference between
  2619. @example
  2620. foo (4, 5, 6)
  2621. @end example
  2622. @noindent
  2623. which passes three arguments to @code{foo} and
  2624. @example
  2625. foo ((4, 5, 6))
  2626. @end example
  2627. @noindent
  2628. which uses the comma operator and passes just one argument
  2629. (with value 6).
  2630. @strong{Warning:} don't use the comma operator around an argument
  2631. of a function unless it makes the code more readable. When you do so,
  2632. don't put part of another argument on the same line. Instead, add a
  2633. line break to make the parentheses around the comma operator easier to
  2634. see, like this.
  2635. @example
  2636. foo ((mumble (x, y), frob (z)),
  2637. *p)
  2638. @end example
  2639. @node Avoid Comma
  2640. @subsection When Not to Use the Comma Operator
  2641. You can use a comma in any subexpression, but in most cases it only
  2642. makes the code confusing, and it is clearer to raise all but the last
  2643. of the comma-separated expressions to a higher level. Thus, instead
  2644. of this:
  2645. @example
  2646. x = (y += 4, 8);
  2647. @end example
  2648. @noindent
  2649. it is much clearer to write this:
  2650. @example
  2651. y += 4, x = 8;
  2652. @end example
  2653. @noindent
  2654. or this:
  2655. @example
  2656. y += 4;
  2657. x = 8;
  2658. @end example
  2659. Use commas only in the cases where there is no clearer alternative
  2660. involving multiple statements.
  2661. By contrast, don't hesitate to use commas in the expansion in a macro
  2662. definition. The trade-offs of code clarity are different in that
  2663. case, because the @emph{use} of the macro may improve overall clarity
  2664. so much that the ugliness of the macro's @emph{definition} is a small
  2665. price to pay. @xref{Macros}.
  2666. @node Binary Operator Grammar
  2667. @chapter Binary Operator Grammar
  2668. @cindex binary operator grammar
  2669. @cindex grammar, binary operator
  2670. @cindex operator precedence
  2671. @cindex precedence, operator
  2672. @cindex left-associative
  2673. @dfn{Binary operators} are those that take two operands, one
  2674. on the left and one on the right.
  2675. All the binary operators in C are syntactically left-associative.
  2676. This means that @w{@code{a @var{op} b @var{op} c}} means @w{@code{(a
  2677. @var{op} b) @var{op} c}}. However, the only operators you should
  2678. repeat in this way without parentheses are @samp{+}, @samp{-},
  2679. @samp{*} and @samp{/}, because those cases are clear from algebra. So
  2680. it is OK to write @code{a + b + c} or @code{a - b - c}, but never
  2681. @code{a == b == c} or @code{a % b % c}. For those operators, use
  2682. explicit parentheses to show how the operations nest.
  2683. Each C operator has a @dfn{precedence}, which is its rank in the
  2684. grammatical order of the various operators. The operators with the
  2685. highest precedence grab adjoining operands first; these expressions
  2686. then become operands for operators of lower precedence.
  2687. The precedence order of operators in C is fully specified, so any
  2688. combination of operations leads to a well-defined nesting. We state
  2689. only part of the full precedence ordering here because it is bad
  2690. practice for C code to depend on the other cases. For cases not
  2691. specified in this chapter, always use parentheses to make the nesting
  2692. explicit.@footnote{Personal note from Richard Stallman: I wrote GCC without
  2693. remembering anything about the C precedence order beyond what's stated
  2694. here. I studied the full precedence table to write the parser, and
  2695. promptly forgot it again. If you need to look up the full precedence order
  2696. to understand some C code, fix the code with parentheses so nobody else
  2697. needs to do that.}
  2698. You can depend on this subsequence of the precedence ordering
  2699. (stated from highest precedence to lowest):
  2700. @enumerate
  2701. @item
  2702. Component access (@samp{.} and @samp{->}).
  2703. @item
  2704. Unary prefix operators.
  2705. @item
  2706. Unary postfix operators.
  2707. @item
  2708. Multiplication, division, and remainder (they have the same precedence).
  2709. @item
  2710. Addition and subtraction (they have the same precedence).
  2711. @item
  2712. Comparisons---but watch out!
  2713. @item
  2714. Logical operators @samp{&&} and @samp{||}---but watch out!
  2715. @item
  2716. Conditional expression with @samp{?} and @samp{:}.
  2717. @item
  2718. Assignments.
  2719. @item
  2720. Sequential execution (the comma operator, @samp{,}).
  2721. @end enumerate
  2722. Two of the lines in the above list say ``but watch out!'' That means
  2723. that the line covers operators with subtly different precedence.
  2724. Never depend on the grammar of C to decide how two comparisons nest;
  2725. instead, always use parentheses to specify their nesting.
  2726. You can let several @samp{&&} operators associate, or several
  2727. @samp{||} operators, but always use parentheses to show how @samp{&&}
  2728. and @samp{||} nest with each other. @xref{Logical Operators}.
  2729. There is one other precedence ordering that code can depend on:
  2730. @enumerate
  2731. @item
  2732. Unary postfix operators.
  2733. @item
  2734. Bitwise and shift operators---but watch out!
  2735. @item
  2736. Conditional expression with @samp{?} and @samp{:}.
  2737. @end enumerate
  2738. The caveat for bitwise and shift operators is like that for logical
  2739. operators: you can let multiple uses of one bitwise operator
  2740. associate, but always use parentheses to control nesting of dissimilar
  2741. operators.
  2742. These lists do not specify any precedence ordering between the bitwise
  2743. and shift operators of the second list and the binary operators above
  2744. conditional expressions in the first list. When they come together,
  2745. parenthesize them. @xref{Bitwise Operations}.
  2746. @node Order of Execution
  2747. @chapter Order of Execution
  2748. @cindex order of execution
  2749. The order of execution of a C program is not always obvious, and not
  2750. necessarily predictable. This chapter describes what you can count on.
  2751. @menu
  2752. * Reordering of Operands:: Operations in C are not necessarily computed
  2753. in the order they are written.
  2754. * Associativity and Ordering:: Some associative operations are performed
  2755. in a particular order; others are not.
  2756. * Sequence Points:: Some guarantees about the order of operations.
  2757. * Postincrement and Ordering:: Ambiguous execution order with postincrement.
  2758. * Ordering of Operands:: Evaluation order of operands
  2759. and function arguments.
  2760. * Optimization and Ordering:: Compiler optimizations can reorder operations
  2761. only if it has no impact on program results.
  2762. @end menu
  2763. @node Reordering of Operands
  2764. @section Reordering of Operands
  2765. @cindex ordering of operands
  2766. @cindex reordering of operands
  2767. @cindex operand execution ordering
  2768. The C language does not necessarily carry out operations within an
  2769. expression in the order they appear in the code. For instance, in
  2770. this expression,
  2771. @example
  2772. foo () + bar ()
  2773. @end example
  2774. @noindent
  2775. @code{foo} might be called first or @code{bar} might be called first.
  2776. If @code{foo} updates a datum and @code{bar} uses that datum, the
  2777. results can be unpredictable.
  2778. The unpredictable order of computation of subexpressions also makes a
  2779. difference when one of them contains an assignment. We already saw
  2780. this example of bad code,
  2781. @example
  2782. x = 20;
  2783. printf ("%d %d\n", x, x = 4);
  2784. @end example
  2785. @noindent
  2786. in which the second argument, @code{x}, has a different value
  2787. depending on whether it is computed before or after the assignment in
  2788. the third argument.
  2789. @node Associativity and Ordering
  2790. @section Associativity and Ordering
  2791. @cindex associativity and ordering
  2792. An associative binary operator, such as @code{+}, when used repeatedly
  2793. can combine any number of operands. The operands' values may be
  2794. computed in any order.
  2795. If the values are integers and overflow can be ignored, they may be
  2796. combined in any order. Thus, given four functions that return
  2797. @code{unsigned int}, calling them and adding their results as here
  2798. @example
  2799. (foo () + bar ()) + (baz () + quux ())
  2800. @end example
  2801. @noindent
  2802. may add up the results in any order.
  2803. By contrast, arithmetic on signed integers, in which overflow is significant,
  2804. is not always associative (@pxref{Integer Overflow}). Thus, the
  2805. additions must be done in the order specified, obeying parentheses and
  2806. left-association. That means computing @code{(foo () + bar ())} and
  2807. @code{(baz () + quux ())} first (in either order), then adding the
  2808. two.
  2809. The same applies to arithmetic on floating-point values, since that
  2810. too is not really associative. However, the GCC option
  2811. @option{-funsafe-math-optimizations} allows the compiler to change the
  2812. order of calculation when an associative operation (associative in
  2813. exact mathematics) combines several operands. The option takes effect
  2814. when compiling a module (@pxref{Compilation}). Changing the order
  2815. of association can enable the program to pipeline the floating point
  2816. operations.
  2817. In all these cases, the four function calls can be done in any order.
  2818. There is no right or wrong about that.
  2819. @node Sequence Points
  2820. @section Sequence Points
  2821. @cindex sequence points
  2822. @cindex full expression
  2823. There are some points in the code where C makes limited guarantees
  2824. about the order of operations. These are called @dfn{sequence
  2825. points}. Here is where they occur:
  2826. @itemize @bullet
  2827. @item
  2828. At the end of a @dfn{full expression}; that is to say, an expression
  2829. that is not part of a larger expression. All side effects specified
  2830. by that expression are carried out before execution moves
  2831. on to subsequent code.
  2832. @item
  2833. At the end of the first operand of certain operators: @samp{,},
  2834. @samp{&&}, @samp{||}, and @samp{?:}. All side effects specified by
  2835. that expression are carried out before any execution of the
  2836. next operand.
  2837. The commas that separate arguments in a function call are @emph{not}
  2838. comma operators, and they do not create sequence points. The rule
  2839. for function arguments and the rule for operands are different
  2840. (@pxref{Ordering of Operands}).
  2841. @item
  2842. Just before calling a function. All side effects specified by the
  2843. argument expressions are carried out before calling the function.
  2844. If the function to be called is not constant---that is, if it is
  2845. computed by an expression---all side effects in that expression are
  2846. carried out before calling the function.
  2847. @end itemize
  2848. The ordering imposed by a sequence point applies locally to a limited
  2849. range of code, as stated above in each case. For instance, the
  2850. ordering imposed by the comma operator does not apply to code outside
  2851. the operands of that comma operator. Thus, in this code,
  2852. @example
  2853. (x = 5, foo (x)) + x * x
  2854. @end example
  2855. @noindent
  2856. the sequence point of the comma operator orders @code{x = 5} before
  2857. @code{foo (x)}, but @code{x * x} could be computed before or after
  2858. them.
  2859. @node Postincrement and Ordering
  2860. @section Postincrement and Ordering
  2861. @cindex postincrement and ordering
  2862. @cindex ordering and postincrement
  2863. The ordering requirements for the postincrement and postdecrement
  2864. operations (@pxref{Postincrement/Postdecrement}) are loose: those side
  2865. effects must happen ``a little later,'' before the next sequence
  2866. point. That still leaves room for various orders that give different
  2867. results. In this expression,
  2868. @example
  2869. z = x++ - foo ()
  2870. @end example
  2871. @noindent
  2872. it's unpredictable whether @code{x} gets incremented before or after
  2873. calling the function @code{foo}. If @code{foo} refers to @code{x},
  2874. it might see the old value or it might see the incremented value.
  2875. In this perverse expression,
  2876. @example
  2877. x = x++
  2878. @end example
  2879. @noindent
  2880. @code{x} will certainly be incremented but the incremented value may
  2881. be replaced with the old value. That's because the incrementation and
  2882. the assignment may occur in either oder. If the incrementation of
  2883. @code{x} occurs after the assignment to @code{x}, the incremented
  2884. value will remain in place. But if the incrementation happens first,
  2885. the assignment will put the not-yet-incremented value back into
  2886. @code{x}, so the expression as a whole will leave @code{x} unchanged.
  2887. The conclusion: @strong{avoid such expressions}. Take care, when you
  2888. use postincrement and postdecrement, that the specific expression you
  2889. use is not ambiguous as to order of execution.
  2890. @node Ordering of Operands
  2891. @section Ordering of Operands
  2892. @cindex ordering of operands
  2893. @cindex operand ordering
  2894. Operands and arguments can be computed in any order, but there are limits to
  2895. this intermixing in GNU C:
  2896. @itemize @bullet
  2897. @item
  2898. The operands of a binary arithmetic operator can be computed in either
  2899. order, but they can't be intermixed: one of them has to come first,
  2900. followed by the other. Any side effects in the operand that's computed
  2901. first are executed before the other operand is computed.
  2902. @item
  2903. That applies to assignment operators too, except that, in simple assignment,
  2904. the previous value of the left operand is unused.
  2905. @item
  2906. The arguments in a function call can be computed in any order, but
  2907. they can't be intermixed. Thus, one argument is fully computed, then
  2908. another, and so on until they have all been done. Any side effects in
  2909. one argument are executed before computation of another argument
  2910. begins.
  2911. @end itemize
  2912. These rules don't cover side effects caused by postincrement and
  2913. postdecrement operators---those can be deferred up to the next
  2914. sequence point.
  2915. If you want to get pedantic, the fact is that GCC can reorder the
  2916. computations in many other ways provided that it doesn't alter the result
  2917. of running the program. However, because it doesn't alter the result
  2918. of running the program, it is negligible, unless you are concerned
  2919. with the values in certain variables at various times as seen by other
  2920. processes. In those cases, you should use @code{volatile} to prevent
  2921. optimizations that would make them behave strangely. @xref{volatile}.
  2922. @node Optimization and Ordering
  2923. @section Optimization and Ordering
  2924. @cindex optimization and ordering
  2925. @cindex ordering and optimization
  2926. Sequence points limit the compiler's freedom to reorder operations
  2927. arbitrarily, but optimizations can still reorder them if the compiler
  2928. concludes that this won't alter the results. Thus, in this code,
  2929. @example
  2930. x++;
  2931. y = z;
  2932. x++;
  2933. @end example
  2934. @noindent
  2935. there is a sequence point after each statement, so the code is
  2936. supposed to increment @code{x} once before the assignment to @code{y}
  2937. and once after. However, incrementing @code{x} has no effect on
  2938. @code{y} or @code{z}, and setting @code{y} can't affect @code{x}, so
  2939. the code could be optimized into this:
  2940. @example
  2941. y = z;
  2942. x += 2;
  2943. @end example
  2944. Normally that has no effect except to make the program faster. But
  2945. there are special situations where it can cause trouble due to things
  2946. that the compiler cannot know about, such as shared memory. To limit
  2947. optimization in those places, use the @code{volatile} type qualifier
  2948. (@pxref{volatile}).
  2949. @node Primitive Types
  2950. @chapter Primitive Data Types
  2951. @cindex primitive types
  2952. @cindex types, primitive
  2953. This chapter describes all the primitive data types of C---that is,
  2954. all the data types that aren't built up from other types. They
  2955. include the types @code{int} and @code{double} that we've already covered.
  2956. @menu
  2957. * Integer Types:: Description of integer types.
  2958. * Floating-Point Data Types:: Description of floating-point types.
  2959. * Complex Data Types:: Description of complex number types.
  2960. * The Void Type:: A type indicating no value at all.
  2961. * Other Data Types:: A brief summary of other types.
  2962. * Type Designators:: Referring to a data type abstractly.
  2963. @end menu
  2964. These types are all made up of bytes (@pxref{Storage}).
  2965. @node Integer Types
  2966. @section Integer Data Types
  2967. @cindex integer types
  2968. @cindex types, integer
  2969. Here we describe all the integer types and their basic
  2970. characteristics. @xref{Integers in Depth}, for more information about
  2971. the bit-level integer data representations and arithmetic.
  2972. @menu
  2973. * Basic Integers:: Overview of the various kinds of integers.
  2974. * Signed and Unsigned Types:: Integers can either hold both negative and
  2975. non-negative values, or only non-negative.
  2976. * Narrow Integers:: When to use smaller integer types.
  2977. * Integer Conversion:: Casting a value from one integer type
  2978. to another.
  2979. * Boolean Type:: An integer type for boolean values.
  2980. * Integer Variations:: Sizes of integer types can vary
  2981. across platforms.
  2982. @end menu
  2983. @node Basic Integers
  2984. @subsection Basic Integers
  2985. @findex char
  2986. @findex int
  2987. @findex short int
  2988. @findex long int
  2989. @findex long long int
  2990. Integer data types in C can be signed or unsigned. An unsigned type
  2991. can represent only positive numbers and zero. A signed type can
  2992. represent both positive and negative numbers, in a range spread almost
  2993. equally on both sides of zero.
  2994. Aside from signedness, the integer data types vary in size: how many
  2995. bytes long they are. The size determines the range of integer values
  2996. the type can hold.
  2997. Here's a list of the signed integer data types, with the sizes they
  2998. have on most computers. Each has a corresponding unsigned type; see
  2999. @ref{Signed and Unsigned Types}.
  3000. @table @code
  3001. @item signed char
  3002. One byte (8 bits). This integer type is used mainly for integers that
  3003. represent characters, usually as elements of arrays or fields of other
  3004. data structures.
  3005. @item short
  3006. @itemx short int
  3007. Two bytes (16 bits).
  3008. @item int
  3009. Four bytes (32 bits).
  3010. @item long
  3011. @itemx long int
  3012. Four bytes (32 bits) or eight bytes (64 bits), depending on the
  3013. platform. Typically it is 32 bits on 32-bit computers
  3014. and 64 bits on 64-bit computers, but there are exceptions.
  3015. @item long long
  3016. @itemx long long int
  3017. Eight bytes (64 bits). Supported in GNU C in the 1980s, and
  3018. incorporated into standard C as of ISO C99.
  3019. @end table
  3020. You can omit @code{int} when you use @code{long} or @code{short}.
  3021. This is harmless and customary.
  3022. @node Signed and Unsigned Types
  3023. @subsection Signed and Unsigned Types
  3024. @cindex signed types
  3025. @cindex unsigned types
  3026. @cindex types, signed
  3027. @cindex types, unsigned
  3028. @findex signed
  3029. @findex unsigned
  3030. An unsigned integer type can represent only positive numbers and zero.
  3031. A signed type can represent both positive and negative number, in a
  3032. range spread almost equally on both sides of zero. For instance,
  3033. @code{unsigned char} holds numbers from 0 to 255 (on most computers),
  3034. while @code{signed char} holds numbers from @minus{}128 to 127. Each of
  3035. these types holds 256 different possible values, since they are both 8
  3036. bits wide.
  3037. Write @code{signed} or @code{unsigned} before the type keyword to
  3038. specify a signed or an unsigned type. However, the integer types
  3039. other than @code{char} are signed by default; with them, @code{signed}
  3040. is a no-op.
  3041. Plain @code{char} may be signed or unsigned; this depends on the
  3042. compiler, the machine in use, and its operating system.
  3043. In many programs, it makes no difference whether @code{char} is
  3044. signed. When it does matter, don't leave it to chance; write
  3045. @code{signed char} or @code{unsigned char}.@footnote{Personal note from
  3046. Richard Stallman: Eating with hackers at a fish restaurant, I ordered
  3047. Arctic Char. When my meal arrived, I noted that the chef had not
  3048. signed it. So I complained, ``This char is unsigned---I wanted a
  3049. signed char!'' Or rather, I would have said this if I had thought of
  3050. it fast enough.}
  3051. @node Narrow Integers
  3052. @subsection Narrow Integers
  3053. The types that are narrower than @code{int} are rarely used for
  3054. ordinary variables---we declare them @code{int} instead. This is
  3055. because C converts those narrower types to @code{int} for any
  3056. arithmetic. There is literally no reason to declare a local variable
  3057. @code{char}, for instance.
  3058. In particular, if the value is really a character, you should declare
  3059. the variable @code{int}. Not @code{char}! Using that narrow type can
  3060. force the compiler to truncate values for conversion, which is a
  3061. waste. Furthermore, some functions return either a character value,
  3062. or @minus{}1 for ``no character.'' Using @code{int} makes it possible
  3063. to distinguish @minus{}1 from a character by sign.
  3064. The narrow integer types are useful as parts of other objects, such as
  3065. arrays and structures. Compare these array declarations, whose sizes
  3066. on 32-bit processors are shown:
  3067. @example
  3068. signed char ac[1000]; /* @r{1000 bytes} */
  3069. short as[1000]; /* @r{2000 bytes} */
  3070. int ai[1000]; /* @r{4000 bytes} */
  3071. long long all[1000]; /* @r{8000 bytes} */
  3072. @end example
  3073. In addition, character strings must be made up of @code{char}s,
  3074. because that's what all the standard library string functions expect.
  3075. Thus, array @code{ac} could be used as a character string, but the
  3076. others could not be.
  3077. @node Integer Conversion
  3078. @subsection Conversion among Integer Types
  3079. C converts between integer types implicitly in many situations. It
  3080. converts the narrow integer types, @code{char} and @code{short}, to
  3081. @code{int} whenever they are used in arithmetic. Assigning a new
  3082. value to an integer variable (or other lvalue) converts the value to
  3083. the variable's type.
  3084. You can also convert one integer type to another explicitly with a
  3085. @dfn{cast} operator. @xref{Explicit Type Conversion}.
  3086. The process of conversion to a wider type is straightforward: the
  3087. value is unchanged. The only exception is when converting a negative
  3088. value (in a signed type, obviously) to a wider unsigned type. In that
  3089. case, the result is a positive value with the same bits
  3090. (@pxref{Integers in Depth}).
  3091. @cindex truncation
  3092. Converting to a narrower type, also called @dfn{truncation}, involves
  3093. discarding some of the value's bits. This is not considered overflow
  3094. (@pxref{Integer Overflow}) because loss of significant bits is a
  3095. normal consequence of truncation. Likewise for conversion between
  3096. signed and unsigned types of the same width.
  3097. More information about conversion for assignment is in
  3098. @ref{Assignment Type Conversions}. For conversion for arithmetic,
  3099. see @ref{Argument Promotions}.
  3100. @node Boolean Type
  3101. @subsection Boolean Type
  3102. @cindex boolean type
  3103. @cindex type, boolean
  3104. @findex bool
  3105. The unsigned integer type @code{bool} holds truth values: its possible
  3106. values are 0 and 1. Converting any nonzero value to @code{bool}
  3107. results in 1. For example:
  3108. @example
  3109. bool a = 0;
  3110. bool b = 1;
  3111. bool c = 4; /* @r{Stores the value 1 in @code{c}.} */
  3112. @end example
  3113. Unlike @code{int}, @code{bool} is not a keyword. It is defined in
  3114. the header file @file{stdbool.h}.
  3115. @node Integer Variations
  3116. @subsection Integer Variations
  3117. The integer types of C have standard @emph{names}, but what they
  3118. @emph{mean} varies depending on the kind of platform in use:
  3119. which kind of computer, which operating system, and which compiler.
  3120. It may even depend on the compiler options used.
  3121. Plain @code{char} may be signed or unsigned; this depends on the
  3122. platform, too. Even for GNU C, there is no general rule.
  3123. In theory, all of the integer types' sizes can vary. @code{char} is
  3124. always considered one ``byte'' for C, but it is not necessarily an
  3125. 8-bit byte; on some platforms it may be more than 8 bits. ISO C
  3126. specifies only that none of these types is narrower than the ones
  3127. above it in the list in @ref{Basic Integers}, and that @code{short}
  3128. has at least 16 bits.
  3129. It is possible that in the future GNU C will support platforms where
  3130. @code{int} is 64 bits long. In practice, however, on today's real
  3131. computers, there is little variation; you can rely on the table
  3132. given previously (@pxref{Basic Integers}).
  3133. To be completely sure of the size of an integer type,
  3134. use the types @code{int16_t}, @code{int32_t} and @code{int64_t}.
  3135. Their corresponding unsigned types add @samp{u} at the front:
  3136. @code{uint16_t}, @code{uint32_t} and @code{uint64_t}.
  3137. To define all these types, include the header file @file{stdint.h}.
  3138. The GNU C Compiler can compile for some embedded controllers that use two
  3139. bytes for @code{int}. On some, @code{int} is just one ``byte,'' and
  3140. so is @code{short int}---but that ``byte'' may contain 16 bits or even
  3141. 32 bits. These processors can't support an ordinary operating system
  3142. (they may have their own specialized operating systems), and most C
  3143. programs do not try to support them.
  3144. @node Floating-Point Data Types
  3145. @section Floating-Point Data Types
  3146. @cindex floating-point types
  3147. @cindex types, floating-point
  3148. @findex double
  3149. @findex float
  3150. @findex long double
  3151. @dfn{Floating point} is the binary analogue of scientific notation:
  3152. internally it represents a number as a fraction and a binary exponent;
  3153. the value is that fraction multiplied by the specified power of 2.
  3154. (The C standard nominally permits other bases, but in GNU C the base
  3155. is always 2.)
  3156. @c ???
  3157. For instance, to represent 6, the fraction would be 0.75 and the
  3158. exponent would be 3; together they stand for the value @math{0.75 * 2@sup{3}},
  3159. meaning 0.75 * 8. The value 1.5 would use 0.75 as the fraction and 1
  3160. as the exponent. The value 0.75 would use 0.75 as the fraction and 0
  3161. as the exponent. The value 0.375 would use 0.75 as the fraction and
  3162. @minus{}1 as the exponent.
  3163. These binary exponents are used by machine instructions. You can
  3164. write a floating-point constant this way if you wish, using
  3165. hexadecimal; but normally we write floating-point numbers in decimal (base 10).
  3166. @xref{Floating Constants}.
  3167. C has three floating-point data types:
  3168. @table @code
  3169. @item double
  3170. ``Double-precision'' floating point, which uses 64 bits. This is the
  3171. normal floating-point type, and modern computers normally do
  3172. their floating-point computations in this type, or some wider type.
  3173. Except when there is a special reason to do otherwise, this is the
  3174. type to use for floating-point values.
  3175. @item float
  3176. ``Single-precision'' floating point, which uses 32 bits. It is useful
  3177. for floating-point values stored in structures and arrays, to save
  3178. space when the full precision of @code{double} is not needed. In
  3179. addition, single-precision arithmetic is faster on some computers, and
  3180. occasionally that is useful. But not often---most programs don't use
  3181. the type @code{float}.
  3182. C would be cleaner if @code{float} were the name of the type we
  3183. use for most floating-point values; however, for historical reasons,
  3184. that's not so.
  3185. @item long double
  3186. ``Extended-precision'' floating point is either 80-bit or 128-bit
  3187. precision, depending on the machine in use. On some machines, which
  3188. have no floating-point format wider than @code{double}, this is
  3189. equivalent to @code{double}.
  3190. @end table
  3191. Floating-point arithmetic raises many subtle issues. @xref{Floating
  3192. Point in Depth}, for more information.
  3193. @node Complex Data Types
  3194. @section Complex Data Types
  3195. @cindex complex numbers
  3196. @cindex types, complex
  3197. @cindex @code{_Complex} keyword
  3198. @cindex @code{__complex__} keyword
  3199. @findex _Complex
  3200. @findex __complex__
  3201. Complex numbers can include both a real part and an imaginary part.
  3202. The numeric constants covered above have real-numbered values. An
  3203. imaginary-valued constant is an ordinary real-valued constant followed
  3204. by @samp{i}.
  3205. To declare numeric variables as complex, use the @code{_Complex}
  3206. keyword.@footnote{For compatibility with older versions of GNU C, the
  3207. keyword @code{__complex__} is also allowed. Going forward, however,
  3208. use the new @code{_Complex} keyword as defined in ISO C11.} The
  3209. standard C complex data types are floating point,
  3210. @example
  3211. _Complex float foo;
  3212. _Complex double bar;
  3213. _Complex long double quux;
  3214. @end example
  3215. @noindent
  3216. but GNU C supports integer complex types as well.
  3217. Since @code{_Complex} is a keyword just like @code{float} and
  3218. @code{double} and @code{long}, the keywords can appear in any order,
  3219. but the order shown above seems most logical.
  3220. GNU C supports constants for complex values; for instance, @code{4.0 +
  3221. 3.0i} has the value 4 + 3i as type @code{_Complex double}.
  3222. @xref{Imaginary Constants}.
  3223. To pull the real and imaginary parts of the number back out, GNU C
  3224. provides the keywords @code{__real__} and @code{__imag__}:
  3225. @example
  3226. _Complex double foo = 4.0 + 3.0i;
  3227. double a = __real__ foo; /* @r{@code{a} is now 4.0.} */
  3228. double b = __imag__ foo; /* @r{@code{b} is now 3.0.} */
  3229. @end example
  3230. @noindent
  3231. Standard C does not include these keywords, and instead relies on
  3232. functions defined in @code{complex.h} for accessing the real and
  3233. imaginary parts of a complex number: @code{crealf}, @code{creal}, and
  3234. @code{creall} extract the real part of a float, double, or long double
  3235. complex number, respectively; @code{cimagf}, @code{cimag}, and
  3236. @code{cimagl} extract the imaginary part.
  3237. @cindex complex conjugation
  3238. GNU C also defines @samp{~} as an operator for complex conjugation,
  3239. which means negating the imaginary part of a complex number:
  3240. @example
  3241. _Complex double foo = 4.0 + 3.0i;
  3242. _Complex double bar = ~foo; /* @r{@code{bar} is now 4 @minus{} 3i.} */
  3243. @end example
  3244. @noindent
  3245. For standard C compatibility, you can use the appropriate library
  3246. function: @code{conjf}, @code{conj}, or @code{confl}.
  3247. @node The Void Type
  3248. @section The Void Type
  3249. @cindex void type
  3250. @cindex type, void
  3251. @findex void
  3252. The data type @code{void} is a dummy---it allows no operations. It
  3253. really means ``no value at all.'' When a function is meant to return
  3254. no value, we write @code{void} for its return type. Then
  3255. @code{return} statements in that function should not specify a value
  3256. (@pxref{return Statement}). Here's an example:
  3257. @example
  3258. void
  3259. print_if_positive (double x, double y)
  3260. @{
  3261. if (x <= 0)
  3262. return;
  3263. if (y <= 0)
  3264. return;
  3265. printf ("Next point is (%f,%f)\n", x, y);
  3266. @}
  3267. @end example
  3268. A @code{void}-returning function is comparable to what some other
  3269. languages (for instance, Fortran and Pascal) call a ``procedure''
  3270. instead of a ``function.''
  3271. @c ??? Already presented
  3272. @c @samp{%f} in an output template specifies to format a @code{double} value
  3273. @c as a decimal number, using a decimal point if needed.
  3274. @node Other Data Types
  3275. @section Other Data Types
  3276. Beyond the primitive types, C provides several ways to construct new
  3277. data types. For instance, you can define @dfn{pointers}, values that
  3278. represent the addresses of other data (@pxref{Pointers}). You can
  3279. define @dfn{structures}, as in many other languages
  3280. (@pxref{Structures}), and @dfn{unions}, which define multiple ways to
  3281. interpret the contents of the same memory space (@pxref{Unions}).
  3282. @dfn{Enumerations} are collections of named integer codes
  3283. (@pxref{Enumeration Types}).
  3284. @dfn{Array types} in C are used for allocating space for objects,
  3285. but C does not permit operating on an array value as a whole. @xref{Arrays}.
  3286. @node Type Designators
  3287. @section Type Designators
  3288. @cindex type designator
  3289. Some C constructs require a way to designate a specific data type
  3290. independent of any particular variable or expression which has that
  3291. type. The way to do this is with a @dfn{type designator}. The
  3292. constructs that need one include casts (@pxref{Explicit Type
  3293. Conversion}) and @code{sizeof} (@pxref{Type Size}).
  3294. We also use type designators to talk about the type of a value in C,
  3295. so you will see many type designators in this manual. When we say,
  3296. ``The value has type @code{int},'' @code{int} is a type designator.
  3297. To make the designator for any type, imagine a variable declaration
  3298. for a variable of that type and delete the variable name and the final
  3299. semicolon.
  3300. For example, to designate the type of full-word integers, we start
  3301. with the declaration for a variable @code{foo} with that type,
  3302. which is this:
  3303. @example
  3304. int foo;
  3305. @end example
  3306. @noindent
  3307. Then we delete the variable name @code{foo} and the semicolon, leaving
  3308. @code{int}---exactly the keyword used in such a declaration.
  3309. Therefore, the type designator for this type is @code{int}.
  3310. What about long unsigned integers? From the declaration
  3311. @example
  3312. unsigned long int foo;
  3313. @end example
  3314. @noindent
  3315. we determine that the designator is @code{unsigned long int}.
  3316. Following this procedure, the designator for any primitive type is
  3317. simply the set of keywords which specifies that type in a declaration.
  3318. The same is true for compound types such as structures, unions, and
  3319. enumerations.
  3320. Designators for pointer types do follow the rule of deleting the
  3321. variable name and semicolon, but the result is not so simple.
  3322. @xref{Pointer Type Designators}, as part of the chapter about
  3323. pointers. @xref{Array Type Designators}), for designators for array
  3324. types.
  3325. To understand what type a designator stands for, imagine a variable
  3326. name inserted into the right place in the designator to make a valid
  3327. declaration. What type would that variable be declared as? That is the
  3328. type the designator designates.
  3329. @node Constants
  3330. @chapter Constants
  3331. @cindex constants
  3332. A @dfn{constant} is an expression that stands for a specific value by
  3333. explicitly representing the desired value. C allows constants for
  3334. numbers, characters, and strings. We have already seen numeric and
  3335. string constants in the examples.
  3336. @menu
  3337. * Integer Constants:: Literal integer values.
  3338. * Integer Const Type:: Types of literal integer values.
  3339. * Floating Constants:: Literal floating-point values.
  3340. * Imaginary Constants:: Literal imaginary number values.
  3341. * Invalid Numbers:: Avoiding preprocessing number misconceptions.
  3342. * Character Constants:: Literal character values.
  3343. * String Constants:: Literal string values.
  3344. * UTF-8 String Constants:: Literal UTF-8 string values.
  3345. * Unicode Character Codes:: Unicode characters represented
  3346. in either UTF-16 or UTF-32.
  3347. * Wide Character Constants:: Literal characters values larger than 8 bits.
  3348. * Wide String Constants:: Literal string values made up of
  3349. 16- or 32-bit characters.
  3350. @end menu
  3351. @node Integer Constants
  3352. @section Integer Constants
  3353. @cindex integer constants
  3354. @cindex constants, integer
  3355. An integer constant consists of a number to specify the value,
  3356. followed optionally by suffix letters to specify the data type.
  3357. The simplest integer constants are numbers written in base 10
  3358. (decimal), such as @code{5}, @code{77}, and @code{403}. A decimal
  3359. constant cannot start with the character @samp{0} (zero) because
  3360. that makes the constant octal.
  3361. You can get the effect of a negative integer constant by putting a
  3362. minus sign at the beginning. In grammatical terms, that is an
  3363. arithmetic expression rather than a constant, but it behaves just like
  3364. a true constant.
  3365. Integer constants can also be written in octal (base 8), hexadecimal
  3366. (base 16), or binary (base 2). An octal constant starts with the
  3367. character @samp{0} (zero), followed by any number of octal digits
  3368. (@samp{0} to @samp{7}):
  3369. @example
  3370. 0 // @r{zero}
  3371. 077 // @r{63}
  3372. 0403 // @r{259}
  3373. @end example
  3374. @noindent
  3375. Pedantically speaking, the constant @code{0} is an octal constant, but
  3376. we can think of it as decimal; it has the same value either way.
  3377. A hexadecimal constant starts with @samp{0x} (upper or lower case)
  3378. followed by hex digits (@samp{0} to @samp{9}, as well as @samp{a}
  3379. through @samp{f} in upper or lower case):
  3380. @example
  3381. 0xff // @r{255}
  3382. 0XA0 // @r{160}
  3383. 0xffFF // @r{65535}
  3384. @end example
  3385. @cindex binary integer constants
  3386. A binary constant starts with @samp{0b} (upper or lower case) followed
  3387. by bits (each represented by the characters @samp{0} or @samp{1}):
  3388. @example
  3389. 0b101 // @r{5}
  3390. @end example
  3391. @noindent
  3392. Binary constants are a GNU C extension, not part of the C standard.
  3393. Sometimes a space is needed after an integer constant to avoid
  3394. lexical confusion with the following tokens. @xref{Invalid Numbers}.
  3395. @node Integer Const Type
  3396. @section Integer Constant Data Types
  3397. @cindex integer constant data types
  3398. @cindex constant data types, integer
  3399. @cindex types of integer constants
  3400. The type of an integer constant is normally @code{int}, if the value
  3401. fits in that type, but here are the complete rules. The type
  3402. of an integer constant is the first one in this sequence that can
  3403. properly represent the value,
  3404. @enumerate
  3405. @item
  3406. @code{int}
  3407. @item
  3408. @code{unsigned int}
  3409. @item
  3410. @code{long int}
  3411. @item
  3412. @code{unsigned long int}
  3413. @item
  3414. @code{long long int}
  3415. @item
  3416. @code{unsigned long long int}
  3417. @end enumerate
  3418. @noindent
  3419. and that isn't excluded by the following rules.
  3420. If the constant has @samp{l} or @samp{L} as a suffix, that excludes the
  3421. first two types (non-@code{long}).
  3422. If the constant has @samp{ll} or @samp{LL} as a suffix, that excludes
  3423. first four types (non-@code{long long}).
  3424. If the constant has @samp{u} or @samp{U} as a suffix, that excludes
  3425. the signed types.
  3426. Otherwise, if the constant is decimal (not binary, octal, or
  3427. hexadecimal), that excludes the unsigned types.
  3428. @c ### This said @code{unsigned int} is excluded.
  3429. @c ### See 17 April 2016
  3430. Here are some examples of the suffixes.
  3431. @example
  3432. 3000000000u // @r{three billion as @code{unsigned int}.}
  3433. 0LL // @r{zero as a @code{long long int}.}
  3434. 0403l // @r{259 as a @code{long int}.}
  3435. @end example
  3436. Suffixes in integer constants are rarely used. When the precise type
  3437. is important, it is cleaner to convert explicitly (@pxref{Explicit
  3438. Type Conversion}).
  3439. @xref{Integer Types}.
  3440. @node Floating Constants
  3441. @section Floating-Point Constants
  3442. @cindex floating-point constants
  3443. @cindex constants, floating-point
  3444. A floating-point constant must have either a decimal point, an
  3445. exponent-of-ten, or both; they distinguish it from an integer
  3446. constant.
  3447. To indicate an exponent, write @samp{e} or @samp{E}. The exponent
  3448. value follows. It is always written as a decimal number; it can
  3449. optionally start with a sign. The exponent @var{n} means to multiply
  3450. the constant's value by ten to the @var{n}th power.
  3451. Thus, @samp{1500.0}, @samp{15e2}, @samp{15e+2}, @samp{15.0e2},
  3452. @samp{1.5e+3}, @samp{.15e4}, and @samp{15000e-1} are six ways of
  3453. writing a floating-point number whose value is 1500. They are all
  3454. equivalent.
  3455. Here are more examples with decimal points:
  3456. @example
  3457. 1.0
  3458. 1000.
  3459. 3.14159
  3460. .05
  3461. .0005
  3462. @end example
  3463. For each of them, here are some equivalent constants written with
  3464. exponents:
  3465. @example
  3466. 1e0, 1.0000e0
  3467. 100e1, 100e+1, 100E+1, 1e3, 10000e-1
  3468. 3.14159e0
  3469. 5e-2, .0005e+2, 5E-2, .0005E2
  3470. .05e-2
  3471. @end example
  3472. A floating-point constant normally has type @code{double}. You can
  3473. force it to type @code{float} by adding @samp{f} or @samp{F}
  3474. at the end. For example,
  3475. @example
  3476. 3.14159f
  3477. 3.14159e0f
  3478. 1000.f
  3479. 100E1F
  3480. .0005f
  3481. .05e-2f
  3482. @end example
  3483. Likewise, @samp{l} or @samp{L} at the end forces the constant
  3484. to type @code{long double}.
  3485. You can use exponents in hexadecimal floating constants, but since
  3486. @samp{e} would be interpreted as a hexadecimal digit, the character
  3487. @samp{p} or @samp{P} (for ``power'') indicates an exponent.
  3488. The exponent in a hexadecimal floating constant is an optionally signed
  3489. decimal integer that specifies a power of 2 (@emph{not} 10 or 16) to
  3490. multiply into the number.
  3491. Here are some examples:
  3492. @example
  3493. @group
  3494. 0xAp2 // @r{40 in decimal}
  3495. 0xAp-1 // @r{5 in decimal}
  3496. 0x2.0Bp4 // @r{16.75 decimal}
  3497. 0xE.2p3 // @r{121 decimal}
  3498. 0x123.ABCp0 // @r{291.6708984375 in decimal}
  3499. 0x123.ABCp4 // @r{4666.734375 in decimal}
  3500. 0x100p-8 // @r{1}
  3501. 0x10p-4 // @r{1}
  3502. 0x1p+4 // @r{16}
  3503. 0x1p+8 // @r{256}
  3504. @end group
  3505. @end example
  3506. @xref{Floating-Point Data Types}.
  3507. @node Imaginary Constants
  3508. @section Imaginary Constants
  3509. @cindex imaginary constants
  3510. @cindex complex constants
  3511. @cindex constants, imaginary
  3512. A complex number consists of a real part plus an imaginary part. (You
  3513. may omit one part if it is zero.) This section explains how to write
  3514. numeric constants with imaginary values. By adding these to ordinary
  3515. real-valued numeric constants, we can make constants with complex
  3516. values.
  3517. The simple way to write an imaginary-number constant is to attach the
  3518. suffix @samp{i} or @samp{I}, or @samp{j} or @samp{J}, to an integer or
  3519. floating-point constant. For example, @code{2.5fi} has type
  3520. @code{_Complex float} and @code{3i} has type @code{_Complex int}.
  3521. The four alternative suffix letters are all equivalent.
  3522. @cindex _Complex_I
  3523. The other way to write an imaginary constant is to multiply a real
  3524. constant by @code{_Complex_I}, which represents the imaginary number
  3525. i. Standard C doesn't support suffixing with @samp{i} or @samp{j}, so
  3526. this clunky method is needed.
  3527. To write a complex constant with a nonzero real part and a nonzero
  3528. imaginary part, write the two separately and add them, like this:
  3529. @example
  3530. 4.0 + 3.0i
  3531. @end example
  3532. @noindent
  3533. That gives the value 4 + 3i, with type @code{_Complex double}.
  3534. Such a sum can include multiple real constants, or none. Likewise, it
  3535. can include multiple imaginary constants, or none. For example:
  3536. @example
  3537. _Complex double foo, bar, quux;
  3538. foo = 2.0i + 4.0 + 3.0i; /* @r{Imaginary part is 5.0.} */
  3539. bar = 4.0 + 12.0; /* @r{Imaginary part is 0.0.} */
  3540. quux = 3.0i + 15.0i; /* @r{Real part is 0.0.} */
  3541. @end example
  3542. @xref{Complex Data Types}.
  3543. @node Invalid Numbers
  3544. @section Invalid Numbers
  3545. Some number-like constructs which are not really valid as numeric
  3546. constants are treated as numbers in preprocessing directives. If
  3547. these constructs appear outside of preprocessing, they are erroneous.
  3548. @xref{Preprocessing Tokens}.
  3549. Sometimes we need to insert spaces to separate tokens so that they
  3550. won't be combined into a single number-like construct. For example,
  3551. @code{0xE+12} is a preprocessing number that is not a valid numeric
  3552. constant, so it is a syntax error. If what we want is the three
  3553. tokens @code{@w{0xE + 12}}, we have to insert two spaces as separators.
  3554. @node Character Constants
  3555. @section Character Constants
  3556. @cindex character constants
  3557. @cindex constants, character
  3558. @cindex escape sequence
  3559. A @dfn{character constant} is written with single quotes, as in
  3560. @code{'@var{c}'}. In the simplest case, @var{c} is a single ASCII
  3561. character that the constant should represent. The constant has type
  3562. @code{int}, and its value is the character code of that character.
  3563. For instance, @code{'a'} represents the character code for the letter
  3564. @samp{a}: 97, that is.
  3565. To put the @samp{'} character (single quote) in the character
  3566. constant, @dfn{escape} it with a backslash (@samp{\}). This character
  3567. constant looks like @code{'\''}. The backslash character here
  3568. functions as an @dfn{escape character}, and such a sequence,
  3569. starting with @samp{\}, is called an @dfn{escape sequence}.
  3570. To put the @samp{\} character (backslash) in the character constant,
  3571. escape it with @samp{\} (another backslash). This character
  3572. constant looks like @code{'\\'}.
  3573. @cindex bell character
  3574. @cindex @samp{\a}
  3575. @cindex backspace
  3576. @cindex @samp{\b}
  3577. @cindex tab (ASCII character)
  3578. @cindex @samp{\t}
  3579. @cindex vertical tab
  3580. @cindex @samp{\v}
  3581. @cindex formfeed
  3582. @cindex @samp{\f}
  3583. @cindex newline
  3584. @cindex @samp{\n}
  3585. @cindex return (ASCII character)
  3586. @cindex @samp{\r}
  3587. @cindex escape (ASCII character)
  3588. @cindex @samp{\e}
  3589. Here are all the escape sequences that represent specific
  3590. characters in a character constant. The numeric values shown are
  3591. the corresponding ASCII character codes, as decimal numbers.
  3592. @example
  3593. '\a' @result{} 7 /* @r{alarm, @kbd{CTRL-g}} */
  3594. '\b' @result{} 8 /* @r{backspace, @key{BS}, @kbd{CTRL-h}} */
  3595. '\t' @result{} 9 /* @r{tab, @key{TAB}, @kbd{CTRL-i}} */
  3596. '\n' @result{} 10 /* @r{newline, @kbd{CTRL-j}} */
  3597. '\v' @result{} 11 /* @r{vertical tab, @kbd{CTRL-k}} */
  3598. '\f' @result{} 12 /* @r{formfeed, @kbd{CTRL-l}} */
  3599. '\r' @result{} 13 /* @r{carriage return, @key{RET}, @kbd{CTRL-m}} */
  3600. '\e' @result{} 27 /* @r{escape character, @key{ESC}, @kbd{CTRL-[}} */
  3601. '\\' @result{} 92 /* @r{backslash character, @kbd{\}} */
  3602. '\'' @result{} 39 /* @r{single quote character, @kbd{'}} */
  3603. '\"' @result{} 34 /* @r{double quote character, @kbd{"}} */
  3604. '\?' @result{} 63 /* @r{question mark, @kbd{?}} */
  3605. @end example
  3606. @samp{\e} is a GNU C extension; to stick to standard C, write
  3607. @samp{\33}. (The number after @samp{backslash} is octal.) To specify
  3608. a character constant using decimal, use a cast; for instance,
  3609. @code{(unsigned char) 27}.
  3610. You can also write octal and hex character codes as
  3611. @samp{\@var{octalcode}} or @samp{\x@var{hexcode}}. Decimal is not an
  3612. option here, so octal codes do not need to start with @samp{0}.
  3613. The character constant's value has type @code{int}. However, the
  3614. character code is treated initially as a @code{char} value, which is
  3615. then converted to @code{int}. If the character code is greater than
  3616. 127 (@code{0177} in octal), the resulting @code{int} may be negative
  3617. on a platform where the type @code{char} is 8 bits long and signed.
  3618. @node String Constants
  3619. @section String Constants
  3620. @cindex string constants
  3621. @cindex constants, string
  3622. A @dfn{string constant} represents a series of characters. It starts
  3623. with @samp{"} and ends with @samp{"}; in between are the contents of
  3624. the string. Quoting special characters such as @samp{"}, @samp{\} and
  3625. newline in the contents works in string constants as in character
  3626. constants. In a string constant, @samp{'} does not need to be quoted.
  3627. A string constant defines an array of characters which contains the
  3628. specified characters followed by the null character (code 0). Using
  3629. the string constant is equivalent to using the name of an array with
  3630. those contents. In simple cases, where there are no backslash escape
  3631. sequences, the length in bytes of the string constant is one greater
  3632. than the number of characters written in it.
  3633. As with any array in C, using the string constant in an expression
  3634. converts the array to a pointer (@pxref{Pointers}) to the array's
  3635. first element (@pxref{Accessing Array Elements}). This pointer will
  3636. have type @code{char *} because it points to an element of type
  3637. @code{char}. @code{char *} is an example of a type designator for a
  3638. pointer type (@pxref{Pointer Type Designators}). That type is used
  3639. for strings generally, not just the strings expressed as constants
  3640. in a program.
  3641. Thus, the string constant @code{"Foo!"} is almost
  3642. equivalent to declaring an array like this
  3643. @example
  3644. char string_array_1[] = @{'F', 'o', 'o', '!', '\0' @};
  3645. @end example
  3646. @noindent
  3647. and then using @code{string_array_1} in the program. There
  3648. are two differences, however:
  3649. @itemize @bullet
  3650. @item
  3651. The string constant doesn't define a name for the array.
  3652. @item
  3653. The string constant is probably stored in a read-only area of memory.
  3654. @end itemize
  3655. Newlines are not allowed in the text of a string constant. The motive
  3656. for this prohibition is to catch the error of omitting the closing
  3657. @samp{"}. To put a newline in a constant string, write it as
  3658. @samp{\n} in the string constant.
  3659. A real null character in the source code inside a string constant
  3660. causes a warning. To put a null character in the middle of a string
  3661. constant, write @samp{\0} or @samp{\000}.
  3662. Consecutive string constants are effectively concatenated. Thus,
  3663. @example
  3664. "Fo" "o!" @r{is equivalent to} "Foo!"
  3665. @end example
  3666. This is useful for writing a string containing multiple lines,
  3667. like this:
  3668. @example
  3669. "This message is so long that it needs more than\n"
  3670. "a single line of text. C does not allow a newline\n"
  3671. "to represent itself in a string constant, so we have to\n"
  3672. "write \\n to put it in the string. For readability of\n"
  3673. "the source code, it is advisable to put line breaks in\n"
  3674. "the source where they occur in the contents of the\n"
  3675. "constant.\n"
  3676. @end example
  3677. The sequence of a backslash and a newline is ignored anywhere
  3678. in a C program, and that includes inside a string constant.
  3679. Thus, you can write multi-line string constants this way:
  3680. @example
  3681. "This is another way to put newlines in a string constant\n\
  3682. and break the line after them in the source code."
  3683. @end example
  3684. @noindent
  3685. However, concatenation is the recommended way to do this.
  3686. You can also write perverse string constants like this,
  3687. @example
  3688. "Fo\
  3689. o!"
  3690. @end example
  3691. @noindent
  3692. but don't do that---write it like this instead:
  3693. @example
  3694. "Foo!"
  3695. @end example
  3696. Be careful to avoid passing a string constant to a function that
  3697. modifies the string it receives. The memory where the string constant
  3698. is stored may be read-only, which would cause a fatal @code{SIGSEGV}
  3699. signal that normally terminates the function (@pxref{Signals}. Even
  3700. worse, the memory may not be read-only. Then the function might
  3701. modify the string constant, thus spoiling the contents of other string
  3702. constants that are supposed to contain the same value and are unified
  3703. by the compiler.
  3704. @node UTF-8 String Constants
  3705. @section UTF-8 String Constants
  3706. @cindex UTF-8 String Constants
  3707. Writing @samp{u8} immediately before a string constant, with no
  3708. intervening space, means to represent that string in UTF-8 encoding as
  3709. a sequence of bytes. UTF-8 represents ASCII characters with a single
  3710. byte, and represents non-ASCII Unicode characters (codes 128 and up)
  3711. as multibyte sequences. Here is an example of a UTF-8 constant:
  3712. @example
  3713. u8"A cónstàñt"
  3714. @end example
  3715. This constant occupies 13 bytes plus the terminating null,
  3716. because each of the accented letters is a two-byte sequence.
  3717. Concatenating an ordinary string with a UTF-8 string conceptually
  3718. produces another UTF-8 string. However, if the ordinary string
  3719. contains character codes 128 and up, the results cannot be relied on.
  3720. @node Unicode Character Codes
  3721. @section Unicode Character Codes
  3722. @cindex Unicode character codes
  3723. @cindex universal character names
  3724. You can specify Unicode characters, for individual character constants
  3725. or as part of string constants (@pxref{String Constants}), using
  3726. escape sequences. Use the @samp{\u} escape sequence with a 16-bit
  3727. hexadecimal Unicode character code. If the code value is too big for
  3728. 16 bits, use the @samp{\U} escape sequence with a 32-bit hexadecimal
  3729. Unicode character code. (These codes are called @dfn{universal
  3730. character names}.) For example,
  3731. @example
  3732. \u6C34 /* @r{16-bit code (UTF-16)} */
  3733. \U0010ABCD /* @r{32-bit code (UTF-32)} */
  3734. @end example
  3735. @noindent
  3736. One way to use these is in UTF-8 string constants (@pxref{UTF-8 String
  3737. Constants}). For instance,
  3738. @example
  3739. u8"fóó \u6C34 \U0010ABCD"
  3740. @end example
  3741. You can also use them in wide character constants (@pxref{Wide
  3742. Character Constants}), like this:
  3743. @example
  3744. u'\u6C34' /* @r{16-bit code} */
  3745. U'\U0010ABCD' /* @r{32-bit code} */
  3746. @end example
  3747. @noindent
  3748. and in wide string constants (@pxref{Wide String Constants}), like
  3749. this:
  3750. @example
  3751. u"\u6C34\u6C33" /* @r{16-bit code} */
  3752. U"\U0010ABCD" /* @r{32-bit code} */
  3753. @end example
  3754. Codes in the range of @code{D800} through @code{DFFF} are not valid
  3755. in Unicode. Codes less than @code{00A0} are also forbidden, except for
  3756. @code{0024}, @code{0040}, and @code{0060}; these characters are
  3757. actually ASCII control characters, and you can specify them with other
  3758. escape sequences (@pxref{Character Constants}).
  3759. @node Wide Character Constants
  3760. @section Wide Character Constants
  3761. @cindex wide character constants
  3762. @cindex constants, wide character
  3763. A @dfn{wide character constant} represents characters with more than 8
  3764. bits of character code. This is an obscure feature that we need to
  3765. document but that you probably won't ever use. If you're just
  3766. learning C, you may as well skip this section.
  3767. The original C wide character constant looks like @samp{L} (upper
  3768. case!) followed immediately by an ordinary character constant (with no
  3769. intervening space). Its data type is @code{wchar_t}, which is an
  3770. alias defined in @file{stddef.h} for one of the standard integer
  3771. types. Depending on the platform, it could be 16 bits or 32 bits. If
  3772. it is 16 bits, these character constants use the UTF-16 form of
  3773. Unicode; if 32 bits, UTF-32.
  3774. There are also Unicode wide character constants which explicitly
  3775. specify the width. These constants start with @samp{u} or @samp{U}
  3776. instead of @samp{L}. @samp{u} specifies a 16-bit Unicode wide
  3777. character constant, and @samp{U} a 32-bit Unicode wide character
  3778. constant. Their types are, respectively, @code{char16_t} and
  3779. @w{@code{char32_t}}; they are declared in the header file
  3780. @file{uchar.h}. These character constants are valid even if
  3781. @file{uchar.h} is not included, but some uses of them may be
  3782. inconvenient without including it to declare those type names.
  3783. The character represented in a wide character constant can be an
  3784. ordinary ASCII character. @code{L'a'}, @code{u'a'} and @code{U'a'}
  3785. are all valid, and they are all equal to @code{'a'}.
  3786. In all three kinds of wide character constants, you can write a
  3787. non-ASCII Unicode character in the constant itself; the constant's
  3788. value is the character's Unicode character code. Or you can specify
  3789. the Unicode character with an escape sequence (@pxref{Unicode
  3790. Character Codes}).
  3791. @node Wide String Constants
  3792. @section Wide String Constants
  3793. @cindex wide string constants
  3794. @cindex constants, wide string
  3795. A @dfn{wide string constant} stands for an array of 16-bit or 32-bit
  3796. characters. They are rarely used; if you're just
  3797. learning C, you may as well skip this section.
  3798. There are three kinds of wide string constants, which differ in the
  3799. data type used for each character in the string. Each wide string
  3800. constant is equivalent to an array of integers, but the data type of
  3801. those integers depends on the kind of wide string. Using the constant
  3802. in an expression will convert the array to a pointer to its first
  3803. element, as usual for arrays in C (@pxref{Accessing Array Elements}).
  3804. For each kind of wide string constant, we state here what type that
  3805. pointer will be.
  3806. @table @code
  3807. @item char16_t
  3808. This is a 16-bit Unicode wide string constant: each element is a
  3809. 16-bit Unicode character code with type @code{char16_t}, so the string
  3810. has the pointer type @code{char16_t@ *}. (That is a type designator;
  3811. @pxref{Pointer Type Designators}.) The constant is written as
  3812. @samp{u} (which must be lower case) followed (with no intervening
  3813. space) by a string constant with the usual syntax.
  3814. @item char32_t
  3815. This is a 32-bit Unicode wide string constant: each element is a
  3816. 32-bit Unicode character code, and the string has type @code{char32_t@ *}.
  3817. It's written as @samp{U} (which must be upper case) followed (with no
  3818. intervening space) by a string constant with the usual syntax.
  3819. @item wchar_t
  3820. This is the original kind of wide string constant. It's written as
  3821. @samp{L} (which must be upper case) followed (with no intervening
  3822. space) by a string constant with the usual syntax, and the string has
  3823. type @code{wchar_t@ *}.
  3824. The width of the data type @code{wchar_t} depends on the target
  3825. platform, which makes this kind of wide string somewhat less useful
  3826. than the newer kinds.
  3827. @end table
  3828. @code{char16_t} and @code{char32_t} are declared in the header file
  3829. @file{uchar.h}. @code{wchar_t} is declared in @file{stddef.h}.
  3830. Consecutive wide string constants of the same kind concatenate, just
  3831. like ordinary string constants. A wide string constant concatenated
  3832. with an ordinary string constant results in a wide string constant.
  3833. You can't concatenate two wide string constants of different kinds.
  3834. In addition, you can't concatenate a wide string constant (of any
  3835. kind) with a UTF-8 string constant.
  3836. @node Type Size
  3837. @chapter Type Size
  3838. @cindex type size
  3839. @cindex size of type
  3840. @findex sizeof
  3841. Each data type has a @dfn{size}, which is the number of bytes
  3842. (@pxref{Storage}) that it occupies in memory. To refer to the size in
  3843. a C program, use @code{sizeof}. There are two ways to use it:
  3844. @table @code
  3845. @item sizeof @var{expression}
  3846. This gives the size of @var{expression}, based on its data type. It
  3847. does not calculate the value of @var{expression}, only its size, so if
  3848. @var{expression} includes side effects or function calls, they do not
  3849. happen. Therefore, @code{sizeof} is always a compile-time operation
  3850. that has zero run-time cost.
  3851. A value that is a bit field (@pxref{Bit Fields}) is not allowed as an
  3852. operand of @code{sizeof}.
  3853. For example,
  3854. @example
  3855. double a;
  3856. i = sizeof a + 10;
  3857. @end example
  3858. @noindent
  3859. sets @code{i} to 18 on most computers because @code{a} occupies 8 bytes.
  3860. Here's how to determine the number of elements in an array
  3861. @code{array}:
  3862. @example
  3863. (sizeof array / sizeof array[0])
  3864. @end example
  3865. @noindent
  3866. The expression @code{sizeof array} gives the size of the array, not
  3867. the size of a pointer to an element. However, if @var{expression} is
  3868. a function parameter that was declared as an array, that
  3869. variable really has a pointer type (@pxref{Array Parm Pointer}), so
  3870. the result is the size of that pointer.
  3871. @item sizeof (@var{type})
  3872. This gives the size of @var{type}.
  3873. For example,
  3874. @example
  3875. i = sizeof (double) + 10;
  3876. @end example
  3877. @noindent
  3878. is equivalent to the previous example.
  3879. You can't apply @code{sizeof} to an incomplete type (@pxref{Incomplete
  3880. Types}), nor @code{void}. Using it on a function type gives 1 in GNU
  3881. C, which makes adding an integer to a function pointer work as desired
  3882. (@pxref{Pointer Arithmetic}).
  3883. @end table
  3884. @strong{Warning}: When you use @code{sizeof} with a type
  3885. instead of an expression, you must write parentheses around the type.
  3886. @strong{Warning}: When applying @code{sizeof} to the result of a cast
  3887. (@pxref{Explicit Type Conversion}), you must write parentheses around
  3888. the cast expression to avoid an ambiguity in the grammar of C@.
  3889. Specifically,
  3890. @example
  3891. sizeof (int) -x
  3892. @end example
  3893. @noindent
  3894. parses as
  3895. @example
  3896. (sizeof (int)) - x
  3897. @end example
  3898. @noindent
  3899. If what you want is
  3900. @example
  3901. sizeof ((int) -x)
  3902. @end example
  3903. @noindent
  3904. you must write it that way, with parentheses.
  3905. The data type of the value of the @code{sizeof} operator is always one
  3906. of the unsigned integer types; which one of those types depends on the
  3907. machine. The header file @code{stddef.h} defines the typedef name
  3908. @code{size_t} as an alias for this type. @xref{Defining Typedef
  3909. Names}.
  3910. @node Pointers
  3911. @chapter Pointers
  3912. @cindex pointers
  3913. Among high-level languages, C is rather low-level, close to the
  3914. machine. This is mainly because it has explicit @dfn{pointers}. A
  3915. pointer value is the numeric address of data in memory. The type of
  3916. data to be found at that address is specified by the data type of the
  3917. pointer itself. Nothing in C can determine the ``correct'' data type
  3918. of data in memory; it can only blindly follow the data type of the
  3919. pointer you use to access the data.
  3920. The unary operator @samp{*} gets the data that a pointer points
  3921. to---this is called @dfn{dereferencing the pointer}. Its value
  3922. always has the type that the pointer points to.
  3923. C also allows pointers to functions, but since there are some
  3924. differences in how they work, we treat them later. @xref{Function
  3925. Pointers}.
  3926. @menu
  3927. * Address of Data:: Using the ``address-of'' operator.
  3928. * Pointer Types:: For each type, there is a pointer type.
  3929. * Pointer Declarations:: Declaring variables with pointer types.
  3930. * Pointer Type Designators:: Designators for pointer types.
  3931. * Pointer Dereference:: Accessing what a pointer points at.
  3932. * Null Pointers:: Pointers which do not point to any object.
  3933. * Invalid Dereference:: Dereferencing null or invalid pointers.
  3934. * Void Pointers:: Totally generic pointers, can cast to any.
  3935. * Pointer Comparison:: Comparing memory address values.
  3936. * Pointer Arithmetic:: Computing memory address values.
  3937. * Pointers and Arrays:: Using pointer syntax instead of array syntax.
  3938. * Low-Level Pointer Arithmetic:: More about computing memory address values.
  3939. * Pointer Increment/Decrement:: Incrementing and decrementing pointers.
  3940. * Pointer Arithmetic Drawbacks:: A common pointer bug to watch out for.
  3941. * Pointer-Integer Conversion:: Converting pointer types to integer types.
  3942. * Printing Pointers:: Using @code{printf} for a pointer's value.
  3943. @end menu
  3944. @node Address of Data
  3945. @section Address of Data
  3946. @cindex address-of operator
  3947. The most basic way to make a pointer is with the ``address-of''
  3948. operator, @samp{&}. Let's suppose we have these variables available:
  3949. @example
  3950. int i;
  3951. double a[5];
  3952. @end example
  3953. Now, @code{&i} gives the address of the variable @code{i}---a pointer
  3954. value that points to @code{i}'s location---and @code{&a[3]} gives the
  3955. address of the element 3 of @code{a}. (It is actually the fourth
  3956. element in the array, since the first element has index 0.)
  3957. The address-of operator is unusual because it operates on a place to
  3958. store a value (an lvalue, @pxref{Lvalues}), not on the value currently
  3959. stored there. (The left argument of a simple assignment is unusual in
  3960. the same way.) You can use it on any lvalue except a bit field
  3961. (@pxref{Bit Fields}) or a constructor (@pxref{Structure
  3962. Constructors}).
  3963. @node Pointer Types
  3964. @section Pointer Types
  3965. For each data type @var{t}, there is a type for pointers to type
  3966. @var{t}. For these variables,
  3967. @example
  3968. int i;
  3969. double a[5];
  3970. @end example
  3971. @itemize @bullet
  3972. @item
  3973. @code{i} has type @code{int}; we say
  3974. @code{&i} is a ``pointer to @code{int}.''
  3975. @item
  3976. @code{a} has type @code{double[5]}; we say @code{&a} is a ``pointer to
  3977. arrays of five @code{double}s.''
  3978. @item
  3979. @code{a[3]} has type @code{double}; we say @code{&a[3]} is a ``pointer
  3980. to @code{double}.''
  3981. @end itemize
  3982. @node Pointer Declarations
  3983. @section Pointer-Variable Declarations
  3984. The way to declare that a variable @code{foo} points to type @var{t} is
  3985. @example
  3986. @var{t} *foo;
  3987. @end example
  3988. To remember this syntax, think ``if you dereference @code{foo}, using
  3989. the @samp{*} operator, what you get is type @var{t}. Thus, @code{foo}
  3990. points to type @var{t}.''
  3991. Thus, we can declare variables that hold pointers to these three
  3992. types, like this:
  3993. @example
  3994. int *ptri; /* @r{Pointer to @code{int}.} */
  3995. double *ptrd; /* @r{Pointer to @code{double}.} */
  3996. double (*ptrda)[5]; /* @r{Pointer to @code{double[5]}.} */
  3997. @end example
  3998. @samp{int *ptri;} means, ``if you dereference @code{ptri}, you get an
  3999. @code{int}.'' @samp{double (*ptrda)[5];} means, ``if you dereference
  4000. @code{ptrda}, then subscript it by an integer less than 5, you get a
  4001. @code{double}.'' The parentheses express the point that you would
  4002. dereference it first, then subscript it.
  4003. Contrast the last one with this:
  4004. @example
  4005. double *aptrd[5]; /* @r{Array of five pointers to @code{double}.} */
  4006. @end example
  4007. @noindent
  4008. Because @samp{*} has lower syntactic precedence than subscripting,
  4009. @samp{double *aptrd[5]} means, ``if you subscript @code{aptrd} by an
  4010. integer less than 5, then dereference it, you get a @code{double}.''
  4011. Therefore, @code{*aptrd[5]} declares an array of pointers, not a
  4012. pointer to an array.
  4013. @node Pointer Type Designators
  4014. @section Pointer-Type Designators
  4015. Every type in C has a designator; you make it by deleting the variable
  4016. name and the semicolon from a declaration (@pxref{Type
  4017. Designators}). Here are the designators for the pointer
  4018. types of the example declarations in the previous section:
  4019. @example
  4020. int * /* @r{Pointer to @code{int}.} */
  4021. double * /* @r{Pointer to @code{double}.} */
  4022. double (*)[5] /* @r{Pointer to @code{double[5]}.} */
  4023. @end example
  4024. Remember, to understand what type a designator stands for, imagine the
  4025. corresponding variable declaration with a variable name in it, and
  4026. figure out what type that variable would have. Thus, the type
  4027. designator @code{double (*)[5]} corresponds to the variable declaration
  4028. @code{double (*@var{variable})[5]}. That deciares a pointer variable
  4029. which, when dereferenced, gives an array of 5 @code{double}s.
  4030. So the type designator means, ``pointer to an array of 5 @code{double}s.''
  4031. @node Pointer Dereference
  4032. @section Dereferencing Pointers
  4033. @cindex dereferencing pointers
  4034. @cindex pointer dereferencing
  4035. The main use of a pointer value is to @dfn{dereference it} (access the
  4036. data it points at) with the unary @samp{*} operator. For instance,
  4037. @code{*&i} is the value at @code{i}'s address---which is just
  4038. @code{i}. The two expressions are equivalent, provided @code{&i} is
  4039. valid.
  4040. A pointer-dereference expression whose type is data (not a function)
  4041. is an lvalue.
  4042. Pointers become really useful when we store them somewhere and use
  4043. them later. Here's a simple example to illustrate the practice:
  4044. @example
  4045. @{
  4046. int i;
  4047. int *ptr;
  4048. ptr = &i;
  4049. i = 5;
  4050. @r{@dots{}}
  4051. return *ptr; /* @r{Returns 5, fetched from @code{i}.} */
  4052. @}
  4053. @end example
  4054. This shows how to declare the variable @code{ptr} as type
  4055. @code{int *} (pointer to @code{int}), store a pointer value into it
  4056. (pointing at @code{i}), and use it later to get the value of the
  4057. object it points at (the value in @code{i}).
  4058. If anyone can provide a useful example which is this basic,
  4059. I would be grateful.
  4060. @node Null Pointers
  4061. @section Null Pointers
  4062. @cindex null pointers
  4063. @cindex pointers, null
  4064. @c ???stdio loads sttddef
  4065. A pointer value can be @dfn{null}, which means it does not point to
  4066. any object. The cleanest way to get a null pointer is by writing
  4067. @code{NULL}, a standard macro defined in @file{stddef.h}. You can
  4068. also do it by casting 0 to the desired pointer type, as in
  4069. @code{(char *) 0}. (The cast operator performs explicit type conversion;
  4070. @xref{Explicit Type Conversion}.)
  4071. You can store a null pointer in any lvalue whose data type
  4072. is a pointer type:
  4073. @example
  4074. char *foo;
  4075. foo = NULL;
  4076. @end example
  4077. These two, if consecutive, can be combined into a declaration with
  4078. initializer,
  4079. @example
  4080. char *foo = NULL;
  4081. @end example
  4082. You can also explicitly cast @code{NULL} to the specific pointer type
  4083. you want---it makes no difference.
  4084. @example
  4085. char *foo;
  4086. foo = (char *) NULL;
  4087. @end example
  4088. To test whether a pointer is null, compare it with zero or
  4089. @code{NULL}, as shown here:
  4090. @example
  4091. if (p != NULL)
  4092. /* @r{@code{p} is not null.} */
  4093. operate (p);
  4094. @end example
  4095. Since testing a pointer for not being null is basic and frequent, all
  4096. but beginners in C will understand the conditional without need for
  4097. @code{!= NULL}:
  4098. @example
  4099. if (p)
  4100. /* @r{@code{p} is not null.} */
  4101. operate (p);
  4102. @end example
  4103. @node Invalid Dereference
  4104. @section Dereferencing Null or Invalid Pointers
  4105. Trying to dereference a null pointer is an error. On most platforms,
  4106. it generally causes a signal, usually @code{SIGSEGV}
  4107. (@pxref{Signals}).
  4108. @example
  4109. char *foo = NULL;
  4110. c = *foo; /* @r{This causes a signal and terminates.} */
  4111. @end example
  4112. @noindent
  4113. Likewise a pointer that has the wrong alignment for the target data type
  4114. (on most types of computer), or points to a part of memory that has
  4115. not been allocated in the process's address space.
  4116. The signal terminates the program, unless the program has arranged to
  4117. handle the signal (@pxref{Signal Handling, The GNU C Library, , libc,
  4118. The GNU C Library Reference Manual}).
  4119. However, the signal might not happen if the dereference is optimized
  4120. away. In the example above, if you don't subsequently use the value
  4121. of @code{c}, GCC might optimize away the code for @code{*foo}. You
  4122. can prevent such optimization using the @code{volatile} qualifier, as
  4123. shown here:
  4124. @example
  4125. volatile char *p;
  4126. volatile char c;
  4127. c = *p;
  4128. @end example
  4129. You can use this to test whether @code{p} points to unallocated
  4130. memory. Set up a signal handler first, so the signal won't terminate
  4131. the program.
  4132. @node Void Pointers
  4133. @section Void Pointers
  4134. @cindex void pointers
  4135. @cindex pointers, void
  4136. The peculiar type @code{void *}, a pointer whose target type is
  4137. @code{void}, is used often in C@. It represents a pointer to
  4138. we-don't-say-what. Thus,
  4139. @example
  4140. void *numbered_slot_pointer (int);
  4141. @end example
  4142. @noindent
  4143. declares a function @code{numbered_slot_pointer} that takes an
  4144. integer parameter and returns a pointer, but we don't say what type of
  4145. data it points to.
  4146. With type @code{void *}, you can pass the pointer around and test
  4147. whether it is null. However, dereferencing it gives a @code{void}
  4148. value that can't be used (@pxref{The Void Type}). To dereference the
  4149. pointer, first convert it to some other pointer type.
  4150. Assignments convert @code{void *} automatically to any other pointer
  4151. type, if the left operand has a pointer type; for instance,
  4152. @example
  4153. @{
  4154. int *p;
  4155. /* @r{Converts return value to @code{int *}.} */
  4156. p = numbered_slot_pointer (5);
  4157. @r{@dots{}}
  4158. @}
  4159. @end example
  4160. Passing an argument of type @code{void *} for a parameter that has a
  4161. pointer type also converts. For example, supposing the function
  4162. @code{hack} is declared to require type @code{float *} for its
  4163. argument, this will convert the null pointer to that type.
  4164. @example
  4165. /* @r{Declare @code{hack} that way.}
  4166. @r{We assume it is defined somewhere else.} */
  4167. void hack (float *);
  4168. @dots{}
  4169. /* @r{Now call @code{hack}.} */
  4170. @{
  4171. /* @r{Converts return value of @code{numbered_slot_pointer}}
  4172. @r{to @code{float *} to pass it to @code{hack}.} */
  4173. hack (numbered_slot_pointer (5));
  4174. @r{@dots{}}
  4175. @}
  4176. @end example
  4177. You can also convert to another pointer type with an explicit cast
  4178. (@pxref{Explicit Type Conversion}), like this:
  4179. @example
  4180. (int *) numbered_slot_pointer (5)
  4181. @end example
  4182. Here is an example which decides at run time which pointer
  4183. type to convert to:
  4184. @example
  4185. void
  4186. extract_int_or_double (void *ptr, bool its_an_int)
  4187. @{
  4188. if (its_an_int)
  4189. handle_an_int (*(int *)ptr);
  4190. else
  4191. handle_a_double (*(double *)ptr);
  4192. @}
  4193. @end example
  4194. The expression @code{*(int *)ptr} means to convert @code{ptr}
  4195. to type @code{int *}, then dereference it.
  4196. @node Pointer Comparison
  4197. @section Pointer Comparison
  4198. @cindex pointer comparison
  4199. @cindex comparison, pointer
  4200. Two pointer values are equal if they point to the same location, or if
  4201. they are both null. You can test for this with @code{==} and
  4202. @code{!=}. Here's a trivial example:
  4203. @example
  4204. @{
  4205. int i;
  4206. int *p, *q;
  4207. p = &i;
  4208. q = &i;
  4209. if (p == q)
  4210. printf ("This will be printed.\n");
  4211. if (p != q)
  4212. printf ("This won't be printed.\n");
  4213. @}
  4214. @end example
  4215. Ordering comparisons such as @code{>} and @code{>=} operate on
  4216. pointers by converting them to unsigned integers. The C standard says
  4217. the two pointers must point within the same object in memory, but on
  4218. GNU/Linux systems these operations simply compare the numeric values
  4219. of the pointers.
  4220. The pointer values to be compared should in principle have the same type, but
  4221. they are allowed to differ in limited cases. First of all, if the two
  4222. pointers' target types are nearly compatible (@pxref{Compatible
  4223. Types}), the comparison is allowed.
  4224. If one of the operands is @code{void *} (@pxref{Void Pointers}) and
  4225. the other is another pointer type, the comparison operator converts
  4226. the @code{void *} pointer to the other type so as to compare them.
  4227. (In standard C, this is not allowed if the other type is a function
  4228. pointer type, but it works in GNU C@.)
  4229. Comparison operators also allow comparing the integer 0 with a pointer
  4230. value. This works by converting 0 to a null pointer of the same type
  4231. as the other operand.
  4232. @node Pointer Arithmetic
  4233. @section Pointer Arithmetic
  4234. @cindex pointer arithmetic
  4235. @cindex arithmetic, pointer
  4236. Adding an integer (positive or negative) to a pointer is valid in C@.
  4237. It assumes that the pointer points to an element in an array, and
  4238. advances or retracts the pointer across as many array elements as the
  4239. integer specifies. Here is an example, in which adding a positive
  4240. integer advances the pointer to a later element in the same array.
  4241. @example
  4242. void
  4243. incrementing_pointers ()
  4244. @{
  4245. int array[5] = @{ 45, 29, 104, -3, 123456 @};
  4246. int elt0, elt1, elt4;
  4247. int *p = &array[0];
  4248. /* @r{Now @code{p} points at element 0. Fetch it.} */
  4249. elt0 = *p;
  4250. ++p;
  4251. /* @r{Now @code{p} points at element 1. Fetch it.} */
  4252. elt1 = *p;
  4253. p += 3;
  4254. /* @r{Now @code{p} points at element 4 (the last). Fetch it.} */
  4255. elt4 = *p;
  4256. printf ("elt0 %d elt1 %d elt4 %d.\n",
  4257. elt0, elt1, elt4);
  4258. /* @r{Prints elt0 45 elt1 29 elt4 123456.} */
  4259. @}
  4260. @end example
  4261. Here's an example where adding a negative integer retracts the pointer
  4262. to an earlier element in the same array.
  4263. @example
  4264. void
  4265. decrementing_pointers ()
  4266. @{
  4267. int array[5] = @{ 45, 29, 104, -3, 123456 @};
  4268. int elt0, elt3, elt4;
  4269. int *p = &array[4];
  4270. /* @r{Now @code{p} points at element 4 (the last). Fetch it.} */
  4271. elt4 = *p;
  4272. --p;
  4273. /* @r{Now @code{p} points at element 3. Fetch it.} */
  4274. elt3 = *p;
  4275. p -= 3;
  4276. /* @r{Now @code{p} points at element 0. Fetch it.} */
  4277. elt0 = *p;
  4278. printf ("elt0 %d elt3 %d elt4 %d.\n",
  4279. elt0, elt3, elt4);
  4280. /* @r{Prints elt0 45 elt3 -3 elt4 123456.} */
  4281. @}
  4282. @end example
  4283. If one pointer value was made by adding an integer to another
  4284. pointer value, it should be possible to subtract the pointer values
  4285. and recover that integer. That works too in C@.
  4286. @example
  4287. void
  4288. subtract_pointers ()
  4289. @{
  4290. int array[5] = @{ 45, 29, 104, -3, 123456 @};
  4291. int *p0, *p3, *p4;
  4292. int *p = &array[4];
  4293. /* @r{Now @code{p} points at element 4 (the last). Save the value.} */
  4294. p4 = p;
  4295. --p;
  4296. /* @r{Now @code{p} points at element 3. Save the value.} */
  4297. p3 = p;
  4298. p -= 3;
  4299. /* @r{Now @code{p} points at element 0. Save the value.} */
  4300. p0 = p;
  4301. printf ("%d, %d, %d, %d\n",
  4302. p4 - p0, p0 - p0, p3 - p0, p0 - p3);
  4303. /* @r{Prints 4, 0, 3, -3.} */
  4304. @}
  4305. @end example
  4306. The addition operation does not know where arrays begin or end in
  4307. memory. All it does is add the integer (multiplied by target object
  4308. size) to the numeric value of the pointer. When the initial pointer
  4309. and the result point into the same array, the result is well-defined.
  4310. @strong{Warning:} Only experts should do pointer arithmetic involving pointers
  4311. into different memory objects.
  4312. The difference between two pointers has type @code{int}, or
  4313. @code{long} if necessary (@pxref{Integer Types}). The clean way to
  4314. declare it is to use the typedef name @code{ptrdiff_t} defined in the
  4315. file @file{stddef.h}.
  4316. C defines pointer subtraction to be consistent with pointer-integer
  4317. addition, so that @code{(p3 - p1) + p1} equals @code{p3}, as in
  4318. ordinary algebra. Pointer subtraction works by subtracting
  4319. @code{p1}'s numeric value from @code{p3}'s, and dividing by target
  4320. object size. The two pointer arguments should point into the same
  4321. array.
  4322. In standard C, addition and subtraction are not allowed on @code{void
  4323. *}, since the target type's size is not defined in that case.
  4324. Likewise, they are not allowed on pointers to function types.
  4325. However, these operations work in GNU C, and the ``size of the target
  4326. type'' is taken as 1 byte.
  4327. @node Pointers and Arrays
  4328. @section Pointers and Arrays
  4329. @cindex pointers and arrays
  4330. @cindex arrays and pointers
  4331. The clean way to refer to an array element is
  4332. @code{@var{array}[@var{index}]}. Another, complicated way to do the
  4333. same job is to get the address of that element as a pointer, then
  4334. dereference it: @code{* (&@var{array}[0] + @var{index})} (or
  4335. equivalently @code{* (@var{array} + @var{index})}). This first gets a
  4336. pointer to element zero, then increments it with @code{+} to point to
  4337. the desired element, then gets the value from there.
  4338. That pointer-arithmetic construct is the @emph{definition} of square
  4339. brackets in C@. @code{@var{a}[@var{b}]} means, by definition,
  4340. @code{*(@var{a} + @var{b})}. This definition uses @var{a} and @var{b}
  4341. symmetrically, so one must be a pointer and the other an integer; it
  4342. does not matter which comes first.
  4343. Since indexing with square brackets is defined in terms of addition
  4344. and dereferencing, that too is symmetrical. Thus, you can write
  4345. @code{3[array]} and it is equivalent to @code{array[3]}. However, it
  4346. would be foolish to write @code{3[array]}, since it has no advantage
  4347. and could confuse people who read the code.
  4348. It may seem like a discrepancy that the definition @code{*(@var{a} +
  4349. @var{b})} requires a pointer, while @code{array[3]} uses an array value
  4350. instead. Why is this valid? The name of the array, when used by
  4351. itself as an expression (other than in @code{sizeof}), stands for a
  4352. pointer to the array's zeroth element. Thus, @code{array + 3}
  4353. converts @code{array} implicitly to @code{&array[0]}, and the result
  4354. is a pointer to element 3, equivalent to @code{&array[3]}.
  4355. Since square brackets are defined in terms of such an addition,
  4356. @code{array[3]} first converts @code{array} to a pointer. That's why
  4357. it works to use an array directly in that construct.
  4358. @node Low-Level Pointer Arithmetic
  4359. @section Pointer Arithmetic at Low-Level
  4360. @cindex pointer arithmetic, low-level
  4361. @cindex low level pointer arithmetic
  4362. The behavior of pointer arithmetic is theoretically defined only when
  4363. the pointer values all point within one object allocated in memory.
  4364. But the addition and subtraction operators can't tell whether the
  4365. pointer values are all within one object. They don't know where
  4366. objects start and end. So what do they really do?
  4367. Adding pointer @var{p} to integer @var{i} treats @var{p} as a memory
  4368. address, which is in fact an integer---call it @var{pint}. It treats
  4369. @var{i} as a number of elements of the type that @var{p} points to.
  4370. These elements' sizes add up to @code{@var{i} * sizeof (*@var{p})}.
  4371. So the sum, as an integer, is @code{@var{pint} + @var{i} * sizeof
  4372. (*@var{p})}. This value is reinterpreted as a pointer of the same
  4373. type as @var{p}.
  4374. If the starting pointer value @var{p} and the result do not point at
  4375. parts of the same object, the operation is not officially legitimate,
  4376. and C code is not ``supposed'' to do it. But you can do it anyway,
  4377. and it gives precisely the results described by the procedure above.
  4378. In some special situations it can do something useful, but non-wizards
  4379. should avoid it.
  4380. Here's a function to offset a pointer value @emph{as if} it pointed to
  4381. an object of any given size, by explicitly performing that calculation:
  4382. @example
  4383. #include <stdint.h>
  4384. void *
  4385. ptr_add (void *p, int i, int objsize)
  4386. @{
  4387. intptr_t p_address = (long) p;
  4388. intptr_t totalsize = i * objsize;
  4389. intptr_t new_address = p_address + totalsize;
  4390. return (void *) new_address;
  4391. @}
  4392. @end example
  4393. @noindent
  4394. @cindex @code{intptr_t}
  4395. This does the same job as @code{@var{p} + @var{i}} with the proper
  4396. pointer type for @var{p}. It uses the type @code{intptr_t}, which is
  4397. defined in the header file @file{stdint.h}. (In practice, @code{long
  4398. long} would always work, but it is cleaner to use @code{intptr_t}.)
  4399. @node Pointer Increment/Decrement
  4400. @section Pointer Increment and Decrement
  4401. @cindex pointer increment and decrement
  4402. @cindex incrementing pointers
  4403. @cindex decrementing pointers
  4404. The @samp{++} operator adds 1 to a variable. We have seen it for
  4405. integers (@pxref{Increment/Decrement}), but it works for pointers too.
  4406. For instance, suppose we have a series of positive integers,
  4407. terminated by a zero, and we want to add them up. Here is a simple
  4408. way to step forward through the array by advancing a pointer.
  4409. @example
  4410. int
  4411. sum_array_till_0 (int *p)
  4412. @{
  4413. int sum = 0;
  4414. for (;;)
  4415. @{
  4416. /* @r{Fetch the next integer.} */
  4417. int next = *p++;
  4418. /* @r{Exit the loop if it's 0.} */
  4419. if (next == 0)
  4420. break;
  4421. /* @r{Add it into running total.} */
  4422. sum += next;
  4423. @}
  4424. return sum;
  4425. @}
  4426. @end example
  4427. @noindent
  4428. The statement @samp{break;} will be explained further on (@pxref{break
  4429. Statement}). Used in this way, it immediately exits the surrounding
  4430. @code{for} statement.
  4431. @code{*p++} parses as @code{*(p++)}, because a postfix operator always
  4432. takes precedence over a prefix operator. Therefore, it dereferences
  4433. the entering value of @code{p}, then increments @code{p} afterwards.
  4434. Incrementing a variable means adding 1 to it, as in @code{p = p + 1}.
  4435. Since @code{p} is a pointer, adding 1 to it advances it by the width
  4436. of the datum it points to---in this case, @code{sizeof (int)}.
  4437. Therefore, each iteration of the loop picks up the next integer from
  4438. the series and puts it into @code{next}.
  4439. This @code{for}-loop has no initialization expression since @code{p}
  4440. and @code{sum} are already initialized, has no end-test since the
  4441. @samp{break;} statement will exit it, and needs no expression to
  4442. advance it since that's done within the loop by incrementing @code{p}
  4443. and @code{sum}. Thus, those three expressions after @code{for} are
  4444. left empty.
  4445. Another way to write this function is by keeping the parameter value unchanged
  4446. and using indexing to access the integers in the table.
  4447. @example
  4448. int
  4449. sum_array_till_0_indexing (int *p)
  4450. @{
  4451. int i;
  4452. int sum = 0;
  4453. for (i = 0; ; i++)
  4454. @{
  4455. /* @r{Fetch the next integer.} */
  4456. int next = p[i];
  4457. /* @r{Exit the loop if it's 0.} */
  4458. if (next == 0)
  4459. break;
  4460. /* @r{Add it into running total.} */
  4461. sum += next;
  4462. @}
  4463. return sum;
  4464. @}
  4465. @end example
  4466. In this program, instead of advancing @code{p}, we advance @code{i}
  4467. and add it to @code{p}. (Recall that @code{p[i]} means @code{*(p +
  4468. i)}.) Either way, it uses the same address to get the next integer.
  4469. It makes no difference in this program whether we write @code{i++} or
  4470. @code{++i}, because the value @emph{of that expression} is not used.
  4471. We use it for its effect, to increment @code{i}.
  4472. The @samp{--} operator also works on pointers; it can be used
  4473. to step backwards through an array, like this:
  4474. @example
  4475. int
  4476. after_last_nonzero (int *p, int len)
  4477. @{
  4478. /* @r{Set up @code{q} to point just after the last array element.} */
  4479. int *q = p + len;
  4480. while (q != p)
  4481. /* @r{Step @code{q} back until it reaches a nonzero element.} */
  4482. if (*--q != 0)
  4483. /* @r{Return the index of the element after that nonzero.} */
  4484. return q - p + 1;
  4485. return 0;
  4486. @}
  4487. @end example
  4488. That function returns the length of the nonzero part of the
  4489. array specified by its arguments; that is, the index of the
  4490. first zero of the run of zeros at the end.
  4491. @node Pointer Arithmetic Drawbacks
  4492. @section Drawbacks of Pointer Arithmetic
  4493. @cindex drawbacks of pointer arithmetic
  4494. @cindex pointer arithmetic, drawbacks
  4495. Pointer arithmetic is clean and elegant, but it is also the cause of a
  4496. major security flaw in the C language. Theoretically, it is only
  4497. valid to adjust a pointer within one object allocated as a unit in
  4498. memory. However, if you unintentionally adjust a pointer across the
  4499. bounds of the object and into some other object, the system has no way
  4500. to detect this error.
  4501. A bug which does that can easily result in clobbering (overwriting)
  4502. part of another object. For example, with @code{array[-1]} you can
  4503. read or write the nonexistent element before the beginning of an
  4504. array---probably part of some other data.
  4505. Combining pointer arithmetic with casts between pointer types, you can
  4506. create a pointer that fails to be properly aligned for its type. For
  4507. example,
  4508. @example
  4509. int a[2];
  4510. char *pa = (char *)a;
  4511. int *p = (int *)(pa + 1);
  4512. @end example
  4513. @noindent
  4514. gives @code{p} a value pointing to an ``integer'' that includes part
  4515. of @code{a[0]} and part of @code{a[1]}. Dereferencing that with
  4516. @code{*p} can cause a fatal @code{SIGSEGV} signal or it can return the
  4517. contents of that badly aligned @code{int} (@pxref{Signals}. If it
  4518. ``works,'' it may be quite slow. It can also cause aliasing
  4519. confusions (@pxref{Aliasing}).
  4520. @strong{Warning:} Using improperly aligned pointers is risky---don't do it
  4521. unless it is really necessary.
  4522. @node Pointer-Integer Conversion
  4523. @section Pointer-Integer Conversion
  4524. @cindex pointer-integer conversion
  4525. @cindex conversion between pointers and integers
  4526. @cindex @code{uintptr_t}
  4527. On modern computers, an address is simply a number. It occupies the
  4528. same space as some size of integer. In C, you can convert a pointer
  4529. to the appropriate integer types and vice versa, without losing
  4530. information. The appropriate integer types are @code{uintptr_t} (an
  4531. unsigned type) and @code{intptr_t} (a signed type). Both are defined
  4532. in @file{stdint.h}.
  4533. For instance,
  4534. @example
  4535. #include <stdint.h>
  4536. #include <stdio.h>
  4537. void
  4538. print_pointer (void *ptr)
  4539. @{
  4540. uintptr_t converted = (uintptr_t) ptr;
  4541. printf ("Pointer value is 0x%x\n",
  4542. (unsigned int) converted);
  4543. @}
  4544. @end example
  4545. @noindent
  4546. The specification @samp{%x} in the template (the first argument) for
  4547. @code{printf} means to represent this argument using hexadecimal
  4548. notation. It's cleaner to use @code{uintptr_t}, since hexadecimal
  4549. printing treats the number as unsigned, but it won't actually matter:
  4550. all @code{printf} gets to see is the series of bits in the number.
  4551. @strong{Warning:} Converting pointers to integers is risky---don't do
  4552. it unless it is really necessary.
  4553. @node Printing Pointers
  4554. @section Printing Pointers
  4555. To print the numeric value of a pointer, use the @samp{%p} specifier.
  4556. For example:
  4557. @example
  4558. void
  4559. print_pointer (void *ptr)
  4560. @{
  4561. printf ("Pointer value is %p\n", ptr);
  4562. @}
  4563. @end example
  4564. The specification @samp{%p} works with any pointer type. It prints
  4565. @samp{0x} followed by the address in hexadecimal, printed as the
  4566. appropriate unsigned integer type.
  4567. @node Structures
  4568. @chapter Structures
  4569. @cindex structures
  4570. @findex struct
  4571. @cindex fields in structures
  4572. A @dfn{structure} is a user-defined data type that holds various
  4573. @dfn{fields} of data. Each field has a name and a data type specified
  4574. in the structure's definition.
  4575. Here we define a structure suitable for storing a linked list of
  4576. integers. Each list item will hold one integer, plus a pointer
  4577. to the next item.
  4578. @example
  4579. struct intlistlink
  4580. @{
  4581. int datum;
  4582. struct intlistlink *next;
  4583. @};
  4584. @end example
  4585. The structure definition has a @dfn{type tag} so that the code can
  4586. refer to this structure. The type tag here is @code{intlistlink}.
  4587. The definition refers recursively to the same structure through that
  4588. tag.
  4589. You can define a structure without a type tag, but then you can't
  4590. refer to it again. That is useful only in some special contexts, such
  4591. as inside a @code{typedef} or a @code{union}.
  4592. The contents of the structure are specified by the @dfn{field
  4593. declarations} inside the braces. Each field in the structure needs a
  4594. declaration there. The fields in one structure definition must have
  4595. distinct names, but these names do not conflict with any other names
  4596. in the program.
  4597. A field declaration looks just like a variable declaration. You can
  4598. combine field declarations with the same beginning, just as you can
  4599. combine variable declarations.
  4600. This structure has two fields. One, named @code{datum}, has type
  4601. @code{int} and will hold one integer in the list. The other, named
  4602. @code{next}, is a pointer to another @code{struct intlistlink}
  4603. which would be the rest of the list. In the last list item, it would
  4604. be @code{NULL}.
  4605. This structure definition is recursive, since the type of the
  4606. @code{next} field refers to the structure type. Such recursion is not
  4607. a problem; in fact, you can use the type @code{struct intlistlink *}
  4608. before the definition of the type @code{struct intlistlink} itself.
  4609. That works because pointers to all kinds of structures really look the
  4610. same at the machine level.
  4611. After defining the structure, you can declare a variable of type
  4612. @code{struct intlistlink} like this:
  4613. @example
  4614. struct intlistlink foo;
  4615. @end example
  4616. The structure definition itself can serve as the beginning of a
  4617. variable declaration, so you can declare variables immediately after,
  4618. like this:
  4619. @example
  4620. struct intlistlink
  4621. @{
  4622. int datum;
  4623. struct intlistlink *next;
  4624. @} foo;
  4625. @end example
  4626. @noindent
  4627. But that is ugly. It is almost always clearer to separate the
  4628. definition of the structure from its uses.
  4629. Declaring a structure type inside a block (@pxref{Blocks}) limits
  4630. the scope of the structure type name to that block. That means the
  4631. structure type is recognized only within that block. Declaring it in
  4632. a function parameter list, as here,
  4633. @example
  4634. int f (struct foo @{int a, b@} parm);
  4635. @end example
  4636. @noindent
  4637. (assuming that @code{struct foo} is not already defined) limits the
  4638. scope of the structure type @code{struct foo} to that parameter list;
  4639. that is basically useless, so it triggers a warning.
  4640. Standard C requires at least one field in a structure.
  4641. GNU C does not require this.
  4642. @menu
  4643. * Referencing Fields:: Accessing field values in a structure object.
  4644. * Dynamic Memory Allocation:: Allocating space for objects
  4645. while the program is running.
  4646. * Field Offset:: Memory layout of fields within a structure.
  4647. * Structure Layout:: Planning the memory layout of fields.
  4648. * Packed Structures:: Packing structure fields as close as possible.
  4649. * Bit Fields:: Dividing integer fields
  4650. into fields with fewer bits.
  4651. * Bit Field Packing:: How bit fields pack together in integers.
  4652. * const Fields:: Making structure fields immutable.
  4653. * Zero Length:: Zero-length array as a variable-length object.
  4654. * Flexible Array Fields:: Another approach to variable-length objects.
  4655. * Overlaying Structures:: Casting one structure type
  4656. over an object of another structure type.
  4657. * Structure Assignment:: Assigning values to structure objects.
  4658. * Unions:: Viewing the same object in different types.
  4659. * Packing With Unions:: Using a union type to pack various types into
  4660. the same memory space.
  4661. * Cast to Union:: Casting a value one of the union's alternative
  4662. types to the type of the union itself.
  4663. * Structure Constructors:: Building new structure objects.
  4664. * Unnamed Types as Fields:: Fields' types do not always need names.
  4665. * Incomplete Types:: Types which have not been fully defined.
  4666. * Intertwined Incomplete Types:: Defining mutually-recursive structure types.
  4667. * Type Tags:: Scope of structure and union type tags.
  4668. @end menu
  4669. @node Referencing Fields
  4670. @section Referencing Structure Fields
  4671. @cindex referencing structure fields
  4672. @cindex structure fields, referencing
  4673. To make a structure useful, there has to be a way to examine and store
  4674. its fields. The @samp{.} (period) operator does that; its use looks
  4675. like @code{@var{object}.@var{field}}.
  4676. Given this structure and variable,
  4677. @example
  4678. struct intlistlink
  4679. @{
  4680. int datum;
  4681. struct intlistlink *next;
  4682. @};
  4683. struct intlistlink foo;
  4684. @end example
  4685. @noindent
  4686. you can write @code{foo.datum} and @code{foo.next} to refer to the two
  4687. fields in the value of @code{foo}. These fields are lvalues, so you
  4688. can store values into them, and read the values out again.
  4689. Most often, structures are dynamically allocated (see the next
  4690. section), and we refer to the objects via pointers.
  4691. @code{(*p).@var{field}} is somewhat cumbersome, so there is an
  4692. abbreviation: @code{p->@var{field}}. For instance, assume the program
  4693. contains this declaration:
  4694. @example
  4695. struct intlistlink *ptr;
  4696. @end example
  4697. @noindent
  4698. You can write @code{ptr->datum} and @code{ptr->next} to refer
  4699. to the two fields in the object that @code{ptr} points to.
  4700. If a unary operator precedes an expression using @samp{->},
  4701. the @samp{->} nests inside:
  4702. @example
  4703. -ptr->datum @r{is equivalent to} -(ptr->datum)
  4704. @end example
  4705. You can intermix @samp{->} and @samp{.} without parentheses,
  4706. as shown here:
  4707. @example
  4708. struct @{ double d; struct intlistlink l; @} foo;
  4709. @r{@dots{}}foo.l.next->next->datum@r{@dots{}}
  4710. @end example
  4711. @node Dynamic Memory Allocation
  4712. @section Dynamic Memory Allocation
  4713. @cindex dynamic memory allocation
  4714. @cindex memory allocation, dynamic
  4715. @cindex allocating memory dynamically
  4716. To allocate an object dynamically, call the library function
  4717. @code{malloc} (@pxref{Basic Allocation, The GNU C Library,, libc, The GNU C Library
  4718. Reference Manual}). Here is how to allocate an object of type
  4719. @code{struct intlistlink}. To make this code work, include the file
  4720. @file{stdlib.h}, like this:
  4721. @example
  4722. #include <stddef.h> /* @r{Defines @code{NULL}.} */
  4723. #include <stdlib.h> /* @r{Declares @code{malloc}.} */
  4724. @dots{}
  4725. struct intlistlink *
  4726. alloc_intlistlink ()
  4727. @{
  4728. struct intlistlink *p;
  4729. p = malloc (sizeof (struct intlistlink));
  4730. if (p == NULL)
  4731. fatal ("Ran out of storage");
  4732. /* @r{Initialize the contents.} */
  4733. p->datum = 0;
  4734. p->next = NULL;
  4735. return p;
  4736. @}
  4737. @end example
  4738. @noindent
  4739. @code{malloc} returns @code{void *}, so the assignment to @code{p}
  4740. will automatically convert it to type @code{struct intlistlink *}.
  4741. The return value of @code{malloc} is always sufficiently aligned
  4742. (@pxref{Type Alignment}) that it is valid for any data type.
  4743. The test for @code{p == NULL} is necessary because @code{malloc}
  4744. returns a null pointer if it cannot get any storage. We assume that
  4745. the program defines the function @code{fatal} to report a fatal error
  4746. to the user.
  4747. Here's how to add one more integer to the front of such a list:
  4748. @example
  4749. struct intlistlink *my_list = NULL;
  4750. void
  4751. add_to_mylist (int my_int)
  4752. @{
  4753. struct intlistlink *p = alloc_intlistlink ();
  4754. p->datum = my_int;
  4755. p->next = mylist;
  4756. mylist = p;
  4757. @}
  4758. @end example
  4759. The way to free the objects is by calling @code{free}. Here's
  4760. a function to free all the links in one of these lists:
  4761. @example
  4762. void
  4763. free_intlist (struct intlistlink *p)
  4764. @{
  4765. while (p)
  4766. @{
  4767. struct intlistlink *q = p;
  4768. p = p->next;
  4769. free (q);
  4770. @}
  4771. @}
  4772. @end example
  4773. We must extract the @code{next} pointer from the object before freeing
  4774. it, because @code{free} can clobber the data that was in the object.
  4775. For the same reason, the program must not use the list any more after
  4776. freeing its elements. To make sure it won't, it is best to clear out
  4777. the variable where the list was stored, like this:
  4778. @example
  4779. free_intlist (mylist);
  4780. mylist = NULL;
  4781. @end example
  4782. @node Field Offset
  4783. @section Field Offset
  4784. @cindex field offset
  4785. @cindex structure field offset
  4786. @cindex offset of structure fields
  4787. To determine the offset of a given field @var{field} in a structure
  4788. type @var{type}, use the macro @code{offsetof}, which is defined in
  4789. the file @file{stddef.h}. It is used like this:
  4790. @example
  4791. offsetof (@var{type}, @var{field})
  4792. @end example
  4793. Here is an example:
  4794. @example
  4795. struct foo
  4796. @{
  4797. int element;
  4798. struct foo *next;
  4799. @};
  4800. offsetof (struct foo, next)
  4801. /* @r{On most machines that is 4. It may be 8.} */
  4802. @end example
  4803. @node Structure Layout
  4804. @section Structure Layout
  4805. @cindex structure layout
  4806. @cindex layout of structures
  4807. The rest of this chapter covers advanced topics about structures. If
  4808. you are just learning C, you can skip it.
  4809. The precise layout of a @code{struct} type is crucial when using it to
  4810. overlay hardware registers, to access data structures in shared
  4811. memory, or to assemble and disassemble packets for network
  4812. communication. It is also important for avoiding memory waste when
  4813. the program makes many objects of that type. However, the layout
  4814. depends on the target platform. Each platform has conventions for
  4815. structure layout, which compilers need to follow.
  4816. Here are the conventions used on most platforms.
  4817. The structure's fields appear in the structure layout in the order
  4818. they are declared. When possible, consecutive fields occupy
  4819. consecutive bytes within the structure. However, if a field's type
  4820. demands more alignment than it would get that way, C gives it the
  4821. alignment it requires by leaving a gap after the previous field.
  4822. Once all the fields have been laid out, it is possible to determine
  4823. the structure's alignment and size. The structure's alignment is the
  4824. maximum alignment of any of the fields in it. Then the structure's
  4825. size is rounded up to a multiple of its alignment. That may require
  4826. leaving a gap at the end of the structure.
  4827. Here are some examples, where we assume that @code{char} has size and
  4828. alignment 1 (always true), and @code{int} has size and alignment 4
  4829. (true on most kinds of computers):
  4830. @example
  4831. struct foo
  4832. @{
  4833. char a, b;
  4834. int c;
  4835. @};
  4836. @end example
  4837. @noindent
  4838. This structure occupies 8 bytes, with an alignment of 4. @code{a} is
  4839. at offset 0, @code{b} is at offset 1, and @code{c} is at offset 4.
  4840. There is a gap of 2 bytes before @code{c}.
  4841. Contrast that with this structure:
  4842. @example
  4843. struct foo
  4844. @{
  4845. char a;
  4846. int c;
  4847. char b;
  4848. @};
  4849. @end example
  4850. This structure has size 12 and alignment 4. @code{a} is at offset 0,
  4851. @code{c} is at offset 4, and @code{b} is at offset 8. There are two
  4852. gaps: three bytes before @code{c}, and three bytes at the end.
  4853. These two structures have the same contents at the C level, but one
  4854. takes 8 bytes and the other takes 12 bytes due to the ordering of the
  4855. fields. A reliable way to avoid this sort of wastage is to order the
  4856. fields by size, biggest fields first.
  4857. @node Packed Structures
  4858. @section Packed Structures
  4859. @cindex packed structures
  4860. @cindex @code{__attribute__((packed))}
  4861. In GNU C you can force a structure to be laid out with no gaps by
  4862. adding @code{__attribute__((packed))} after @code{struct} (or at the
  4863. end of the structure type declaration). Here's an example:
  4864. @example
  4865. struct __attribute__((packed)) foo
  4866. @{
  4867. char a;
  4868. int c;
  4869. char b;
  4870. @};
  4871. @end example
  4872. Without @code{__attribute__((packed))}, this structure occupies 12
  4873. bytes (as described in the previous section), assuming 4-byte
  4874. alignment for @code{int}. With @code{__attribute__((packed))}, it is
  4875. only 6 bytes long---the sum of the lengths of its fields.
  4876. Use of @code{__attribute__((packed))} often results in fields that
  4877. don't have the normal alignment for their types. Taking the address
  4878. of such a field can result in an invalid pointer because of its
  4879. improper alignment. Dereferencing such a pointer can cause a
  4880. @code{SIGSEGV} signal on a machine that doesn't, in general, allow
  4881. unaligned pointers.
  4882. @xref{Attributes}.
  4883. @node Bit Fields
  4884. @section Bit Fields
  4885. @cindex bit fields
  4886. A structure field declaration with an integer type can specify the
  4887. number of bits the field should occupy. We call that a @dfn{bit
  4888. field}. These are useful because consecutive bit fields are packed
  4889. into a larger storage unit. For instance,
  4890. @example
  4891. unsigned char opcode: 4;
  4892. @end example
  4893. @noindent
  4894. specifies that this field takes just 4 bits.
  4895. Since it is unsigned, its possible values range
  4896. from 0 to 15. A signed field with 4 bits, such as this,
  4897. @example
  4898. signed char small: 4;
  4899. @end example
  4900. @noindent
  4901. can hold values from -8 to 7.
  4902. You can subdivide a single byte into those two parts by writing
  4903. @example
  4904. unsigned char opcode: 4;
  4905. signed char small: 4;
  4906. @end example
  4907. @noindent
  4908. in the structure. With bit fields, these two numbers fit into
  4909. a single @code{char}.
  4910. Here's how to declare a one-bit field that can hold either 0 or 1:
  4911. @example
  4912. unsigned char special_flag: 1;
  4913. @end example
  4914. You can also use the @code{bool} type for bit fields:
  4915. @example
  4916. bool special_flag: 1;
  4917. @end example
  4918. Except when using @code{bool} (which is always unsigned,
  4919. @pxref{Boolean Type}), always specify @code{signed} or @code{unsigned}
  4920. for a bit field. There is a default, if that's not specified: the bit
  4921. field is signed if plain @code{char} is signed, except that the option
  4922. @option{-funsigned-bitfields} forces unsigned as the default. But it
  4923. is cleaner not to depend on this default.
  4924. Bit fields are special in that you cannot take their address with
  4925. @samp{&}. They are not stored with the size and alignment appropriate
  4926. for the specified type, so they cannot be addressed through pointers
  4927. to that type.
  4928. @node Bit Field Packing
  4929. @section Bit Field Packing
  4930. Programs to communicate with low-level hardware interfaces need to
  4931. define bit fields laid out to match the hardware data. This section
  4932. explains how to do that.
  4933. Consecutive bit fields are packed together, but each bit field must
  4934. fit within a single object of its specified type. In this example,
  4935. @example
  4936. unsigned short a : 3, b : 3, c : 3, d : 3, e : 3;
  4937. @end example
  4938. @noindent
  4939. all five fields fit consecutively into one two-byte @code{short}.
  4940. They need 15 bits, and one @code{short} provides 16. By contrast,
  4941. @example
  4942. unsigned char a : 3, b : 3, c : 3, d : 3, e : 3;
  4943. @end example
  4944. @noindent
  4945. needs three bytes. It fits @code{a} and @code{b} into one
  4946. @code{char}, but @code{c} won't fit in that @code{char} (they would
  4947. add up to 9 bits). So @code{c} and @code{d} go into a second
  4948. @code{char}, leaving a gap of two bits between @code{b} and @code{c}.
  4949. Then @code{e} needs a third @code{char}. By contrast,
  4950. @example
  4951. unsigned char a : 3, b : 3;
  4952. unsigned int c : 3;
  4953. unsigned char d : 3, e : 3;
  4954. @end example
  4955. @noindent
  4956. needs only two bytes: the type @code{unsigned int}
  4957. allows @code{c} to straddle bytes that are in the same word.
  4958. You can leave a gap of a specified number of bits by defining a
  4959. nameless bit field. This looks like @code{@var{type} : @var{nbits};}.
  4960. It is allocated space in the structure just as a named bit field would
  4961. be allocated.
  4962. You can force the following bit field to advance to the following
  4963. aligned memory object with @code{@var{type} : 0;}.
  4964. Both of these constructs can syntactically share @var{type} with
  4965. ordinary bit fields. This example illustrates both:
  4966. @example
  4967. unsigned int a : 5, : 3, b : 5, : 0, c : 5, : 3, d : 5;
  4968. @end example
  4969. @noindent
  4970. It puts @code{a} and @code{b} into one @code{int}, with a 3-bit gap
  4971. between them. Then @code{: 0} advances to the next @code{int},
  4972. so @code{c} and @code{d} fit into that one.
  4973. These rules for packing bit fields apply to most target platforms,
  4974. including all the usual real computers. A few embedded controllers
  4975. have special layout rules.
  4976. @node const Fields
  4977. @section @code{const} Fields
  4978. @cindex const fields
  4979. @cindex structure fields, constant
  4980. @c ??? Is this a C standard feature?
  4981. A structure field declared @code{const} cannot be assigned to
  4982. (@pxref{const}). For instance, let's define this modified version of
  4983. @code{struct intlistlink}:
  4984. @example
  4985. struct intlistlink_ro /* @r{``ro'' for read-only.} */
  4986. @{
  4987. const int datum;
  4988. struct intlistlink *next;
  4989. @};
  4990. @end example
  4991. This structure can be used to prevent part of the code from modifying
  4992. the @code{datum} field:
  4993. @example
  4994. /* @r{@code{p} has type @code{struct intlistlink *}.}
  4995. @r{Convert it to @code{struct intlistlink_ro *}.} */
  4996. struct intlistlink_ro *q
  4997. = (struct intlistlink_ro *) p;
  4998. q->datum = 5; /* @r{Error!} */
  4999. p->datum = 5; /* @r{Valid since @code{*p} is}
  5000. @r{not a @code{struct intlistlink_ro}.} */
  5001. @end example
  5002. A @code{const} field can get a value in two ways: by initialization of
  5003. the whole structure, and by making a pointer-to-structure point to an object
  5004. in which that field already has a value.
  5005. Any @code{const} field in a structure type makes assignment impossible
  5006. for structures of that type (@pxref{Structure Assignment}). That is
  5007. because structure assignment works by assigning the structure's
  5008. fields, one by one.
  5009. @node Zero Length
  5010. @section Arrays of Length Zero
  5011. @cindex array of length zero
  5012. @cindex zero-length arrays
  5013. @cindex length-zero arrays
  5014. GNU C allows zero-length arrays. They are useful as the last element
  5015. of a structure that is really a header for a variable-length object.
  5016. Here's an example, where we construct a variable-size structure
  5017. to hold a line which is @code{this_length} characters long:
  5018. @example
  5019. struct line @{
  5020. int length;
  5021. char contents[0];
  5022. @};
  5023. struct line *thisline
  5024. = ((struct line *)
  5025. malloc (sizeof (struct line)
  5026. + this_length));
  5027. thisline->length = this_length;
  5028. @end example
  5029. In ISO C90, we would have to give @code{contents} a length of 1, which
  5030. means either wasting space or complicating the argument to @code{malloc}.
  5031. @node Flexible Array Fields
  5032. @section Flexible Array Fields
  5033. @cindex flexible array fields
  5034. @cindex array fields, flexible
  5035. The C99 standard adopted a more complex equivalent of zero-length
  5036. array fields. It's called a @dfn{flexible array}, and it's indicated
  5037. by omitting the length, like this:
  5038. @example
  5039. struct line
  5040. @{
  5041. int length;
  5042. char contents[];
  5043. @};
  5044. @end example
  5045. The flexible array has to be the last field in the structure, and there
  5046. must be other fields before it.
  5047. Under the C standard, a structure with a flexible array can't be part
  5048. of another structure, and can't be an element of an array.
  5049. GNU C allows static initialization of flexible array fields. The effect
  5050. is to ``make the array long enough'' for the initializer.
  5051. @example
  5052. struct f1 @{ int x; int y[]; @} f1
  5053. = @{ 1, @{ 2, 3, 4 @} @};
  5054. @end example
  5055. @noindent
  5056. This defines a structure variable named @code{f1}
  5057. whose type is @code{struct f1}. In C, a variable name or function name
  5058. never conflicts with a structure type tag.
  5059. Omitting the flexible array field's size lets the initializer
  5060. determine it. This is allowed only when the flexible array is defined
  5061. in the outermost structure and you declare a variable of that
  5062. structure type. For example:
  5063. @example
  5064. struct foo @{ int x; int y[]; @};
  5065. struct bar @{ struct foo z; @};
  5066. struct foo a = @{ 1, @{ 2, 3, 4 @} @}; // @r{Valid.}
  5067. struct bar b = @{ @{ 1, @{ 2, 3, 4 @} @} @}; // @r{Invalid.}
  5068. struct bar c = @{ @{ 1, @{ @} @} @}; // @r{Valid.}
  5069. struct foo d[1] = @{ @{ 1 @{ 2, 3, 4 @} @} @}; // @r{Invalid.}
  5070. @end example
  5071. @node Overlaying Structures
  5072. @section Overlaying Different Structures
  5073. @cindex overlaying structures
  5074. @cindex structures, overlaying
  5075. Be careful about using different structure types to refer to the same
  5076. memory within one function, because GNU C can optimize code assuming
  5077. it never does that. @xref{Aliasing}. Here's an example of the kind of
  5078. aliasing that can cause the problem:
  5079. @example
  5080. struct a @{ int size; char *data; @};
  5081. struct b @{ int size; char *data; @};
  5082. struct a foo;
  5083. struct b *q = (struct b *) &foo;
  5084. @end example
  5085. Here @code{q} points to the same memory that the variable @code{foo}
  5086. occupies, but they have two different types. The two types
  5087. @code{struct a} and @code{struct b} are defined alike, but they are
  5088. not the same type. Interspersing references using the two types,
  5089. like this,
  5090. @example
  5091. p->size = 0;
  5092. q->size = 1;
  5093. x = p->size;
  5094. @end example
  5095. @noindent
  5096. allows GNU C to assume that @code{p->size} is still zero when it is
  5097. copied into @code{x}. The compiler ``knows'' that @code{q} points to
  5098. a @code{struct b} and this cannot overlap with a @code{struct a}.
  5099. Other compilers might also do this optimization. The ISO C standard
  5100. considers such code erroneous, precisely so that this optimization
  5101. will be valid.
  5102. @node Structure Assignment
  5103. @section Structure Assignment
  5104. @cindex structure assignment
  5105. @cindex assigning structures
  5106. Assignment operating on a structure type copies the structure. The
  5107. left and right operands must have the same type. Here is an example:
  5108. @example
  5109. #include <stddef.h> /* @r{Defines @code{NULL}.} */
  5110. #include <stdlib.h> /* @r{Declares @code{malloc}.} */
  5111. @r{@dots{}}
  5112. struct point @{ double x, y; @};
  5113. struct point *
  5114. copy_point (struct point point)
  5115. @{
  5116. struct point *p
  5117. = (struct point *) malloc (sizeof (struct point));
  5118. if (p == NULL)
  5119. fatal ("Out of memory");
  5120. *p = point;
  5121. return p;
  5122. @}
  5123. @end example
  5124. Notionally, assignment on a structure type works by copying each of
  5125. the fields. Thus, if any of the fields has the @code{const}
  5126. qualifier, that structure type does not allow assignment:
  5127. @example
  5128. struct point @{ const double x, y; @};
  5129. struct point a, b;
  5130. a = b; /* @r{Error!} */
  5131. @end example
  5132. @xref{Assignment Expressions}.
  5133. @node Unions
  5134. @section Unions
  5135. @cindex unions
  5136. @findex union
  5137. A @dfn{union type} defines alternative ways of looking at the same
  5138. piece of memory. Each alternative view is defined with a data type,
  5139. and identified by a name. A union definition looks like this:
  5140. @example
  5141. union @var{name}
  5142. @{
  5143. @var{alternative declarations}@r{@dots{}}
  5144. @};
  5145. @end example
  5146. Each alternative declaration looks like a structure field declaration,
  5147. except that it can't be a bit field. For instance,
  5148. @example
  5149. union number
  5150. @{
  5151. long int integer;
  5152. double float;
  5153. @}
  5154. @end example
  5155. @noindent
  5156. lets you store either an integer (type @code{long int}) or a floating
  5157. point number (type @code{double}) in the same place in memory. The
  5158. length and alignment of the union type are the maximum of all the
  5159. alternatives---they do not have to be the same. In this union
  5160. example, @code{double} probably takes more space than @code{long int},
  5161. but that doesn't cause a problem in programs that use the union in the
  5162. normal way.
  5163. The members don't have to be different in data type. Sometimes
  5164. each member pertains to a way the data will be used. For instance,
  5165. @example
  5166. union datum
  5167. @{
  5168. double latitude;
  5169. double longitude;
  5170. double height;
  5171. double weight;
  5172. int continent;
  5173. @}
  5174. @end example
  5175. This union holds one of several kinds of data; most kinds are floating
  5176. points, but the value can also be a code for a continent which is an
  5177. integer. You @emph{could} use one member of type @code{double} to
  5178. access all the values which have that type, but the different member
  5179. names will make the program clearer.
  5180. The alignment of a union type is the maximum of the alignments of the
  5181. alternatives. The size of the union type is the maximum of the sizes
  5182. of the alternatives, rounded up to a multiple of the alignment
  5183. (because every type's size must be a multiple of its alignment).
  5184. All the union alternatives start at the address of the union itself.
  5185. If an alternative is shorter than the union as a whole, it occupies
  5186. the first part of the union's storage, leaving the last part unused
  5187. @emph{for that alternative}.
  5188. @strong{Warning:} if the code stores data using one union alternative
  5189. and accesses it with another, the results depend on the kind of
  5190. computer in use. Only wizards should try to do this. However, when
  5191. you need to do this, a union is a clean way to do it.
  5192. Assignment works on any union type by copying the entire value.
  5193. @node Packing With Unions
  5194. @section Packing With Unions
  5195. Sometimes we design a union with the intention of packing various
  5196. kinds of objects into a certain amount of memory space. For example.
  5197. @example
  5198. union bytes8
  5199. @{
  5200. long long big_int_elt;
  5201. double double_elt;
  5202. struct @{ int first, second; @} two_ints;
  5203. struct @{ void *first, *second; @} two_ptrs;
  5204. @};
  5205. union bytes8 *p;
  5206. @end example
  5207. This union makes it possible to look at 8 bytes of data that @code{p}
  5208. points to as a single 8-byte integer (@code{p->big_int_elt}), as a
  5209. single floating-point number (@code{p->double_elt}), as a pair of
  5210. integers (@code{p->two_ints.first} and @code{p->two_ints.second}), or
  5211. as a pair of pointers (@code{p->two_ptrs.first} and
  5212. @code{p->two_ptrs.second}).
  5213. To pack storage with such a union makes assumptions about the sizes of
  5214. all the types involved. This particular union was written expecting a
  5215. pointer to have the same size as @code{int}. On a machine where one
  5216. pointer takes 8 bytes, the code using this union probably won't work
  5217. as expected. The union, as such, will function correctly---if you
  5218. store two values through @code{two_ints} and extract them through
  5219. @code{two_ints}, you will get the same integers back---but the part of
  5220. the program that expects the union to be 8 bytes long could
  5221. malfunction, or at least use too much space.
  5222. The above example shows one case where a @code{struct} type with no
  5223. tag can be useful. Another way to get effectively the same result
  5224. is with arrays as members of the union:
  5225. @example
  5226. union eight_bytes
  5227. @{
  5228. long long big_int_elt;
  5229. double double_elt;
  5230. int two_ints[2];
  5231. void *two_ptrs[2];
  5232. @};
  5233. @end example
  5234. @node Cast to Union
  5235. @section Cast to a Union Type
  5236. @cindex cast to a union
  5237. @cindex union, casting to a
  5238. In GNU C, you can explicitly cast any of the alternative types to the
  5239. union type; for instance,
  5240. @example
  5241. (union eight_bytes) (long long) 5
  5242. @end example
  5243. @noindent
  5244. makes a value of type @code{union eight_bytes} which gets its contents
  5245. through the alternative named @code{big_int_elt}.
  5246. The value being cast must exactly match the type of the alternative,
  5247. so this is not valid:
  5248. @example
  5249. (union eight_bytes) 5 /* @r{Error! 5 is @code{int}.} */
  5250. @end example
  5251. A cast to union type looks like any other cast, except that the type
  5252. specified is a union type. You can specify the type either with
  5253. @code{union @var{tag}} or with a typedef name (@pxref{Defining
  5254. Typedef Names}).
  5255. Using the cast as the right-hand side of an assignment to a variable of
  5256. union type is equivalent to storing in an alternative of the union:
  5257. @example
  5258. union foo u;
  5259. u = (union foo) x @r{means} u.i = x
  5260. u = (union foo) y @r{means} u.d = y
  5261. @end example
  5262. You can also use the union cast as a function argument:
  5263. @example
  5264. void hack (union foo);
  5265. @r{@dots{}}
  5266. hack ((union foo) x);
  5267. @end example
  5268. @node Structure Constructors
  5269. @section Structure Constructors
  5270. @cindex structure constructors
  5271. @cindex constructors, structure
  5272. You can construct a structure value by writing its type in
  5273. parentheses, followed by an initializer that would be valid in a
  5274. declaration for that type. For instance, given this declaration,
  5275. @example
  5276. struct foo @{int a; char b[2];@} structure;
  5277. @end example
  5278. @noindent
  5279. you can create a @code{struct foo} value as follows:
  5280. @example
  5281. ((struct foo) @{x + y, 'a', 0@})
  5282. @end example
  5283. @noindent
  5284. This specifies @code{x + y} for field @code{a},
  5285. the character @samp{a} for field @code{b}'s element 0,
  5286. and the null character for field @code{b}'s element 1.
  5287. The parentheses around that constructor are to necessary, but we
  5288. recommend writing them to make the nesting of the containing
  5289. expression clearer.
  5290. You can also show the nesting of the two by writing it like
  5291. this:
  5292. @example
  5293. ((struct foo) @{x + y, @{'a', 0@} @})
  5294. @end example
  5295. Each of those is equivalent to writing the following statement
  5296. expression (@pxref{Statement Exprs}):
  5297. @example
  5298. (@{
  5299. struct foo temp = @{x + y, 'a', 0@};
  5300. temp;
  5301. @})
  5302. @end example
  5303. You can also create a union value this way, but it is not especially
  5304. useful since that is equivalent to doing a cast:
  5305. @example
  5306. ((union whosis) @{@var{value}@})
  5307. @r{is equivalent to}
  5308. ((union whosis) (@var{value}))
  5309. @end example
  5310. @node Unnamed Types as Fields
  5311. @section Unnamed Types as Fields
  5312. @cindex unnamed structures
  5313. @cindex unnamed unions
  5314. @cindex structures, unnamed
  5315. @cindex unions, unnamed
  5316. A structure or a union can contain, as fields,
  5317. unnamed structures and unions. Here's an example:
  5318. @example
  5319. struct
  5320. @{
  5321. int a;
  5322. union
  5323. @{
  5324. int b;
  5325. float c;
  5326. @};
  5327. int d;
  5328. @} foo;
  5329. @end example
  5330. @noindent
  5331. You can access the fields of the unnamed union within @code{foo} as if they
  5332. were individual fields at the same level as the union definition:
  5333. @example
  5334. foo.a = 42;
  5335. foo.b = 47;
  5336. foo.c = 5.25; // @r{Overwrites the value in @code{foo.b}}.
  5337. foo.d = 314;
  5338. @end example
  5339. Avoid using field names that could cause ambiguity. For example, with
  5340. this definition:
  5341. @example
  5342. struct
  5343. @{
  5344. int a;
  5345. struct
  5346. @{
  5347. int a;
  5348. float b;
  5349. @};
  5350. @} foo;
  5351. @end example
  5352. @noindent
  5353. it is impossible to tell what @code{foo.a} refers to. GNU C reports
  5354. an error when a definition is ambiguous in this way.
  5355. @node Incomplete Types
  5356. @section Incomplete Types
  5357. @cindex incomplete types
  5358. @cindex types, incomplete
  5359. A type that has not been fully defined is called an @dfn{incomplete
  5360. type}. Structure and union types are incomplete when the code makes a
  5361. forward reference, such as @code{struct foo}, before defining the
  5362. type. An array type is incomplete when its length is unspecified.
  5363. You can't use an incomplete type to declare a variable or field, or
  5364. use it for a function parameter or return type. The operators
  5365. @code{sizeof} and @code{_Alignof} give errors when used on an
  5366. incomplete type.
  5367. However, you can define a pointer to an incomplete type, and declare a
  5368. variable or field with such a pointer type. In general, you can do
  5369. everything with such pointers except dereference them. For example:
  5370. @example
  5371. extern void bar (struct mysterious_value *);
  5372. void
  5373. foo (struct mysterious_value *arg)
  5374. @{
  5375. bar (arg);
  5376. @}
  5377. @r{@dots{}}
  5378. @{
  5379. struct mysterious_value *p, **q;
  5380. p = *q;
  5381. foo (p);
  5382. @}
  5383. @end example
  5384. @noindent
  5385. These examples are valid because the code doesn't try to understand
  5386. what @code{p} points to; it just passes the pointer around.
  5387. (Presumably @code{bar} is defined in some other file that really does
  5388. have a definition for @code{struct mysterious_value}.) However,
  5389. dereferencing the pointer would get an error; that requires a
  5390. definition for the structure type.
  5391. @node Intertwined Incomplete Types
  5392. @section Intertwined Incomplete Types
  5393. When several structure types contain pointers to each other, you can
  5394. define the types in any order because pointers to types that come
  5395. later are incomplete types. Thus,
  5396. Here is an example.
  5397. @example
  5398. /* @r{An employee record points to a group.} */
  5399. struct employee
  5400. @{
  5401. char *name;
  5402. @r{@dots{}}
  5403. struct group *group; /* @r{incomplete type.} */
  5404. @r{@dots{}}
  5405. @};
  5406. /* @r{An employee list points to employees.} */
  5407. struct employee_list
  5408. @{
  5409. struct employee *this_one;
  5410. struct employee_list *next; /* @r{incomplete type.} */
  5411. @r{@dots{}}
  5412. @};
  5413. /* @r{A group points to one employee_list.} */
  5414. struct group
  5415. @{
  5416. char *name;
  5417. @r{@dots{}}
  5418. struct employee_list *employees;
  5419. @r{@dots{}}
  5420. @};
  5421. @end example
  5422. @node Type Tags
  5423. @section Type Tags
  5424. @cindex type tags
  5425. The name that follows @code{struct} (@pxref{Structures}), @code{union}
  5426. (@pxref{Unions}, or @code{enum} (@pxref{Enumeration Types}) is called
  5427. a @dfn{type tag}. In C, a type tag never conflicts with a variable
  5428. name or function name; the type tags have a separate @dfn{name space}.
  5429. Thus, there is no name conflict in this code:
  5430. @example
  5431. struct pair @{ int a, b; @};
  5432. int pair = 1;
  5433. @end example
  5434. @noindent
  5435. nor in this one:
  5436. @example
  5437. struct pair @{ int a, b; @} pair;
  5438. @end example
  5439. @noindent
  5440. where @code{pair} is both a structure type tag and a variable name.
  5441. However, @code{struct}, @code{union}, and @code{enum} share the same
  5442. name space of tags, so this is a conflict:
  5443. @example
  5444. struct pair @{ int a, b; @};
  5445. enum pair @{ c, d @};
  5446. @end example
  5447. @noindent
  5448. and so is this:
  5449. @example
  5450. struct pair @{ int a, b; @};
  5451. struct pair @{ int c, d; @};
  5452. @end example
  5453. When the code defines a type tag inside a block, the tag's scope is
  5454. limited to that block (as for local variables). Two definitions for
  5455. one type tag do not conflict if they are in different scopes; rather,
  5456. each is valid in its scope. For example,
  5457. @example
  5458. struct pair @{ int a, b; @};
  5459. void
  5460. pair_up_doubles (int len, double array[])
  5461. @{
  5462. struct pair @{ double a, b; @};
  5463. @r{@dots{}}
  5464. @}
  5465. @end example
  5466. @noindent
  5467. has two definitions for @code{struct pair} which do not conflict. The
  5468. one inside the function applies only within the definition of
  5469. @code{pair_up_doubles}. Within its scope, that definition
  5470. @dfn{shadows} the outer definition.
  5471. If @code{struct pair} appears inside the function body, before the
  5472. inner definition, it refers to the outer definition---the only one
  5473. that has been seen at that point. Thus, in this code,
  5474. @example
  5475. struct pair @{ int a, b; @};
  5476. void
  5477. pair_up_doubles (int len, double array[])
  5478. @{
  5479. struct two_pairs @{ struct pair *p, *q; @};
  5480. struct pair @{ double a, b; @};
  5481. @r{@dots{}}
  5482. @}
  5483. @end example
  5484. @noindent
  5485. the structure @code{two_pairs} has pointers to the outer definition of
  5486. @code{struct pair}, which is probably not desirable.
  5487. To prevent that, you can write @code{struct pair;} inside the function
  5488. body as a variable declaration with no variables. This is a
  5489. @dfn{forward declaration} of the type tag @code{pair}: it makes the
  5490. type tag local to the current block, with the details of the type to
  5491. come later. Here's an example:
  5492. @example
  5493. void
  5494. pair_up_doubles (int len, double array[])
  5495. @{
  5496. /* @r{Forward declaration for @code{pair}.} */
  5497. struct pair;
  5498. struct two_pairs @{ struct pair *p, *q; @};
  5499. /* @r{Give the details.} */
  5500. struct pair @{ double a, b; @};
  5501. @r{@dots{}}
  5502. @}
  5503. @end example
  5504. However, the cleanest practice is to avoid shadowing type tags.
  5505. @node Arrays
  5506. @chapter Arrays
  5507. @cindex array
  5508. @cindex elements of arrays
  5509. An @dfn{array} is a data object that holds a series of @dfn{elements},
  5510. all of the same data type. Each element is identified by its numeric
  5511. @var{index} within the array.
  5512. We presented arrays of numbers in the sample programs early in this
  5513. manual (@pxref{Array Example}). However, arrays can have elements of
  5514. any data type, including pointers, structures, unions, and other
  5515. arrays.
  5516. If you know another programming language, you may suppose that you know all
  5517. about arrays, but C arrays have special quirks, so in this chapter we
  5518. collect all the information about arrays in C@.
  5519. The elements of a C array are allocated consecutively in memory,
  5520. with no gaps between them. Each element is aligned as required
  5521. for its data type (@pxref{Type Alignment}).
  5522. @menu
  5523. * Accessing Array Elements:: How to access individual elements of an array.
  5524. * Declaring an Array:: How to name and reserve space for a new array.
  5525. * Strings:: A string in C is a special case of array.
  5526. * Array Type Designators:: Referring to a specific array type.
  5527. * Incomplete Array Types:: Naming, but not allocating, a new array.
  5528. * Limitations of C Arrays:: Arrays are not first-class objects.
  5529. * Multidimensional Arrays:: Arrays of arrays.
  5530. * Constructing Array Values:: Assigning values to an entire array at once.
  5531. * Arrays of Variable Length:: Declaring arrays of non-constant size.
  5532. @end menu
  5533. @node Accessing Array Elements
  5534. @section Accessing Array Elements
  5535. @cindex accessing array elements
  5536. @cindex array elements, accessing
  5537. If the variable @code{a} is an array, the @var{n}th element of
  5538. @code{a} is @code{a[@var{n}]}. You can use that expression to access
  5539. an element's value or to assign to it:
  5540. @example
  5541. x = a[5];
  5542. a[6] = 1;
  5543. @end example
  5544. @noindent
  5545. Since the variable @code{a} is an lvalue, @code{a[@var{n}]} is also an
  5546. lvalue.
  5547. The lowest valid index in an array is 0, @emph{not} 1, and the highest
  5548. valid index is one less than the number of elements.
  5549. The C language does not check whether array indices are in bounds, so
  5550. if the code uses an out-of-range index, it will access memory outside the
  5551. array.
  5552. @strong{Warning:} Using only valid index values in C is the
  5553. programmer's responsibility.
  5554. Array indexing in C is not a primitive operation: it is defined in
  5555. terms of pointer arithmetic and dereferencing. Now that we know
  5556. @emph{what} @code{a[i]} does, we can ask @emph{how} @code{a[i]} does
  5557. its job.
  5558. In C, @code{@var{x}[@var{y}]} is an abbreviation for
  5559. @code{*(@var{x}+@var{y})}. Thus, @code{a[i]} really means
  5560. @code{*(a+i)}. @xref{Pointers and Arrays}.
  5561. When an expression with array type (such as @code{a}) appears as part
  5562. of a larger C expression, it is converted automatically to a pointer
  5563. to element zero of that array. For instance, @code{a} in an
  5564. expression is equivalent to @code{&a[0]}. Thus, @code{*(a+i)} is
  5565. computed as @code{*(&a[0]+i)}.
  5566. Now we can analyze how that expression gives us the desired element of
  5567. the array. It makes a pointer to element 0 of @code{a}, advances it
  5568. by the value of @code{i}, and dereferences that pointer.
  5569. Another equivalent way to write the expression is @code{(&a[0])[i]}.
  5570. @node Declaring an Array
  5571. @section Declaring an Array
  5572. @cindex declaring an array
  5573. @cindex array, declaring
  5574. To make an array declaration, write @code{[@var{length}]} after the
  5575. name being declared. This construct is valid in the declaration of a
  5576. variable, a function parameter, a function value type (the value can't
  5577. be an array, but it can be a pointer to one), a structure field, or a
  5578. union alternative.
  5579. The surrounding declaration specifies the element type of the array;
  5580. that can be any type of data, but not @code{void} or a function type.
  5581. For instance,
  5582. @example
  5583. double a[5];
  5584. @end example
  5585. @noindent
  5586. declares @code{a} as an array of 5 @code{double}s.
  5587. @example
  5588. struct foo bstruct[length];
  5589. @end example
  5590. @noindent
  5591. declares @code{bstruct} as an array of @code{length} objects of type
  5592. @code{struct foo}. A variable array size like this is allowed when
  5593. the array is not file-scope.
  5594. Other declaration constructs can nest within the array declaration
  5595. construct. For instance:
  5596. @example
  5597. struct foo *b[length];
  5598. @end example
  5599. @noindent
  5600. declares @code{b} as an array of @code{length} pointers to
  5601. @code{struct foo}. This shows that the length need not be a constant
  5602. (@pxref{Arrays of Variable Length}).
  5603. @example
  5604. double (*c)[5];
  5605. @end example
  5606. @noindent
  5607. declares @code{c} as a pointer to an array of 5 @code{double}s, and
  5608. @example
  5609. char *(*f (int))[5];
  5610. @end example
  5611. @noindent
  5612. declares @code{f} as a function taking an @code{int} argument and
  5613. returning a pointer to an array of 5 strings (pointers to
  5614. @code{char}s).
  5615. @example
  5616. double aa[5][10];
  5617. @end example
  5618. @noindent
  5619. declares @code{aa} as an array of 5 elements, each of which is an
  5620. array of 10 @code{double}s. This shows how to declare a
  5621. multidimensional array in C (@pxref{Multidimensional Arrays}).
  5622. All these declarations specify the array's length, which is needed in
  5623. these cases in order to allocate storage for the array.
  5624. @node Strings
  5625. @section Strings
  5626. @cindex string
  5627. A string in C is a sequence of elements of type @code{char},
  5628. terminated with the null character, the character with code zero.
  5629. Programs often need to use strings with specific, fixed contents. To
  5630. write one in a C program, use a @dfn{string constant} such as
  5631. @code{"Take me to your leader!"}. The data type of a string constant
  5632. is @code{char *}. For the full syntactic details of writing string
  5633. constants, @ref{String Constants}.
  5634. To declare a place to store a non-constant string, declare an array of
  5635. @code{char}. Keep in mind that it must include one extra @code{char}
  5636. for the terminating null. For instance,
  5637. @example
  5638. char text[] = @{ 'H', 'e', 'l', 'l', 'o', 0 @};
  5639. @end example
  5640. @noindent
  5641. declares an array named @samp{text} with six elements---five letters
  5642. and the terminating null character. An equivalent way to get the same
  5643. result is this,
  5644. @example
  5645. char text[] = "Hello";
  5646. @end example
  5647. @noindent
  5648. which copies the elements of the string constant, including @emph{its}
  5649. terminating null character.
  5650. @example
  5651. char message[200];
  5652. @end example
  5653. @noindent
  5654. declares an array long enough to hold a string of 199 ASCII characters
  5655. plus the terminating null character.
  5656. When you store a string into @code{message} be sure to check or prove
  5657. that the length does not exceed its size. For example,
  5658. @example
  5659. void
  5660. set_message (char *text)
  5661. @{
  5662. int i;
  5663. for (i = 0; i < sizeof (message); i++)
  5664. @{
  5665. message[i] = text[i];
  5666. if (text[i] == 0)
  5667. return;
  5668. @}
  5669. fatal_error ("Message is too long for `message');
  5670. @}
  5671. @end example
  5672. It's easy to do this with the standard library function
  5673. @code{strncpy}, which fills out the whole destination array (up to a
  5674. specified length) with null characters. Thus, if the last character
  5675. of the destination is not null, the string did not fit. Many system
  5676. libraries, including the GNU C library, hand-optimize @code{strncpy}
  5677. to run faster than an explicit @code{for}-loop.
  5678. Here's what the code looks like:
  5679. @example
  5680. void
  5681. set_message (char *text)
  5682. @{
  5683. strncpy (message, text, sizeof (message));
  5684. if (message[sizeof (message) - 1] != 0)
  5685. fatal_error ("Message is too long for `message');
  5686. @}
  5687. @end example
  5688. @xref{String and Array Utilities, The GNU C Library, , libc, The GNU C
  5689. Library Reference Manual}, for more information about the standard
  5690. library functions for operating on strings.
  5691. You can avoid putting a fixed length limit on strings you construct or
  5692. operate on by allocating the space for them dynamically.
  5693. @xref{Dynamic Memory Allocation}.
  5694. @node Array Type Designators
  5695. @section Array Type Designators
  5696. Every C type has a type designator, which you make by deleting the
  5697. variable name and the semicolon from a declaration (@pxref{Type
  5698. Designators}). The designators for array types follow this rule, but
  5699. they may appear surprising.
  5700. @example
  5701. @r{type} int a[5]; @r{designator} int [5]
  5702. @r{type} double a[5][3]; @r{designator} double [5][3]
  5703. @r{type} struct foo *a[5]; @r{designator} struct foo *[5]
  5704. @end example
  5705. @node Incomplete Array Types
  5706. @section Incomplete Array Types
  5707. @cindex incomplete array types
  5708. @cindex array types, incomplete
  5709. An array is equivalent, for most purposes, to a pointer to its zeroth
  5710. element. When that is true, the length of the array is irrelevant.
  5711. The length needs to be known only for allocating space for the array, or
  5712. for @code{sizeof} and @code{typeof} (@pxref{Auto Type}). Thus, in some
  5713. contexts C allows
  5714. @itemize @bullet
  5715. @item
  5716. An @code{extern} declaration says how to refer to a variable allocated
  5717. elsewhere. It does not need to allocate space for the variable,
  5718. so if it is an array, you can omit the length. For example,
  5719. @example
  5720. extern int foo[];
  5721. @end example
  5722. @item
  5723. When declaring a function parameter as an array, the argument value
  5724. passed to the function is really a pointer to the array's zeroth
  5725. element. This value does not say how long the array really is, there
  5726. is no need to declare it. For example,
  5727. @example
  5728. int
  5729. func (int foo[])
  5730. @end example
  5731. @end itemize
  5732. These declarations are examples of @dfn{incomplete} array types, types
  5733. that are not fully specified. The incompleteness makes no difference
  5734. for accessing elements of the array, but it matters for some other
  5735. things. For instance, @code{sizeof} is not allowed on an incomplete
  5736. type.
  5737. With multidimensional arrays, only the first dimension can be omitted:
  5738. @example
  5739. extern struct chesspiece *funnyboard foo[][8];
  5740. @end example
  5741. In other words, the code doesn't have to say how many rows there are,
  5742. but it must state how big each row is.
  5743. @node Limitations of C Arrays
  5744. @section Limitations of C Arrays
  5745. @cindex limitations of C arrays
  5746. @cindex first-class object
  5747. Arrays have quirks in C because they are not ``first-class objects'':
  5748. there is no way in C to operate on an array as a unit.
  5749. The other composite objects in C, structures and unions, are
  5750. first-class objects: a C program can copy a structure or union value
  5751. in an assignment, or pass one as an argument to a function, or make a
  5752. function return one. You can't do those things with an array in C@.
  5753. That is because a value you can operate on never has an array type.
  5754. An expression in C can have an array type, but that doesn't produce
  5755. the array as a value. Instead it is converted automatically to a
  5756. pointer to the array's element at index zero. The code can operate
  5757. on the pointer, and through that on individual elements of the array,
  5758. but it can't get and operate on the array as a unit.
  5759. There are three exceptions to this conversion rule, but none of them
  5760. offers a way to operate on the array as a whole.
  5761. First, @samp{&} applied to an expression with array type gives you the
  5762. address of the array, as an array type. However, you can't operate on the
  5763. whole array that way---if you apply @samp{*} to get the array back,
  5764. that expression converts, as usual, to a pointer to its zeroth
  5765. element.
  5766. Second, the operators @code{sizeof}, @code{_Alignof}, and
  5767. @code{typeof} do not convert the array to a pointer; they leave it as
  5768. an array. But they don't operate on the array's data---they only give
  5769. information about its type.
  5770. Third, a string constant used as an initializer for an array is not
  5771. converted to a pointer---rather, the declaration copies the
  5772. @emph{contents} of that string in that one special case.
  5773. You @emph{can} copy the contents of an array, just not with an
  5774. assignment operator. You can do it by calling the library function
  5775. @code{memcpy} or @code{memmove} (@pxref{Copying and Concatenation, The
  5776. GNU C Library, , libc, The GNU C Library Reference Manual}). Also,
  5777. when a structure contains just an array, you can copy that structure.
  5778. An array itself is an lvalue if it is a declared variable, or part of
  5779. a structure or union that is an lvalue. When you construct an array
  5780. from elements (@pxref{Constructing Array Values}), that array is not
  5781. an lvalue.
  5782. @node Multidimensional Arrays
  5783. @section Multidimensional Arrays
  5784. @cindex multidimensional arrays
  5785. @cindex array, multidimensional
  5786. Strictly speaking, all arrays in C are unidimensional. However, you
  5787. can create an array of arrays, which is more or less equivalent to a
  5788. multidimensional array. For example,
  5789. @example
  5790. struct chesspiece *board[8][8];
  5791. @end example
  5792. @noindent
  5793. declares an array of 8 arrays of 8 pointers to @code{struct
  5794. chesspiece}. This data type could represent the state of a chess
  5795. game. To access one square's contents requires two array index
  5796. operations, one for each dimension. For instance, you can write
  5797. @code{board[row][column]}, assuming @code{row} and @code{column}
  5798. are variables with integer values in the proper range.
  5799. How does C understand @code{board[row][column]}? First of all,
  5800. @code{board} is converted automatically to a pointer to the zeroth
  5801. element (at index zero) of @code{board}. Adding @code{row} to that
  5802. makes it point to the desired element. Thus, @code{board[row]}'s
  5803. value is an element of @code{board}---an array of 8 pointers.
  5804. However, as an expression with array type, it is converted
  5805. automatically to a pointer to the array's zeroth element. The second
  5806. array index operation, @code{[column]}, accesses the chosen element
  5807. from that array.
  5808. As this shows, pointer-to-array types are meaningful in C@.
  5809. You can declare a variable that points to a row in a chess board
  5810. like this:
  5811. @example
  5812. struct chesspiece *(*rowptr)[8];
  5813. @end example
  5814. @noindent
  5815. This points to an array of 8 pointers to @code{struct chesspiece}.
  5816. You can assign to it as follows:
  5817. @example
  5818. rowptr = &board[5];
  5819. @end example
  5820. The dimensions don't have to be equal in length. Here we declare
  5821. @code{statepop} as an array to hold the population of each state in
  5822. the United States for each year since 1900:
  5823. @example
  5824. #define NSTATES 50
  5825. @{
  5826. int nyears = current_year - 1900 + 1;
  5827. int statepop[NSTATES][nyears];
  5828. @r{@dots{}}
  5829. @}
  5830. @end example
  5831. The variable @code{statepop} is an array of @code{NSTATES} subarrays,
  5832. each indexed by the year (counting from 1900). Thus, to get the
  5833. element for a particular state and year, we must subscript it first
  5834. by the number that indicates the state, and second by the index for
  5835. the year:
  5836. @example
  5837. statepop[state][year - 1900]
  5838. @end example
  5839. @cindex array, layout in memory
  5840. The subarrays within the multidimensional array are allocated
  5841. consecutively in memory, and within each subarray, its elements are
  5842. allocated consecutively in memory. The most efficient way to process
  5843. all the elements in the array is to scan the last subscript in the
  5844. innermost loop. This means consecutive accesses go to consecutive
  5845. memory locations, which optimizes use of the processor's memory cache.
  5846. For example:
  5847. @example
  5848. int total = 0;
  5849. float average;
  5850. for (int state = 0; state < NSTATES, ++state)
  5851. @{
  5852. for (int year = 0; year < nyears; ++year)
  5853. @{
  5854. total += statepop[state][year];
  5855. @}
  5856. @}
  5857. average = total / nyears;
  5858. @end example
  5859. C's layout for multidimensional arrays is different from Fortran's
  5860. layout. In Fortran, a multidimensional array is not an array of
  5861. arrays; rather, multidimensional arrays are a primitive feature, and
  5862. it is the first index that varies most rapidly between consecutive
  5863. memory locations. Thus, the memory layout of a 50x114 array in C
  5864. matches that of a 114x50 array in Fortran.
  5865. @node Constructing Array Values
  5866. @section Constructing Array Values
  5867. @cindex constructing array values
  5868. @cindex array values, constructing
  5869. You can construct an array from elements by writing them inside
  5870. braces, and preceding all that with the array type's designator in
  5871. parentheses. There is no need to specify the array length, since the
  5872. number of elements determines that. The constructor looks like this:
  5873. @example
  5874. (@var{elttype}[]) @{ @var{elements} @};
  5875. @end example
  5876. Here is an example, which constructs an array of string pointers:
  5877. @example
  5878. (char *[]) @{ "x", "y", "z" @};
  5879. @end example
  5880. That's equivalent in effect to declaring an array with the same
  5881. initializer, like this:
  5882. @example
  5883. char *array[] = @{ "x", "y", "z" @};
  5884. @end example
  5885. and then using the array.
  5886. If all the elements are simple constant expressions, or made up of
  5887. such, then the compound literal can be coerced to a pointer to its
  5888. zeroth element and used to initialize a file-scope variable
  5889. (@pxref{File-Scope Variables}), as shown here:
  5890. @example
  5891. char **foo = (char *[]) @{ "x", "y", "z" @};
  5892. @end example
  5893. @noindent
  5894. The data type of @code{foo} is @code{char **}, which is a pointer
  5895. type, not an array type. The declaration is equivalent to defining
  5896. and then using an array-type variable:
  5897. @example
  5898. char *nameless_array[] = @{ "x", "y", "z" @};
  5899. char **foo = &nameless_array[0];
  5900. @end example
  5901. @node Arrays of Variable Length
  5902. @section Arrays of Variable Length
  5903. @cindex array of variable length
  5904. @cindex variable-length arrays
  5905. In GNU C, you can declare variable-length arrays like any other
  5906. arrays, but with a length that is not a constant expression. The
  5907. storage is allocated at the point of declaration and deallocated when
  5908. the block scope containing the declaration exits. For example:
  5909. @example
  5910. #include <stdio.h> /* @r{Defines @code{FILE}.} */
  5911. #include <string.h> /* @r{Declares @code{str}.} */
  5912. FILE *
  5913. concat_fopen (char *s1, char *s2, char *mode)
  5914. @{
  5915. char str[strlen (s1) + strlen (s2) + 1];
  5916. strcpy (str, s1);
  5917. strcat (str, s2);
  5918. return fopen (str, mode);
  5919. @}
  5920. @end example
  5921. @noindent
  5922. (This uses some standard library functions; see @ref{String and Array
  5923. Utilities, , , libc, The GNU C Library Reference Manual}.)
  5924. The length of an array is computed once when the storage is allocated
  5925. and is remembered for the scope of the array in case it is used in
  5926. @code{sizeof}.
  5927. @strong{Warning:} don't allocate a variable-length array if the size
  5928. might be very large (more than 100,000), or in a recursive function,
  5929. because that is likely to cause stack overflow. Allocate the array
  5930. dynamically instead (@pxref{Dynamic Memory Allocation}).
  5931. Jumping or breaking out of the scope of the array name deallocates the
  5932. storage. Jumping into the scope is not allowed; that gives an error
  5933. message.
  5934. You can also use variable-length arrays as arguments to functions:
  5935. @example
  5936. struct entry
  5937. tester (int len, char data[len][len])
  5938. @{
  5939. @r{@dots{}}
  5940. @}
  5941. @end example
  5942. As usual, a function argument declared with an array type
  5943. is really a pointer to an array that already exists.
  5944. Calling the function does not allocate the array, so there's no
  5945. particular danger of stack overflow in using this construct.
  5946. To pass the array first and the length afterward, use a forward
  5947. declaration in the function's parameter list (another GNU extension).
  5948. For example,
  5949. @example
  5950. struct entry
  5951. tester (int len; char data[len][len], int len)
  5952. @{
  5953. @r{@dots{}}
  5954. @}
  5955. @end example
  5956. The @code{int len} before the semicolon is a @dfn{parameter forward
  5957. declaration}, and it serves the purpose of making the name @code{len}
  5958. known when the declaration of @code{data} is parsed.
  5959. You can write any number of such parameter forward declarations in the
  5960. parameter list. They can be separated by commas or semicolons, but
  5961. the last one must end with a semicolon, which is followed by the
  5962. ``real'' parameter declarations. Each forward declaration must match
  5963. a ``real'' declaration in parameter name and data type. ISO C11 does
  5964. not support parameter forward declarations.
  5965. @node Enumeration Types
  5966. @chapter Enumeration Types
  5967. @cindex enumeration types
  5968. @cindex types, enumeration
  5969. @cindex enumerator
  5970. An @dfn{enumeration type} represents a limited set of integer values,
  5971. each with a name. It is effectively equivalent to a primitive integer
  5972. type.
  5973. Suppose we have a list of possible emotional states to store in an
  5974. integer variable. We can give names to these alternative values with
  5975. an enumeration:
  5976. @example
  5977. enum emotion_state @{ neutral, happy, sad, worried,
  5978. calm, nervous @};
  5979. @end example
  5980. @noindent
  5981. (Never mind that this is a simplistic way to classify emotional states;
  5982. it's just a code example.)
  5983. The names inside the enumeration are called @dfn{enumerators}. The
  5984. enumeration type defines them as constants, and their values are
  5985. consecutive integers; @code{neutral} is 0, @code{happy} is 1,
  5986. @code{sad} is 2, and so on. Alternatively, you can specify values for
  5987. the enumerators explicitly like this:
  5988. @example
  5989. enum emotion_state @{ neutral = 2, happy = 5,
  5990. sad = 20, worried = 10,
  5991. calm = -5, nervous = -300 @};
  5992. @end example
  5993. Each enumerator which does not specify a value gets value zero
  5994. (if it is at the beginning) or the next consecutive integer.
  5995. @example
  5996. /* @r{@code{neutral} is 0 by default,}
  5997. @r{and @code{worried} is 21 by default.} */
  5998. enum emotion_state @{ neutral,
  5999. happy = 5, sad = 20, worried,
  6000. calm = -5, nervous = -300 @};
  6001. @end example
  6002. If an enumerator is obsolete, you can specify that using it should
  6003. cause a warning, by including an attribute in the enumerator's
  6004. declaration. Here is how @code{happy} would look with this
  6005. attribute:
  6006. @example
  6007. happy __attribute__
  6008. ((deprecated
  6009. ("impossible under plutocratic rule")))
  6010. = 5,
  6011. @end example
  6012. @xref{Attributes}.
  6013. You can declare variables with the enumeration type:
  6014. @example
  6015. enum emotion_state feelings_now;
  6016. @end example
  6017. In the C code itself, this is equivalent to declaring the variable
  6018. @code{int}. (If all the enumeration values are positive, it is
  6019. equivalent to @code{unsigned int}.) However, declaring it with the
  6020. enumeration type has an advantage in debugging, because GDB knows it
  6021. should display the current value of the variable using the
  6022. corresponding name. If the variable's type is @code{int}, GDB can
  6023. only show the value as a number.
  6024. The identifier that follows @code{enum} is called a @dfn{type tag}
  6025. since it distinguishes different enumeration types. Type tags are in
  6026. a separate name space and belong to scopes like most other names in C@.
  6027. @xref{Type Tags}, for explanation.
  6028. You can predeclare an @code{enum} type tag like a structure or union
  6029. type tag, like this:
  6030. @example
  6031. enum foo;
  6032. @end example
  6033. @noindent
  6034. The @code{enum} type is incomplete until you finish defining it.
  6035. You can optionally include a trailing comma at the end of a list of
  6036. enumeration values:
  6037. @example
  6038. enum emotion_state @{ neutral, happy, sad, worried,
  6039. calm, nervous, @};
  6040. @end example
  6041. @noindent
  6042. This is useful in some macro definitions, since it enables you to
  6043. assemble the list of enumerators without knowing which one is last.
  6044. The extra comma does not change the meaning of the enumeration in any
  6045. way.
  6046. @node Defining Typedef Names
  6047. @chapter Defining Typedef Names
  6048. @cindex typedef names
  6049. @findex typedef
  6050. You can define a data type keyword as an alias for any type, and then
  6051. use the alias syntactically like a built-in type keyword such as
  6052. @code{int}. You do this using @code{typedef}, so these aliases are
  6053. also called @dfn{typedef names}.
  6054. @code{typedef} is followed by text that looks just like a variable
  6055. declaration, but instead of declaring variables it defines data type
  6056. keywords.
  6057. Here's how to define @code{fooptr} as a typedef alias for the type
  6058. @code{struct foo *}, then declare @code{x} and @code{y} as variables
  6059. with that type:
  6060. @example
  6061. typedef struct foo *fooptr;
  6062. fooptr x, y;
  6063. @end example
  6064. @noindent
  6065. That declaration is equivalent to the following one:
  6066. @example
  6067. struct foo *x, *y;
  6068. @end example
  6069. You can define a typedef alias for any type. For instance, this makes
  6070. @code{frobcount} an alias for type @code{int}:
  6071. @example
  6072. typedef int frobcount;
  6073. @end example
  6074. @noindent
  6075. This doesn't define a new type distinct from @code{int}. Rather,
  6076. @code{frobcount} is another name for the type @code{int}. Once the
  6077. variable is declared, it makes no difference which name the
  6078. declaration used.
  6079. There is a syntactic difference, however, between @code{frobcount} and
  6080. @code{int}: A typedef name cannot be used with
  6081. @code{signed}, @code{unsigned}, @code{long} or @code{short}. It has
  6082. to specify the type all by itself. So you can't write this:
  6083. @example
  6084. unsigned frobcount f1; /* @r{Error!} */
  6085. @end example
  6086. But you can write this:
  6087. @example
  6088. typedef unsigned int unsigned_frobcount;
  6089. unsigned_frobcount f1;
  6090. @end example
  6091. In other words, a typedef name is not an alias for @emph{a keyword}
  6092. such as @code{int}. It stands for a @emph{type}, and that could be
  6093. the type @code{int}.
  6094. Typedef names are in the same namespace as functions and variables, so
  6095. you can't use the same name for a typedef and a function, or a typedef
  6096. and a variable. When a typedef is declared inside a code block, it is
  6097. in scope only in that block.
  6098. @strong{Warning:} Avoid defining typedef names that end in @samp{_t},
  6099. because many of these have standard meanings.
  6100. You can redefine a typedef name to the exact same type as its first
  6101. definition, but you cannot redefine a typedef name to a
  6102. different type, even if the two types are compatible. For example, this
  6103. is valid:
  6104. @example
  6105. typedef int frobcount;
  6106. typedef int frotzcount;
  6107. typedef frotzcount frobcount;
  6108. typedef frobcount frotzcount;
  6109. @end example
  6110. @noindent
  6111. because each typedef name is always defined with the same type
  6112. (@code{int}), but this is not valid:
  6113. @example
  6114. enum foo @{f1, f2, f3@};
  6115. typedef enum foo frobcount;
  6116. typedef int frobcount;
  6117. @end example
  6118. @noindent
  6119. Even though the type @code{enum foo} is compatible with @code{int},
  6120. they are not the @emph{same} type.
  6121. @node Statements
  6122. @chapter Statements
  6123. @cindex statements
  6124. A @dfn{statement} specifies computations to be done for effect; it
  6125. does not produce a value, as an expression would. In general a
  6126. statement ends with a semicolon (@samp{;}), but blocks (which are
  6127. statements, more or less) are an exception to that rule.
  6128. @ifnottex
  6129. @xref{Blocks}.
  6130. @end ifnottex
  6131. The places to use statements are inside a block, and inside a
  6132. complex statement. A @dfn{complex statement} contains one or two
  6133. components that are nested statements. Each such component must
  6134. consist of one and only one statement. The way to put multiple
  6135. statements in such a component is to group them into a @dfn{block}
  6136. (@pxref{Blocks}), which counts as one statement.
  6137. The following sections describe the various kinds of statement.
  6138. @menu
  6139. * Expression Statement:: Evaluate an expression, as a statement,
  6140. usually done for a side effect.
  6141. * if Statement:: Basic conditional execution.
  6142. * if-else Statement:: Multiple branches for conditional execution.
  6143. * Blocks:: Grouping multiple statements together.
  6144. * return Statement:: Return a value from a function.
  6145. * Loop Statements:: Repeatedly executing a statement or block.
  6146. * switch Statement:: Multi-way conditional choices.
  6147. * switch Example:: A plausible example of using @code{switch}.
  6148. * Duffs Device:: A special way to use @code{switch}.
  6149. * Case Ranges:: Ranges of values for @code{switch} cases.
  6150. * Null Statement:: A statement that does nothing.
  6151. * goto Statement:: Jump to another point in the source code,
  6152. identified by a label.
  6153. * Local Labels:: Labels with limited scope.
  6154. * Labels as Values:: Getting the address of a label.
  6155. * Statement Exprs:: A series of statements used as an expression.
  6156. @end menu
  6157. @node Expression Statement
  6158. @section Expression Statement
  6159. @cindex expression statement
  6160. @cindex statement, expression
  6161. The most common kind of statement in C is an @dfn{expression statement}.
  6162. It consists of an expression followed by a
  6163. semicolon. The expression's value is discarded, so the expressions
  6164. that are useful are those that have side effects: assignment
  6165. expressions, increment and decrement expressions, and function calls.
  6166. Here are examples of expression statements:
  6167. @smallexample
  6168. x = 5; /* @r{Assignment expression.} */
  6169. p++; /* @r{Increment expression.} */
  6170. printf ("Done\n"); /* @r{Function call expression.} */
  6171. *p; /* @r{Cause @code{SIGSEGV} signal if @code{p} is null.} */
  6172. x + y; /* @r{Useless statement without effect.} */
  6173. @end smallexample
  6174. In very unusual circumstances we use an expression statement
  6175. whose purpose is to get a fault if an address is invalid:
  6176. @smallexample
  6177. volatile char *p;
  6178. @r{@dots{}}
  6179. *p; /* @r{Cause signal if @code{p} is null.} */
  6180. @end smallexample
  6181. If the target of @code{p} is not declared @code{volatile}, the
  6182. compiler might optimize away the memory access, since it knows that
  6183. the value isn't really used. @xref{volatile}.
  6184. @node if Statement
  6185. @section @code{if} Statement
  6186. @cindex @code{if} statement
  6187. @cindex statement, @code{if}
  6188. @findex if
  6189. An @code{if} statement computes an expression to decide
  6190. whether to execute the following statement or not.
  6191. It looks like this:
  6192. @example
  6193. if (@var{condition})
  6194. @var{execute-if-true}
  6195. @end example
  6196. The first thing this does is compute the value of @var{condition}. If
  6197. that is true (nonzero), then it executes the statement
  6198. @var{execute-if-true}. If the value of @var{condition} is false
  6199. (zero), it doesn't execute @var{execute-if-true}; instead, it does
  6200. nothing.
  6201. This is a @dfn{complex statement} because it contains a component
  6202. @var{if-true-substatement} that is a nested statement. It must be one
  6203. and only one statement. The way to put multiple statements there is
  6204. to group them into a @dfn{block} (@pxref{Blocks}).
  6205. @node if-else Statement
  6206. @section @code{if-else} Statement
  6207. @cindex @code{if}@dots{}@code{else} statement
  6208. @cindex statement, @code{if}@dots{}@code{else}
  6209. @findex else
  6210. An @code{if}-@code{else} statement computes an expression to decide
  6211. which of two nested statements to execute.
  6212. It looks like this:
  6213. @example
  6214. if (@var{condition})
  6215. @var{if-true-substatement}
  6216. else
  6217. @var{if-false-substatement}
  6218. @end example
  6219. The first thing this does is compute the value of @var{condition}. If
  6220. that is true (nonzero), then it executes the statement
  6221. @var{if-true-substatement}. If the value of @var{condition} is false
  6222. (zero), then it executes the statement @var{if-false-substatement} instead.
  6223. This is a @dfn{complex statement} because it contains components
  6224. @var{if-true-substatement} and @var{if-else-substatement} that are
  6225. nested statements. Each must be one and only one statement. The way
  6226. to put multiple statements in such a component is to group them into a
  6227. @dfn{block} (@pxref{Blocks}).
  6228. @node Blocks
  6229. @section Blocks
  6230. @cindex block
  6231. @cindex compound statement
  6232. A @dfn{block} is a construct that contains multiple statements of any
  6233. kind. It begins with @samp{@{} and ends with @samp{@}}, and has a
  6234. series of statements and declarations in between. Another name for
  6235. blocks is @dfn{compound statements}.
  6236. Is a block a statement? Yes and no. It doesn't @emph{look} like a
  6237. normal statement---it does not end with a semicolon. But you can
  6238. @emph{use} it like a statement; anywhere that a statement is required
  6239. or allowed, you can write a block and consider that block a statement.
  6240. So far it seems that a block is a kind of statement with an unusual
  6241. syntax. But that is not entirely true: a function body is also a
  6242. block, and that block is definitely not a statement. The text after a
  6243. function header is not treated as a statement; only a function body is
  6244. allowed there, and nothing else would be meaningful there.
  6245. In a formal grammar we would have to choose---either a block is a kind
  6246. of statement or it is not. But this manual is meant for humans, not
  6247. for parser generators. The clearest answer for humans is, ``a block
  6248. is a statement, in some ways.''
  6249. @cindex nested block
  6250. @cindex internal block
  6251. A block that isn't a function body is called an @dfn{internal block}
  6252. or a @dfn{nested block}. You can put a nested block directly inside
  6253. another block, but more often the nested block is inside some complex
  6254. statement, such as a @code{for} statement or an @code{if} statement.
  6255. There are two uses for nested blocks in C:
  6256. @itemize @bullet
  6257. @item
  6258. To specify the scope for local declarations. For instance, a local
  6259. variable's scope is the rest of the innermost containing block.
  6260. @item
  6261. To write a series of statements where, syntactically, one statement is
  6262. called for. For instance, the @var{execute-if-true} of an @code{if}
  6263. statement is one statement. To put multiple statements there, they
  6264. have to be wrapped in a block, like this:
  6265. @example
  6266. if (x < 0)
  6267. @{
  6268. printf ("x was negative\n");
  6269. x = -x;
  6270. @}
  6271. @end example
  6272. @end itemize
  6273. This example (repeated from above) shows a nested block which serves
  6274. both purposes: it includes two statements (plus a declaration) in the
  6275. body of a @code{while} statement, and it provides the scope for the
  6276. declaration of @code{q}.
  6277. @example
  6278. void
  6279. free_intlist (struct intlistlink *p)
  6280. @{
  6281. while (p)
  6282. @{
  6283. struct intlistlink *q = p;
  6284. p = p->next;
  6285. free (q);
  6286. @}
  6287. @}
  6288. @end example
  6289. @node return Statement
  6290. @section @code{return} Statement
  6291. @cindex @code{return} statement
  6292. @cindex statement, @code{return}
  6293. @findex return
  6294. The @code{return} statement makes the containing function return
  6295. immediately. It has two forms. This one specifies no value to
  6296. return:
  6297. @example
  6298. return;
  6299. @end example
  6300. @noindent
  6301. That form is meant for functions whose return type is @code{void}
  6302. (@pxref{The Void Type}). You can also use it in a function that
  6303. returns nonvoid data, but that's a bad idea, since it makes the
  6304. function return garbage.
  6305. The form that specifies a value looks like this:
  6306. @example
  6307. return @var{value};
  6308. @end example
  6309. @noindent
  6310. which computes the expression @var{value} and makes the function
  6311. return that. If necessary, the value undergoes type conversion to
  6312. the function's declared return value type, which works like
  6313. assigning the value to a variable of that type.
  6314. @node Loop Statements
  6315. @section Loop Statements
  6316. @cindex loop statements
  6317. @cindex statements, loop
  6318. @cindex iteration
  6319. You can use a loop statement when you need to execute a series of
  6320. statements repeatedly, making an @dfn{iteration}. C provides several
  6321. different kinds of loop statements, described in the following
  6322. subsections.
  6323. Every kind of loop statement is a complex statement because contains a
  6324. component, here called @var{body}, which is a nested statement.
  6325. Most often the body is a block.
  6326. @menu
  6327. * while Statement:: Loop as long as a test expression is true.
  6328. * do-while Statement:: Execute a loop once, with further looping
  6329. as long as a test expression is true.
  6330. * break Statement:: End a loop immediately.
  6331. * for Statement:: Iterative looping.
  6332. * Example of for:: An example of iterative looping.
  6333. * Omitted for-Expressions:: for-loop expression options.
  6334. * for-Index Declarations:: for-loop declaration options.
  6335. * continue Statement:: Begin the next cycle of a loop.
  6336. @end menu
  6337. @node while Statement
  6338. @subsection @code{while} Statement
  6339. @cindex @code{while} statement
  6340. @cindex statement, @code{while}
  6341. @findex while
  6342. The @code{while} statement is the simplest loop construct.
  6343. It looks like this:
  6344. @example
  6345. while (@var{test})
  6346. @var{body}
  6347. @end example
  6348. Here, @var{body} is a statement (often a nested block) to repeat, and
  6349. @var{test} is the test expression that controls whether to repeat it again.
  6350. Each iteration of the loop starts by computing @var{test} and, if it
  6351. is true (nonzero), that means the loop should execute @var{body} again
  6352. and then start over.
  6353. Here's an example of advancing to the last structure in a chain of
  6354. structures chained through the @code{next} field:
  6355. @example
  6356. #include <stddef.h> /* @r{Defines @code{NULL}.} */
  6357. @r{@dots{}}
  6358. while (chain->next != NULL)
  6359. chain = chain->next;
  6360. @end example
  6361. @noindent
  6362. This code assumes the chain isn't empty to start with; if the chain is
  6363. empty (that is, if @code{chain} is a null pointer), the code gets a
  6364. @code{SIGSEGV} signal trying to dereference that null pointer (@pxref{Signals}).
  6365. @node do-while Statement
  6366. @subsection @code{do-while} Statement
  6367. @cindex @code{do}--@code{while} statement
  6368. @cindex statement, @code{do}--@code{while}
  6369. @findex do
  6370. The @code{do}--@code{while} statement is a simple loop construct that
  6371. performs the test at the end of the iteration.
  6372. @example
  6373. do
  6374. @var{body}
  6375. while (@var{test});
  6376. @end example
  6377. Here, @var{body} is a statement (possibly a block) to repeat, and
  6378. @var{test} is an expression that controls whether to repeat it again.
  6379. Each iteration of the loop starts by executing @var{body}. Then it
  6380. computes @var{test} and, if it is true (nonzero), that means to go
  6381. back and start over with @var{body}. If @var{test} is false (zero),
  6382. then the loop stops repeating and execution moves on past it.
  6383. @node break Statement
  6384. @subsection @code{break} Statement
  6385. @cindex @code{break} statement
  6386. @cindex statement, @code{break}
  6387. @findex break
  6388. The @code{break} statement looks like @samp{break;}. Its effect is to
  6389. exit immediately from the innermost loop construct or @code{switch}
  6390. statement (@pxref{switch Statement}).
  6391. For example, this loop advances @code{p} until the next null
  6392. character or newline.
  6393. @example
  6394. while (*p)
  6395. @{
  6396. /* @r{End loop if we have reached a newline.} */
  6397. if (*p == '\n')
  6398. break;
  6399. p++
  6400. @}
  6401. @end example
  6402. When there are nested loops, the @code{break} statement exits from the
  6403. innermost loop containing it.
  6404. @example
  6405. struct list_if_tuples
  6406. @{
  6407. struct list_if_tuples next;
  6408. int length;
  6409. data *contents;
  6410. @};
  6411. void
  6412. process_all_elements (struct list_if_tuples *list)
  6413. @{
  6414. while (list)
  6415. @{
  6416. /* @r{Process all the elements in this node's vector,}
  6417. @r{stopping when we reach one that is null.} */
  6418. for (i = 0; i < list->length; i++
  6419. @{
  6420. /* @r{Null element terminates this node's vector.} */
  6421. if (list->contents[i] == NULL)
  6422. /* @r{Exit the @code{for} loop.} */
  6423. break;
  6424. /* @r{Operate on the next element.} */
  6425. process_element (list->contents[i]);
  6426. @}
  6427. list = list->next;
  6428. @}
  6429. @}
  6430. @end example
  6431. The only way in C to exit from an outer loop is with
  6432. @code{goto} (@pxref{goto Statement}).
  6433. @node for Statement
  6434. @subsection @code{for} Statement
  6435. @cindex @code{for} statement
  6436. @cindex statement, @code{for}
  6437. @findex for
  6438. A @code{for} statement uses three expressions written inside a
  6439. parenthetical group to define the repetition of the loop. The first
  6440. expression says how to prepare to start the loop. The second says how
  6441. to test, before each iteration, whether to continue looping. The
  6442. third says how to advance, at the end of an iteration, for the next
  6443. iteration. All together, it looks like this:
  6444. @example
  6445. for (@var{start}; @var{continue-test}; @var{advance})
  6446. @var{body}
  6447. @end example
  6448. The first thing the @code{for} statement does is compute @var{start}.
  6449. The next thing it does is compute the expression @var{continue-test}.
  6450. If that expression is false (zero), the @code{for} statement finishes
  6451. immediately, so @var{body} is executed zero times.
  6452. However, if @var{continue-test} is true (nonzero), the @code{for}
  6453. statement executes @var{body}, then @var{advance}. Then it loops back
  6454. to the not-quite-top to test @var{continue-test} again. But it does
  6455. not compute @var{start} again.
  6456. @node Example of for
  6457. @subsection Example of @code{for}
  6458. Here is the @code{for} statement from the iterative Fibonacci
  6459. function:
  6460. @example
  6461. int i;
  6462. for (i = 1; i < n; ++i)
  6463. /* @r{If @code{n} is 1 or less, the loop runs zero times,} */
  6464. /* @r{since @code{i < n} is false the first time.} */
  6465. @{
  6466. /* @r{Now @var{last} is @code{fib (@var{i})}}
  6467. @r{and @var{prev} is @code{fib (@var{i} @minus{} 1)}.} */
  6468. /* @r{Compute @code{fib (@var{i} + 1)}.} */
  6469. int next = prev + last;
  6470. /* @r{Shift the values down.} */
  6471. prev = last;
  6472. last = next;
  6473. /* @r{Now @var{last} is @code{fib (@var{i} + 1)}}
  6474. @r{and @var{prev} is @code{fib (@var{i})}.}
  6475. @r{But that won't stay true for long,}
  6476. @r{because we are about to increment @var{i}.} */
  6477. @}
  6478. @end example
  6479. In this example, @var{start} is @code{i = 1}, meaning set @code{i} to
  6480. 1. @var{continue-test} is @code{i < n}, meaning keep repeating the
  6481. loop as long as @code{i} is less than @code{n}. @var{advance} is
  6482. @code{i++}, meaning increment @code{i} by 1. The body is a block
  6483. that contains a declaration and two statements.
  6484. @node Omitted for-Expressions
  6485. @subsection Omitted @code{for}-Expressions
  6486. A fully-fleshed @code{for} statement contains all these parts,
  6487. @example
  6488. for (@var{start}; @var{continue-test}; @var{advance})
  6489. @var{body}
  6490. @end example
  6491. @noindent
  6492. but you can omit any of the three expressions inside the parentheses.
  6493. The parentheses and the two semicolons are required syntactically, but
  6494. the expressions between them may be missing. A missing expression
  6495. means this loop doesn't use that particular feature of the @code{for}
  6496. statement.
  6497. @c ??? You can't do this if START is a declaration.
  6498. Instead of using @var{start}, you can do the loop preparation
  6499. before the @code{for} statement: the effect is the same. So we
  6500. could have written the beginning of the previous example this way:
  6501. @example
  6502. int i = 0;
  6503. for (; i < n; ++i)
  6504. @end example
  6505. @noindent
  6506. instead of this way:
  6507. @example
  6508. int i;
  6509. for (i = 0; i < n; ++i)
  6510. @end example
  6511. Omitting @var{continue-test} means the loop runs forever (or until
  6512. something else causes exit from it). Statements inside the loop can
  6513. test conditions for termination and use @samp{break;} to exit. This
  6514. is more flexible since you can put those tests anywhere in the loop,
  6515. not solely at the beginning.
  6516. Putting an expression in @var{advance} is almost equivalent to writing
  6517. it at the end of the loop body; it does almost the same thing. The
  6518. only difference is for the @code{continue} statement (@pxref{continue
  6519. Statement}). So we could have written this:
  6520. @example
  6521. for (i = 0; i < n;)
  6522. @{
  6523. @r{@dots{}}
  6524. ++i;
  6525. @}
  6526. @end example
  6527. @noindent
  6528. instead of this:
  6529. @example
  6530. for (i = 0; i < n; ++i)
  6531. @{
  6532. @r{@dots{}}
  6533. @}
  6534. @end example
  6535. The choice is mainly a matter of what is more readable for
  6536. programmers. However, there is also a syntactic difference:
  6537. @var{advance} is an expression, not a statement. It can't include
  6538. loops, blocks, declarations, etc.
  6539. @node for-Index Declarations
  6540. @subsection @code{for}-Index Declarations
  6541. You can declare loop-index variables directly in the @var{start}
  6542. portion of the @code{for}-loop, like this:
  6543. @example
  6544. for (int i = 0; i < n; ++i)
  6545. @{
  6546. @r{@dots{}}
  6547. @}
  6548. @end example
  6549. This kind of @var{start} is limited to a single declaration; it can
  6550. declare one or more variables, separated by commas, all of which are
  6551. the same @var{basetype} (@code{int}, in this example):
  6552. @example
  6553. for (int i = 0, j = 1, *p = NULL; i < n; ++i, ++j, ++p)
  6554. @{
  6555. @r{@dots{}}
  6556. @}
  6557. @end example
  6558. @noindent
  6559. The scope of these variables is the @code{for} statement as a whole.
  6560. See @ref{Variable Declarations} for a explanation of @var{basetype}.
  6561. Variables declared in @code{for} statements should have initializers.
  6562. Omitting the initialization gives the variables unpredictable initial
  6563. values, so this code is erroneous.
  6564. @example
  6565. for (int i; i < n; ++i)
  6566. @{
  6567. @r{@dots{}}
  6568. @}
  6569. @end example
  6570. @node continue Statement
  6571. @subsection @code{continue} Statement
  6572. @cindex @code{continue} statement
  6573. @cindex statement, @code{continue}
  6574. @findex continue
  6575. The @code{continue} statement looks like @samp{continue;}, and its
  6576. effect is to jump immediately to the end of the innermost loop
  6577. construct. If it is a @code{for}-loop, the next thing that happens
  6578. is to execute the loop's @var{advance} expression.
  6579. For example, this loop increments @code{p} until the next null character
  6580. or newline, and operates (in some way not shown) on all the characters
  6581. in the line except for spaces. All it does with spaces is skip them.
  6582. @example
  6583. for (;*p; ++p)
  6584. @{
  6585. /* @r{End loop if we have reached a newline.} */
  6586. if (*p == '\n')
  6587. break;
  6588. /* @r{Pay no attention to spaces.} */
  6589. if (*p == ' ')
  6590. continue;
  6591. /* @r{Operate on the next character.} */
  6592. @r{@dots{}}
  6593. @}
  6594. @end example
  6595. @noindent
  6596. Executing @samp{continue;} skips the loop body but it does not
  6597. skip the @var{advance} expression, @code{p++}.
  6598. We could also write it like this:
  6599. @example
  6600. for (;*p; ++p)
  6601. @{
  6602. /* @r{Exit if we have reached a newline.} */
  6603. if (*p == '\n')
  6604. break;
  6605. /* @r{Pay no attention to spaces.} */
  6606. if (*p != ' ')
  6607. @{
  6608. /* @r{Operate on the next character.} */
  6609. @r{@dots{}}
  6610. @}
  6611. @}
  6612. @end example
  6613. The advantage of using @code{continue} is that it reduces the
  6614. depth of nesting.
  6615. Contrast @code{continue} with the @code{break} statement. @xref{break
  6616. Statement}.
  6617. @node switch Statement
  6618. @section @code{switch} Statement
  6619. @cindex @code{switch} statement
  6620. @cindex statement, @code{switch}
  6621. @findex switch
  6622. @findex case
  6623. @findex default
  6624. The @code{switch} statement selects code to run according to the value
  6625. of an expression. The expression, in parentheses, follows the keyword
  6626. @code{switch}. After that come all the cases to select among,
  6627. inside braces. It looks like this:
  6628. @example
  6629. switch (@var{selector})
  6630. @{
  6631. @var{cases}@r{@dots{}}
  6632. @}
  6633. @end example
  6634. A case can look like this:
  6635. @example
  6636. case @var{value}:
  6637. @var{statements}
  6638. break;
  6639. @end example
  6640. @noindent
  6641. which means ``come here if @var{selector} happens to have the value
  6642. @var{value},'' or like this (a GNU C extension):
  6643. @example
  6644. case @var{rangestart} ... @var{rangeend}:
  6645. @var{statements}
  6646. break;
  6647. @end example
  6648. @noindent
  6649. which means ``come here if @var{selector} happens to have a value
  6650. between @var{rangestart} and @var{rangeend} (inclusive).'' @xref{Case
  6651. Ranges}.
  6652. The values in @code{case} labels must reduce to integer constants.
  6653. They can use arithmetic, and @code{enum} constants, but they cannot
  6654. refer to data in memory, because they have to be computed at compile
  6655. time. It is an error if two @code{case} labels specify the same
  6656. value, or ranges that overlap, or if one is a range and the other is a
  6657. value in that range.
  6658. You can also define a default case to handle ``any other value,'' like
  6659. this:
  6660. @example
  6661. default:
  6662. @var{statements}
  6663. break;
  6664. @end example
  6665. If the @code{switch} statement has no @code{default:} label, then it
  6666. does nothing when the value matches none of the cases.
  6667. The brace-group inside the @code{switch} statement is a block, and you
  6668. can declare variables with that scope just as in any other block
  6669. (@pxref{Blocks}). However, initializers in these declarations won't
  6670. necessarily be executed every time the @code{switch} statement runs,
  6671. so it is best to avoid giving them initializers.
  6672. @code{break;} inside a @code{switch} statement exits immediately from
  6673. the @code{switch} statement. @xref{break Statement}.
  6674. If there is no @code{break;} at the end of the code for a case,
  6675. execution continues into the code for the following case. This
  6676. happens more often by mistake than intentionally, but since this
  6677. feature is used in real code, we cannot eliminate it.
  6678. @strong{Warning:} When one case is intended to fall through to the
  6679. next, write a comment like @samp{falls through} to say it's
  6680. intentional. That way, other programmers won't assume it was an error
  6681. and ``fix'' it erroneously.
  6682. Consecutive @code{case} statements could, pedantically, be considered
  6683. an instance of falling through, but we don't consider or treat them that
  6684. way because they won't confuse anyone.
  6685. @node switch Example
  6686. @section Example of @code{switch}
  6687. Here's an example of using the @code{switch} statement
  6688. to distinguish among characters:
  6689. @cindex counting vowels and punctuation
  6690. @example
  6691. struct vp @{ int vowels, punct; @};
  6692. struct vp
  6693. count_vowels_and_punct (char *string)
  6694. @{
  6695. int c;
  6696. int vowels = 0;
  6697. int punct = 0;
  6698. /* @r{Don't change the parameter itself.} */
  6699. /* @r{That helps in debugging.} */
  6700. char *p = string;
  6701. struct vp value;
  6702. while (c = *p++)
  6703. switch (c)
  6704. @{
  6705. case 'y':
  6706. case 'Y':
  6707. /* @r{We assume @code{y_is_consonant} will check surrounding
  6708. letters to determine whether this y is a vowel.} */
  6709. if (y_is_consonant (p - 1))
  6710. break;
  6711. /* @r{Falls through} */
  6712. case 'a':
  6713. case 'e':
  6714. case 'i':
  6715. case 'o':
  6716. case 'u':
  6717. case 'A':
  6718. case 'E':
  6719. case 'I':
  6720. case 'O':
  6721. case 'U':
  6722. vowels++;
  6723. break;
  6724. case '.':
  6725. case ',':
  6726. case ':':
  6727. case ';':
  6728. case '?':
  6729. case '!':
  6730. case '\"':
  6731. case '\'':
  6732. punct++;
  6733. break;
  6734. @}
  6735. value.vowels = vowels;
  6736. value.punct = punct;
  6737. return value;
  6738. @}
  6739. @end example
  6740. @node Duffs Device
  6741. @section Duff's Device
  6742. @cindex Duff's device
  6743. The cases in a @code{switch} statement can be inside other control
  6744. constructs. For instance, we can use a technique known as @dfn{Duff's
  6745. device} to optimize this simple function,
  6746. @example
  6747. void
  6748. copy (char *to, char *from, int count)
  6749. @{
  6750. while (count > 0)
  6751. *to++ = *from++, count--;
  6752. @}
  6753. @end example
  6754. @noindent
  6755. which copies memory starting at @var{from} to memory starting at
  6756. @var{to}.
  6757. Duff's device involves unrolling the loop so that it copies
  6758. several characters each time around, and using a @code{switch} statement
  6759. to enter the loop body at the proper point:
  6760. @example
  6761. void
  6762. copy (char *to, char *from, int count)
  6763. @{
  6764. if (count <= 0)
  6765. return;
  6766. int n = (count + 7) / 8;
  6767. switch (count % 8)
  6768. @{
  6769. do @{
  6770. case 0: *to++ = *from++;
  6771. case 7: *to++ = *from++;
  6772. case 6: *to++ = *from++;
  6773. case 5: *to++ = *from++;
  6774. case 4: *to++ = *from++;
  6775. case 3: *to++ = *from++;
  6776. case 2: *to++ = *from++;
  6777. case 1: *to++ = *from++;
  6778. @} while (--n > 0);
  6779. @}
  6780. @}
  6781. @end example
  6782. @node Case Ranges
  6783. @section Case Ranges
  6784. @cindex case ranges
  6785. @cindex ranges in case statements
  6786. You can specify a range of consecutive values in a single @code{case} label,
  6787. like this:
  6788. @example
  6789. case @var{low} ... @var{high}:
  6790. @end example
  6791. @noindent
  6792. This has the same effect as the proper number of individual @code{case}
  6793. labels, one for each integer value from @var{low} to @var{high}, inclusive.
  6794. This feature is especially useful for ranges of ASCII character codes:
  6795. @example
  6796. case 'A' ... 'Z':
  6797. @end example
  6798. @strong{Be careful:} with integers, write spaces around the @code{...}
  6799. to prevent it from being parsed wrong. For example, write this:
  6800. @example
  6801. case 1 ... 5:
  6802. @end example
  6803. @noindent
  6804. rather than this:
  6805. @example
  6806. case 1...5:
  6807. @end example
  6808. @node Null Statement
  6809. @section Null Statement
  6810. @cindex null statement
  6811. @cindex statement, null
  6812. A @dfn{null statement} is just a semicolon. It does nothing.
  6813. A null statement is a placeholder for use where a statement is
  6814. grammatically required, but there is nothing to be done. For
  6815. instance, sometimes all the work of a @code{for}-loop is done in the
  6816. @code{for}-header itself, leaving no work for the body. Here is an
  6817. example that searches for the first newline in @code{array}:
  6818. @example
  6819. for (p = array; *p != '\n'; p++)
  6820. ;
  6821. @end example
  6822. @node goto Statement
  6823. @section @code{goto} Statement and Labels
  6824. @cindex @code{goto} statement
  6825. @cindex statement, @code{goto}
  6826. @cindex label
  6827. @findex goto
  6828. The @code{goto} statement looks like this:
  6829. @example
  6830. goto @var{label};
  6831. @end example
  6832. @noindent
  6833. Its effect is to transfer control immediately to another part of the
  6834. current function---where the label named @var{label} is defined.
  6835. An ordinary label definition looks like this:
  6836. @example
  6837. @var{label}:
  6838. @end example
  6839. @noindent
  6840. and it can appear before any statement. You can't use @code{default}
  6841. as a label, since that has a special meaning for @code{switch}
  6842. statements.
  6843. An ordinary label doesn't need a separate declaration; defining it is
  6844. enough.
  6845. Here's an example of using @code{goto} to implement a loop
  6846. equivalent to @code{do}--@code{while}:
  6847. @example
  6848. @{
  6849. loop_restart:
  6850. @var{body}
  6851. if (@var{condition})
  6852. goto loop_restart;
  6853. @}
  6854. @end example
  6855. The name space of labels is separate from that of variables and functions.
  6856. Thus, there is no error in using a single name in both ways:
  6857. @example
  6858. @{
  6859. int foo; // @r{Variable @code{foo}.}
  6860. foo: // @r{Label @code{foo}.}
  6861. @var{body}
  6862. if (foo > 0) // @r{Variable @code{foo}.}
  6863. goto foo; // @r{Label @code{foo}.}
  6864. @}
  6865. @end example
  6866. Blocks have no effect on ordinary labels; each label name is defined
  6867. throughout the whole of the function it appears in. It looks strange to
  6868. jump into a block with @code{goto}, but it works. For example,
  6869. @example
  6870. if (x < 0)
  6871. goto negative;
  6872. if (y < 0)
  6873. @{
  6874. negative:
  6875. printf ("Negative\n");
  6876. return;
  6877. @}
  6878. @end example
  6879. If the goto jumps into the scope of a variable, it does not
  6880. initialize the variable. For example, if @code{x} is negative,
  6881. @example
  6882. if (x < 0)
  6883. goto negative;
  6884. if (y < 0)
  6885. @{
  6886. int i = 5;
  6887. negative:
  6888. printf ("Negative, and i is %d\n", i);
  6889. return;
  6890. @}
  6891. @end example
  6892. @noindent
  6893. prints junk because @code{i} was not initialized.
  6894. If the block declares a variable-length automatic array, jumping into
  6895. it gives a compilation error. However, jumping out of the scope of a
  6896. variable-length array works fine, and deallocates its storage.
  6897. A label can't come directly before a declaration, so the code can't
  6898. jump directly to one. For example, this is not allowed:
  6899. @example
  6900. @{
  6901. goto foo;
  6902. foo:
  6903. int x = 5;
  6904. bar(&x);
  6905. @}
  6906. @end example
  6907. @noindent
  6908. The workaround is to add a statement, even an empty statement,
  6909. directly after the label. For example:
  6910. @example
  6911. @{
  6912. goto foo;
  6913. foo:
  6914. ;
  6915. int x = 5;
  6916. bar(&x);
  6917. @}
  6918. @end example
  6919. Likewise, a label can't be the last thing in a block. The workaround
  6920. solution is the same: add a semicolon after the label.
  6921. These unnecessary restrictions on labels make no sense, and ought in
  6922. principle to be removed; but they do only a little harm since labels
  6923. and @code{goto} are rarely the best way to write a program.
  6924. These examples are all artificial; it would be more natural to
  6925. write them in other ways, without @code{goto}. For instance,
  6926. the clean way to write the example that prints @samp{Negative} is this:
  6927. @example
  6928. if (x < 0 || y < 0)
  6929. @{
  6930. printf ("Negative\n");
  6931. return;
  6932. @}
  6933. @end example
  6934. @noindent
  6935. It is hard to construct simple examples where @code{goto} is actually
  6936. the best way to write a program. Its rare good uses tend to be in
  6937. complex code, thus not apt for the purpose of explaining the meaning
  6938. of @code{goto}.
  6939. The only good time to use @code{goto} is when it makes the code
  6940. simpler than any alternative. Jumping backward is rarely desirable,
  6941. because usually the other looping and control constructs give simpler
  6942. code. Using @code{goto} to jump forward is more often desirable, for
  6943. instance when a function needs to do some processing in an error case
  6944. and errors can occur at various different places within the function.
  6945. @node Local Labels
  6946. @section Locally Declared Labels
  6947. @cindex local labels
  6948. @cindex macros, local labels
  6949. @findex __label__
  6950. In GNU C you can declare @dfn{local labels} in any nested block
  6951. scope. A local label is used in a @code{goto} statement just like an
  6952. ordinary label, but you can only reference it within the block in
  6953. which it was declared.
  6954. A local label declaration looks like this:
  6955. @example
  6956. __label__ @var{label};
  6957. @end example
  6958. @noindent
  6959. or
  6960. @example
  6961. __label__ @var{label1}, @var{label2}, @r{@dots{}};
  6962. @end example
  6963. Local label declarations must come at the beginning of the block,
  6964. before any ordinary declarations or statements.
  6965. The label declaration declares the label @emph{name}, but does not define
  6966. the label itself. That's done in the usual way, with
  6967. @code{@var{label}:}, before one of the statements in the block.
  6968. The local label feature is useful for complex macros. If a macro
  6969. contains nested loops, a @code{goto} can be useful for breaking out of
  6970. them. However, an ordinary label whose scope is the whole function
  6971. cannot be used: if the macro can be expanded several times in one
  6972. function, the label will be multiply defined in that function. A
  6973. local label avoids this problem. For example:
  6974. @example
  6975. #define SEARCH(value, array, target) \
  6976. do @{ \
  6977. __label__ found; \
  6978. __auto_type _SEARCH_target = (target); \
  6979. __auto_type _SEARCH_array = (array); \
  6980. int i, j; \
  6981. int value; \
  6982. for (i = 0; i < max; i++) \
  6983. for (j = 0; j < max; j++) \
  6984. if (_SEARCH_array[i][j] == _SEARCH_target) \
  6985. @{ (value) = i; goto found; @} \
  6986. (value) = -1; \
  6987. found:; \
  6988. @} while (0)
  6989. @end example
  6990. This could also be written using a statement expression
  6991. (@pxref{Statement Exprs}):
  6992. @example
  6993. #define SEARCH(array, target) \
  6994. (@{ \
  6995. __label__ found; \
  6996. __auto_type _SEARCH_target = (target); \
  6997. __auto_type _SEARCH_array = (array); \
  6998. int i, j; \
  6999. int value; \
  7000. for (i = 0; i < max; i++) \
  7001. for (j = 0; j < max; j++) \
  7002. if (_SEARCH_array[i][j] == _SEARCH_target) \
  7003. @{ value = i; goto found; @} \
  7004. value = -1; \
  7005. found: \
  7006. value; \
  7007. @})
  7008. @end example
  7009. Ordinary labels are visible throughout the function where they are
  7010. defined, and only in that function. However, explicitly declared
  7011. local labels of a block are visible in nested function definitions
  7012. inside that block. @xref{Nested Functions}, for details.
  7013. @xref{goto Statement}.
  7014. @node Labels as Values
  7015. @section Labels as Values
  7016. @cindex labels as values
  7017. @cindex computed gotos
  7018. @cindex goto with computed label
  7019. @cindex address of a label
  7020. In GNU C, you can get the address of a label defined in the current
  7021. function (or a local label defined in the containing function) with
  7022. the unary operator @samp{&&}. The value has type @code{void *}. This
  7023. value is a constant and can be used wherever a constant of that type
  7024. is valid. For example:
  7025. @example
  7026. void *ptr;
  7027. @r{@dots{}}
  7028. ptr = &&foo;
  7029. @end example
  7030. To use these values requires a way to jump to one. This is done
  7031. with the computed goto statement@footnote{The analogous feature in
  7032. Fortran is called an assigned goto, but that name seems inappropriate in
  7033. C, since you can do more with label addresses than store them in special label
  7034. variables.}, @code{goto *@var{exp};}. For example,
  7035. @example
  7036. goto *ptr;
  7037. @end example
  7038. @noindent
  7039. Any expression of type @code{void *} is allowed.
  7040. @xref{goto Statement}.
  7041. @menu
  7042. * Label Value Uses:: Examples of using label values.
  7043. * Label Value Caveats:: Limitations of label values.
  7044. @end menu
  7045. @node Label Value Uses
  7046. @subsection Label Value Uses
  7047. One use for label-valued constants is to initialize a static array to
  7048. serve as a jump table:
  7049. @example
  7050. static void *array[] = @{ &&foo, &&bar, &&hack @};
  7051. @end example
  7052. Then you can select a label with indexing, like this:
  7053. @example
  7054. goto *array[i];
  7055. @end example
  7056. @noindent
  7057. Note that this does not check whether the subscript is in bounds---array
  7058. indexing in C never checks that.
  7059. You can make the table entries offsets instead of addresses
  7060. by subtracting one label from the others. Here is an example:
  7061. @example
  7062. static const int array[] = @{ &&foo - &&foo, &&bar - &&foo,
  7063. &&hack - &&foo @};
  7064. goto *(&&foo + array[i]);
  7065. @end example
  7066. @noindent
  7067. Using offsets is preferable in shared libraries, as it avoids the need
  7068. for dynamic relocation of the array elements; therefore, the array can
  7069. be read-only.
  7070. An array of label values or offsets serves a purpose much like that of
  7071. the @code{switch} statement. The @code{switch} statement is cleaner,
  7072. so use @code{switch} by preference when feasible.
  7073. Another use of label values is in an interpreter for threaded code.
  7074. The labels within the interpreter function can be stored in the
  7075. threaded code for super-fast dispatching.
  7076. @node Label Value Caveats
  7077. @subsection Label Value Caveats
  7078. Jumping to a label defined in another function does not work.
  7079. It can cause unpredictable results.
  7080. The best way to avoid this is to store label values only in
  7081. automatic variables, or static variables whose names are declared
  7082. within the function. Never pass them as arguments.
  7083. @cindex cloning
  7084. An optimization known as @dfn{cloning} generates multiple simplified
  7085. variants of a function's code, for use with specific fixed arguments.
  7086. Using label values in certain ways, such as saving the address in one
  7087. call to the function and using it again in another call, would make cloning
  7088. give incorrect results. These functions must disable cloning.
  7089. Inlining calls to the function would also result in multiple copies of
  7090. the code, each with its own value of the same label. Using the label
  7091. in a computed goto is no problem, because the computed goto inhibits
  7092. inlining. However, using the label value in some other way, such as
  7093. an indication of where an error occurred, would be optimized wrong.
  7094. These functions must disable inlining.
  7095. To prevent inlining or cloning of a function, specify
  7096. @code{__attribute__((__noinline__,__noclone__))} in its definition.
  7097. @xref{Attributes}.
  7098. When a function uses a label value in a static variable initializer,
  7099. that automatically prevents inlining or cloning the function.
  7100. @node Statement Exprs
  7101. @section Statements and Declarations in Expressions
  7102. @cindex statements inside expressions
  7103. @cindex declarations inside expressions
  7104. @cindex expressions containing statements
  7105. @c the above section title wrapped and causes an underfull hbox.. i
  7106. @c changed it from "within" to "in". --mew 4feb93
  7107. A block enclosed in parentheses can be used as an expression in GNU
  7108. C@. This provides a way to use local variables, loops and switches within
  7109. an expression. We call it a @dfn{statement expression}.
  7110. Recall that a block is a sequence of statements
  7111. surrounded by braces. In this construct, parentheses go around the
  7112. braces. For example:
  7113. @example
  7114. (@{ int y = foo (); int z;
  7115. if (y > 0) z = y;
  7116. else z = - y;
  7117. z; @})
  7118. @end example
  7119. @noindent
  7120. is a valid (though slightly more complex than necessary) expression
  7121. for the absolute value of @code{foo ()}.
  7122. The last statement in the block should be an expression statement; an
  7123. expression followed by a semicolon, that is. The value of this
  7124. expression serves as the value of statement expression. If the last
  7125. statement is anything else, the statement expression's value is
  7126. @code{void}.
  7127. This feature is mainly useful in making macro definitions compute each
  7128. operand exactly once. @xref{Macros and Auto Type}.
  7129. Statement expressions are not allowed in expressions that must be
  7130. constant, such as the value for an enumerator, the width of a
  7131. bit-field, or the initial value of a static variable.
  7132. Jumping into a statement expression---with @code{goto}, or using a
  7133. @code{switch} statement outside the statement expression---is an
  7134. error. With a computed @code{goto} (@pxref{Labels as Values}), the
  7135. compiler can't detect the error, but it still won't work.
  7136. Jumping out of a statement expression is permitted, but since
  7137. subexpressions in C are not computed in a strict order, it is
  7138. unpredictable which other subexpressions will have been computed by
  7139. then. For example,
  7140. @example
  7141. foo (), ((@{ bar1 (); goto a; 0; @}) + bar2 ()), baz();
  7142. @end example
  7143. @noindent
  7144. calls @code{foo} and @code{bar1} before it jumps, and never
  7145. calls @code{baz}, but may or may not call @code{bar2}. If @code{bar2}
  7146. does get called, that occurs after @code{foo} and before @code{bar1}.
  7147. @node Variables
  7148. @chapter Variables
  7149. @cindex variables
  7150. Every variable used in a C program needs to be made known by a
  7151. @dfn{declaration}. It can be used only after it has been declared.
  7152. It is an error to declare a variable name more than once in the same
  7153. scope; an exception is that @code{extern} declarations and tentative
  7154. definitions can coexist with another declaration of the same
  7155. variable.
  7156. Variables can be declared anywhere within a block or file. (Older
  7157. versions of C required that all variable declarations within a block
  7158. occur before any statements.)
  7159. Variables declared within a function or block are @dfn{local} to
  7160. it. This means that the variable name is visible only until the end
  7161. of that function or block, and the memory space is allocated only
  7162. while control is within it.
  7163. Variables declared at the top level in a file are called @dfn{file-scope}.
  7164. They are assigned fixed, distinct memory locations, so they retain
  7165. their values for the whole execution of the program.
  7166. @menu
  7167. * Variable Declarations:: Name a variable and and reserve space for it.
  7168. * Initializers:: Assigning initial values to variables.
  7169. * Designated Inits:: Assigning initial values to array elements
  7170. at particular array indices.
  7171. * Auto Type:: Obtaining the type of a variable.
  7172. * Local Variables:: Variables declared in function definitions.
  7173. * File-Scope Variables:: Variables declared outside of
  7174. function definitions.
  7175. * Static Local Variables:: Variables declared within functions,
  7176. but with permanent storage allocation.
  7177. * Extern Declarations:: Declaring a variable
  7178. which is allocated somewhere else.
  7179. * Allocating File-Scope:: When is space allocated
  7180. for file-scope variables?
  7181. * auto and register:: Historically used storage directions.
  7182. * Omitting Types:: The bad practice of declaring variables
  7183. with implicit type.
  7184. @end menu
  7185. @node Variable Declarations
  7186. @section Variable Declarations
  7187. @cindex variable declarations
  7188. @cindex declaration of variables
  7189. Here's what a variable declaration looks like:
  7190. @example
  7191. @var{keywords} @var{basetype} @var{decorated-variable} @r{[}= @var{init}@r{]};
  7192. @end example
  7193. The @var{keywords} specify how to handle the scope of the variable
  7194. name and the allocation of its storage. Most declarations have
  7195. no keywords because the defaults are right for them.
  7196. C allows these keywords to come before or after @var{basetype}, or
  7197. even in the middle of it as in @code{unsigned static int}, but don't
  7198. do that---it would surprise other programmers. Always write the
  7199. keywords first.
  7200. The @var{basetype} can be any of the predefined types of C, or a type
  7201. keyword defined with @code{typedef}. It can also be @code{struct
  7202. @var{tag}}, @code{union @var{tag}}, or @code{enum @var{tag}}. In
  7203. addition, it can include type qualifiers such as @code{const} and
  7204. @code{volatile} (@pxref{Type Qualifiers}).
  7205. In the simplest case, @var{decorated-variable} is just the variable
  7206. name. That declares the variable with the type specified by
  7207. @var{basetype}. For instance,
  7208. @example
  7209. int foo;
  7210. @end example
  7211. @noindent
  7212. uses @code{int} as the @var{basetype} and @code{foo} as the
  7213. @var{decorated-variable}. It declares @code{foo} with type
  7214. @code{int}.
  7215. @example
  7216. struct tree_node foo;
  7217. @end example
  7218. @noindent
  7219. declares @code{foo} with type @code{struct tree_node}.
  7220. @menu
  7221. * Declaring Arrays and Pointers:: Declaration syntax for variables of
  7222. array and pointer types.
  7223. * Combining Variable Declarations:: More than one variable declaration
  7224. in a single statement.
  7225. @end menu
  7226. @node Declaring Arrays and Pointers
  7227. @subsection Declaring Arrays and Pointers
  7228. @cindex declaring arrays and pointers
  7229. @cindex array, declaring
  7230. @cindex pointers, declaring
  7231. To declare a variable that is an array, write
  7232. @code{@var{variable}[@var{length}]} for @var{decorated-variable}:
  7233. @example
  7234. int foo[5];
  7235. @end example
  7236. To declare a variable that has a pointer type, write
  7237. @code{*@var{variable}} for @var{decorated-variable}:
  7238. @example
  7239. struct list_elt *foo;
  7240. @end example
  7241. These constructs nest. For instance,
  7242. @example
  7243. int foo[3][5];
  7244. @end example
  7245. @noindent
  7246. declares @code{foo} as an array of 3 arrays of 5 integers each,
  7247. @example
  7248. struct list_elt *foo[5];
  7249. @end example
  7250. @noindent
  7251. declares @code{foo} as an array of 5 pointers to structures, and
  7252. @example
  7253. struct list_elt **foo;
  7254. @end example
  7255. @noindent
  7256. declares @code{foo} as a pointer to a pointer to a structure.
  7257. @example
  7258. int **(*foo[30])(int, double);
  7259. @end example
  7260. @noindent
  7261. declares @code{foo} as an array of 30 pointers to functions
  7262. (@pxref{Function Pointers}), each of which must accept two arguments
  7263. (one @code{int} and one @code{double}) and return type @code{int **}.
  7264. @example
  7265. void
  7266. bar (int size)
  7267. @{
  7268. int foo[size];
  7269. @r{@dots{}}
  7270. @}
  7271. @end example
  7272. @noindent
  7273. declares @code{foo} as an array of integers with a size specified at
  7274. run time when the function @code{bar} is called.
  7275. @node Combining Variable Declarations
  7276. @subsection Combining Variable Declarations
  7277. @cindex combining variable declarations
  7278. @cindex variable declarations, combining
  7279. @cindex declarations, combining
  7280. When multiple declarations have the same @var{keywords} and
  7281. @var{basetype}, you can combine them using commas. Thus,
  7282. @example
  7283. @var{keywords} @var{basetype}
  7284. @var{decorated-variable-1} @r{[}= @var{init1}@r{]},
  7285. @var{decorated-variable-2} @r{[}= @var{init2}@r{]};
  7286. @end example
  7287. @noindent
  7288. is equivalent to
  7289. @example
  7290. @var{keywords} @var{basetype}
  7291. @var{decorated-variable-1} @r{[}= @var{init1}@r{]};
  7292. @var{keywords} @var{basetype}
  7293. @var{decorated-variable-2} @r{[}= @var{init2}@r{]};
  7294. @end example
  7295. Here are some simple examples:
  7296. @example
  7297. int a, b;
  7298. int a = 1, b = 2;
  7299. int a, *p, array[5];
  7300. int a = 0, *p = &a, array[5] = @{1, 2@};
  7301. @end example
  7302. @noindent
  7303. In the last two examples, @code{a} is an @code{int}, @code{p} is a
  7304. pointer to @code{int}, and @code{array} is an array of 5 @code{int}s.
  7305. Since the initializer for @code{array} specifies only two elements,
  7306. the other three elements are initialized to zero.
  7307. @node Initializers
  7308. @section Initializers
  7309. @cindex initializers
  7310. A variable's declaration, unless it is @code{extern}, should also
  7311. specify its initial value. For numeric and pointer-type variables,
  7312. the initializer is an expression for the value. If necessary, it is
  7313. converted to the variable's type, just as in an assignment.
  7314. You can also initialize a local structure-type (@pxref{Structures}) or
  7315. local union-type (@pxref{Unions}) variable this way, from an
  7316. expression whose value has the same type. But you can't initialize an
  7317. array this way (@pxref{Arrays}), since arrays are not first-class
  7318. objects in C (@pxref{Limitations of C Arrays}) and there is no array
  7319. assignment.
  7320. You can initialize arrays and structures componentwise,
  7321. with a list of the elements or components. You can initialize
  7322. a union with any one of its alternatives.
  7323. @itemize @bullet
  7324. @item
  7325. A component-wise initializer for an array consists of element values
  7326. surrounded by @samp{@{@r{@dots{}}@}}. If the values in the initializer
  7327. don't cover all the elements in the array, the remaining elements are
  7328. initialized to zero.
  7329. You can omit the size of the array when you declare it, and let
  7330. the initializer specify the size:
  7331. @example
  7332. int array[] = @{ 3, 9, 12 @};
  7333. @end example
  7334. @item
  7335. A component-wise initializer for a structure consists of field values
  7336. surrounded by @samp{@{@r{@dots{}}@}}. Write the field values in the same
  7337. order as the fields are declared in the structure. If the values in
  7338. the initializer don't cover all the fields in the structure, the
  7339. remaining fields are initialized to zero.
  7340. @item
  7341. The initializer for a union-type variable has the form @code{@{
  7342. @var{value} @}}, where @var{value} initializes the @emph{first alternative}
  7343. in the union definition.
  7344. @end itemize
  7345. For an array of arrays, a structure containing arrays, an array of
  7346. structures, etc., you can nest these constructs. For example,
  7347. @example
  7348. struct point @{ double x, y; @};
  7349. struct point series[]
  7350. = @{ @{0, 0@}, @{1.5, 2.8@}, @{99, 100.0004@} @};
  7351. @end example
  7352. You can omit a pair of inner braces if they contain the right
  7353. number of elements for the sub-value they initialize, so that
  7354. no elements or fields need to be filled in with zeros.
  7355. But don't do that very much, as it gets confusing.
  7356. An array of @code{char} can be initialized using a string constant.
  7357. Recall that the string constant includes an implicit null character at
  7358. the end (@pxref{String Constants}). Using a string constant as
  7359. initializer means to use its contents as the initial values of the
  7360. array elements. Here are examples:
  7361. @example
  7362. char text[6] = "text!"; /* @r{Includes the null.} */
  7363. char text[5] = "text!"; /* @r{Excludes the null.} */
  7364. char text[] = "text!"; /* @r{Gets length 6.} */
  7365. char text[]
  7366. = @{ 't', 'e', 'x', 't', '!', 0 @}; /* @r{same as above.} */
  7367. char text[] = @{ "text!" @}; /* @r{Braces are optional.} */
  7368. @end example
  7369. @noindent
  7370. and this kind of initializer can be nested inside braces to initialize
  7371. structures or arrays that contain a @code{char}-array.
  7372. In like manner, you can use a wide string constant to initialize
  7373. an array of @code{wchar_t}.
  7374. @node Designated Inits
  7375. @section Designated Initializers
  7376. @cindex initializers with labeled elements
  7377. @cindex labeled elements in initializers
  7378. @cindex case labels in initializers
  7379. @cindex designated initializers
  7380. In a complex structure or long array, it's useful to indicate
  7381. which field or element we are initializing.
  7382. To designate specific array elements during initialization, include
  7383. the array index in brackets, and an assignment operator, for each
  7384. element:
  7385. @example
  7386. int foo[10] = @{ [3] = 42, [7] = 58 @};
  7387. @end example
  7388. @noindent
  7389. This does the same thing as:
  7390. @example
  7391. int foo[10] = @{ 0, 0, 0, 42, 0, 0, 0, 58, 0, 0 @};
  7392. @end example
  7393. The array initialization can include non-designated element values
  7394. alongside designated indices; these follow the expected ordering
  7395. of the array initialization, so that
  7396. @example
  7397. int foo[10] = @{ [3] = 42, 43, 44, [7] = 58 @};
  7398. @end example
  7399. @noindent
  7400. does the same thing as:
  7401. @example
  7402. int foo[10] = @{ 0, 0, 0, 42, 43, 44, 0, 58, 0, 0 @};
  7403. @end example
  7404. Note that you can only use constant expressions as array index values,
  7405. not variables.
  7406. If you need to initialize a subsequence of sequential array elements to
  7407. the same value, you can specify a range:
  7408. @example
  7409. int foo[100] = @{ [0 ... 19] = 42, [20 ... 99] = 43 @};
  7410. @end example
  7411. @noindent
  7412. Using a range this way is a GNU C extension.
  7413. When subsequence ranges overlap, each element is initialized by the
  7414. last specification that applies to it. Thus, this initialization is
  7415. equivalent to the previous one.
  7416. @example
  7417. int foo[100] = @{ [0 ... 99] = 43, [0 ... 19] = 42 @};
  7418. @end example
  7419. @noindent
  7420. as the second overrides the first for elements 0 through 19.
  7421. The value used to initialize a range of elements is evaluated only
  7422. once, for the first element in the range. So for example, this code
  7423. @example
  7424. int random_values[100]
  7425. = @{ [0 ... 99] = get_random_number() @};
  7426. @end example
  7427. @noindent
  7428. would initialize all 100 elements of the array @code{random_values} to
  7429. the same value---probably not what is intended.
  7430. Similarly, you can initialize specific fields of a structure variable
  7431. by specifying the field name prefixed with a dot:
  7432. @example
  7433. struct point @{ int x; int y; @};
  7434. struct point foo = @{ .y = 42; @};
  7435. @end example
  7436. @noindent
  7437. The same syntax works for union variables as well:
  7438. @example
  7439. union int_double @{ int i; double d; @};
  7440. union int_double foo = @{ .d = 34 @};
  7441. @end example
  7442. @noindent
  7443. This casts the integer value 34 to a double and stores it
  7444. in the union variable @code{foo}.
  7445. You can designate both array elements and structure elements in
  7446. the same initialization; for example, here's an array of point
  7447. structures:
  7448. @example
  7449. struct point point_array[10] = @{ [4].y = 32, [6].y = 39 @};
  7450. @end example
  7451. Along with the capability to specify particular array and structure
  7452. elements to initialize comes the possibility of initializing the same
  7453. element more than once:
  7454. @example
  7455. int foo[10] = @{ [4] = 42, [4] = 98 @};
  7456. @end example
  7457. @noindent
  7458. In such a case, the last initialization value is retained.
  7459. @node Auto Type
  7460. @section Referring to a Type with @code{__auto_type}
  7461. @findex __auto_type
  7462. @findex typeof
  7463. @cindex macros, types of arguments
  7464. You can declare a variable copying the type from
  7465. the initializer by using @code{__auto_type} instead of a particular type.
  7466. Here's an example:
  7467. @example
  7468. #define max(a,b) \
  7469. (@{ __auto_type _a = (a); \
  7470. __auto_type _b = (b); \
  7471. _a > _b ? _a : _b @})
  7472. @end example
  7473. This defines @code{_a} to be of the same type as @code{a}, and
  7474. @code{_b} to be of the same type as @code{b}. This is a useful thing
  7475. to do in a macro that ought to be able to handle any type of data
  7476. (@pxref{Macros and Auto Type}).
  7477. The original GNU C method for obtaining the type of a value is to use
  7478. @code{typeof}, which takes as an argument either a value or the name of
  7479. a type. The previous example could also be written as:
  7480. @example
  7481. #define max(a,b) \
  7482. (@{ typeof(a) _a = (a); \
  7483. typeof(b) _b = (b); \
  7484. _a > _b ? _a : _b @})
  7485. @end example
  7486. @code{typeof} is more flexible than @code{__auto_type}; however, the
  7487. principal use case for @code{typeof} is in variable declarations with
  7488. initialization, which is exactly what @code{__auto_type} handles.
  7489. @node Local Variables
  7490. @section Local Variables
  7491. @cindex local variables
  7492. @cindex variables, local
  7493. Declaring a variable inside a function definition (@pxref{Function
  7494. Definitions}) makes the variable name @dfn{local} to the containing
  7495. block---that is, the containing pair of braces. More precisely, the
  7496. variable's name is visible starting just after where it appears in the
  7497. declaration, and its visibility continues until the end of the block.
  7498. Local variables in C are generally @dfn{automatic} variables: each
  7499. variable's storage exists only from the declaration to the end of the
  7500. block. Execution of the declaration allocates the storage, computes
  7501. the initial value, and stores it in the variable. The end of the
  7502. block deallocates the storage.@footnote{Due to compiler optimizations,
  7503. allocation and deallocation don't necessarily really happen at
  7504. those times.}
  7505. @strong{Warning:} Two declarations for the same local variable
  7506. in the same scope are an error.
  7507. @strong{Warning:} Automatic variables are stored in the run-time stack.
  7508. The total space for the program's stack may be limited; therefore,
  7509. in using very large arrays, it may be necessary to allocate
  7510. them in some other way to stop the program from crashing.
  7511. @strong{Warning:} If the declaration of an automatic variable does not
  7512. specify an initial value, the variable starts out containing garbage.
  7513. In this example, the value printed could be anything at all:
  7514. @example
  7515. @{
  7516. int i;
  7517. printf ("Print junk %d\n", i);
  7518. @}
  7519. @end example
  7520. In a simple test program, that statement is likely to print 0, simply
  7521. because every process starts with memory zeroed. But don't rely on it
  7522. to be zero---that is erroneous.
  7523. @strong{Note:} Make sure to store a value into each local variable (by
  7524. assignment, or by initialization) before referring to its value.
  7525. @node File-Scope Variables
  7526. @section File-Scope Variables
  7527. @cindex file-scope variables
  7528. @cindex global variables
  7529. @cindex variables, file-scope
  7530. @cindex variables, global
  7531. A variable declaration at the top level in a file (not inside a
  7532. function definition) declares a @dfn{file-scope variable}. Loading a
  7533. program allocates the storage for all the file-scope variables in it,
  7534. and initializes them too.
  7535. Each file-scope variable is either @dfn{static} (limited to one
  7536. compilation module) or @dfn{global} (shared with all compilation
  7537. modules in the program). To make the variable static, write the
  7538. keyword @code{static} at the start of the declaration. Omitting
  7539. @code{static} makes the variable global.
  7540. The initial value for a file-scope variable can't depend on the
  7541. contents of storage, and can't call any functions.
  7542. @example
  7543. int foo = 5; /* @r{Valid.} */
  7544. int bar = foo; /* @r{Invalid!} */
  7545. int bar = sin (1.0); /* @r{Invalid!} */
  7546. @end example
  7547. But it can use the address of another file-scope variable:
  7548. @example
  7549. int foo;
  7550. int *bar = &foo; /* @r{Valid.} */
  7551. int arr[5];
  7552. int *bar3 = &arr[3]; /* @r{Valid.} */
  7553. int *bar4 = arr + 4; /* @r{Valid.} */
  7554. @end example
  7555. It is valid for a module to have multiple declarations for a
  7556. file-scope variable, as long as they are all global or all static, but
  7557. at most one declaration can specify an initial value for it.
  7558. @node Static Local Variables
  7559. @section Static Local Variables
  7560. @cindex static local variables
  7561. @cindex variables, static local
  7562. @findex static
  7563. The keyword @code{static} in a local variable declaration says to
  7564. allocate the storage for the variable permanently, just like a
  7565. file-scope variable, even if the declaration is within a function.
  7566. Here's an example:
  7567. @example
  7568. int
  7569. increment_counter ()
  7570. @{
  7571. static int counter = 0;
  7572. return ++counter;
  7573. @}
  7574. @end example
  7575. The scope of the name @code{counter} runs from the declaration to the
  7576. end of the containing block, just like an automatic local variable,
  7577. but its storage is permanent, so the value persists from one call to
  7578. the next. As a result, each call to @code{increment_counter}
  7579. returns a different, unique value.
  7580. The initial value of a static local variable has the same limitations
  7581. as for file-scope variables: it can't depend on the contents of
  7582. storage or call any functions. It can use the address of a file-scope
  7583. variable or a static local variable, because those addresses are
  7584. determined before the program runs.
  7585. @node Extern Declarations
  7586. @section @code{extern} Declarations
  7587. @cindex @code{extern} declarations
  7588. @cindex declarations, @code{extern}
  7589. @findex extern
  7590. An @code{extern} declaration is used to refer to a global variable
  7591. whose principal declaration comes elsewhere---in the same module, or in
  7592. another compilation module. It looks like this:
  7593. @example
  7594. extern @var{basetype} @var{decorated-variable};
  7595. @end example
  7596. Its meaning is that, in the current scope, the variable name refers to
  7597. the file-scope variable of that name---which needs to be declared in a
  7598. non-@code{extern}, non-@code{static} way somewhere else.
  7599. For instance, if one compilation module has this global variable
  7600. declaration
  7601. @example
  7602. int error_count = 0;
  7603. @end example
  7604. @noindent
  7605. then other compilation modules can specify this
  7606. @example
  7607. extern int error_count;
  7608. @end example
  7609. @noindent
  7610. to allow reference to the same variable.
  7611. The usual place to write an @code{extern} declaration is at top level
  7612. in a source file, but you can write an @code{extern} declaration
  7613. inside a block to make a global or static file-scope variable
  7614. accessible in that block.
  7615. Since an @code{extern} declaration does not allocate space for the
  7616. variable, it can omit the size of an array:
  7617. @example
  7618. extern int array[];
  7619. @end example
  7620. You can use @code{array} normally in all contexts where it is
  7621. converted automatically to a pointer. However, to use it as the
  7622. operand of @code{sizeof} is an error, since the size is unknown.
  7623. It is valid to have multiple @code{extern} declarations for the same
  7624. variable, even in the same scope, if they give the same type. They do
  7625. not conflict---they agree. For an array, it is legitimate for some
  7626. @code{extern} declarations can specify the size while others omit it.
  7627. However, if two declarations give different sizes, that is an error.
  7628. Likewise, you can use @code{extern} declarations at file scope
  7629. (@pxref{File-Scope Variables}) followed by an ordinary global
  7630. (non-static) declaration of the same variable. They do not conflict,
  7631. because they say compatible things about the same meaning of the variable.
  7632. @node Allocating File-Scope
  7633. @section Allocating File-Scope Variables
  7634. @cindex allocation file-scope variables
  7635. @cindex file-scope variables, allocating
  7636. Some file-scope declarations allocate space for the variable, and some
  7637. don't.
  7638. A file-scope declaration with an initial value @emph{must} allocate
  7639. space for the variable; if there are two of such declarations for the
  7640. same variable, even in different compilation modules, they conflict.
  7641. An @code{extern} declaration @emph{never} allocates space for the variable.
  7642. If all the top-level declarations of a certain variable are
  7643. @code{extern}, the variable never gets memory space. If that variable
  7644. is used anywhere in the program, the use will be reported as an error,
  7645. saying that the variable is not defined.
  7646. @cindex tentative definition
  7647. A file-scope declaration without an initial value is called a
  7648. @dfn{tentative definition}. This is a strange hybrid: it @emph{can}
  7649. allocate space for the variable, but does not insist. So it causes no
  7650. conflict, no error, if the variable has another declaration that
  7651. allocates space for it, perhaps in another compilation module. But if
  7652. nothing else allocates space for the variable, the tentative
  7653. definition will do it. Any number of compilation modules can declare
  7654. the same variable in this way, and that is sufficient for all of them
  7655. to use the variable.
  7656. @c @opindex -fno-common
  7657. @c @opindex --warn_common
  7658. In programs that are very large or have many contributors, it may be
  7659. wise to adopt the convention of never using tentative definitions.
  7660. You can use the compilation option @option{-fno-common} to make them
  7661. an error, or @option{--warn-common} to warn about them.
  7662. If a file-scope variable gets its space through a tentative
  7663. definition, it starts out containing all zeros.
  7664. @node auto and register
  7665. @section @code{auto} and @code{register}
  7666. @cindex @code{auto} declarations
  7667. @cindex @code{register} declarations
  7668. @findex auto
  7669. @findex register
  7670. For historical reasons, you can write @code{auto} or @code{register}
  7671. before a local variable declaration. @code{auto} merely emphasizes
  7672. that the variable isn't static; it changes nothing.
  7673. @code{register} suggests to the compiler storing this variable in a
  7674. register. However, GNU C ignores this suggestion, since it can
  7675. choose the best variables to store in registers without any hints.
  7676. It is an error to take the address of a variable declared
  7677. @code{register}, so you cannot use the unary @samp{&} operator on it.
  7678. If the variable is an array, you can't use it at all (other than as
  7679. the operand of @code{sizeof}), which makes it rather useless.
  7680. @node Omitting Types
  7681. @section Omitting Types in Declarations
  7682. @cindex omitting types in declarations
  7683. The syntax of C traditionally allows omitting the data type in a
  7684. declaration if it specifies a storage class, a type qualifier (see the
  7685. next chapter), or @code{auto} or @code{register}. Then the type
  7686. defaults to @code{int}. For example:
  7687. @example
  7688. auto foo = 42;
  7689. @end example
  7690. This is bad practice; if you see it, fix it.
  7691. @node Type Qualifiers
  7692. @chapter Type Qualifiers
  7693. A declaration can include type qualifiers to advise the compiler
  7694. about how the variable will be used. There are three different
  7695. qualifiers, @code{const}, @code{volatile} and @code{restrict}. They
  7696. pertain to different issues, so you can use more than one together.
  7697. For instance, @code{const volatile} describes a value that the
  7698. program is not allowed to change, but might have a different value
  7699. each time the program examines it. (This might perhaps be a special
  7700. hardware register, or part of shared memory.)
  7701. If you are just learning C, you can skip this chapter.
  7702. @menu
  7703. * const:: Variables whose values don't change.
  7704. * volatile:: Variables whose values may be accessed
  7705. or changed outside of the control of
  7706. this program.
  7707. * restrict Pointers:: Restricted pointers for code optimization.
  7708. * restrict Pointer Example:: Example of how that works.
  7709. @end menu
  7710. @node const
  7711. @section @code{const} Variables and Fields
  7712. @cindex @code{const} variables and fields
  7713. @cindex variables, @code{const}
  7714. @findex const
  7715. You can mark a variable as ``constant'' by writing @code{const} in
  7716. front of the declaration. This says to treat any assignment to that
  7717. variable as an error. It may also permit some compiler
  7718. optimizations---for instance, to fetch the value only once to satisfy
  7719. multiple references to it. The construct looks like this:
  7720. @example
  7721. const double pi = 3.14159;
  7722. @end example
  7723. After this definition, the code can use the variable @code{pi}
  7724. but cannot assign a different value to it.
  7725. @example
  7726. pi = 3.0; /* @r{Error!} */
  7727. @end example
  7728. Simple variables that are constant can be used for the same purposes
  7729. as enumeration constants, and they are not limited to integers. The
  7730. constantness of the variable propagates into pointers, too.
  7731. A pointer type can specify that the @emph{target} is constant. For
  7732. example, the pointer type @code{const double *} stands for a pointer
  7733. to a constant @code{double}. That's the type that results from taking
  7734. the address of @code{pi}. Such a pointer can't be dereferenced in the
  7735. left side of an assignment.
  7736. @example
  7737. *(&pi) = 3.0; /* @r{Error!} */
  7738. @end example
  7739. Nonconstant pointers can be converted automatically to constant
  7740. pointers, but not vice versa. For instance,
  7741. @example
  7742. const double *cptr;
  7743. double *ptr;
  7744. cptr = &pi; /* @r{Valid.} */
  7745. cptr = ptr; /* @r{Valid.} */
  7746. ptr = cptr; /* @r{Error!} */
  7747. ptr = &pi; /* @r{Error!} */
  7748. @end example
  7749. This is not an ironclad protection against modifying the value. You
  7750. can always cast the constant pointer to a nonconstant pointer type:
  7751. @example
  7752. ptr = (double *)cptr; /* @r{Valid.} */
  7753. ptr = (double *)&pi; /* @r{Valid.} */
  7754. @end example
  7755. However, @code{const} provides a way to show that a certain function
  7756. won't modify the data structure whose address is passed to it. Here's
  7757. an example:
  7758. @example
  7759. int
  7760. string_length (const char *string)
  7761. @{
  7762. int count = 0;
  7763. while (*string++)
  7764. count++;
  7765. return count;
  7766. @}
  7767. @end example
  7768. @noindent
  7769. Using @code{const char *} for the parameter is a way of saying this
  7770. function never modifies the memory of the string itself.
  7771. In calling @code{string_length}, you can specify an ordinary
  7772. @code{char *} since that can be converted automatically to @code{const
  7773. char *}.
  7774. @node volatile
  7775. @section @code{volatile} Variables and Fields
  7776. @cindex @code{volatile} variables and fields
  7777. @cindex variables, @code{volatile}
  7778. @findex volatile
  7779. The GNU C compiler often performs optimizations that eliminate the
  7780. need to write or read a variable. For instance,
  7781. @example
  7782. int foo;
  7783. foo = 1;
  7784. foo++;
  7785. @end example
  7786. @noindent
  7787. might simply store the value 2 into @code{foo}, without ever storing 1.
  7788. These optimizations can also apply to structure fields in some cases.
  7789. If the memory containing @code{foo} is shared with another program,
  7790. or if it is examined asynchronously by hardware, such optimizations
  7791. could confuse the communication. Using @code{volatile} is one way
  7792. to prevent them.
  7793. Writing @code{volatile} with the type in a variable or field declaration
  7794. says that the value may be examined or changed for reasons outside the
  7795. control of the program at any moment. Therefore, the program must
  7796. execute in a careful way to assure correct interaction with those
  7797. accesses, whenever they may occur.
  7798. The simplest use looks like this:
  7799. @example
  7800. volatile int lock;
  7801. @end example
  7802. This directs the compiler not to do certain common optimizations on
  7803. use of the variable @code{lock}. All the reads and writes for a volatile
  7804. variable or field are really done, and done in the order specified
  7805. by the source code. Thus, this code:
  7806. @example
  7807. lock = 1;
  7808. list = list->next;
  7809. if (lock)
  7810. lock_broken (&lock);
  7811. lock = 0;
  7812. @end example
  7813. @noindent
  7814. really stores the value 1 in @code{lock}, even though there is no
  7815. sign it is really used, and the @code{if} statement reads and
  7816. checks the value of @code{lock}, rather than assuming it is still 1.
  7817. A limited amount of optimization can be done, in principle, on
  7818. @code{volatile} variables and fields: multiple references between two
  7819. sequence points (@pxref{Sequence Points}) can be simplified together.
  7820. Use of @code{volatile} does not eliminate the flexibility in ordering
  7821. the computation of the operands of most operators. For instance, in
  7822. @code{lock + foo ()}, the order of accessing @code{lock} and calling
  7823. @code{foo} is not specified, so they may be done in either order; the
  7824. fact that @code{lock} is @code{volatile} has no effect on that.
  7825. @node restrict Pointers
  7826. @section @code{restrict}-Qualified Pointers
  7827. @cindex @code{restrict} pointers
  7828. @cindex pointers, @code{restrict}-qualified
  7829. @findex restrict
  7830. You can declare a pointer as ``restricted'' using the @code{restrict}
  7831. type qualifier, like this:
  7832. @example
  7833. int *restrict p = x;
  7834. @end example
  7835. @noindent
  7836. This enables better optimization of code that uses the pointer.
  7837. If @code{p} is declared with @code{restrict}, and then the code
  7838. references the object that @code{p} points to (using @code{*p} or
  7839. @code{p[@var{i}]}), the @code{restrict} declaration promises that the
  7840. code will not access that object in any other way---only through
  7841. @code{p}.
  7842. For instance, it means the code must not use another pointer
  7843. to access the same space, as shown here:
  7844. @example
  7845. int *restrict p = @var{whatever};
  7846. int *q = p;
  7847. foo (*p, *q);
  7848. @end example
  7849. @noindent
  7850. That contradicts the @code{restrict} promise by accessing the object
  7851. that @code{p} points to using @code{q}, which bypasses @code{p}.
  7852. Likewise, it must not do this:
  7853. @example
  7854. int *restrict p = @var{whatever};
  7855. struct @{ int *a, *b; @} s;
  7856. s.a = p;
  7857. foo (*p, *s.a);
  7858. @end example
  7859. @noindent
  7860. This example uses a structure field instead of the variable @code{q}
  7861. to hold the other pointer, and that contradicts the promise just the
  7862. same.
  7863. The keyword @code{restrict} also promises that @code{p} won't point to
  7864. the allocated space of any automatic or static variable. So the code
  7865. must not do this:
  7866. @example
  7867. int a;
  7868. int *restrict p = &a;
  7869. foo (*p, a);
  7870. @end example
  7871. @noindent
  7872. because that does direct access to the object (@code{a}) that @code{p}
  7873. points to, which bypasses @code{p}.
  7874. If the code makes such promises with @code{restrict} then breaks them,
  7875. execution is unpredictable.
  7876. @node restrict Pointer Example
  7877. @section @code{restrict} Pointer Example
  7878. Here are examples where @code{restrict} enables real optimization.
  7879. In this example, @code{restrict} assures GCC that the array @code{out}
  7880. points to does not overlap with the array @code{in} points to.
  7881. @example
  7882. void
  7883. process_data (const char *in,
  7884. char * restrict out,
  7885. size_t size)
  7886. @{
  7887. for (i = 0; i < size; i++)
  7888. out[i] = in[i] + in[i + 1];
  7889. @}
  7890. @end example
  7891. Here's a simple tree structure, where each tree node holds data of
  7892. type @code{PAYLOAD} plus two subtrees.
  7893. @example
  7894. struct foo
  7895. @{
  7896. PAYLOAD payload;
  7897. struct foo *left;
  7898. struct foo *right;
  7899. @};
  7900. @end example
  7901. Now here's a function to null out both pointers in the @code{left}
  7902. subtree.
  7903. @example
  7904. void
  7905. null_left (struct foo *a)
  7906. @{
  7907. a->left->left = NULL;
  7908. a->left->right = NULL;
  7909. @}
  7910. @end example
  7911. Since @code{*a} and @code{*a->left} have the same data type,
  7912. they could legitimately alias (@pxref{Aliasing}). Therefore,
  7913. the compiled code for @code{null_left} must read @code{a->left}
  7914. again from memory when executing the second assignment statement.
  7915. We can enable optimization, so that it does not need to read
  7916. @code{a->left} again, by writing @code{null_left} in a less
  7917. obvious way.
  7918. @example
  7919. void
  7920. null_left (struct foo *a)
  7921. @{
  7922. struct foo *b = a->left;
  7923. b->left = NULL;
  7924. b->right = NULL;
  7925. @}
  7926. @end example
  7927. A more elegant way to fix this is with @code{restrict}.
  7928. @example
  7929. void
  7930. null_left (struct foo *restrict a)
  7931. @{
  7932. a->left->left = NULL;
  7933. a->left->right = NULL;
  7934. @}
  7935. @end example
  7936. Declaring @code{a} as @code{restrict} asserts that other pointers such
  7937. as @code{a->left} will not point to the same memory space as @code{a}.
  7938. Therefore, the memory location @code{a->left->left} cannot be the same
  7939. memory as @code{a->left}. Knowing this, the compiled code may avoid
  7940. reloading @code{a->left} for the second statement.
  7941. @node Functions
  7942. @chapter Functions
  7943. @cindex functions
  7944. We have already presented many examples of functions, so if you've
  7945. read this far, you basically understand the concept of a function. It
  7946. is vital, nonetheless, to have a chapter in the manual that collects
  7947. all the information about functions.
  7948. @menu
  7949. * Function Definitions:: Writing the body of a function.
  7950. * Function Declarations:: Declaring the interface of a function.
  7951. * Function Calls:: Using functions.
  7952. * Function Call Semantics:: Call-by-value argument passing.
  7953. * Function Pointers:: Using references to functions.
  7954. * The main Function:: Where execution of a GNU C program begins.
  7955. * Advanced Definitions:: Advanced features of function definitions.
  7956. * Obsolete Definitions:: Obsolete features still used
  7957. in function definitions in old code.
  7958. @end menu
  7959. @node Function Definitions
  7960. @section Function Definitions
  7961. @cindex function definitions
  7962. @cindex defining functions
  7963. We have already presented many examples of function definitions. To
  7964. summarize the rules, a function definition looks like this:
  7965. @example
  7966. @var{returntype}
  7967. @var{functionname} (@var{parm_declarations}@r{@dots{}})
  7968. @{
  7969. @var{body}
  7970. @}
  7971. @end example
  7972. The part before the open-brace is called the @dfn{function header}.
  7973. Write @code{void} as the @var{returntype} if the function does
  7974. not return a value.
  7975. @menu
  7976. * Function Parameter Variables:: Syntax and semantics
  7977. of function parameters.
  7978. * Forward Function Declarations:: Functions can only be called after
  7979. they have been defined or declared.
  7980. * Static Functions:: Limiting visibility of a function.
  7981. * Arrays as Parameters:: Functions that accept array arguments.
  7982. * Structs as Parameters:: Functions that accept structure arguments.
  7983. @end menu
  7984. @node Function Parameter Variables
  7985. @subsection Function Parameter Variables
  7986. @cindex function parameter variables
  7987. @cindex parameter variables in functions
  7988. @cindex parameter list
  7989. A function parameter variable is a local variable (@pxref{Local
  7990. Variables}) used within the function to store the value passed as an
  7991. argument in a call to the function. Usually we say ``function
  7992. parameter'' or ``parameter'' for short, not mentioning the fact that
  7993. it's a variable.
  7994. We declare these variables in the beginning of the function
  7995. definition, in the @dfn{parameter list}. For example,
  7996. @example
  7997. fib (int n)
  7998. @end example
  7999. @noindent
  8000. has a parameter list with one function parameter @code{n}, which has
  8001. type @code{int}.
  8002. Function parameter declarations differ from ordinary variable
  8003. declarations in several ways:
  8004. @itemize @bullet
  8005. @item
  8006. Inside the function definition header, commas separate parameter
  8007. declarations, and each parameter needs a complete declaration
  8008. including the type. For instance, if a function @code{foo} has two
  8009. @code{int} parameters, write this:
  8010. @example
  8011. foo (int a, int b)
  8012. @end example
  8013. You can't share the common @code{int} between the two declarations:
  8014. @example
  8015. foo (int a, b) /* @r{Invalid!} */
  8016. @end example
  8017. @item
  8018. A function parameter variable is initialized to whatever value is
  8019. passed in the function call, so its declaration cannot specify an
  8020. initial value.
  8021. @item
  8022. Writing an array type in a function parameter declaration has the
  8023. effect of declaring it as a pointer. The size specified for the array
  8024. has no effect at all, and we normally omit the size. Thus,
  8025. @example
  8026. foo (int a[5])
  8027. foo (int a[])
  8028. foo (int *a)
  8029. @end example
  8030. @noindent
  8031. are equivalent.
  8032. @item
  8033. The scope of the parameter variables is the entire function body,
  8034. notwithstanding the fact that they are written in the function header,
  8035. which is just outside the function body.
  8036. @end itemize
  8037. If a function has no parameters, it would be most natural for the
  8038. list of parameters in its definition to be empty. But that, in C, has
  8039. a special meaning for historical reasons: ``Do not check that calls to
  8040. this function have the right number of arguments.'' Thus,
  8041. @example
  8042. int
  8043. foo ()
  8044. @{
  8045. return 5;
  8046. @}
  8047. int
  8048. bar (int x)
  8049. @{
  8050. return foo (x);
  8051. @}
  8052. @end example
  8053. @noindent
  8054. would not report a compilation error in passing @code{x} as an
  8055. argument to @code{foo}. By contrast,
  8056. @example
  8057. int
  8058. foo (void)
  8059. @{
  8060. return 5;
  8061. @}
  8062. int
  8063. bar (int x)
  8064. @{
  8065. return foo (x);
  8066. @}
  8067. @end example
  8068. @noindent
  8069. would report an error because @code{foo} is supposed to receive
  8070. no arguments.
  8071. @node Forward Function Declarations
  8072. @subsection Forward Function Declarations
  8073. @cindex forward function declarations
  8074. @cindex function declarations, forward
  8075. The order of the function definitions in the source code makes no
  8076. difference, except that each function needs to be defined or declared
  8077. before code uses it.
  8078. The definition of a function also declares its name for the rest of
  8079. the containing scope. But what if you want to call the function
  8080. before its definition? To permit that, write a compatible declaration
  8081. of the same function, before the first call. A declaration that
  8082. prefigures a subsequent definition in this way is called a
  8083. @dfn{forward declaration}. The function declaration can be at top
  8084. @c ??? file scope
  8085. level or within a block, and it applies until the end of the containing
  8086. scope.
  8087. @xref{Function Declarations}, for more information about these
  8088. declarations.
  8089. @node Static Functions
  8090. @subsection Static Functions
  8091. @cindex static functions
  8092. @cindex functions, static
  8093. @findex static
  8094. The keyword @code{static} in a function definition limits the
  8095. visibility of the name to the current compilation module. (That's the
  8096. same thing @code{static} does in variable declarations;
  8097. @pxref{File-Scope Variables}.) For instance, if one compilation module
  8098. contains this code:
  8099. @example
  8100. static int
  8101. foo (void)
  8102. @{
  8103. @r{@dots{}}
  8104. @}
  8105. @end example
  8106. @noindent
  8107. then the code of that compilation module can call @code{foo} anywhere
  8108. after the definition, but other compilation modules cannot refer to it
  8109. at all.
  8110. @cindex forward declaration
  8111. @cindex static function, declaration
  8112. To call @code{foo} before its definition, it needs a forward
  8113. declaration, which should use @code{static} since the function
  8114. definition does. For this function, it looks like this:
  8115. @example
  8116. static int foo (void);
  8117. @end example
  8118. It is generally wise to use @code{static} on the definitions of
  8119. functions that won't be called from outside the same compilation
  8120. module. This makes sure that calls are not added in other modules.
  8121. If programmers decide to change the function's calling convention, or
  8122. understand all the consequences of its use, they will only have to
  8123. check for calls in the same compilation module.
  8124. @node Arrays as Parameters
  8125. @subsection Arrays as Parameters
  8126. @cindex array as parameters
  8127. @cindex functions with array parameters
  8128. Arrays in C are not first-class objects: it is impossible to copy
  8129. them. So they cannot be passed as arguments like other values.
  8130. @xref{Limitations of C Arrays}. Rather, array parameters work in
  8131. a special way.
  8132. @menu
  8133. * Array Parm Pointer::
  8134. * Passing Array Args::
  8135. * Array Parm Qualifiers::
  8136. @end menu
  8137. @node Array Parm Pointer
  8138. @subsubsection Array parameters are pointers
  8139. Declaring a function parameter variable as an array really gives it a
  8140. pointer type. C does this because an expression with array type, if
  8141. used as an argument in a function call, is converted automatically to
  8142. a pointer (to the zeroth element of the array). If you declare the
  8143. corresponding parameter as an ``array'', it will work correctly with
  8144. the pointer value that really gets passed.
  8145. This relates to the fact that C does not check array bounds in access
  8146. to elements of the array (@pxref{Accessing Array Elements}).
  8147. For example, in this function,
  8148. @example
  8149. void
  8150. clobber4 (int array[20])
  8151. @{
  8152. array[4] = 0;
  8153. @}
  8154. @end example
  8155. @noindent
  8156. the parameter @code{array}'s real type is @code{int *}; the specified
  8157. length, 20, has no effect on the program. You can leave out the length
  8158. and write this:
  8159. @example
  8160. void
  8161. clobber4 (int array[])
  8162. @{
  8163. array[4] = 0;
  8164. @}
  8165. @end example
  8166. @noindent
  8167. or write the parameter declaration explicitly as a pointer:
  8168. @example
  8169. void
  8170. clobber4 (int *array)
  8171. @{
  8172. array[4] = 0;
  8173. @}
  8174. @end example
  8175. They are all equivalent.
  8176. @node Passing Array Args
  8177. @subsubsection Passing array arguments
  8178. The function call passes this pointer by
  8179. value, like all argument values in C@. However, the result is
  8180. paradoxical in that the array itself is passed by reference: its
  8181. contents are treated as shared memory---shared between the caller and
  8182. the called function, that is. When @code{clobber4} assigns to element
  8183. 4 of @code{array}, the effect is to alter element 4 of the array
  8184. specified in the call.
  8185. @example
  8186. #include <stddef.h> /* @r{Defines @code{NULL}.} */
  8187. #include <stdlib.h> /* @r{Declares @code{malloc},} */
  8188. /* @r{Defines @code{EXIT_SUCCESS}.} */
  8189. int
  8190. main (void)
  8191. @{
  8192. int data[] = @{1, 2, 3, 4, 5, 6@};
  8193. int i;
  8194. /* @r{Show the initial value of element 4.} */
  8195. for (i = 0; i < 6; i++)
  8196. printf ("data[%d] = %d\n", i, data[i]);
  8197. printf ("\n");
  8198. clobber4 (data);
  8199. /* @r{Show that element 4 has been changed.} */
  8200. for (i = 0; i < 6; i++)
  8201. printf ("data[%d] = %d\n", i, data[i]);
  8202. printf ("\n");
  8203. return EXIT_SUCCESS;
  8204. @}
  8205. @end example
  8206. @noindent
  8207. shows that @code{data[4]} has become zero after the call to
  8208. @code{clobber4}.
  8209. The array @code{data} has 6 elements, but passing it to a function
  8210. whose argument type is written as @code{int [20]} is not an error,
  8211. because that really stands for @code{int *}. The pointer that is the
  8212. real argument carries no indication of the length of the array it
  8213. points into. It is not required to point to the beginning of the
  8214. array, either. For instance,
  8215. @example
  8216. clobber4 (data+1);
  8217. @end example
  8218. @noindent
  8219. passes an ``array'' that starts at element 1 of @code{data}, and the
  8220. effect is to zero @code{data[5]} instead of @code{data[4]}.
  8221. If all calls to the function will provide an array of a particular
  8222. size, you can specify the size of the array to be @code{static}:
  8223. @example
  8224. void
  8225. clobber4 (int array[static 20])
  8226. @r{@dots{}}
  8227. @end example
  8228. @noindent
  8229. This is a promise to the compiler that the function will always be
  8230. called with an array of 20 elements, so that the compiler can optimize
  8231. code accordingly. If the code breaks this promise and calls the
  8232. function with, for example, a shorter array, unpredictable things may
  8233. happen.
  8234. @node Array Parm Qualifiers
  8235. @subsubsection Type qualifiers on array parameters
  8236. You can use the type qualifiers @code{const}, @code{restrict}, and
  8237. @code{volatile} with array parameters; for example:
  8238. @example
  8239. void
  8240. clobber4 (volatile int array[20])
  8241. @r{@dots{}}
  8242. @end example
  8243. @noindent
  8244. denotes that @code{array} is equivalent to a pointer to a volatile
  8245. @code{int}. Alternatively:
  8246. @example
  8247. void
  8248. clobber4 (int array[const 20])
  8249. @r{@dots{}}
  8250. @end example
  8251. @noindent
  8252. makes the array parameter equivalent to a constant pointer to an
  8253. @code{int}. If we want the @code{clobber4} function to succeed, it
  8254. would not make sense to write
  8255. @example
  8256. void
  8257. clobber4 (const int array[20])
  8258. @r{@dots{}}
  8259. @end example
  8260. @noindent
  8261. as this would tell the compiler that the parameter should point to an
  8262. array of constant @code{int} values, and then we would not be able to
  8263. store zeros in them.
  8264. In a function with multiple array parameters, you can use @code{restrict}
  8265. to tell the compiler that each array parameter passed in will be distinct:
  8266. @example
  8267. void
  8268. foo (int array1[restrict 10], int array2[restrict 10])
  8269. @r{@dots{}}
  8270. @end example
  8271. @noindent
  8272. Using @code{restrict} promises the compiler that callers will
  8273. not pass in the same array for more than one @code{restrict} array
  8274. parameter. Knowing this enables the compiler to perform better code
  8275. optimization. This is the same effect as using @code{restrict}
  8276. pointers (@pxref{restrict Pointers}), but makes it clear when reading
  8277. the code that an array of a specific size is expected.
  8278. @node Structs as Parameters
  8279. @subsection Functions That Accept Structure Arguments
  8280. Structures in GNU C are first-class objects, so using them as function
  8281. parameters and arguments works in the natural way. This function
  8282. @code{swapfoo} takes a @code{struct foo} with two fields as argument,
  8283. and returns a structure of the same type but with the fields
  8284. exchanged.
  8285. @example
  8286. struct foo @{ int a, b; @};
  8287. struct foo x;
  8288. struct foo
  8289. swapfoo (struct foo inval)
  8290. @{
  8291. struct foo outval;
  8292. outval.a = inval.b;
  8293. outval.b = inval.a;
  8294. return outval;
  8295. @}
  8296. @end example
  8297. This simpler definition of @code{swapfoo} avoids using a local
  8298. variable to hold the result about to be return, by using a structure
  8299. constructor (@pxref{Structure Constructors}), like this:
  8300. @example
  8301. struct foo
  8302. swapfoo (struct foo inval)
  8303. @{
  8304. return (struct foo) @{ inval.b, inval.a @};
  8305. @}
  8306. @end example
  8307. It is valid to define a structure type in a function's parameter list,
  8308. as in
  8309. @example
  8310. int
  8311. frob_bar (struct bar @{ int a, b; @} inval)
  8312. @{
  8313. @var{body}
  8314. @}
  8315. @end example
  8316. @noindent
  8317. and @var{body} can access the fields of @var{inval} since the
  8318. structure type @code{struct bar} is defined for the whole function
  8319. body. However, there is no way to create a @code{struct bar} argument
  8320. to pass to @code{frob_bar}, except with kludges. As a result,
  8321. defining a structure type in a parameter list is useless in practice.
  8322. @node Function Declarations
  8323. @section Function Declarations
  8324. @cindex function declarations
  8325. @cindex declararing functions
  8326. To call a function, or use its name as a pointer, a @dfn{function
  8327. declaration} for the function name must be in effect at that point in
  8328. the code. The function's definition serves as a declaration of that
  8329. function for the rest of the containing scope, but to use the function
  8330. in code before the definition, or from another compilation module, a
  8331. separate function declaration must precede the use.
  8332. A function declaration looks like the start of a function definition.
  8333. It begins with the return value type (@code{void} if none) and the
  8334. function name, followed by argument declarations in parentheses
  8335. (though these can sometimes be omitted). But that's as far as the
  8336. similarity goes: instead of the function body, the declaration uses a
  8337. semicolon.
  8338. @cindex function prototype
  8339. @cindex prototype of a function
  8340. A declaration that specifies argument types is called a @dfn{function
  8341. prototype}. You can include the argument names or omit them. The
  8342. names, if included in the declaration, have no effect, but they may
  8343. serve as documentation.
  8344. This form of prototype specifies fixed argument types:
  8345. @example
  8346. @var{rettype} @var{function} (@var{argtypes}@r{@dots{}});
  8347. @end example
  8348. @noindent
  8349. This form says the function takes no arguments:
  8350. @example
  8351. @var{rettype} @var{function} (void);
  8352. @end example
  8353. @noindent
  8354. This form declares types for some arguments, and allows additional
  8355. arguments whose types are not specified:
  8356. @example
  8357. @var{rettype} @var{function} (@var{argtypes}@r{@dots{}}, ...);
  8358. @end example
  8359. For a parameter that's an array of variable length, you can write
  8360. its declaration with @samp{*} where the ``length'' of the array would
  8361. normally go; for example, these are all equivalent.
  8362. @example
  8363. double maximum (int n, int m, double a[n][m]);
  8364. double maximum (int n, int m, double a[*][*]);
  8365. double maximum (int n, int m, double a[ ][*]);
  8366. double maximum (int n, int m, double a[ ][m]);
  8367. @end example
  8368. @noindent
  8369. The old-fashioned form of declaration, which is not a prototype, says
  8370. nothing about the types of arguments or how many they should be:
  8371. @example
  8372. @var{rettype} @var{function} ();
  8373. @end example
  8374. @strong{Warning:} Arguments passed to a function declared without a
  8375. prototype are converted with the default argument promotions
  8376. (@pxref{Argument Promotions}. Likewise for additional arguments whose
  8377. types are unspecified.
  8378. Function declarations are usually written at the top level in a source file,
  8379. but you can also put them inside code blocks. Then the function name
  8380. is visible for the rest of the containing scope. For example:
  8381. @example
  8382. void
  8383. foo (char *file_name)
  8384. @{
  8385. void save_file (char *);
  8386. save_file (file_name);
  8387. @}
  8388. @end example
  8389. If another part of the code tries to call the function
  8390. @code{save_file}, this declaration won't be in effect there. So the
  8391. function will get an implicit declaration of the form @code{extern int
  8392. save_file ();}. That conflicts with the explicit declaration
  8393. here, and the discrepancy generates a warning.
  8394. The syntax of C traditionally allows omitting the data type in a
  8395. function declaration if it specifies a storage class or a qualifier.
  8396. Then the type defaults to @code{int}. For example:
  8397. @example
  8398. static foo (double x);
  8399. @end example
  8400. @noindent
  8401. defaults the return type to @code{int}.
  8402. This is bad practice; if you see it, fix it.
  8403. Calling a function that is undeclared has the effect of an creating
  8404. @dfn{implicit} declaration in the innermost containing scope,
  8405. equivalent to this:
  8406. @example
  8407. extern int @dfn{function} ();
  8408. @end example
  8409. @noindent
  8410. This declaration says that the function returns @code{int} but leaves
  8411. its argument types unspecified. If that does not accurately fit the
  8412. function, then the program @strong{needs} an explicit declaration of
  8413. the function with argument types in order to call it correctly.
  8414. Implicit declarations are deprecated, and a function call that creates one
  8415. causes a warning.
  8416. @node Function Calls
  8417. @section Function Calls
  8418. @cindex function calls
  8419. @cindex calling functions
  8420. Starting a program automatically calls the function named @code{main}
  8421. (@pxref{The main Function}). Aside from that, a function does nothing
  8422. except when it is @dfn{called}. That occurs during the execution of a
  8423. function-call expression specifying that function.
  8424. A function-call expression looks like this:
  8425. @example
  8426. @var{function} (@var{arguments}@r{@dots{}})
  8427. @end example
  8428. Most of the time, @var{function} is a function name. However, it can
  8429. also be an expression with a function pointer value; that way, the
  8430. program can determine at run time which function to call.
  8431. The @var{arguments} are a series of expressions separated by commas.
  8432. Each expression specifies one argument to pass to the function.
  8433. The list of arguments in a function call looks just like use of the
  8434. comma operator (@pxref{Comma Operator}), but the fact that it fills
  8435. the parentheses of a function call gives it a different meaning.
  8436. Here's an example of a function call, taken from an example near the
  8437. beginning (@pxref{Complete Program}).
  8438. @example
  8439. printf ("Fibonacci series item %d is %d\n",
  8440. 19, fib (19));
  8441. @end example
  8442. The three arguments given to @code{printf} are a constant string, the
  8443. integer 19, and the integer returned by @code{fib (19)}.
  8444. @node Function Call Semantics
  8445. @section Function Call Semantics
  8446. @cindex function call semantics
  8447. @cindex semantics of function calls
  8448. @cindex call-by-value
  8449. The meaning of a function call is to compute the specified argument
  8450. expressions, convert their values according to the function's
  8451. declaration, then run the function giving it copies of the converted
  8452. values. (This method of argument passing is known as
  8453. @dfn{call-by-value}.) When the function finishes, the value it
  8454. returns becomes the value of the function-call expression.
  8455. Call-by-value implies that an assignment to the function argument
  8456. variable has no direct effect on the caller. For instance,
  8457. @example
  8458. #include <stdlib.h> /* @r{Defines @code{EXIT_SUCCESS}.} */
  8459. #include <stdio.h> /* @r{Declares @code{printf}.} */
  8460. void
  8461. subroutine (int x)
  8462. @{
  8463. x = 5;
  8464. @}
  8465. void
  8466. main (void)
  8467. @{
  8468. int y = 20;
  8469. subroutine (y);
  8470. printf ("y is %d\n", y);
  8471. return EXIT_SUCCESS;
  8472. @}
  8473. @end example
  8474. @noindent
  8475. prints @samp{y is 20}. Calling @code{subroutine} initializes @code{x}
  8476. from the value of @code{y}, but this does not establish any other
  8477. relationship between the two variables. Thus, the assignment to
  8478. @code{x}, inside @code{subroutine}, changes only @emph{that} @code{x}.
  8479. If an argument's type is specified by the function's declaration, the
  8480. function call converts the argument expression to that type if
  8481. possible. If the conversion is impossible, that is an error.
  8482. If the function's declaration doesn't specify the type of that
  8483. argument, then the @emph{default argument promotions} apply.
  8484. @xref{Argument Promotions}.
  8485. @node Function Pointers
  8486. @section Function Pointers
  8487. @cindex function pointers
  8488. @cindex pointers to functions
  8489. A function name refers to a fixed function. Sometimes it is useful to
  8490. call a function to be determined at run time; to do this, you can use
  8491. a @dfn{function pointer value} that points to the chosen function
  8492. (@pxref{Pointers}).
  8493. Pointer-to-function types can be used to declare variables and other
  8494. data, including array elements, structure fields, and union
  8495. alternatives. They can also be used for function arguments and return
  8496. values. These types have the peculiarity that they are never
  8497. converted automatically to @code{void *} or vice versa. However, you
  8498. can do that conversion with a cast.
  8499. @menu
  8500. * Declaring Function Pointers:: How to declare a pointer to a function.
  8501. * Assigning Function Pointers:: How to assign values to function pointers.
  8502. * Calling Function Pointers:: How to call functions through pointers.
  8503. @end menu
  8504. @node Declaring Function Pointers
  8505. @subsection Declaring Function Pointers
  8506. @cindex declaring function pointers
  8507. @cindex function pointers, declaring
  8508. The declaration of a function pointer variable (or structure field)
  8509. looks almost like a function declaration, except it has an additional
  8510. @samp{*} just before the variable name. Proper nesting requires a
  8511. pair of parentheses around the two of them. For instance, @code{int
  8512. (*a) ();} says, ``Declare @code{a} as a pointer such that @code{*a} is
  8513. an @code{int}-returning function.''
  8514. Contrast these three declarations:
  8515. @example
  8516. /* @r{Declare a function returning @code{char *}.} */
  8517. char *a (char *);
  8518. /* @r{Declare a pointer to a function returning @code{char}.} */
  8519. char (*a) (char *);
  8520. /* @r{Declare a pointer to a function returning @code{char *}.} */
  8521. char *(*a) (char *);
  8522. @end example
  8523. The possible argument types of the function pointed to are the same
  8524. as in a function declaration. You can write a prototype
  8525. that specifies all the argument types:
  8526. @example
  8527. @var{rettype} (*@var{function}) (@var{arguments}@r{@dots{}});
  8528. @end example
  8529. @noindent
  8530. or one that specifies some and leaves the rest unspecified:
  8531. @example
  8532. @var{rettype} (*@var{function}) (@var{arguments}@r{@dots{}}, ...);
  8533. @end example
  8534. @noindent
  8535. or one that says there are no arguments:
  8536. @example
  8537. @var{rettype} (*@var{function}) (void);
  8538. @end example
  8539. You can also write a non-prototype declaration that says
  8540. nothing about the argument types:
  8541. @example
  8542. @var{rettype} (*@var{function}) ();
  8543. @end example
  8544. For example, here's a declaration for a variable that should
  8545. point to some arithmetic function that operates on two @code{double}s:
  8546. @example
  8547. double (*binary_op) (double, double);
  8548. @end example
  8549. Structure fields, union alternatives, and array elements can be
  8550. function pointers; so can parameter variables. The function pointer
  8551. declaration construct can also be combined with other operators
  8552. allowed in declarations. For instance,
  8553. @example
  8554. int **(*foo)();
  8555. @end example
  8556. @noindent
  8557. declares @code{foo} as a pointer to a function that returns
  8558. type @code{int **}, and
  8559. @example
  8560. int **(*foo[30])();
  8561. @end example
  8562. @noindent
  8563. declares @code{foo} as an array of 30 pointers to functions that
  8564. return type @code{int **}.
  8565. @example
  8566. int **(**foo)();
  8567. @end example
  8568. @noindent
  8569. declares @code{foo} as a pointer to a pointer to a function that
  8570. returns type @code{int **}.
  8571. @node Assigning Function Pointers
  8572. @subsection Assigning Function Pointers
  8573. @cindex assigning function pointers
  8574. @cindex function pointers, assigning
  8575. Assuming we have declared the variable @code{binary_op} as in the
  8576. previous section, giving it a value requires a suitable function to
  8577. use. So let's define a function suitable for the variable to point
  8578. to. Here's one:
  8579. @example
  8580. double
  8581. double_add (double a, double b)
  8582. @{
  8583. return a+b;
  8584. @}
  8585. @end example
  8586. Now we can give it a value:
  8587. @example
  8588. binary_op = double_add;
  8589. @end example
  8590. The target type of the function pointer must be upward compatible with
  8591. the type of the function (@pxref{Compatible Types}).
  8592. There is no need for @samp{&} in front of @code{double_add}.
  8593. Using a function name such as @code{double_add} as an expression
  8594. automatically converts it to the function's address, with the
  8595. appropriate function pointer type. However, it is ok to use
  8596. @samp{&} if you feel that is clearer:
  8597. @example
  8598. binary_op = &double_add;
  8599. @end example
  8600. @node Calling Function Pointers
  8601. @subsection Calling Function Pointers
  8602. @cindex calling function pointers
  8603. @cindex function pointers, calling
  8604. To call the function specified by a function pointer, just write the
  8605. function pointer value in a function call. For instance, here's a
  8606. call to the function @code{binary_op} points to:
  8607. @example
  8608. binary_op (x, 5)
  8609. @end example
  8610. Since the data type of @code{binary_op} explicitly specifies type
  8611. @code{double} for the arguments, the call converts @code{x} and 5 to
  8612. @code{double}.
  8613. The call conceptually dereferences the pointer @code{binary_op} to
  8614. ``get'' the function it points to, and calls that function. If you
  8615. wish, you can explicitly represent the dereference by writing the
  8616. @code{*} operator:
  8617. @example
  8618. (*binary_op) (x, 5)
  8619. @end example
  8620. The @samp{*} reminds people reading the code that @code{binary_op} is
  8621. a function pointer rather than the name of a specific function.
  8622. @node The main Function
  8623. @section The @code{main} Function
  8624. @cindex @code{main} function
  8625. @findex main
  8626. Every complete executable program requires at least one function,
  8627. called @code{main}, which is where execution begins. You do not have
  8628. to explicitly declare @code{main}, though GNU C permits you to do so.
  8629. Conventionally, @code{main} should be defined to follow one of these
  8630. calling conventions:
  8631. @example
  8632. int main (void) @{@r{@dots{}}@}
  8633. int main (int argc, char *argv[]) @{@r{@dots{}}@}
  8634. int main (int argc, char *argv[], char *envp[]) @{@r{@dots{}}@}
  8635. @end example
  8636. @noindent
  8637. Using @code{void} as the parameter list means that @code{main} does
  8638. not use the arguments. You can write @code{char **argv} instead of
  8639. @code{char *argv[]}, and likewise for @code{envp}, as the two
  8640. constructs are equivalent.
  8641. @ignore @c Not so at present
  8642. Defining @code{main} in any other way generates a warning. Your
  8643. program will still compile, but you may get unexpected results when
  8644. executing it.
  8645. @end ignore
  8646. You can call @code{main} from C code, as you can call any other
  8647. function, though that is an unusual thing to do. When you do that,
  8648. you must write the call to pass arguments that match the parameters in
  8649. the definition of @code{main}.
  8650. The @code{main} function is not actually the first code that runs when
  8651. a program starts. In fact, the first code that runs is system code
  8652. from the file @file{crt0.o}. In Unix, this was hand-written assembler
  8653. code, but in GNU we replaced it with C code. Its job is to find
  8654. the arguments for @code{main} and call that.
  8655. @menu
  8656. * Values from main:: Returning values from the main function.
  8657. * Command-line Parameters:: Accessing command-line parameters
  8658. provided to the program.
  8659. * Environment Variables:: Accessing system environment variables.
  8660. @end menu
  8661. @node Values from main
  8662. @subsection Returning Values from @code{main}
  8663. @cindex returning values from @code{main}
  8664. @cindex success
  8665. @cindex failure
  8666. @cindex exit status
  8667. When @code{main} returns, the process terminates. Whatever value
  8668. @code{main} returns becomes the exit status which is reported to the
  8669. parent process. While nominally the return value is of type
  8670. @code{int}, in fact the exit status gets truncated to eight bits; if
  8671. @code{main} returns the value 256, the exit status is 0.
  8672. Normally, programs return only one of two values: 0 for success,
  8673. and 1 for failure. For maximum portability, use the macro
  8674. values @code{EXIT_SUCCESS} and @code{EXIT_FAILURE} defined in
  8675. @code{stdlib.h}. Here's an example:
  8676. @cindex @code{EXIT_FAILURE}
  8677. @cindex @code{EXIT_SUCCESS}
  8678. @example
  8679. #include <stdlib.h> /* @r{Defines @code{EXIT_SUCCESS}} */
  8680. /* @r{and @code{EXIT_FAILURE}.} */
  8681. int
  8682. main (void)
  8683. @{
  8684. @r{@dots{}}
  8685. if (foo)
  8686. return EXIT_SUCCESS;
  8687. else
  8688. return EXIT_FAILURE;
  8689. @}
  8690. @end example
  8691. Some types of programs maintain special conventions for various return
  8692. values; for example, comparison programs including @code{cmp} and
  8693. @code{diff} return 1 to indicate a mismatch, and 2 to indicate that
  8694. the comparison couldn't be performed.
  8695. @node Command-line Parameters
  8696. @subsection Accessing Command-line Parameters
  8697. @cindex command-line parameters
  8698. @cindex parameters, command-line
  8699. If the program was invoked with any command-line arguments, it can
  8700. access them through the arguments of @code{main}, @code{argc} and
  8701. @code{argv}. (You can give these arguments any names, but the names
  8702. @code{argc} and @code{argv} are customary.)
  8703. The value of @code{argv} is an array containing all of the
  8704. command-line arguments as strings, with the name of the command
  8705. invoked as the first string. @code{argc} is an integer that says how
  8706. many strings @code{argv} contains. Here is an example of accessing
  8707. the command-line parameters, retrieving the program's name and
  8708. checking for the standard @option{--version} and @option{--help} options:
  8709. @example
  8710. #include <string.h> /* @r{Declare @code{strcmp}.} */
  8711. int
  8712. main (int argc, char *argv[])
  8713. @{
  8714. char *program_name = argv[0];
  8715. for (int i = 1; i < argc; i++)
  8716. @{
  8717. if (!strcmp (argv[i], "--version"))
  8718. @{
  8719. /* @r{Print version information and exit.} */
  8720. @r{@dots{}}
  8721. @}
  8722. else if (!strcmp (argv[i], "--help"))
  8723. @{
  8724. /* @r{Print help information and exit.} */
  8725. @r{@dots{}}
  8726. @}
  8727. @}
  8728. @r{@dots{}}
  8729. @}
  8730. @end example
  8731. @node Environment Variables
  8732. @subsection Accessing Environment Variables
  8733. @cindex environment variables
  8734. You can optionally include a third parameter to @code{main}, another
  8735. array of strings, to capture the environment variables available to
  8736. the program. Unlike what happens with @code{argv}, there is no
  8737. additional parameter for the count of environment variables; rather,
  8738. the array of environment variables concludes with a null pointer.
  8739. @example
  8740. #include <stdio.h> /* @r{Declares @code{printf}.} */
  8741. int
  8742. main (int argc, char *argv[], char *envp[])
  8743. @{
  8744. /* @r{Print out all environment variables.} */
  8745. int i = 0;
  8746. while (envp[i])
  8747. @{
  8748. printf ("%s\n", envp[i]);
  8749. i++;
  8750. @}
  8751. @}
  8752. @end example
  8753. Another method of retrieving environment variables is to use the
  8754. library function @code{getenv}, which is defined in @code{stdlib.h}.
  8755. Using @code{getenv} does not require defining @code{main} to accept the
  8756. @code{envp} pointer. For example, here is a program that fetches and prints
  8757. the user's home directory (if defined):
  8758. @example
  8759. #include <stdlib.h> /* @r{Declares @code{getenv}.} */
  8760. #include <stdio.h> /* @r{Declares @code{printf}.} */
  8761. int
  8762. main (void)
  8763. @{
  8764. char *home_directory = getenv ("HOME");
  8765. if (home_directory)
  8766. printf ("My home directory is: %s\n", home_directory);
  8767. else
  8768. printf ("My home directory is not defined!\n");
  8769. @}
  8770. @end example
  8771. @node Advanced Definitions
  8772. @section Advanced Function Features
  8773. This section describes some advanced or obscure features for GNU C
  8774. function definitions. If you are just learning C, you can skip the
  8775. rest of this chapter.
  8776. @menu
  8777. * Variable-Length Array Parameters:: Functions that accept arrays
  8778. of variable length.
  8779. * Variable Number of Arguments:: Variadic functions.
  8780. * Nested Functions:: Defining functions within functions.
  8781. * Inline Function Definitions:: A function call optimization technique.
  8782. @end menu
  8783. @node Variable-Length Array Parameters
  8784. @subsection Variable-Length Array Parameters
  8785. @cindex variable-length array parameters
  8786. @cindex array parameters, variable-length
  8787. @cindex functions that accept variable-length arrays
  8788. An array parameter can have variable length: simply declare the array
  8789. type with a size that isn't constant. In a nested function, the
  8790. length can refer to a variable defined in a containing scope. In any
  8791. function, it can refer to a previous parameter, like this:
  8792. @example
  8793. struct entry
  8794. tester (int len, char data[len][len])
  8795. @{
  8796. @r{@dots{}}
  8797. @}
  8798. @end example
  8799. Alternatively, in function declarations (but not in function
  8800. definitions), you can use @code{[*]} to denote that the array
  8801. parameter is of a variable length, such that these two declarations
  8802. mean the same thing:
  8803. @example
  8804. struct entry
  8805. tester (int len, char data[len][len]);
  8806. @end example
  8807. @example
  8808. struct entry
  8809. tester (int len, char data[*][*]);
  8810. @end example
  8811. @noindent
  8812. The two forms of input are equivalent in GNU C, but emphasizing that
  8813. the array parameter is variable-length may be helpful to those
  8814. studying the code.
  8815. You can also omit the length parameter, and instead use some other
  8816. in-scope variable for the length in the function definition:
  8817. @example
  8818. struct entry
  8819. tester (char data[*][*]);
  8820. @r{@dots{}}
  8821. int dataLength = 20;
  8822. @r{@dots{}}
  8823. struct entry
  8824. tester (char data[dataLength][dataLength])
  8825. @{
  8826. @r{@dots{}}
  8827. @}
  8828. @end example
  8829. @c ??? check text above
  8830. @cindex parameter forward declaration
  8831. In GNU C, to pass the array first and the length afterward, you can
  8832. use a @dfn{parameter forward declaration}, like this:
  8833. @example
  8834. struct entry
  8835. tester (int len; char data[len][len], int len)
  8836. @{
  8837. @r{@dots{}}
  8838. @}
  8839. @end example
  8840. The @samp{int len} before the semicolon is the parameter forward
  8841. declaration; it serves the purpose of making the name @code{len} known
  8842. when the declaration of @code{data} is parsed.
  8843. You can write any number of such parameter forward declarations in the
  8844. parameter list. They can be separated by commas or semicolons, but
  8845. the last one must end with a semicolon, which is followed by the
  8846. ``real'' parameter declarations. Each forward declaration must match
  8847. a subsequent ``real'' declaration in parameter name and data type.
  8848. Standard C does not support parameter forward declarations.
  8849. @node Variable Number of Arguments
  8850. @subsection Variable-Length Parameter Lists
  8851. @cindex variable-length parameter lists
  8852. @cindex parameters lists, variable length
  8853. @cindex function parameter lists, variable length
  8854. @cindex variadic function
  8855. A function that takes a variable number of arguments is called a
  8856. @dfn{variadic function}. In C, a variadic function must specify at
  8857. least one fixed argument with an explicitly declared data type.
  8858. Additional arguments can follow, and can vary in both quantity and
  8859. data type.
  8860. In the function header, declare the fixed parameters in the normal
  8861. way, then write a comma and an ellipsis: @samp{, ...}. Here is an
  8862. example of a variadic function header:
  8863. @example
  8864. int add_multiple_values (int number, ...)
  8865. @end example
  8866. @cindex @code{va_list}
  8867. @cindex @code{va_start}
  8868. @cindex @code{va_end}
  8869. The function body can refer to fixed arguments by their parameter
  8870. names, but the additional arguments have no names. Accessing them in
  8871. the function body uses certain standard macros. They are defined in
  8872. the library header file @file{stdarg.h}, so the code must
  8873. @code{#include} that file.
  8874. In the body, write
  8875. @example
  8876. va_list ap;
  8877. va_start (ap, @var{last_fixed_parameter});
  8878. @end example
  8879. @noindent
  8880. This declares the variable @code{ap} (you can use any name for it)
  8881. and then sets it up to point before the first additional argument.
  8882. Then, to fetch the next consecutive additional argument, write this:
  8883. @example
  8884. va_arg (ap, @var{type})
  8885. @end example
  8886. After fetching all the additional arguments (or as many as need to be
  8887. used), write this:
  8888. @example
  8889. va_end (ap);
  8890. @end example
  8891. Here's an example of a variadic function definition that adds any
  8892. number of @code{int} arguments. The first (fixed) argument says how
  8893. many more arguments follow.
  8894. @example
  8895. #include <stdarg.h> /* @r{Defines @code{va}@r{@dots{}} macros.} */
  8896. @r{@dots{}}
  8897. int
  8898. add_multiple_values (int argcount, ...)
  8899. @{
  8900. int counter, total = 0;
  8901. /* @r{Declare a variable of type @code{va_list}.} */
  8902. va_list argptr;
  8903. /* @r{Initialize that variable..} */
  8904. va_start (argptr, argcount);
  8905. for (counter = 0; counter < argcount; counter++)
  8906. @{
  8907. /* @r{Get the next additional argument.} */
  8908. total += va_arg (argptr, int);
  8909. @}
  8910. /* @r{End use of the @code{argptr} variable.} */
  8911. va_end (argptr);
  8912. return total;
  8913. @}
  8914. @end example
  8915. With GNU C, @code{va_end} is superfluous, but some other compilers
  8916. might make @code{va_start} allocate memory so that calling
  8917. @code{va_end} is necessary to avoid a memory leak. Before doing
  8918. @code{va_start} again with the same variable, do @code{va_end}
  8919. first.
  8920. @cindex @code{va_copy}
  8921. Because of this possible memory allocation, it is risky (in principle)
  8922. to copy one @code{va_list} variable to another with assignment.
  8923. Instead, use @code{va_copy}, which copies the substance but allocates
  8924. separate memory in the variable you copy to. The call looks like
  8925. @code{va_copy (@var{to}, @var{from})}, where both @var{to} and
  8926. @var{from} should be variables of type @code{va_list}. In principle,
  8927. do @code{va_end} on each of these variables before its scope ends.
  8928. Since the additional arguments' types are not specified in the
  8929. function's definition, the default argument promotions
  8930. (@pxref{Argument Promotions}) apply to them in function calls. The
  8931. function definition must take account of this; thus, if an argument
  8932. was passed as @code{short}, the function should get it as @code{int}.
  8933. If an argument was passed as @code{float}, the function should get it
  8934. as @code{double}.
  8935. C has no mechanism to tell the variadic function how many arguments
  8936. were passed to it, so its calling convention must give it a way to
  8937. determine this. That's why @code{add_multiple_values} takes a fixed
  8938. argument that says how many more arguments follow. Thus, you can
  8939. call the function like this:
  8940. @example
  8941. sum = add_multiple_values (3, 12, 34, 190);
  8942. /* @r{Value is 12+34+190.} */
  8943. @end example
  8944. In GNU C, there is no actual need to use the @code{va_end} function.
  8945. In fact, it does nothing. It's used for compatibility with other
  8946. compilers, when that matters.
  8947. It is a mistake to access variables declared as @code{va_list} except
  8948. in the specific ways described here. Just what that type consists of
  8949. is an implementation detail, which could vary from one platform to
  8950. another.
  8951. @node Nested Functions
  8952. @subsection Nested Functions
  8953. @cindex nested functions
  8954. @cindex functions, nested
  8955. @cindex downward funargs
  8956. @cindex thunks
  8957. A @dfn{nested function} is a function defined inside another function.
  8958. (The ability to do this indispensable for automatic translation of
  8959. certain programming languages into C.) The nested function's name is
  8960. local to the block where it is defined. For example, here we define a
  8961. nested function named @code{square}, then call it twice:
  8962. @example
  8963. @group
  8964. foo (double a, double b)
  8965. @{
  8966. double square (double z) @{ return z * z; @}
  8967. return square (a) + square (b);
  8968. @}
  8969. @end group
  8970. @end example
  8971. The nested function definition can access all the variables of the containing
  8972. function that are visible at the point of its definition. This is
  8973. called @dfn{lexical scoping}. For example, here we show a nested
  8974. function that uses an inherited variable named @code{offset}:
  8975. @example
  8976. @group
  8977. bar (int *array, int offset, int size)
  8978. @{
  8979. int access (int *array, int index)
  8980. @{ return array[index + offset]; @}
  8981. int i;
  8982. @r{@dots{}}
  8983. for (i = 0; i < size; i++)
  8984. @r{@dots{}} access (array, i) @r{@dots{}}
  8985. @}
  8986. @end group
  8987. @end example
  8988. Nested function definitions can appear wherever automatic variable
  8989. declarations are allowed; that is, in any block, interspersed with the
  8990. other declarations and statements in the block.
  8991. The nested function's name is visible only within the parent block;
  8992. the name's scope starts from its definition and continues to the end
  8993. of the containing block. If the nested function's name
  8994. is the same as the parent function's name, there will be
  8995. no way to refer to the parent function inside the scope of the
  8996. name of the nested function.
  8997. Using @code{extern} or @code{static} on a nested function definition
  8998. is an error.
  8999. It is possible to call the nested function from outside the scope of its
  9000. name by storing its address or passing the address to another function.
  9001. You can do this safely, but you must be careful:
  9002. @example
  9003. @group
  9004. hack (int *array, int size, int addition)
  9005. @{
  9006. void store (int index, int value)
  9007. @{ array[index] = value + addition; @}
  9008. intermediate (store, size);
  9009. @}
  9010. @end group
  9011. @end example
  9012. Here, the function @code{intermediate} receives the address of
  9013. @code{store} as an argument. If @code{intermediate} calls @code{store},
  9014. the arguments given to @code{store} are used to store into @code{array}.
  9015. @code{store} also accesses @code{hack}'s local variable @code{addition}.
  9016. It is safe for @code{intermediate} to call @code{store} because
  9017. @code{hack}'s stack frame, with its arguments and local variables,
  9018. continues to exist during the call to @code{intermediate}.
  9019. Calling the nested function through its address after the containing
  9020. function has exited is asking for trouble. If it is called after a
  9021. containing scope level has exited, and if it refers to some of the
  9022. variables that are no longer in scope, it will refer to memory
  9023. containing junk or other data. It's not wise to take the risk.
  9024. The GNU C Compiler implements taking the address of a nested function
  9025. using a technique called @dfn{trampolines}. This technique was
  9026. described in @cite{Lexical Closures for C@t{++}} (Thomas M. Breuel,
  9027. USENIX C@t{++} Conference Proceedings, October 17--21, 1988).
  9028. A nested function can jump to a label inherited from a containing
  9029. function, provided the label was explicitly declared in the containing
  9030. function (@pxref{Local Labels}). Such a jump returns instantly to the
  9031. containing function, exiting the nested function that did the
  9032. @code{goto} and any intermediate function invocations as well. Here
  9033. is an example:
  9034. @example
  9035. @group
  9036. bar (int *array, int offset, int size)
  9037. @{
  9038. /* @r{Explicitly declare the label @code{failure}.} */
  9039. __label__ failure;
  9040. int access (int *array, int index)
  9041. @{
  9042. if (index > size)
  9043. /* @r{Exit this function,}
  9044. @r{and return to @code{bar}.} */
  9045. goto failure;
  9046. return array[index + offset];
  9047. @}
  9048. @end group
  9049. @group
  9050. int i;
  9051. @r{@dots{}}
  9052. for (i = 0; i < size; i++)
  9053. @r{@dots{}} access (array, i) @r{@dots{}}
  9054. @r{@dots{}}
  9055. return 0;
  9056. /* @r{Control comes here from @code{access}
  9057. if it does the @code{goto}.} */
  9058. failure:
  9059. return -1;
  9060. @}
  9061. @end group
  9062. @end example
  9063. To declare the nested function before its definition, use
  9064. @code{auto} (which is otherwise meaningless for function declarations;
  9065. @pxref{auto and register}). For example,
  9066. @example
  9067. bar (int *array, int offset, int size)
  9068. @{
  9069. auto int access (int *, int);
  9070. @r{@dots{}}
  9071. @r{@dots{}} access (array, i) @r{@dots{}}
  9072. @r{@dots{}}
  9073. int access (int *array, int index)
  9074. @{
  9075. @r{@dots{}}
  9076. @}
  9077. @r{@dots{}}
  9078. @}
  9079. @end example
  9080. @node Inline Function Definitions
  9081. @subsection Inline Function Definitions
  9082. @cindex inline function definitions
  9083. @cindex function definitions, inline
  9084. @findex inline
  9085. To declare a function inline, use the @code{inline} keyword in its
  9086. definition. Here's a simple function that takes a pointer-to-@code{int}
  9087. and increments the integer stored there---declared inline.
  9088. @example
  9089. struct list
  9090. @{
  9091. struct list *first, *second;
  9092. @};
  9093. inline struct list *
  9094. list_first (struct list *p)
  9095. @{
  9096. return p->first;
  9097. @}
  9098. inline struct list *
  9099. list_second (struct list *p)
  9100. @{
  9101. return p->second;
  9102. @}
  9103. @end example
  9104. optimized compilation can substitute the inline function's body for
  9105. any call to it. This is called @emph{inlining} the function. It
  9106. makes the code that contains the call run faster, significantly so if
  9107. the inline function is small.
  9108. Here's a function that uses @code{pair_second}:
  9109. @example
  9110. int
  9111. pairlist_length (struct list *l)
  9112. @{
  9113. int length = 0;
  9114. while (l)
  9115. @{
  9116. length++;
  9117. l = pair_second (l);
  9118. @}
  9119. return length;
  9120. @}
  9121. @end example
  9122. Substituting the code of @code{pair_second} into the definition of
  9123. @code{pairlist_length} results in this code, in effect:
  9124. @example
  9125. int
  9126. pairlist_length (struct list *l)
  9127. @{
  9128. int length = 0;
  9129. while (l)
  9130. @{
  9131. length++;
  9132. l = l->second;
  9133. @}
  9134. return length;
  9135. @}
  9136. @end example
  9137. Since the definition of @code{pair_second} does not say @code{extern}
  9138. or @code{static}, that definition is used only for inlining. It
  9139. doesn't generate code that can be called at run time. If not all the
  9140. calls to the function are inlined, there must be a definition of the
  9141. same function name in another module for them to call.
  9142. @cindex inline functions, omission of
  9143. @c @opindex fkeep-inline-functions
  9144. Adding @code{static} to an inline function definition means the
  9145. function definition is limited to this compilation module. Also, it
  9146. generates run-time code if necessary for the sake of any calls that
  9147. were not inlined. If all calls are inlined then the function
  9148. definition does not generate run-time code, but you can force
  9149. generation of run-time code with the option
  9150. @option{-fkeep-inline-functions}.
  9151. @cindex extern inline function
  9152. Specifying @code{extern} along with @code{inline} means the function is
  9153. external and generates run-time code to be called from other
  9154. separately compiled modules, as well as inlined. You can define the
  9155. function as @code{inline} without @code{extern} in other modules so as
  9156. to inline calls to the same function in those modules.
  9157. Why are some calls not inlined? First of all, inlining is an
  9158. optimization, so non-optimized compilation does not inline.
  9159. Some calls cannot be inlined for technical reasons. Also, certain
  9160. usages in a function definition can make it unsuitable for inline
  9161. substitution. Among these usages are: variadic functions, use of
  9162. @code{alloca}, use of computed goto (@pxref{Labels as Values}), and
  9163. use of nonlocal goto. The option @option{-Winline} requests a warning
  9164. when a function marked @code{inline} is unsuitable to be inlined. The
  9165. warning explains what obstacle makes it unsuitable.
  9166. Just because a call @emph{can} be inlined does not mean it
  9167. @emph{should} be inlined. The GNU C compiler weighs costs and
  9168. benefits to decide whether inlining a particular call is advantageous.
  9169. You can force inlining of all calls to a given function that can be
  9170. inlined, even in a non-optimized compilation. by specifying the
  9171. @samp{always_inline} attribute for the function, like this:
  9172. @example
  9173. /* @r{Prototype.} */
  9174. inline void foo (const char) __attribute__((always_inline));
  9175. @end example
  9176. @noindent
  9177. This is a GNU C extension. @xref{Attributes}.
  9178. A function call may be inlined even if not declared @code{inline} in
  9179. special cases where the compiler can determine this is correct and
  9180. desirable. For instance, when a static function is called only once,
  9181. it will very likely be inlined. With @option{-flto}, link-time
  9182. optimization, any function might be inlined. To absolutely prevent
  9183. inlining of a specific function, specify
  9184. @code{__attribute__((__noinline__))} in the function's definition.
  9185. @node Obsolete Definitions
  9186. @section Obsolete Function Features
  9187. These features of function definitions are still used in old
  9188. programs, but you shouldn't write code this way today.
  9189. If you are just learning C, you can skip this section.
  9190. @menu
  9191. * Old GNU Inlining:: An older inlining technique.
  9192. * Old-Style Function Definitions:: Original K&R style functions.
  9193. @end menu
  9194. @node Old GNU Inlining
  9195. @subsection Older GNU C Inlining
  9196. The GNU C spec for inline functions, before GCC version 5, defined
  9197. @code{extern inline} on a function definition to mean to inline calls
  9198. to it but @emph{not} generate code for the function that could be
  9199. called at run time. By contrast, @code{inline} without @code{extern}
  9200. specified to generate run-time code for the function. In effect, ISO
  9201. incompatibly flipped the meanings of these two cases. We changed GCC
  9202. in version 5 to adopt the ISO specification.
  9203. Many programs still use these cases with the previous GNU C meanings.
  9204. You can specify use of those meanings with the option
  9205. @option{-fgnu89-inline}. You can also specify this for a single
  9206. function with @code{__attribute__ ((gnu_inline))}. Here's an example:
  9207. @example
  9208. inline __attribute__ ((gnu_inline))
  9209. int
  9210. inc (int *a)
  9211. @{
  9212. (*a)++;
  9213. @}
  9214. @end example
  9215. @node Old-Style Function Definitions
  9216. @subsection Old-Style Function Definitions
  9217. @cindex old-style function definitions
  9218. @cindex function definitions, old-style
  9219. @cindex K&R-style function definitions
  9220. The syntax of C traditionally allows omitting the data type in a
  9221. function declaration if it specifies a storage class or a qualifier.
  9222. Then the type defaults to @code{int}. For example:
  9223. @example
  9224. static foo (double x);
  9225. @end example
  9226. @noindent
  9227. defaults the return type to @code{int}. This is bad practice; if you
  9228. see it, fix it.
  9229. An @dfn{old-style} (or ``K&R'') function definition is the way
  9230. function definitions were written in the 1980s. It looks like this:
  9231. @example
  9232. @var{rettype}
  9233. @var{function} (@var{parmnames})
  9234. @var{parm_declarations}
  9235. @{
  9236. @var{body}
  9237. @}
  9238. @end example
  9239. In @var{parmnames}, only the parameter names are listed, separated by
  9240. commas. Then @var{parm_declarations} declares their data types; these
  9241. declarations look just like variable declarations. If a parameter is
  9242. listed in @var{parmnames} but has no declaration, it is implicitly
  9243. declared @code{int}.
  9244. There is no reason to write a definition this way nowadays, but they
  9245. can still be seen in older GNU programs.
  9246. An old-style variadic function definition looks like this:
  9247. @example
  9248. #include <varargs.h>
  9249. int
  9250. add_multiple_values (va_alist)
  9251. va_dcl
  9252. @{
  9253. int argcount;
  9254. int counter, total = 0;
  9255. /* @r{Declare a variable of type @code{va_list}.} */
  9256. va_list argptr;
  9257. /* @r{Initialize that variable.} */
  9258. va_start (argptr);
  9259. /* @r{Get the first argument (fixed).} */
  9260. argcount = va_arg (int);
  9261. for (counter = 0; counter < argcount; counter++)
  9262. @{
  9263. /* @r{Get the next additional argument.} */
  9264. total += va_arg (argptr, int);
  9265. @}
  9266. /* @r{End use of the @code{argptr} variable.} */
  9267. va_end (argptr);
  9268. return total;
  9269. @}
  9270. @end example
  9271. Note that the old-style variadic function definition has no fixed
  9272. parameter variables; all arguments must be obtained with
  9273. @code{va_arg}.
  9274. @node Compatible Types
  9275. @chapter Compatible Types
  9276. @cindex compatible types
  9277. @cindex types, compatible
  9278. Declaring a function or variable twice is valid in C only if the two
  9279. declarations specify @dfn{compatible} types. In addition, some
  9280. operations on pointers require operands to have compatible target
  9281. types.
  9282. In C, two different primitive types are never compatible. Likewise for
  9283. the defined types @code{struct}, @code{union} and @code{enum}: two
  9284. separately defined types are incompatible unless they are defined
  9285. exactly the same way.
  9286. However, there are a few cases where different types can be
  9287. compatible:
  9288. @itemize @bullet
  9289. @item
  9290. Every enumeration type is compatible with some integer type. In GNU
  9291. C, the choice of integer type depends on the largest enumeration
  9292. value.
  9293. @c ??? Which one, in GCC?
  9294. @c ??? ... it varies, depending on the enum values. Testing on
  9295. @c ??? fencepost, it appears to use a 4-byte signed integer first,
  9296. @c ??? then moves on to an 8-byte signed integer. These details
  9297. @c ??? might be platform-dependent, as the C standard says that even
  9298. @c ??? char could be used as an enum type, but it's at least true
  9299. @c ??? that GCC chooses a type that is at least large enough to
  9300. @c ??? hold the largest enum value.
  9301. @item
  9302. Array types are compatible if the element types are compatible
  9303. and the sizes (when specified) match.
  9304. @item
  9305. Pointer types are compatible if the pointer target types are
  9306. compatible.
  9307. @item
  9308. Function types that specify argument types are compatible if the
  9309. return types are compatible and the argument types are compatible,
  9310. argument by argument. In addition, they must all agree in whether
  9311. they use @code{...} to allow additional arguments.
  9312. @item
  9313. Function types that don't specify argument types are compatible if the
  9314. return types are.
  9315. @item
  9316. Function types that specify the argument types are compatible with
  9317. function types that omit them, if the return types are compatible and
  9318. the specified argument types are unaltered by the argument promotions
  9319. (@pxref{Argument Promotions}).
  9320. @end itemize
  9321. In order for types to be compatible, they must agree in their type
  9322. qualifiers. Thus, @code{const int} and @code{int} are incompatible.
  9323. It follows that @code{const int *} and @code{int *} are incompatible
  9324. too (they are pointers to types that are not compatible).
  9325. If two types are compatible ignoring the qualifiers, we call them
  9326. @dfn{nearly compatible}. (If they are array types, we ignore
  9327. qualifiers on the element types.@footnote{This is a GNU C extension.})
  9328. Comparison of pointers is valid if the pointers' target types are
  9329. nearly compatible. Likewise, the two branches of a conditional
  9330. expression may be pointers to nearly compatible target types.
  9331. If two types are compatible ignoring the qualifiers, and the first
  9332. type has all the qualifiers of the second type, we say the first is
  9333. @dfn{upward compatible} with the second. Assignment of pointers
  9334. requires the assigned pointer's target type to be upward compatible
  9335. with the right operand (the new value)'s target type.
  9336. @node Type Conversions
  9337. @chapter Type Conversions
  9338. @cindex type conversions
  9339. @cindex conversions, type
  9340. C converts between data types automatically when that seems clearly
  9341. necessary. In addition, you can convert explicitly with a @dfn{cast}.
  9342. @menu
  9343. * Explicit Type Conversion:: Casting a value from one type to another.
  9344. * Assignment Type Conversions:: Automatic conversion by assignment operation.
  9345. * Argument Promotions:: Automatic conversion of function parameters.
  9346. * Operand Promotions:: Automatic conversion of arithmetic operands.
  9347. * Common Type:: When operand types differ, which one is used?
  9348. @end menu
  9349. @node Explicit Type Conversion
  9350. @section Explicit Type Conversion
  9351. @cindex cast
  9352. @cindex explicit type conversion
  9353. You can do explicit conversions using the unary @dfn{cast} operator,
  9354. which is written as a type designator (@pxref{Type Designators}) in
  9355. parentheses. For example, @code{(int)} is the operator to cast to
  9356. type @code{int}. Here's an example of using it:
  9357. @example
  9358. @{
  9359. double d = 5.5;
  9360. printf ("Floating point value: %f\n", d);
  9361. printf ("Rounded to integer: %d\n", (int) d);
  9362. @}
  9363. @end example
  9364. Using @code{(int) d} passes an @code{int} value as argument to
  9365. @code{printf}, so you can print it with @samp{%d}. Using just
  9366. @code{d} without the cast would pass the value as @code{double}.
  9367. That won't work at all with @samp{%d}; the results would be gibberish.
  9368. To divide one integer by another without rounding,
  9369. cast either of the integers to @code{double} first:
  9370. @example
  9371. (double) @var{dividend} / @var{divisor}
  9372. @var{dividend} / (double) @var{divisor}
  9373. @end example
  9374. It is enough to cast one of them, because that forces the common type
  9375. to @code{double} so the other will be converted automatically.
  9376. The valid cast conversions are:
  9377. @itemize @bullet
  9378. @item
  9379. One numerical type to another.
  9380. @item
  9381. One pointer type to another.
  9382. (Converting between pointers that point to functions
  9383. and pointers that point to data is not standard C.)
  9384. @item
  9385. A pointer type to an integer type.
  9386. @item
  9387. An integer type to a pointer type.
  9388. @item
  9389. To a union type, from the type of any alternative in the union
  9390. (@pxref{Unions}). (This is a GNU extension.)
  9391. @item
  9392. Anything, to @code{void}.
  9393. @end itemize
  9394. @node Assignment Type Conversions
  9395. @section Assignment Type Conversions
  9396. @cindex assignment type conversions
  9397. Certain type conversions occur automatically in assignments
  9398. and certain other contexts. These are the conversions
  9399. assignments can do:
  9400. @itemize @bullet
  9401. @item
  9402. Converting any numeric type to any other numeric type.
  9403. @item
  9404. Converting @code{void *} to any other pointer type
  9405. (except pointer-to-function types).
  9406. @item
  9407. Converting any other pointer type to @code{void *}.
  9408. (except pointer-to-function types).
  9409. @item
  9410. Converting 0 (a null pointer constant) to any pointer type.
  9411. @item
  9412. Converting any pointer type to @code{bool}. (The result is
  9413. 1 if the pointer is not null.)
  9414. @item
  9415. Converting between pointer types when the left-hand target type is
  9416. upward compatible with the right-hand target type. @xref{Compatible
  9417. Types}.
  9418. @end itemize
  9419. These type conversions occur automatically in certain contexts,
  9420. which are:
  9421. @itemize @bullet
  9422. @item
  9423. An assignment converts the type of the right-hand expression
  9424. to the type wanted by the left-hand expression. For example,
  9425. @example
  9426. double i;
  9427. i = 5;
  9428. @end example
  9429. @noindent
  9430. converts 5 to @code{double}.
  9431. @item
  9432. A function call, when the function specifies the type for that
  9433. argument, converts the argument value to that type. For example,
  9434. @example
  9435. void foo (double);
  9436. foo (5);
  9437. @end example
  9438. @noindent
  9439. converts 5 to @code{double}.
  9440. @item
  9441. A @code{return} statement converts the specified value to the type
  9442. that the function is declared to return. For example,
  9443. @example
  9444. double
  9445. foo ()
  9446. @{
  9447. return 5;
  9448. @}
  9449. @end example
  9450. @noindent
  9451. also converts 5 to @code{double}.
  9452. @end itemize
  9453. In all three contexts, if the conversion is impossible, that
  9454. constitutes an error.
  9455. @node Argument Promotions
  9456. @section Argument Promotions
  9457. @cindex argument promotions
  9458. @cindex promotion of arguments
  9459. When a function's definition or declaration does not specify the type
  9460. of an argument, that argument is passed without conversion in whatever
  9461. type it has, with these exceptions:
  9462. @itemize @bullet
  9463. @item
  9464. Some narrow numeric values are @dfn{promoted} to a wider type. If the
  9465. expression is a narrow integer, such as @code{char} or @code{short},
  9466. the call converts it automatically to @code{int} (@pxref{Integer
  9467. Types}).@footnote{On an embedded controller where @code{char}
  9468. or @code{short} is the same width as @code{int}, @code{unsigned char}
  9469. or @code{unsigned short} promotes to @code{unsigned int}, but that
  9470. never occurs in GNU C on real computers.}
  9471. In this example, the expression @code{c} is passed as an @code{int}:
  9472. @example
  9473. char c = '$';
  9474. printf ("Character c is '%c'\n", c);
  9475. @end example
  9476. @item
  9477. If the expression
  9478. has type @code{float}, the call converts it automatically to
  9479. @code{double}.
  9480. @item
  9481. An array as argument is converted to a pointer to its zeroth element.
  9482. @item
  9483. A function name as argument is converted to a pointer to that function.
  9484. @end itemize
  9485. @node Operand Promotions
  9486. @section Operand Promotions
  9487. @cindex operand promotions
  9488. The operands in arithmetic operations undergo type conversion automatically.
  9489. These @dfn{operand promotions} are the same as the argument promotions
  9490. except without converting @code{float} to @code{double}. In other words,
  9491. the operand promotions convert
  9492. @itemize @bullet
  9493. @item
  9494. @code{char} or @code{short} (whether signed or not) to @code{int}.
  9495. @item
  9496. an array to a pointer to its zeroth element, and
  9497. @item
  9498. a function name to a pointer to that function.
  9499. @end itemize
  9500. @node Common Type
  9501. @section Common Type
  9502. @cindex common type
  9503. Arithmetic binary operators (except the shift operators) convert their
  9504. operands to the @dfn{common type} before operating on them.
  9505. Conditional expressions also convert the two possible results to their
  9506. common type. Here are the rules for determining the common type.
  9507. If one of the numbers has a floating-point type and the other is an
  9508. integer, the common type is that floating-point type. For instance,
  9509. @example
  9510. 5.6 * 2 @result{} 11.2 /* @r{a @code{double} value} */
  9511. @end example
  9512. If both are floating point, the type with the larger range is the
  9513. common type.
  9514. If both are integers but of different widths, the common type
  9515. is the wider of the two.
  9516. If they are integer types of the same width, the common type is
  9517. unsigned if either operand is unsigned, and it's @code{long} if either
  9518. operand is @code{long}. It's @code{long long} if either operand is
  9519. @code{long long}.
  9520. These rules apply to addition, subtraction, multiplication, division,
  9521. remainder, comparisons, and bitwise operations. They also apply to
  9522. the two branches of a conditional expression, and to the arithmetic
  9523. done in a modifying assignment operation.
  9524. @node Scope
  9525. @chapter Scope
  9526. @cindex scope
  9527. @cindex block scope
  9528. @cindex function scope
  9529. @cindex function prototype scope
  9530. Each definition or declaration of an identifier is visible
  9531. in certain parts of the program, which is typically less than the whole
  9532. of the program. The parts where it is visible are called its @dfn{scope}.
  9533. Normally, declarations made at the top-level in the source -- that is,
  9534. not within any blocks and function definitions -- are visible for the
  9535. entire contents of the source file after that point. This is called
  9536. @dfn{file scope} (@pxref{File-Scope Variables}).
  9537. Declarations made within blocks of code, including within function
  9538. definitions, are visible only within those blocks. This is called
  9539. @dfn{block scope}. Here is an example:
  9540. @example
  9541. @group
  9542. void
  9543. foo (void)
  9544. @{
  9545. int x = 42;
  9546. @}
  9547. @end group
  9548. @end example
  9549. @noindent
  9550. In this example, the variable @code{x} has block scope; it is visible
  9551. only within the @code{foo} function definition block. Thus, other
  9552. blocks could have their own variables, also named @code{x}, without
  9553. any conflict between those variables.
  9554. A variable declared inside a subblock has a scope limited to
  9555. that subblock,
  9556. @example
  9557. @group
  9558. void
  9559. foo (void)
  9560. @{
  9561. @{
  9562. int x = 42;
  9563. @}
  9564. // @r{@code{x} is out of scope here.}
  9565. @}
  9566. @end group
  9567. @end example
  9568. If a variable declared within a block has the same name as a variable
  9569. declared outside of that block, the definition within the block
  9570. takes precedence during its scope:
  9571. @example
  9572. @group
  9573. int x = 42;
  9574. void
  9575. foo (void)
  9576. @{
  9577. int x = 17;
  9578. printf ("%d\n", x);
  9579. @}
  9580. @end group
  9581. @end example
  9582. @noindent
  9583. This prints 17, the value of the variable @code{x} declared in the
  9584. function body block, rather than the value of the variable @code{x} at
  9585. file scope. We say that the inner declaration of @code{x}
  9586. @dfn{shadows} the outer declaration, for the extent of the inner
  9587. declaration's scope.
  9588. A declaration with block scope can be shadowed by another declaration
  9589. with the same name in a subblock.
  9590. @example
  9591. @group
  9592. void
  9593. foo (void)
  9594. @{
  9595. char *x = "foo";
  9596. @{
  9597. int x = 42;
  9598. @r{@dots{}}
  9599. exit (x / 6);
  9600. @}
  9601. @}
  9602. @end group
  9603. @end example
  9604. A function parameter's scope is the entire function body, but it can
  9605. be shadowed. For example:
  9606. @example
  9607. @group
  9608. int x = 42;
  9609. void
  9610. foo (int x)
  9611. @{
  9612. printf ("%d\n", x);
  9613. @}
  9614. @end group
  9615. @end example
  9616. @noindent
  9617. This prints the value of @code{x} the function parameter, rather than
  9618. the value of the file-scope variable @code{x}.
  9619. Labels (@pxref{goto Statement}) have @dfn{function} scope: each label
  9620. is visible for the whole of the containing function body, both before
  9621. and after the label declaration:
  9622. @example
  9623. @group
  9624. void
  9625. foo (void)
  9626. @{
  9627. @r{@dots{}}
  9628. goto bar;
  9629. @r{@dots{}}
  9630. @{ // @r{Subblock does not affect labels.}
  9631. bar:
  9632. @r{@dots{}}
  9633. @}
  9634. goto bar;
  9635. @}
  9636. @end group
  9637. @end example
  9638. Except for labels, a declared identifier is not
  9639. visible to code before its declaration. For example:
  9640. @example
  9641. @group
  9642. int x = 5;
  9643. int y = x + 10;
  9644. @end group
  9645. @end example
  9646. @noindent
  9647. will work, but:
  9648. @example
  9649. @group
  9650. int x = y + 10;
  9651. int y = 5;
  9652. @end group
  9653. @end example
  9654. @noindent
  9655. cannot refer to the variable @code{y} before its declaration.
  9656. @include cpp.texi
  9657. @node Integers in Depth
  9658. @chapter Integers in Depth
  9659. This chapter explains the machine-level details of integer types: how
  9660. they are represented as bits in memory, and the range of possible
  9661. values for each integer type.
  9662. @menu
  9663. * Integer Representations:: How integer values appear in memory.
  9664. * Maximum and Minimum Values:: Value ranges of integer types.
  9665. @end menu
  9666. @node Integer Representations
  9667. @section Integer Representations
  9668. @cindex integer representations
  9669. @cindex representation of integers
  9670. Modern computers store integer values as binary (base-2) numbers that
  9671. occupy a single unit of storage, typically either as an 8-bit
  9672. @code{char}, a 16-bit @code{short int}, a 32-bit @code{int}, or
  9673. possibly, a 64-bit @code{long long int}. Whether a @code{long int} is
  9674. a 32-bit or a 64-bit value is system dependent.@footnote{In theory,
  9675. any of these types could have some other size, bit it's not worth even
  9676. a minute to cater to that possibility. It never happens on
  9677. GNU/Linux.}
  9678. @cindex @code{CHAR_BIT}
  9679. The macro @code{CHAR_BIT}, defined in @file{limits.h}, gives the number
  9680. of bits in type @code{char}. On any real operating system, the value
  9681. is 8.
  9682. The fixed sizes of numeric types necessarily limits their @dfn{range
  9683. of values}, and the particular encoding of integers decides what that
  9684. range is.
  9685. @cindex two's-complement representation
  9686. For unsigned integers, the entire space is used to represent a
  9687. nonnegative value. Signed integers are stored using
  9688. @dfn{two's-complement representation}: a signed integer with @var{n}
  9689. bits has a range from @math{-2@sup{(@var{n} - 1)}} to @minus{}1 to 0
  9690. to 1 to @math{+2@sup{(@var{n} - 1)} - 1}, inclusive. The leftmost, or
  9691. high-order, bit is called the @dfn{sign bit}.
  9692. @c ??? Needs correcting
  9693. There is only one value that means zero, and the most negative number
  9694. lacks a positive counterpart. As a result, negating that number
  9695. causes overflow; in practice, its result is that number back again.
  9696. For example, a two's-complement signed 8-bit integer can represent all
  9697. decimal numbers from @minus{}128 to +127. We will revisit that
  9698. peculiarity shortly.
  9699. Decades ago, there were computers that didn't use two's-complement
  9700. representation for integers (@pxref{Integers in Depth}), but they are
  9701. long gone and not worth any effort to support.
  9702. @c ??? Is this duplicate?
  9703. When an arithmetic operation produces a value that is too big to
  9704. represent, the operation is said to @dfn{overflow}. In C, integer
  9705. overflow does not interrupt the control flow or signal an error.
  9706. What it does depends on signedness.
  9707. For unsigned arithmetic, the result of an operation that overflows is
  9708. the @var{n} low-order bits of the correct value. If the correct value
  9709. is representable in @var{n} bits, that is always the result;
  9710. thus we often say that ``integer arithmetic is exact,'' omitting the
  9711. crucial qualifying phrase ``as long as the exact result is
  9712. representable.''
  9713. In principle, a C program should be written so that overflow never
  9714. occurs for signed integers, but in GNU C you can specify various ways
  9715. of handling such overflow (@pxref{Integer Overflow}).
  9716. Integer representations are best understood by looking at a table for
  9717. a tiny integer size; here are the possible values for an integer with
  9718. three bits:
  9719. @multitable @columnfractions .25 .25 .25 .25
  9720. @headitem Unsigned @tab Signed @tab Bits @tab 2s Complement
  9721. @item 0 @tab 0 @tab 000 @tab 000 (0)
  9722. @item 1 @tab 1 @tab 001 @tab 111 (-1)
  9723. @item 2 @tab 2 @tab 010 @tab 110 (-2)
  9724. @item 3 @tab 3 @tab 011 @tab 101 (-3)
  9725. @item 4 @tab -4 @tab 100 @tab 100 (-4)
  9726. @item 5 @tab -3 @tab 101 @tab 011 (3)
  9727. @item 6 @tab -2 @tab 110 @tab 010 (2)
  9728. @item 7 @tab -1 @tab 111 @tab 001 (1)
  9729. @end multitable
  9730. The parenthesized decimal numbers in the last column represent the
  9731. signed meanings of the two's-complement of the line's value. Recall
  9732. that, in two's-complement encoding, the high-order bit is 0 when
  9733. the number is nonnegative.
  9734. We can now understand the peculiar behavior of negation of the
  9735. most negative two's-complement integer: start with 0b100,
  9736. invert the bits to get 0b011, and add 1: we get
  9737. 0b100, the value we started with.
  9738. We can also see overflow behavior in two's-complement:
  9739. @example
  9740. 3 + 1 = 0b011 + 0b001 = 0b100 = (-4)
  9741. 3 + 2 = 0b011 + 0b010 = 0b101 = (-3)
  9742. 3 + 3 = 0b011 + 0b011 = 0b110 = (-2)
  9743. @end example
  9744. @noindent
  9745. A sum of two nonnegative signed values that overflows has a 1 in the
  9746. sign bit, so the exact positive result is truncated to a negative
  9747. value.
  9748. @c =====================================================================
  9749. @node Maximum and Minimum Values
  9750. @section Maximum and Minimum Values
  9751. @cindex maximum integer values
  9752. @cindex minimum integer values
  9753. @cindex integer ranges
  9754. @cindex ranges of integer types
  9755. @findex INT_MAX
  9756. @findex UINT_MAX
  9757. @findex SHRT_MAX
  9758. @findex LONG_MAX
  9759. @findex LLONG_MAX
  9760. @findex USHRT_MAX
  9761. @findex ULONG_MAX
  9762. @findex ULLONG_MAX
  9763. @findex CHAR_MAX
  9764. @findex SCHAR_MAX
  9765. @findex UCHAR_MAX
  9766. For each primitive integer type, there is a standard macro defined in
  9767. @file{limits.h} that gives the largest value that type can hold. For
  9768. instance, for type @code{int}, the maximum value is @code{INT_MAX}.
  9769. On a 32-bit computer, that is equal to 2,147,483,647. The
  9770. maximum value for @code{unsigned int} is @code{UINT_MAX}, which on a
  9771. 32-bit computer is equal to 4,294,967,295. Likewise, there are
  9772. @code{SHRT_MAX}, @code{LONG_MAX}, and @code{LLONG_MAX}, and
  9773. corresponding unsigned limits @code{USHRT_MAX}, @code{ULONG_MAX}, and
  9774. @code{ULLONG_MAX}.
  9775. Since there are three ways to specify a @code{char} type, there are
  9776. also three limits: @code{CHAR_MAX}, @code{SCHAR_MAX}, and
  9777. @code{UCHAR_MAX}.
  9778. For each type that is or might be signed, there is another symbol that
  9779. gives the minimum value it can hold. (Just replace @code{MAX} with
  9780. @code{MIN} in the names listed above.) There is no minimum limit
  9781. symbol for types specified with @code{unsigned} because the
  9782. minimum for them is universally zero.
  9783. @code{INT_MIN} is not the negative of @code{INT_MAX}. In
  9784. two's-complement representation, the most negative number is 1 less
  9785. than the negative of the most positive number. Thus, @code{INT_MIN}
  9786. on a 32-bit computer has the value @minus{}2,147,483,648. You can't
  9787. actually write the value that way in C, since it would overflow.
  9788. That's a good reason to use @code{INT_MIN} to specify
  9789. that value. Its definition is written to avoid overflow.
  9790. @include fp.texi
  9791. @node Compilation
  9792. @chapter Compilation
  9793. @cindex object file
  9794. @cindex compilation module
  9795. @cindex make rules
  9796. @cindex link
  9797. Early in the manual we explained how to compile a simple C program
  9798. that consists of a single source file (@pxref{Compile Example}).
  9799. However, we handle only short programs that way. A typical C program
  9800. consists of many source files, each of which is usually a separate
  9801. @dfn{compilation module}---meaning that it has to be compiled
  9802. separately. (The source files that are not separate compilation
  9803. modules are those that are used via @code{#include}; see @ref{Header
  9804. Files}.)
  9805. To compile a multi-module program, you compile each of the program's
  9806. compilation modules, making an @dfn{object file} for that module. The
  9807. last step is to @dfn{link} the many object files together into a
  9808. single executable for the whole program.
  9809. The full details of how to compile C programs (and other programs)
  9810. with GCC are documented in xxxx.
  9811. @c ??? ref
  9812. Here we give only a simple introduction.
  9813. These commands compile two compilation modules, @file{foo.c} and
  9814. @file{bar.c}, running the compiler for each module:
  9815. @example
  9816. gcc -c -O -g foo.c
  9817. gcc -c -O -g bar.c
  9818. @end example
  9819. @noindent
  9820. In these commands, @option{-g} says to generate debugging information,
  9821. @option{-O} says to do some optimization, and @option{-c} says to put
  9822. the compiled code for that module into a corresponding object file and
  9823. go no further. The object file for @file{foo.c} is automatically
  9824. called @file{foo.o}, and so on.
  9825. If you wish, you can specify the additional compilation options. For
  9826. instance, @option{-Wformat -Wparenthesis -Wstrict-prototypes} request
  9827. additional warnings.
  9828. @cindex linking object files
  9829. After you compile all the program's modules, you link the object files
  9830. into a combined executable, like this:
  9831. @example
  9832. gcc -o foo foo.o bar.o
  9833. @end example
  9834. @noindent
  9835. In this command, @option{-o foo} species the file name for the
  9836. executable file, and the other arguments are the object files to link.
  9837. Always specify the executable file name in a command that generates
  9838. one.
  9839. One reason to divide a large program into multiple compilation modules
  9840. is to control how each module can access the internals of the others.
  9841. When a module declares a function or variable @code{extern}, other
  9842. modules can access it. The other functions and variables defined in a
  9843. module can't be accessed from outside that module.
  9844. The other reason for using multiple modules is so that changing one
  9845. source file does not require recompiling all of them in order to try
  9846. the modified program. It is sufficient to recompile the source file
  9847. that you changed, then link them all again. Dividing a large program
  9848. into many substantial modules in this way typically makes
  9849. recompilation much faster.
  9850. Normally we don't run any of these commands directly. Instead we
  9851. write a set of @dfn{make rules} for the program, then use the
  9852. @command{make} program to recompile only the source files that need to
  9853. be recompiled, by following those rules. @xref{Top, The GNU Make
  9854. Manual, , make, The GNU Make Manual}.
  9855. @node Directing Compilation
  9856. @chapter Directing Compilation
  9857. This chapter describes C constructs that don't alter the program's
  9858. meaning @emph{as such}, but rather direct the compiler how to treat
  9859. some aspects of the program.
  9860. @menu
  9861. * Pragmas:: Controlling compilation of some constructs.
  9862. * Static Assertions:: Compile-time tests for conditions.
  9863. @end menu
  9864. @node Pragmas
  9865. @section Pragmas
  9866. A @dfn{pragma} is an annotation in a program that gives direction to
  9867. the compiler.
  9868. @menu
  9869. * Pragma Basics:: Pragma syntax and usage.
  9870. * Severity Pragmas:: Settings for compile-time pragma output.
  9871. * Optimization Pragmas:: Controlling optimizations.
  9872. @end menu
  9873. @c See also @ref{Macro Pragmas}, which save and restore macro definitions.
  9874. @node Pragma Basics
  9875. @subsection Pragma Basics
  9876. C defines two syntactical forms for pragmas, the line form and the
  9877. token form. You can write any pragma in either form, with the same
  9878. meaning.
  9879. The line form is a line in the source code, like this:
  9880. @example
  9881. #pragma @var{line}
  9882. @end example
  9883. @noindent
  9884. The line pragma has no effect on the parsing of the lines around it.
  9885. This form has the drawback that it can't be generated by a macro expansion.
  9886. The token form is a series of tokens; it can appear anywhere in the
  9887. program between the other tokens.
  9888. @example
  9889. _Pragma (@var{stringconstant})
  9890. @end example
  9891. @noindent
  9892. The pragma has no effect on the syntax of the tokens that surround it;
  9893. thus, here's a pragma in the middle of an @code{if} statement:
  9894. @example
  9895. if _Pragma ("hello") (x > 1)
  9896. @end example
  9897. @noindent
  9898. However, that's an unclear thing to do; for the sake of
  9899. understandability, it is better to put a pragma on a line by itself
  9900. and not embedded in the middle of another construct.
  9901. Both forms of pragma have a textual argument. In a line pragma, the
  9902. text is the rest of the line. The textual argument to @code{_Pragma}
  9903. uses the same syntax as a C string constant: surround the text with
  9904. two @samp{"} characters, and add a backslash before each @samp{"} or
  9905. @samp{\} character in it.
  9906. With either syntax, the textual argument specifies what to do.
  9907. It begins with one or several words that specify the operation.
  9908. If the compiler does not recognize them, it ignores the pragma.
  9909. Here are the pragma operations supported in GNU C@.
  9910. @c ??? Verify font for []
  9911. @table @code
  9912. @item #pragma GCC dependency "@var{file}" [@var{message}]
  9913. @itemx _Pragma ("GCC dependency \"@var{file}\" [@var{message}]")
  9914. Declares that the current source file depends on @var{file}, so GNU C
  9915. compares the file times and gives a warning if @var{file} is newer
  9916. than the current source file.
  9917. This directive searches for @var{file} the way @code{#include}
  9918. searches for a non-system header file.
  9919. If @var{message} is given, the warning message includes that text.
  9920. Examples:
  9921. @example
  9922. #pragma GCC dependency "parse.y"
  9923. _pragma ("GCC dependency \"/usr/include/time.h\" \
  9924. rerun fixincludes")
  9925. @end example
  9926. @item #pragma GCC poison @var{identifiers}
  9927. @itemx _Pragma ("GCC poison @var{identifiers}")
  9928. Poisons the identifiers listed in @var{identifiers}.
  9929. This is useful to make sure all mention of @var{identifiers} has been
  9930. deleted from the program and that no reference to them creeps back in.
  9931. If any of those identifiers appears anywhere in the source after the
  9932. directive, it causes a compilation error. For example,
  9933. @example
  9934. #pragma GCC poison printf sprintf fprintf
  9935. sprintf(some_string, "hello");
  9936. @end example
  9937. @noindent
  9938. generates an error.
  9939. If a poisoned identifier appears as part of the expansion of a macro
  9940. that was defined before the identifier was poisoned, it will @emph{not}
  9941. cause an error. Thus, system headers that define macros that use
  9942. the identifier will not cause errors.
  9943. For example,
  9944. @example
  9945. #define strrchr rindex
  9946. _Pragma ("GCC poison rindex")
  9947. strrchr(some_string, 'h');
  9948. @end example
  9949. @noindent
  9950. does not cause a compilation error.
  9951. @item #pragma GCC system_header
  9952. @itemx _Pragma ("GCC system_header")
  9953. Specify treating the rest of the current source file as if it came
  9954. from a system header file. @xref{System Headers, System Headers,
  9955. System Headers, gcc, Using the GNU Compiler Collection}.
  9956. @item #pragma GCC warning @var{message}
  9957. @itemx _Pragma ("GCC warning @var{message}")
  9958. Equivalent to @code{#warning}. Its advantage is that the
  9959. @code{_Pragma} form can be included in a macro definition.
  9960. @item #pragma GCC error @var{message}
  9961. @itemx _Pragma ("GCC error @var{message}")
  9962. Equivalent to @code{#error}. Its advantage is that the
  9963. @code{_Pragma} form can be included in a macro definition.
  9964. @item #pragma GCC message @var{message}
  9965. @itemx _Pragma ("GCC message @var{message}")
  9966. Similar to @samp{GCC warning} and @samp{GCC error}, this simply prints an
  9967. informational message, and could be used to include additional warning
  9968. or error text without triggering more warnings or errors. (Note that
  9969. unlike @samp{warning} and @samp{error}, @samp{message} does not include
  9970. @samp{GCC} as part of the pragma.)
  9971. @end table
  9972. @node Severity Pragmas
  9973. @subsection Severity Pragmas
  9974. These pragmas control the severity of classes of diagnostics.
  9975. You can specify the class of diagnostic with the GCC option that causes
  9976. those diagnostics to be generated.
  9977. @table @code
  9978. @item #pragma GCC diagnostic error @var{option}
  9979. @itemx _Pragma ("GCC diagnostic error @var{option}")
  9980. For code following this pragma, treat diagnostics of the variety
  9981. specified by @var{option} as errors. For example:
  9982. @example
  9983. _Pragma ("GCC diagnostic error -Wformat")
  9984. @end example
  9985. @noindent
  9986. specifies to treat diagnostics enabled by the @var{-Wformat} option
  9987. as errors rather than warnings.
  9988. @item #pragma GCC diagnostic warning @var{option}
  9989. @itemx _Pragma ("GCC diagnostic warning @var{option}")
  9990. For code following this pragma, treat diagnostics of the variety
  9991. specified by @var{option} as warnings. This overrides the
  9992. @var{-Werror} option which says to treat warnings as errors.
  9993. @item #pragma GCC diagnostic ignore @var{option}
  9994. @itemx _Pragma ("GCC diagnostic ignore @var{option}")
  9995. For code following this pragma, refrain from reporting any diagnostics
  9996. of the variety specified by @var{option}.
  9997. @item #pragma GCC diagnostic push
  9998. @itemx _Pragma ("GCC diagnostic push")
  9999. @itemx #pragma GCC diagnostic pop
  10000. @itemx _Pragma ("GCC diagnostic pop")
  10001. These pragmas maintain a stack of states for severity settings.
  10002. @samp{GCC diagnostic push} saves the current settings on the stack,
  10003. and @samp{GCC diagnostic pop} pops the last stack item and restores
  10004. the current settings from that.
  10005. @samp{GCC diagnostic pop} when the severity setting stack is empty
  10006. restores the settings to what they were at the start of compilation.
  10007. Here is an example:
  10008. @example
  10009. _Pragma ("GCC diagnostic error -Wformat")
  10010. /* @r{@option{-Wformat} messages treated as errors. } */
  10011. _Pragma ("GCC diagnostic push")
  10012. _Pragma ("GCC diagnostic warning -Wformat")
  10013. /* @r{@option{-Wformat} messages treated as warnings. } */
  10014. _Pragma ("GCC diagnostic push")
  10015. _Pragma ("GCC diagnostic ignored -Wformat")
  10016. /* @r{@option{-Wformat} messages suppressed. } */
  10017. _Pragma ("GCC diagnostic pop")
  10018. /* @r{@option{-Wformat} messages treated as warnings again. } */
  10019. _Pragma ("GCC diagnostic pop")
  10020. /* @r{@option{-Wformat} messages treated as errors again. } */
  10021. /* @r{This is an excess @samp{pop} that matches no @samp{push}. } */
  10022. _Pragma ("GCC diagnostic pop")
  10023. /* @r{@option{-Wformat} messages treated once again}
  10024. @r{as specified by the GCC command-line options.} */
  10025. @end example
  10026. @end table
  10027. @node Optimization Pragmas
  10028. @subsection Optimization Pragmas
  10029. These pragmas enable a particular optimization for specific function
  10030. definitions. The settings take effect at the end of a function
  10031. definition, so the clean place to use these pragmas is between
  10032. function definitions.
  10033. @table @code
  10034. @item #pragma GCC optimize @var{optimization}
  10035. @itemx _Pragma ("GCC optimize @var{optimization}")
  10036. These pragmas enable the optimization @var{optimization} for the
  10037. following functions. For example,
  10038. @example
  10039. _Pragma ("GCC optimize -fforward-propagate")
  10040. @end example
  10041. @noindent
  10042. says to apply the @samp{forward-propagate} optimization to all
  10043. following function definitions. Specifying optimizations for
  10044. individual functions, rather than for the entire program, is rare but
  10045. can be useful for getting around a bug in the compiler.
  10046. If @var{optimization} does not correspond to a defined optimization
  10047. option, the pragma is erroneous. To turn off an optimization, use the
  10048. corresponding @samp{-fno-} option, such as
  10049. @samp{-fno-forward-propagate}.
  10050. @item #pragma GCC target @var{optimizations}
  10051. @itemx _Pragma ("GCC target @var{optimizations}")
  10052. The pragma @samp{GCC target} is similar to @samp{GCC optimize} but is
  10053. used for platform-specific optimizations. Thus,
  10054. @example
  10055. _Pragma ("GCC target popcnt")
  10056. @end example
  10057. @noindent
  10058. activates the optimization @samp{popcnt} for all
  10059. following function definitions. This optimization is supported
  10060. on a few common targets but not on others.
  10061. @item #pragma GCC push_options
  10062. @itemx _Pragma ("GCC push_options")
  10063. The @samp{push_options} pragma saves on a stack the current settings
  10064. specified with the @samp{target} and @samp{optimize} pragmas.
  10065. @item #pragma GCC pop_options
  10066. @itemx _Pragma ("GCC pop_options")
  10067. The @samp{pop_options} pragma pops saved settings from that stack.
  10068. Here's an example of using this stack.
  10069. @example
  10070. _Pragma ("GCC push_options")
  10071. _Pragma ("GCC optimize forward-propagate")
  10072. /* @r{Functions to compile}
  10073. @r{with the @code{forward-propagate} optimization.} */
  10074. _Pragma ("GCC pop_options")
  10075. /* @r{Ends enablement of @code{forward-propagate}.} */
  10076. @end example
  10077. @item #pragma GCC reset_options
  10078. @itemx _Pragma ("GCC reset_options")
  10079. Clears all pragma-defined @samp{target} and @samp{optimize}
  10080. optimization settings.
  10081. @end table
  10082. @node Static Assertions
  10083. @section Static Assertions
  10084. @cindex static assertions
  10085. @findex _Static_assert
  10086. You can add compiler-time tests for necessary conditions into your
  10087. code using @code{_Static_assert}. This can be useful, for example, to
  10088. check that the compilation target platform supports the type sizes
  10089. that the code expects. For example,
  10090. @example
  10091. _Static_assert ((sizeof (long int) >= 8),
  10092. "long int needs to be at least 8 bytes");
  10093. @end example
  10094. @noindent
  10095. reports a compile-time error if compiled on a system with long
  10096. integers smaller than 8 bytes, with @samp{long int needs to be at
  10097. least 8 bytes} as the error message.
  10098. Since calls @code{_Static_assert} are processed at compile time, the
  10099. expression must be computable at compile time and the error message
  10100. must be a literal string. The expression can refer to the sizes of
  10101. variables, but can't refer to their values. For example, the
  10102. following static assertion is invalid for two reasons:
  10103. @example
  10104. char *error_message
  10105. = "long int needs to be at least 8 bytes";
  10106. int size_of_long_int = sizeof (long int);
  10107. _Static_assert (size_of_long_int == 8, error_message);
  10108. @end example
  10109. @noindent
  10110. The expression @code{size_of_long_int == 8} isn't computable at
  10111. compile time, and the error message isn't a literal string.
  10112. You can, though, use preprocessor definition values with
  10113. @code{_Static_assert}:
  10114. @example
  10115. #define LONG_INT_ERROR_MESSAGE "long int needs to be \
  10116. at least 8 bytes"
  10117. _Static_assert ((sizeof (long int) == 8),
  10118. LONG_INT_ERROR_MESSAGE);
  10119. @end example
  10120. Static assertions are permitted wherever a statement or declaration is
  10121. permitted, including at top level in the file, and also inside the
  10122. definition of a type.
  10123. @example
  10124. union y
  10125. @{
  10126. int i;
  10127. int *ptr;
  10128. _Static_assert (sizeof (int *) == sizeof (int),
  10129. "Pointer and int not same size");
  10130. @};
  10131. @end example
  10132. @node Type Alignment
  10133. @appendix Type Alignment
  10134. @cindex type alignment
  10135. @cindex alignment of type
  10136. @findex _Alignof
  10137. @findex __alignof__
  10138. Code for device drivers and other communication with low-level
  10139. hardware sometimes needs to be concerned with the alignment of
  10140. data objects in memory.
  10141. Each data type has a required @dfn{alignment}, always a power of 2,
  10142. that says at which memory addresses an object of that type can validly
  10143. start. A valid address for the type must be a multiple of its
  10144. alignment. If a type's alignment is 1, that means it can validly
  10145. start at any address. If a type's alignment is 2, that means it can
  10146. only start at an even address. If a type's alignment is 4, that means
  10147. it can only start at an address that is a multiple of 4.
  10148. The alignment of a type (except @code{char}) can vary depending on the
  10149. kind of computer in use. To refer to the alignment of a type in a C
  10150. program, use @code{_Alignof}, whose syntax parallels that of
  10151. @code{sizeof}. Like @code{sizeof}, @code{_Alignof} is a compile-time
  10152. operation, and it doesn't compute the value of the expression used
  10153. as its argument.
  10154. Nominally, each integer and floating-point type has an alignment equal to
  10155. the largest power of 2 that divides its size. Thus, @code{int} with
  10156. size 4 has a nominal alignment of 4, and @code{long long int} with
  10157. size 8 has a nominal alignment of 8.
  10158. However, each kind of computer generally has a maximum alignment, and
  10159. no type needs more alignment than that. If the computer's maximum
  10160. alignment is 4 (which is common), then no type's alignment is more
  10161. than 4.
  10162. The size of any type is always a multiple of its alignment; that way,
  10163. in an array whose elements have that type, all the elements are
  10164. properly aligned if the first one is.
  10165. These rules apply to all real computers today, but some embedded
  10166. controllers have odd exceptions. We don't have references to cite for
  10167. them.
  10168. @c We can't cite a nonfree manual as documentation.
  10169. Ordinary C code guarantees that every object of a given type is in
  10170. fact aligned as that type requires.
  10171. If the operand of @code{_Alignof} is a structure field, the value
  10172. is the alignment it requires. It may have a greater alignment by
  10173. coincidence, due to the other fields, but @code{_Alignof} is not
  10174. concerned about that. @xref{Structures}.
  10175. Older versions of GNU C used the keyword @code{__alignof__} for this,
  10176. but now that the feature has been standardized, it is better
  10177. to use the standard keyword @code{_Alignof}.
  10178. @findex _Alignas
  10179. @findex __aligned__
  10180. You can explicitly specify an alignment requirement for a particular
  10181. variable or structure field by adding @code{_Alignas
  10182. (@var{alignment})} to the declaration, where @var{alignment} is a
  10183. power of 2 or a type name. For instance:
  10184. @example
  10185. char _Alignas (8) x;
  10186. @end example
  10187. @noindent
  10188. or
  10189. @example
  10190. char _Alignas (double) x;
  10191. @end example
  10192. @noindent
  10193. specifies that @code{x} must start on an address that is a multiple of
  10194. 8. However, if @var{alignment} exceeds the maximum alignment for the
  10195. machine, that maximum is how much alignment @code{x} will get.
  10196. The older GNU C syntax for this feature looked like
  10197. @code{__attribute__ ((__aligned__ (@var{alignment})))} to the
  10198. declaration, and was added after the variable. For instance:
  10199. @example
  10200. char x __attribute__ ((__aligned__ 8));
  10201. @end example
  10202. @xref{Attributes}.
  10203. @node Aliasing
  10204. @appendix Aliasing
  10205. @cindex aliasing (of storage)
  10206. @cindex pointer type conversion
  10207. @cindex type conversion, pointer
  10208. We have already presented examples of casting a @code{void *} pointer
  10209. to another pointer type, and casting another pointer type to
  10210. @code{void *}.
  10211. One common kind of pointer cast is guaranteed safe: casting the value
  10212. returned by @code{malloc} and related functions (@pxref{Dynamic Memory
  10213. Allocation}). It is safe because these functions do not save the
  10214. pointer anywhere else; the only way the program will access the newly
  10215. allocated memory is via the pointer just returned.
  10216. In fact, C allows casting any pointer type to any other pointer type.
  10217. Using this to access the same place in memory using two
  10218. different data types is called @dfn{aliasing}.
  10219. Aliasing is necessary in some programs that do sophisticated memory
  10220. management, such as GNU Emacs, but most C programs don't need to do
  10221. aliasing. When it isn't needed, @strong{stay away from it!} To do
  10222. aliasing correctly requires following the rules stated below.
  10223. Otherwise, the aliasing may result in malfunctions when the program
  10224. runs.
  10225. The rest of this appendix explains the pitfalls and rules of aliasing.
  10226. @menu
  10227. * Aliasing Alignment:: Memory alignment considerations for
  10228. casting between pointer types.
  10229. * Aliasing Length:: Type size considerations for
  10230. casting between pointer types.
  10231. * Aliasing Type Rules:: Even when type alignment and size matches,
  10232. aliasing can still have surprising results.
  10233. @end menu
  10234. @node Aliasing Alignment
  10235. @appendixsection Aliasing and Alignment
  10236. In order for a type-converted pointer to be valid, it must have the
  10237. alignment that the new pointer type requires. For instance, on most
  10238. computers, @code{int} has alignment 4; the address of an @code{int}
  10239. must be a multiple of 4. However, @code{char} has alignment 1, so the
  10240. address of a @code{char} is usually not a multiple of 4. Taking the
  10241. address of such a @code{char} and casting it to @code{int *} probably
  10242. results in an invalid pointer. Trying to dereference it may cause a
  10243. @code{SIGBUS} signal, depending on the platform in use (@pxref{Signals}).
  10244. @example
  10245. foo ()
  10246. @{
  10247. char i[4];
  10248. int *p = (int *) &i[1]; /* @r{Misaligned pointer!} */
  10249. return *p; /* @r{Crash!} */
  10250. @}
  10251. @end example
  10252. This requirement is never a problem when casting the return value
  10253. of @code{malloc} because that function always returns a pointer
  10254. with as much alignment as any type can require.
  10255. @node Aliasing Length
  10256. @appendixsection Aliasing and Length
  10257. When converting a pointer to a different pointer type, make sure the
  10258. object it really points to is at least as long as the target of the
  10259. converted pointer. For instance, suppose @code{p} has type @code{int
  10260. *} and it's cast as follows:
  10261. @example
  10262. int *p;
  10263. struct
  10264. @{
  10265. double d, e, f;
  10266. @} foo;
  10267. struct foo *q = (struct foo *)p;
  10268. q->f = 5.14159;
  10269. @end example
  10270. @noindent
  10271. the value @code{q->f} will run past the end of the @code{int} that
  10272. @code{p} points to. If @code{p} was initialized to the start of an
  10273. array of type @code{int[6]}, the object is long enough for three
  10274. @code{double}s. But if @code{p} points to something shorter,
  10275. @code{q->f} will run on beyond the end of that, overlaying some other
  10276. data. Storing that will garble that other data. Or it could extend
  10277. past the end of memory space and cause a @code{SIGSEGV} signal
  10278. (@pxref{Signals}).
  10279. @node Aliasing Type Rules
  10280. @appendixsection Type Rules for Aliasing
  10281. C code that converts a pointer to a different pointer type can use the
  10282. pointers to access the same memory locations with two different data
  10283. types. If the same address is accessed with different types in a
  10284. single control thread, optimization can make the code do surprising
  10285. things (in effect, make it malfunction).
  10286. Here's a concrete example where aliasing that can change the code's
  10287. behavior when it is optimized. We assume that @code{float} is 4 bytes
  10288. long, like @code{int}, and so is every pointer. Thus, the structures
  10289. @code{struct a} and @code{struct b} are both 8 bytes.
  10290. @example
  10291. #include <stdio.h>
  10292. struct a @{ int size; char *data; @};
  10293. struct b @{ float size; char *data; @};
  10294. void sub (struct a *p, struct b *q)
  10295. @{
  10296.   int x;
  10297.   p->size = 0;
  10298.   q->size = 1;
  10299.   x = p->size;
  10300.   printf("x       =%d\n", x);
  10301.   printf("p->size =%d\n", (int)p->size);
  10302.   printf("q->size =%d\n", (int)q->size);
  10303. @}
  10304. int main(void)
  10305. @{
  10306.   struct a foo;
  10307.   struct a *p = &foo;
  10308.   struct b *q = (struct b *) &foo;
  10309.   sub (p, q);
  10310. @}
  10311. @end example
  10312. This code works as intended when compiled without optimization. All
  10313. the operations are carried out sequentially as written. The code
  10314. sets @code{x} to @code{p->size}, but what it actually gets is the
  10315. bits of the floating point number 1, as type @code{int}.
  10316. However, when optimizing, the compiler is allowed to assume
  10317. (mistakenly, here) that @code{q} does not point to the same storage as
  10318. @code{p}, because their data types are not allowed to alias.
  10319. From this assumption, the compiler can deduce (falsely, here) that the
  10320. assignment into @code{q->size} has no effect on the value of
  10321. @code{p->size}, which must therefore still be 0. Thus, @code{x} will
  10322. be set to 0.
  10323. GNU C, following the C standard, @emph{defines} this optimization as
  10324. legitimate. Code that misbehaves when optimized following these rules
  10325. is, by definition, incorrect C code.
  10326. The rules for storage aliasing in C are based on the two data types:
  10327. the type of the object, and the type it is accessed through. The
  10328. rules permit accessing part of a storage object of type @var{t} using
  10329. only these types:
  10330. @itemize @bullet
  10331. @item
  10332. @var{t}.
  10333. @item
  10334. A type compatible with @var{t}. @xref{Compatible Types}.
  10335. @item
  10336. A signed or unsigned version of one of the above.
  10337. @item
  10338. A qualified version of one of the above.
  10339. @xref{Type Qualifiers}.
  10340. @item
  10341. An array, structure (@pxref{Structures}), or union type
  10342. (@code{Unions}) that contains one of the above, either directly as a
  10343. field or through multiple levels of fields. If @var{t} is
  10344. @code{double}, this would include @code{struct s @{ union @{ double
  10345. d[2]; int i[4]; @} u; int i; @};} because there's a @code{double}
  10346. inside it somewhere.
  10347. @item
  10348. A character type.
  10349. @end itemize
  10350. What do these rules say about the example in this subsection?
  10351. For @code{foo.size} (equivalently, @code{a->size}), @var{t} is
  10352. @code{int}. The type @code{float} is not allowed as an aliasing type
  10353. by those rules, so @code{b->size} is not supposed to alias with
  10354. elements of @code{j}. Based on that assumption, GNU C makes a
  10355. permitted optimization that was not, in this case, consistent with
  10356. what the programmer intended the program to do.
  10357. Whether GCC actually performs type-based aliasing analysis depends on
  10358. the details of the code. GCC has other ways to determine (in some cases)
  10359. whether objects alias, and if it gets a reliable answer that way, it won't
  10360. fall back on type-based heuristics.
  10361. @c @opindex -fno-strict-aliasing
  10362. The importance of knowing the type-based aliasing rules is not so as
  10363. to ensure that the optimization is done where it would be safe, but so
  10364. as to ensure it is @emph{not} done in a way that would break the
  10365. program. You can turn off type-based aliasing analysis by giving GCC
  10366. the option @option{-fno-strict-aliasing}.
  10367. @node Digraphs
  10368. @appendix Digraphs
  10369. @cindex digraphs
  10370. C accepts aliases for certain characters. Apparently in the 1990s
  10371. some computer systems had trouble inputting these characters, or
  10372. trouble displaying them. These digraphs almost never appear in C
  10373. programs nowadays, but we mention them for completeness.
  10374. @table @samp
  10375. @item <:
  10376. An alias for @samp{[}.
  10377. @item :>
  10378. An alias for @samp{]}.
  10379. @item <%
  10380. An alias for @samp{@{}.
  10381. @item %>
  10382. An alias for @samp{@}}.
  10383. @item %:
  10384. An alias for @samp{#},
  10385. used for preprocessing directives (@pxref{Directives}) and
  10386. macros (@pxref{Macros}).
  10387. @end table
  10388. @node Attributes
  10389. @appendix Attributes in Declarations
  10390. @cindex attributes
  10391. @findex __attribute__
  10392. You can specify certain additional requirements in a declaration, to
  10393. get fine-grained control over code generation, and helpful
  10394. informational messages during compilation. We use a few attributes in
  10395. code examples throughout this manual, including
  10396. @table @code
  10397. @item aligned
  10398. The @code{aligned} attribute specifies a minimum alignment for a
  10399. variable or structure field, measured in bytes:
  10400. @example
  10401. int foo __attribute__ ((aligned (8))) = 0;
  10402. @end example
  10403. @noindent
  10404. This directs GNU C to allocate @code{foo} at an address that is a
  10405. multiple of 8 bytes. However, you can't force an alignment bigger
  10406. than the computer's maximum meaningful alignment.
  10407. @item packed
  10408. The @code{packed} attribute specifies to compact the fields of a
  10409. structure by not leaving gaps between fields. For example,
  10410. @example
  10411. struct __attribute__ ((packed)) bar
  10412. @{
  10413. char a;
  10414. int b;
  10415. @};
  10416. @end example
  10417. @noindent
  10418. allocates the integer field @code{b} at byte 1 in the structure,
  10419. immediately after the character field @code{a}. The packed structure
  10420. is just 5 bytes long (assuming @code{int} is 4 bytes) and its
  10421. alignment is 1, that of @code{char}.
  10422. @item deprecated
  10423. Applicable to both variables and functions, the @code{deprecated}
  10424. attribute tells the compiler to issue a warning if the variable or
  10425. function is ever used in the source file.
  10426. @example
  10427. int old_foo __attribute__ ((deprecated));
  10428. int old_quux () __attribute__ ((deprecated));
  10429. @end example
  10430. @item __noinline__
  10431. The @code{__noinline__} attribute, in a function's declaration or
  10432. definition, specifies never to inline calls to that function. All
  10433. calls to that function, in a compilation unit where it has this
  10434. attribute, will be compiled to invoke the separately compiled
  10435. function. @xref{Inline Function Definitions}.
  10436. @item __noclone__
  10437. The @code{__noclone__} attribute, in a function's declaration or
  10438. definition, specifies never to clone that function. Thus, there will
  10439. be only one compiled version of the function. @xref{Label Value
  10440. Caveats}, for more information about cloning.
  10441. @item always_inline
  10442. The @code{always_inline} attribute, in a function's declaration or
  10443. definition, specifies to inline all calls to that function (unless
  10444. something about the function makes inlining impossible). This applies
  10445. to all calls to that function in a compilation unit where it has this
  10446. attribute. @xref{Inline Function Definitions}.
  10447. @item gnu_inline
  10448. The @code{gnu_inline} attribute, in a function's declaration or
  10449. definition, specifies to handle the @code{inline} keyword the way GNU
  10450. C originally implemented it, many years before ISO C said anything
  10451. about inlining. @xref{Inline Function Definitions}.
  10452. @end table
  10453. For full documentation of attributes, see the GCC manual.
  10454. @xref{Attribute Syntax, Attribute Syntax, System Headers, gcc, Using
  10455. the GNU Compiler Collection}.
  10456. @node Signals
  10457. @appendix Signals
  10458. @cindex signal
  10459. @cindex handler (for signal)
  10460. @cindex @code{SIGSEGV}
  10461. @cindex @code{SIGFPE}
  10462. @cindex @code{SIGBUS}
  10463. Some program operations bring about an error condition called a
  10464. @dfn{signal}. These signals terminate the program, by default.
  10465. There are various different kinds of signals, each with a name. We
  10466. have seen several such error conditions through this manual:
  10467. @table @code
  10468. @item SIGSEGV
  10469. This signal is generated when a program tries to read or write outside
  10470. the memory that is allocated for it, or to write memory that can only
  10471. be read. The name is an abbreviation for ``segmentation violation''.
  10472. @item SIGFPE
  10473. This signal indicates a fatal arithmetic error. The name is an
  10474. abbreviation for ``floating-point exception'', but covers all types of
  10475. arithmetic errors, including division by zero and overflow.
  10476. @item SIGBUS
  10477. This signal is generated when an invalid pointer is dereferenced,
  10478. typically the result of dereferencing an uninitialized pointer. It is
  10479. similar to @code{SIGSEGV}, except that @code{SIGSEGV} indicates
  10480. invalid access to valid memory, while @code{SIGBUS} indicates an
  10481. attempt to access an invalid address.
  10482. @end table
  10483. These kinds of signal allow the program to specify a function as a
  10484. @dfn{signal handler}. When a signal has a handler, it doesn't
  10485. terminate the program; instead it calls the handler.
  10486. There are many other kinds of signal; here we list only those that
  10487. come from run-time errors in C operations. The rest have to do with
  10488. the functioning of the operating system. The GNU C Library Reference
  10489. Manual gives more explanation about signals (@pxref{Program Signal
  10490. Handling, The GNU C Library, , libc, The GNU C Library Reference
  10491. Manual}).
  10492. @node GNU Free Documentation License
  10493. @appendix GNU Free Documentation License
  10494. @include fdl.texi
  10495. @node Symbol Index
  10496. @unnumbered Index of Symbols and Keywords
  10497. @printindex fn
  10498. @node Concept Index
  10499. @unnumbered Concept Index
  10500. @printindex cp
  10501. @bye