PDA

View Full Version : [SOLVED] Me trying to learn C on my own... this is hopeless



crazyfuturamanoob
October 28th, 2008, 04:43 PM
I am going to learn C programming language on my own, without a specific teacher or anything.
I tried to wrote a program with a basic function. Here it goes:

#include <stdio.h>

int main ()
{
arhonfunktio (2, 3);
return 0;
}

void arhonfunktio (float a, float b)
{
float value;
value = a*b;
printf ("value = %d\n", value);
}

But when I try to compile:

arho@ohra-laptop:~/Työpöytä$ gcc test.c -o test.o
test.c:10: warning: conflicting types for ‘arhonfunktio’
test.c:5: warning: previous implicit declaration of ‘arhonfunktio’ was here
arho@ohra-laptop:~/Työpöytä$

What are those errors? I checked my code a million times and over but found nothing that could cause those warnings.

However, when I run that it works.

curvedinfinity
October 28th, 2008, 04:50 PM
Heya, "warnings" are just the compiler saying something like "you're doing something that might be dangerous, but we're going on ahead anyway." :)


conflicting types for ‘arhonfunktio’

This one is because you are passing integers to a function that takes floats.


previous implicit declaration of ‘arhonfunktio’ was here

This one is because you should define your function above where you use it. :)

By the way, it takes some balls to teach yourself C. Congrats for getting as far as you have.

swappo1
October 28th, 2008, 04:56 PM
Here try this:

#include <stdio.h>

void arhonfunktio (float a, float b); // function prototype

int main ()
{
arhonfunktio (2, 3);
return 0;
}

void arhonfunktio (float a, float b)
{
float value;
value = a*b;
printf ("value = %f\n", value); // %f for floating point variable
}

You want to add the function prototype before main(). This tells the compiler a function will be defined later. Also use %f for floating point variables.

crazyfuturamanoob
October 28th, 2008, 05:01 PM
Heya, "warnings" are just the compiler saying something like "you're doing something that might be dangerous, but we're going on ahead anyway." :)


conflicting types for ‘arhonfunktio’

This one is because you are passing integers to a function that takes floats.


previous implicit declaration of ‘arhonfunktio’ was here

This one is because you should define your function above where you use it. :)

By the way, it takes some balls to teach yourself C. Congrats for getting as far as you have.

Thanks.

wmcbrine
October 29th, 2008, 01:36 AM
This one is because you are passing integers to a function that takes floats.No, it's because the implicit function declaration (like all implicit function declarations) has an int for its return value, while the actual function says it returns void. It's only one warning, on two lines.

Passing integers to a function that takes floats results in silent promotion, not warnings.