Assign values to a variable

Asked

Viewed 668 times

1

I have the following program:

#include <stdio.h>
int a,b;
int main(int argc,char *argv[]){
    int c;
    printf("%d",a+b);
    printf("%d",c);
    return 0;
}

How do I pass a value to the global or local variable via terminal? When I output the program, I want you to return the sum given a command via terminal, example:

I compile the program:

cc exemplo.c -o exemplo

Then to do the output:

./exemplo < a=1 b=2
  • I don’t understand what you want, want to ask the user to enter the data?

  • i enter the command to output the program but need to pass the values 1 and 2 for the variables a and b within the program. Is it not possible to do this friction directly?. /example < a=1 b=2

2 answers

1

In its main function, the argc represents the number of arguments being passed, while the argv is an array with each argument. In your example, by invoking ./exemplo 1 2, would be 3 arguments (argc = 3), being them:

argv[0] = "example"

argv[1] = "1"

argv[2] = "2"

This way, you can use them like this:

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

int a,b;
int main(int argc,char *argv[]){
    int c;

    // string to long(string, endptr, base)
    // Converte o argv de String para Inteiro
    a = strtol(argv[1], NULL, 10);
    b = strtol(argv[2], NULL, 10);

    printf("%d",a+b);
    printf("%d",c);
    return 0;
}

Test and give me feedback. I’m currently without a C compiler, but edit if something is out when I test.

0


You need to get the data from array of arguments that the main() takes. Takes each of the elements without taking 0 which is the name of the executable.

How the argument comes as string, need to make a conversion with strtol().

It has a series of checks that would be good to do, how to know if came the amount of arguments needed, if the number conversion worked and others that might be useful to the problem.

You don’t need variables to display the sum. But if you’re going to use variables for something else, don’t make them global, you almost never need.

You don’t need this static variable, I just did so by the limitation of ideone.

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

static char *argv[] = { "app", "123", "456" }; //gambiarra pra simular a passagem de argumento pela linha de comando

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • then there is no form or command that can directly assign a value to a variable other than by agrv[]

  • No, how would you imagine it could be?

  • I’m not sure but my teacher I think called a function that was inside the program with prototype int soma(int a, int b) later when called on the terminal (I have Aki doubts) . /example < sum 1 2 . And returns 3

  • There is another question, without knowing what you need, it is difficult to give an adequate answer.

  • but I’m not getting how to pass these values to that int sum(int a, int b)

  • Your question says nothing of this.

  • but that’s what I meant but I thought I used global variables to receive the values

  • We can only answer what was asked, no one can get inside your head and know what is going on there, only what is written can be answered.

Show 3 more comments

Browser other questions tagged

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