Jump to content

Dúvida relativa a erros em C


pudim
 Share

Recommended Posts

O problema é o seguinte:

Escreva um programa para somar uma sequência de números inteiros. O programa começa por ler qual o número de valores a somar, depois vai lendo os sucessivos valores enquanto faz as contas, e no final escreve o resultado.

Aqui está a minha resolução:

int main(void)
{
int n, total = 0, i, v;
scanf("%d", &n);
for( i=0; i < n; i++)
scanf("%d", &v);
total += v;
printf("%d\n", total);
return 0;
}

Dá os seguintes erros:

Compiling source file(s)...

I23.c

I23.c: In function `main':

I23.c:11: warning: control reaches end of non-void function

I23.c: At top level:

I23.c:12: error: parse error before '{' token

IP-B.exe - 1 error(s), 1 warning(s)

Se alguém me pudesse esclarecer o significado do erro e me dissesse o que está incorrecto na minha resolução, tendo em conta o enunciado do problema, agradecia 😛

Link to comment
Share on other sites

Já adicionei as chavetas:

int main(void)

{

int n, total = 0, i, v;

scanf("%d", &n);

for( i=0; i < n; i++)

{

scanf("%d", &v);

total += v;

}        

printf("%d\n", total);

                return 0;

}

Dá 0 erros e 2 warnings:

--------------------Configuration: IP-B - Debug--------------------

Compiling source file(s)...

I23.c

I23.c: In function `main':

I23.c:4: warning: implicit declaration of function `scanf'

I23.c:10: warning: implicit declaration of function `printf'

Linking...

IP-B.exe - 0 error(s), 2 warning(s)

A que se deve estes warnings e o que é que está incorrecto na minha resolução? Se alguém me puder esclarecer agradecia.  😛

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.