if.c

/*
 * this demponstrates the if statement; normally this prints that
 * "x is not 12".
 *
 * compile with -DBAD to get the code that assigns 12 to x in the condition
 * of the if statement; do not define it to get the code that does the
 * comparison and not the assignment
 *
 * Matt Bishop, ECS 36A
 * April 9, 2024    original version
 */
#include <stdio.h>


int main(void)
{
	int x = 3;          /* used to either hold 3 or be assigned 12 */
    
	/* the if statement */
#ifdef BAD
	/* this condition is an assignment */
	if (x = 12)
#else
	/* this condition is an assignment */
	if (x == 12)
#endif
		printf("x is 12!\n");
	else
		printf("x is not 12!\n");

	/*
	 * all done!
	 */
	return(0);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh