Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Been doing a task in C programming language. I had a problem with fmod which gave me a specific error after trying to execute and give permission.(Undefined reference to fmod, error ld returned 1 exit status). Some people helped me on this specific problem and told me what to do so I tried their command and It worked ( I used: gcc -o homework2 homework2.c -lm). Now Its giving me a different error and I'm not sure what the problem is since I can't really find a mistake in my code.

The error is:

line 4: syntax error near unexpected token '('
line 4: 'int main(){'

What I have tried:

C#
#include<stdio.h>
#include <math.h>

int main(){
//declaring variables
int test, a, b, n, k;
//factorial1 is for n, factorial2 is for k
double factorial1, factorial2, square, c;

//asking for number of input
printf("\n  Please type number of cases you want to input: ");
scanf("%d", &test);

// checking the number of test cases
if (test>=1 && test<= 200000){
  for(a=1; a<=test; a++){
    //asking for a positive integer
    printf("\n Please enter a positive number (integer):");
     scanf("%d", &n);
      //cheking the value of N
      if(n>=1 && n<=200000){
       // declaring factorials (setting them to value 1 each time we enter the loop)
       factorial1 = 1;
       factorial2 = 1;
       // n factorial
       for (b=1; b<=n; b++){
        factorial1*=b;
    }
       //n square factorial
       square=factorial1*factorial1;
       // k factorial
       for(k=1; ;k++){
       factorial2=factorial2*k;
       //making sure factorial2 (k factorial) is a multiple of factorial1 (n factorial)
       c=fmod(factorial2,square);
       if(c==0){
       //printing the value if the upper one is correct
       printf("%d",k);
       break;
      }
   }
}

else {
       //case when value n is not satisfied
       printf("\n Please make sure you typed a correct valued number");
      }
   }
}

else {
      //case when value of test cases is not satisfied
     printf("\n Please make sure you typed a correct value for test cases");
    }
return 0;
}  
Posted
Updated 14-Jan-21 8:53am

I just tried it using gcc via rextester, and your code shows no such error:
Compilation time: 0.12 sec, absolute service time: 0,29 sec
Error(s):
1693378824/source.c: In function ‘main’:
1693378824/source.c:12:1: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d", &test);
 ^~~~~~~~~~~~~~~~~~
1693378824/source.c:19:6: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
      scanf("%d", &n);
      ^~~~~~~~~~~~~~~
/tmp/ccqMGKKo.o: In function `main':
source.c:(.text.startup+0xff): undefined reference to `fmod'
collect2: error: ld returned 1 exit status
The reference error is probably due to a lack of library for the linker phase, but there are no compilation errors - just warnings you should fix.

Check you are compiling and editing the same file ...
 
Share this answer
 
Comments
CPallini 14-Jan-21 14:54pm    
5.You are right, the OP just need to link it with the math library.
From a high-level overview, the code looks nice, secondly the code "compiles" just fine too: C++ Shell[^].

What I feel like is there might be an invalid character (most likely an invisible one) that is causing trouble for the compiler. Oh, and don't forget the version of the compiler, platform that you are using...
 
Share this answer
 
Comments
CHill60 14-Jan-21 11:26am    
Luckily it also compiles as 'C' too :laugh:
Your code compiles fine (as already noted by our Griff). Just link it with the math library in order to obtain the executable, e.g.
gcc -Wall foo.c -lm

Please note, factorial results become 'unmanageable' very fast: even 64 bit integers can just hold 20!, no more.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900