How to pass values via terminal to a program function

Asked

Viewed 82 times

2

#include <stdio.h>
#include <string.h>
void soma(int a,int b);
int main(int argc,char *argv[])
{
    printf("A soma=");
    return 0;
}

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

As step the terminal values for the function parameters soma()?

Compile:

cc exemplo.c -o exemplo

Run (does not work):

./exemplo < soma 3 4
  • What is this < soma in the call to the application ? What is your goal ? The idea is to be able to specify the operation directly in the call ?

  • the goal is to pass a value to "a" and "b" and present the sum by calling the main program first

  • And why the text soma and the < are included in the call ?

  • the character "<" is to access a zone in the program I think(the function) but it is not to present the result. When I do . /example < sum 3 4 should write as result "The sum =7"

  • @user48571 The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

1

According to the previous answer, I think this is what you want:

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

int soma(int a, int b) {
    return a + b;
}

int main(int argc, char *argv[]) {
    printf("Soma: %d", soma((int)strtol(argv[1], NULL, 10) + (int)strtol(argv[2], NULL, 10)));
}

I put in the Github for future reference.

But if you want to make the separate impression (I wouldn’t, but I wouldn’t even have this auxiliary function either:

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

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

int main(int argc, char *argv[]) {
    printf("Soma: ");
    soma((int)strtol(argv[1], NULL, 10) + (int)strtol(argv[2], NULL, 10))
}

I put in the Github for future reference.

So it would make a little more sense:

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

int soma(char *primeiro, char *segundo) {
    return (int)strtol(primeiro, NULL, 10) + (int)strtol(segundo, NULL, 10));
}

int main(int argc, char *argv[]) {
    printf("Soma: %d", soma(argv[1], argv[2]));
}

I put in the Github for future reference.

You must not pass the "sum". If the intention is to call the function according to the text, and is not in the question, there complicates a little, or rather, depending on the required technique.

  • so I know how to do but I want to select a function in specific and not only this . /example < divide 4 2 (call of another function)

  • You know, but you didn’t. So you need to ask what you want. Read your question and see if it has that in it. No, you’re asking different things than you actually want.

  • The command I am requesting is very clear, I need to call a specific function that is within a program and I want to return the result passing values to it. The question is how do I pass these values to this function via terminal execution . /example < sum 3 4 (does not work). This Aki is not an argument from the main program "< sum 3 4"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.