Вы находитесь на странице: 1из 11

Constantes em C

Prof. Cesar G. Miguel


cesar.miguel@sumare.edu.br
Uso de Constantes

• Comando define
#define NOMECONST valor

– Ex:
#define N 10
#define TAM 15
#define MAX 8

Tópico 3: Constantes - Prof. Cesar G. Miguel


Uso de Constantes

• Comando define
#define NOMECONST valor

– Ex:
#define N 10
#define TAM 15
#define MAX 8

Onde aparecer a constante,


será substituída pelo valor definido.
Tópico 3: Constantes - Prof. Cesar G. Miguel
Uso de Constantes

• Em vetores
– Ex:

int vet[10];
void main() {
int i;
for(i=0; i<10; i++)
vet[i] = i+5;
//...

Tópico 3: Constantes - Prof. Cesar G. Miguel


Uso de Constantes

• Em vetores
– Ex: E se fossem 20 elementos?

int vet[10];
void main() {
int i;
for(i=0; i<10; i++)
vet[i] = i+5;
//...

Tópico 3: Constantes - Prof. Cesar G. Miguel


Uso de Constantes

• Em vetores
– Ex:

int vet[20];
void main() {
int i;
for(i=0; i<20; i++)
vet[i] = i+5;
//...

Tópico 3: Constantes - Prof. Cesar G. Miguel


Uso de Constantes

• Em vetores
– Ex:

int vet[20];
void main() {
int i;
for(i=0; i<20; i++)
vet[i] = i+5;
//...
E se fossem vários laços?
1000 linhas de código-fonte?
Tópico 3: Constantes - Prof. Cesar G. Miguel
Uso de Constantes

• Em vetores
– Ex:
#define N 10
int vet[10];
void main() {
int i;
for(i=0; i<10; i++)
vet[i] = i+5;
//...

Tópico 3: Constantes - Prof. Cesar G. Miguel


Uso de Constantes

• Em vetores
– Ex:
#define N 10
int vet[N];
void main() {
int i;
for(i=0; i<N; i++)
vet[i] = i+5;
//...

Tópico 3: Constantes - Prof. Cesar G. Miguel


Uso de Constantes

• Em vetores
– Ex:
#define N 20
int vet[N];
void main() {
int i;
for(i=0; i<N; i++)
vet[i] = i+5;
//...

Tópico 3: Constantes - Prof. Cesar G. Miguel


“Números” aleatórios

• rand() - função para gerar sequências


aleatórias
• srand() - função para configurar o passo inicial
do gerador de números aleatórios (semente)
• time(NULL) – função que retorna o tempo, em
segundos, acumulado desde 01/01/1970

Tópico 3: Constantes - Prof. Cesar G. Miguel

Вам также может понравиться