I have a problem trying to use an increment in the printf - C function

Asked

Viewed 156 times

0

Can anyone help me understand why the output of this code is: 24 23 and not 23 23 ???

int a =23;

printf("%d %d",a, a++);

inserir a descrição da imagem aqui }

  • Expected side effect. Search by Quence point at http://www.faqs.org/faqs/C-faq/faq/.

  • not to use this type of code, as stated in the answer below is indefnida the order of evaluation of the parameters for a function

1 answer

4


The standard language C does not establish the order of evaluation of the parameters of a function. Therefore, according to the standard of the language its code has undefined behavior. The order of evaluation will depend on each compiler.

In this case, it is clear that the compiler has chosen the a++ will be evaluated before the a.

Since the C language does not give you any guarantee about who will be evaluated first in this type of situation, I advise avoiding this kind of thing in a professional code.

  • Thanks for the help

Browser other questions tagged

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