Você está na página 1de 104

Lgica e Programao

Prof.
Me

l icad

Marcos

iane

ira

Fischborn
V

.2

 

CAPITULO 1 - SISTEMAS DE NUMERAO.......................................................................................................7


1.1 Sistemas analgicos e digitais ............................................................................................................7
1.2 Necessidade de representao de nmeros em outras bases: ............................................................8
1.2.1 Numerao posicional e numerao no posicional.................................................................................. 9

1.3 Principais tipos de bases numricas.................................................................................................12


1.3.1 Sistema Decimal .....................................................................................................................................12
1.3.2 Sistema Binrio ......................................................................................................................................12
1.3.3 Sistema Octal..........................................................................................................................................13
1.3.4 Sistema Hexadecimal..............................................................................................................................13

1.4 Tabela de equivalncia entre bases..................................................................................................14


1.5 Converses de Base..........................................................................................................................14
1.5.1 Decimal inteiro para base r .....................................................................................................................14
1.5.2 Converso de frao decimal para base r.................................................................................................15
1.5.3 Converso entre um nmero decimal com parte inteira e parte fracionria para um nmero na base r......15
1.5.4 Converso de base r para base decimal....................................................................................................16
1.5.5 Converso da base binria para a base 2 K ................................................................................................16
1.5.6 Converso da base 2 K para a base binria................................................................................................17
1.5.7 Mapa de converso .................................................................................................................................17

1.6 Unidades de Medida.........................................................................................................................18


1.7 Exerccios:........................................................................................................................................18
CAPTULO 2 - LGEBRA DE BOOLE. .............................................................................................................19
2.1 Introduo ........................................................................................................................................19
2.2 Operao E.......................................................................................................................................19
2.3 Operao OU ...................................................................................................................................20
2.4 Operao NO .................................................................................................................................20
2.5 Postulados ........................................................................................................................................20
2.6 Elementos de relaes lgicas..........................................................................................................21
2.7 Teoremas e relaes lgicas.............................................................................................................22
2.8 Funes Booleanas...........................................................................................................................24
2.9 Tabelas verdade................................................................................................................................24
2.10 Minitermos......................................................................................................................................25
2.11 Tabelas verdade aplicadas algoritmos ........................................................................................26
2.12 Exerccios resolvidos......................................................................................................................28
2.13 Exerccios propostos ......................................................................................................................29
CAPTULO 3 PROGRAMAO ESTRUTURADA EM C. ................................................................................31
3.1 Conceitos de Programao Estruturada ..........................................................................................31
3.1.1 Definies ..............................................................................................................................................31
3.1.2 Objetivos ................................................................................................................................................31
3.1.3 Propriedades ...........................................................................................................................................31
3.1.4 Estruturas Bsicas de Controle................................................................................................................32
3.1.5 Formato Padronizado do Mdulo............................................................................................................32
3.1.6 Estrutura de Controle do Programa .........................................................................................................33
3.1.7 Documentao ........................................................................................................................................33

3.2 Ambientes de programao. .............................................................................................................34


3.3 Estrutura de um programa em "C. .................................................................................................35
3.3.1 Regras Gerais .........................................................................................................................................35
3.3.2 Palavras reservadas (keywords): .............................................................................................................36
3.3.3 O comando #define.................................................................................................................................36
3.3.4 O comando #include ...............................................................................................................................36

3.4 Tipos de dados..................................................................................................................................37


3.4.1 Tipo Inteiro.............................................................................................................................................38
3.4.2 Tipo Real................................................................................................................................................38
3.4.3 Tipo caractere e tipo fila de caractere (string)..........................................................................................38
3.4.4 Tipo Lgico ............................................................................................................................................39

3.5 Variveis...........................................................................................................................................40
3.6 Representao dos tipos de dados na linguagem de programao C ..............................................40
3.7 Onde as variveis so declaradas:...................................................................................................41
3.7.1 Observaes sobre os nomes das variveis..............................................................................................42

3.8 Inicializao de variveis.................................................................................................................43

3.9 Constantes ........................................................................................................................................43


3.10 Modificadores de tipo.....................................................................................................................43
3.11 Expresses ......................................................................................................................................44
3.11.1 Operaes primitivas ............................................................................................................................44
3.11.2 Expresses aritmticas ..........................................................................................................................45
3.11.3 Algumas funes matemticas da biblioteca C ...................................................................................45

3.12 Operadores.....................................................................................................................................46
3.12.1 Operador de atribuio..........................................................................................................................46
3.12.2 Operadores aritmticos .........................................................................................................................46
3.12.3 Precedncia (Hierarquia) nas operaes ................................................................................................46
3.12.4 Operadores de incremento e decremento ...............................................................................................47
3.12.5 Operadores relacionais..........................................................................................................................47
3.12.6 Operadores lgicos ...............................................................................................................................47
3.12.7 Precedncia (Hierarquia) dos operadores relacionais e lgicos. .............................................................48

3.13 Exerccios:......................................................................................................................................49
CAPTULO 4 - ALGORITMOS, LGICA DE PROGRAMAO E FLUXOGRAMAS. ................................................51
4.1 Introduo ........................................................................................................................................51
4.2 Conceito de algoritmo ......................................................................................................................51
4.3 Representao dos algoritmos..........................................................................................................52
4.3.1 Linguagem natural ..................................................................................................................................53
4.3.2 Fluxogramas ...........................................................................................................................................53
4.3.3 Pseudo-linguagens ..................................................................................................................................53

4.4 Diretrizes para construo de um algoritmo....................................................................................53


4.5 Representao grfica: fluxograma ou diagrama de blocos............................................................54
4.6 Formas padres para utilizao de fluxogramas .............................................................................55
4.7 Exemplos ..........................................................................................................................................57
CAPTULO 5 - FUNES BSICAS (FUNES DE ENTRADA E SADA). ............................................................58
5.1 Funo de sada : printf().................................................................................................................58
5.1.1 Especificadores de formato .....................................................................................................................59
5.1.2 Seqncias de Escape .............................................................................................................................60

5.2 Funo de Entrada/Leitura : scanf()................................................................................................60


5.2.1 Usando scanf com strings .......................................................................................................................62

5.3 Funes para entrada e sada de strings: gets e puts.......................................................................64


5.4 Funo de Entrada : getch() ............................................................................................................64
5.4 Exerccios:........................................................................................................................................65
CAPTULO 6 - ESTRUTURAS DE CONTROLE E DE REPETIO. ........................................................................71
6.1 - Tipos de Comandos ........................................................................................................................71
6.1.1 Seqncia ...............................................................................................................................................71
6.1.2 Seleo ...................................................................................................................................................71
6.1.3 Repetio................................................................................................................................................71
6.1.4 Atribuio...............................................................................................................................................72

6.2 - Comando if ( ) ................................................................................................................................72


6.2.1 If s aninhados ........................................................................................................................................73

6.3 - O comando switch () ......................................................................................................................74


6.4 - Comando while ()...........................................................................................................................77
6.5 - O loop for (;;).................................................................................................................................78
6.6 - O loop do { } while.........................................................................................................................80
6.7 - Interrupo de loops ......................................................................................................................80
6.7.1 O comando break....................................................................................................................................80
6.7.2 O comando continue ...............................................................................................................................81

6.8 - O Comando exit()...........................................................................................................................82


CAPTULO 7 - FUNES DEFINIDAS PELO USURIO.......................................................................................83
7.1 Valores de Retorno ...........................................................................................................................84
7.2 Chamada por Valor..........................................................................................................................86
7.3 Chamada por Referncia..................................................................................................................87
7.4 Argumentos "argc" e "argv" do "main( )"........................................................................................87
CAPTULO 8 MATRIZES, VETORES E STRINGS............................................................................................90
8.1 Vetores (Matrizes de uma Dimenso)...............................................................................................90
8.2 Strings...............................................................................................................................................91
8.2.1 Atribuio de valores a strings ................................................................................................................91

8.3 Matrizes (Multidimensional) ............................................................................................................92


8.4 Matrizes de Strings ...........................................................................................................................93

8.5 Inicializao de Matrizes..................................................................................................................93


8.5.1 Inicializao de Matrizes de Caracteres...................................................................................................93
8.5.2 Inicializao de Matrizes Multidimensionais...........................................................................................94
8.5.3 Inicializao de Matrizes sem Tamanho ..................................................................................................94

CAPTULO 9 INTRODUO A PONTEIROS. ..................................................................................................97


9.1 Exibindo endereos de memria.......................................................................................................97
9.2 Declarando variveis ponteiros .......................................................................................................98
9.3 Alterando o valor que o ponteiro referencia ....................................................................................99
9.4 Usando ponteiros com funes.......................................................................................................100
ANEXO .......................................................................................................................................................103
BIBLIOGRAFIA ............................................................................................................................................104

  
EXEMPLO: EXPRESSO LGICA.........................................................................................................................22
EXEMPLO: MINIMIZAO .................................................................................................................................23
EXEMPLO: MINIMIZAO .................................................................................................................................24
EXEMPLO: FUNO BOOLEANA ........................................................................................................................24
EXEMPLO: TABELA VERDADE ...........................................................................................................................25
EXEMPLO: TABELA VERDADE ...........................................................................................................................25
EXEMPLO: TABELA VERDADE ...........................................................................................................................27
EXEMPLO: TABELA VERDADE ...........................................................................................................................27
PROGRAMA EXEMPLO: FUNO MAIN()............................................................................................................35
EXEMPLO: #DEFINE ...........................................................................................................................................36
EXEMPLO: #INCLUDE ........................................................................................................................................37
PROGRAMA EXEMPLO: INCLUSO DE BIBLIOTECAS ..........................................................................................37
EXEMPLO: FILA DE CARACTERES - STRINGS ......................................................................................................39
EXEMPLO: DECLARAO DE VARIVEIS ...........................................................................................................42
EXEMPLO: INICIALIZAO DE VARIVEIS .........................................................................................................43
EXEMPLO: #DEFINE E CONST PARA CONSTANTES ..............................................................................................43
EXEMPLO: ATRIBUIO DE VALORES ................................................................................................................46
EXEMPLO: INCREMENTO E DECREMENTO ..........................................................................................................47
EXEMPLO: EXPRESSO BOOLEANA EM 'C' .........................................................................................................48
EXEMPLO: EXPRESSO BOOLEANA EM 'C' .........................................................................................................48
EXEMPLO: TABELAS VERDADE COM EXPRESSES EM 'C'...................................................................................48
PROGRAMA EXEMPLO: PRINTF SADA DE DADOS ............................................................................................58
PROGRAMA EXEMPLO: PRINTF SADA FORMATADA ........................................................................................59
PROGRAMA EXEMPLO: PRINTF SADA COM SEQNCIAS DE ESCAPE ..............................................................60
PROGRAMA EXEMPLO: SCANF ENTRADA FORMATADA SIMPLES .....................................................................61
PROGRAMA EXEMPLO: PRINTF SCANF ............................................................................................................62
PROGRAMA EXEMPLO: STRINGS COM ESPAOS E SCANF ...................................................................................62
PROGRAMA EXEMPLO: SCANF COM DELIMITADORES %[]..................................................................................63
PROGRAMA EXEMPLO: SCANF COM DELIMITADORES ........................................................................................63
PROGRAMA EXEMPLO: PUTS & GETS ................................................................................................................64
PROGRAMA EXEMPLO: GETCH ..........................................................................................................................64
EXEMPLO: COMANDOS EM SEQNCIA .............................................................................................................71
EXEMPLO: UTILIZAO DE SELEO .................................................................................................................71
PROGRAMA EXEMPLO: REPETIO ....................................................................................................................71
PROGRAMA EXEMPLO: IF-ELSE .........................................................................................................................73
PROGRAMA EXEMPLO: IF ANINHADO ................................................................................................................73
EXEMPLO: MAIS IFS ANINHADOS ......................................................................................................................74
PROGRAMA EXEMPLO: SWITCH-CASE ................................................................................................................75
PROGRAMA EXEMPLO: SWITCH-CASE ................................................................................................................75
PROGRAMA EXEMPLO: SWITCH-CASE ................................................................................................................76
PROGRAMA EXEMPLO: WHILE ...........................................................................................................................78
PROGRAMA EXEMPLO: FOR ...............................................................................................................................79

PROGRAMA EXEMPLO: FOR ...............................................................................................................................79


PROGRAMA EXEMPLO: DO-WHILE .....................................................................................................................80
PROGRAMA EXEMPLO: BREAK ..........................................................................................................................80
PROGRAMA EXEMPLO: CONTINUE .....................................................................................................................81
PROGRAMA EXEMPLO: FUNO DEFINIDA PELO USURIO .................................................................................83
PROGRAMA EXEMPLO: SEM VALORES DE RETORNO ..........................................................................................84
PROGRAMA EXEMPLO: VALORES DE RETORNO ..................................................................................................84
PROGRAMA EXEMPLO: VALORES DE RETORNO ..................................................................................................85
PROGRAMA EXEMPLO: CHAMADA POR VALOR ..................................................................................................86
PROGRAMA EXEMPLO: ARGUMENTOS PARA MAIN .............................................................................................87
PROGRAMA EXEMPLO: ARGUMENTOS PARA MAIN .............................................................................................88
EXEMPLO: DECLARAO DE UM VETOR ............................................................................................................90
PROGRAMA EXEMPLO: UTILIZAO DE VETORES NUMRICOS ..........................................................................90
PROGRAMA EXEMPLO: UTILIZAO DE MATRIZES ............................................................................................92
EXEMPLO: MATRIZ DE STRINGS .........................................................................................................................93
EXEMPLO: INICIALIZAO DE MATRIZ ..............................................................................................................93
PROGRAMA EXEMPLO: INICIALIZAO DE MATRIZES DE CARACTERES .............................................................93
EXEMPLO: INICIALIZAO DE MATRIZES MULTIDIMENSIONAIS .........................................................................94
EXEMPLO: INICIALIZAO DE MATRIZES SEM TAMANHO ..................................................................................94
PROGRAMA EXEMPLO: MATRIZES DE CARACTERES E VETORES .........................................................................94
PROGRAMA EXEMPLO: SOMA DE MATRIZES NUMRICAS ...................................................................................95
PROGRAMA EXEMPLO: USANDO OPERADOR & ..................................................................................................97
PROGRAMA EXEMPLO: USANDO OS OPERADORES & E * ....................................................................................98
PROGRAMA EXEMPLO: USANDO O PONTEIRO DE INDIREO .............................................................................99
PROGRAMA EXEMPLO: PASSAGEM DE ARGUMENTOS POR REFERNCIA ...........................................................100
PROGRAMA EXEMPLO: ENDEREOS NA PASSAGEM POR REFERNCIA ..............................................................101
PROGRAMA EXEMPLO: TROCA DE VARIVEIS ATRAVS DE REFERNCIAS .......................................................101

 !"
#%$'&)(+*,(-$/.1032)465%(,78.:9'$/;=<>5%(,(@?'<@$6&BA/C1(D58.EC/F)08A=G/(+5).+?H9I<%0J9K(/L3()M/;%<ONPB./LDL@2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
>k:@B 6H:8Bh^HBteB /:/@@:3J@@Bx8:HHB8^@Bv3
sHBO-/B@-B+3s:83a/8:H8O6HB
1.3.3 Sistema Octal

_BH8JBHH8JHH8^:):)H^QU OJHDtQqcH@:B@
Hr8:@+a% d\
B^8B)J@:@B+r83J:@8r8:BHH8^8H8H:H)H^8@
)t BJB:8B t/@H8BtmH:@8
B  8JOm ^8HH D)f > 8 BO6J
8J:B8H^8@-D:8J:H)Bk8-B:B)sHH@8:-B:B8JB
56,32)8 = 5x81 + 6x80 + 3x8-1 + 2x8-2
56,32)8 = 40 + 6 + 3x 1/8 + 2x 1/64 = 46,40625)10

1.3.4 Sistema Hexadecimal

bBH8J3Jc@BHH@:H8HBe)^@:8B^:8^8@^qE tHDtE
\
bn 
T

hx r :@H 
T

hx r 8tH6:B8^BH3
/:H8:8^1^BJ:)Hk8XBX X OXa H  DH@H:8:8:H8H@BkHr8:@
8JH3@H@B3^>^DH@HB@:% ddH^q+ d^q N^$ d
B8J
BH8H8J

8:):@HH3
B
^B6^m^@:BBHBt6:

^Hm^8^)6JHB3t/:D@O/,J:8BBJ:8BrHDJ3HtR@
)tBH8^@B@:B@
8Hm
D' tp3@ ,ByBtmJ)JHHH1^@H8/_
@:>B^H1^HH:)Hr@DBt6:8H@H8H^8H@/+-36J+B:B8JB
5A3,E1)H = 5x162 + Ax161 + 3x160 + Ex16-1 + 1x16-2
5A3,E1)H = 5x162 + 10x161 + 3x160 + 14x16-1 + 1x16-2
5A3,E1)H = 1280 + 160 + 3 + 0,8750 + 0,0625 = 1443,8789)10

13

g$.pKH'H%$,p Kp'JII-".Kp K
x|{

8
H




{H
D


'p %")KIBm'p
B8J@:88k+B:8^H)HH


|

HHB
HH:
H^)
H^B
^8B
^8:
^H)
^HB
8HB
8H:
8^)
8^B
H8B
H8:
HH)
HHB

~}

/



D

8
H




{H
D


H
D

\
b

8:1sO=@3r:8O/+:>:1^O=@@+8:@@2m3

1.5.1 Decimal inteiro para base r

)^)sR@Be3^8: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,256
2,048
0,384
3,072

=
=
=
=
=

2
0
2
0
3

+
+
+
+
+

0,032
0,256
0,048
0,384
0,072

Assim: 0,254)10 0,20203)8

)JB:@B 8^)J6JOu3HO6DfD1
0,375 x 2 = 0,750 =
0,750 x 2 = 1,500 =
0,500 x 2 = 1,000 =

@O/>HH8O/8

0 + 0,750
1 + 0,500
1 + 0,000

PARAR, FICOU ZERO

Assim: 0,375)10 = 0,011)2

)JB:@B 8^)J6JOu3HO6D$\H:)k@O6HH8O=)
0,800
0,600
0,200
0,400
0,800
0,600
0,200

x
x
x
x
x
x
x

2
2
2
2
2
2
2

=
=
=
=
=
=
=

1,600
1,200
0,400
0,800
1,600
1,200
0,400

=
=
=
=
=
=
=

1
1
0
0
1
1
0

+
+
+
+
+
+
+

0,600
0,200
0,400
0,800
0,600
0,200
0,400

PARAR, DZIMA

Assim: 0,8)10 0,1100110..)2

1.5.3 Converso entre um nmero decimal com parte inteira e parte fracionria para
um nmero na base r

@:BBOYDJBHH3kHH>:1^H8J:+@H@@HB)J:-8/->@6JH8:@/
k
c@@H8OjJ3HJ3vB:y^8B^HB:B@:)J@hHB@@HH%s:8/hw@mJ
O/:B8^@/@

15

)JB:@B 8^)J6JOukHHB8JBDfEBO/+@@@@H@OR8
57 |_2
1
28 |_2
0
14 |_2
0
7 |_2
1
3 |_2
1
1
Assim: 57)10 = 111001)2
0,300
0,600
0,200
0,400
0,800
0,600
0,200

x
x
x
x
x
x
x

2
2
2
2
2
2
2

=
=
=
=
=
=
=

0,600
1,200
0,400
0,800
1,600
1,200
0,400

=
=
=
=
=
=
=

0
1
0
0
1
1
0

+
+
+
+
+
+
+

0,600
0,200
0,400
0,800
0,600
0,200
0,400

PARAR, DZIMA

0,3)10 0,0100110..)2
Assim: 57,3)10 111001,0100110..)2

1.5.4 Converso de base r para base decimal.

q/BH:@B@Bk3O/3s:,H3t6H8J:B8@^@B

1.5.5 Converso da base binria para a base 2K

jBO6J@@3H@@O=8-H%sr@Y:)dB:8H-3kO=B8^>H]-H3:a':@B]>
8 8JOKcH)H:'J:3O+3q^883tmJH3^q@d6^,Hrd':@H
@O/aa:8:@O/Bnc@^6^BBnBO/c:R:8J+1yBB^H8Bj^8JBB^^
B^H1sHH@3J: mH:H8B:H @BHHH:H%J:H@ B^ tRH8: Bt'JHB:
H@BHBH^>H8H+H-B6J:B3kBDt=H)^a rfs
)JB:@B8^)^6JOX )J6rHH@=8r8^H8p8^H+@ORr-8:@Dd8HH-
HBJBH:H):H
8 = 23
16 = 24
10

