Problem to assign more than one value to variable

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 to be the value of peca1, however I can not.

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

    
asked by anonymous 12.05.2016 / 20:24

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. It does not seem to be of any use to do this in this code. Of course, I know it can be an exercise that demands just that. If this is the case, you should make this explicit.

I will not even mention the 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 this case the value of e , would be the end result.

If the goal was not really this, but make a calculation with the two variables, then do the calculation.

If the intention was really to store two values in the same variable, this would have to be done with a compound type. It could be a pointer to a list with both values, but I doubt that's the intention. It could be an array that allows you to put a series of data that works analogously to the array (albeit slightly differently). I'm putting in the example because of the comment indicating what this is.

By exposing 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 is one side and the other side, 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]);
    return 0;
}

See working on ideone and CodingGround .

    
12.05.2016 / 21:53