1

My program is to get (a^b)%c . my program -

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

double powfunction (double x, double y)
{
    double answer = pow(x,y);
    return answer;
}
int main()
{
    int t, result, c, asign;
    double a, b;
    scanf("%d",&t);
    for(t;t>0;t--){
        scanf("%lf %lf %d",&a,&b,&c);
        asign=powfunction(a,b);
        printf("%d",asign%c);
    }
}

I named it tt.c . when I tried to run the program writing gcc -o tt tt.c -lm in linux terminal it's giving the following output.

tt.c:9:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    9 | main()
      | ^~~~
tt.c: In function ‘main’:
tt.c:15:12: warning: format ‘%lf’ expects argument of type ‘double *’, but argument 2 has type ‘double’ [-Wformat=]
   15 |   scanf("%lf %lf %d",a,b,c);
      |          ~~^         ~
      |            |         |
      |            double *  double
tt.c:15:16: warning: format ‘%lf’ expects argument of type ‘double *’, but argument 3 has type ‘double’ [-Wformat=]
   15 |   scanf("%lf %lf %d",a,b,c);
      |              ~~^       ~
      |                |       |
      |                |       double
      |                double *
tt.c:15:19: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat=]
   15 |   scanf("%lf %lf %d",a,b,c);
      |                  ~^      ~
      |                   |      |
      |                   int *  int
pronay@MackbookPro:~$ 

After printing this the program is exited. Any solution for this?

I used gcc -o tt tt.c -lm cmd to run it because someone said this is how to link the math library

1 Answers1

0

The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.

You are passing the variables by value to scanf. However scanf expects pointers to the variables. You need to do this instead:

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

double powfunction (double x, double y)
{
    double answer = pow(x,y);
    return answer;
}
main()
{
    int t, result, c, asign;
    double a, b;
    scanf("%d",&t);
    for(t;t>0;t--){
        //pass pointers to a, b, c instead
        scanf("%lf %lf %d",&a,&b,&c);
        asign=powfunction(a,b);
        printf("%d",asign%c);
    }
}

Also the main function should have this signature

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

or

int main(void)

and should return an integer value. If it does not, the return value is assumed to be 0.

lulle2007200
  • 888
  • 9
  • 20
  • ```#include #include double powfunction (double x, double y) { double answer = pow(x,y); int answer1=answer; return answer1; } int main() { int t, result, c, asign; double a, b; scanf("%d",&t); for(t;t>0;t--){ scanf("%lf %lf %d",&a,&b,&c); asign=powfunction(a,b); printf("%d",asign%c); } }``` –  May 02 '21 at 14:32
  • I made my program like this –  May 02 '21 at 14:33
  • its now showing `/usr/bin/ld: cannot open output file source: Is a directory collect2: error: ld returned 1 exit status` now –  May 02 '21 at 14:33
  • 1
    @PronaySarker That means the name you're giving to the resulting executable is the name of an existing directory. Use a different name, or rename / remove the directory. – dbush May 02 '21 at 14:41
  • I did it right before 2/3 mins, but it's not taking any input. Just exiting the programm –  May 02 '21 at 14:48
  • The `main` function can also have the prototype: `int main(void)` – DarkAtom May 02 '21 at 14:59
  • And according to C99, if `main` doesn't explicitly return a value, it is assumed it returns 0. – DarkAtom May 02 '21 at 15:04
  • Right, edited the answer. Imo not returning a value is something you should do, but i guess its an option after all. – lulle2007200 May 02 '21 at 15:16