110,

k = 3
k = 4
101

010
2

110,
6 ,

101
5

Assim: 10110,1011)2 = 26,54)8

0110,

1011

0001
1
1

Assim: 10110,1011)2 = 16,B)16

16

0110,
6 ,
6 ,

1011
11
B

100
4

1.5.6 Converso da base 2K para a base binria

8^1sO=88 H @:8 { 8O/ BH@=3 8JB @O H@8: 8H 8


/ :H)^HHB3 H8@@HO
JHHx@8^8@ ^8BH 'Jx@BHHB:H3JBHxH3s 8O
B^@@@:@3 ) @: B^B1sHH88J HB8O=8 8@ t/: H3: '^8H@H
^@BO6B3^j68BJHH8H8J@B
)JB:@B 8^)J6JOu 3HO6D^8JH tHD8O/@H@OR)
8

= 23

k = 3

123,47

1
001

2
010

3,
4
011, 100

7
111

Assim: 123,47)8 = 1010011,100111)8

)JB:@B 8^)J6JOu3HO6@BJHB:@8JB
^E@RrHB8OR8
16 = 24

C13

k = 4
C
12
1100

1
1
0001

3
3
0011

Assim: C13)H = 110000010011)2

1.5.7 Mapa de converso


DECIMAL
INTEIRO

DS
BASE r
FP

DECIMAL
INTEIRO
DECIMAL
FRACIONRIO

BASE r

MS
BASE r
FP

DECIMAL
FRACIONRIO

BASE r

EX 4 Bits
HEX (16)

FG 3 Bits
BIN (2)

FG 4 Bits
HEX (16)

OCTAL (8)
EX 3 Bits

BIN (2)

OCTAL (8)

r38piRJ@B-HS
H^)sd=@8-:8t6@:8:

17

O8BJ@@H
D
i ^@3JH@:B8:8J:DHHB@HH1J:76
+
i :)^@8J:DHH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d@O6:@Bk >D^@Om:@BBH^H8r,:)t/
:>:@:sO=@1s:H
d@O6:@B,s k^8O/:B3j3H:H@kdQ@86
:>:@:sO=@1s:H

^=BH8H

BH^B8

^@%s:

e
L

= A.B

A
= A+B

B=HH1Hk:8H8J+@3^BH8:
BtmJ

Lgica de
chaves
V

+
-

HHH+r88^HH)H-B^s/:HB3k3B^H@ @^8HDB=B@8H-3H:H8r8H
H@3J: / YB^@BB-r)BJO/8
)^6:H@3rBH:B@
D

Exemplo: Expresso lgica

L = AB + CD + D
C
A

D
B
L

I fpI+" gtIH"K$&$pIBImg



A=A



A + A + A + ... = A



A A A = A

A + 1 =1

_'>'I
_'>'I
_'>'I
_'>'I

A
A
A

A
1

22


_'>'I
_'>'I



_'>'I

A0= 0

A 1 = A

0
AA = 0

 

A + A =1

_'>'I

0
A

A+0=A



_'>'I



_'>'I



_'>'I



A ( A + B) =

A
A + AB=A + B


 

AA + AB=

_'>'I
_'>'I

A + AB= A

_'>'I

A + AB=A

A
B

A ( A + B) = A A + A B = 0 + A B = A B
A=A

Bt6BJ:DH-BRHt6B:
+8B^8^)@8OY:@:D@O'H:
F/)H^@B3J:8@kH8J
p)HJ H-HH3::8H:1^@H-3 6J:3Ho B^
@:H+8H:BHBJ@)H^fF
(A + B + C + D + ....) = A B C D....

F/b8H^8HBJ@)HnHe8 p mJ@8Hp>BaHHBJ@8B^ce8^8H
@:H+8H:BHBJ@)H^fF
(A B C D ....) = A + B + C + D....
Exemplo: Minimizao

R^HH)^@H>-B:/:H@%8J8:@B@@B)^3^@BH3^>+H8t/BH@
F = ( A B + C) (A C B)
F = ( A B + C) + ( A C B)
F = ( A + B + C) + A + C + B
F = A.B.C + A + C + B = A(BC + 1) + C + B = A + C + B

23

p8HJ,B^

Exemplo: Minimizao

R^HH)^@H>-B:/:H@%8J8:@B@@B)^3^@BH3^>+H8t/BH@
F = XY + X+ Y
F = ( X Y ) X + Y = (X + Y ) X + Y
F = XX + YX + Y
F = Y + Y X = Y (1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H>H+@HH@B-H+3^@=@@@H@+B+:@B:H@

24

/H@HHH)::8H+H:-1J:HB@-B)JO6JB:BO=8:
@:B@:@3rB^D8^8^)HHBJ>DHBt/BH:@+3^6:H88kH>@BH8J:H3
d8J@H@BB+JBBt3-:@B@B@O/@@H8B^HB@:88
HH)^8^B@@B @H:B8B 3: 6:H@B@HB^ 8O=H8:B @H)^3t/:@3
B^6:H@3r:H8B
Exemplo: Tabela verdade

H^HB@HO6-@BH@Brdg $ T)
JWRH^8H-H@DH:@BHE^O/HBB+-@H8^8/

^6:H@3:HB^

!
%

"

%
%

%
%
%
&
&

&

&

%
&

%
&

&

F(A, B, C) = A B + A B C

A.B

&

&
%

&

&

&

%
&

&
%

&

A.B

&

&

&

&

A.B.C

&

&

&

&

&

&

%
&

%
%

&

%
&

%
%

%
&

%
%

&

$
&
&
&

%
&
&

Exemplo: Tabela verdade

!
%

"

%
%

%
%
%
&
&

&

&
&
%

&
%

&

&

&

&

&

&

AC

A.C

&

%
&

&

AB

%
&

&

F(A, B) = A B + A C

%
%

&

%
&

&

&
%

&

&

&

&

&
%

&
&
&

I ('sK%"tI
4d@:8B @ @BH8JH T8BHHH:B@ 3t/3 :6@@:8H@H:cd8BJ :@ @H@
8JH3@TH8B^HH1J-8O6J
F(A, B, C) = A B C + A B C + A C

^@H nT
83 H sR@3sHH B @BH@BRJ:_JB@OmJ:@B ^
JO6B^ A B C A B C Bh3^@H@B)HH^B8w)HJ^@b:BH^3^H
6J@8H^:)HJ3J:8BH:H:q@Qt/@Q^O=83J:HqB:HH83B8JH3^H
JO6B^ 8B :@ B@BJ3B^ ^HH8HO6H:x88JBb^BH)J'H^ @B
6J@8HHBJ@=H8^@8:8H3HJB@@k^O=83J:H8@:@Bj@H/
He@BH@@BUHc8Jj:BHBB aJO68 A C @Bae8j^HB3J6H_):H
@Br8^8H8kEJO=@3HH
T
25

J:8@H: s/HBH )HHB JB:@B @ 3^/@B@: BO/%s: B 8J


8HJ3Ht=8H@:3q:HH8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>B^@@H@8J
r3Qde/
O)8t/+@@HO/88JB
N E  O8BO/+:8HO6H8J+Bj
o
OB  O

r3

I  " )gg" p p mI
@R:HH8:8H->B^/8H@B-8^8@@H:@3r8^3^8HH3^>DJB6BH:
F = B C D + A C ( D)
F = BC D A CD
F = BC D A CD
F = A BCD


R:HH8:8H->B^/8H@B-8^8@@H:@3r8^3^8HH3^>DJB6BH:
F = AB+ C+ AC
F = A B + C A C
F = (A B + C) (A + C)
F = A BA + A BC + A C + C
F = A B + A BC + A C + C
F = A B (1 + C) + C (1 + A)
F = AB+ C

)R:HH8:8H->B^/8H@B-8^8@@H:@3r8^3^8HH3^>DJB6BH:
F = A CBA + A B A C A D
F = A C ( B + A) + A B + A C + A D
F = A CB + A C + A B + A C + A D
F = A C ( B + 1 + 1) + A B + A D
F = AB+ AC + AD

28

HJR:HH8:8H->B^/8H@B-8^8@@H:@3r8^3^8HH3^>DJB6BH:
F = A B (C D) + ( A + B) B A + C D
F = A B C D + ( A + B) + B + A + C D
F = A BC D + A B + B + A + C D
F = C D ( A B + 1) + A ( A B + 1) + B
F = CD + A + B

D)R:HH8:8H->B^/8H@B-8^8@@H:@3r8^3^8HH3^>DJB6BH:
F = (A C) ( A B) + (C D B) + A
F = (A + C) (A + B) + C D B A
F = A + A B + A C + B C + (C + D + B) A
F = A (1 + B + C) + B C + A C + A D + A B
F = A + BC + AC + A D + A B
F = A (1 + C + D + B) + B C
F = A + BC

I '"$ I#'" $#pKI


@RB^8J>:>H@@HH:sORBHHDH:3^/:B8H:>:@@3H
H F(A, B) = A + B
@ F(A, B) = A + B
@ F(A, B) = A B
H F(A, B) = A + B
H F(A, B) = A B
H F(A, B, C) = A (B + C)
J F(A, B) = A B + A B
@ F(A, B) = A B + A B
@ F(A, B,C) = A + A B + AC
H
F(A, B, C) = A B + C

R:HH8:BH-@>B^m:H8:@@8:8Hr8J@:8H@@Br8:3^@HB3^>DJBt/)J:

F = A C + A D ( A + B C) ( B C D)
Re sp F = A C D

F = A B C D A ( B + C) C D
Re sp F = A B C D

29

F = A D ( D + B) + A + D + C ( A + A D)
Re sp F = A + D

H
H

F = ( A C + A D) B D
Re sp F = A + B + C + D
F = AB+ CB
Re sp F = B C

F = A BC + A B

F = A B C + A ( B + C)

Re sp F = B ( A + C)

Re sp F = A + B C
F = (A + B) ( A + C)
Re sp F = A + B + C

30

Captulo 3 Programao estruturada em C.


m I6pKII IBK+" p'"It$&$(p " ,K,'"IK

3.1.1 Definies

H BaB^B)Jc8:jHH8HHH@%_B@)sOR@HBH@8J:@:3Hc@O/6HO/BJB@B
:8tR8^6@Hp6s 3:8t/R8a3^H8:B s=@: :B8HH: 3 @:B@BJB8H
^@+r8:@@:8:8B
B @@8^@8xJ: H /:8OR8HQ 8:H@:83 H mHt/3::@3 :8tR8^'HH
:@h/:B@:8B
BO6JaB_6Bt/BHncB_6^@:@)nHa8J:B:H@@@3

8 8^{ HH8H B 3^)s@B8J: J@ mH/BHH3t 8JH @3^H 8O/
6J@@:h )J:8@ :)O=8^6HBj /BO6: B )H:@:@):@B 3^)JB
:8^)J:8HH 3^/ H) H: :8tR3^/@ 8:HH8@ B 8J8t6HB
:8tR8^6H-Hr8HJ)^HH83kHB:@HOk3^H3J@8J@@3

BBQ@3JkB:8^83
rmHt/3::@B+:8t=1^/@B-@8B^@D^@3^BH8H:,@^HH):
@8B @8 H J R3:8^H8:@8 8H^@B3J : @O6H@H B
L 8 H6HJO@@/BBJ
B+J^ 6
:@@8: O/H :8tR8^6@ B:HH@8 B )J8t6JB
L @ B6H O8:/BHJH@88Bu @@8BH: @B+
r8H/:83o 6
1
r
t

>

:)=8:/HBo 6
LL @:B@:@3 H 8^/s:H8H@ B )J:@:3@:@B @8O=8^s/H@ )J
:H8:@BJ3-H-6Ht63J:@3r:B8:H@:B@
) ) @:)^@B J:B B^8Ba 6H/BJ@@B @)t=)^/H@ H=8:@H
J:8BQB:>JBB:B^Hr6Ht6%J:@B8@B  3t6J-36HO68J
8^8:B@
@B^B_8JJ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H^ 8 :H8:@8J3


JO/@H 8^Hq6:H8^)H:

n)
l @3J@B 83^8J@ 6Bt6:

3.3.1 Regras Gerais

a=8H:R:HH88@O,R3HHQH8mHO/BJkB)
oXH)JQ@O,
:HB@B+H:HO'
@HH@3-H:BORr3B/:BH8-@OYH:@B/HB

@BH8H8H/BO/
O/ W /Q~Q/}N_+@HH8H@H1JB8OB@@HORBH:
J:BOR :H@@B@:H@ja@BH@Be8t-:8Hje6HORBJc8HJ@8ae@O
BJ@H8JB3
)
)
oBHHH@Q:B^/8H8J:Q@3t'BJHH83HB:HB8::8Hq^
@Br@Of Y3JBtmJ
RJ:)H :@@@B @3 8^)J @OI Y3JBtm: H%s:B@ :BH ^@ @B
B^B8JB@Of YB:BO6^@B^8@H:HH@3
8O/qHH)cHB^3@@HH%/
/:8OmsBB
;~|{x t
BJ+:HB8Bk+@B^/+HB:)^3HHH8tT)@YHB@3s@F

O8BO/:vH:
HBHBH:x^B HHBBH: 8B t6BJBHH 8BB @6:8J/@
:@BO/:)H:
Q3t6J3BkB>JBHB--@)t/
sJ3^>:+8HHH@B^+8B+J6JB@HB:-8O
P{~/}>~UJyONIzQ/
~x@UsOR@)s:B>H3JBrBH@@HO6BH:

Programa Exemplo: funo main()


Descrio: observe a estrutura da funo main(). Neste exemplo a funo
no envia nenhum parmetro (a palavra void entre parntesis) e no
retorna nenhum valor (a palavra void antes de main). Logo em seguida

35

aberta uma chave "{", escrevem-se as variveis, operadores e funes e a


chave , ento, fechada "}".
#include <stdio.h>
void main(void)
{
//variaveis............
//operadores e funes.........
printf("Eu no fao nada.");
}

3.3.2 Palavras reservadas (keywords):

@:)Hk@3JB-BB^8J8HH+@BB3/:+@@3J:>BOBB3^@BH3)
oBHHBH8J
sO=83sH,^@BH@B+B@B@OR)8JB-8^8:8HOY:)::-8BH%/@-@
Bt6Hr8B@kBO6BkH3:HHBB:
8HH%/:/:@psHH:,-HBH8H@H:+@H@@8BJ:8:+BB83^/+HH:H8@H:
8H-HBt6H>^B HHH@3:
break
do
if
switch

case
double
int
typedef

char
else
return
union

const
enum
signed
unsigned

continue
float
sizeof
void

default
for
struct
while

:Jtg-Td3^8JH+@HB3t/:/:8OmsBB:BUOBH8@@BJ3)
B
3.3.3 O comando #define

_)HJ@@8JHH:H)j8^8@@HB8e8O6HH@@H>8@H:8H@:B@BBt>8J
8t=B8 8H^H)HHBy/@HH8:8^He @B@8^@@B@HBy@HB 8t/B8
@HHEsH,^@Dr@H:1^@:@8HBOYBtY:@8^)t6HBr8O6^B1J^8B:8J
#define identificador string

Exemplo: #define
#define
#define
#define
#define
#define
#define
#define
#define

DOWN
ENTER
ESC
FALSE
LEFT
RIGHT
TRUE
UP

80
13
27
0
75
77
1
72

3.3.4 O comando #include

HH8O=H3H:H@BH3t_8O/:@vHHhHH@H8v8v:8t6d6^H)Hd88:8J
:H8B8BrB^8BH-Op^H):+^8+:8J+@:@BO6:@3--8HJ@8B BH@:@8

36

#include <arquivo "header" (cabealho) .h>

d @O6H8JO6@ 8 8B )J8:@HH3^ 8t'HHBJ@)J @O6 BH@:8 :


O6^B)J^DHrBB@:8HB:@-8HO63kB

dk8O/:8BO/: D@B8J@:)HHB:k8tpJHB:@8Jr@O6rHH@8HU6^H=J^
Hr8:@:8@:8r=@HB@-@@3BB@O=8
8HHr8Bs@H6JH@B:@3kB1H^8H, ER:)HJ@@H:B8@)Q8^B:@H
BHJ@H@B
3 E)^Op^H)::B@:@@@@:8

#include "arquivo
Zde
"header" .h"

Exemplo: #include
#include <stdio.h>
#include definicoes.h

/* header padro da linguagem C */


/* header definido pelo programador */

sJH@ : :HB8JH @:BB@@ BtR@:@@B: 8BB^ H@/H@:)J: H


8H^B@HH3/:3 OBH3:@BJ3 )
o:8JB8^8J8H:vB HB@@8HH:@:w:@
@B BH@HH8H: 1^ mBt/BH@ BO/3s@vB )BJ:@3 o@tkH@)
^@:8B_:HB8B
ryB@HByHH@B8HJ:)y8tH/HB8:@B:8JyB3J
@OTHH@@@@H

Programa Exemplo: Incluso de bibliotecas


#include <stdio.h>
#include <conio.h>

//para o printf
//para o getch

void main(void)
{
int dia, mes, ano;
dia = 26;
mes = 05;
ano = 2000;
printf("Agora ninguem me segura!!\n");
printf("Meu primeiro programa!!\n");
printf("Hoje, no dia: %d/%d/%d\n", dia, mes, ano);
getch();
}

m #mIBKJpIKm

H:8H )^8B s3H^ 8@J@@O t/:8H:@ 1H ^@ @ H@3J @


H@BO6JB:@@ B  1 H @H3^B 8^H8 :B:8^8@@ @8t/8H^ B
8^8HBH8H:B8J@bH@8tmJ@8J@-yJ:)t=8bH@8J:B83t6H:8J:eJ3
Bt6HBHr@8JORB8>8J@38@H@H@@:B@BH:

HHHj3J8^@BH)Bc3jH=@B8e)^8JH3@^q@H8^)O/O+88Hj
8HO6
HHH@3@BBHHOR8J@UJ^@-H:8/ Hk3B^/
u-/
Ld:@r@3J
@3^HHH) @B@8^H3 8J8^8 % H/:{ H@ H@8tma8J HO/@/ H
@O/@H@x:@x@ 8BJ@@
O
O- R@8J BO/:{ B :@@RH@:Hv@H 6:88 /3H w^Bd8HBB B:H3B8O/ @/ H: G
8BJ
HO/@/ H: 8:8O/3 - ^B8BHt/3JH
L@@8 /:) H^@ @:@@
_
HHB @6
% )i%uy:G
 % )i%u:X 
RJ:x8@Hx BBb
:H@3k+8^KJr83Ht/@B'HD^@@@:H J^8-@B:@+B3J:@3HH
37

v@BH3/8wB@8t=8Bx8tB6:H8^8H3h:H8^HwHH)^8^@@:H8B:
8HH-)t=B8+H8J@6q/H3ne8H^H)
4d@:B3y@_=@e8y6Ht6BH
H3s3b8OH@H3:^^H8:eBe@HB^jH@B^@B3^ce@HHtR:@HHBj^8
@bH:8 H /@6:@@8HOj8s6Ht/3Jk@888^8ORO=)8HHBH@O/@@
HB^BH d'HHBH:)@^ 8HHB:O6:H @:8H@8J H8B:@ B%s:H / g;dzlj]cg^
8tkBJ3^@BHHBn:HB8Ua^H8nBnBHBv/HBHBJaJ@@8^@HBBaH
JBHO=@DJ@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-6xi:d)8Jj
vHJH ^8H8^8OwH)Bt6J8@B 8H^8H8J
m:rHBH:@)JO=H1^aRXuB@ H) bHk: H^
@O/@8JO6H:B8:H@:3j




nBHI
B^/:B:@:@8j)jJH8c8O/@8JO/qBOj6HBO/@HHBH3a33s^B8c@8O
nZlR:H8H-H):@H:@>@ORH8@H@^83)HB:@B
W
%
5
5

Caractere W
Smbolo de porcentagem
Caractere 5
Isto no um caractere, o nmero inteiro 5, pois no tem
aspas simples

