Nugget_Mon
August 23rd, 2009, 04:44 PM
Ubuntu 9.04
gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3
Hello,
I have a couple of noob questions. I was following the C tutorial from Dev-C++ 4.9.9.2 (PortableApps) and ran into a couple of errors. I then switched over to GCC and got the same errors.
#include <stdio.h>
void main()
{
float a=10/3;
printf("a=%f\n\n",a);
return 0;
}
The terminal output is :
~/My_C_Programs$ gcc -o program1 program1.c
program1.c: In function ‘main’:
program1.c:7: warning: ‘return’ with a value, in function returning void
program1.c:4: warning: return type of ‘main’ is not ‘int’
~/My_C_Programs$ ./program1
a=3.000000
If I change the code as follows there are no errors and the output is correct:
#include <stdio.h>
int main() // changed void to int
{
float a=10.0/3.0; //added decimals to integers
printf("a=%f\n\n",a);
return 0;
}
~/My_C_Programs$ gcc -o program1 program1.c
~/My_C_Programs$ ./program1
a=3.333333
At first I thought the "return 0;" was the issue with the "void main()" error, but when removed it the error persists.
The error with the float variable. I got the idea to add the decimals from my TI-89. But if I have a more complicated code, how do I ensure that if my code ends up dividing two integers that I actually get a float answer?
Thank you,
Mon
gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3
Hello,
I have a couple of noob questions. I was following the C tutorial from Dev-C++ 4.9.9.2 (PortableApps) and ran into a couple of errors. I then switched over to GCC and got the same errors.
#include <stdio.h>
void main()
{
float a=10/3;
printf("a=%f\n\n",a);
return 0;
}
The terminal output is :
~/My_C_Programs$ gcc -o program1 program1.c
program1.c: In function ‘main’:
program1.c:7: warning: ‘return’ with a value, in function returning void
program1.c:4: warning: return type of ‘main’ is not ‘int’
~/My_C_Programs$ ./program1
a=3.000000
If I change the code as follows there are no errors and the output is correct:
#include <stdio.h>
int main() // changed void to int
{
float a=10.0/3.0; //added decimals to integers
printf("a=%f\n\n",a);
return 0;
}
~/My_C_Programs$ gcc -o program1 program1.c
~/My_C_Programs$ ./program1
a=3.333333
At first I thought the "return 0;" was the issue with the "void main()" error, but when removed it the error persists.
The error with the float variable. I got the idea to add the decimals from my TI-89. But if I have a more complicated code, how do I ensure that if my code ends up dividing two integers that I actually get a float answer?
Thank you,
Mon