C/Variables: Diferenzas entre revisións

Contido eliminado Contido engadido
Gallaecio (conversa | contribucións)
Traduzo dende en:C Programming outro pouco
Gallaecio (conversa | contribucións)
Traduzo dende en:C Programming un anaco máis e marcho polo de agora
Liña 87:
Nesta declaración declaramos tres variables: <code>NumeroDePaxinas</code>, <code>i</code> e <code>l</code>, séndolle a éste último asignado o literal <font style="color:#ff48ff">5</font>.
 
===The <code>char</code> type===
TheAs variables de tipo <code>char</code> typepoden ister capablecomo ofvalor holdingcalquera any member of the execution character setcarácter. ItAlmacena storeso themesmo sametipo kindde ofdatos dataca as anun <code>int</code> (i.e. integers), butpero alwayssempre hasten a size of oneun byte. The size of a byte is specified by the macro <code>CHAR_BIT</code> which specifies the number of(8 bits in a char (byte). Inde standard C it never can be less than 8 bitstamaño. AUnha variable ofde typetipo <code>char</code> isúsase mosthabitualmente oftenpara usedalmacenar todatos storede charactercarácteres. data,A hencemaioría its name.das Mostveces implementationsutilízace useo thesistema [[w:ASCII|ASCII]] characterpara setos as the execution character setcarácteres, but it's best not to know or care aboutpero thatdisto unlessnon thehai actualque valuespreocuparse arede importantmomento.
 
ExamplesExemplos ofde charactercarácteres literalsliterais areson '"a'", '"b'", '"1'", etc., asasí wellcoma asalgúns somecarácteres specialespeciais characterscoma such as '"<code>\0</code>'", (theo nullcaracter character)nulo, ande '"<code>\n</code>' (newline, recall "Hello, World"). Note that the <code>char</code> value must be enclosedo withinsalto singlede quotationsliña.
 
Cando inicializamos unha variable de caracter, podemos facelo de dúas maneiras. A primeira é a favorita, e a segunta considérase un mal xeito de programar.
When we initialize a character variable, we can do it two ways. One is preferred, the other way is '''''bad''''' programming practice.
 
'''<font style="color:#1b991b">char</font>''' letra1 = <font style="color:#ff48ff">'a'</font>;
The first way is to write
<source lang=c>
char letter1 = 'a';
</source>
 
Isto é "boa" programación xa que permite que calquera que lea o código vexa que <code>letra1</code> inicialízace coa letra <code>'a'</code>.
This is ''good'' programming practice in that it allows a person reading your code to understand that letter is being initialized with the letter 'a' to start off with.
 
A segunda forma, que non debería de usarse, é
The second way, which should ''not'' be used when you are coding letter characters, is to write
<source lang=c>
char letter2 = 97; /* in ASCII, 97 = 'a' */
</source>
 
'''<font style="color:#1b991b">char</font>''' letra2 = <font style="color:#ff48ff">97</font>; <font style="color:blue">// En ASCII, "a" equivale a 97</font>
This is considered by some to be extremely '''''bad''''' practice, if we are using it to store a character, not a small number, in that if someone reads your code, most readers are forced to look up what character corresponds with the number 97 in the encoding scheme. In the end, <code>letter1</code> and <code>letter2</code> store both the same thing -- the letter "a", but the first method is clearer, easier to debug, and much more straightforward.
 
Algúns considérano unha moi mala práctica de programación, se estamos a usalo para almacenar un caracter, non un número pequeno, xa que se alguén le o código, verase na obriga de buscar o caracter que equivale ao número 97. A fin de contas, <code>letra1</code> e <code>letra2</code> veñen a ser o mesmo, pero é moito máis recomendable usar a primeira forma.
One important thing to mention is that characters for numerals are represented differently from their corresponding number, i.e. '1' is not equal to 1.
 
Hai que destacar que os carácteres de números represéntanse de xeito diferente aos propios números. Non é o mesmo <code>'1'</code> (caracter) que <code>1</code>.
 
There is one more kind of literal that needs to be explained in connection with chars: the '''string literal'''. A string is a series of characters, usually intended to be displayed. They are surrounded by double quotes (" ", not ' '). An example of a string literal is the "Hello, world!\n" in the "Hello, World" example.