Calling C function on *nix terminal

  • Context:
  • Thread starter Thread starter BubblesAreUs
  • Start date Start date
  • Tags Tags
    Function
Click For Summary

Discussion Overview

The discussion revolves around how to properly call a function in a C program when executed from a Unix terminal. It includes aspects of function invocation, command-line arguments, and basic input/output operations in C programming.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant asks how to call a function from the terminal after compiling a C program, specifically mentioning the use of command-line arguments.
  • Another participant suggests modifying the main function to accept command-line arguments and refers to using argument pointers.
  • A subsequent post questions the correct syntax for calling the function from the terminal, indicating confusion about how to pass arguments.
  • Another participant points out that the main function does not invoke the minimum function and suggests using standard input/output functions like scanf() and printf() for user interaction.
  • One participant expresses gratitude for successfully calling the function after making suggested changes.
  • Another participant reiterates the terminology of "calling a function" and emphasizes that the operating system does not affect the function call, highlighting the need for the main function to invoke the minimum function.

Areas of Agreement / Disagreement

Participants generally agree on the need for the main function to invoke the minimum function and the importance of handling input/output correctly. However, there is some confusion regarding the specifics of passing arguments from the terminal, indicating that the discussion remains somewhat unresolved.

Contextual Notes

Limitations include the lack of clarity on how to properly format command-line arguments and the absence of examples demonstrating the use of scanf() and printf() for input and output.

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.
 
Technology 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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 7 ·
Replies
7
Views
1K
Replies
4
Views
5K
  • · Replies 30 ·
2
Replies
30
Views
7K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 17 ·
Replies
17
Views
10K