C language input and output

First, data input

The main purpose is to write a program to process the data. Data come from? Sources of data There are many ways, such as reading data from a file on disk, read data from the database, fetch data from a Web page and so on, there is a primitive way data is entered from the keyboard.

In the C language, there are three functions available user input from the keyboard.

getchar: enter a single character, save the character variable.

gets: input line data, stored into a string variable.

scanf: formatted input function, a plurality of data can be entered, stored in the plurality of variables.

Second, a data output

In the C language, there are three functions can output data to the screen.

putchar: output a single character.

puts: output string.

printf: formatted output function, the output may be constant, variable like.

Third, out of context

Thirty years ago, the personal computer is not popular, mainly written in C language software, including user interface, data input and output functions are important. Now, data input and display pages to complete the APP and software, C programs are mainly used for network communications and data processing, the program running in the background, without user interface. So, getchar, gets, scanf these functions almost do not.

It is a beginner, the need for teaching, using scanf data input from the keyboard for function demo program, so we just need to learn simple to use scanf function, getchar and gets functions without concern.

Input function C language is not important, but the output is still very important function.

putchar and puts function useless, has been forgotten, then forget it.

printf function is very important, since we will be in-depth study, this chapter discusses only its simple usage.

Now, only the scanf and printf function.

Four, printf output

printf function is a function of the output format for the data output to the screen.

Printf function calls the method is:

printf(格式化字符串,参数列表);

1, the output of descriptive text

The output of text comprising up in double quotation marks, the text \ n represents a new line, a plurality of \ n can change multiple lines.

printf("我心匪席,不可卷也,我心匪石,不可转也。\n");

The above code will output the text on the screen:

我心匪席,不可卷也,我心匪石,不可转也。

After the input text, and then outputs a newline.

2, the output integer

Output integer constant or variable represented by% d, the integer constant or variable listed in the parameter to be output.

printf("我年龄是%d岁。\n",18);
int age=18;
printf("我年龄是%d岁。\n",age);

3, the output character

Constant or variable output character is represented by% c, listed character to be constant or variable in the output parameter.

printf("我姓别是:%c。\n",'x');        // 姓别:x-男;y-女
char xb='x';
printf("我姓别是:%c。\n",xb);

4, the output floats

Floating-point constant or variable output is represented by% lf, listed float constant or variable in the parameter to be output.

printf("我体重是%lf公斤。\n",62.5);
double weight=62.5;
printf("我体重是%lf公斤。\n", weight);

5, the output string

Constant or variable output string expressed in% s, listed in the string to be constant or variable in the output parameter.

printf("我的姓名是%s。\n","西施");
char name[21];
memset(name,0,sizeof(name));
strcpy(name, "西施");
printf("我的姓名是%s。\n",name);

6, a plurality of content output

One call printf function can output multiple variables or constants.

int age=18;
char xb='x';
double weight=62.5;
char name[21];
memset(name,0,sizeof(name));
strcpy(name, "西施");
printf("我的姓名是:%s,姓别:%c,年龄:%d岁,体重%lf公斤。\n",name,xb,age,weight);

Format later noted that the first parameter, the printf function (format string) parameter list (constant or variable) to one correspondence, a carrot filling into a pit, no more, no less, the order can not be wrong, or will cause unexpected results.

7, an example (book12.c)

/*
 *  程序名:book12.c,此程序演示printf函数输出变量的内容
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

int main()
{
  int age=18;
  char xb='y';
  double weight=45.5;

  char name[21];
  memset(name,0,sizeof(name));
  strcpy(name,"西施");

  printf("我的姓名是:%s,姓别:%c,年龄:%d岁,体重%f公斤。\n",name,xb,age,weight);

  return 0;
}

running result

Here Insert Picture Description

Five, scanf input

function is a function scanf formatted input for accepting data input from the keyboard, the user input data is completed, press the Enter key (the Enter) input ends.

Call the method scanf function is:

scanf(格式化字符串,参数列表);

Be careful not to scanf format string in the final plus \ n.

1, enter an integer

Entering an integer format represented by% d, integer variable names are listed in the argument for the input data storage.

Before the input data, to prompt the user typically first output word. The same below.

int age=0;
printf("请输入您的年龄:");   // 提示文字不要换行,让用户在后面输入,下同。
scanf("%d",&age);     // 在变量名前要加符号&,先不要问原因,以后再介绍。

2, an input character

The input character format with% c, character variable names are listed in the argument for the input data storage.

char xb=0;
printf("请输入您姓别:"); 
scanf("%c",&xb);     // 在变量名前要加符号&

3, enter a float

Floating point format with input% lf, variable names listed in the float parameters, the input data for storage.

double weight=62.5;
printf("请输入您体重:");  
scanf("%lf",&weight);     // 在变量名前要加符号&。

4, the input string

Input string format with% s, listed in the parameter string variable name, input data for storage.

char name[21];
memset(name,0,sizeof(name));
printf("请输入您姓名:"); 
scanf("%s",name);     // 注意了,字符串变量名前可以不加符号&,不要问原因,以后再介绍。

5, a plurality of input content

Scanf function is called once more data can be entered, press the Enter key after the completion of all of the data input end of the input.

int age=0;
char xb=0;
double weight=0;
char name[21];
memset(name,0,sizeof(name));
printf("请输入您的姓名、姓别(x-男;y-女),年龄和体重,中间用空格分隔:");
scanf("%s %c %d %lf",name,&xb,&age,&weight);   // 只有name变量前没加&。

Format and back of the first argument, scanf (format string) parameter list (list of variables) to one correspondence, a carrot a pit filled in, no more, no less, the order can not be wrong, otherwise they will produce unexpected results.

6, Example (book13.c)

/*
 *  程序名:book13.c,此程序演示scanf函数输入数据。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

int main()
{
  int age=0;
  char xb=0;
  double weight=0;

  char name[21];
  memset(name,0,sizeof(name));

  printf("请输入您的姓名、姓别(x-男;y-女),年龄和体重,中间用空格分隔:");
  scanf("%s %c %d %lf",name,&xb,&age,&weight); // 只有name变量前没加&,其它的都加了。

  printf("您的姓名是:%s,姓别:%c,年龄:%d岁,体重%lf公斤。\n",name,xb,age,weight);

  return 0;
}

running result

Here Insert Picture Description

VI Notes

1) This section only to learn scanf and printf function simplest usage.

2) scanf function was used only in the basics of learning C program, basically not in the actual development.

3) printf is not a function, is a series of functions, powerful, a lot of changes, widely used, is extremely important, in the future we will study it in depth.

4) variable scanf function list, in addition to strings, the other variables will be added in front of &, represents taking the address, if not, the program will not compile error, but the unintended consequences (1 occurs when an assignment is not successful run ; 2- mistakes Core dump).

Variable 5) printf function list, the variable name does not need to add &.

Seven, after-school job

Writing sample program from the keyboard to enter the name of your favorite female (male) god, sex, age, height (cm), weight (kg), and your favorite of her body parts, the input data is stored in a variable, then the value of the variable on the screen is displayed.

Claim:

1) storing a string variable name;

2) Sex storage (x- male, y- female) with a character variable;

3) age and height is stored as an integer variable;

4) Weight stored as a floating point variable;

5) body parts stored as a string.

Eight, copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Guess you like

Origin www.cnblogs.com/wucongzhou/p/12499349.html