printf, scanf

January 21 2002

announcements

some of you have printed copies of the lab schedule... these are most likely not correct anymore. print another copy... there shouldn't be any more changes to the lab schedule now.

the best way to get better at programming is to do lots of programming. so, don't hesitate to try things out on the computer. for example, if you're curious about what some printf statement will do, try it and find out. a lot of you seem to be afraid of breaking the computer... don't worry about the computer, it can take a lot of abuse. :) be fearless!

comments

use /* to begin a comment, and */ to end a comment. comments are completely ignored by the computer... they should be used to make your program easier for other people to undertand. complicated programs should have about as many lines of comments as lines of code. always comment your code... it makes it much easier for other people to read. try to avoid stating the obvious in your comments... for example:

/* not very useful comment: print PI * r * r */
/* better comment: print the area of the circle with radius r */
printf("%lf\n", PI * r * r);

more fun with printf

as we discussed last week, we can use printf in our programs to display information on the screen. the stuff that you write in the double quotes will appear on the screen, with some exceptions: a %d will be replaced by an integer, a %c will be replaced by a character, a %lf will be replaced by a double, a \n will be replaced by a newline, a \a will ring the bell, etc.

here's some more examples of printf statements, and the output they will produce:

command: printf("int: %d, char: %c, double: %lf\n", 5, '*', 5.0);
 output: int: 5, char: *, double: 5.000000

command: printf("you got %d%% on the test\n", 100);
 output: your got 100% on the test

command: printf("use \\n in your program to make newlines\n");
 output: use \n in your program to make newlines

one last note about printf: make sure you use the right format specifier [%d for ints, %c for characters, %lf for doubles] in your printf statements! weird things will happen if you don't. for example:

command: printf("oops %d\n", 5.5);
 output: oops 0

assigment

assignment statements are used to change the value of a variable. they are pretty straightforward... for example:

int students = 5;
students = 100;

things to note: the thing on the left side of the equals sign is the name of a variable that will receive the new value [the thing on the left must be a variable! we can't assign new values to constants, for example], and the thing on the right is the new value for the variable. don't forget the semicolon.

in assigment statements, if the value on the right is an int, and the variable on the left is a double, the int will be automatically promoted to a double. in other words:

double x;
double y;

/**
 * 100 will be automatically promoted to 100.0
 **/
x = 100;

/**
 * don't forget that 100/3 produces an int [33]!! the 33 will be
 * automatically promoted to 33.0
 **/
y = 100 / 3; 

printf("%lf %lf\n", x, y);

scanf

we use scanf to read input from the keyboard. scanf statements look a lot like printf statements:

char ch;
int x;
double y;

printf("enter a character, an integer, and a double:\n");
scanf("%c %d %lf", &ch, &x, &y);
printf("%c %d %lf\n", ch, x, y);

note that the scanf for reading the char, int, and double looks a lot like the printf used to print out the char, the int, and the double. for now, just put single spaces between your format specifers [%whatevers] in your scanf statements. the names of the variables that the input will be stored in must be preceded by an ampersand [&]. for now, don't worry about what the ampersand does, we'll talk about it a whole lot more in a few weeks.

++ and --

++ and -- are quick ways to increase or decrease the value of a variable by one. for example:

int x = 5;
double y = 3;
x++;
y--;
printf("%d %lf\n", x, y);

x++ increase the value of x by 1, so x is now 6. y-- decreases the value of y by 1, so y is now 2. the printf will say 6 2.000000

if you use ++ and --, use them on a line by themselves, as we did above. all kinds of weird stuff happens if you try to use them in a larger expression [if you're curious, see what printf("%d\n", x++); and printf("%d\n", ++x); do. they both increase the value of x by 1, but there is the issue of when the value is increased...]

exercies

go over the exercies we didn't get to last week.

given this code:

int x;
int y;
scanf("%d %d", &x, &y);

suppose i'm not allowed to change the lines above, but i am allowed to add more code. what do i need to add if i want to swap the values in x and y after the scanf? [i want whatever value is in x to be in y, and whatever value is in y to be in x]

modify the solutions to the exercises from last week to take input from the user [for example, the area of circle program should now ask the user for the radius].