Note that there are some explanatory texts on larger screens.

plurals
  1. POUndefined reference to function - Learning C
    primarykey
    data
    text
    <p>I'm learning C by translating some of the things I have done in Python to C. I have tried to look online as much as I can before coming here but it seems really difficult to find answers to what I'm looking for.</p> <p>What follows is my (so-far) translation of the Miller-Rabin test for a number's primality.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;math.h&gt; _Bool prime(long long n); int main() { int i = 6; for (i; i &lt; 9; i++) { if (prime(i)) { printf("%i\n", prime(i)); } } } _Bool prime(long long n) { int s = 0; int r = 0; int a_index = 0; // printf("the value of a_index when initialised is: %d\n", a_index); // printf("the value of s when initialised is: %d\n", s); int *a_list; _Bool is_prime = 1; _Bool composite_part_a = 1; _Bool composite_part_b = 1; long long d = n - 1; while (d % 2 == 0) { s++; d = d / 2; } if (4759123141 &lt;= n &amp;&amp; n &lt; 2152302898747) { // malloc a_list[0] = 2; a_list[1] = 3; a_list[2] = 5; a_list[3] = 7; a_list[4] = 11; } else if (9080191 &lt;= n &amp;&amp; n &lt; 4759123141) { // malloc a_list[0] = 2; a_list[1] = 7; a_list[2] = 61; } else if (1373653 &lt;= n &amp;&amp; n &lt; 9080191) { // malloc a_list[0] = 31; a_list[1] = 73; } else if (4 &lt;= n &amp;&amp; n &lt; 1373653) { a_list = (int *) malloc(sizeof(int) * 2); a_list[0] = 2; a_list[1] = 3; printf("the value of a_list[0] upon its first assignment is: %d\n", a_list[0]); // printf("the first element of a_list is: %d\n", a_list[0]); // printf("the second element of a_list is: %d\n", a_list[1]); } else if (n == 3 | n == 2) { return 1; } else if (n % 2 == 0 | n == 1) { return 0; } printf("the value of a_list[0] over here is: %d\n", a_list[0]); // printf("%d\n", a_list[1]); for (a_index; a_index &lt; sizeof(a_list) / sizeof(int); a_index++) { printf("test"); if ((long long)pow(a_index[a_list], d) % n != 1) { composite_part_a = 1; } else { composite_part_a = 0; } // printf("the value of r is: %d\n", r); // printf("the value of s is: %d\n", s); for (r; r &lt; s; r++) { printf("%lld\n", (int)pow(a_list[a_index], exp2(r) * d) % n); if ((long long)pow(a_index[a_list], exp2(r) * d) % n != -1) { composite_part_b = 1; } else { composite_part_b = 0; break; } } if (composite_part_a &amp;&amp; composite_part_b) { return 0; } } return is_prime; } </code></pre> <p>The trouble with learning C is there isn't much good literature for pure beginners, outside of what I hear about K&amp;R but that's in the mail and I can't get my hands on it right now. The program returns these errors:</p> <pre><code>3.c: In function ‘prime’: 3.c:52:26: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default] /tmp/ccGQnk9T.o: In function `prime': 3.c:(.text+0x272): undefined reference to `pow' 3.c:(.text+0x2b9): undefined reference to `exp2' 3.c:(.text+0x2db): undefined reference to `pow' 3.c:(.text+0x30b): undefined reference to `exp2' 3.c:(.text+0x32d): undefined reference to `pow' collect2: ld returned 1 exit status </code></pre> <p>First off, haven't I included to introduce pow and else? I know it's not proper to ask two questions and my main question is about pow and exp2, but if you do have a suggestion about the malloc as well feel free to include it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload