The three most commonly used input and output functions in C scanf (), printf (), GetChar (), and Putchar ()

Source: Internet
Author: User
Tags modifier

This article introduces the three most commonly used input and output functions scanf (), printf (), GetChar () and Putchar () in C language.

One, scanf () function
The function of the format input function scanf () is to enter data from the keyboard, which is assigned to the appropriate input in the specified input format. The general format of the function is:
scanf ("Control string", enter a list of items);
Where the control string specifies the data input format, must be enclosed in double quotation marks, the content is composed of the format description and the ordinary character two parts. The entry list consists of one or more variable addresses, separated by a comma "," between the variable addresses when there are multiple address variables.
scanf () variables to add the address operator, is the variable name before adding "&", which is easy for beginners to ignore a problem. Be aware that the input type is consistent with the variable type.
The following is a discussion of the two components of a control string: Format description and normal character.
1. Format description
The format specification specifies the type of data format in which the variables in the input are entered, in the form:
% [< modifier >] < format word >
Each format character and its meaning are shown in table 3-1.




Second, printf () function
Corresponding to the formatted input function scanf () is the formatted output function printf (), whose function is to output the output items listed in the output item list to the default output device (typically the display) in the format specified by the control string, with the following basic format:
printf ("Control string", output Item list)
An output item can be a constant, a variable, an expression, whose type and number must be the type of the format character in the control string,
The number is the same, and when there are multiple output items, the items are separated by commas.
The control string must be enclosed in double quotation marks, consisting of a format description and two parts of the normal character.
1. Format description
The General format is:
%[< modifier >]< Format character >
The format character specifies the output format of the corresponding output item, and the commonly used format characters are shown in table 3-2.

As you can see, when the specified field width is less than the actual width of the data, the number is rounded by the actual field width output of the number, to the floating-point numbers, and the corresponding decimal digits. For example: 12.34567 press%5.2f output, output 12.35. If the field width is less than or equal to the width of the integer portion of the floating-point number, the floating-point number is output by the actual digits, but the scale is still subject to the value given by the width modifier. As above 12.34567 press%2.1f output, the result is: 12.3.
In practical applications, there is also a more flexible field-width control method, with the value of a constant or variable as the output field width, the method is a "*" as a modifier, inserted after the%.
For example: i=123;
printf ("%*d", 5,i);

long int b;
short int C;
unsigned int D;
Char e;
float F;
Double g;
a=1023;
b=2222;
c=123;
d=1234;
e= ' x ';
f=3.1415926535898;
g=3.1415926535898;
printf ("a=%d\n", a);
printf ("a=%0\n", a);
printf ("a=%x\n", a);
printf ("b=%ld\n", b);
printf ("c=%d\n", c);
printf ("d=%u\n", D);
printf ("e=%c\n", e);
printf ("f=%f\n", f);
printf ("g=%f\n", g);
printf ("\ n");
}
Execute the program with the output as:
run¿
a=1023
a=1777
A=3ff
b=2222
C=123
d=1234
E=x
f=3.141593
g=3.141593

Three, GetChar () function and Putchar () function
Putchar () and GetChar () are functions that input and output a single character.
The function of GetChar () is to return a character of the keyboard input with no parameters, usually in the following format:
Ch=getchar ()
CH is a character variable, the above statement receives a character entered from the keyboard and assigns it to Ch.
The function of Putchar () is to output a character to the screen, which is functionally equivalent to%c in the printf function. Putchar () must
An output item is required, and the output item can be a character constant, a variable, an expression, but only a single character and not a string.
[Example 3-3] Enter a character, echo the character, and output its ASCII code value.
#include <stdio.h>
Main ()
{
Charch;
Ch=getchar ();
Putchar (CH);
printf ("%d\n", ch);
}
To run the program:
run¿
G
g103
It is important to note that the character input and output function is defined in the header file stdio.h, so when the program uses Putchar () or
GetChar () function, the phrase must precede main ():
# include "Stdio.h"
Include the stdio.h in.
3.2.4 Program Application Example
[Example 3-4] The following program is an example of a complex number addition.
#include <stdio.h>
Main ()
{
FLOATA1,B1,A2,B2;
Charch;
printf ("\t\t\tcomplexsaddition\n");
printf ("pleaseinputthefirstcomplex:\n");
printf ("\trealpart:");
scanf ("%f", &AMP;A1);
printf ("\tvirtualpart:");
scanf ("%f", &AMP;B1);
printf ("%5.2f+i%5.2f\n", A1,B1);
printf ("\npleaseinputthesecondcomplex:\n");
printf ("\trealpart:");
scanf ("%f", &AMP;A2);
printf ("\tvirtualpart:");
scanf ("%f", &AMP;B2);
printf ("%5.2f+i%5.2f\n", A2,B2);
printf ("\ntheadditionis:");
printf ("%6.3f+i%6.3f\n", A1+A2,B1+B2);
printf ("Programnormalterminated,pressenter ...");
Ch=getchar ();
Ch=getchar ();
}
The results of the operation are as follows:
run¿
Complexsaddition
Pleaseinputthefirstcomplex:
realpart:1.2
virtualpart:3.4
1.20+i3.40
Pleaseinputthesecondcomplex:
realpart:5.6
virtualpart:7.8
5.60+i7.80
theadditionis:6.800+i11.200
Programnormalterminated,pressenter ....

The three most commonly used input and output functions in C scanf (), printf (), GetChar (), and Putchar ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.