Input and output of C-language primary lectures

Source: Internet
Author: User
Tags function prototype integer printf

Data output Statement

This section describes statements that output data to a standard output device display. In C, all data input/output is done by the library function. So it's all a function statement. This section introduces the printf function and the Putchar function first. The printf function printf function is called the format output function, and the last letter F of the keyword is formatted. The function is to display the specified data to the monitor screen in the format specified by the user. We have used this function several times in the previous example.

First, the general form of printf function calls

The printf function is a standard library function whose function prototype is in the header file "Stdio.h". However, as a special case, it is not required to include the Stdio.h file before using the printf function. The general form of printf function calls is: printf ("Format control string", Output table column) where the format control string is used to specify the output format. A format control string can consist of both a format string and a unformatted string. The format string is a string that begins with a%, followed by a variety of format characters, to indicate the type, form, length, scale, and so on of the output data. If "%d" indicates a decimal integer output, "%ld" indicates a decimal long integer output, and "%c" indicates a character-type output, and so on. The discussion will be given later.

Non-formatted strings are printed as they are in the output, prompting in the display. Output table columns give individual output items, requiring that the format string and output items should correspond one by one in number and type.

void main()
{
int a=88,b=89;
printf("%d %d\n",a,b);
printf("%d,%d\n",a,b);
printf("%c,%c\n",a,b);
printf("a=%d,b=%d",a,b);
}
a<--8,b<--89
printf("%d %d\n",a,b);
printf("%d,%d\n",a,b);
printf("%c,%c\n",a,b);
printf("a=%d,b=%d",a,b);

In this example, the value of a,b is output four times, but the result of the output is not the same because the format control string is different. In the output Statement format control string for line four, a space (unformatted character) is added between the two-format string%d, so there is a space between the a,b values of the output. Line five the printf statement format Control string adds a comma of unformatted characters so that a comma is added between the a,b values of the output. The format string for line six requires that the A,B value be output by character type. Line seventh adds the unformatted string in order to prompt the output.

Second, format string

The general form of a format string in Turbo C is: [flag] [Output minimum width] [. precision] [length] type where the items in brackets [] are optional. The meaning of the items is described below:

1. The type type character is used to represent the type of output data, and its format character and meaning are shown in the following table:

Format character format character meaning of output type

D output signed integers in decimal form (positive not output symbols)

o Output unsigned integers in eight (no output prefix O)

x output unsigned integer in 16 (do not output prefix ox)

U output unsigned integers in decimal form

F output single, double real numbers in decimal form

E output single and double precision real numbers in exponential form

g to output single and double precision real numbers in shorter output widths of%f%e

C Output Single character

S output string

2. Logo

The symbol character is-, +, #, the space four kinds, its meaning is shown in the following table:

Flag format character flag meaning

-the result is left-aligned, the right blank

+ Output symbol (plus or minus) space output value is positive with a space, negative with minus sign

# has no effect on the C,s,d,u class; for class O, prefix the output. For the X class, prefix the output with 0x; give a decimal point to the E,g,f class when the result has a decimal number

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.