Lecture plans for CISC105, 05F, 09.14 (1) Attendance (1.5) A few notes about printf/scanf int x; /* this line "declares" x */ printf("Please enter a value for x:"); scanf("%d",&x); printf("x=%d\n",x); * they are both functions * the things in the parens () are called arguments or parameters * the first argument is always the format string * the f in printf and scanf stands for "formatted". (1.6) Declaration of variables When you declare a variable, you give it a type and a name. int x; float y; char c; "int", "float" and "char" are all examples of "types" in C. They tell the compiler how much memory to set aside for the variable, and how to interpret the bits. short w; /* this declares a variable of type "short". On strauss, a short is an integer that is of size 16 bits. */ int a; /* this declares an integer which is of size 32 bits */ The number of bits set aside for an "int" or a "short" depends on the system. 0100 0001 0000 0000 short w = "16640"; /* binary will be 0100 0001 0000 0000 */ char initials[2] = "A"; /* binary will also be 0100 0001 0000 0000 */ (2) Calendar with reading assignments online (3) From Windows, if you use the SSH program from http://udeploy.udel.edu, you can print to a local printer from strauss using "kpr filename" [Thanks to Kevin Owocki, TA for CISC181, for this tip!] (4) More on printf, scanf The "f" stands for "formatted". printf => print formatted scanf => scan formatted Both printf and scanf are functions, written by someone else. We don't know how they work "inside"; they are like "black boxes". We can only observe their behavior from the outside. It is good to read about them in your text. It is also very good to experiment with them... play around with them. (5) Several examples of printf printf("%d %d %d\n", 1, 2, 3); int x, y, z; printf("%d %d %d\n", x, y, z); int a, b, c; a = 3; b = 4; c = 5; printf("%d %d %d\n", a, b, c); printf("%d %d %d\n", a, b); /* incorrect; not enough variables */ printf("%d %f %d\n", a, b, c); /* incorrect; type doesn't match */ double avogadro = 6.022E23; const double pi = 3.1415926535897932; printf("%lf %lf \n", avogadro, pi ); printf("%f %f \n", avogadro, pi ); /* should this work? */ /* an example of formatting things in columns */ printf("%5s %3s %7s\n", "a", "b", "c"); printf("%5s %3s %7s\n", "-", "-", "-"); printf("%5d %3d %7d\n", a, b, c); char dogsName[] = "Fido"; /* allocates 5 spaces in memory */ printf("My dog's name is %s\n",dogsName); /* You can do math directly inside */ int x=5; printf("If x is %d, then x times 2 is %d and x squared is %d\n", x, 2*x, x*x); /* another way to format it */ printf("If x is %d, then x times 2 is %d and x squared is %d\n", x, 2*x, x*x); /* the following looks correct, but isn't... ^ is NOT the exponent operator */ /* Instead it is bitwise XOR, something we might talk about later but not today! */ int y=5; printf("If y is %d, then y squared is NOT necessarily %d\n",y,y^2); (6) Playing with scanf /* The right way to read three integers: */ int a, b, c; printf("Please enter value for a: "); scanf("%d",&a); printf("Please enter value for b: "); scanf("%d",&b); printf("Please enter value for c: "); scanf("%d",&c); printf("The values I got: a=%d b=%d c=%d\n",a,b,c); Try the code above in various ways. Show what works, and what causes the code to break. How does it break? /* Another way: */ int a,b,c; printf("Please enter values for a b c, separated by spaces: "); scanf("%d%d%d",&a, &b, &c); printf("The values I got: a=%d b=%d c=%d\n",a,b,c); Try the code above in various ways. Show what works, and what causes the code to break. How does it break? (7) More fun with scanf... /* How to read in float? (%f) double? (%lf) */ /* What happens if you read a float with %d instead of %f? */ /* What happens if you read an int with %f instead of %d? */ /* What happens if you read a float with %lf instead of %f? */ /* What happens if you read a double with %d instead of %lf? */ /* What happens if you read a double with %f instead of %lf? */