pete > courses > CS 202 Fall 26 > Tier 2, Phase 3, Problem 02: printing integers
Tier 2, Phase 3, Problem 02: printing integers
As discussed in class, one of the the main responsibilities of the printf function is to convert from the machine representation of data to a human-readable representation. For example, the "%d" format specifier causes printf to convert the corresponding integer parameter (which is in two’s complement) to printable ASCII characters that represent the same number.
In this assignment, you will write C functions that print ASCII representations of integers:
- The print_int function will take as its parameter a value of type int32_t and print it, using ASCII characters, as base 10.
- The print_uint function will take as its parameter a value of type uint32_t and print it, using ASCII characters, as base 10.
- The print_hex function take as its parameter a value of type uint32_t and print it, using ASCII characters, as base 16.
When printing, you may precede the digits with any (reasonable) number of spaces, if you like. Do not, however, pad hex values with zeroes.
You may use this file as a starting point: print-integers-skeleton.c. You should probably add other function calls to main to test your code. With the existing tests, however, this is the output it could produce (recall that leading spaces are acceptable):
$ ./print-integers
42
-40
42
0x2a
Your program must both compile and run on weathertop. You may edit your code on your own computer and scp it to weathertop, as you have done with assignment submissions, whenever you want to compile and run it. You may find this tedious, however, if you save and re-compile often (which is a good idea!). To make that easier, remote editing features of text editors are discussed at the bottom of the guide to using department machines.
Extra information
You may find this C program handy for reference, as it demonstrates several data types and operations, including puts: examples.c.
Restrictions
The only library function you are permitted to call from the three printing functions is puts. It takes as its parameter a string of characters and its effect is to output that string of characters to the screen, followed by a newline. (For the purpose of this assignment, let us assume that "a string of characters" is equivalent to "an array of characters"; it isn’t, quite, but that understanding will do for now.)
You may call printf from within the main function, but only to help with testing.
Note: if your program does not compile, we cannot test its functionality.
Submission Instructions
Copy the print-integers.c file to weathertop and then run:
$ 202 submit t2p3p02 print-integers.c
Submissions will not be accepted after 2pm on DDDDDDD, DD MMM YY.