| CSC 161 | Grinnell College | Spring, 2012 |
| Imperative Problem Solving and Data Structures | ||
The goal of this lab is to practice the concepts of and functions concerning input and output in the C programming language.
During this lab, you will gain familiarity with I/O, as well as learn some basic functions concerning I/O.
Move to the directory you use for this class, and create a directory for this lab.
getchar and putcharDownload and save the
file getchar-example.c to your
current directory, compile it, and run it a few times to make sure you
understand it. Recall that the single-character printing
function putchar, like getchar, deals with a
single character per call.
Recall that getchar gets a single character; blank space and newline characters are considered viable characters.
Like getchar, putchar takes a single
character. The function putchar prints a single character
to stdout.
getchar,
initialize the size of an array with the number you read, fill the
array of characters with entered characters, and then print the string
formed. (You may use printf to print the string.)
Recall that getchar returns a character. In the previous
lab, you learned about ASCII. Use what you learned to convert the
numerical character into an integer.
<ctype.h> has functions that
compare types and modify letter cases.
Examples:
getcharHint: be sure to include <ctype.h>.
printfprintf statement uses the wrong type for the declared
variable.
#include <stdio.h>
int
main()
{
int x = 9, y;
double s = 13.86;
char * word = "computer";
char ch = word[5];
y = (int) s;
printf ("\tThe value of x is %lf.\n", x);
printf ("\tThe value of y is %f.\n", y);
printf ("\tThe value of s is %d.\n", s);
printf ("\tThe value of word is %c.\n", word);
printf ("\tThe value of ch is %s.\n", ch);
printf ("\tThe value of ch is %d.\n", ch);
return 0;
}
Will the code compile? Save the program to a file in your directory and check. Run the program. Do any line(s) cause the program to segfault? Why? Think about what each of the types are and how to print those types.
Fix the line(s) that prevents the code from completing, and run the code. Explain why you get each incorrect value. How are integers, doubles, and floats treated by each other? How is a character different from a string?
Hint: consider how each variable type is stored and evaluated.
Change the printf statements so the correct values are
printed when the program is run, and check that your changes fixed the
code.
Look at the following two lines of code:
Will these two lines print the same or different output? Why?
Make sure both lines are in your code, compile, and run. Did the output match your expectations? If not, why did the code behave this way?
/****
*
* Program to print the values of different types, with proper spacing.
* It should print a as "7", b as "15.2594", and c as "something".
*
****/
#include <stdio.h>
int
main()
{
int a = 7;
double b = 15.2594;
char * c = "something";
printf ("The value of a is %d.\n", a);
printf ("The value of b is %.4lf.\n", b);
printf ("The value of c is %9s.\n", c);
return 0;
}
Compile and run the code. Is the output what you expected?
In the statement that prints the value of b, edit
the %.4lf to be %.2lf. What do you think
the output will be? Compile and run the code to check.
Now edit the statement that prints the value of b
to %.6lf. What do you think will happen? Why? Run the
code to check.
Given your previous experience, what do you think will happen if
you edit the line for the value of c from %9s
to %6s. What about editing it to %12s? Run
the program. Did your results match your expectations?
As you may recall, when a number is placed between the '%'
character and the variable type, the printf function
allocates at least that many spaces for the information. If the
information takes that much or more space, then the data is
printed normally. However, if the information takes less space, the
information is "right-aligned" with the allocated space, with the
"extra" spaces left blank. In contrast, when a '.' character
(followed by a number) is placed between the '%' and the variable
type, only that much space is allocated after the decimal
point. For example, if the number is 48.3557, and the print
statement indicates that only one value after the decimal should be
printed, the printed result will show 48.3 as the variable inserted.
printf SpacingModify your program from Exercise 1e that takes in an integer,
initializes an array of characters with that length, takes in characters
until the array is full, and then prints the string. For this exercise,
print each character with tabbed spacing. Use only a single
printf or fprintf statement.
%ns, where n is
some number, instead of a tab to separate each letter. In the section on data representation, you learned about the computer's binary representation of decimal numbers. Use what you learned to write a short program that prints the numbers 0 to 32 and their binary representations. Sample output is below:
Conversion Table for Integers and Binary
Numbers Binary Representation
0 000000
1 000001
2 000010
3 000011
4 000100
5 000101
6 000110
7 000111
8 001000
9 001001
...
28 011100
29 011101
30 011110
31 011111
32 100000
Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.