亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C 输入输出 (I/O)

C 输入输出 (I/O)

在本教程中,您将学习使用 scanf() 函数从用户那里获取输入,并使用 printf() 函数向用户显示输出。

视频:在 C 编程中获取用户输入

C 输出

在C编程中,printf() 是主要的输出功能之一。该函数将格式化的输出发送到屏幕。例如,


示例 1:C 输出

#include <stdio.h>    
int main()
{ 
    // Displays the string inside quotations
    printf("C Programming");
    return 0;
}

输出

C Programming

这个程序是如何工作的?


示例2:整数输出

#include <stdio.h>
int main()
{
    int testInteger = 5;
    printf("Number = %d", testInteger);
    return 0;
}

输出

Number = 5

我们使用 %d 打印 int 的格式说明符 类型。在这里,%d 引号内的内容将替换为 testInteger 的值 .


示例 3:float 和 double 输出

#include <stdio.h>
int main()
{
    float number1 = 13.5;
    double number2 = 12.4;

    printf("number1 = %f\n", number1);
    printf("number2 = %lf", number2);
    return 0;
}

输出

number1 = 13.500000
number2 = 12.400000

打印 float ,我们使用 %f 格式说明符。同样,我们使用 %lf 打印 double 价值观。


示例 4:打印字符

#include <stdio.h>
int main()
{
    char chr = 'a';    
    printf("character = %c", chr);  
    return 0;
} 

输出

character = a

打印 char ,我们使用 %c 格式说明符。


C 输入

在 C 编程中,scanf() 是从用户那里获取输入的常用功能之一。 scanf() 函数从键盘等标准输入中读取格式化输入。


示例 5:整数输入/输出

#include <stdio.h>
int main()
{
    int testInteger;
    printf("Enter an integer: ");
    scanf("%d", &testInteger);  
    printf("Number = %d",testInteger);
    return 0;
}

输出

Enter an integer: 4
Number = 4

在这里,我们使用了 %d scanf() 中的格式说明符 取int的函数 来自用户的输入。当用户输入一个整数时,它被存储在 testInteger 变量。

注意,我们使用了 &testIntegerscanf() 内 .这是因为 &testInteger 获取 testInteger 的地址 ,而用户输入的值存储在该地址中。


示例 6:浮点和双输入/输出

#include <stdio.h>
int main()
{
    float num1;
    double num2;

    printf("Enter a number: ");
    scanf("%f", &num1);
    printf("Enter another number: ");
    scanf("%lf", &num2);

    printf("num1 = %f\n", num1);
    printf("num2 = %lf", num2);

    return 0;
}

输出

Enter a number: 12.523
Enter another number: 10.2
num1 = 12.523000
num2 = 10.200000

我们使用 %f%lf float 的格式说明符 和 double 分别。


示例 7:C 字符 I/O

#include <stdio.h>
int main()
{
    char chr;
    printf("Enter a character: ");
    scanf("%c",&chr);     
    printf("You entered %c.", chr);  
    return 0;
}   

输出

Enter a character: g
You entered g

当用户在上述程序中输入一个字符时,该字符本身不被存储。而是存储一个整数值(ASCII 值)。

当我们使用 %c 显示该值时 文本格式,显示输入的字符。如果我们使用 %d 显示字符,它的ASCII值被打印出来。


示例 8:ASCII 值

#include <stdio.h>
int main()
{
    char chr;
    printf("Enter a character: ");
    scanf("%c", &chr);     

    // When %c is used, a character is displayed
    printf("You entered %c.\n",chr);  

    // When %d is used, ASCII value is displayed
    printf("ASCII value is %d.", chr);  
    return 0;
}

输出

Enter a character: g
You entered g.
ASCII value is 103.

I/O 多值

以下是您可以从用户那里获取多个输入并显示它们的方法。

#include <stdio.h>
int main()
{
    int a;
    float b;

    printf("Enter integer and then a float: ");
  
    // Taking multiple inputs
    scanf("%d%f", &a, &b);

    printf("You entered %d and %f", a, b);  
    return 0;
}

输出

Enter integer and then a float: -3
3.4
You entered -3 and 3.400000

I/O 格式说明符

从上面的例子可以看出,我们使用

下面列出了常用的 C 数据类型及其格式说明符。

数据类型 格式说明符
int %d
char %c
float %f
double %lf
short int %hd
unsigned int %u
long int %li
long long int %lli
unsigned long int %lu
unsigned long long int %llu
signed char %c
unsigned char %c
long double %Lf

C语言

  1. C# 基本输入和输出
  2. C++ 基本输入/输出
  3. Python 输入、输出和导入
  4. Python 文件 I/O
  5. Java 基本输入和输出
  6. Axiomtek:功能丰富的机器视觉应用系统
  7. 加入:用于可扩展数据通信的 I/O 模块
  8. C++ 基本输入/输出:Cout、Cin、Cerr 示例
  9. D 锁存器
  10. C - 输入和输出
  11. C - 文件 I/O
  12. C# - 文件 I/O