Thông tin

Tác giả Mathieu Xhonneux
Hạn chót Không có hạn chót
Giới hạn nộp bài Không có giới hạn
Các tag chuyên mục S2, String, Level 1

Tags

Đăng nhập

[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.


Câu hỏi 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) {
Câu hỏi 2: 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() {
Câu hỏi 3: 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);
Câu hỏi 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) {