Formatting Input/Output in C language

70Views
No Comments

共计 2477 个字符,预计需要花费 7 分钟才能阅读完成。

Formatting Input/Output in C language refers to the ability to specify the format in which the data is read or written to the input/output devices. The standard I/O library in C provides a set of functions for formatting input/output operations.

The following are some of the commonly used functions for formatting input/output in C:

scanf()

This function is used to read formatted input from the standard input device (usually the keyboard). It takes a format string as its first argument, which specifies the format of the input, and reads the input values accordingly.

The format string consists of conversion specifiers, which begin with the percent (%) character, followed by a format specifier character that specifies the type of input. Some of the commonly used format specifiers are:

  • %d – for reading integers
  • %f – for reading floating-point numbers
  • %c – for reading a single character
  • %s – for reading a string of characters
int x;
float y;
scanf("%d%f", &x, &y); // reads an integer and a floating-point number from the keyboard and stores them in x and y, respectively

printf()

This function is used to write formatted output to the standard output device (usually the screen). It takes a format string as its first argument, which specifies the format of the output, and writes the output values accordingly.

The format string consists of conversion specifiers, which begin with the percent (%) character, followed by a format specifier character that specifies the type of output. Some of the commonly used format specifiers are:

  • %d – for writing integers
  • %f – for writing floating-point numbers
  • %c – for writing a single character
  • %s – for writing a string of characters
int x = 10;
float y = 3.14;
printf("The value of x is %d and the value of y is %f", x, y); // writes the string "The value of x is 10 and the value of y is 3.14" to the screen

sprintf()

This function is used to write formatted output to a character array instead of the standard output device. It takes a format string as its first argument, which specifies the format of the output, and writes the output values accordingly to the character array.

int x = 10;
float y = 3.14;
char str[100];
sprintf(str, "The value of x is %d and the value of y is %f", x, y); // writes the string "The value of x is 10 and the value of y is 3.14" to the character array str

sscanf()

This function is used to read formatted input from a string instead of the standard input device. It takes a format string as its first argument, which specifies the format of the input, and reads the input values accordingly from the string.

int x;
float y;
char str[] = "10 3.14";
sscanf(str, "%d%f", &x, &y); // reads an integer and a floating-point number from the string and stores them in x and y, respectively

Note that the format string in these functions should match the type and order of the variables that are being read or written. If the format string and the variables do not match, it can lead to undefined behavior or incorrect results.

正文完
 
Amitesh Kumar
Copyright notice: Our original article, by Amitesh Kumar 2023-02-22 publish, total 2477 words.
转载说明:Unless otherwise specified, all articles are published by cc-4.0 protocol. Please indicate the source of reprint.
Comment(No Comments)