Information

Author(s) Mathieu Xhonneux
Deadline Geen deadline
Submission limit No limitation
Category tags s2, category_string, level1

Tags

Sign in

[S2] Printing data

In this exercise, you will familiarize yourself with the functions printf(3) (printing on the standard output) and sprintf(3) <`printf (text formatting). Consult the documentation before coding to learn how to use these functions properly.


Question 1: Digital

printf can also print the content of a variable. Write the body of the function print_digit.

/* print_digit
 * @n: an integer
 *
 * result: print to stdout "The magic number is NUMBER.\n"
 */
void print_digit(int number) {
Question 2: Floating away

printf is a powerful formatting tool which can handle several data types, and several formatting parameters.

What would the following call print to stdout ?

float PI = 3.1415926;
printf("pi = %.4f", PI);
Question 3: Foo

The function printf writes data to the standard output stream (stdout).

Write the body of the function print_foo which will print "foo\n" (without the quotes, the symbol '\n' represents a line feed, i.e. the end of a line) to stdout.

void print_foo() {
Question 4: sprintf

sprintf(2) is a variant of printf(2) which doesn't write to stdout, but instead writes to a buffer.

Write the body of the function format_str.

/* format_str, example:
 * format_str(buf, 42, "Olivier", 'B') will write into buf
 * the string "Mister Olivier B. has 42 eggs" (no line feed)
 * The given buffer is guaranteed to be big enough
 */
void format_str(char *buffer, unsigned int d, char *name, char initial) {