Programação Avançada em C
Usando Algoritmos e Estruturas de Dados
Fundamentais
The Ualg Informatics Lab
Universidade do Algarve
http://w3.ualg.pt/~jvo
jvo@ualg.pt
! "#$%& '(')
'* +,-( ./
0 $1" 1#,2,34& "$15 367"-76,8& $&2 9: , ; 67"-<, = #",16>" = &: : &-$ ? 1#6296<%&@A $& B %&@
= &: "#76,3@! "8,8, , =#6,<%& 8" C 2#,$ D "#6>,8,$ E(F G H,3(G ,#, >"# 9: , 7IJ6,
8"$1, 367"-<,K >6$61" 411JLMM7#",16>"7&: : &-$(&#HM367"-$"$M2N@-7@-8ME(FMJ1M &9 "-> 6" 9: , 7,#1, J,#, =#",16>" = &: : &-$K 'O' P"7&-8 P1#""1K P961" Q..K P,- R#,-76$7&K = ,36S&#-6,
Índice
0.
Preâmbulo
1.
Introdução
! " # $ % & # ' (2.
Arrays e Ponteiros
) % * & + , !% -! # ' (
3.
Reserva dinâmica de memória
" # ' (
4.
Primeiras noções de complexidade
' # ' ( ' ) ' * * ) & # ' (
5.
Algoritmos elementares de ordenação
& ! & ) & ) * & ) && # ' (
6.
Mergesort
+ + + + # ' (7.
Quicksort
, , , , , % , ,& ! * ,+ ! ,, # ' (8.
Introdução às listas ligadas
- - - % - # ' (
9.
Primeiras noções de Projecto e de Projecto por
Contrato
# 'IR
! ( & + # ' (10.
As listas ligadas revisitadas
! !% ) & + , # ' (11.
Tipos de dados abstractos
12.
O tipo de dados abstracto pilha
$ ! $ ! ! '% % $ $ & '% !' !' + # ' (
13.
O tipo de dados abstracto fila de espera
$ ! ! ! $ $ # ' (
14.
As filas revisitadas
! ! % # ' (15.
Implementação em C de ADTs com múltiplas
instâncias
& $ & $ ! & ! ! % & # ' (16.
O tipo de dados abstracto polinómio
+ ! + + + # ' (
17.
O ADT matriz esparsa
, , !% ! , , ,
,
*
,&
# ' (
18.
Primeiras noções sobre árvores
- - - - # ' (
19.
Árvores binárias
% !% # ' (20.
Acervos e filas com prioridades
! ! & # ' (
Referências bibliográficas
Referência rápida sobre C
R1.
Preliminares
R2.
Algumas características do C
R3.
Exemplo de programa
R4.
Elementos de sintaxe
! % ! & ! + ! , , , , - ) - ) - ) - ) - ) -& ) -+ -, ) (--
R5.
Controlo de fluxo do programa
& % ! & % & % & % && % ! &+ % '
R6.
Funções
+ + !% + *R7.
Arrays e Matrizes
, * , "R8.
Ponteiros
- - - - -& !%R9.
Sinónimos de tipo
R10.
Estruturas
R11.
Uniões
R12.
Campos de bits
R13.
Enumerações
R14.
Directivas de Pré-processador
R15.
Biblioteca de funções
& & & !% ( ! & !% !Índice Remissivo
0.
Preâmbulo
? $ "$1#919#,$ 8" 8,8&$ S, ": J,#1" 8& 7&-9-1& 8" 7&: J"1-76,$ 7"-1#,6$ 9" 9:
"$1986&$& 8" -S&#: 5167, 8">"#5 ,896#6#(
B , H,3"#6, 8,$ 36-H9,H"-$ 8" J#&H#,: ,<%&K , 36-H9,H": = &79J, 9: 39H,# 8" 8"$1,9"(
$"H9#,: "-1" 9: , 8,$ 36-H9,H"-$ 9$,8,$ ,719,3: "-1" 7&: : ,6$ 1": J& 8" J#&>,$
8,8,$( ? 36-H9,H": = 9: , 36-H9,H": J"9"-,K 8" "$7#61, 7&-8"-$,8,K "1#": ,: "-1"
"S676"-1"K 9" J"#: 61" 9: "3"> ,8& -> "3 8" 6-16: 68,8" 7&: & 7&: J91,8&#( B %& "6$1"
&91#, 36-H9,H": 8" ,31& ->"3 ,719,3 9" J"#: 61, : ,6&# -> "3 8" J#&6: 68,8" 7&: ,
: 596-,K "$J"76,3: "-1" -& 9" $" #"S"#" $ $9,$ 7,J,768,8"$ 8" ,#61: 167, 7&:
J&-1"6#&$( ? 9-1,# , 6$1&K , 36-H9,H": = 1": : "7,-6$: &$ 8" ,2$1#,7<%& $9S676"-1"$
J,#, J"#: 616# 9: , 6: J3": "-1,<%& ,8"9,8, 8" "$1#919#,$ 8" 8,8&$K ,3H=: &$K
J#&H#,: ,$ " : "$: & $6$1": ,$(
? & 7,$,#: &$ "$1#919#,$ 8" 8,8&$ 7&: = -%& "$1,#": &$ , $"# : 961& H6-,6$( 0 $1,
"$7&34, $"H968, -,$ 86$76J36-,$ 8" ? 3H=: &$ " 0 $1#919#,$ 8" D ,8&$ ": : 961& 79#$&$
8" -S&#: 5167, J&# "$1" : 9-8& S&#,K 6-7396-8& ,3H9-$ 8&$ -&$$&$ J#IJ#6&$ 79#$&$ 8"
0-H"-4,#6,(
0 $$"-76,3: "-1" , H6-,368,8" 8& 36>#& "$15 #"3,76&-,8, 7&: , ,2,H": J#,H: 5167,
$"H968, -, ,J#"$"-1,<%& 8,$ : ,1#6,$( ? -&$$, "J"#6-76, ": $,3, 8" ,93,K : &$1#,@-&$
9" & ,$$9-1&K -%& $"-8& 86S763K : 961,$ >" "$ 6-16: 68,16>& J,#, & #"7: @74"H,8&(
? & 3&-H& 8& 36>#& 91636 ,: &$ 9: , 36-H9,H": 73,#,K 8" 3"619#, S5763K $": & S#"9"-1" "
8"$-"7"$$5#6& 17-67&@76"-1S67& ,$$&76,8& , "$1,$ : ,1#6,$( ? J"$,# 8"$1" "$163&
-%& $" J#"$76-8" 8& #6H&# 9" 3">, & 8"$"->&3>6: "-1& 8" J#&H#,: ,$ 8" K
8"$6H-,8,: "-1" -& 9" $" #"S"#" $9, 7&##"7<%&( G,#, J#&$$"H96# "$1" &2"716> &K 7"8&
,J#"$"-1,: &$ -&<"$ 8" J#&"71& " 8" J#&"71& J&# 7&-1#,1& 9" -&$ ,98,: -&
-&$ -,$ "$1#919#,$ 8" 8,8&$ S9-8,: "-1,6$K J,#, 86$7916#: &$ " 639$1#,#: &$ ,$ >5#6,$
,31"#-,16> ,$ $9, 6: J3": "-1,<%& ": = ( C >&39: " ,91&@7&-168&K 6-7396-8&
6-739$6>,: "-1" -& S6-,3 9: , #"S"#-76, #5J68,K : ,$ 7&: J3"1,K $&2#" = (
&8&$ &$ 7&-7"61&$ $%& 8">68,: "-1" 639$1#,8&$( A 1636 ,: &$ "": J3&$ 6-1"-$6> , "
"1"-$6>,: "-1"( R, ": &@3& ,& ->"3 8, 6-1#&89<%& 8" 9: 7&-7"61& : ,$ 1,: 2: 8"J&6$
,& ->"3 8, 6: J3": "-1,<%& ": = ( R, ": &@3& ,6-8, 8" 9: , S&#: , $6$1": 5167, 8"$8" ,
6-1#&89<%& 8&$ "3": "-1&$ S9-8,: "-1,6$ 8, J#&H#,: ,<%&K 7&: & , -&<%& 8" 16J&$ &9 8"
16J&$ 8" 8,8&$ ,2$1#,71&$K ,1 ,&$ 1IJ67&$ : ,6$ ,>,-<,8&$K 7&: & & 8,$ : ,1#6 "$
"$J,#$,$( ? ,2,H": K J,-1&K ": 6-"-1": "-1" J#5167,( G ,#, 9" & 3"61&# J&$$, 6#
S, "-8& , $9, J#IJ#6, ,91&@,> ,36,<%&K 7,8, 7,J193& 1"#: 6-, 7&: 9: 7&-9-1& 8"
""#776&$K 1"-1,16>,: "-1" "-,8&$ J&# ": 7#"$7"-1" 8" 86S67938,8"(
8"-16S67,# " #"J#&89 6# 2&,$ J#5167,$ 8" J#&H#,: ,<%& 1": >6-8& , $"# #"7&-4"768&
7&: & 8,$ ,716>68,8"$ : ,6$ "S67, "$ -& J#&7"$$& 8" "-$6-&M,J#"-86 ,H": 8,
J#&H#,: ,<%&( D " S,71&K J#&H#,: ,# 8, ": 8" 7&: 9-67,#( = &: 9-67,# 7&: ,
: 596-,K : ,$ S9-8,: "-1,3: "-1" 7&: 9-67,# "-1#" J#&H#,: ,8&#"$( P,2"# 3"# 7I86H& "
68"-16S67,# 7I86H& 8" 9" J&$$, $"# #"@91636 ,8& 9,-8& -"7"$$5#6&K 1&#-,@$"
,$$6: 1%& 6: J,-1" 7&: & $,2"# "$7#"> "# 7I86H&(
? & $"H96#: &$ 9: , ,2,H": J#,H: 5167, 9$,-8& 6-1"-$6>, " "1"-$6> ,: "-1" "": J3&$
8" 2&,$ J#5167,$ 8" J#&H#,: ,<%& ": = "$J"#,: &$ "$1,# 7&-1#6296# J,#, & $97"$$& 8"$1"
J#&7"$$& 8" "-$6-&M,J#"-86 ,H": (
? J#&H#,: ,<%& " "$1#919#, 8" 8,8&$ ": = 9: 1": , #"3,16>,: "-1" : ,89#& 7&:
,J367,<"$ ": >,#6,8$$6: ,$ 5#",$ 8, "-H"-4,#6, 6-S&#: 5167,K 8, 6-S&#: 5167, 8" H"$1%&
" 8&$ 7&: J91,8&#"$ ": H"#,3( 0-7&-1#,: &$ ,J367,<"$ 8,$ "$1#919#,$ 8" 8,8&$ ": 5#",$
6-1"#"$$,-1" J,#, 9: , ,: J3, ,986-76,L "-H"-4"6#&$K 6-S&#: 5167&$K "$198,-1"$ "
8&7"-1"$ 9-6> "#$615#6&$K : ,$ 1,: 2: , &91#&$ J#&S6$$6&-,6$ 9" 9"#": ,719,36 ,# &$
$"9$ 7&-4"76: "-1&$ -& : 261& 8" 9: J#&H#,: , 8" ,J#"-86 ,H": ,& 3&-H& 8, > 68,K " ,&
,91&@868,71, 9" 9"# ,J#"-8"# , 8"$"->&3> "# &$ $"9$ J#&H#,: ,$ 8" 9: , S&#: , $6: J3"$
: ,$ $6$1": 5167,( ? &#H,-6 ,<%& 8& 7&-1" 8& 1&#-, & 36>#& ,7"$$> "3 ,& -&>6<& ":
J#&H#,: ,<%& : ,$ 1,: 2: ,& 3"61&# "->&3>68& 45 : 961& -& 8"$"->&3> 6: "-1& 8"
J#&H#,: ,$(
C 1"1& 8& 36>#& "$7#61& ": 6: "$ B " &: ,-( C 7I86H& = "$7#61& ":
Arial Narrow
K 7&: & ":#include <stdio.h>
/* Primeiro programa. */
int main() {
printf(“Olá mundo!”);
return 0;
}
C $ 7&: "-15#6&$ ,& 7I86H& $%& "$7#61& ": GH9$( G&# 9: , 9"$1%& 8" 9-6S&#: 68,8"K
1&8&$ &$ 68"-16S67,8&#"$ -&: "$ 8" >,#65>"6$K S9-<"$K "17( $%& "$7#61&$ ": -H3$(
G,#, ,3: 8&$ "3": "-1&$ H#5S67&$ 8" : &8"3,<%& 7&: & &$ 86,H#,: ,$ 8" "$1#919#, &9
S39&H#,: ,$K 91636 ,@$" , 7,6, 8" 1"1& 7&: 36: 61"$ , J#"1& J,#, 68"-16S67,# &$
? &$ : "9$ ,39-&$ J"3& $"9 "$1: 93& J"#: ,-"-1"(
? &$ 7&3"H,$ 8& D "J,#1,: "-1& 8" 0-H"-4,#6, 03"71#I-67, " -S&#: 5167, 8, R,7938,8" 8"
= 6-76,$ " "7-&3&H6, 8, A -6>"#$68,8" 8& ? 3H,#> " 9" 7&: 6H& 1: 3"776&-,8&
86$76J36-,$K &-8" ,$ : ,1#6,$ ,H&#, ,J#"$"-1,8,$ 1: > 6-8& , $"# 3"776&-,8,$( 0 :
J,#16793,# H&$1,#6, 8" ,H#,8"7"# G ,93, ! "-19#, J"3& $"9 ,J&6& -, "3,2&#,<%& 8,
J#6: "6#, >"#$%& "3"71#I-67, 8&$ ,J&-1,: "-1&$ 8, 86$76J36-, G#&H#,: ,<%& (
"7&-4"<& 6H9,3: "-1" &$ : "6&$ " & ,: 26"-1" J#&J76& #"S3",<%& J#&JL&-,8&$
1.
Introdução
A : ,3H=: & 7&##"$J&-8" , 9: , S&#: , 8" #"$&3> "# 9: J#&23": ,( ,6$ 7&-7#"1,: "-1"K
9: ,3H=: & 9: 7&-9-1& S6-61&$ 8" 6-$1#9<"$ J#"76$,$ " "-,8,$ 9" #"$&3> " 9:
8"1"#: 6-,8& J#&23": ,( A : ,3H=: & 8"> " $,16$S, "# 9: 7&-9-1& 8" J#&J#6"8,8"$
7&: &L
'( D ">" 1"#: 6-,# -9: 7&-9-1& S6-61& 8" J,$$&$ S6-6198"
E( = ,8, J,$$& 1": 9" $"# J#"76$,: "-1" 8"S6-68& 8"S6-6198"
Q( = ,8, J,$$& 8">" $"# $9S676"-1": "-1" $6: J3"$ "S67576,
U
( D ">" #"$&3>"# & J#&23": , ": 1": J& 163 J,#, 9" , $&39<%& J#&89 68, 1"-4,
6-1"#"$$" "S676-76,
F( ": . &9 : ,6$ "-1#,8,$ "
/( ": ' &9 : ,6$ $,8,$(
B , #"$&39<%& 8" J#&23": ,$ 9$,: &$ ,3H=: &$ 79& 8"$": J"-4& J&8" 8"J"-8"# "
: 961& 8,$ "$1#919#,$ 8" 8,8&$ 9" 91636 ,: (
8& -&$$& 6-1"#"$$" 8"$"->&3>"#
,3H=: &$ "S676"-1"$( 961,$ > " "$K , -67, 46JI1"$" 9" 1"#": &$ 8" "-7&-1#,# 9: ,
$&39<%& J,#, 9: 7"#1& J#&23": , "$15 -, 91636 ,<%& 8" ,3H=: &$ "S676"-1"$(
A : , "$1#919#, 8" 8,8&$ -%& : ,6$ 8& 9" 9: , S&#: , 8" &#H,-6 ,# >,3&#"$ #"3,76&-,8&$
" ": 79&$ "": J3&$ $" 6-739": ,$ : ,1#6 "$K ,$ 36$1,$K ,$ 5#>&#"$ " &$ H#,S&$(
B & "$198& 8,$ "$1#919#,$ 8" 8,8&$ , -&<%& 8" 16J&$ 8" 8,8&$ 9: , -&<%& 7"-1#,3(
: -6: & 8" 16J&$ 8" 8,8&$ J#6: 616>&$ &9 J#@8"S6-68&$K ,$$6: 7&: & , 7,J,768,8" 8"
7&-$1#96# -&>&$ 16J&$K &9 16J&$ 8"S6-68&$ J"3& 91636 ,8&#( ,$ ,S6-,3 & 9" 9: 16J& 8"
8,8&$
A : 16J& 8" 8,8&$ 9: 7&-9-1& 8" >,3&#"$ " 9: , 7&3"7<%& 8" &J"#,<"$ $&2#" "$$"$
>,3&#"$(0 ": J3&L C 16J& 8" 8,8&$
int
7&-$6$1" 8& $"H96-1" 7&-9-1& 8" > ,3&#"$ .K 'K @'K EK @EK (((KINT_MAX
KINT
B &-8"INT
MAX
"INT
MIN
#"J#"$"-1,: &$ >,3&#"$ : 56: &$ " : -6: &$ 8&$ 6-1"6#&$( ? $
&J"#,<"$ $&2#" &$
int
$%& : 961,$ " 6-739": &$ &J"#,8&#"$ ,#61: 167&$+, -, *, /
"%
K 1"$1" 8" 6H9,38,8" " 8"$6H9,38,8"K &J"#,<"$ 8" ,1#6296<%&K "17(G&# 9" #, %& , -&<%& 8" 16J& 8" 8,8&$ 1%& 6: J,-1" 0 $$"-76,3: "-1" J&# 1#$
"-$ 8" #, "$(
'( A : 16J& 8" 8,8&$ J#&JL&-, 9: , 6-1"#J#"1,<%& J,#, &$ >,3&#"$ H9,#8,8&$ ":
: ": I#6,
E( ?98,: , &#H,-6 ,# " , 8&79: "-1,# 7&-7"61&$
Q( G&$$626361,: 9" & 7&: J63,8&# ,98" & J#&H#,: ,8&# -, >"#6S67,<%& 8, 7&##"7<%&
8& $"9 7I86H&(
0 $1"$ ,$J"71&$ $"#%& 639$1#,8&$ 8" $"H968,(
! ,: &$ 7&: "<,# J&# >"# & 9" , 36-H9,H": ? B P = S#"9"-1": "-1" #"S"#68, 8,96
J,#, , S#"-1" $6: J3"$: "-1" J&# = @ -&$ &S"#"7" J,#, &#H,-6 ,# " J#&7"$$,# 6-S&#: ,<%&(
0: = K ,$ ,2$1#,7<"$ 8& : 9-8& #",3 $%&K ": 316: , ,-536$"K #"J#"$"-1,8&$ J"3&$ 16J&$
J#6: 616>&$ 8" 8,8&$L
char
Kint, float, double
(0: = K 9$,: &$ 9: - : "#& S6& 8" 261$ J,#, #"J#"$"-1,# - : "#&$( ? $$6: K &$
int
$%& J&# -"7"$$68,8" 6-1"6#&$ 9" >6>": 8"-1#& 8" 36: 61"$ "$J"7S67&$ 9" 8"J"-8": 8& - : "#&$%& #"J#"$"-1,8&$ 9$,-8& ,J"-,$ Q 261$( C $ >,3&#"$ J&$$> "6$ J,#, "$$"$ Q 261$ $%&L
...K ..'K .'.K .''K '..K '.'K ''. " '''
C $ 7&: J63,8&#"$ 8" = 9$,: & $6$1": , 8" -9: "#,<%& 26-5#6& -,19#,3 J,#, #"J#"$"-1,#
6-1"6#&$ $": $6-,3K J"3& 9"L ... . .. ' ' . '. E . '' Q '.. U '. ' F ''. / ''' O
G,-1&K & - : "#& 8" 6-1"6#&$ 86S"#"-1"$ 9" J&8"#": &$ #"J#"$"-1,# 9$,-8& Q 261$
8,8& J&# E *( 0 $$"$ 6-1"6#&$ 7&: "<,: ": . " >%& ,1 E@' O( 0 $1"$ $%& &$ 36: 61"$ 8"
>,#6,<%&K &9 , H,: , 8" >,3&#"$ J&$$> "6$K J,#, 6-1"6#&$ $": $6-,3 8" Q 261$(
P" ": >" 8" Q 261$ 16>"$$": &$ *K "-1%& J&8"#6,: &$ #"J#"$"-1,# EEF/ >,3&#"$
86S"#"-1"$ " , H,: , 8" >,3&#"$ $"#6, 8" . , E@'EFF(
B & 7,$& H"#,3K J,#, 9: 7&: J63,8&# 8"
n
261$K (K J,#, 9: 7&: J63,8&# 9" #"J#"$"-1" 6-1"6#&$ 9$,-8&n
261$K "6$16#%& En
6-1"6#&$ 86S"#"-1"$K $"-8& &$ 36: 61"$ 8" > ,#6,<%& . " En
@'(C 9" >6: &$ >5368& J,#, 6-1"6#&$ $": $6-,3( ? #"J#"$"-1,<%& 8" 6-1"6#&$ 7&: $6-,3
7&-16-9,: , $"# &$ : "$: &$L ...K .. 'K . '.K .''K '..K '. 'K ''. " '''( 0: 7&: J3": "-1&
J,#, EK & 261 : ,6$ "$9"#8, -& - : "#& &9 & 261 : ,6$ $6H-6S67,16>& 9$,8& J,#,
6-867,# & $6-,3 8& - : "#&( P" "$$" 261 S&# . "$1,: &$ -, J#"$"-<, 8" 9: - : "#& J&$616>&
$" "$$" 261 S&# ' 1"#": &$ 9: - : "#& -"H,16> &(
B : "#&$ J&$616>&$ 7&: "<,: J&# . " &$ #"$1,-1"$ 261$ #"J#"$"-1,: & >,3&# ,2$&391& 8&
- : "#&( B : "#&$ -"H,16> &$ 7&: "<,: J&# 'K $"-8& & $"9 > ,3&# ,2$&391& 8,8& J"3,
86S"#"-<, "-1#" , 9,-168,8" 8" - : "#&$ 9" $" 7&-$"H9": #"J#"$"-1,# 7&: & - : "#&
8" 261$ #"$1,-1"$ " & >,3&# ": 26-5#6& -,19#,3 #"J#"$"-1,8& J&# "$$"$ : "$: &$ 261$(
... . .. ' ' . '. E . '' Q '.. @ U@. U '. ' @ U@' Q ''. @ U@E E ''' @ U@Q '
? $$6: K ": 2&#, 9: 6-1"6#& 7&: $6-,3K #"J#"$"-1,8& 9$,-8& Q 261$K 1"-4, ,6-8, E *
>,3&#"$ J&$$>"6$K &$ $"9$ 36: 61"$ 8" >,#6,<%& $%&L @E@U " E@' Q(
P" 16> "#: &$ * 261$ "-1%& , H,: , 8" >,#6,<%& >,6 8" E @'E* , E@' 'EO(
B & 7,$& H"#,3K 9: 6-1"6#& 7&: $6-,3 #"J#"$"-1,8& 9$,-8&
n
261$ 1"#5 En
>,3&#"$ 86S"#"-1"$ " 9: , H,: , 8" >,#6,<%& 9" > ,6 8" E , E@'(A : , : ,-"6#, 8" $,2"#: &$ 9,6$ $%& &$ 36: 61"$ 8" > ,#6,<%& J,#, &$ 6-1"6#&$ -&
7&: J63,8&# 8" = 9" "$1,: &$ , 9$,# #"7&##"# $ : ,7#&$ 8"S6-68,$ -& S674"6#& 8"
7,2"<,34&
limits.h.
G ,#, &$ 6-1"6#&$K "$$"$ 36: 61"$ $%& 8,8&$ J&#LINT
MIN
"INT
MAX
(0: = -"H&76,: &$ "$J,<& " J#"76$%& "$7&34"-8& "-1#"
char, int, long int
" "-1#"float,
double ou long double
(! ,3&#"$ 1J67&$ J,#, ,3H9-$ 16J&$ ": 7&: J63,8&#"$ 8" '/ 261$
# 74,# * 261$ . , EFF $4 " 6-1 '/ 261$ @QE O/* , QE O/O 9-$6H-"8 $4 " 9-$6H-"8 6-1 '/ 261$ . , /F FQF 3&-H QE 261$ @E 'UO U*Q /U* , E 'UO U*Q /UO 9-$6H-"8 3&-H QE 261$ . , U ETU T/O ETF
S3&,1 QE 261$ J#"76$%& O 8H61&$
S&923" /
U
261$ J#"76$%& 'F 8H61&$
3&-H 8&923" *. 261$ J#"76$%& '
T
8H61&$
! 6: &$ J,-1& 9" #"J#"$"-1,<"$ 6H9,6$ $6H-6S67,: K ": H"#,3K 7&6$,$ 86S"#"-1"$ J,#,
16J&$ 86S"#"-1"$( ! ",: &$ ,H&#, 7&: & &$ 16J&$ 8" 8,8&$ -&$ ,98,: , &#H,-6 ,# " ,
8&79: "-1,# 7&-7"61&$(
A : J#&H#,: , &#H,-6 , &$ 8,8&$ #"3,76&-,-8&@&$ 7&: &$ 7&-7"61&$ 8& J#&23": ,
": #"$&39<%&( G&# "": J3&K 9: "861&# H#5S67& &#H,-6 , &$ $"9$ 8,8&$ 8" S&#: , , 9"
#"J#"$"-1": J&-1&$K 36-4,$ " S6H9#,$ H"&: 1#67,$( A : , &#H,-6 ,<%& 8" 8,8&$ 8"$1" 16J&
1&#-, & J#&H#,: , : ,6$ 3"H>"3K K : ,6$ S5763 8" 7&: J#""-8"# " : ,6$ S5763 8" : ,-1"#K
K 8" "$1"-8"# &9 : "34&#,#(
G&# "": J3&K 7&-$68"#": &$ 9" ": >" 8" 6: J3": "-1,#: &$ & "861&# H#5S67& 7&: J3"1&
,J"-,$ -"7"$$5#6& 7,3793,# 86$1-76,$ "97368",-,$ "-1#" J&-1&$ 8& J3,-&(
& !'# $ '% ( ) * * + * * + , -, - . . / / 0
−
+
−
=
1 2 2 342 5 2 35 2 66 4 42 )int main() {
float a, b, c, d;
printf("Abssissa do ponto A:");
scanf("%f", &a);
/* ... */
printf("dist: %f \n", dist(a, b, c, d));
return 0;
}
78dist
2 2 4 72 )double dist(float x, float y, float z, float t) {
float dx = x - y;
float dy = z - t;
return sqrt(dx * dx + dy * dy);
}
2sqrt
( 78 9 7 6math.h
2 2 368 2 2 2 72 2 2 2 78
dist
5 2 66 75 2x
y
3z
t
2 ( 6 4 2 43 4 2 2 42 7 2 2 7 2 66 92 2 6 36 8 6 78 6! "#$"$#% %&#!&%' ()$ %&#$*%' + ,%,) - !.!/"$%0' !/"! ,! "1*) ,12!#!/"! - 3$!
*),!' !# ' %/1*$0%,) 4)' ) $' % $/1,%,!- 5676- 4)' $' /)' ! 4)' $' - ' % 3$! *),!' ) %1/,% #!2!#1# % 4%,% 4)' *)/!/"! 1/,1.1,$%0 ,! $' 4!#") ,%,) *!0) /)' !8 4%,% 4)' *)/!/"! 1/,1.1,$%0 49%' %#!' ) ' !' :#) ,! ,%,) 8 ;/14% )*!#%<=! 3$! ! ">) ,!21/1,% *%#% % ! "#$"$#% >) ) %4! ) % ' !' :#) - % 4?*1% ! % %"#1:$1<>)8 @),!' ) $ %# % ! "#$"$#% *%#% ,!21/1# $' "1*) ,! ,%,) 3$! #!*#! !/"! *)/") A
struct point {
float x;
float y;
};
! ! 4#!. !#A
struct point A, B;
A.x=1.0; A.y=1.0;
B.x=4.0; B.y=2.0;
)' ) % )*!#%<>) ,! 4?*1% ! " ,!21/1,% *%#% % ! "#$"$#% - *),!' ) *% %#
,1#!4"%' !/"! ! "#$"$#% 4)' ) %#&$' !/") ,! 2$/<=! 8 @)# !!' *0)A
double dist(struct point p, struct point q) {
float dx = p.x - q.x;
float dy = p.y - q.y;
return sqrt(dx * dx + dy * dy);
};
0"!#/%"1.%' !/"! *),!#%' ) "!# $ %,) % *%0%.#%49%. !typedef
*%#% 1/2)#' %# ) 4)' *10%,)# ,! 3$! 3$!#%' ) 4#1%# $' /)' ! *%#% ) /).) "1*) ! % 1' 2%4101"%# % ! 4#1"%8typedef
"!' % !&$1/"! 1/"%!Atypedef <tipo elementar ou estruturado> <novo identificador>
!' *0)A
typedef int Number;
) 4% ) ,% ! "#$"$#%
point
"!#1%' )typedef struct { float x, y; } point;
! *)#"%/") *),!#%' ) "!# ! 4#1") % 2$/<>) %/"!#1)# ,% !&$1/"! 2)#' %A
double dist( point p, point q) {
/* … como antes … */
};
)"! ! 3$! )typedef
/>) 1/"#),$ $' /).) "1*) %*!/% $' 1/?/1' ) ()$ $' % %:#!.1%"$#%+ ,! $' "1*) 3$! *),1% !# ! *!41214%,) ,! )$"#% 2)#' %8 *#)&#%' % 4)' *0!") !#1%#include <stdio.h>
#include <math.h>
typedef struct { float x, y; } point;
double dist( point, point);
int main() {
point a,b;
printf("Primeiro ponto:");
scanf("%f", &a.x);
/* ... */
printf("dist: %f \n", dist(a,b));
return 0;
}
double dist(point p, point q) {
float dx= p.x - q.x;
float dy=p.y - q.y;
return sqrt(dx * dx + dy * dy);
};
%"$#%0' !/"! 3$! /%,% /) 1' *!,! ,! %&#$*%# ' !' :#) ,! ,%,) 3$! ! %' !0!
*#?*#1) ! "#$"$#% 8 !' *0)A
typedef struct circle{
point center;
float radius;
} /* ou o que é a mesma coisa: */
typedef struct{
point center;
float radius;
}
}
}
point.
!" # $ %#$ &'"} circle;
!"# ""$ "% !&% #
" &' "(
circle aCircle, bCircle;
aCircle.center.x = 0.0;
aCircle.center.y = 0.0;
aCircle.radius = 1.0;
bCircle = aCircle; /* OK*/
) * !" !"% !& # + (
•
, $ " $ & &% -" ". !"/•
+ $ !"&" & * * 0&# 1 ) 2 $ " $ &#•
!" " '# % 3 ) !"% & '# 456575 8 9:;<= > ! !" " &" 2 "# ? * !* & !" " "% & 3 * % " '#$ 3$ &" *
char
& - .int
& # " !"struct s {
int i,j;
};
&"i
" &"j
#> " &&" &
& %# ? 2$ * $ $ 0 '# ? &$ "# & % "% 3 " # + (
union u {
char lowbyte;
int word;
};
$ *
char lowbyte
$ * "word
& 2" % '# " *$ " &$lowbyte
word
word
$lowbyte
# (union u aUnion;
aUnion.word=0x105; /* 105H = 261D */
printf("%d", aUnion.lowbyte);
> ( 0 * ' + *3 & """ " *lowbyte
$ *highbyte
, " (typedef struct {
unsigned char low, high;
} lhbytes;
typedef union {
lhbytes byte;
int word;
} bytesword;
+ "(bytesword bw;
bw.word =261;
printf("%d %d \n", bw.byte.low, bw.byte.high);
# +"*" *(
bytesword bw;
bw.byte.low =5;
bw.byte.high =1;
printf("%x \n", bw.word);
2( + (•
, "% & * 2$ " " $ ##$ & " "& #•
"% 0 !&" " " &" -"& " " " .# > " * & 2% $ * ! 3 * ! *!"& "! % & && "
& $ & " !&% &&%
&'#
!& "% "$ & "# 455 <:= # " * & " " &%# # " * " & & "& " &%# # + & * " " * " " " % * 0 $ & " ( . " . "" & . " " . ! &. " !. # + & * " " * & # # " " #
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define COMPMAX 128
typedef struct {int letras, espacos, palavras;} contador;
int main() {
char dentro=0, texto[COMPMAX];
int i;
fgets(texto,COMPMAX,stdin);
for (i=0; i<strlen(texto); i++) {
if (!dentro && !isspace(texto[i])) {
dentro=1;
printf("%c",texto[i]);
}
else if (dentro && isspace(texto[i])) {
dentro=0;
}
}
/* printf("\n%d %d %d \n", c.letras, c.espacos, c.palavras); */
return 0;
}
. + * * !2# . ! &" " $ #&. ! &" !*"& - &" . & # ! * " ! 3 !&
2.
Arrays e Ponteiros
&) " " & & "& "
" "
-ℜ
.# 2ℜ
" " "&ℜ
& " "n
&"# * "& -& ".$d
$ " "X
Y
$ "X
" &"(x
1
, x
2
, ..., x
n
)
Y
(y
1
, y
2
, ..., y
n
)
$ * " %(d =
=
−
=
−
+
+
−
+
−
? & "$ & * 3 &% * 0 2 "! % & "# +"*" * &ℜ
$ " "$ && &$ &
ℜ
$ "% 3 &" 0 # * &
2 * *3 "%
n
$ ) * "% " $ 3 ' !"% * & & "&#" % " 2 "
* " "% 0& &( # % 0 " " *
2 %#
,
3 & &% $ $ * % &
)"& "#
+
* & & "& " "
ℜ
# & "ai
& " " ! (int ai[10];
& "ai
" ! (ai[0]
"& 2% " #/* 1ª Versão */
#define N 10
int i;
int ai[N];
ai[0]=1;
for( i=1; i<N; i++) ai[i]=0;
%
#define N 10
int i;
for( ai[0]=1, i=1; i<N; ai[i++]=0);
> "% % " & "# 3$
int ai[]={1,2,3};
int bi[3];
bi=ai;
+ > * !2 % ("
for( i=0; i<3; bi[i]=ai[i++]);
? * * #
+
& $
(
#define N 10
typedef struct { float x, y;} point;
point apoints[N];
apoints[0].x=0.0;
apoints[0].y=0.0;
/* Programa que calcula a distância entre dois pontos de
ℜ
ℜ
ℜ
ℜ
n
*/
#include <stdio.h>
#include <math.h>
#define N 10
double distn(float[], float[], int);
void read(float[], int);
int main() {
float x[N], y[N];
printf(“Ponto X: “);
read(x, N);
printf(“Ponto Y: “);
read(y, N);
printf(“d= %f”, distn(x, y, N));
return 0;
}
double distn(float p[], float q[], int n) {
int i;
float sum = 0.0;
for (i=0; i<n; i++)
sum += (p[i]-q[i])* (p[i]-q[i]);
return sqrt(sum);
}
void read(float a[], int n) {
int i;
for(i=0; i<n; i++) {
printf(“Coordenada %d”, i+1);
scanf(“%f”, &a[i]);
printf(“\n”);
}
}
> 3 " !&" " & " # !&"&
!& & " )"&
"% " " * " # ? $ !& "% !2 !&% )"&# ? 3 $ 3 " & " % - ". " %# +
&""& & " " &
&"& '$ * "&" #
! " " & "
ai
$ & "# " * - . & % ' * & & #int ai[10];
" 3 * & ' > & " " & " "# ,2 * " & * & & " "
%$ "0
sizeof(<tipo>)
"! %# +sizeof
" -" " .size_t
# 7545 9<:= , " - ". & 0 * " 0 # " 0*" & " " " (char c=3;
char *ptr_c;
/*declaração de ponteiro para um char */
*" !2 %
ptr_c=&c;
/*endereço do char c */
"%
ptr_c
!& & ! ? * 3 $ 0 & "!& -" .$ $ - . & 2% - .# ? & " # + " " "3&void*
# * " 0 &"void*
# * &" 0 "ptr_c
$ &*ptr_c
$ & (printf("%c", *ptr_c);
, & 3 * & -* . " " " & " # + "(#define N 10
int ai[N];
ai[2]== *(a+2); /* É sempre verdade */
"0
&
$ " "++
--
$sizeof
*" " 3 " * %# ) *int ai[3];
int bi[3];
/* ... */
bi=ai; /*ERRO*/
*" " & " !"%$ & 0 " " " # ?" & * & $ & % "
* & %$ * " !"% !"#
$ &"
!"% * & & (
int soma( int *, int);
int main() {
int a[]={1,2,3};
int n = sizeof(a) / sizeof (int);
pritnf("%d", soma(a, n));
return 0;
};
int soma( int *x, int n) {
int i, s=0;
for(i=0; i<n; s+=x[i++]);
return s;
& " "& 3 ) * 3 " "
7575 <;<= 9<:= # ? - . "
n
"# > 3 " "3 " !" - 0. " " "& "#int a[3];
int *ptr=&a[1];
ptr+1;
ptr-1;
* " &$ & " & $ "
ptr
3 "& " - & ". ' * !* " " " & - . " " # + 3 "& 2 & % " 0 "# + "char
float
"$ *" ' & 3 & 3 !"# # ? " * "& " 2# + (int a[5], *ptr1, *ptr2, i;
ptr1=&a[1];
ptr2=&a[2];
i = (int) (ptr2-ptr1);
" &%(ptr2-ptr1)
"ptr_diff_t
* 0 !"stddef.h
# # ? & -" &"$>, >=, <, <=, ==, !=
. " * "& " 2## ? !& " " *( ## > " / ## 2 " /
(void *)
"0 /* ou */ NULL
" " < = < : : .int x, *ptr_x, y;
ptr_x=&x;
*ptr_x=0;
y=*ptr_x+3;
.void swap( int *, int *);
int main() {
int i=1, j=2;
printf("i:%d, j:%d \n",i,j);
swap(&i,&j);
printf("i:%d, j:%d \n",i,j);
return 0;
};
void swap( int *x, int *y) {
int tmp;
tmp=*x;
*x=*y;
*y=tmp;
7565 :9= +" 3 &&$ & % 0 &&
"
'\0'
$ & &' " 3& 3 2# ? & " & "char str[]={'a','b','c','\0'};
> &$ 3
& "(
char str[]="abc";
3 "& &"&""& * !2 "$ " "
# 1 "&0 !" &
<string.h>
" "#? $ !"% "
string, strlen
$ * " & " "$ ##$ " " && &" && "# +!"% 0 " ! "(
/* 1ª Versão: usando arrays */
int strlen1( char s[]) {
int len=0;
while (s[len]) len++;
return len;
};
/* 2ª Versão : usando ponteiros */
int strlen2( char *s) {
char *p=s;
while (*p) p++;
*a while (*p !='\0') p++;
return p-s;
};
/*Programa de teste */
int main() {
char str[]="abc";
printf(%d",strlen2(str));
return 0;
}
" % 0 % "
""
char str[]="abc";
/* array */
char *ptr="abc";
/* ponteiro */
? & !" " "$ !" " & ( # " !& & 0 * " $ #$ &
ptr++
# > " 3 & &""# $ 3 &str++
# # " !2ptr="zybaz";
& " 3 ) % " "/ # " " 3 &" "#755 9<:= 9=9<= > " 3 * &
#define N 10
) &const int n=10;
3$ &n
& &"" "# &"" ' " " &""/ * &"&#const int *ppci=&n; /* ppci: ponteiro para constante inteira */
> " &"" " &"" 0 & "& "# > " " &"" "% #
int i=3;
ppci=&
> +i
"% &""$ "% 0 3ppci
# 755 9<:= 9=9<=& * * &""$ " &"" "%
#
& " &"" " ! (
int i =4;
int * const pcpi =&i;
pcpi = &i; /* ERRO */
" " &""# 75 5 9<:= 9=9<= 9=9<= $ " " &"" &""$ "% 3 ) " " " " ' "# & % " 3 ! & (
const int ci=1;
const int * const pcpci=&ci;
0 " " 755 9<:= 9;<= > " ' " & &' !"% 3 " 0 # 3 ! " " !"# & "$ " !" 3 )$ !" " !"# & % " !"% * &!* " " !"%# $ &" !"
min
max
!" & (int min(int a, int b) {
return (a<b ? a : b);
}
int max(int a, int b) {
return (a > b ? a : b) ;
}
, " !"$ ##$ " !" "int
* "int
3 & - "& 2 2. & (int (*pf)(int, int)=0
/*
int *pf(int, int)
3 & % !"% * " "int.
& " && "
pf
" !"% & ) " " &pf
# + & !2 " (pf = max; /* equivalente a pf=&max; */
pf = min;
"&% & !"% 3 " 3 ! "
! (
f() {
int a, b;
a=min(10,20); /* Chamada directa à função */
b=pf(10,20); /* Chamada à função através do seu ponteiro, */
/* e a abreviação de (*pf) (10,20); */
};
"% " !"% " )& " 3( 0& " !" + &$ * " & " # &%* & $ $ &"
!"% & # "& !" * "
3 !"% * & # > * "
0 $ )" $ $ " % "
" * & &# + 2 &
&' &" !"% " )" $ & &
0
$ & & " & & $
& "& !"% * & & " " -".
!"% & " # + " 3 " & 0 )" #
include <stdio.h>
int min(int, int);
int max(int, int);
int calcula(int (*) (int, int), int*, const int);
int main() {
int a[]={1, 2, 3};
const int n = sizeof(a) / sizeof(int);
int maximo, minimo;
maximo = calcula(max, a, n);
minimo = calcula(min, a, n);
printf("min: %d - max: %d \n", minimo, maximo);
return 0;
};
int calcula(int (*pf) (int, int), int *a, const int n) {
int i, res=a[0];
for (i=1; i<n; i++) res = pf(res, a[i]);
return res;
&'# 1
- . !0& "& !"% " &
!"%
calcula
* !"% )" $ 0 $ &# 755 9<:= <== ? & 3 " 2->
$ & " #typedef struct { float x, y; } point;
point p, *pp;
p.x=0.0;
pp=&p;
pp->y=0.0;
" % ! *((*pp).y=0.0;
755 = < 9<:= ! * " 3 & (int ai[5];
3 * " " 3 & & (int*api[5];
+"% & 0 & % ! + "*
api
" "/ "% ' "# 2% "$ !"% * " &""“false”
n
! " &""“true”
& &"0#const char* bool (int n) {
static const char * names[]={"false", "true"};
return (n? names[1]: names[0]);
};
> " " )# ""$ "%
& !& " !&0 & "# "%
( !"% & ! * " " & " )"& " " # "$ " " " 2 # &
static
0names
#static
$ 0 & " ")& & -& % "" & ." " ! &#
static
$ 0 & !"% "& *" !"% 3 & 2 " " !
&% $ " &" " & !"%#
""$ 0 &")" & $ $ "% 0 &) ! !"% " 3 !"# 7545 <9= 9 :9 *" " * & &" !" " "# , & * 3 "(
* * !& $ & (
> fact 3
6
+ & " 3 &" &" " !"%
main
# " !"%main
3 & $ 3 & & "(#
argc
* &"3 " " & * 3 & -* 3 " ' .# $ " &argc
# #argv
* 3 "$ " " " &0 " " &" &"" "& * ! & # & $
argv[0]
""fact"
argv[1]
""3"
- $ " /"% " .#$ * & * & " &% -"
stdout
&. ' " " & * ! & $&(
int main(int argc, char *argv[]) {
int i;
printf(“O meu nome e os argumentos com que foi chamado são: \n”);
for(int i=0; i < argc; i++) printf(" %s \n",argv[i]);
return 0;
};
* !" " &" !2
<:=
#
& " & " "$ &" !"% * !*
( . + " &# . *"& " 3 " "'"$ $ " &&" " &&"# # + & "%
printf
* 2 % & " *# 0 2 !* &" "#2.1.
int *pi=0, i=1;
2.2.
int a[]={1, 2, 3};
int pi=&i;
int *pi = a;
*pi += i;
pi++;
++i;
*pi;
i
# , 2 $ !" " $ &"!"% * " &"& && " 3&# ?
$ &' " 0 &( # + & &'
!&"# # " !"% & " *% "$ " !"% "
# + !"% " &"& && *
"% " !"% & " " !"%
#
" "# ? $ &' " 0 ( # , 2 $ !" " $ " !" * &" " && " "# ? $ ! " " !" $ &' " 0 ) #
3.
Reserva dinâmica de memória
0 * " " " &# ? 3 $ 0 3!" * "% #? $−
0 - $ 0 & ! * * !"% * & * * !"%. * % & 3 ! # !" * 3 * "% 0 /−
0 & -##$ * % !" " ")& & ". "*" & &%/−
0 0& % & " * !"% !" 3 ! # ? & 0 * "& " " * " # % " " 0 * &" 0 * " 3 " 0 $ 3 2 3 "# &% 0 " 3 ! " -&%. " & '# " & ' 3 ! &
0 &% & &" '
# ? 2 0 % & 0 " &#
> &' " !2 " & ' "& "#
int *pi;
pi = (int *) malloc(sizeof * pi);
*sizeof
*" "&0 ' " " &' """ " %# > " 2 " &stdlib
!" " & '$ & ' " # > ' !"% 3 & " "$ " * " 3 &") " "#void *malloc( size_t n);
void *calloc( size_t , size_t);
!"%
malloc
" " & ' "% "& 2 "%n
$NULL
! # & $ "pi
* &" $0 *malloc
"void*
# " " ) &(int *pi;
pi = (int *) calloc(1, sizeof * pi);
!"%
calloc
& "& 2 ' * # &malloc
3 "0NULL
! # ' ") $ * $ 3 !"# ? $ ' % ! # ' & "' !& &" $ !&$ ' "$
& (
if( pi == 0) {
exit(1);
}
" " & " & '$ * !2 "
& "0 &' & (
% " ) $ & !& & 3
stdio.h
$ " & !& ( #stdout
$ % $ " ) % *$ %$ 3 &%# #stdin
$ % $ " " % *$ %$ 3 & # #stderr
$ % $ 3 &%/ $ "% &&"# ) * &printf("abc");
* "
fprintf(stdout,"abc");
int i;
scanf("%d", &i);
* "fscanf(stdin,"%d", &i);
printf
" "" $ & & "% "! " ) ! &&"!& $ #
, "