38

sO=1^ ]N_/}]NI/ @B 8H3^@H3:  |/ R) BJB:BB


@ HH%/ /]N_@ J3H: 8 sB@txB @/:)HO/: ^ 3J :@H H
@O/@8JO6HyB3J H8H B @/:8J6: 8^B@:B@@ 8@B Iaq
:@HhBw8O/@8J/Hb^8=B8vB3sb^e@B:/h:)O/h@HB::H3H:
a  )kBH8RH8hb@@HO/B)JH)@O/:%JO/_H:8H)h
8JOmH
a `JJ@ 8BH 8 H38JBJ
4d):@B 8J 8t=38 H:8HO6@B H%s 8O
H@:B@HBa_ 8H6nHe@O6:)JO6:^@aHHe8HHe8^)HOZ
|n)y: `HJ
@O/D8/:8H/rB8B

4d@:8B

Exemplo: Fila de caracteres - strings


szFila[12] = "Linguagem C"
0
L

1
i

2
n

/N;[\a~

3
g

5
a

6
g

RHBHt=8,dHB@BH

7
e

a`JJ@

8
m

10
C

Ip/H@6-+@@HO/B8J+BF1F/8=B8J

byte
string
char

4
u

0
a
a

1
\0
------

F)F^HHB _`HJ:-3+J3Bt=8
Ipl r8-H@OX+^H8@-@8:@@+-a`:J@DHBHO=@
HJHFa/} |{z
F +JOmJH@BH)tT8k@/:)J/H@8F F

]N

@3^HJBH:>HB^3H8HB^83^H3t=B8:
laranjas
Marga Rida

55.67
55.67

;

Um conjunto de caracteres em fila formando a string


laranjas.
Um conjunto de caracteres em fila formando a string
Marga Rida. O espao em branco TAMBM um
caractere.
Um conjunto de caracteres em fila formando uma
palavra no numrica 55,67. Isto no um nmero.
Isto no uma string. o nmero real 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8:@H@HBB @R@ B@@:B@=8 @:Bb
:@H/:8 :@H sHBtv BOv8^@:8H@B :8J/@ @8@Oh^@:)H:
a `JJ@@@BtmH:@BD^HHB=@
BJEsO=8)sH+8H:)^8J+8tut/:+@6J@
BHH 8HH :B BOR@HH8B^ B @@HO6H8 ^8 :@O6H

H@3t6J@8B %HJ 8O6H@BJ:)J %^@:BH3J 83 Y:)JH@ H


sOR@3sB8r3^8J3HBk3r68t/BHp 6
H)n3aHH3 Uj^H8eR3^8^1JaHJO=@%sBE8@eq^8HjHH

@O6 B3t6J@8:@H 8 6HO63Jn^BH8:@3 @H@@8 B JBHO=@


H:)^H@B3k+@Hp6
H@3t6J@8B >g
6sBBt: ^B+DJO=@%JH-mJHH@8

HHHheJO=@)JHb^H3@8HB@:8w8_JBHO/@-:8HO6@3hHbJ@)J
8JH@O:@:B8HBk%kH8:8J@>:H8:@8J:B>H6B/BJ@8B

Varivel

er e
End

r38t

Nome

,
o
I n 1@@f or

/:@:ma
1J:@B+
H-)JDJOR@3JH

m m$#%" IgIp+&)(IJp'1#'g@pJKpKIBpJpq,pKpIK#+" K$"It+&$(I

d>:H8^DH-BBB^-):HH3:B%sB8OT:BR8H:B-@B:BH)J+Bt6J
Sintaxe:

tipo lista_variveis;

d@H
}|P{~H%^r@O8-@H8rH+BBB+sB:BB
caytzlDay;daq )J+J,J@B,sOR@)s:B+@:8O/B8^-)Oudm^@H
HBj:HB3@H@3O6JBJB@X^ksHB/:B8J@m^U8O-%JB^@3U8B
sB H i e b JH3t B@:H@8H B 6HORBJ:@% ^H3H @3
8^H8:H@3cH:@8@HqBc 8J'aBB8^qH@H3c@qHBb/@/:@@8J:3B
)tHH)D@O/c@BH^H8aBaHH3n3^H8:a3Jc@:8He8JHBtE8BH
H^8O/HB:@:)B
40

Tipo primitivo
inteiro

Tipo em C
int
int (16)
int (32)
float
double
char
char
int
void

real
caractere
lgico

Bits utilizados
16
32
32
64
8
8

Bytes

Faixa de valor

Dependente do sistema operacional


2
32768 a 32767
4
(21474836471) a 2147483647
4
3.4E-38 3.4E+38
8
1.7E-308 1.7E+308
1
128 to 127
1
128 to 127
Dependente do sistema operacional
0
Sem valor

@Jt 0 Y@8^>H+BHB:

bsb 5

n:H8cH8:@6aH:8:8H:3HjBeH38JBH^BO6:B8^8Hc86jj:8H
mHt/8H-)H^H@8HB
HQ@H8^83JB
n)
pl8@HH)sB@@H@O
F%@H@ F>3HH6^8H:)HQH8J
:HH8BT^  QsBH@c@@B:B@OR8e8:H8O
F3@HB FQ@cB@@@O6O+^8
F3@HB
FQcO6:8H:)HaBjHHB@By@BH%/J8e38^H8@HBj
F3@BH F
++^H8
f7KsH@3uB@3J
^B8 B3^H8 tRH:8:8B @/ ) ^H) /zN^x^H) |/} @
:H8:@BJ8 n)
lH8B Y@8JH@HsO=)3sHU8O/3U@@:BjJ@)J
8x8JBbJOj3^Hns@Bt6:-h^hI
G|azQ/z>  {~/} ax~

XQ/5XQ/]N {~iN x|/]N_/} x /]NI~


XQ/ ax~U|/x~{Z{ PzN_\~z|//

|{z| |//

]Nxx/|zNf~
double

double word
word
byte
bit

float
int (DOS)
char
1 char = 1 byte
1 word = 2 bytes
1 double word = 4 bytes

r38Jt%Dd3J:H)-BrH3^,B^,^H8:+H+BHB^

m8)(gJKpI'"I'gE0

m :'p'Hpa ' "

)JH3
@d BHHHH8H: H:86 BOR H: :HH8B: 4d@:BB @B
HH:BH@H@ Bt/ H HJHH : :HH8H:eHH8:BH@3 :BH@B J:HR
:8J@ s=@)s@H @B H@B@HB: H Dl]ay;dsa c gvbaq 8JBB 8O
H@HB:3^@B3^@O@O6:+B6Ht6B:
4d@:8Bx8BvBH:BHBH:H@8tmvBw8Jh8HH@BQHH:b@BwHBBJHH8H
Dliaq;dsaqgbayen3y)JH8y@OB@HH:eB@8t6yBB:@H3y@HH@3
8J sR@3s@ B^@B JB J:)H 8HJ B 8J J=@%sB JBJ8H
@B^8HH+HHH@3-Dsd=@3^B-3:@H+HBk6:8HH@HH8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@@Br)H

I
n

p
lROH6: `sp

RJBO=@H@+@B:/r)HH:@3r8@ no
l6

)^)J:m:-@3J/8BJ:8Br3B

np l6

bsb 5

d 8BJ:x3^ @BH8^@@B@HBO6: IsO=@=s:Hxx:HB8H:@B3s3 @BJB


HB^)J:qRBt/@+@:H3@:8-@B8^@BHB)
pH
=8J@68/:8HO6-B3Jr3OBBt/E^-@B@:HH)HB

d @O6@8JO6: 8B^BH)J: H3sB @O BB/: 8BO6^ ^

HH8:HH8BB
d

=)J:m:
 'HHH:83JBO/@8JO6@j8BaH8^H8@H@3^1JJb/8HH

@OXHJ@8:H@B8J
sJB:@UsORB3s:B>H3JBrBH@@HO6BH:

HB :BH)h 8BJ H )B JO=@%sB H3s @ B)^HB@H@B^1J 8


8^8HBHHy3yH3Ht=)BH>H3H3dB@n%d8J)HJ:n83H v
N M
>B^H
8^H+H8Br:@@BH@BJDrBB:8:8:H@:8BrBr6^)^H)@kHs=@%sH
Exemplo: Declarao de variveis
Variveis tipo caracter
Variveis tipo inteiro
Variveis tipo Real

Variveis tipo Lgico

char cLetra, cSimbolo;


int iBase;
int iAmplitude, iExpoente;
float fPeso;
float fSalario;
double dResultado;
double dAngulo;
char bAvanca, bCalcula;
int bPassou;

42

m uKp ' &)(pH'>'" E

)

o8^HB)JH-B=8:@Om3^>sBBtR@k@Omk+J@8tR@-BHDs=@%s@H
BkH:8B-JB:3B^@+:rB8@HO6BH^u8J3:@:@38rHH@@-Hr8:@H-
8J8^H)::8JD:8^U8@H-BEsO=83^H
tipo nome_varivel = constante;

Exemplo: Inicializao de variveis


char cLetra = 'a'; /* um caracter, declarado com aspas simples*/
int iPrimeiro = 0; /*um nmero inteiro */
float fNum = 123.23; /*um nmero real (ponto flutuante)*/
int iHoras = 12, iDia = 13;

4d@:8B8>JBDJ:HsO=B%s:B>B-H:3H+^B8r@:8u8HB^3@8JBJ8^

H:+@+HH8J+@HH@@:B/:83d8:+8X^m^@B:

m u6Ip

IIK
-)^H8HH8J:D@B+HHBtR@@)H^>:@>rmHt6BJ+@Br)JB-BBJO/
-)^H8HH8J:D8JHB@OHD:@HB^8O^B8r@@HH)

bsb 5

b8^B8J:8@:b@B)t6:H3J@8JbBH:H@@B:yBO/3J:_Hb3^Hb3HJ:8B:
H-@H8^88JB

oH

x|//+
~/}
Exemplo: #define e const para constantes
#define PI 3.1415
const float PI = 3.1415

dH@ms_^@
x|/@Bw%:@:@B8HH8@H):@H@3J:8H)J
H:@BO/:88 3 ^B8 B J/@3JH :)^@H8B :@ 8:8@@H:BB B
:H@@H@:BBHBt
~{}kB^)J-+H:@@O/@@3Q3-^H)kHEsO=B%sH-+H8
BBH@H>Hr):@H

('

m dIK I6'"IBKH#K

vHJ:8:B@BBtaB:H8hw )J@:@BHBw@/hH3HO/OxB8:HB:H@H3hB
^H8 @:8HH:)H:@B^1 s=@: :8:H@:%J: B ^H) @:8
1t
BJB:@B@BkBJH8Hk:)8Hr 8HO683JBJ)Hu@B^B8B^+@BJBJ)J:>^
@:8 @3J3^)J O)H8 8@H:B@ HJ8@:H@ :H8 @@8 B @)
@B^B@B^E^-8@8@d/+J8r83k:@@8:BDsBBt6:+BBJB:1J:
b^@HH@:H8BBt
|{z/xH@@@8e^@nB8yJ:H_38^H@BH@3^1JH@:B8
8 HH@H @B 8 H@Ba sOR@3^H J3 HB@H_ ^@H:8:HBBBt
|az/xjH8:B@^BcB8B:HH8^@8:H@3^1JjH@@B@q8BHB@B
8BH@HJ=@3s3--) )JO6r@8BH8H
^@H:8HH@BBt {~DN_} :H8:B@:H8 ) H8H:6 B B H3^
^@H@@:H8@Btt
{~{z :B8:H@HB@ 8 H8J:6 H  B8^G
)J@B @O
/:8H:@3JH@+@HH=/:
|{z{x-^G
|az/x
43

Modificador
signed
unsigned
short
long

Modificao
c/sinal
s/sinal
int de 16 bits
int de 32 bits

s:tfDSRHH:8:H@B3t/:DHD^B8
Tipo
char
unsigned
signed
short
unsigned
long
unsigned

char
char
int
short int
int
long int

bits
8
8
8
16
16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HB@:B
8JH3^p5
Funo
abs()
fabs()
asin()

Prottipo
int abs (int x);
float fabs (float x);
double asin (double x);

acos()

double acos (double x);

atan()

double atan (double x);

atan2()

double atan2 (double x, double y);

tan()
cos()

double tan (double x);


double cos (double x);

x em radianos
x em radianos

sin()

double sin (double x);

x em radianos

exp()
pow()
sqrt()

double exp (double x);


double pow ( double x, double y );
double sqrt (double x);'

45

Descrio
Valor Absoluto Inteiro
Valor Absoluto Real
Arco Seno
Faixa: -pi/2 a pi/2
/* resposta em radianos */
Arco Cosseno
Faixa: 0 a pi
/* resposta em radianos */
Arco Tangente
Faixa: -pi/2 a pi/2
Arco Tangente
Faixa: -pi a pi
Tangente
Cosseno
Faixa: -1 a 1
Seno
Faixa: -1 a 1
Expoente - e
Potncia
Raiz Quadrada

log()
log10()

double log (double x);


double log10
J (double
fDir x);

s: O

Logartmo Natural
Logartmo na Base 10

3BH8H:,J3JB:3^B@:>H-BH8^@%JB

m #p'"Im'" I

^ @O6BBt 8 H)J8JB 3^@:@3HB @d/ BH@H8J HBJOmJH@BH:


^@O6:8J@J)JBJB:B@:D)H^8ORB^)s@:c3H^B8:
HHj^q^@/H8O/:
8JHH)JH HHHHH8BOv H%JO/Oh^ sHB/: H: sORB%s:H 3J
J:8B )^@@@BO ^ R:H8BJB3^ 6Bs@B@:)JH H :HB8H: B 88s@
^@O6:8J@
3.12.1 Operador de atribuio

^@/HBOaHwBRHHHB8BBH@HhHw8^8H FI dCFOU4^8:8B @wH8


:8J^H3jB^BO/H3OD /:3@BJH3 j@3:/:BBB @/H)JBjHB@H
Hr)^@H>-BORHHH@% JO=8%sH H8^8O6H+H:BBrHB8H

Exemplo: atribuio de valores


iIdade = 30;
fPeso = 72,00;

3.12.2 Operadores aritmticos


Operador

Ao

+
*
/
%
-++

subtrao
adio
multiplicao
diviso
resto da diviso
decremento
incremento

s:Jp-d8O/BBtR@OR)HHB^@8^
3.12.3 Precedncia (Hierarquia) nas operaes
Hierarquia

Operao

1
2

Parnteses
Funes matemticas
(sen, cos, abs...)
- (menos unrio)
++ ou -* ou / ou %
+ ou -

3
4
5
6

@Jt++:8O/6^B@+H>:8O/:)J:
8H^/B8N;JH[3\a~BH4^@@H:8@J3D
c8J)JO+@@@qJQJ@BQ^BO/@8J@qHQJ@8HO6HBq
H 4s^> 181@/+ 1J /@

46

3.12.4 Operadores de incremento e decremento

@:HH):@ ,B:@O6@@B
H @H8t6@-%-^B/:83
-88^HH)H:>^)O/:3H:+8BkB:H%sHB:)J:
ee



x++;
x--;

x = x + 1;
x = x - 1;

y/^@N;O/B[B\at~Q 8:Bn@O8J%^@@B_:)J:c^eH@)^HeBe:@6:8B4d@@8B

/:8HH 8: ^8O/:3B :H8J@BJ3 )


o HHB@@ :@O6:@B H
HH6BJ:)@r^+H:63J:8Bk:)::-@kH3O+JHBOU3-^@Om:@BW4dB@@B
x^@/HBtJB B:8:HwBw^BO/:@3)
H@hwJHBOnBxJ@O/@@B
:8J@H+HH63J:8J@8B:B@/BJ@8:^83

Exemplo: incremento e decremento

/=)J:mr BtyRBser
HH-101keQx`kser
+B:118^H+@H6BH:8H+H

x = 10;
y = x++;

' J> ed@xser


R8J11
@6ewy Hser
@6BJ11@8JhBBH:@3ddHb:8J8hBt=HBB
s HBOS`^

x = 10;
y'`
= d ++x;>d

3.12.5 Operadores relacionais


Operador

Ao

>
>=
<
<=
==
!=

maior que
maior ou igual a
menor que
menor ou igual a
igual a
diferente de

s:J$\rd8O/BBtR@/BB:B8^B:H
3.12.6 Operadores lgicos

B8J@^@6HB/@k)tH/@B8^@B8  J@HH@>^H)H^-)@@H8:B3k^8-t6BJ
H HBJ@/ H T8HHB sJH: : HHH8H@ 3HJHH8@: dH)J:
:8J=8tm::8J>8JHB@Ot/3HHH:+8J@:8H@@B-:)::>:8O6HBO/:
Operador

Ao (Boole)

&&
||
!

AND (E : (.))
OR (OU : (+))
NOT (NO : NEGAO (-))

:vt$bUd8/HB6:3B^H)J
r3^/:B)J:>^@r@@B-^@mHBt/@c/B@:H8:8:HkBB:H8:/B@t=@6B
8O/ @H:) @OR s/BHH:6 a H:@HBH JO/BBB BO/ :
^@O6:8J@ BHJB@: t s _ ^ @B 8 J:1J: J@ B/B
47

H:@@)JJ%s@H:)c@:B8J@Bc)^/BJH/HT8BJH_:):H@:8Bc3^
^@O6HBt6H   1)  IsB R_Hs B 3^/HB8J@ BH:H@H
@BJB:@:): ,B^@:1J:)HJBB:@:H:)^8HHB:
3.12.7 Precedncia (Hierarquia) dos operadores relacionais e lgicos.

H H:8J Bt6J ^8 ^ :@O/H3t/H JBJ3J3^H)^ : ^@/HB/:


BH^B8^QJ@HB:@3c8JHB/Om^B@@jH@3J:BH8BQn/:1HJ:@B3s@
8^@@@H:88kHqTr)O/B:H8@8JOYH:)^/+H+8^/@B8H:>BH^H3:
Hierarquia

Operao

1
2
3
4
5

>

!
>= < <=
== !=
&&
||

:Jtg)P s/:@BH@HB@HE^@O6HBt6HBB:H8^
/:B@Btm: 3:@:@8 8HJB@@ B8J %^/:@8B 3H:H@B @H8^@8HB
)
oH

Exemplo: expresso booleana em 'C'

F= A + AB+ AB
F =! A ||! A & &! B ||! (! A & &! B)

/:B@Btm: B@@:@8 8HJB@@ B8J %^/:@8B 3H:H@B :B8^@8HB


)
oH

Exemplo: expresso booleana em 'C'

F = (A + B + C B) B
F = (A || B ||! (C & &B)) & &B

RH^8H-D::@HBEsO/3HHBr83^HH8HB^/@H@3k3@^H@

Exemplo: tabelas verdade com expresses em 'C'

F =! A ||! B

B ^6@H@3 F =! A ||! B h3^H1sHB:3J  B^d/:H8B F = A + B k^@8O


H@ sHy@H3 J%t/BJ H H RHt6J@ B^8)sHH88J  BJ/:B@3

F = AB

B8J3

A
0
0
1
1

B
0
1
0
1

!A
1
1
0
0

A
0
0
1
1

B
0
1
0
1

(A && B)
0
0
0
1

48

!B
1
0
1
0

!A || !B
1
1
1
0

!(A && B)
1
1
1
0

m q")K)0

@ ^@ H8JB@:H8 B@HO J8 3J sO=8)sH B ^H) @8J:6X


)JB:@:@H)^@

