c语言输入姓名输出姓和名_C输入和输出

c语言输入姓名输出姓和名

Input means to provide the program with some data to be used in the program and Output means to display data on screen or write the data to a printer or a file.

输入装置为程序提供一些要在程序中使用的数据, 输出装置为在屏幕上显示数据或将数据写入打印机或文件。

C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result.

C编程语言提供了许多内置函数,可以读取任何给定的输入并在需要输出结果时在屏幕上显示数据。

In this tutorial, we will learn about such functions, which can be used in our program to take input from user and to output the result on screen.

在本教程中,我们将学习这些功能,这些功能可在我们的程序中用于从用户那里获取输入并在屏幕上输出结果。

All these built-in functions are present in C header files, we will also specify the name of header files in which a particular function is defined while discussing about it.

所有这些内置函数都存在于C头文件中,我们还将在讨论该文件时指定在其中定义了特定功能的头文件的名称。

scanf()printf()函数 (scanf() and printf() functions)

The standard input-output header file, named stdio.h contains the definition of the functions printf() and scanf(), which are used to display output on screen and to take input from user respectively.

标准输入输出头文件stdio.h包含函数printf()scanf()的定义,这些函数用于在屏幕上显示输出并分别从用户处获取输入。

#include<stdio.h>

void main()
{
    // defining a variable
    int i;
    /* 
        displaying message on the screen
        asking the user to input a value
    */
    printf("Please enter a value...");
    /*
        reading the value entered by the user
    */
    scanf("%d", &i);
    /*
        displaying the number as output
    */
    printf( "\nYou entered: %d", i);
}

When you will compile the above code, it will ask you to enter a value. When you will enter the value, it will display the value you have entered on screen.

当您编译上面的代码时,它将要求您输入一个值。 输入值时,它将在屏幕上显示您输入的值。

You must be wondering what is the purpose of %d inside the scanf() or printf() functions. It is known as format string and this informs the scanf() function, what type of input to expect and in printf() it is used to give a heads up to the compiler, what type of output to expect.

您一定想知道在scanf()printf()函数中%d的目的是什么。 它称为格式字符串 ,它通知scanf()函数,期望输入的类型,以及在printf()中用于提示编译器,期望输出的类型。

Format StringMeaning
%dScan or print an integer as signed decimal number
%fScan or print a floating point number
%cTo scan or print a character
%sTo scan or print a character string. The scanning ends at whitespace.
格式字符串 含义
%d 扫描或打印一个整数作为带符号的十进制数字
%f 扫描或打印浮点数
%c 扫描或打印字符
%s 扫描或打印字符串。 扫描在空白处结束。

We can also limit the number of digits or characters that can be input or output, by adding a number with the format string specifier, like "%1d" or "%3s", the first one means a single numeric digit and the second one means 3 characters, hence if you try to input 42, while scanf() has "%1d", it will take only 4 as input. Same is the case for output.

通过添加带有格式字符串说明符的数字,例如"%1d""%3s" ,我们还可以限制可以输入或输出的数字或字符的数量,第一个表示单个数字,第二个表示单个数字。表示3个字符,因此,如果您尝试输入42 ,而scanf()具有"%1d" ,则仅需输入4字符。 输出也是一样。

In C Language, computer monitor, printer etc output devices are treated as files and the same process is followed to write output to these devices as would have been followed to write the output to a file.

在C语言中,计算机监视器,打印机等输出设备被视为文件,并且遵循与将输出写入文件相同的过程将输出写入这些设备。

NOTE : printf() function returns the number of characters printed by it, and scanf() returns the number of characters read by it.

注意: printf()函数返回其打印的字符数, scanf()返回其读取的字符数。

int i = printf("studytonight");

In this program printf("studytonight"); will return 12 as result, which will be stored in the variable i, because studytonight has 12 characters.

在这个程序中printf("studytonight"); 将返回结果12 ,该结果将存储在变量i ,因为studytonight具有12个字符。

getchar()putchar()函数 (getchar() & putchar() functions)

The getchar() function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. You can use this method in a loop in case you want to read more than one character. The putchar() function displays the character passed to it on the screen and returns the same character. This function too displays only a single character at a time. In case you want to display more than one characters, use putchar() method in a loop.

getchar()函数从终端读取一个字符,并将其作为整数返回。 此函数一次仅读取单个字符。 如果您想读取多个字符,则可以在循环中使用此方法。 putchar()函数在屏幕上显示传递给它的字符,并返回相同的字符。 此功能一次也只显示一个字符。 如果要显示多个字符,请循环使用putchar()方法。

#include <stdio.h>

void main( )
{
    int c;
    printf("Enter a character");
    /*
        Take a character as input and 
        store it in variable c
    */
    c = getchar();
    /*
        display the character stored 
        in variable c 
    */
    putchar(c);
}

When you will compile the above code, it will ask you to enter a value. When you will enter the value, it will display the value you have entered.

当您编译上面的代码时,它将要求您输入一个值。 当您输入值时,它将显示您输入的值。

gets()puts()函数 (gets() & puts() functions)

The gets() function reads a line from stdin(standard input) into the buffer pointed to by str pointer, until either a terminating newline or EOF (end of file) occurs. The puts() function writes the string str and a trailing newline to stdout.

gets()函数从stdin (标准输入)读取一行到str 指针指向的缓冲区中,直到出现换行符终止或EOF(文件结尾)为止。 puts()函数将字符串str和尾随换行符写入stdout

str → This is the pointer to an array of chars where the C string is stored. (Ignore if you are not able to understand this now.)

str →这是指向存储C字符串的chars数组的指针。 (如果您现在无法理解,请忽略。)

#include<stdio.h>

void main()
{
    /* character array of length 100 */
    char str[100];
    printf("Enter a string");
    gets( str );
    puts( str );
    getch();
}

When you will compile the above code, it will ask you to enter a string. When you will enter the string, it will display the value you have entered.

当您编译上面的代码时,它将要求您输入一个字符串。 当您输入字符串时,它将显示您输入的值。

scanf()gets()之间的区别 (Difference between scanf() and gets())

The main difference between these two functions is that scanf() stops reading characters when it encounters a space, but gets() reads space as character too.

这两个函数的主要区别在于, scanf()遇到空格时会停止读取字符,但是gets()也会将空格作为字符读取。

If you enter name as Study Tonight using scanf() it will only read and store Study and will leave the part after space. But gets() function will read it completely.

如果使用scanf()输入名称为Study Study Tonight ,它将仅读取和存储Study,并且在空格后保留该部分。 但是gets()函数将完全读取它。

翻译自: https://www.studytonight.com/c/c-input-output-function.php

c语言输入姓名输出姓和名

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值