1

I've crafted a small code to process a variable number of strings, but some how I got these strings processed in reverse order. The expected output is: Jolasd14; actual output is: asdJol14.

int Columna(int anchoColumna,...){
    va_list longitudCadena;
    va_start ( longitudCadena, anchoColumna );
    char caracter;
    do{
        caracter =va_arg ( longitudCadena, int );
        anchoColumna -= ( int ) caracter;
    } while ( caracter != '\0' );
    va_end ( longitudCadena );
    return anchoColumna;
}
int main ( int cantidadArgumentos, char** argumentos ) {
    printf("%d",Columna(20,printf("Jol"),printf("asd")));
    return 0;
}


EDIT: I tried a similar thing with strings only and I got the expected output.

ExpectedOutput

  • The premise of your question is wrong. It's any function call that can do this as the order of function argument evaluation is unspecified in C. – Jens Apr 23 '20 at 05:40

1 Answers1

3

The order that printf("Jol") and printf("asd") execute is not specified. They both occur before Columna() is executed.

Simply executed them is the order you want before the Columna() call, passing in their respective return values.

int a = printf("Jol");
int b = printf("asd");
printf("%d",Columna(20,a,b);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256