Algunas normas de estilo para programar en C

Anuncio
Tabla de Contenidos
! "
#
!
$
!
!
% &
' &
$
(
%
)*
%
+
,
+
++ --
&
.
'
-
"
'
)
)
1. Introducción
"
$
#
%
#
&
$
*#
+
-
%*
#
#
#.
+
( #
#
!
#
*.
/
#
!
'( )
+
!
, !,
'
&
#
!
*
#
#
*
(
)
+ #
# #
#
& %#
#
#
#
()
#
0
0
#
-+
#,
) 1
2. El fichero fuente
2
* +
• 4
!
#
!
*
()
%
*
• 6
*
*
) #
*
*
#
3 #
"
#
#
!
(
(
2
+
2
*
!
#
$!
.h'(
*
#
$
, #
main $
2
2
(
!
* #
*
#
*
#
!
5
+
3
() + #
'
!
•
,
(
!
!
•
(+
!
#include
')
# #
&
# #
$stdio.h( stdlib.h(
• 5
#
%
* &
"
#
• -
*
3
$+
'( )
!*.
"
#
, #
!
'
(
#
#
7
+
*
()
/*
**
**
**
**
**
**
**
**
*/
Fichero:
Autor:
Fecha:
demostraciones.c
Fulanito Menganito Blas
21-12-99
Descripción:
conjunto de funciones de
demostraciones matemáticas.
/* Includes del sistema */
#include <stdio.h>
/* Includes de la aplicación */
#include "ejemplos.h"
/* constantes */
#define MAXIMO
100
#define ERROR
"Opción no permitida"
/* tipos definidos por el usuario */
/* Prototipo de funciones locales */
int main()
{
return 0;
}
/* Definiciones de funciones locales */
3. Sangrado y separaciones
"
*
*
"
#
* +
,
*
,
&
$
* +
#
%
0
8
8
( #
!/
' 9 #
3
if (correcto)
printf("no hubo problemas");
else
printf("SI hubo problemas");
&%
if (correcto) printf("no hubo problemas");
else printf("SI hubo problemas");
:
!
* +
*
#
#
*
3
#include <stdio.h>
int main()
{
int num=0;
int contador=0;
int impares=0;
int positivos=0;
int resp;
/*
/*
/*
/*
/*
número a leer de teclado */
contador total */
contador de impares */
contador de positivos */
respuesta del usuario */
/* Separamos definiciones de código */
do
{
printf("\nIntroduzca un número: ");
scanf("%d", &num);
contador++;
if (num>0)
positivos++;
if (num%2)
impares++;
while (getchar()!='\n')
/* leer hasta */
;
/* nueva_linea */
printf("Terminar (S/N)?");
resp = getchar();
}
while (resp!='S');
printf("Numeros: %d\t Pares: %d\t Positivos: %d\n",
contador, contador - impares, positivos);
return 0;
}
< )
%
%
#
# #
&
* +
(*
&
# #
(
"
3
control
{
sentencia;
sentencia;
}
) +
&
# *
#
#
=>? 3
control {
sentencia;
sentencia;
}
5
2#
( *
*
%
#
(
*
#
+ +
&
3
/* Incorrecto: ¡No demasiado legible! */
while ( cond1 != "error" && cond2 !=TRUE &&
cond3 >=
7.5 && cond4 <=10 && cond5 !="esto es verdad")
@
,
#
3
/* correcto */
while (cond1 != "error" && cond2 != TRUE &&
cond3 >= 7.5 && cond4 <= 10 &&
cond5 != "esto es verdad")
;
: *
+
#
* +
2 (
%
/ # (
!
#
!
()
*
) * #
*
)
A
# *
*
*
# !
&
!/
%
4. Comentarios
A
#
* &
!
+
#
%
"
()
* #
*
+
.
&
5
* +
#
# &
)
&
/*
** bucle que mientras el depósito no está lleno aumenta
** el caudal siguiendo las tablas de Leibniz-Kirchoff
*/
while(nolleno)
{
?
}
4
2
+
5
*,
#
2
< )+
!,
&
3
i++;
/* Se autoincrementa el valor de i en una unidad */
!
!
* #
!
!
"
*
2#
!
* &
!
#
# ,
#
) &
!
&
!
#
*
!
!
(
A
!
5. Identificadores y Constantes
"
* &
$_'( ) +
?
+
,
!
+
&
!
3
,
* )
#
*
#
velocidad = espacio/tiempo;
&%
resultado = valor/aux2;
&
&
+
!
-
,
"
&
*
*
!
!
!
(#
* (
!
!
%
#
& #
( )
!
!
-
!
)
!
!
&
* (
*
#
*
!
#
# (
)
)
3
int trayecto[100];
int origen;
int destino;
&
!
+
&
!
(
!
&
.#
/* comentario sobre trayecto */
/* comentario sobre origen
*/
/* comentario sobre destino */
+ #
)
#
(
#
#
)# * *
**
!
%
!
&
&
%+
%
$='
#
*
$=='
* #
#
if ( 4 == a )
3
if ( a == 4 )
A
*
A !
(
*
*
7
,
&
B -CD"59A-"
* ( #
A
!
&
#
(
)7
(
+
)
#
)7
(
&
.
EE 7
+
A
#
.
*
,
*
,
*
!
!
)
)
+ &
3
#define CERO
#define CERO
#define A
!
)
(
8
8
(+
(
*
#
*
int
*pi;
%
int
48
'0'
'A'
*
/* INCORRECTO */
/* CORRECTO */
*
&
*
&%
#
!
x, y, *z;
(
.
*
(
3
struct barco
{
int flotacion;
int tipo;
.
float precio;
};
/* en metros */
/* según clasif. internacional */
/* en dólares */
*
)
6. Programación Modular y Estructurada
"
*
F
%
+
*
#
goto continue3
!
!
$
%
"
&'
#
") *
"(
) %
+
#
,! -
A
4
+
break
#
switch
!
* ,
7
& ()
- +
#,
7
#
!
2
(#
*
&
!
#
$
,(
-
*
*
, &
7
* ,
#
!
!
+
!
(
return'( +
!
*
,
+
* #
#
!
*
!
.
7 # (
& &
!
#
,
!
!
%
+
&
(
"
"
#
void
A
,
/ !
(
"
(
!
#
!
*
+
#
7. Sentencias Compuestas
A
!
#
& (#
#
+.
#
+ &
& < )&
A # #
(
#
!
) &
3
control
{
sentencia;
sentencia;
}
"
#
*
* +
#
7.1
A
*
!
& (
*
do-while
#
*
switch
+
*
!
()
()
&
#
"
*
#
*
break( )
)
#
0
0
3
switch (expresion_entera)
{
case ETIQ_1:
/* igual que ETIQ_2 */
case ETIQ_2:
sentencia;
break;
case ETIQ_3:
sentencia;
break;
}
7.2
if-else
"
+
&)
else $
A
%+
* %
#
&
+
(#
(
+
,
else
#
* * %
if-if-
if'
*
if-else if-else if
+
!/
#
!
&
else if
3
, +
if (expresion)
{
...
}
else if (expr2)
{
...
}
else if (expr3)
{
...
}
else
{
...
}
8. Otros
# ! *
%
6 ?:-: ?G
%
!
)
#
()
# &
+
&
( ) +
(
)
#
!
+ FALSO )
3
if (VERDADERO == func())
*
!
if (FALSO != func())
A
**
*
&
d = (a = b + c) + r;
) +
/* Incorrecto */
,
!
(
* (
!
**
+
,
(#
2#
# #
2#
(
+
& &
7
3
while (EOF != (c = getchar()))
{
/* procesa el carácter */
}
9
%
#
#
#
* (
$&
++ )--( ) + #
#
#
/ #
!
!
,
(
* /'
9. Organización en ficheros
#
,
*
#
*
7
#
"
!
*
()
) 7
7
!
' A
!
/ 5
!
*
!
!
!/ .c
.c $
*
.h $
# '
*
&
A !
!
,
.c FG
*
!
10. Ficheros de cabeceras
5
!
!
( #
%
4
!
2
.h A
!
'
#
!
#
)
*
#
#include <stdio.h>'
5
#
#
#
#
#
!
+
!
#
!
+ &)
!
* (#
*
$#
#
&
%
!
!
.c )
!
stdio.h( string.h(
(#
#
!
$#
/ #
*
- * !
#
#
(
*
.h
5
&
!
•
*
!
•
*
*
*
(
*
*
3
#
#
&
9
A !
!
!
#
!
*/
*
# *
!
$
!
# ( ) +
!
.c
*
+
!
#
)
!
#
!
& * :
# * +
#
.h'
F
*
!
&
*
H
F
*
!
# #
+
!
*
cuenta_palabras.h
3
*
+
#
&
*
+
!
/ # (
#
!
*
%
#
&
!
*
cuenta_palabras(
*
*
+
#ifndef CUENTA_PALABRAS_H
#define CUENTA_PALABRAS_H
void cuenta_palabras(int argc,char **argv,
int *palabras,int *total);
#endif
:
+
+
(
&
$CUENTA_PALABRAS_H' )
, !
!
!
$
, #endif'
+
)
"
*
!
!
(
!
#
+
.h(
(
*
!
!
$
,
# *
+
,
#
#
# # !
.c
*
!
(
# #
+
*
!
!
#
'
!
!
" 2
,
11. La sentencia exit
2#
#
C #
*
C
?
(#
+
#
*
*
)*
2#
*
(
!
!
!
+.
, /
+
# #
#
#
C #
#
& +
3
#
#
%
!/
0
#
!
# &
( )#
exitK
/
(
3
1
5
7
%
#
/
J
(
#
& &
#
#
+
&
()
+
exit
12. Los operadores ++ y --.
B
3
I
/*
** Operador de incremento
*/
#include <stdio.h>
int main()
{
int i = 7;
printf("%d \n", i++ * i++);
printf("%d \n", i);
return 0;
}
< )+
&
& *
# .
+
# #
#
#
2#
"
# .0
0
+ LL # &
()
,
A
*
/
7*7(
0
/
!
"
#
#
7*8
0
#
%
) +
#
%
*
&
!
#
13. Orden de Evaluación
2#
3
f() + g() * h()
#
+
#
%
+
(#
+.
!
( J+ .
#
&
&& ) ||K "
%+
2 #
#
()
3
while (EOF != (c = getchar()) && '\n' != c)
-
,(
&
2#
#
!
#
#
#
!
*
#
(
2#
/ # (
p & NULL $) +
&
+
& &
/
&
(
#
!
comprueba'3
if (NULL != p && comprueba(*p))
{
...
}
14. De nuevo los Prototipos
-#
(#
% (
!
&
(
#
+
( +
)
# %
/ # 3
&
#
#
(
/*
** Demostración de error debido a falta de prototipo.
** En este caso, al no existir prototipo, el
**
compilador supone que la funcion multip devuelve
**
un valor del tipo int.
*/
#include <stdio.h>
int main()
{
double prod;
int res;
prod=multip(7.3, 3.5);
printf("\n Prod= %f \n", prod);
return 0;
}
A !
multip
,
!
!
!
3
double multip(double a, double b)
{
return a*b;
}
"
#
)/
3
%> gcc proto.c proto_aux.c
%> a.out
" #
* & +
#
#
3
%> gcc -Wall proto.c proto_aux.c
"
* & +
#
!
# *
*
#
Descargar