1

This is driving me absolutely crazy because it's such a simple thing to do but I can't seem to get it.

Let's say I have a program called test.c whose content is as follows.

#include <stdio.h>

main() {
    int bool_or(int x, int y) {
        int result = x|y;
        //return result;
        printf("%d\n", result);
}

int bool_xor(int x, int y) {
    int result= x^y;
        //return result;
        printf("%d\n", result);
}
}

Now let's say I want to make a function call to one of those functions from the terminal. How do I do that? I compiled the program and I can manually comment out the function definitions and input specific values in the editor and get something to display, but what if I want to simply just write something like bool_xor(3, 5) on the command line like I would if I were running a language like Python.

Basically what I'm asking is how do I make a function call. Thanks!

4
  • Are you able to define multiple function inside main(){...} function?? – αғsнιη Dec 31 '14 at 7:14
  • Similar to this Q/A How to call just a function from text script? – αғsнιη Dec 31 '14 at 7:15
  • Should I not be able to? How else could I go about running a function or having multiple functions in a single text file? Also I checked the Q/A and I tried it out but it didn't work because bool_or and bool_xor aren't already defined functions. – Leo P Dec 31 '14 at 7:43
  • 4
    ask this on stackoverflow.com – user284234 Dec 31 '14 at 8:40
4

I'm sorry, but you have to read up on C syntax. There were two major issues:

  • Defining functions inside main is not possible in standard C and even in the GCC extension, it certainly doesn't work how you think it does.
  • Calling a function doesn't happen by magic, you must tell what to use as parameters and how to rework them to a suitable form.

I have re-worked your code to a working state (in a file called bool_xor.c):

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

int bool_xor(int x, int y) {
    int result= x^y;
    printf("%d\n", result);
    return 0;
}

int main(int argc, char **argv) {
    if (argc<3) {
        printf("Too few args.\n");
        exit(1);
    }

    return bool_xor( atoi(argv[1]), atoi(argv[2]) );
}

And now compile and a couple of simple tests:

$ gcc -Wall --pedantic -o bool_xor bool_xor.c 

$ ./bool_xor 1 2
3

$ ./bool_xor 7 5
2

To create a separate or function, you will either need to reengineer the main() to decide which function it's calling or just have another bool_or.c file and generate a separate binary that does that single job (like this one does).

Here's an example of a combined compare.c that takes an xor or or argument before the values to determine which internal function to execute.

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

int bool_or(int x, int y) {
    printf("%d\n", x|y);
    return 0;
}

int bool_xor(int x, int y) {
    printf("%d\n", x^y);
    return 0;
}

int main(int argc, char **argv) {
    if (argc!=4) {
        printf("Needs 3 parameters. Expects:\n\t%s xor|or value value\n", argv[0]);
        exit(1);
    }

    /* TODO check that arguments are valid */

    if (strcmp(argv[1], "xor") == 0)
        return bool_xor( atoi(argv[2]), atoi(argv[3]) );
    return bool_or( atoi(argv[2]), atoi(argv[3]) );
}
7
  • I believe GCC has extensions for nested functions, but yes, this is not portable C. – muru Dec 31 '14 at 9:02
  • gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html - right! I learnt something new today. ;-) – Hannu Dec 31 '14 at 9:07
  • There's an obvious exception to the "Calling a function doesn't happen by magic" point in that the main function is called implicitly. – Oli Dec 31 '14 at 9:13
  • I like the edits, somehow it proves that even if you think you know the answer - you still might be wrong, at least partly. – Hannu Dec 31 '14 at 9:32
  • 1
    @LittleByBlue C is not an interpreted language to be used so, I'm afraid. – muru Dec 31 '14 at 10:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.