Calling C function on *nix terminal

  • Context:
  • Thread starter Thread starter BubblesAreUs
  • Start date Start date
  • Tags Tags
    Function
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 2K views
BubblesAreUs
Messages
43
Reaction score
1
Code:

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

int main()
{ return EXIT_SUCCESS;
}

float minimum( float x, float y)
{
if (x < y)

return x;
else
return y;

}

/Code

How does one call out a function on a Unix terminal? After compiling with gcc, I have tried using ./a.out minimum(4.0,7.0) ( or even [4.0,7.0] , but no minimum value is returned.
 
Physics news on Phys.org
You must take numbers from unix. So make main as
Code:
main(int argc, char **argv)
and see how take argument pointers on *argv[].
 
theodoros.mihos said:
You must take numbers from unix. So make main as
Code:
main(int argc, char **argv)
and see how take argument pointers on *argv[].
Thanks. I've made changes you suggested and yet no output is returned.

Do I type the following into the terminal:

./a.out minimum 4,9 ?
 
BubblesAreUs said:
int main()
{return EXIT_SUCCESS;
}

Your main() function literally does nothing except return immediately. Your minimum() function does not get performed at all, because your main() function does not call (invoke) it.

Also, you need to get the numbers that you want minimum() to act on, into the program somehow, and you have to display the results of your calculation. theodoros.milhos has indicated one way to do the input. The usual way in a beginner's program is to use scanf() for input, and printf() for output, so that when you run the program it looks something like this:

Code:
./a.out
Give me two numbers:

and then you enter two numbers, and it goes on like this:

Code:
./a.out
Give me two numbers: 4.0 7.0
The minimum is 4.0.

All this is covered in the first chapter of any decent C textbook or online tutorial. I suggest you find one and start from the beginning, which usually involves a "Hello, world!" program that does only output. Then you will learn about variables, and input, and how to use functions.
 
Thanks mate. I have managed to call out the function. :)
 
BubblesAreUs said:
Thanks mate. I have managed to call out the function. :)
The usual terminology is that you "call a function" not "call it out."

Also, it makes no difference whether the OS is Unix, Linux, Windows, or whatever. The problem was that your main() function wasn't doing anything except immediately returning a value of 0. What was missing was the call to your minimum function.