Unexpected result in C program

Asked

Viewed 82 times

1

In that code:

#include <stdio.h>
#include <stdlib.h>
int main() {
int x, y = 10;
//incrementa depois atribui
y = ++x;
printf("x = %d\n", x);
printf("y = %d\n", y);
system("pause");
return 0;

}

Compiled and executed in codeblocks it returns the size of the variable plus the increment; The result should be x = 11; and y = 11; In ideone it returns x and y = 1; The difference between x++; and ++x; is that x++ = 10 + 1 and ++x = 1 + 10?

  • Good question: has code, has the expected result, has the result obtained.

  • I don’t understand you pmg?

  • user ... many users ask questions that are difficult to answer: your question is efficient.

1 answer

3


You are not starting variable x

here:

int x, y = 10;

should be

int x = 10, y = 10;
  • Yes, but I did it like in the book.

  • 1

    then either the book is wrong or you read it wrong.

Browser other questions tagged

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