1
#include <stdio.h>
void fun_one(int a,int b);
void fun_two(int a,int b);
int main()
{
    printf("Result=");
    return 0;
}

void fun_one(int a,int b){
    printf("%d\n",a+b);
}
void fun_two(int a,int b){
    printf("%d\n",a*b);
}

I compiled and run the program this way:

cc exec.c -o exec

./exec < fun_two 3 4

But it did not return what I expected.Is there an error in the command?

Output: Result=

user48571
  • 27
  • 8
  • What's your expected output? – George Apr 11 '18 at 21:35
  • Result=12 or 12 – user48571 Apr 11 '18 at 21:39
  • You do not call any of the functions, thart is why you only get the output of the function `main()`. You call the binary in a way which looks like you want to pipe the content of a file named "fun_two" into it - and two unrelated numbers. Please explain what you think you do with that way of calling your program. You are not trying to call one of the functions, giving it parameters and then expect output from it, are you? – Yunnosch Apr 11 '18 at 21:41
  • If you want to read arguments from the command line, you'll have to call main with parameters, and if you want to perform mathematical operations on those args, you'll need to parse them. – George Apr 11 '18 at 21:43
  • yes I need the result 12 in output when I execute ./exec < fun_two 3 4 but I think I have an error in the command entered – user48571 Apr 11 '18 at 21:44
  • 1
    Would the output of "Result=12" be OK for being called as `exec fun_two 3 4` ? In that case learn about command line arguments and `argc` `argv`. – Yunnosch Apr 11 '18 at 21:47
  • One of the questions you are asking is "how to use commandline parameters in C?" and is answered here: https://stackoverflow.com/questions/47535120/read-input-txt-file-and-also-output-bmp-file-from-terminal-c-programming/47536091#47536091 Other questions are "How do I call a funtion?", "How do I parse numerical values from char sequences?", "How can I call a function by its name in a string?" Those are too many questions in one. I recommend to start with a HelloWorld and some tutorials. – Yunnosch Apr 11 '18 at 21:54
  • To phrase it differently, pleae study this helpful article on how to solve most any problem in programming: https://ericlippert.com/2014/03/21/find-a-simpler-problem/ – Yunnosch Apr 11 '18 at 21:56

1 Answers1

1

This edit of your code shows how to use your function 2 with two number you enter.

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


void fun_one(int a,int b);
void fun_two(int a,int b);
int main(int, char **);


int main(int argc, char ** argv)
{
    int a,b;
    a= atoi(argv[1]);
    b=atoi(argv[2]);
    printf("Result=");fun_two(a,b); // similar to what you want from comments

    //using both functions
    printf("a+b=");fun_one(a,b); 
    printf("a*b=");fun_two(a,b); 
    return 0;
}

void fun_one(int a,int b){
    printf("%d\n",a+b);
    return;
}
void fun_two(int a,int b){
    printf("%d\n",a*b);
    return;
}

I have revised your code so that if you compile it with

gcc -o exec exec.c 

You will be able to run it with

./exec 4 3

to get the required output - note that I expect compilation with cc will give exactly the same output, but I can't test it.

What I have done is changed the definition of main so that it can accept input from when you call it. The extra bits are put into strings.

The function atoi converts ascii string to an integer number so that the numbers you put on the command line can be handled as numbers and not as strings.

Please note that this code is rather fragile and may seg. fault. if you don't put in two numbers after you call the program the result will be unpredictable. You could make it more reliable by checking the value of argc - the number of things typed on the line when you entered the command and by checking that atoi worked, but that would make the code longer and I think it is more important to see a way of doing what you want to achieve at the moment.

A more versatile code is below, which allows you to choose which function you want to run in the code....

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


void fun_one(int a,int b);
void fun_two(int a,int b);
int main(int, char **);


int main(int argc, char ** argv)
{
    int a,b;
    a= atoi(argv[2]);
    b=atoi(argv[3]);

    if (!strcmp("fun_one",argv[1])) 
    { 
         printf("Result=");fun_one(a,b); 
    }
    else if (!strcmp("fun_two",argv[1])) 
    { 
         printf("Result=");fun_two(a,b); 
    }
    else printf("Didn't understand function you requested.  \n\n");

    return 0;
}

void fun_one(int a,int b){
    printf("%d\n",a+b);
    return;
}
void fun_two(int a,int b){
    printf("%d\n",a*b);
    return;
}

The function now gives the following output for the following input

./exec fun_three 3 4
Didn't understand function you requested.  

./exec fun_one 3 4
Result=7
./exec fun_two 3 4
Result=12

so now with the code above you enter three things after the command - the function you want, then the two numbers - if the function is not recognised there is an error message.

The function strcmp compares two strings and returns zero if they are identical. zero is equal to false in logic so the ! symbol is used as a not, which converts zero to one and numbers not equal to zero to zero. The effect is that if the two string are identical the logical test will be true.

tom
  • 1,303
  • 10
  • 17
  • I am pretty sure that the fun_one is not for decoration and that OP wants a solution which also allows to call that function with parameters. In that light, your answer seems to only answer half or less of the question. It is however an accepted participation in the answering process. Maybe somebody else will provide the rest of the solution, parsing a function name form parameter and calling the coreresponding function. Or do you want to extend your answer to also cover that? – Yunnosch Apr 11 '18 at 21:59
  • @Yunnosch the OP said in comments the answer expected was 12 - so I put in fun2 and guessed the OP could change to fun2 -- but it is a good suggestion and I will change – tom Apr 11 '18 at 22:07
  • Sorry, but just calling both functions and not even expecting the input "fun_two" described by OP looks even less like an answer to OPs question. – Yunnosch Apr 11 '18 at 22:12
  • @Yunnosch - fair comment - I was concerned that putting in that functionality would make the code long and take it further away from the original. -- so now I added a second code which includes the ability to choose which function is used. – tom Apr 11 '18 at 22:31
  • Nice. However, consider whether the last, most versatile version is not the ony one needed, especially as it seems noticably less "fragile". – Yunnosch Apr 11 '18 at 22:52