Topic: [new user] Compile/build a pure C code (How to?/Examples?)

Hello,

I am a new user of SF.

I could successfully compile Fortran code. I would like now to compile a pure C code. I tried it with a simple code (see below) but it didn't work. Could you please give me a feedback, why this simple code doesn't work?

Thank you in advance,
Peter

/* Program quad_root.c */

#include <math.h>

main(){
   float a, b, c;
   float xl, x2;
   float D;

   printf("Enter coefficient a:");
   scanf("%f",&a);
   /* test for quadratic */
   if (a==0){
     printf("Illegal quadratic!\n");
     exit(0) ;
   }
   printf("Enter coefficient b:");
   scanf("%f",&b);
   printf("Enter coefficient c: " ) ;
   scanf("%f",&c);
   /* Compute argument for square root and
      test for complex roots */
   D = b*b - (4*a*c);
   if(D < 0){
     printf("complex roots! \n");
     exit(0);
   }
   /* Compute roots of quadratic*/
   xl = (-b-sqrt(D))/2*a);
   x2 = (-b+sqrt(D))/(2*a);
   /* Output the results*/
   printf("xl = %f\n",xl);
   printf("x2 = %f\n",x2);
   exit(0);
}

Re: [new user] Compile/build a pure C code (How to?/Examples?)

Peter,

Your code has a handful of problems that are not allowing it to compile.  First, if you're going to call printf and scanf, you need to also include stdio.h.  Second, if you're going to call exit, you need to include stdlib.h.  Third, there is a syntax error on line 29 that the compiler correctly flags:

.\alone.c:29:25: error: expected ';' before ')' token
   29 |    xl = (-b-sqrt(D))/2*a);
      |                         ^

If you look at line 30, it probably should look more like that.

With those changes, the code does run, though it isn't appearing in the Simply Fortran Console tab on Windows.  It works fine by running in an external console, though.  The modified code is:

/* Program quad_root.c */

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

main(){
   float a, b, c;
   float xl, x2;
   float D;

   printf("Enter coefficient a:");
   scanf("%f",&a);
   /* test for quadratic */
   if (a==0){
     printf("Illegal quadratic!\n");
     exit(0) ;
   }
   printf("Enter coefficient b:");
   scanf("%f",&b);
   printf("Enter coefficient c: " ) ;
   scanf("%f",&c);
   /* Compute argument for square root and
      test for complex roots */
   D = b*b - (4*a*c);
   if(D < 0){
     printf("complex roots! \n");
     exit(0);
   }
   /* Compute roots of quadratic*/
   xl = (-b-sqrt(D))/(2*a);
   x2 = (-b+sqrt(D))/(2*a);
   /* Output the results*/
   printf("xl = %f\n",xl);
   printf("x2 = %f\n",x2);
   exit(0);
}
Jeff Armstrong
Approximatrix, LLC

Re: [new user] Compile/build a pure C code (How to?/Examples?)

Hello Jeff,

Thank you very much for your time and for the quick reply! I copied the example from a book and did not expect to have these bugs. Do you have an idea, why it isn't appearing in the Simply Fortran Console tab on Windows ? I received a compilation warning:
    return type defaults to 'int' [-Wimplicit-int]

Thank you in advance,
Peter

Re: [new user] Compile/build a pure C code (How to?/Examples?)

I'm not sure why it wasn't appearing.  My guess is that there is an issue flushing the standard output prior to awaiting input.  It is considered a bug with Simply Fortran, and I'll look at it today.

You're still receiving an error because main functions are normally defined as:

int main(int argc, char *argv[])

and normally end with

return 0; /* Or any exit code */

The use of exit(0) in you code is a little surprising, especially if it's from a book.

Jeff Armstrong
Approximatrix, LLC

Re: [new user] Compile/build a pure C code (How to?/Examples?)

Thank you, Jeff !