Calling C function on *nix terminal

  • Thread starter BubblesAreUs
  • Start date
  • Tags
    Function
In summary, the conversation discusses how to call a function on a Unix terminal and how to take number inputs from the terminal in order to perform a minimum calculation. The main function needs to be adjusted to properly call the minimum function and display the results using printf. The language used in Unix does not affect the functionality of the program.
  • #1
BubblesAreUs
43
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.
 
Technology news on Phys.org
  • #2
You must take numbers from unix. So make main as
Code:
main(int argc, char **argv)
and see how take argument pointers on *argv[].
 
  • #3
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 ?
 
  • #4
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.
 
  • #5
Thanks mate. I have managed to call out the function. :)
 
  • #6
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.
 

1. How do I call a C function on a *nix terminal?

To call a C function on a *nix terminal, you will first need to compile the C code into an executable file. This can be done using a C compiler such as GCC. Once the file is compiled, you can simply run the executable file on the terminal to execute the C function.

2. Can I call a C function from any *nix terminal?

Yes, you can call a C function from any *nix terminal as long as the C code has been compiled into an executable file. The terminal will need to have a C compiler installed in order for the code to be compiled.

3. How do I pass arguments to a C function on a *nix terminal?

To pass arguments to a C function on a *nix terminal, you will need to use the command line arguments. These are values that are passed to the program when it is executed. In the C code, you can use the argc and argv parameters to access the command line arguments.

4. Can I call multiple C functions on a *nix terminal?

Yes, you can call multiple C functions on a *nix terminal by creating a main function that calls the other C functions. You can also compile multiple C files into a single executable file and run it on the terminal to execute multiple C functions.

5. Is it possible to call a C function on a *nix terminal without compiling the code?

No, it is not possible to call a C function on a *nix terminal without compiling the code. The C code needs to be compiled into an executable file before it can be executed on the terminal. However, you can use a C interpreter such as CINT to execute C code without the need for compilation.

Similar threads

  • Programming and Computer Science
Replies
4
Views
734
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
893
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
4
Views
900
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
4
Views
782
Back
Top