Problem to assign more than one value to variable

Asked

Viewed 246 times

2

#include <stdio.h>


int main(){
int d;
int e;
int peca1;  


printf("digite um lado da peca: ");
scanf("%i", &d);
printf("digite outro lado da peca: ");
scanf("%i", &e);

peca1 = d , e 
printf("%i, %i", peca1);

return 0;

}

I wanted to assign the value of d and e for it to be the value of sin1, however I cannot.

Should I use pointers to assign these 2 values to peca1?

  • Its variable is to type Int, there is no way you can save two different values in this variable.

  • in the case the weight would have a dimension rather than just a value?

  • you could put the same as string and concatenate the two

  • 1

    Or store the 2 values in an array and then display them.

  • I got it by array. Thanks for all your help!

  • @Brunocosta Did the answer solve your problem? Do you think you can accept it? If you don’t know how you do it, check out [tour]. This would help a lot to indicate that the solution was useful to you and to give an indication that there was a satisfactory solution. You can also vote on any question or answer you find useful on the entire site (when you accept you will have the 15 points required).

Show 1 more comment

1 answer

4

It is not possible to store two values in the same variable of a scalar type as in the case of integers. Nor does it seem useful to do this in this code. Of course, I know it can be an exercise that requires just this. If that’s the case, you should make this explicit.

I’m not even gonna talk about syntax errors. Without the error the code would even work if it had another expression, the comma operator serves to separate expressions and in case the last expression, in case the value of e, would be the final result.

If the goal really wasn’t even that and rather make a calculation with the two variables, then make the calculation.

If the intention really was to store two values in the same variable, this would have to be done with a composite type. It could be a pointer to a list with the two values, but I doubt it’s the intention. It could be a array which makes it possible to place a series of data which works in a manner analogous to array (though slightly differently). I am putting in the example because of the comment indicating that this is it.

From the exposition of the code seems to be more a case of using a structure, since it is not a sequence of data, but a specific set of data, after all it is a side and the other side, it has specificity, the data are members of a set and not elements of a sequence. This semantic difference is important.

Anyway without a clear statement, the goal is ambiguous.

#include <stdio.h>
typedef struct peca {
    int lado1;
    int lado2;
} peca;
int main() {
    int d = 0;
    int e = 0;
    printf("digite um lado da peca: ");
    scanf("%i", &d);
    printf("digite outro lado da peca: ");
    scanf("%i", &e);
    peca peca1 = { d, e };
    printf("\n%i, %i", peca1.lado1, peca1.lado2);
    int peca[] = { d, e };
    printf("\n%i, %i", peca[0], peca[1]);
}

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

Browser other questions tagged

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