📜  C 中格式化和未格式化的输入/输出函数及示例

📅  最后修改于: 2022-05-13 01:54:47.830000             🧑  作者: Mango

C 中格式化和未格式化的输入/输出函数及示例

本文重点详细讨论以下主题-

  • 格式化的 I/O 函数。
  • 未格式化的 I/O 函数。
  • 格式化 I/O 函数与未格式化 I/O 函数。

格式化的 I/O 函数

格式化的 I/O 函数用于获取用户的各种输入并向用户显示多个输出。这些类型的 I/O 函数可以帮助使用格式说明符以不同的格式向用户显示输出。这些 I/O 支持所有数据类型,如 int、float、char 等等。

为什么它们被称为格式化 I/O?

这些函数称为格式化 I/O 函数,因为我们可以在这些函数中使用格式说明符,因此我们可以根据需要对这些函数进行格式化。

一些格式说明符的列表-

S NO.Format Specifier          Type            Description                                                    
1%dint/signed intused for I/O signed integer value
2%ccharUsed for I/O character value
3%ffloatUsed for I/O decimal floating-point value        
4%sstringUsed for I/O string/group of characters                                                       
5%ldlong intUsed for I/O long signed integer value
6%uunsigned int   Used for I/O unsigned integer value
7%iunsigned intused for the I/O integer value
8%lfdoubleUsed for I/O fractional or floating data 
9%nprintsprints nothing 

本节将讨论以下格式化的 I/O 函数-

  1. 打印函数()
  2. 扫描函数()
  3. sprintf()
  4. sscanf()

打印():

printf()函数在 C 程序中用于在控制台屏幕上显示任何值,如浮点数、整数、字符、字符串等。它是一个已在 stdio.h(头文件)中声明的预定义函数。

语法 1:

显示任何变量值。

例子:

C
// C program to implement
// printf() function
#include 
  
// Driver code
int main()
{
    // Declaring an int type variable
    int a;
  
    // Assigning a value in a variable
    a = 20;
  
    // Printing the value of a variable
    printf("%d", a);
  
    return 0;
}


C
// C program to implement
// printf() function
#include 
  
// Driver code
int main()
{
    // Displays the string written
    // inside the double quotes
    printf("This is a string");
    return 0;
}


C
// C program to implement
// scanf() function
#include 
  
// Driver code
int main()
{
    int num1;
  
    // Printing a message on
    // the output screen
    printf("Enter a integer number: ");
  
    // Taking an integer value
    // from keyboard
    scanf("%d", &num1);
  
    // Displaying the entered value
    printf("You have entered %d", num1);
  
    return 0;
}


C
// C program to implement
// the sprintf() function
#include 
  
// Driver code
int main()
{
    char str[50];
    int a = 2, b = 8;
  
    // The string "2 and 8 are even number"
    // is now stored into str
    sprintf(str, "%d and %d are even number",
            a, b);
  
    // Displays the string
    printf("%s", str);
    return 0;
}


C
// C program to implement
// sscanf() function
#include 
  
// Driver code
int main()
{
    char str[50];
    int a = 2, b = 8, c, d;
  
    // The string "a = 2 and b = 8"
    // is now stored into str
    // character array
    sprintf(str, "a = %d and b = %d",
            a, b);
  
    // The value of a and b is now in
    // c and d
    sscanf(str, "a = %d and b = %d",
           &c, &d);
  
    // Displays the value of c and d
    printf("c = %d and d = %d", c, d);
    return 0;
}


C
// C program to implement
// getch() function
#include 
#include 
  
// Driver code
int main()
{
    printf("Enter any character: ");
  
    // Reads a character but
    // not displays
    getch();
  
    return 0;
}


C
// C program to implement
// the getche() function
#include 
#include 
  
// Driver code
int main()
{
    printf("Enter any character: ");
  
    // Reads a character and
    // displays immediately
    getche();
    return 0;
}


C
// C program to implement
// the getchar() function
#include 
#include 
  
// Driver code
int main()
{
    // Declaring a char type variable
    char ch;
  
    printf("Enter the character: ");
  
    // Taking a character from keyboard
    ch = getchar();
  
    // Displays the value of ch
    printf("%c", ch);
    return 0;
}


