Please note that my office hours have changed. Check my web page!
Like variables, expressions have a type. For our purposes, there are three (and a half) types: integer, double, boolean, and character. There aren't very many interesting operations on character types so let's skip those for now.
What is the value and type of the following expressions?
| ________ | ________ | 5+5 * 7+8 |
| ________ | ________ | (10 % 6) + (10 / 6) |
| ________ | ________ | ((double)(10 % 6)) + (10.0 / 6.0) |
| ________ | ________ | (0 == 0) |
| ________ | ________ | (0 == 0) || (0 == 1) |
| ________ | ________ | (0 == 0) && (0 == 1) |
| ________ | ________ | !(0 == 0) |
| ________ | ________ | -5 < 7 < 9 |
double x;
scanf("%lf", &x);
if (x < 0.0) {
printf("Your number is negative.\n");
}
Example 2.
if (0 != 0) {
printf("When do I execute?");
}
Example 3.
double percent;
printf("Enter a percentage between 0 and 100: ");
scanf("%lf", &percent);
if (percent < 0.0 || percent > 100.0) {
printf("Arrrgghh...");
percent = 0.0;
} else {
printf("Thank you.");
}
In his essay Anarchism Triumphant (it's not really about anarchism, by the way), Eben Moglen writes:
The function of source code in relation to other human beings is not widely grasped by non-programmers . . . In most programming languages, far more space is spent in telling people what the program does than in telling the computer how to do it.
In other words, a computer program is really a medium for interpersonal communication as much as, if not more than, a functional object. Make your code self-documenting, and where it is not self-documenting, document it with comments. Read the slides on style and take them seriously. I will post some specific style guidelines on my web page fairly soon. Please read them and adhere to them in assignments for this class.
Can you tell what the following obfuscated C does?
/* Originally by Ken Perlin, http://mrl.nyu.edu/perlin */
main(k){float i,j,r,x,y=-16;while(puts(""),y++<15)for(x
=0;x++<84;putchar(" .:-;!/>)|&IH%*#"[k&15]))for(i=k=r=0;
j=r*r-i*i-2+x/25,i=2*r*i+y/10,j*j+i*i<11&&k++<111;r=j);}
(If you're feeling curious, go into the labs one day and type it up. You'll be pleasantly surprised.)