^8 H)^HB:B@ B@HO :@ 8J sO=8%sH 3 ^B) /BHX
)JB:@:@H)^@
) ^@ B)^H@BH@ H@BO ^8 )J JOR@3JH B :H3 BH^B8X
)JB:@:@H)^@
HJ J@ B8^H@@H8 :@Hx^8 8H JO=@3^H B ^H1 @/:8:OX
)JB:@:@H)^@
D) 8O/x@HHt/:@8HHx@H/)J^H) @:@H8 /:/:@@)J:@BB
)
o +k 8JOm-Hr `JJ:DH@B3^



 
 
 
 
 

 

Exemplo: 1,567
real float 4 bytes
3.1415
100
101,345
69123
11.98 e+125
"b"
"agosto"
'x'
"beleza"
"1.234"
'7'
-25932
124
-255
75.500
1000000,1
"Eu odeio Logica de Programacao"
"Imagine, entao, linguagem C"

 

@-BHB^E^-83^HH)H:,^BBt6@U/@H3^@B+:,^@@3HJ:
int iA, iB, iC, iD, bE, bF;
iA = 10; iB = 20; iC = 30; iD = 40;
bE = 1; bF = 0;



0 (FALSO), iA menor que iB


Exemplo: iA >= iB
iB < iA
iC == iA
(iA + iB)*(iC - iA)
(iA + iB) >= iC
(iA + iC) / bE
(bF && bE)
(bF || bE)
(!bF)
(!bE)
!(bF && bE)
!!(bF && bE)
!(bF || bE)
(!bF || !bE)
!(iA + iB)
!(iA <= iB)
(iA || iB) && (iA || iC)
(!!iA || !!iB)
iA != iB

 











49

 


iA == iB
!iA == !iB
!bF != iE
(!!iA && bE) && (!!!iB || bF)

84^@H+rR8J:6+^B/:83kBJ@H3JHB@@B:/:H)H:>:8:8HX




iA + iB iC
iA * iB / (iC)
(iA + iB) * iC
(iA / iB) * iB
iA + iB * (iC iD)
iA * iB * iC * iD
-iB * (iC)
++iB * iA

\H



 

H8:8J@
/@@@BJB3 ^H8 8 @HB 8J H@ @3^HH%J:
B^6:H8HH
H Q
eHUi
@ eJH
@ JHr
H JH
H b 
beUH
H Q
He
J UHe
@ Q
HS
e
@ Q
He
H DQ8
e
8 DtS
e
bHjHHH8c@8^/:38J:q:@@3H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H<I<JGFKL=G;?MNO9NF<
Instrues a
serem
processadas

PA>A<@XO=>FOFNW;=GH<?;C>A>@CTHB?<YDFEWGHO=< >GCWH<FZFG?>9KN?L=M=QT;F<\MR[]N?OP^< VCO<C@XSR>T N_@a>9`UFGbVRN?GMF;COH>-< @O`FO_GCW<


`@XGG <=B?`RdFOF>F`O_T;C< `Ta<C`FSORT `FT9<h@cGF`OGYdHN?<CeFO9OFUC`R`ROOi< [ N;=fgW9G3@cO< THVFG3@cODFEGR<

Instrues a
serem
processadas

55

j G=dGFD=SFTkM=T;?`HNFMNJT;COd
Condio

Instrues a serem
processadas quando a
condio falsa.

Instrues a serem
processadas quando a
condio verdadeira.

P ;?TA@mWOd<FW9GFGdR;JGF>DFGS=T MFOA@cM?OT9MF;>`9GN?@MHNFNCeJT9O;C`FORO d VCT-l @


GxA?BCWs9G3OnNR@c`F<C>OV=`RGG3WtG@o_N_BF@c<CT;CT=>WO4GGu;CdJ>HyCpG OodN=`9<M?TzBOiO9q qr<uV?j O@XNg@XGHW{O<FV?G}OTH<?>FK|?OSFBCOHT+<wd v[
~MFOFO?W3`RO{NF;BT WO`=O<`HG @cG<FVJTV<?>F@XTO_MF< GH<F>F<?T=OJWW9O{G;C>RBWT
W`9V3ONC@HTRNRoAG3< @@XOJGR}W;?>OAGD=SF@qIGRT ;C>TRG\dF [>F~iO_ ;C|?q `OuTdGON<J;>Fo9<?O BCG^OFoHHGCBWTA;C>F@WHO-`HOG@
MFMFTT?WH;RO<C>;?`=@gT9B?><N=ORczGR;F_M?T_;CG>@HO \?V?mOR@AcMJ?T?FW_-[T_<

O8:8-HR:@B:B@B1^8t'HHH3
Inicializao;
Condio;
Incremento

Instrues a
serem
processadas.

O8:8-HR:@B:B@B1^@:@B8^8B

Repita

Instrues a serem
processadas.

H:)J
:H8
B
/:8B:B@B

HHBH@H@8H:@3 HB ^O=@3H:H H
8^)t6JB:R3JO=)^k@q8/HHQ
HvBH/8H:8Hx@3wH:B^hB8:HH@3^
8rHHHB)kBH:8
H :H)^8BJB n)
ol 8HJH8B ^@
/HB:@H H8J /@BB^H83
HH)t=H83}
C-
B8H
JB8
H
R:@3^B8B
@
@ O6:8JR@H )HH s=@:H):8B 3^
=)JO=): H @O/3H 8 :BBH B
H:) @@8J BO6J 6B@B^H8B
:8:8J:8+B+::8^+)JEsB
H :H)^8BJB n)
ol 8HJH8B ^@
/HB:@H :8J 6:@3^H8B @B :
HH)t=H)H:
i?_

Condio

RJ:H8BJB-:)O/HHDH-@BB^

Informe os Dados

Entrada dos dados

B8H BO6J 8:H8O=)^)J 8 /:8BJ


^H)J: J@H: )J JH ^8
@:8:H@O68 :)^@O B @H8O=) 3
6Bt/BJ 8J H:HBBHB B@3t6JH@3
^@ mHt/3H :B@O/ ^@ HH
:8:8J-8BH18:H3JH

56

m : KC#p'

i
1@@8J:B8B
@8 @@HB@H 1 8H B8JO6:B@HH B%^@:@3 8B
8 J:BH8JB8^Qc8H@8O=8B:@j8HO6:^Q:@:HQH3:B@O8@JBB^
sB3t :B8^3t6HB B=s 8O B^HH)B @ HHH 8H@8t6
H:8BHt/3:-H@)JrpHt/8H

Incio

Soma=Valor1
+Valor2

Informe o
primeiro valor

A soma :,
Soma

Valor1
Fim

Informe o segundo
valor

Valor2

i1@:8H:B@B@3
B:HH)H%kt6:@H:3HBtB3s-:6@@:8HB@B6B
8 /BH3H=8 Bwd@3J8 )B :@H8:@HBH B JH8HHBOm^ 8tbBH
@O=)tH=88^ 1 @H t/BBH H t/:@@@8 3 H3HtR8HH B
t/:8H:@B8t+HHqH@BO6:^k^H@BJB6:cm@t/BHt6@H@B6JB
JBtm^>:H8 Y@HH@>@O=3OH=8B

inicio

f Distancia*=1000.;
Este programa transforma km em m!\n

A distancia em metros eh = ,fDistancia


Informe a distncia em km:

Fim
f Distancia

57

Captulo 5 - Funes bsicas (funes de entrada e sada).

HHHadB8Hy8b8@H8^B3tD8yHBHO=8BHyn8@^8:)Hb@@HH8BJ:)HnH
sO=83s:B ^8/HB6: :HB8J@ B: @:B)^@B: /:8H@:)J:
:8^8HBH:+:UJO=@3H:H+D^U^@O/BB/:r)H3JB^88^>@>:HB8J:
B H3Ht=88H 6:BH@ H 8 HBJOm^H8H3  8@O6 H :HH)J:
/HBH:HBBH:@O/:BHH8:BO8tH/3HBJ@8Jj@j3t6HjH8emH/3J
)tBJB^)Bu@8QB:@:B8O=@8r:HB8J:D^@r8:HBHH@@)JBk-:8/HH+H
HHB: B@BO6JBBJ 8HB BHBOR8 HBH8J: ^@ 8:=)J3 @ JBB
:HH)J:@@8:@@B-^BHB^+@O6kr8^/@H8/:BB8J:,JBHBJB^8@:
+^B8J:E^8t6@
:H3^8BJ3 )
o )^HH@ B 8J6^ 6:H=)^ ^@ @ t/B:@H3 B
:HH)J: ^@ )JH3 @ B8@H: B@8tm 3 6Ht68J

HBH :BH8B
BJ:B8J)H-HO/H@-:H8:B@:3@WdB^8:@rH@8J@:HH)H:-BBQ=H8HR
/8^=8^R BB:@K RHd/ JB^3OR m@O/ mBO/BH BH
H:8:/H

=

m ,K%&$(IJpJ $ '0 #%" K

:H@@B B @:@B =H)HR 8:B:@ 8 HHH B 8H:H8J@3t


@ O/@8JO6Hc8=H8^c8HOm^ @8J:m^3JOm^ /B:HxwHJH:x@
^8t6: H@3tmJ:)JH ^@ :)HB ^@O/BBH: @ :3Ht=8 3t/3s@ H@
sO=83s:Bh@HH@3b=H8J'aH^8O/n:eH@8O6J:1J:n@jJHBeH_8
HJB @3 O8=8JH3 RBmJBHH3J ^ @B d@3 s@ B:@@@HO B^
@O/@8JO6HHDBt6J%J:@8r8:8:@HH3^
BO6JD::H+8:HH@DH@HH83k
=H)Jc_ )8-H3HH-@@HBJ8OT)H@/H8J/@>H-36JBH@@3 ) 6
Programa Exemplo: printf sada de dados
Descrio: Este programa demonstra a sada de dados com printf().
#include <stdio.h>
void main (void)
{
printf("Saida de dados com prinf();\n");
printf("Funcao de impressao com saida formatada!");
printf("Utiliza uma string de controle na forma mais basica.\n");
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FNN;C;COHOHdd

Hd?TO=> `OG?Rd=OTABF@hd?>aM=T?W4`Ga<NF;JO`dU?o`=NOI>TR<TAO9@WVCOp_<cOJF9`F`FU_`R@c`ioBF[`=dJ`Oz`F[ `R`F`kM=T?W4V3@XGFMHNF<?S=T


oG
HHd?d?TTO=O=>> OOd=d=TATA@h@hM=M=T?T?W4W4<<NFNF;J;JOOddk`=;OIOIRRTATA@@WWOOYcJT9F`iB[`RY`F`F`ROH\<FG?ROR9`FTF?`R;J`FO` V@GFMNR<CST
M
MH;JOFCO`O @ `?WTM=OAM?OO@XOd?@T-OHM=>M?@r>GA`=G9@ O@ `FT lkNgW_V3@XGH<F<?T
< @b<J>Ai@JNF;JHo @BH?WOa/:9BN?8d=:OI@H`RGIBM=+OA@XO:MJH>8G3:@gHG@<@@lY@NCHW_BV3@X/G:<R<CO B+Bt'HBH

Programa Exemplo: printf sada formatada


Descrio: este programa utiliza especificadores de formato para formatar
os dados de sada. Imprime nmeros inteiros, reais, smbolo, caractere e
string.
#include <stdio.h> /*para o printf*/
#include <conio.h> /*para o getch*/
void main (void)
{
printf("d imprime um numero decimal inteiro %d\n", 2);
printf("i tambem imprime um decimal inteiro %i\n", 11);
printf("f imprime um valor real %f\n", 13.12);
printf("e imprime um real com notacao cientifica %e\n", 8.1e+2);
printf("use dois sinais percentuais para mostrar um simples %%\n");
printf("c imprime um caractere %c\n", '#');
printf("s imprime a palavra %s\n", "modelo");
getch();
}

59

5.1.2 Seqncias de Escape

j8H^8n@@O/:8/BnHBB^aB8JjJBHaB_8H:H8JB3t@aBt6HnBe8J
J:@BHjB8B 8^B@B^@8B BO/3s@ H: @B8:HHB H :H@@@aBJ
@B 8:HH8 H :H8:@ 1J @O=@ H BO6:8J6: H@HBJ 3O/
J:HBH@HT+HBH-E-JH8HH3

?HF O J ? =GFGV
FF F=
O==G?>O9;?@THD=TIMFG`R<=Gk<?T VJZFo9NF;JOmFT@WHGGJ`
;@ ;J@gT?G?>TAOY@Cd;CTaN;=`?OGaM?gOR;C@F>H@cTG3@ =W@XO9GF>NF<IB9@;FRN=;F3G?H?GF|gAG?`RX
> >R>ROOk_JTAG3@J@X>HNJeFNFTM?OH;J>Fd Od
A O_O_<F<FV?V?OO<<a`9<B=NXWVFd?V?O_dRG< <
 =OAG3@X@T @ON;X9G3@c>N?`FO
9 ::H;C>8G3@R@ To= OBD?SR8T :HHB@+@H@@B

Programa Exemplo: printf sada com seqncias de escape


Descrio: este exemplo demonstra seqncias de escape de nova linha,
tabulao e acionamento do alto-falante do micro.
#include <stdio.h>
#include <conio.h>
void main (void)
{
printf("A\nB\nC");
getch();
printf("\nA\tB\tC");
getch();
printf("\nAB\b\bC");
getch();
printf("\nBeep\a");
getch();
}

O

I,%" 0 gc

m ,'+&)(KJp I-"IK

:BH@BH-@)t/HH>H@H8RDH+J3H@-:+@O6:8H/:>3+J:8HHB
3t6J@DJ:B+@:H3@:+B-@BH@Br@8
scanf ("%s", uma_string); /*l uma palavra do teclado*/
scanf ("%d", &um_inteiro); /*l um nmero inteiro do teclado*/
scanf ("%f", &um_real_tipo_float); /*l um nmero real do teclado*/
scanf ("%lf", &um_real_tipo_double); /*l um nmero real do
teclado*/

60

: BH@B H@:8R HB^@8 HH H8 B^ :H@:B8:H8H3t/H H Bt6HBH


8^@@@HH3J 8O RB8J'c HH3JBJ H H@:8R 8H 8tRH8 H
8^8O6JH>
H@:8ca F,t5liayp/x fd=gpt5l/gcdFOUcaytFfdJDlyx]wQdptgBp6
t5liaqp-xfd  gp t5 Ol/ gc
d {}]-N:|3atz
6J3B8HB8:/ :@8BQB@6:8Jd/:k)JB^3HB
:8t6y@H8:
\~ P{~xc>~/}]N/z/~Qc]N}N_/
P/|/|Ma~| P///n~/P/||/>x~]N_nx~iNI/}~Vx||{x~
/}N:|{~iNI//}Edy:H8HH@:B)HBt/@bHbBtpJBH3HH:@38HHH8H
@O=8:8^8@c d
sayt fdHlx]wdptg UB3Jj@/@sd=@3sB/H@q^8e)O/'3Jj:@@/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J3H-BrHB:@R

b@HH@BcH3:@R@)J:@@:3@:8^qBam:H8cH):j:)t6HBB6JBHBH
8HHr@:8/HBO6:H>@BJ)^+8+J:)J:BH8
Programa Exemplo: scanf entrada formatada simples
Descrio: este programa mostra a utilizao da funo scanf. Observe
os caracteres '&' antes das variveis inteiras.
#include <stdio.h> /*para o printf e scanf*/
#include <conio.h> /*para o getch*/
void main (void)
{
int iValor, iVal1, iVal2;
printf("digite valor inteiro: ");
scanf ("%d", &iValor);
printf("digite dois valores (separados por espacos): ");

61

scanf ("%d %d", &iVal1, &iVal2);


printf("%d + %d = %d\n", iVal1, iVal2, iVal1+iVal2);
getch();
}

Programa Exemplo: printf scanf


Descrio: este programa mostra a utilizao das funes printf e scanf.
Observe que scanf primeiro permite a entrada de uma string com no mximo
127 caracteres de comprimento. A posio 128 deve ser reservada para um
nulo. A string no precisa do caracter '&'. Quando o valor inteiro iIdade
recebido, o caracter '&' obrigatrio.
#include <conio.h> /*para o getch*/
#include <stdio.h> /*para o printf*/
void main (void)
{
int iIdade;
char szNome[128];
printf("Qual seu Nome: ");
scanf("%s",szNome);
/* nome o endereo da string "nome" */
printf("Qual a sua Idade: ");
scanf("%d",&iIdade);
printf("Seu nome eh %s e sua idade eh %d\n", szNome, iIdade);
printf("Formatando o drive C:");
getch();
}

5.2.1 Usando scanf com strings

: BH@B H@:8 8:8:@H /8J:m @d/:8Jd/ B /@@8 H8^8HHB


8HH 8^)H :H@BV
BH BH3B :8:8JO6BJ 8t=H)^ )H :H@@8^
:8t6n8n8BH36Hc8J@3H^@H8:8t/OrH//@@:D8BBy%HB^@H@:H8BB
@HBr6B/BJDr@3:H
Programa exemplo: strings com espaos e scanf

Descrio: Este programa recebe uma string atravs de scanf e imprime a


mesma com puts. A string pode ter 127 caracteres, desde que no possua
espaos...
#include <stdio.h>
#include <conio.h>
void main (void)
{
char szString[128];
printf("Digite uma string e tecle enter: ");
scanf("%s", szString);
printf(szString);
getch();
}

62

R^H8B^kmHt/8H:k):@:@BBk6H^@@@B^k8/-:)t/BH-@r8tRH8:

6H^B@ H@:8 3^@:8BHH @O6 :8/BH H @8H:m: HBHB:


@ O/@8JO6H )t=B8: 8/ :)^@:8 @8O/HB H )O=H)^ 3B
:HH8B H8:8 :H)^@8HB n)
pl BtR@:@ ^ @H@@H8:@@H3t/: H
Bt6HBH<2%3J 23
:H@:BB:H8HBO H 8O6J3H ?2%3 HB 8B J@ H@8@ 8JB@:@
@O/@8JO6HB@8t6H8Jq8O/H8j3J^8HB:38^8t'c1e@/H8HO/
^@@B-@Hr86J-%8J{ :H)Bk:H8:@@:B@BB-H:)6k3:+8J@H@BJB
6HORBJ 8BJH @:B)/ 8:@:BH@@B B :B@:B8@H@H8t H
Bt6HBH
2%3 6@t/BHr@Op^8J-k:)O/H@r@r@O/@)JO/8J: HHH3B^
-B-:H8H8k3r/:38
Programa exemplo: scanf com delimitadores %[]
Descrio: este programa recebe uma string atravs de scanf e imprime a
mesma com puts. A string pode ter 127 caracteres, desde que os caracteres
estejam dentro do conjunto delimitado pelos [] (caracteres maisculos e o
(branco)).
#include <stdio.h>
#include <conio.h>
void main (void)
{
char szString[128];
printf("Digite uma string e tecle enter:");
scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]", szString);
puts(szString);
getch();

: H8:H@:3@HBtaHvB6JBH 23wBHv8@H@:3RwH@Bv@O6:8J6:
H8Jr8tRB8Q)J^3-HB@O'8B8t6HH@Hr)QB@k@O6@8JR:-8HB8H
:H@@H@:BBHB-@8t/>8J:@8BJ@

Programa exemplo: scanf com delimitadores


Descrio: este programa recebe uma string atravs de scanf e imprime a
mesma com puts. A string pode ter 127 caracteres. A entrada da string
ser interrompida quando o usurio digitar uma tabulao ou ENTER.
#include <stdio.h>
#include <conio.h>
void main (void)
{
char szString[128];
printf("Digite uma string e tecle enter:");
scanf("%[^\t\n]", szString);
puts(szString);
getch();
}

63

O

m ,mq&)pK#K$"sgp"ImHJ'KHpJ-"'p)0 pgB#+,p

j:BH@B+JB^R^%JB8H1t=H8kH@O/H)JO/@BBH@/8r@m^
B J@@HH) )J ) @O/@3JO/ H@B B @8 @ 8tRH)k :H@@3
/:HB@O/DHJBJ>^-@/:)J/@>^@-:@/:@Br@+JBH-BJ+B^@HH@H8
8%O> 1WBH8QH8@O=8r8J3kH:)JOTr:3O/HH+HkBBB^+8Hk:HB@8^
J8^8B 8H TR
] 6
d HH8OR8 3Bt/:8@H@ :@8:8
/:@msH3x@O/vw8t=B)QH3^ORx)^6:)Jh^ysH3t6:h@:BHt=8
H8Hw)JBH@BOwmHt/B@bHH:@r8tc8H8h8O=H)@:8t6BH
H3s @b6:@Bx H3:HH8@)Jj
r3H@)J: )H J8^O'xJ@)JB nHo l
@O/+:H)J8t/Xk:3@HBr)O=H8
:HH8BwH8:ORB:H@8:8t=B8vBw8O/:)HO/:8H@8v@O6H8J
^H8:/O@/:3J/@ 8H ^)tm^ :H8^ H BH3^Q @@H@B JB:@B
B^H8 8 8O6:8:O6xHh^@H8H@Hw:HH8 ) HH@HwB @HB8t/H)
^@:8Bk@@8^8O6+)O/:3HO/>H@B-H>J@8H
Programa Exemplo: puts & gets
Descrio: o programa a seguir pede para que o usurio digite uma string
e usa a funo gets() para recuper-la. O programa usa a funo puts para
mostrar a string na tela. Voc pode usar a tecla Backspace e espaos em
branco para editar sua entrada.
#include <conio.h> /*para o getch */
#include <stdio.h> /*para o puts e gets */
void main (void)
{
char szStr[128];
puts("digite uma string e pressione ENTER ");
gets(szStr);
puts(szStr);
getch();
}