C
// C program to implement
// the putchar() function
#include 
#include 
  
// Driver code
int main()
{
    char ch;
    printf("Enter any character: ");
  
    // Reads a character
    ch = getchar();
  
    // Displays that character
    putchar(ch);
    return 0;
}


C
// C program to implement
// the gets() function
#include 
#include 
  
// Driver code
int main()
{
    // Declaring a char type array
    // of length 50 characters
    char name[50];
  
    printf("Please enter some texts: ");
  
    // Reading a line of character or
    // a string
    gets(name);
  
    // Displaying this line of character
    // or a string
    printf("You have entered: %s",
           name);
    return 0;
}


C
// C program to implement
// the puts() function
#include 
  
// Driver code
int main()
{
    char name[50];
    printf("Enter your text: ");
  
    // Reads string from user
    gets(name);
  
    printf("Your text is: ");
  
    // Displays string
    puts(name);
  
    return 0;
}


C
// C program to implement
// the putch() functions
#include 
#include 
  
// Driver code
int main()
{
    char ch;
    printf("Enter any character:\n ");
  
    // Reads a character from the keyboard
    ch = getch();
  
    printf("\nEntered character is: ");
  
    // Displays that character on the console
    putch(ch);
    return 0;
}


输出
20

语法 2:

显示任何字符串或消息

例子:

C

// C program to implement
// printf() function
#include 
  
// Driver code
int main()
{
    // Displays the string written
    // inside the double quotes
    printf("This is a string");
    return 0;
}
输出
This is a string

扫描():

scanf()函数在 C 程序中用于用户从键盘读取或获取任何值,这些值可以是任何数据类型,如整数、浮点数、字符、字符串等等。该函数在 stdio.h(头文件)中声明,这就是为什么它也是一个预定义函数。在 scanf()函数中,我们使用 &(address-of 运算符) 将变量值存储在该变量的内存位置。

句法:

例子:

C

// C program to implement
// scanf() function
#include 
  
// Driver code
int main()
{
    int num1;
  
    // Printing a message on
    // the output screen
    printf("Enter a integer number: ");
  
    // Taking an integer value
    // from keyboard
    scanf("%d", &num1);
  
    // Displaying the entered value
    printf("You have entered %d", num1);
  
    return 0;
}
输出
Enter a integer number: You have entered 0

输出:

Enter a integer number: 56
You have entered 56

冲刺():

sprintf 代表“字符串打印” 。此函数类似于 printf()函数,但此函数将字符串打印到字符数组中,而不是在控制台屏幕上打印。

句法:

例子:

C

// C program to implement
// the sprintf() function
#include 
  
// Driver code
int main()
{
    char str[50];
    int a = 2, b = 8;
  
    // The string "2 and 8 are even number"
    // is now stored into str
    sprintf(str, "%d and %d are even number",
            a, b);
  
    // Displays the string
    printf("%s", str);
    return 0;
}
输出
2 and 8 are even number

sscanf():

sscanf 代表“字符串scanf”。此函数类似于 scanf()函数,但此函数从字符串或字符数组而不是控制台屏幕读取数据。

句法:

例子:

C

// C program to implement
// sscanf() function
#include 
  
// Driver code
int main()
{
    char str[50];
    int a = 2, b = 8, c, d;
  
    // The string "a = 2 and b = 8"
    // is now stored into str
    // character array
    sprintf(str, "a = %d and b = %d",
            a, b);
  
    // The value of a and b is now in
    // c and d
    sscanf(str, "a = %d and b = %d",
           &c, &d);
  
    // Displays the value of c and d
    printf("c = %d and d = %d", c, d);
    return 0;
}
输出
c = 2 and d = 8

未格式化的输入/输出函数

未格式化的 I/O 函数仅用于字符数据类型或字符数组/字符串,不能用于任何其他数据类型。这些函数用于在控制台读取用户的单个输入,并允许在控制台显示值。

为什么它们被称为未格式化的 I/O?

这些函数被称为未格式化的 I/O 函数,因为我们不能在这些函数中使用格式说明符,因此不能根据我们的需要格式化这些函数。

