29.01.2014 Views

Introdução à Linguagem C - pucrs

Introdução à Linguagem C - pucrs

Introdução à Linguagem C - pucrs

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Introdução</strong> <strong>à</strong> <strong>Linguagem</strong> C<br />

Estrutura de um Programa em C<br />

• Cabeçalho: bibliotecas, variáveis globais,<br />

declaração de rotinas.<br />

• Bloco de Instruções Principal e Bloco de<br />

Rotinas<br />

Profª. Leticia Lopes Leite<br />

Laboratório de Programação A<br />

• Documentação do Programa (comentários)<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Características da <strong>Linguagem</strong> C<br />

• Estruturada:<br />

• formada por blocos por funções<br />

• a execução do programa começa sempre<br />

pela função main.<br />

• a linguagem C faz distinção entre<br />

maiúsculas e minúsculas.<br />

Exemplo<br />

#include <br />

void main ( )<br />

{<br />

printf (“Olá”);<br />

}<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Cabeçalho<br />

• Bibliotecas:<br />

• <br />

• <br />

• <br />

• <br />

• <br />

O processo de Compilação em C<br />

• Existem comandos que são processados<br />

durante a compilação do programa<br />

(diretivas de compilação) . Estes comandos<br />

informam ao compilador do C quais são as<br />

constantes usadas no programa e quais<br />

bibliotecas devem ser anexadas ao<br />

programa executável.<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br


Para o compilador...<br />

Lembrem-se ...<br />

void main ( )<br />

{<br />

}<br />

void main ( ) { }<br />

A função main ( ) deve existir em<br />

algum lugar do programa, pois é a<br />

partir dela que o programa começará<br />

a ser executado.<br />

São programas idênticos<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Dados<br />

Constantes<br />

Variáveis<br />

Variáveis<br />

• Espaço de memória reservado para armazenar um<br />

certo tipo de dado tendo um nome para referenciar o<br />

seu conteúdo.<br />

• Não apresenta valor fixo.<br />

• Nome pode ter até 32 caracteres, começando com<br />

letras ou sublinhado, seguidos de letras, números ou<br />

sublinhados.<br />

• Não podem ter nome igual ao de palavras reservadas<br />

da linguagem.<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Palavras Reservadas<br />

asm auto break case cdecl char<br />

class const continue _cs default delete<br />

do double _ds else enum _es<br />

extern _export far _fastcall float for<br />

friend goto huge if inline int<br />

interrupt _loadds long near new operator<br />

pascal private protected public register return<br />

_saveregs _seg short signed sizeof _ss<br />

static struct switch template this typedef<br />

union unsigned virtual void volatile while<br />

Constantes<br />

• Apresenta um valor fixo e inalterável.<br />

• Tem um tipo de dado associado.<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br


Tipos de Dados<br />

• A divisão se deve basicamente ao<br />

número de bytes reservados para cada<br />

dado.<br />

• Cada tipo de dado possui um intervalo<br />

de valores permitidos.<br />

Tipos Básicos de Dados<br />

• Inteiro <strong>à</strong> int<br />

• Real (ponto flutuante) <strong>à</strong> float<br />

• Caracter <strong>à</strong> char<br />

• Seqüência de caracteres <strong>à</strong> string<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Representação dos Tipos Básicos<br />

de Dados<br />

• Integer <strong>à</strong> %d<br />

• Float <strong>à</strong> %f<br />

• Char <strong>à</strong> %c<br />

Observação 1:<br />

• Separador decimal para números em<br />

ponto flutuante é o ponto final ( . ).<br />

• Também pode ser escrito em notação<br />

científica.<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Observação 2:<br />

Tipos de Dados x Memória<br />

• Os caracteres são representados entre aspas<br />

simples ( ´ ).<br />

Tipo<br />

Bytes<br />

Escala<br />

• Exemplos:<br />

Char<br />

1<br />

-128 a 127<br />

´Mariana´<br />

´Elvira´<br />

S<br />

t<br />

r<br />

i<br />

n<br />

g<br />

´M´<br />

´A´<br />

c<br />

h<br />

a<br />

r<br />

Int<br />

Float<br />

Double<br />

2<br />

4<br />

8<br />

-32.768 a 32.767<br />

3.4e -38 a 3.4e +38<br />

1.7e -308 a 1.7e +308<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br


Operadores Aritméticos<br />

Exemplos<br />

Operação<br />

Adição<br />

Subtração<br />

Multiplicação<br />

Divisão<br />

Operador<br />

+<br />

-<br />

*<br />

/<br />

• 1+45<br />

• 45-38<br />

• 24*8<br />

• 12.5/3<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Operadores Aritméticos<br />

Exemplos<br />

Operação<br />

Resto da Divisão<br />

Inteira<br />

Potenciação<br />

Raiz Quadrada<br />

Absoluto<br />

Somente para<br />

Operador operadores<br />

inteiros<br />

%<br />

pow<br />

sqrt<br />

abs<br />

• 4%3 <strong>à</strong> 1<br />

• pow(4,2) <strong>à</strong> 16<br />

• sqrt (36) <strong>à</strong> 6<br />

• abs (-1234) <strong>à</strong> 1234<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Operadores Relacionais<br />

Operadores Relacionais<br />

Operação<br />

Operador<br />

Operação<br />

Operador<br />

Igual a<br />

==<br />

Menor ou igual<br />

<br />

Incremento<br />

++<br />

Maior ou igual<br />

>=<br />

Menor<br />

<<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

As instruções com operadores relacionais retornam<br />

0 para Falso e 1 para Verdadeiro.<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br


Precedência de Operadores<br />

• Avaliação da esquerda para direita.<br />

• Mudança na precedência: uso de parênteses<br />

• Semelhante a matemática:<br />

-- ++<br />

* /<br />

Mais alta<br />

Exemplos<br />

• 4 == 4<br />

• 5 != 4<br />

• 5 > 4<br />

• 4 >= 4<br />

• 3 < 7<br />

• 3


Declaração de Variáveis<br />

• Informa ao processador quais são os<br />

nomes utilizados para armazenar dados<br />

de variáveis e quais tipos são utilizados.<br />

• Exemplo:<br />

char a;<br />

float media;<br />

int z, k, m;<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br<br />

Separador para<br />

variáveis de um<br />

mesmo tipo - vírgula<br />

Declaração de variáveis<br />

• Normalmente é feita dentro de uma rotina.<br />

• Exemplo:<br />

void main ( )<br />

{<br />

int k, z,j;<br />

char a;<br />

. . .<br />

}<br />

Profª. Leticia L. Leite - lleite@inf.<strong>pucrs</strong>.br

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!