C/Variables: Diferenzas entre revisións

Contido eliminado Contido engadido
Gallaecio (conversa | contribucións)
Liña 234:
</source>
 
A variable <tt>g</tt> é accesible a través de <tt>arriba</tt> e <tt>abaixo</tt> e mantén o seu valor. As variables <tt>l</tt> tamén manteñen o seu valor, pero hai dúas variables distintas en cada un dos ámbitos. As variables estáticas son un bo xeito de utilizar ''encapsulación'', un termo sacado do modo de pensar orientado a obxectos que ben significando non admitir a realización de cambios nunha variable agás por chamadas de funcións.
The j var is accessible by both up and down and retains its value. the k vars also retain their value, but they are two different variables in each their scopes. static vars are a good way to implement encapsulation, a term from the object-oriented way of thinking that effectively means not allowing changes to be made to a variable except through function calls.
 
O programa producirá a seguinte saída:
Running the program above will produce the following output:
<source lang=c>
upChamouse a arriba() called. kl= 1, jg= 1
upChamouse a arriba() called. kl= 2, jg= 2
upChamouse a arriba() called. kl= 3, jg= 3
downChamouse a abaixo() called. kl= -1, jg= 2
downChamouse a abaixo() called. kl= -2, jg= 1
</source>
<tt>'''extern'''</tt> isúsase usedcando whenun a file needsficheiro tonecesita accessacceder a unha variable innoutro anotherficheiro fileque thatpodería itnon mayestar not haveincluída (<tt>#includedinclude</tt>) directlydirectamente. Therefore,Pero en realidade <tt>''extern''</tt> doesnon notcrea actuallyespazo carvepara outunha space for a newnova variable, itsenón justque providesfornece theao compilercompilador withde sufficientinformación informationabondo topara accessacceder the remoteá variable remota.
 
'''<tt>volatile</tt>''' isé aun specialtipo typede modifiermodificador whichespecial informsque theinforma compilerao thatcompilador thede valueque ofo thevalor da variable may be changedpodería bycambiarse externalpor entitiesentidades otherexternas thanalleas theao programpropio itselfprograma. ThisIsto isé necessarynecesario forpara certaincertos programsprogramas compiledcompilados withcon optimizationsoptimizacións -se ifnon ase variabledefinise wereunha notvariable definedcomo <tt>volatile</tt> theno thecompilador compilerpodería mayasumir assumeque thatcertas certainoperacións operationsrelativas involving theá variable arepódense safeoptimizar tocando optimizeen awayrealidade whennon in fact they aren'tpoden. ''<tt>volatile''</tt> isé particularlyparticularmente relevantrelevante whenao workingtraballarmos withcon embeddedsistemas systemsintegrados (wherenos aque programo mayprograma notpodería havenon completeter un control oftotal adunha variable) ande multi-threadedaplicacións applicationsmultifío.
 
<tt>'''auto'''</tt> é un modificador que especifica unha variable "automática" que se crea automaticamente ao estar no ámbito e se destrúe ao saír. Se pensas que isto semella o que levas facendo todo o tempo ao declarar unha variable, estás no certo: tódolos elementos dentro dun bloque son de maneira implícita "automáticos". Por esta razón, a palabra clave <tt>auto</tt> é máis a resposta a unha pregunta trivial que un modificador útil, e existen unha morea de programadores moi competentes que nin sequera están ao tanto da súa existencia.
<tt>'''auto'''</tt> is a modifier which specifies an "automatic" variable that is automatically created when in scope and destroyed when out of scope. If you think this sounds like pretty much what you've been doing all along when you declare a variable, you're right: all declared items within a block are implicitly "automatic". For this reason, the ''auto'' keyword is more like the answer to a trivia question than a useful modifier, and there are lots of very competent programmers that are unaware of its existence.
 
'''<tt>register</tt>''' é unha chamada ao compilador para que intente optimizar o almacenamento da variable dada almacenándoa nun rexistro do procesador da computadora ao iniciar o programa. A maioría dos compiladores con optimización fano de todos xeitos, polo que o uso desta palabra clave non adoita ser necesario. De feito, ANSI C di que un compilador pode ignorar esta palabra clave se quere -e moitos fano. O Visual C++ de Microsoft é un exemplo dunha implementación que ignora completamente a clave <tt>register</tt>.
'''<tt>register</tt>''' is a hint to the compiler to attempt to optimize the storage of the given variable by storing it in a register of the computer's CPU when the program is run. Most optimizing compilers do this anyway, so use of this keyword is often unnecessary. In fact, ANSI C states that a compiler can ignore this keyword if it so desires -- and many do. Microsoft Visual C++ is an example of an implementation that completely ignores the ''register'' keyword.
 
 
===Concepts===
* [[Programming:Key concepts in programming|Concepts]]
** [[Programming:Variables|Variables]]
*** [[Programming:Types|Types]]
** [[Programming:Data_Structures|Data_Structures]]
*** [[Programming:Data_Structures:Arrays|Arrays]]
 
===In this section===
*[[C Programming/Variables|C variables]]
**[[C Programming/Types|C types]]
**[[C Programming/Arrays|C arrays]]
 
** [[Programmingen:Variables|C Programming/Variables]]
[[et:Programmeerimiskeel C/Muutujad]]
[[it:C/Variabili, operatori e costanti/Variabili]]