mX

m ,'+&)('BmI"I'0Ig

j:B@H:8BHH-@r:@H8B-JB:3ORHj/:B@@OE1@/@8J/-@-:8O/HB-B
J:@BHB /BHt'@^88 B 'Ht/3Jn :HH83 8B B^B@ H3t/
:H8:H@HBBDJHBtXH-H)t/BBr@DJ@H
Bt6H+J@@-@@BH@ JB^@Rp6
Programa Exemplo: getch
Descrio: o programa pede que o usurio digite um letra. O teclado
verificado com getch() e em seguida a letra impressa com printf. O
segundo getch() apenas segura o programa para a visualizao da tela.
#include <conio.h> /*para o getch */
#include <stdio.h> /*para o printf */
void main (void)

64

{
char cLetra;
printf("digite uma letra: ");
cLetra = getch();
printf("\nVoce digitou a letra %c\n", cLetra);
getch();
}

m '" )II)0

r):@ 8 B:8@HO/BH ^@B 8 8HH BO/x BH8O=8 :8:8:O8:H


8J6^yH)H:6^__B@8^Hn8H:H@Byy8^=3^@_eJ@@@eB^_B:H

sHBO/:BH6%s+-6HO6BJDBk:@8:@BHB _
 t
Expresses
Mdia: M = (A+B)/2
Variveis
Os nmeros: iA, iB
A mdia: fC
Restries
Os dois nmeros so valores inteiros
Descrio
o programa calcula a mdia de dois nmeros

incio

Digite o primeiro nmero

iA

Digite o segundo nmero

iB

fC=(iA+iB)/2.0

A mdia fC

fim

65

#include <stdio.h>
#include <conio.h>
void main (void)
{
int iA, iB;
float fC;
printf("Digite o primeiro numero: ");
scanf("%d", &iA);
printf("Digite o segundo numero: ");
scanf("%d", &iB);
fC = (float)(iA + iB)/2.;
printf ("A media eh: %f", fC);
getch();
}

r):@_8_@:8HBt6BJa^8eH:BeB:Hn8J6^en8^68JcJaB:Hn@
t/H3 8H8t/OR8 H 8:8^6vBH/%J KHt/BH 3 :@8^@3J3
_
 t

Variveis
Os dois nmeros: iA, iB
Media: fMedia
Restries:
Os dois nmeros so valores inteiros
Descrio:
Este programa pede que usurio digite dois nmeros e depois os
imprime na ordem contrria leitura.

Incio

Primeiro nmero?:

iA

Segundo nmero?:

iB

iB

iA

FIm

66

#include <stdio.h>
#include <conio.h>
void main (void)
{
int iA, iB;
printf("Digite o primeiro numero: ");
scanf("%d", &iA);
printf("Digite o segundo numero: ");
scanf("%d", &iB);
printf ("Segundo: %d\n", iB);
printf ("Primeiro: %d\n", iA);
getch();
}

t r):@

8 H:)HHO/8J :@ BH@ t/:  8:O6: 8^=)J JB:@


:8t6+^Et/:uBHR3sDrmBt/BJ3k@H)^8BJB _
 t

Expresses
Mdia: M = (A + B + C)/3
Variveis
Trs nmeros: iA, iB, iC
Media: fMedia
Restries:
Os trs nmeros so valores inteiros
A mdia um nmero real
Descrio:
Neste algoritmo o usurio deve digitar 3 nmeros e a mdia
calculada e impressa.

67

Incio

Primeiro nmero? :

iA

Segundo nmero? :

iB

Terceiro nmero? :

iC

fMedia=(iA+iB+iC)/3

fMedia

Fim

#include <stdio.h>
#include <conio.h>
void main (void)
{
int iA, iB, iC;
float fMedia;
printf("Digite o primeiro numero: ");
scanf("%d", &iA);
printf("Digite o segundo numero: ");
scanf("%d", &iB);
printf("Digite o terceiro numero: ");
scanf("%d", &iC);
fMedia = (float)(iA + iB + iC)/3.f;
printf ("A media eh: %f", fMedia);
getch();
}

H r):@
8 @:8HBO/BH 8O6 @B:H@Hd O/B H
/BY@8^@8XBB/3s,mBt/3H-Br:B8^8BHB _

Expresses:
rea do tringulo: A=bxh/2
Variveis:

68

t//Y:8:8B

A base do tringulo: fB
A altura do tringulo: fH
A rea do tringulo: fA
Restries:
A base, altura e rea podem ser valores reais.
Descrio:
Este algoritmo calcula a rea de um tringulo retngulo
depois que o usurio informa a base e a altura.

Incio

Base e altura?

fB, fH

fA=fB*fH/2

A rea fA

Fim

#include <conio.h>
#include <stdio.h>
void main (void)
{
float fBase, fAltura, fArea;
printf("Digite a base e a altura do triangulo, b h: ");
scanf("%f %f", &fBase, &fAltura);
fArea = fBase*fAltura/2;
printf("A area eh: %f\n", fArea);
getch();
}

Dt r):@

) H:8BBt/BH ^@ B:8 6:8 8 8J HRBH8H/:HB@


@ H:B@H-@R8JBmkk/BQ8^s=8J+^rB:H>sBBO/:BHR%s
mHt/8H-B+:H8^3BJB _
 O

Expresses:
rea da circunferncia: A = PI * r * r
Permetro da circunferncia: P = 2 * PI * r
Variveis:
O raio do crculo: fRaio
O permetro do crculo: fPerim
A rea do do crculo: fArea
Restries:
O raio, o permetro e a rea podem ser valores reais.
O valor da constante PI ser um define
Descrio:

69

Este algoritmo calcula a rea e o permetro de uma circunferncia


depois que o usurio informa o valor do raio.

Incio

Raio?

fRaio

fPerim = 2*PI*fRaio
fArea = PI*fRaio*fRaio;

Permetro
rea

Fim

#include <conio.h>
#include <stdio.h>
#define PI 3.141592654
void main (void)
{
float fRaio, fPerim, fArea;
printf("Qual o raio?: ");
scanf("%f", &fRaio);
fPerim = 2.f*PI*fRaio;
fArea = PI*fRaio*fRaio;
printf("O perimetro eh: %f\n", fPerim);
printf("A area eh: %f\n", fArea);
getch();
}

70

Captulo 6 - Estruturas de controle e de repetio.

m g#Km)6/IOpKpI

6.1.1 Seqncia

B b8HH:@3^c:@n8yH:1HnH_8^8O6JBnBy'Ht63J_@3_BJHH8JB3^
@:H8:@B-BH@HH@B@OR+6H^8H-HH)tRH@3

Exemplo: comandos em seqncia


clrscr();
printf("Letra: ");
letra = getch();
printf("Valor = ");
scanf("%f",&Valor);
resp = valor * 1.25

/*
/*
/*
/*
/*
/*

Limpa a tela */
Imprime */
Atribuio */
Imprime */
Entrada de um valor */
Atribuio comando de seqncia */

6.1.2 Seleo

B 8HH:8B^ ^) ) B@8H H 8:8t6H@ 3 s6Ht/)J @O6^)JB


@HH@@B-H8t/D:@:U^>:@H+BH8t=B8::

Exemplo: utilizao de seleo


if (numero % 2 == 0)
{
printf("NUMERO PAR\n");
}
else
{
printf("NUMERO IMPAR\n");
}

6.1.3 Repetio

B 8HH:8B^ ^) ) B@8H H 8:8t6H@ 3 s6Ht/)J @O6^)JB


/:@3^H@3rH)-^UJ:H>HH)O=H8J@

Programa exemplo: repetio


Descrio: este programa pede que o usurio pressione qualquer tecla.
medida que as teclas vo sendo pressionadas os cdigos das mesmas vo
sendo impressos na tela: o caractere e o decimal equivalente da tabela
ASCII. O programa interrompido quando a tecla ENTER pressionada.
#include <stdio.h>

71

#include <conio.h>
#define TRUE 1
#define FALSE 0
void main(void)
{
char cTecla, cContinua;
clrscr();
printf("Mostra as teclas pressionadas.\n");
printf("Pressione qualquer tecla: ");
cContinua = TRUE;
while (cContinua)
{
cTecla = getch();
if (cTecla == 13)
/* (13) cdigo da tecla ENTER */
cContinua = FALSE;
printf("Tecla: %d | %c\n",cTecla, cTecla);
}
getch();
}

6.1.4 Atribuio

LH8J3kDtfDrKd@OmHBtH^t=HHBHBBJ

 m

m $'pOImKJ

b8HJ:8B_@e@O6:8HaHBHO6Oke@B@:HH8nBaBJ:BB@BaBnH:)HnB
6HO/BJ+8Hr8:@r)-3^B:@@8BkH3J8:@@H@B
Sintaxe:
if (condio)
{
declarao 1;
}
else
{
declarao 2:
}

)HJ:83 BH@ :HH8:)H 3:@:H8B 3t )sH:8HH B


sO/BHH:' K^@H3:@O sBBt :@@O6:8H H J 8@H@@B 8O/
BJ:B8JH3 E8@8 8J)t/OR8R8^@@H@3jHB:BusHBO8J8Bj8O6J
8HJ@@B @O'rBJBH8JH3
3HJ:@8QD^r3BJ:@3 )HHBr@OH8J8H:
^ 8H:8:8H:JH/:)BJ:@3^B@+H3@rH83J:dTJ:8H-J@@@B
8BJ:@3rBtH8^@3:

if (condio)
{
declarao 1; /*comando composto */
declarao 2; /*mais de uma linha de instruo */
}
else

72

{
declarao 3; /*comando simples*/
}

Programa exemplo: if-else


Descrio: este programa verifica se o nmero digitado pelo usurio par
ou mpar.
#include <stdio.h>
#include <conio.h>
void main(void)
{
int iNumero;
//clrscr();
printf("Verifica se o numero digitado eh par ou impar.\n");
printf("Entre com um numero inteiro: ");
scanf("%d",&iNumero);
if ((iNumero % 2) == 0) //se o resto da diviso for igual a zero
{
printf("NUMERO PAR\n");
}
else
{
printf("NUMERO IMPAR\n");
}
getch();
}

6.2.1 If s aninhados

BB@HHH@8B8q8HHH@BQ@HH@OR@Bq@8^Q8q8HHH@3q@
8)HJ:88kH@8

^H

Programa exemplo: if aninhado


Descrio: este programa pede que o usurio digite um valor inteiro
qualquer positivo, negativo ou zero e depois imprime na tela o resultado:
maior, menor ou igual a zero.
#include <stdio.h>
#include <conio.h>
void main(void)
{
int iNumero;
printf("Verifica se o numero eh >, < ou == a zero.\n");
printf("Entre com um numero inteiro: ");
scanf("%d",&iNumero);
if (iNumero == 0)
{
printf("ZERO\n");

73

}
else
{
printf("voce digitou %d\n", iNumero);
if (iNumero > 0)
{
printf("o numero eh maior que zero\n");
}
else
{
printf("o numero eh menor que zero\n");
}
}
getch();
}

Exemplo: mais ifs aninhados


if (x)
{
if (y)
{
printf("1");
}
else
{
printf("2");
}
}

^8Haj 3HJ:@3dc d+@/HH/X )g I


 UUdced,:8J :)JHB-B
aj J:BqmH^8H^B:B@H@BQ^)s>88HH:@3qH:8HB8^H@BB
HH8^8:@H@8BnHeH@%s@a8nBJ3^@Bn:B8Je@BnH@)Ja)d8H:c
:8J 6:H@:%HUbB@3^:y3:8^H8BbB^B) H@B@HbH38JO=/BH:8B
B 6Ht63JHBt B @:83t B:8:B@:@B 3s:):@:H :H8:H@:%J:
H@H@@ HB@:

smA

m )dIItIpKJ

B8J 3HH:@B HH 6Ht6%J ::)JO): J=@3J@ B3@:HH%sBH:)J


8 ^8O/b8J_@B8JyHy8HH8J@)J:_H8::/:a:y@/:)JO6H+H:8:H_H
:H8:8t/ 8H 8^BHB@H:B@@w 8H^B8JH)t BJ :B3J 8HJ:8B ^
@B^)^ H 8@J@@B: ^@ @3JH3 @H8JB@HB: :@HH 3:H8J:)Jh
8HJ@@Bj@HH@@Bj3J:H3HHB@@Ba8^1s@:HB3J8^H3H@H:BH@X
8HJ@@Br/H^UrH@3BB/D:=8JTr8:8H+Br3HJ:83r ^)^H
Sintaxe:
switch (varivel)
{
case constante_1 :
{
declarao;
}
break;
case constante_2 :

74

{
declarao;
}
break;
default:
{
declarao;
}
}

Programa exemplo: switch-case


Descrio: Este programa pede que o usurio digite um nmero entre 0 e 2
e depois avalia o nmero com um switch-case, escrevendo o nmero
literalmente.
#include <stdio.h>
#include <conio.h>
void main(void)
{
int iDigito;
clrscr();
printf("Entre com um numero [0 .. 2]: ");
scanf("%d",&iDigito);
switch (iDigito)
{
case 0:
{
printf("Zero\n");
}
break;
case 1:
{
printf("Um\n");
}
break;
case 2:
{
printf("Dois\n");
}
break;
default:
{
printf("ERRO: de 0 a 2\n");
}
}
getch();
}

Programa exemplo: switch-case

75

Descrio: Este programa pede que o usurio digite um nmero entre 0 e 3


e depois avalia o nmero com um switch-case, tal como no exemplo
anterior, entretanto faz a leitura atravs de um caractere ao invs de
utilizar um inteiro. Depois escreve o nmero literalmente.
#include <stdio.h>
#include <conio.h>
void main(void)
{
char cDigito;
clrscr();
printf("Entre com um digito [0 .. 3]: ");
scanf("%c",&cDigito);
switch (cDigito)
{
case '0':
{
printf("Zero\n");
}
break;
case '1':
{
printf("Um\n");
}
break;
case '2':
{
printf("Dois\n");
}
break;
case '3':
{
printf("Tres\n");
}
break;
default:
{
printf("ERRO: Numero fora dos limites! \n");
}
}
getch();
}

Programa exemplo: switch-case


Descrio: Este programa monta uma pequena entrada de dados utilizando
switch-case. A captura da opo do usurio feita com um caractere. Cada
opo leva a um direcionamento onde um conjunto de procedimentos pode ser
realizado.
#include <stdio.h>
#include <conio.h>

76

void main(void)
{
char cOpcao;
clrscr();
printf("[I]nclusao\n");
printf("[A]lteracao\n");
printf("[T]ermino\n");
printf("Qual a opcao: ");
scanf("%c",&cOpcao);
switch (cOpcao)
{
case 'i':
case 'I':
{
printf("Faz uma chamada a um procedimento de inclusao.\n");
}
break;
case 'a':
case 'A':
{
printf("Faz uma chamada a um procedimento de alteracao.\n");
}
break;
case 't':
case 'T':
{
printf("Faz
encerramento.\n");
}
break;

uma

chamada

um

procedimento

de

default:
{
printf("ERRO: Opcao Invalida\n");
}
}
getch();
}

63

m $'pOImK

B8J_8BJ:8B6HH:8B_8
F)8H: FRH:3Hc^@_@v/@@3^my@8^@@8Hy
8^@@H@B-BtX^O/HBB:/W4d8H@Bkk8H@:H@%QBtYHH@@+k8^8O6JB-B
6HO/BJc@:BBa@O/ja:H8@^@a@BH@j
F=BH^ FG
F%3B^ FP^B@Ba
8^@@@HH3 B B8^@H)J:^@B s=@:H8:@3 H )^@:@8B B3s @O
sO=8:H@B@8HB@H8B
F)8B: Ft q8:8:HBBj%t+@H:8Y
)BB^
@B--BHHH8JB3@B@-=8H@/DJH
W |/}
while (condio)
{
comando 1;

77

comando 2;
}

H^@@H@Bq^@H%^@Oy3^/@H8Bx^H:@BH/3
'r O /E^>H

6:H8BHH3 8H1@^^

HHJ@@B8:H

@ ) )HJH@3 sB:8H8:BH: ^ 8H@8^8H _s:H


8HJ@@B^>@O/3j/:@%^@BJ>BHD^@+-8^8:B@B+HO/@^B8+BH:8

Programa exemplo: while


Descrio: faz a contagem de 1 at 10 com o loop while. A comparao
realizada NO INCIO do loop. Se for falso no executa o loop.
#include <stdio.h>
#include <conio.h>
void main (void)
{
int iCont = 1;
while (iCont <= 10)
{
printf ("%d\n",iCont);
iCont++;
}
getch();
}

A A X

m )dI$# + "

8BJ:@3rBtXHH:B8r8>^H)-H-B:3kB+BH:HH88
)^HBHqO/:@H3J:)H:a)eBBjHH:H3H@BHBHHBH@@H:838^@HH8Ba
HH6BJ:)@ HBH@@H@@B:@3 ) )BJ:@8 H Bt=@BBH@3 J@
8H^B@HH3rB8c)O/q:8J@@BB:BO+s=83s@B8:8t'HH3a8:8
8^@@H@B 3H 3^6:H8B B /BH:8B ^@ H:8J s=@3^B H
8^8O6JHqBH:88J)t/HB:3JH3t@RjB3JO6:B@O,^8:8BcB:8
JO6:H@O6 HH6BJ:)B BH@H@ J:B:/ 8H@ sOR@3sB H
8^8O6JH+BQ8:8Q3O/-H3JOmHHr@HH,sH+:@-r8H:H)JH)tr/H@B:Y
H:8
W |/}
for (inicializao; condio; incremento)
{
declarao 1;
declarao 2;
}

pbaaycaHg8 8HH@@B '^ s=3^ 8:@O6@B^ 8tbd6:@HH H


Bt=BHHH88r^B8@^H8HBBtTH@r@O/rHHB@@H:8BOU/@EsO=@%sHO6H@
Hr)^8tm:H-3-BH^t
gpfagHg 8: 8:/:B8B /HH:@8^8H ^8 J@8J sO=@%sH H
8^8O6JHqBBH^8J)t/HB:3JH3t@RjB3JO6:B@O,^8:8Bc3H^
JO6:H@O6
78

@OpOO/+
l=d5HBdHptg/HDHD
HH:8@eaJ:@:s6_8HHys=@3^BeH_8^8'JHeByBB^
:8^UkB@:-8-/:8BJ@B

UkBH^qRBt6@O6/:B3^@3-:8^8:3H-8:@:H88kBOuJO/BHH:'
Tq8^@@@@Br8O/DH:8J@Br)r8HJ@8rB-3H:
4d@H%^@O 8H H B@:  B@8J: z aypbaacaAHg6 ,gpfagRg76
aypOOl=d5dptg@r)HHr@dY^BBB^8B^/HH@3DsH@@H>BZ

Programa exemplo: for


Descrio: faz a contagem de 1 at 10 com o loop for.
#include <stdio.h>
#include <conio.h>
void main(void)
{
int iCont;
for (iCont = 1;iCont <= 10;iCont++)
{
printf("%d\n",iCont);
}
getch();
}

Programa exemplo: for


Descrio: faz a contagem para cima e para baixo em duas variveis. O
loop encerrado quando os valores das variveis se igualam (em 5).
#include <stdio.h>
#include <conio.h>
void main(void)
{
int iCont, iContB;
for (iCont = 0,iContB = 10;iCont != iContB; iCont++, iContB--)
{
printf("|%d|%d|\n", iCont, iContB);
}
getch();
}

QBB^3:u66+-B@H)s@B:8H+B8BJHH)J8BJ:88Q^B8H
inicializao;
while (condio)
{
comando;
incremento;
}

