Calling C function on *nix terminal

  • Thread starter Thread starter BubblesAreUs
  • Start date Start date
  • Tags Tags
    Function
AI Thread Summary
To call a function in a Unix terminal after compiling a C program with gcc, the main function must invoke the desired function, such as minimum. The initial code provided only returned immediately without executing the minimum function. To properly pass arguments to the program, the main function should be defined as main(int argc, char **argv), allowing it to accept command-line arguments. Input can be handled using scanf() for user input, and output can be displayed using printf(). A typical interaction would involve prompting the user for two numbers and then displaying the minimum value. The discussion emphasizes the importance of understanding basic C programming concepts, including function calls, input/output handling, and the structure of a functional main() to ensure the program operates as intended.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top