C/Estrutura e estilo: Diferenzas entre revisións

Contido eliminado Contido engadido
Gallaecio (conversa | contribucións)
Gallaecio (conversa | contribucións)
Liña 78:
Isto fará o código móito máis fácil de ler que antes. Pero aínda pode ser máis lexible.
 
=== Indentation Tabulacións===
Aínda que engadir saltos de liña entre bloques de código importantes pode facer o código máis lexible, non dá unha idea do fluxo do programa. O uso do tabulador pode sermos moi útil nestes momentos: as tabulacións separan visualmente os camiños de código ao mover os puntos onde estes comezan a unha nova columna na liña. Esta práctica tan sinxela fará moito máis doado ler o código. As tabulacións seguen unha regra moi simple:
 
*Todo o código que estea nun novo camiño (por exemplo, entre as dúas chaves "{" e "}") contará cunha tabulación máis ca o camiño anterior.
Although adding simple line breaks between key blocks of code can make code marginally easier to read, it provides no gauge of the flow of the program. The use of your tab key can be very useful now: indentation visually separates paths of code by moving their starting points to a new column in the line. This simple practice will make it much easier to read code. Indentation follows a fairly simple rule:
 
Así que apliquémolo ao código co que estamos a traballar:
*All code inside a new path (i.e. Between the two '{' brackets '}') should be indented by one tab more than the code in the previous path.
 
<font style="color:#bc5ff8">#include</font> <font style="color:#ff48ff"><stdio.h></font>
So, based on our code from the previous section, there are two paths that require indentation:
'''<font style="color:#1b991b">int</font>''' main('''<font style="color:#1b991b">void</font>''')
{
'''<font style="color:#1b991b">int</font>''' i=<font style="color:#ff48ff">0</font>;
printf(<font style="color:#ff48ff">"Ola mundo!</font><font style="color:#ff48ff">"</font>);
'''<font style="color:#bb2323">for</font>''' (i=<font style="color:#ff48ff">0</font>; i<<font style="color:#ff48ff">1</font>; i++)
{
printf(<font style="color:#ff48ff">"</font><font style="color:#a7a0d7">\n</font><font style="color:#ff48ff">"</font>);
'''<font style="color:#bb2323">break</font>''';
}
'''<font style="color:#bb2323">return</font>''' <font style="color:#ff48ff">0</font>;
}
 
Obviamente agora é perfectamente comprensible a primeira vista que camiño van dentro de cales. O uso de tabulacións pode ser moi importante.
*Lines 40 to 100
*Lines 70 and 80
 
É preciso comentar que algúns editores de texto xa automáticamente realizan as tabulacións ao premer na tecla intro.
10 #include <stdio.h>
11
20 int main(void)
30 {
40 int i=0;
41
50 printf("Hello, World!");
51
60 for (i=0; i<1; i++)
61 {
70 printf("\n");
80 break;
90 }
91
100 return 0;
110 }
120
 
It is now fairly obvious as to which parts of the program fit inside which paths of code. You can tell which parts of the program will loop, and which ones will not. Although it might not be immediately noticeable, once many nested loops and paths get added to the structure of the program, the use of indentation can be very important.
 
NOTE: Many text editors automatically indent appropriately when you hit the enter/return key.
 
== Comments ==