79

Agwp

m $dI$#sp

8BJ:@3rB>M^H)H+:3^@3j/HH@@@r)J+H@8-@j/@8B:H@3
B:8 B::B8JBB :):@:)@ )^@:BBB BsO/8HH@6hJ:
8^@@H@B--JB8JHH8k:B@HHHB:@88H+^@DQBH:-@> HkBH:H3HHB
@HB+J:)J-8HEsH

|/}

`RT4 M=T?WOH;?`FT+
FN=dFGXM?T_;C`NFDJST


Programa exemplo: do-while


Descrio: faz a contagem de 1 at 10 com o loop do-while. A verificao
realizada no final, logo o loop do-while sempre executado pelo menos
uma vez.
#include <stdio.h>
#include <conio.h>
void main(void)
{
int iCont = 1;
do
{
printf("%d\n",iCont);
iCont++;
} while (iCont <= 10);
getch();
}

m $K+""-,'#+&$(K:KHK#K

6.7.1 O comando break

4d@:8Bhw8H:H8HH8tcHH)^8O6x8HJ8@BxmH^HB8t6hBw8wB@:

HH 8JB:8BJBHH8J :H@O3/ BH^ 8^)6JH


6^B@B^8-8r=8J:s63BJ:83kH:)^@3-BH:

6H6BJ

Programa exemplo: break


Descrio: este programa tem um loop for que faria a contagem de 0 at
99. Mas a existncia do comando break interrompe a contagem quando iCont
for igual a 10.
#include <stdio.h>
#include <conio.h>
void main(void)

80

{
int iCont;
for (iCont = 0; iCont < 100; iCont++)
{
printf("%d\n",iCont);
if (iCont == 10)
{
printf("saindo..\n");
break;
}
}
getch();
}

6.7.2 O comando continue

8 HJ@8B 8J)^HH8 B s@ H %t=@O HH@OB/3J:1H BR@


6H:8J3JO/@8BkB+BHJ>B@H+r3J:)H+^8-@3^)sT)-H:8

Programa exemplo: continue


Descrio: este programa imprime somente os nmeros pares. Quando o
nmero for mpar o if ser verdadeiro e o programa vai pular a execuo
das linhas abaixo e continuar na prxima iterao. O resultado a
impresso s de nmeros pares, pois o if dar zero como resultado.
#include <stdio.h>
#include <conio.h>
void main(void)
{
int iCont;
//clrscr();
for (iCont = 0; iCont < 100; iCont++)
{
if (iCont % 2)
/* 0 par, 1 impar */
{
continue;
}
printf("%d ",iCont);
}
getch();
}

B^nB@^Hk^H8Hy_B>^BBHy88@J@@B)^3^HB@_HBb8B_^@n
8^8O6JH B mHt/8H s :6BJBJ@)J 8O/ J@8H )^@:H@8^8H
H:8:H-)J8^HB8+k%H^
B @:8 ) BH^ B:u 66I 3H^H1JHBt =8J@6 BJ:@8J
HH6BJ:)@ B BH^ 3@8^H J:8H 8^8HHH8:8H @8J: H
:H@BBJ:):_HHHQb3H^y8:)^HH@Z
B8H)t'^8_@H/3J:8H3HH
HH:BHh 8^)^HB@H@B J @/HH B 3H^c
H :@ xsO=8%sH H
8^8O6JHbBH@8J)^@HHB8:@Hb@8J6B^B@3J:)Jb@B^6b:8JH@bB
HH6BJ:)@
81

A


8BJ:@3rB^)-:8tpJ+-6BO/BJ+B^8H3^@B3^@:83
m )'gOKKps)

82

Captulo 7 - Funes definidas pelo usurio.

O<@mK9BFNV3@mT9T<oA@cO?RWTO9;?< >RG< G?sROFMW9BNR@X;C>FOT_`F<THqt< VJTOFB?>HM?l T ORM=oFT?TW@mVJO dN?M?<JORSF`FT T9< @XGG d?O=MF>9T?NgW O=WBCG>;JN=>dG NC`O=<F`NCG W9V=dFdRGNCW9<NJq >FOF`MJOT?W [
;V3RfX;@mW9T>oAG3@XG=@c@c>FO?T WO9O;C>R`RTiG VJq4TF`M?M?GT?T9W;OH<?;?TAG`F@m@T_W<G^[ KTBFG=JAOOA@B@cORWO `FGT ;C>=<=TNCG?W9W V=`RdO N?M?THMJN=dFTFM?GW9ODJV?@ EdRGGC< sO N`d=OF>FTF`HO-oG^@XN=GFM?`FFO?THO W9< GR;C`H>V9G G @ToAV@c@c@XGO?TFd?Wo-OHO_@M<^OFN?WTOHOH;JBCD?OFWS`GHTOH;C<q >O `BTWG
MFTJWO;J`FT9<q]MOFWOR`FO<9BF;=D?EG<w[
dGJo9V9N?@HTN?od@XNCO?`RWHOFOF``FGaT@uGb@XVCG?TO9`RVG @XT?M3_@CGNCN?O>F@O=WG<=;BC>FOTI< `RTkV9@p9MJpV`@JNCNJO_oFTz<u[ 9BF;=D?EGR<\q^TK_B?G VJGA@W3NC>G{WO9NCT@

Forma geral:
tipo nome (tipo parmetros)
{
corpo da funo;
}

-BN=<TIV?Ti`RvTkFM?<FT=VFWG=O9M;NC`_T N?MF O T >H NbVCT`ROHTd?T@c O d=TA[ @4KB?GOB;JDFSFT `RG?G`RG?TdJG3@4O=>@O?_l<`RT


JOO@@J?NCW_Z=G?>GNF@T_<<K9v BJGn @gGFBgMFWGO =GA@dRSRNFTI<?>FTO+<q O@m<?oGBCVJWOAGH@X;CO=>`THO <K_VCBCT-O9@ ;C`RTIU_O@co9BFBFdJ;=O9D?<Sq TaFMJT-TF@hW MTHOF< WOR`F;?OzTFWH[ G< `OH<
4V3@TRoA@XO?WHOIO<?G=oBFN3@N?dHBF<?>A@cOBXWOI_BF;FDJSTa`GFNR;FN?`=OV?GRd?TB<FBJZA@JNCT[

Programa exemplo: funo definida pelo usurio


Descrio: o programa a seguir define uma funo criada pelo usurio
(MostraAlfabeto). O programa utiliza a funo para imprimir o alfabeto um
nmero de vezes determinado pelo usurio.
#include <stdio.h>
#include <conio.h>
void MostraAlfabeto (void);
void main (void)
{
int iI, iNum;
printf("Imprime Alfabeto\n");
printf("Quantas Vezes? :");
scanf("%d",&iNum);
for(iI=0;iI<iNum;iI++)
{
MostraAlfabeto();
}
getch();
}
void MostraAlfabeto (void)
{
int iI;
for(iI='A'; iI<='Z'; iI++) putchar(iI);

83

putchar('\n');
}

;6g'"K3'6g $" m

9 T=R`FO < q`O9G<T9dJB=;FG?D?WEHGBC<\WqYGCOs9dJMFTAGJ@A>T[ OFK_@gB?G?GF>d=TO@J<;CTK9BJ`G Tb<CSRORT dFTA`@YGM=ldFOAG@c<OFV?`GROM<N?HM?NFTFM?WRORT `FT <?GV=;G`d?T T`FM?T T=W>O9N;C`RVTT
[
R;JMFT;J>A@XO?WHT<a;?TA@mWOd?WHG;C>HG>A@gQ<>NVCT_<`RG_BF;FD?EHG<\v
Oky=BR;?DFEGH<KHBFGY;CSFTa`GCHTd?GJWIROdFTA@gG<\[ j SRTVFB9@OFWHG;?>GYV3@mTMFG=`B3@OHN<[
Programa exemplo: sem valores de retorno

Descrio: este programa utiliza duas funes definidas pelo usurio para
imprimir valores e strings. Ambas no retornam nenhum valor.
#include <stdio.h>
#include <conio.h>
void MostraSoma(float fV1, float fV2);
void MostraMensagem (char *sMsg);
void main(void)
{
float fV1, fV2;
printf("Soma valores.\n");
printf("Digite dois valores, separados por espaos: ");
scanf("%f %f", &fV1, &fV2);
MostraSoma(fV1, fV2);
MostraMensagem("Saindo...");
getch();
}
void MostraSoma(float fV1, float fV2)
{
printf("Soma dos valores %f + %f = %f\n", fV1, fV2, fV1+fV2);
}
void MostraMensagem (char *sMsg)
{
puts(sMsg);
}

MFTJW y=BRF;?ODF<=ERG G<;CK9O9B?< GTGFVFG3GJ@c>9OBCDFO=ERWG<TH[ VFG33@cO< D?EGRO<ndJTGJ@XW GH< <=GB`<G O@@GJoH>BCTAW@CG;C;JT >FT9<;?SFGbT `<FGCHG Td?@HGRG?<CW >@BgNRW;CoGOW d?T-O @
BOR<=dFB?TAZA@g@CGNCTz< [ N;C>HGN3@T_<[ JTF`HGCW <FG3@ @gG?O9NF< T_B THB?>A@cT< >9NFVJT< `G?_NF;NC`RT< VFGFd=T

Programa exemplo: valores de retorno


Descrio: Este programa utiliza uma funo definida pelo usurio para
calcular a base elevada ao expoente.
#include <stdio.h>
#include <conio.h>

84

#include <math.h>
float pot (float x, int y);
void main(void)
{
float fBase,fResp;
int iExpoente;
clrscr();
printf("Calcula a base elevada ao expoente.\n");
printf("Base: ");
scanf("%f",&fBase);
printf("Expoente: ");
scanf("%d",&iExpoente);
fResp = pot(fBase,iExpoente);
printf("Potencia = %7.2f\n",fResp);
getch();
}
/* --- Funo definida pelo Usurio ---- */
float pot (float fX, int iY)
{
float fValor;
fValor = exp(log(fX)*iY);
return(fValor);
}

M<NXWV?dRy=GBR<C;?WDFGHE;CGH>< GYNFKH;JBF`G N?MFOaWTO;F<FNRBFVFMFBFGHdJ<FO?<?W TT9NFB;?FTIT-@W@XORODFM=EOG<< <CTa`G OW`OG?;RRNFTVdCBJ_dFGCOW D=SFTzB[ W ROd?T-@ KB=G

Programa exemplo: valores de retorno


Descrio: este programa utiliza uma funo definida pelo usurio para
calcular o determinante da equao do segundo grau (tambm conhecida como
equao de Bhaskara). Dependendo do resultado, a funo retorna um
inteiro: 0,1,-1, com o qual o resultado classificado pela funo
main().
#include <stdio.h>
#include <conio.h>
int VerificaDeterminante(float fA, float fB, float fC);
void main(void)
{
float fA,fB,fC;
int iValor;
//clrscr();
printf("Calcula o Determinante da Equacao de Bhaskara\n");
printf("a = ");
scanf("%f",&fA);
printf("b = ");
scanf("%f",&fB);
printf("c = ");
scanf("%f",&fC);

85

iValor = VerificaDeterminante(fA,fB,fC);
if (iValor == 0)
printf("Determinante ZERO\n");
else if (iValor > 0)
printf("Determinante POSITIVO\n");
else
printf("Determinante NEGATIVO\n");
getch();
}
int VerificaDeterminante(float fA, float fB, float fC)
{
float fDet;
fDet = fB * fB - 4.f * fA * fC;
if (fDet == 0) return(0);
else if (fDet > 0)
return(1);
else return(-1);

N=< ;?>G?GHsN_GC@XW3TV?<wdFqTH< @GJO N<qG VCT9;q >GN9O@d=T9l?<?W wqh`BCG >N=dGNF;C_e?O=NCWnO@XGJTnW M=T?V?WOO@;JF`FWT G=>A @X TH < 4`G VCOA@cKO B?O@d=GJK>BFTAG9@C@ ;CO>9@INFVJBXWT
ORdFTA@OTYM?TFWRO9;C`RTK9BJGIMHCOFWT_BO9BF;=D?ST[
|FV?T4T=>QGJs;=MGCW3N?OV?dF T VCO@XOq T4OuM?@TFTW>O_NR;C;C`O T ` G M aO?W`9OFN?`=eYOz[ _BFV_;FD?@XT=SRoAT @XO??WHOi q4VOG@;CO>RSFTz@GJqY>TAO@C>;C@OANF@BRTYNTOHd?TO@d?T-`=O @
@GJ>TA@C;CO`FTV?T@ JF O-@N?Z?_GFd F [
|FMFT}OJWNFGC;Rs9CGT4WV?`d=GbT <CiO9qUC`ROT{V_M?@cT=TWV?O9TA;@g`MT NCT_;C O `FTn V=GFd?l}T_<BC>9M?N?TFdHWRN?O9eF;CO=`R`TT<Y`>-Gb@XQ<<?GdFGRGD?eFSGHT<[
qJ`ROAG@cV?OnGH;CM?`OG`=;JOn`FT MFO`R<JTT
Tn`OM=O_T?W;CZOdH;J`FNFT<FG4 VJOA @O4 YO`N=@TReY>N4;OkBF`H;=GD?SM T CO= W O`F O+ q K BF Gw qh VCTA@I <=B?OYnGRVCO-e\@q]OO>A@GJ@C>N?TAB@CNa;COTY@HTkOdFTOd?@T- @
O@JNCZ=Gd A [
WfJdFG>s9NRGCV?W9dFV=Td?< T ;JO MR;?SFO?T WOR`F@XO G=>FT`F@O ;JO 9BF;F;JGDF;RSFFT BCW G H>OO?dFW_TF@Al?q W GHBg;CWH>AO @GJ><CO>-;C@>RNT ;o G;J;O9NCO TA@VCWHO-O @ FWH`RGFG >A@cTBW<
V?V3TH@mT;?>MFG=GH`N_NC@XWT GH;CVJ>OATH@X< O `HM?G O@NgORW9MFV3>G@cG@X<G<C<S=[ T ;O <=G3@>HGFG dFO[K9B?G ~ T?WGT <?>FO_;C< SRT 9BF@c;=GFD?>FETAGR@C< ;COFW GCs9G=;?MGHBC;F>RO?BgWW HO_OV?GdFTA;?OH@< q
K99BB=O;Fd?D?K_SRTiB?GA[ @nV_@cTMGR<<CO=WG;C>=T GCs9GFMRB?>FOF`RT^;?O<W_G<gWOH<N?M=O@cG<C>-@N?>FTGJsGMBJDFSFT`RO
Cm IOKp#K'"36g$"
V?V?ROO<>@@ O??W_W_G?G?l >>@@OTT_<4=T;?S=@TAWHT4@OWH>OG3d`R@cG SFT`=MO?GJO?WHGOFNC`B>RO;JTnDFT9SF;?Ti;O`[<IGT^OJTA@gN?@cH>FZ?OO_dF;?GT>=N@Tz<4q `FBT >9N?OOdH< N?@o_eFBgOFOW`RdFGO>R;<4G3>@XT VCOO-DJ@l EOnGM=< MHTCVOFNHWOGZ_`FNC?T >RdFOO<VJ[hOA@|O ;CBWT_TO<
TM@gOFN?WoORNR`F;COOd\[ VCT-@ OHd?TA@ OHB;?D=SFTOMRG<<CO G OdF>RGA@OOHVFG;JO<BgWHO MFpVRNCO`RT Od=TA@

Programa exemplo: chamada por valor


Descrio: este programa demonstra a utilizao das chamadas por valor. O
valor recebido pela funo alterado atravs de um processamento, mas
quando a funo retorna o valor original permanece inalterado.
#include <stdio.h>
#include <conio.h>
void MudaValor(int iA);

86

void main(void)
{
int iQuantia = 10;
printf("Antes da chamada da funcao: %d\n", iQuantia);
MudaValor(iQuantia);
printf("Depois da chamada da funcao: %d\n", iQuantia);
getch();
}
void MudaValor(int iQuantia)
{
puts("Dentro da funcao!");
iQuantia = iQuantia * 5 + 10;
printf("Valor dentro da funcao: %d\n", iQuantia);
}

P<?OUJ`FOI`HG<?>RGkV9@TFo@mOFWOa>GCWYO<?GFoBRNF;?>RGRTA@WO+v

Antes da chamada da funcao: 10


Dentro da funcao!
Valor dentro da funcao: 60
Depois da chamada da funcao: 10

9;?TJRW9<FG3G@m`FG OK_B?OG@gNCZFO^G=Hd B;?GD=sSFNFT<J>9GBJ;C`F>HOJGx_OH;Cd?OnTA@H_B;?D=`SFGJT9NFW;=O9G^NF;BCWiO [XOAW_@C?NCTAZF@XOG=d^>GHdF;FTR?MFOJOW dTbM=TFW9W^GH<gTWT W_G;?T?<CW_WRTG


<?S=THOA@JNCZ=GN=<M=T?W9VFd=GF>FO=WG;C>HG`HNCG3@gG;?>RG<[
p KOKp#K+"3/m +"g'
jByJT<NOo9@W`F;FOONJV?NF`OAM?GO @cOkMKMROFBF?WGO?OFWH`RO9OAO< @rTHOOR;?dF`@>GTRG3>@cTNO;CDFGO+ER;?[G`<G@XHGGDJNCT >RO`R< TO-;JT@o9< BXW9VCGO-;C@ >RFT WHGFl>A@cTVCO_< <F<?OFO=H`G?T>FO-M=@T?SFWT T O VCO-@ FOAWH@CGFNCZF>A@T+GRd [
R<>G>Fp9VFN=M?T<FG3@cZIG?s_VFd?T-@O`FT;?TYV3@p=s9NCWRTkM=OVUC>_B?dFT[
m" +,KtIKI $" pe=+" e/K OKX
V?O@X OWHO@XNG;9MFXGR F GA@VJTT<a<<FVCBRON}@ F`FWT9G=N=>A<u@XT<OA@`o_OaBgWdGNF;;R>CTOa< ` GaO@M?oTFM W O_;C`G T[ OA@co? NR;C>A@JU=;F<FGFM=T<BF<JOF`FT_<
OO@m@mooF4MY ~T_T_;C;C>>lGRWkN3@TT;FVCOfCWH@mOGA@TaBW`OGWHOAOF>@XoH@gBgN?W9eG`H;J>FGkT9<aVT9;C;COY>HGdN3NF@T_;RC<IOaV`OAGI@XOYM=T?M?WOOH@mO;?`FM?T>HG3@G<
WOHNV_;3@XcT=\o[ @O=WOuOt<?G=oBN@ WRT9<C>-@OuOtBJ>NFdN=e?ODJST}`T< O@o_BgW9GR;?>FT_<VCO_<F<?O=`T<uVCO-@O
Programa exemplo: argumentos para main

Descrio: este programa recebe parmetros do Sistema Operacional (uma


string qualquer) e imprime a string ao contrrio.
#include <stdio.h>
#include <conio.h>
#include <string.h> /*Para o strlen*/

87

void main(int argc, char *argv[])


{
int iI,iN;
if (argc != 2)
{
printf("ERRO: Faltou o Nome para INVERTER\n");
}
else
{
iN = strlen(argv[1]) - 1;
for (iI = iN;iI >= 0;iI--)
{
printf("%c",argv[1][iI]);
}
}
getch();

?;<FRTJG9<W9>@ GG4NRV?V_;gO@m9@XTG3OnoA@m@c>9NFO?NC;W`ROuT GA@V_>HV_@XG3@XG@AGRMR[]MNN<C_<OB4O <J<?GAGY<?G9@^F@ T-M@NFOF;=GJWMFsORdG`FB=MRTU?B?`F>FOT OR`FT4VC;CO-T9On@< >9NVCO-@NC>@>9GR`FN_;FO <@d`FNR`RT4;FT ?M?O T?V3W3@m`HVFTGNFd?G?ORM?>R`FT=TiTWq @9O9q ;M?`TnTFTW9;?BMFT?WTJWHGW G;J>OTG
MOFWOR`FTI`HGPi@o_BgWG;>T<aV?OA@cOIT9@TRoA@XOJWO4@cTFo@mO?WP@o9BXW9G;J><F\[
P<?OUJ`FOVJOA@XOYG<J>GkV@XTFo-@OFWOV?T=`GA@NJO<JGA@3v
FRGR;><VC@ORT_`F<COz>Oi[Fv1v]PG?WRv T9|NRB=G;g GA@m>G GB?|=T?W9G

Programa exemplo: argumentos para main


Descrio: este programa recebe parmetros pelo Sistema Operacional (uma
string de caracteres) e ordena (coloca em ordem alfabtica), imprimindoos em ordem alfabtica.
#include <stdio.h>
#include <conio.h>
void main(int argc, char *argv[])
{
int iI;
int iNumCar;
char cTmp;
if (argc != 2)
{
printf("ERRO: Faltou o nome\n");
}
else
{
iI = 0;
iNumCar = 1;
while (argv[1][iI] != '\0')
{
iI++;
iNumCar++;
}
iNumCar--;
printf("Numero de caracteres: %d\n",iNumCar);

88

iI = 0;
do {
if (argv[1][iI] > argv[1][iI+1])
{
cTmp = argv[1][iI];
argv[1][iI] = argv[1][iI+1];
argv[1][iI+1] = cTmp;
iI = 0;
}
else
{
iI++;
}
} while (argv[1][iI] != '\0');
printf("Nome ordenado.......: ");
for (iI = 1 ; iI <= iNumCar ; iI++)
{
printf("%c",argv[1][iI]);
}
}
getch();

P<?OUJ`FOVJOA@XOYG<J>GkV@XTFo-@OFWOV?T=`GA@NJO<JGA@3v
R;>@OR`FOz[Fv1P v T-@`G;Oxy-R? 9|=M?oJB?OR
9ORMJT<FG3@`R@mTkGM?K_T=WkB?GOaT>FOHV9F@TFGFodFO@mOFPWOaj=~ T::\@`H[ G;?OO9<hWO9N=f<?MHB?dFO<G`GVCT_NF<O9<hW9N;Ff=<FMB=d?O9<q`G

Numero de caracteres: 16
Nome ordenado.......: EFMNRTUVabcfghju

89

Captulo 8 Matrizes, Vetores e Strings

?@WRGJO G3@gGWR;FOM>ANJ@COFN?`e OH<Il V=GFd?BgTWHOW9G<WM?TTHdF;JGT?D?W9SRGwT q BC`H>_G N?dNJHeFOAO@;JN?`FZJT99FG<=NRG< `RGk`T BgW4UW;GH`9<gN?WMRT G\[ >NVCT KB=G <CSRT
?FWROHO NCsT WO>M?T-@NF@Fe@G<RMJVCT9T9;F;<R`NFG}<?>G OFTGJWV_@JNgVCW_T9G<=N3NFD?@mT{EGH< GRdFM?G?T9WGH;>9;C>UCo_TBCq O9<G `T GWW_OGCNRW<p@JNCOHO+d?>[4Tu MFTAG@R;?`R@XGGA<=@XV?GRTD?;JT `G}WOOFNRT<
fFV?THdJ>9<NgN?WHD=T4SFT GdF GJWXGeF;CG>R@TiT[r|?GObT^dNfJ;CdFoH>B?NOFWoHT GCW G~<?>=ZTV_;CO^@NWVCGT_N<F@XNFT4D?SRGT d=G?|W GHw;Cq>TT<I;?``GG4| BgW4M=TAG=@>F@cGT<F@VJTGH;?<C`>GZnOF;JOT
MFTJWV_@CNgW9G;J>FTa`TG?>T@A[
?MFWO@XO9M=GJ>>GATA@X@uGH<[l BXWO WO>A@CN?e `G BXWO `9NgW_G;<CS=T G BCWO j >@JNF;Jo l BCW GF>FT-@`HG
m ;mp '"I_ JI-" KBp,KOA67 tIp)(IX
P`GFMRd?O@OHD?STa`GYBgWOWOR>A@JN?eYB;=NC`9NgW_G;<=N?T;JOd GF>=TA@>G?WYO<?GFoB=N;C>HGFT-@WOv
>9>OJN=WV?OTi;Rv C-TzNFv]VJTI|fCWH`RGGA@`RTaOF`F`TaGa`GGadFGJWM?OG`F;COY>RTG<d=`FGCW9OGWH;J>FOFTz>@g[ NFe OR>l>FOFWO_;F?T?i[
tipo nome_varivel [tamanho];

Exemplo: declarao de um vetor

9~ ORR<FG3G@mOFTOD?SRV9Ti@Tvo@XP O?WHOFd`FNRT;Co9@ BWOFO_o;CGC>W G3@ ~ OYG<?;?>AS=@CT BC>9BGA@X@gOYN?M?N?T9M=;?OuMHNF<?T9O<u`dTN<W9N?`>O=GH`<}T<w[`FTu9GJ>TA@mFWOF>-@NFew[


int x[10];

/* 10 elementos: x[0] x[9] */

Programa exemplo: utilizao de vetores numricos


Descrio: o programa a seguir ilustra o uso de vetores numricos. Pedese que o usurio entre valores inteiros que so armazenados em um vetor.
Ao digitar -999, a seqncia de entrada terminada e os valores
digitados so mostrados na tela.
#include <stdio.h>
#include <conio.h>
void main (void)
{
int iNum[100]; /* Declara um vetor de inteiros de 100 posicoes */
int iCount=0;
int iTotalNums;
do {
printf ("\nEntre com um numero inteiro (-999 p/ terminar): ");
scanf ("%d",&iNum[iCount]);
iCount++;
} while (iNum[iCount-1]!=-999);
iTotalNums=iCount-1;
printf ("\n\n\n\t Os numeros que voce digitou foram:\n\n");
for (iCount=0;iCount<iTotalNums;iCount++)
{

90

printf (" %d",iNum[iCount]);


}
getch();
}

m Hp-"'g
GW<=VFGF MH~ NC 9qhN?M=BXOFW`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HNR<C;C>Fo To;JSFG9T9@XOAC>R@cZGA@W3BgNFW;?O OF`RWHO G;F<JXOFBoWGCO W<?`H>G @N`9;CNCo O=o9;C<?p_G?<CW >9N?M=BWT
N;`9N?M=O;?`FTaTV3@THFdFG?WROz[
P<`GRMFdFOA@cODFERG<v
<?S=TNJ`Q;C>_N?MFOH<+v
char szCode[3] = "abcd";

char s[] = "abc", t[3] = "abc";

char s[] = {'a', 'b', 'c', '\0'},


t[3] = {'a', 'b', 'c' };

9|GRGJ><?TA>Gu@q M?TO_<<CTzGq d=G?W<FGGH;C>FOT9<<C>A@XGH@JN=<C>;?o}O;J>GTA<@ <FW9G3G@cSF;JTAT@ N;FKN=BFMG N?OTudHN?eF>FO=OF`FWRT9O9<;FCM?T TFW G<RV?G[ MRN?N=M?O`FTVCO-@OuT


PO>A@CN?BFN=D?STa`GOdJT@XGH<O<C>@gNF;?o<aV?T=`GY<?GA@GN>FOI`HG`=T9NF<hWTF`=T9<v
" a R m$ #
8.2.1 Atribuio de valores a strings

char szPalavra[6];

szPalavra [0] = 'H';


szPalavra [1] = 'e';
szPalavra [2] = 'l';

91

szPalavra [3] = 'l';


szPalavra [4] = 'o';
szPalavra [5] = '\0';

% RI

'&( A  )+* -X  ,+- X / .01#


R<>OaB;?D=SFTYMFTVRNCOITYM?T_;C>GfJ`FTa`Gk<J>A@JN=;?o32YV?OA@cO<>A@JNF;Jo/\[
strcpy (szPalavra, "Hello");

m uJ-" p,K6OIm g'I

POM9@`RGG<=MFMFd?GO-;C>R@OODJ;?S`=TT9F<=`RGG G;Bg>WH@XO GaMFWTOdR>M@JFN?GJe >G<l O<N?`T9QHBC;C>->9@N?O9M=<O `NC WHG;`<CGREHMFGdF<\Ov@XOD=SFT `G BgW _G?>TA@q
B>@TYG?sGJW9VFdJTzqOFoFT@mOVJOA@XOBgWOWOF>-@NFeaNJ`NCWGH;F<NT;?Odv
`W9|O=GRN><?@>G@GNJN=>FeO G;s9ON=GCW3;?`dRV?GJNdFsTi;CO^oqBJOFO_To<GJWUFMF;JT`dHNF~iBFMF;?GqOH< `FT O URd=;CGOA`9<J@XNJKoHMBFB3GbG3@OH@cW`F\O9O[ N=<nNFBJ;JO`G?;?`sR`=TON9@XGONCOJ< >=WOT9dH<NFH;OAV_C@J@OHNG=< OnG;?WMHOO_?dJNFG3>9< @nB_@c@XT_OOB VRNCd`G G9OJ@W9T G;CBW>H`ROOG
K9`G=BJGMd?O-T @OU`F;CT+`_qN?MWGGH;C T9<G<JBgKW BFG9@W`O_Oi<[O^P3dlCdWNF;Jo`_B?NFOF<oH<CGCTW T< ~UF;?;?`HSFNFTMFGH<bHONOA@NJGO?@W NC_N?`MFG OA@e=GAN@T<C>RT^OFT VCO@OOd?T-T @
B<=B?ZA@CNCTz[4O9;>G3@T<aU;C`_N?MG<a;?OONsOVFG3@mW9N?>NJ`FOYl>FO-@XGF=OI`RTV_@mToA@cO?WOF`RTA@A[
tipo nome_varivel [tamanho_1][tamanho_2]...[tamanho_n];

tipo nome_varivel [altura][largura];

Programa exemplo: utilizao de matrizes


Descrio: o exemplo a seguir preenche uma matriz, seqencialmente por
linhas, com nmeros de 1 a 200.
#include <stdio.h>
#include <conio.h>
void main (void)
{
int mMtx [20][10];
int iI,iJ,iCount;
iCount=1;
for (iI=0;iI<20;iI++)
{
for (iJ=0;iJ<10;iJ++)
{
mMtx[iI][iJ]=iCount;
iCount++;
}
}
for (iI=0;iI<20;iI++)
{
for (iJ=0;iJ<10;iJ++)
{
printf("%d ",mMtx[iI][iJ]);
}
printf("\n");
}

92

getch();
}

m JI-"  K3'@m-"'g
JON`9@XONgW_G;M9<F@JNJNCTOA;?@mORWdT9< `G4BgM=WOAO @XORMF>WGOF@X>-G@<NFq e

`H`GGT4<C>>-O?@WHNFO;J;o< T4``TG9U=GC;?WH`TN?< MRG4GBF<J<JKOABF@ G3@m`BT4WO `HG?>WHG3OF@mW9>N@N=;Oe


T
_
C
;
TMFTJW;FV_f@CWNgW9GAG@T;J>FT`HWG Z?s3<Ng>WTa@NR`;Co9Ga< M?OG`FOT<?>@>NO?;CWHo+O[ ;CT `RT UR;C`9NJMFG `N3@cGNC>FT GH<FVFGFMHNC9NJMFO T

Exemplo: matriz de strings


char nome[3][10];
+----------------------------------------+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+--------------------------------------------|
| 0 |
|
|
|
|
|
|
|
|
| \0 |
|--------------------------------------------|
| 1 |
|
|
|
|
|
|
|
|
| \0 |
|--------------------------------------------|
| 2 |
|
|
|
|
|
|
|
|
| \0 |
+--------------------------------------------+

BCWROGCs_GC<JW>AVJ@JdFN=T4;?oMA@gVJN?OAOn@X>HBCNFWRMOB=d?WOO@b>@`NFGCeY_GC`WG6T< 5kG<?<R>AV?@CGNFM;?NJo<kNFMJM?OTF@W6O97V?GHM?;CO-O9@<OMJ>T G3@gURG;C<\`9[


N?MRCG OA@GHO4<?KOB=MRG3G@X<`=<TzOqI@WHT_TB<
<FGJOiq ;?T?W_G iq ;CT?W9G 2F[
m pgp '&)(pH'sg " I
dMFTJN=W<?>FVCOOR8H>UCOGHd=TANF<@GRG?<\WYv >dNFV?NTY<>OM?T=Wk`TaG >MFNTVTk;=<?>F?O_O_;C<?>GG`< OW<=GOV?>AOA@CN?@cOFei`[ OH< VJTA@ U_@coB?d=O< K9B?G <CS=T
tipo nome_matriz[tam_1]...[tam_n] = {lista_valores};

Exemplo: inicializao de matriz


int i[10] = {0,1,2,3,4,5,6,7,8,9};

P^<FG9@ N=;N?MHGNCNJO>FdO N=e?`OG}DJSFTnZ-`@GkN?OHW<}O>F@TNF@eFWHGRO<Y<\`[G4 M=OAV9@X@XORTFMFo->@GOF@XWGOu<q O T9B4<=G?<Fo9NXBFWNV?@ dRG<CNFWdGHBR;C<C>>G@O <C>O_@<}NR;Co9`<NCHqGAVC@TR<J`OG<


T@WO_<`GYN;=NFMNJOdNFeJOD?SRTi[

8.5.1 Inicializao de Matrizes de Caracteres

Programa exemplo: inicializao de matrizes de caracteres


Descrio: o programa a seguir ilustra a inicializao de strings.
#include <stdio.h>
#include <conio.h>
void main (void)
{

93

char szStr1[128]="Inicializa";
char szStr2[128]={"Outra inicializacao"};
static char szStr3[128]="Inicializa como static";
char s[] = {'a', 'b', 'c', '\0'}, t[3] = {'a', 'b', 'c' };
puts(szStr1);
puts(szStr2);
puts(szStr3);
puts(s);
puts(t);
printf("%c\n",szStr1[18]); //imprime nulo tem 10 caracteres
printf("%c\n",szStr2[28]); //imprime nulo tem 19 caracteres
printf("%c\n",szStr3[38]); //imprime nulo tem 22 caracteres
puts("fim");
getch();
}

8.5.2 Inicializao de Matrizes Multidimensionais

N;C>:9z;<2F>=bwq?q@2iq4zq?5q@7iqAiq?=
9z9z  ?>
> == 
9i9i 2F2Fww F
 == 75
9z9z<<FF?>
>== 2
9i9i5J5JwwF
== ?

Exemplo: inicializao de matrizes multidimensionais

WMFW^O=TH>;?>@BgGN=WHe\@rOqn>FNRTRG;F`FNF;CT9>RMR<SFN?OTTd<aN=Te?NFO;M=D=NJT?SFMWTNCVFOHN=`dd?GNFOe?`=BCORTW`F@ ObT@cM3WG@O<INJ>OA@JV9@XN?@XZ eG<JqaGBXW;C<JO>HGG;C<ISFWTOR;J>AOI@JTN?`R@e GFMGRo-d?<@O-V?O9@GR;OM`DJN?G STiN=T[ M?O`=<FTBJTNFMRNF>GO=;JW>OG;T V?OA`R@cOO

8.5.3 Inicializao de Matrizes sem Tamanho

Exemplo: inicializao de matrizes sem tamanho


char erro[] = "erro de leitura\n";

Programa exemplo: matrizes de caracteres e vetores


Descrio: este programa pede para o usurio digitar nomes e idades e os
guarda em dois vetores (uma matriz para os nomes e um vetor para as
idades). Ao final da entrada de dados os nomes e as idades so impressos
na tela. Observe que na entrada de dados o nome referenciado somente
pelo primeiro ndice. Admite-se no mximo 40 caracteres para cada nome.
#include <stdio.h>
#include <conio.h>
void main(void)
{
char szNome[10][40];
int iIdade[10];

94

int iI,iN;
char cCh;
iI = 0;
do {
clrscr();
printf("Nome: ");
scanf("%s",szNome[iI]);
flushall();
printf("Idade: ");
scanf("%d",&iIdade[iI]);

/* entrada de um nome */
/* limpa o buffer de entrada */
/* entrada de uma idade */

iI++;
printf("Continua [S/N]? ");
do {
cCh = getch();
puts("");
} while (cCh != 'S' && cCh != 's' && cCh != 'N' && cCh != 'n');
} while (cCh != 'n' && cCh != 'N');
iN = iI - 1;

/* numero de elementos */

clrscr();
for (iI=0; iI<=iN; iI++)
{
printf("| %s | %d | \n",szNome[iI],iIdade[iI]);
}
getch();
}

Programa exemplo: soma de matrizes numricas


Descrio: este programa pede para que o usurio informe as dimenses de
duas matrizes que sero somadas. As dimenses devem estar no range de 1 a
10. O usurio ento informa os valores contidos nas duas matrizes A e B.
O programa executa a soma das duas matrizes e mostra o resultado da soma
C=A+B.
#include <stdio.h>
#include <conio.h>
void main(void)
{
float a[10][10],b[10][10],c[10][10];
int
col,lin,m,n;
clrscr();
printf("Informe a ORDEM da MATRIZ: (mxn)\n");
do {
printf("Numero de linhas (m): ");
scanf("%d",&m);
} while (m < 1 || m > 10);
/* m de 1

10 */

do {
printf("Numero de colunas (n): ");
scanf("%d",&n);
} while (n < 1 || n > 10);

10 */

95

/* n de 1

for (lin = 0;lin < m;lin++)


{
for (col = 0;col < n;col++)
{
printf("A[%d,%d] = ",lin,col);
scanf("%f",&a[lin][col]);
printf("B[%d,%d] = ",lin,col);
scanf("%f",&b[lin][col]);
}
}
printf("\n");
for (lin = 0;lin < m;lin++)
{
for (col = 0;col < n;col++)
{
c[lin][col] = a[lin][col] + b[lin][col];
printf("C[%d,%d] = %f\n",lin,col,c[lin][col]);
}
}
getch();
}

96

Captulo 9 Introduo a Ponteiros.

B9MFOTJBW@X@JONCVFZ=;JBJ>>FOGG`FNFT-<wO @3[[>GCWs9BObGFGMHV=GB?DF;?OAS=`R@CTGNC;CZ?`R9`TG=G d`RTBlW >HBCWNV3VC@mT;CTTFoAW`@cGbGO?WO9O O<=<?@JOTNCdFZ=MHoNCB=OFG;`d <uTXORVJTFO`d?BgT-GWO@XG<F<G3VCT9@4<=<JNFBSFD?WT SROT OA@ ;?OAWO@JONeRW9ZFGGC;?WHGROFpAd `R@JTNMH<OCO`RG@9WT q
lNV?TH;>z<`N?qN?D=RSFGATId=@XTFGH`HO;CG>i>qG\WHG?[`RWTp-BFP3@FdRNCdRlCOzGFW [ `_ONF<K9<CBCT+O_qt;C>9NM=`FOFO``O G `OAGb@CNCW9ZFGCHWGp-d @N?O^GH<C;J>GZ MFGHO<F<?<RZA<CT@CNCMONJOF`V?OOA@mO O OA@BgWRWOO eG;JfZ;FFN=dJM?OO
|JTHGR<?;?>>GGHN_@XM?TO9<VFUJO->@BFWdJORT{eG<?;JGO?@W^ZV?OHTV_<R@N?GDF<JEGGR;C<>FOR``FGbT{W9BGCWWRp@>9NJN=OiV?[rTPA`HdFG lJWOA`@CG NCZFBH<CGORd`FT9MH<COFM?WTJO=W`TOOVJT@N?;?Z?>_GHGN_N@XT<[q
V?VFTHG9;?@W9>GHNJ>N_G@XTWH<{O;>FN=OFVWHB?d=lCOAW @ RV?OTF@`HNJGCZ?W 9G<?NRG<@ GWkBF<?O=BR`;?TDF<tERGMF<T?[ WHTO@o_BgW9GR;?>FT_<u`G HBF;?DFERG<qT KB=G
m .mKK:IK6$" $&IpBKJOI'"

P R<>dGNF;JToVFB?G9OFoR@XOFG?`FW T-@ ~z@g G?>FVJT-T@<;?<FOBRN^TBGW ;C`HTG3V=@XG3G@XDJOFT`RTA`H@G MHBCWO?OWHOF`OATC@NJZ?T9VJGRG3d @XOF;J`ROTA@nWHGC`HWG^pA@CNCGHOz;C`[G3@cGD?GT;C`HG3@;D3GFD=\T [


`GWGJWp@NJOklOA@mWOeGR;?OF`RTkGCW4BCWROO-@N?ZJ9GFd`Ta>NVTVCT_;C>GRN3@T+[
Programa exemplo: usando operador &
Descrio: Este programa aloca na memria um valor inteiro, um valor real
e um caractere. Todos os valores so inicializados. Em seguida, usando a
funo printf os valores so exibidos na tela, junto com suas posies
iniciais de memria.
#include <conio.h>
#include <stdio.h>
void main (void)
{
int iCont = 1;
float fValor = 5.5;
char cLet = 'a';
printf ("valor inteiro = %d \t\tendereco int = %X\n",iCont, &iCont);
printf ("valor real = %f \t\tendereco float = %X\n",fValor,&fValor);
printf ("valor char = %c \t\t\tendereco char = %X\n",cLet, &cLet);
getch();

9WsHGWGpAMBJ@C>FNCOO9;C`RVJTTF`GYT <?GV_@ @mT`9oAN@cO?G3W@gOiGq;?>GBgW`O O=KBF<JGFOd=U?O`FO WRT9<CVC>-T9@<FO<R`FUgO9G=;?d TY<F<FGG3B@CNCMFO TJWVFBJO >FO`Fd?TTM?@\OHdv NFeJOD?SRT `G

valor inteiro = 1
valor real = 5.500000
valor char = a

endereco int = 12FF74


endereco float = 12FF78
endereco char = 12FF7C

P<FGJo9`_BFNFN9<@3VCT_[<FNFHDJSF<?T4G@`9GRG <?>GHK_<aB?G `OF`RTT< <H;COOdFTAW@gGJGW< p@gN?NRO;C>VCGRTRN_`@XGnT< <?G;@BXW @GV9M?@XT=GW9<JVGBC;C>R>FOFOR`F`FT-On@ V?GRM?d?T=O4W 9N<o9N=B_<?>@cOGJWOO


T9TMRVJGABVC@OHHO MN?T;J' O9du>G<`E
G O 5VJ2 OA@X>HN?N_>@4<}`FTTHMBGV;JO?`F
W GA@cGD?IT G 9'>H2_GyF<\y [nJzPAq<RT<FNCW+qOHd?TA @@XOG=d?OT-d@ xNF;?O>Rd?GT-N_@k@cT THMBN V~ HO T;J>
`G 9RTY>GG<;J`GAO @XGRV?D?OT@X>N92H@kyRy`RJ T~ [ G;C`G@XGDJKT 2HyRy JLGTOHd?T@YMCO-@TMB=V?HO  9>RGO^VJOA@X>HN3@
97

int iCont=1;
&iCont

12FF74

float fValor=5.5;
&fValor
12FF78
char cLet=a;
&cLet
12FF7C

4 bytes

5.5

4 bytes

1 byte

ig. 8.1 Disposio de dados na memria.

m 7mK'"IpK/ '"Ep/#pKII "I

V?JOOT@X@J@ONCZ=BWk`GGOd\MF<JqdJ>OGAT @@gO-N>_@<?NFM=V?TTBgWO `RM=Ti[ OAOk@Cd?NCT-OZF@<JH>GFGAK9d @gB?NFG<FVJM?TGRTY;?d?>RO^laGN_M?M@cT_CTi;CO=q>WlCOW^``FGJTaG G`=GNV9<F@XG GJTM=GFV=GH`G3<FG3@XVFO=@G=`MTAT^NC@_;JN?`T?MFGaW9OAG@tN;C`R`_T ON_@GF;?OAD=TJSF@CW9NCTOG ZF-[ `RGROd
G?sGJW9VFd?TaO<FG?o_BFN_@`GCWHT;<C>-@OaOBC>_N?dNJeFOD=SFTI`RT<TVFG3@cOF`T@G<PDbGM\[
Programa exemplo: usando os operadores & e *
Descrio: este programa aloca na memria um inteiro e um ponteiro para
inteiro. Em seguida atribui ao ponteiro o endereo do inteiro. Utilizando
printf mostram-se o valor do inteiro, o endereo onde inicia a varivel
inteira, o valor da varivel ponteiro (que armazena o endereo do
inteiro) e o valor da indireo acusada pelo ponteiro (que aponta para o
valor contido no endereo que ele armazena).
#include <stdio.h>
#include <conio.h>
void main (void)
{
int iCont = 1;
int *pC;
pC = &iCont;

/*Declara a varivel ponteiro.*/


/*Atribui o endereo de iCont a pc.*/

printf ("valor inteiro = %d \t\tendereco int = %X\n",iCont, &iCont);


printf ("valor de pC = %X \t\tvalor de *pC = %d\n",pC,*pC);
getch();

9W?WRGWO{pA@C<?NCOHOU?`FVJOuTF`V?GYT<R<?<FGUg@ 9G=`9dNG3V?@gOG@X;?Ou>GG<C`>O=GKBFGFV3d=@XOT=oWR@T9O=<CW>-O@Ol `FOO{;?TY<?G=<FoGBBNF;JMF>TJG WVFBJm>FOO`Fd?TTH@M?\Ov dRNFe?OHD?ST `G

valor inteiro = 1
valor de pC = 12FF78

endereco int = 12FF78


valor de *pC = 1

98

P}N;>9GNN9o9@B_O @cOTMRO BVC<FOGJo9^BFN9'@ 9>NJGRd<BF<JG^>A@GO <C>ROZ<ONC@>_WHBCOOeDJGRS;?T OF`ROoGA@mO^O`FVCOO@mV?>9GRN_d?@4T`=T^V3@TRGoA;?@`ROFGAWR@XOiGF[ID=TKP 2Hy=OAyQ@CJGNCLZF[hGRPd


`OG=M@Jd?NCZ=O-@OG`Fd OHV?THOA;?@>WRGHON_eF@XGHT ;COV?GOA;C@c`HO G3@GFNRDJ;CT9><GRN_@X`T GNF ;JM>V G~ N@T9R <[aGH<C?>>O NFdRNFl e?O_;CO `TTTA@mWTOV?G@M=OTF`=WTT @:DG=dFVCO O@mOl
TV?THJ>;?>G3GH@ N_@XTTi[SGC;JOA`@XG3O @G=TDF?T >HG3`R@T TNF;?OR>RdFGTAN_@@T+`q T T N;C>HHGON3dF@mTAT @ O`T VCO-@GH>9;CN_`@GA@c`GT D?T V?Tl ;J>GON>A@X@CTN?BFB=<?U?OH`FT <?G ORTT
T9VJGA@OR`FT@`GYN;C`_N_@XGDJST M\[
int iCont=1;
&iCont

12FF78
1

Varivel iCont

*pC
int *pC;

12FF78

Varivel pC

pC=&iCont;

Fig. 8.2 Disposio de dados na memria.

m m6+" g'psap'"f$,K:#KIIp " "m '"I'g

WT=`9VNC@X_TFN?o-MF@OAOF@WOtTO Od=<?TAG?@ro_BF`=N3O@HOAW@JT9NC<CZ=>@XGO dw[ T B=<?T`FT TVFG9@XOF`FT-@`GN=;?`N9@XGFDFS=T MR VCO@mO


Programa Exemplo: usando o ponteiro de indireo
Descrio: Este programa aloca na memria um inteiro e um ponteiro para
inteiro. Em seguida atribui ao ponteiro o endereo do inteiro. O valor do
inteiro modificado atravs do operador de indireo (*). Utilizando
printf mostra-se o valor do inteiro antes e depois do uso do operador.
#include <stdio.h>
#include <conio.h>
void main (void)
{
int iCont = 1;
int *pC;
pC = &iCont;

/*Declara a varivel ponteiro.*/


/*Atribui o endereo de iCont a pc.*/

printf ("Valor inicial de iCont = %d\n",iCont);


*pC = 30;

/*Um novo valor para iCont, indiretamente.*/

printf ("Valor final de iCont = %d\n",iCont);


getch();
}

99

R<>GYV_@XTFo-@OFWROIO_V_@XG<JG;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SFMHTnCOFM3WR@CONC`FOO`FOVTV?@ Gd?T@GJBG3<FBC@gZ-Q;F@N?MTYNOK_B?G GO@d=G=>MGAGF@mROGYTT

Programa exemplo: passagem de argumentos por referncia

#include <stdio.h>
#include <conio.h>
void main (void)
{
int iCont=5;
void MudaValor(int *pIn);

/*Ponteiro para inteiro.*/


/*Declara a funo.*/

printf("Valor inicial = %d\n",iCont);


MudaValor(&iCont);
/*Passa por referncia.*/
printf("Valor final = %d\n",iCont);
getch();
}
void MudaValor(int *pIn)
{
*pIn += 10;

/*Recebe o ponteiro.*/
/*Altera o valor do inteiro.*/

printf("Valor dentro da funcao = %d\n", *pIn);

P<?OUJ`FOI`HG<?>RGkV9@TFo@mOFWOaO9V_@gG<FGR;C>OaO<a<FG?o_BFN;C>HG<hWG;F<JOFoGR;<v

Valor inicial = 5
Valor dentro da funcao = 15
Valor final = 15

V?OdF`=T4OTRV?GF<?d=GO@OFBWR;JT9DF<YSFT KB=V9Ga@NFOY;J>FzO[r@gN?P Z?_GFMHdnCO?NWH~ OFT`;?O>4 lnHNRB;F;?NFD?MRSRN?TOd9N=BCe?`ROFO`Rx_OOHd?M=TTF@4W4lTHHOGdFNC>RTO @


=|V?OHGR<?<j><CqO_G4;CW`GJTTs<JRG?>A<?W_@G
k
9M B=VJ:;FD?;gQSR=lT  3 qBC`<COT=x9WT}OOHdFTG ;?@ `RGAO@@XTG=GRMD?GFT ROGdJ`RTO@T K_O-B?GG@;?NC`RZFGA@HG@XG<RGRduNCD?`T GN ~ `=T9;CO O;>id?q THO-M?;@OSN?dTZJNJeFGT Od D=SFTGiOdJqu`TG@^MFT?W_`=W GCO WpO @JO-NC@ON=N?;Z?O<C_>-dJGFT@diM?BF[OFD?`RSRPOT
V?O@XO OO@gN?Z?GRd N ~ T;J>iqKBFG GR<C>Z;?T G;J`GA@cGFDFT OVJT;?>FOR`FTV?GdJT VJT9;C>HGN3@mTz[Y
100

GV3;@m`ToAG3@g@cO?GFWDFT4O`=GCOs_GCWOVJ@gdFN?TZ?_O GFdn<?G=N o~ BTN;?>4@A[n;?S=|?T4TF>HlG OKd?BF>HG G3@XTHO=< `TiqrG;?MJ`RTFG3W@TGFD=TVJTF< `GORK<=BG3N @WT_G<C@>N?@mON=`FM?T_O< `=TV?GFVF`GFdJGJTW
<FG9@r`_NCG3@gG;?>RG<`FOFK9BJGdFGH<T=>N?`=T<I;JT<JGBM?TFW9VRBC>O=`FT@3[
Programa exemplo: endereos na passagem por referncia
Descrio: Este programa tem uma funo criada pelo usurio que recebe o
valor de um inteiro atravs de uma chamada por referncia e altera o
valor do inteiro. O programa tambm mostra o endereo da varivel na
memria, referenciado pelo ponteiro.
#include <stdio.h>
#include <conio.h>
void main (void)
{
int iCont=5;
void MudaValor(int *pIn);

/*Ponteiro para inteiro.*/


/*Declara a funo.*/

printf("Valor inicial = %d\nEndereco do valor = %X\n",iCont, &iCont);


MudaValor(&iCont);
/*Passa por referncia.*/
printf("Valor final = %d\n",iCont);
getch();
}
void MudaValor(int *pIn)
/*Recebe o ponteiro.*/
{
printf("Endereco do valor dentro da funcao: %X\n", pIn);
*pIn += 10;

/*Altera o valor do inteiro.*/

printf("Valor dentro da funcao = %d\n", *pIn);

P<?OUJ`FOI`HG<?>RGkV9@TFo@mOFWOaO9V_@gG<FGR;C>OaT<a<FG?o_BFN;C>HG<@XG<FB=d?>O=`T<wv

Valor inicial = 5
Endereco do valor = 12FF7C
Endereco do valor dentro da funcao: 12FF7C
Valor dentro da funcao = 15
Valor final = 15

OGJT=M=;``GGCG3<FWH<=@gTGFGDF<YTTTR;CO<?TdJGAT@KH@B?OAOK@dB?GbK_@B?G@G<RGNC<RT4`NCGb`GGY;CT4`H;CG3HTY@OG=d?GD?T-;?T4@`GO`@XM=GGGDJ<FTzN <J~ qOFT_`O;CTndF>z>[ V=GGF@dFO9O;T_`FV?_T9GABFC@;FT+ORD?[`FSRTT@9BCMF`ROx3V?ORGd?T@W@NC>HlYG4TKB=W_G G<CWR<JTG


O@JVNC@Z=ToAG@cNFO?<}WO `HGFOMdJO<?@GFORo`FBRO9N_<}@ `B?GH>H;CNF>d@mN=T e?O `G}V?THW;COH>NG;N9[@T9P< VOBR;?@D?O SRT}>-@T@MJTHOM?@ OJx_TORdFTA@XOHGHd?< T@ @GF`RMRG GFGu`9BCO_TH<<
V?TH;?>GHN_@XT<aV?O@XOa`BCO9<hO-@N?Z?_GN<[

Programa exemplo: troca de variveis atravs de referncias


Descrio: este programa tem uma funo criada pelo usurio que recebe os
endereos de dois inteiros e faz a troca dos valores atravs do acesso
aos valores residentes naqueles endereos.
#include <stdio.h>
#include <conio.h>

101

void main (void)


{
int iPrim=1, iSec=2;
void TrocaValores(int *pIn1, int *pSec);
printf("Antes da troca: primeiro=%d, segundo=%d\n", iPrim, iSec);
TrocaValores(&iPrim, &iSec);

/*Envia os enderecos*/

printf("Depois da troca: primeiro=%d, segundo=%d\n", iPrim, iSec);


getch();
}
void TrocaValores(int *pIn1, int *pIn2)
{
int iTemp;

/*Recebe os ponteiros.*/

iTemp = *pIn1;
*pIn1 = *pIn2;
*pIn2 = iTemp;

P<?OUJ`FOI`RTV@XTFo-@O?WOaOV9@XG<?GH;C>OO9<a<?GFoHBNF;J>G<hWG;=<COoGH;F<\v

Antes da troca: primeiro=1, segundo=2


Depois da troca: primeiro=2, segundo=1

~NTJ;W>TGN9@VCT TR``G G <?NcG39@G?W_V^TRl<?G3@mTJO>`=N?T `FT;?OT >@XG?O?s_GJlW9< VFdJ`RT T `THO VFG3@ORB`F;JTDF@ SFT MR\\@qYTM?K9OCB?x_G OdJOTM@XGRGH<<<CqO T T OOd=dJTTA@@
@GR<NC`HG;?>GY;?TYG;J`G3@G=DFTa`FOHOA@JNZ?9GFd[
P<@mTM?OCx_OO@d?NJT-Z?@X9GGR<N< BC>N=NddNF@CeJNgOW ;JSFG TnTbN j GFOM d=TA@I<?SR`HT GN j O-GF@MN?Z?GHGNFNQ<_@NCWNR;C>G GN9<=@NgO9W<q[ MCP O=WO`FMO_CO=<WOO`FO <?GB=O<
G;`G3@gGFDFT<w[

102

Anexo

Tabela ASCII
Non-Printing Characters
Name

Ctrl
char

Dec

Hex

Printing Characters
Char

Dec

Hex

Char

Dec

Hex

Char

Dec

Hex

Char

null

ctrl-@

NUL

32

20

Space

64

40

96

60

start of heading

ctrl-A

SOH

33

21

65

41

97

61

start of text

ctrl-B

STX

34

22

"

66

42

98

62

end of text

ctrl-C

ETX

35

23

67

43

99

63

end of xmit

ctrl-D

EOT

36

24

68

44

100

64

d
e

enquiry

ctrl-E

ENQ

37

25

69

45

101

65

acknowledge

ctrl-F

ACK

38

26

&

70

46

102

66

bell

ctrl-G

BEL

39

27

'

71

47

103

67

backspace

ctrl-H

BS

40

28

72

48

104

68

horizontal tab

ctrl-I

HT

41

29

73

49

105

69

line feed

ctrl-J

10

0A

LF

42

2A

74

4A

106

6A

vertical tab

ctrl-K

11

0B

VT

43

2B

75

4B

107

6B

form feed

ctrl-L

12

0C

FF

44

2C

76

4C

108

6C

carriage feed

ctrl-M

13

0D

CR

45

2D

77

4D

109

6D

shift out

ctrl-N

14

0E

SO

46

2E

78

4E

110

6E

shift in

ctrl-O

15

0F

SI

47

2F

79

4F

111

6F

data line escape

ctrl-P

16

10

DLE

48

30

80

50

112

70

device control 1

ctrl-Q

17

11

DC1

49

31

81

51

113

71

device control 2

ctrl-R

18

12

DC2

50

32

82

52

114

72

device control 3

ctrl-S

19

13

DC3

51

33

83

53

115

73

device control 4

ctrl-T

20

14

DC4

52

34

84

54

116

74

neg acknowledge

ctrl-U

21

15

NAK

53

35

85

55

117

75

synchronous idel

ctrl-V

22

16

SYN

54

36

86

56

118

76

end of xmit block

ctrl-W

23

17

ETB

55

37

87

57

119

77

cancel

ctrl-X

24

18

CAN

56

38

88

58

120

78

end of medium

ctrl-Y

25

19

EM

57

39

89

59

121

79

substitute

ctrl-Z

26

1A

SUB

58

3A

90

5A

122

7A

escape

ctrl-[

27

1B

ESC

59

3B

91

5B

123

7B

file separator

ctrl-\

28

1C

FS

60

3C

<

92

5C

124

7C

group separator

ctrl-]

29

1D

GS

61

3D

93

5D

125

7D

30

1E

62

3E

>

94

5E

126

7E

95

5F

127

7F

DEL

record separator

ctrl-^

RS

m<n<noprq<q\s<sstuo<mwvyx{z|<xt_};~Gt<~};q;s<nx;y;qx<|{<;yq<x<|z<ztum{n<
unit separator

ctrl-_

31

1F

US

63

3F

103

9BjXJN=><7CVC_T_:2;FRUCP GRj=L~3dI[ G:XRWP v q_OoHV;F[G3M@[ -BB;?GHO?<FWV\OJ[ W3T@>FRT+Wq O9jBN@J<CNg=>H9GCW|?Oj<NF`<?>RG||BCWzBgW_[G3VC`@XORDFq SFT+OHMFqG2<F=<O`F2\TY[ G?Wzv


=2C5 >> FJ|z_B Rq4 j HOH|iN3@q >RTJdF;d[ NT=>FdF>+G=>A[@3d=;RoN?MFGO:9@B9ONCo_JTFNC>TORd=diGFO[ ;JOkj STGaMO9N3BJ@cd?MT^BNC>Rv4T<O/`9Ga@T_M;I?O?CHT=TOGFO?=W_<wGq?;?>FGT+7Q7d[ 5wj [ ST
JOHR BFd?Tj : v_|y9Mw:i@mq O ~ JOA N?@Xddd
`F[ T 39 @@TRO_oA<FNF@XOJdwWq?:?G`H7dGJ;?J\>H[ N?N=G3@h|=O?WN=;Co ~ T;CHG;C>_NCT9;F< [ j ;NFVRV?GF>H<\v
R
G
\
<
?y>=jg>9hVPvFd?oQTA@CNC>F[mWR<=T9;<NFVRqV?G?G>9737Q<JI[TA@coiP3q `AOH@CMFNCO9G;<=T <CTkTRGJW6OFK9B=5GNC WY`H`GGOF_oRTd<?NCH>=TIGN3`@OG~2 =@gBF\Le\[q
`?m>=G=>9NVRB=vFd|F9C?TaG?K_ `BFq GNBVJGi2^F[8;=BMF\L HG\[ [mJBr=@H+@[AH@8HHOF:`8@gHNCBO9U;CT=M?BRBO9tVC/T_8<C^>9)N?^d=UO?ORJd?HoTA@CNC>FWR T9<[C>FWzq1P3MRG<<CTYG?WSj

Bibliograf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