Ok, so im still pretty new to C programming and i was reading the stickies and i found a post in the C Book Recomendations sticky that has some assignments. The assignment was to print out the square root of the numbers of 1-10. It sounded pretty simple and so i decided to do it. I came up with this:
Code:
#include <stdio.h>
#include <math.h>
int main()
{
int i=1;
int square = 0;
while(i <= 10){
square = sqrt(i);
printf("%d\n", &square);
i++;
}
}
The first problem i came across was the compiler spitting out some wierd warning at me, but i found out that there was a special flag that i had to use to link the math.h library.
Code:
gcc -o newsqrt newsqrtc -lm
which i assume means ''Link Math'' (please correct me if im wrong i couldnt find it in the documentation that i found). So i run the program and it doesnt work like i thought it would.
Code:
Mike@laptop:~> ./newsqrt
-1078179784
-1078179784
-1078179784
-1078179784
-1078179784
-1078179784
-1078179784
-1078179784
-1078179784
-1078179784
I assume that its memory addresses or something.
i recompiled the proggram using
Code:
gcc -o -ggdb newsqrt newsqrt.c
and i run it through gdb and i get this:
Code:
(gdb) l
1 #include <stdio.h>
2 #include <math.h>
3 int main()
4 {
5 int i=1;
6 int square = 0;
7 while(i <= 10){
8 square = sqrt(i);
9 printf("%d\n", &square);
10 i++;
(gdb) b 7
Breakpoint 1 at 0x8048433: file newsqrt.c, line 7.
(gdb) r
Starting program: /home/Mike/newsqrt
Breakpoint 1, main () at newsqrt.c:7
7 while(i <= 10){
(gdb) s
8 square = sqrt(i);
(gdb) s
9 printf("%d\n", &square);
(gdb) s
-1075492664
10 i++;
and then it goes through it all again.
My big questions are; what am i forgetting and what (if anything) can i do to make this code better?
i hope that this isnt too much for my first thread. I searched the forum for people with a similar problem but i didnt find it.
thanks in advance for the help.
-Mikey_ickey