c语⾔输⼊姓名输出姓和名_C输⼊和输出
c语⾔输⼊姓名输出姓和名
Output means to display data on
Input
Input means to provide the program with some data to be used in the program and Output
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 ");
/*
reading the value entered by the user
reference什么意思中文
jssplice删除数组
*/
scanf("%d", &i);
/*
displaying the number as output
*/
putchar函数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.
当您编译上⾯的代码时,它将要求您输⼊⼀个值。 输⼊值时,它将在屏幕上显⽰您输⼊的值。
format string and You must be wondering what is the purpose of %d inside the scanf() or printf() functions. It is known as format string
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 String Meaning
%d Scan or print an integer as signed decimal number
%f Scan or print a floating point number
%c To scan or print a character
java pojo%s To scan or print a character string. The scanning ends at whitespace.
格式字符串含义
%d扫描或打印⼀个整数作为带符号的⼗进制数字
%f扫描或打印浮点数
%c扫描或打印字符
%s扫描或打印字符串。 扫描在空⽩处结束。
limit the number of digits or characters that can be input or output, by adding a number with the format We can also limit the number of digits or characters
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 NOTE :
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 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)
stdin(standard input) into the buffer pointed to by str , until either a terminating
The gets() function reads a line from stdin
stdout.
newline or EOF (end of file) occurs. The puts() function writes the string str and a trailing newline to stdout
stdin (标准输⼊)读取⼀⾏到str 的缓冲区中,直到出现换⾏符终⽌或EOF(⽂件结尾)为⽌。 puts()函数将字符串str和尾随换⾏gets()函数从stdin
stdout 。
符写⼊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 Study Tonight using scanf() it will only read and store Study Study and will leave the part after space. But gets() function will read it completely.
如果使⽤scanf()输⼊名称为Study Study Tonight Study Study Tonight ,它将仅读取和存储Study,Study,并且在空格后保留该部分。 但是gets()函数将完全读取它。
c语⾔输⼊姓名输出姓和名

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。