本节将讨论以下未格式化的 I/O 函数-

  1. getch()
  2. getche()
  3. 获取字符()
  4. putchar()
  5. 获取()
  6. 放()
  7. 推销()

获取():

getch()函数由用户从键盘读取单个字符,但不会在控制台屏幕上显示该字符,并且无需按 Enter 键即可立即返回。此函数在 conio.h(头文件)中声明。 getch() 也用于保持屏幕。

句法:

例子:

C

// C program to implement
// getch() function
#include 
#include 
  
// Driver code
int main()
{
    printf("Enter any character: ");
  
    // Reads a character but
    // not displays
    getch();
  
    return 0;
}

输出:

getche():

getche()函数从用户的键盘读取单个字符并将其显示在控制台屏幕上,并立即返回而不按回车键。此函数在 conio.h(头文件)中声明。

句法:

例子:

C

// C program to implement
// the getche() function
#include 
#include 
  
// Driver code
int main()
{
    printf("Enter any character: ");
  
    // Reads a character and
    // displays immediately
    getche();
    return 0;
}

输出:

获取字符():

getchar()函数用于仅从键盘读取第一个单个字符,无论用户是否键入了多个字符,并且此函数一次读取一个字符,直到且除非按下回车键。此函数在 stdio.h(头文件)中声明

句法:

例子:

C

// C program to implement
// the getchar() function
#include 
#include 
  
// Driver code
int main()
{
    // Declaring a char type variable
    char ch;
  
    printf("Enter the character: ");
  
    // Taking a character from keyboard
    ch = getchar();
  
    // Displays the value of ch
    printf("%c", ch);
    return 0;
}

输出:

放字符():

putchar()函数用于一次显示单个字符,方法是将该字符直接传递给它或传递一个已存储字符的变量。此函数在 stdio.h(头文件)中声明

句法:

例子:

C

// C program to implement
// the putchar() function
#include 
#include 
  
// Driver code
int main()
{
    char ch;
    printf("Enter any character: ");
  
    // Reads a character
    ch = getchar();
  
    // Displays that character
    putchar(ch);
    return 0;
}

输出:

获取():

get()函数由用户从键盘读取一组字符或字符串,这些字符存储在字符数组中。此函数允许我们编写以空格分隔的文本或字符串。此函数在 stdio.h(头文件)中声明。

句法:

例子:

C

// C program to implement
// the gets() function
#include 
#include 
  
// Driver code
int main()
{
    // Declaring a char type array
    // of length 50 characters
    char name[50];
  
    printf("Please enter some texts: ");
  
    // Reading a line of character or
    // a string
    gets(name);
  
    // Displaying this line of character
    // or a string
    printf("You have entered: %s",
           name);
    return 0;
}

输出:

放():

在 C 编程中,puts()函数用于显示已经存储在字符数组中的一组字符或字符串。此函数在 stdio.h(头文件)中声明。

句法:

例子:

C

// C program to implement
// the puts() function
#include 
  
// Driver code
int main()
{
    char name[50];
    printf("Enter your text: ");
  
    // Reads string from user
    gets(name);
  
    printf("Your text is: ");
  
    // Displays string
    puts(name);
  
    return 0;
}

输出:

推销():

putch()函数用于显示用户给定的单个字符,并且该字符打印在当前光标位置。此函数在 conio.h(头文件)中声明

句法:

例子:

C

// C program to implement
// the putch() functions
#include 
#include 
  
// Driver code
int main()
{
    char ch;
    printf("Enter any character:\n ");
  
    // Reads a character from the keyboard
    ch = getch();
  
    printf("\nEntered character is: ");
  
    // Displays that character on the console
    putch(ch);
    return 0;
}

输出:

格式化 I/O 与未格式化 I/O

S No.Formatted I/O functions                              Unformatted I/O functions                       
1These functions allow us to take input or display output in the user’s desired format.These functions do not allow to take input or display output in user desired format.
2These functions support format specifiers.These functions do not support format specifiers.
3These are used for storing data more user friendlyThese functions are not more user-friendly.
4Here, we can use all data types.Here, we can use only character and string data types.
5printf(), scanf, sprintf() and sscanf() are examples of these functions.getch(), getche(), gets() and puts(), are some examples of these functions.