CSM217: C for Computer Scientists

Lecture 4: Exercises

Exercise 1

Write a program to print the character representations of the ASCII values 0 - 127 inclusive. If the character is unprintable, then your program should indicate that it is unprintable. For example,

...
ASCII 126 is character ~
ASCII 127 is unprintable
Your program should contain only one printf statement, and the test to check whether a character is printable should be a conditional expression (of the form ?:) embedded in the printf statement. To find a function which tests the printability of a character, check out the ctype.h library. See Lecture 4 for information about conditional expressions.

The solution is here, or else you can mail me.

Exercise 2

In the program that you wrote for Exercise 1, change the data type of the variable a from int to char and re-compile the program. You should get a warning message. Run the program anyway. Can you explain what the error message means and why the program behaves the way it does? I won't post the answer to this particular problem just yet, but if you want to mail me your answers, please do. On the other hand, if your program does not change its behaviour, try running it on UNIX instead of Windows... (telnet to babe.cs.um.edu.mt).

Mail me your answer.

Exercise 3

Rewrite the program for Exercise 1 using a switch statement instead of conditional expressions.

The solution is here, or else you can mail me.