C Programming
Sample C program
main()
{ int i;
printf("\t # \t\t Square\n");
for (i=0; i<=25;++i) printf("\t %d \t\t\t %d \n",i,square_root(i)); } square_root(int i) { return i*i; }
Compiling our sample c program using the GNU's GCC (g++) compiler
gcc test.c
or using the Sun's CC compiler
cc test.c
By default the program will be compiled to a.out We can even specify the output file by using the -o option
[root@srv31 c]# cc -o test test.c
[root@srv31 c]# ls
a.out test test.c
[root@srv31 c]# ./test
# Square
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
11 121
12 144
13 169
14 196
15 225
16 256
17 289
18 324
19 361
20 400
21 441
22 484
23 529
24 576
25 625
cc -o test test.c myfunction.c
Using math library (e.g. taken from link below)
#include
main()
{ int i;
printf("\t Number \t\t Square Root of Number\n\n");
for (i=0; i<=360; ++i)
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
}
Compiling it:
[root@srv31 c]# vi math.c
[root@srv31 c]# cc -o math2 math.c
/tmp/ccssZM3h.o(.text+0x4a): In function `main':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status
[root@srv31 c]# cc -o math2 math.c -lm
For more information, read the Linux Programmer's Manual or visit these sites:
0 Comments:
Post a Comment
<< Home