Skip to content

Instantly share code, notes, and snippets.

@phaya

phaya/ifequal.c Secret

Created October 12, 2013 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phaya/60828f76620fbb41a469 to your computer and use it in GitHub Desktop.
Save phaya/60828f76620fbb41a469 to your computer and use it in GitHub Desktop.
El siguiente programa imprime que "n es igual a 10" pero n se toma como valor inicial 2 ¿qué está pasando?
#include <stdio.h>
int main() {
int n = 2;
if (n = 10) {
printf("n is equal a 10\n");
}
return 0;
}
@ArturoBlazquez
Copy link

El problema está en que cuando pones el if hay que poner n==10, con dos iguales, que es el operador de igualdad. En este caso, como sólo hay un igual, le estás asignando a n el valor 10.

@phaya
Copy link
Author

phaya commented Oct 14, 2013

yes! El resultado de la asignación n=10 es lo que evalúa el ìf`, y como 10 != 0 resulta verdadero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment