C language scanf input format printf output format

Source: Internet
Author: User
Tags function prototype


For students who are new to C, they may encounter this problem.

Because bloggers are also novices, often because they don't know about single-precision double-precision and input and output formats.

This question is also Baidu many times, so here is a summary to share with you

printf output format

1. Conversion specifier
      % a (% A) floating-point number, hexadecimal number, and p- (P-) notation (C99)% c character
      % d signed decimal integer
      % f floating-point number (including float and doulbe)% e (% E) floating-point exponential output [e- (E-) notation]% g (% G) floating-point number does not show meaningless zero "0"
      % i signed decimal integer (same as% d)% u unsigned decimal integer
      % o Octal integer e.g. 0123
      % x (% X) hexadecimal integer 0f (0F) e.g. 0x1234% p pointer% s string %% "%"
2. Sign
      Align left: "-" e.g. "% -20s" Align right: "+" e.g. "% + 20s"
      Space: If the symbol is positive, it will display a space, if it is negative, it will display "-" eg "% 6.2f". Class, prefixed with 0x in the output;
           For e, g, f classes, the decimal point is given only when the result has a decimal.
3. Format string (format)
      [Sign] [Minimum output width] [. Accuracy] [length] type "% -md": Left-justified, if m is less than the actual, it is output according to the actual.
     "% m.ns": output m bits, take n characters from the string (from the left), pad with spaces to the left, and m = n when n> m or m is omitted
      e.g. "% 7.2s" Enter CHINA

                               Output "CH" "% m.nf": output floating-point number, m is width, n is the right digit of decimal point

      e.g. "% 3.1f" Enter 3852.99
                              Output 3853.0 Length: h short shaping amount, l long shaping amount
The full format of printf's format control:%-0 m.n l or h format characters
The items that make up the format description are explained below:
①%: indicates the start symbol of the format description, which is indispensable.
②-: Yes-indicates left-justified output. If omitted, it indicates right-justified output. ③0: If there is 0, it means fill the designated space with 0. If it is omitted, it means the designated space is not filled.
④ m.n: m refers to the field width, that is, the number of characters occupied by the corresponding output item on the output device. N refers to accuracy. The number of decimal places used to describe the real type of the output. When n is specified, the implied precision is n = 6 digits.
⑤ l or h: l refers to long type for integer type and double type for real type. h is used to modify integer format characters to short.



The scanf function is called a format input function, that is, inputting data from the keyboard into a specified variable in a format specified by the user.

The general form of the scanf function
The scanf function is a standard library function, and its function prototype is in the header file "stdio.h". Like the printf function, the C language also allows you not to include the stdio.h file before using the scanf function. The general form of the scanf function is:
scanf ("format control string", address table column);
Among them, the format control string has the same function as the printf function, but it cannot display non-format strings, that is, it cannot display prompt strings. The address table column gives the address of each variable. The address is composed of the address operator "&" followed by the variable name.

For example: & a, & b respectively represent the addresses of variables a and b.

This address is the address that the compilation system allocates to the a and b variables in memory. In C, the concept of address is used, which is different from other languages. A distinction should be made between the two concepts of variable value and variable address. The address of the variable is allocated by the C compiler system, and the user does not need to care about the specific address.

Format string The general form of a format string is:
    % [*] [Input data width] [length] type
    Items with square brackets [] are optional. The meaning of each item is as follows.

1) Type
Indicates the type of input data. The format characters and meanings are shown in the following table.

Format Character Meaning

d Enter a decimal integer

o Enter octal integer

x Enter a hexadecimal integer

u Enter an unsigned decimal integer

f or e for real numbers (in decimal or exponential form)

c Enter a single character

s input string



2) "*" sign
Used to indicate the input item, the corresponding variable is not assigned after reading, that is, the input value is skipped. Such as:


scanf ("% d% * d% d", & a, & b);
scanf ("% d% * d% d", & a, & b);
When the input is: 1 2 3, 1 is assigned to a, 2 is skipped, and 3 is assigned to b.

3) width
Specify the width (ie number of characters) of the input as a decimal integer. E.g:


scanf ("% 5d", & a);
scanf ("% 5d", & a);


Entering 12345678 only assigns 12345 to the variable a, and the rest is truncated.

Another example:

scanf ("% 4d% 4d", & a, & b);
scanf ("% 4d% 4d", & a, & b);


Entering 12345678 will assign 1234 to a and 5678 to b.
4) Length
The length format characters are l and h, where l indicates input long integer data (such as% ld) and double-precision floating-point numbers (such as% lf). h represents input short integer data.

The scanf function must also pay attention to the following points:
There is no precision control in scanf function,

For example: scanf ("% 5.2f", & a); is illegal. You cannot use this statement to enter a real number with two decimal places.

The address of the variable is required in scanf, and an error will occur if the variable name is given.

If scanf ("% d", a); is illegal, it should be changed to scnaf ("% d", & a); to be legal.

When entering multiple numeric data, if there are no non-format characters in the format control string as the interval between input data, spaces, TAB or carriage return can be used as the interval. C compiler considers the data to be over when it encounters a space, TAB, carriage return, or illegal data (for example, when "12A" is entered for "% d", A is illegal data).

When entering character data, if there are no unformatted characters in the format control string, all the characters entered are considered valid characters.

E.g:

scanf ("% c% c% c", & a, & b, & c);
scanf ("% c% c% c", & a, & b, & c);
Enter d, e, f to ‘d’ to a, ‘‘ to b, and ‘e’ to c. Only when the input is def can ‘d’ be assigned to a, ‘e’ be assigned to b, and ‘f’ be assigned to c.

If you add spaces as spaces in the format control, such as:


scanf ("% c% c% c", & a, & b, & c);
scanf ("% c% c% c", & a, & b, & c);
You can add spaces between the data when entering.

example;


#include <stdio.h>
intmain (void) {
chara, b;
printf ("input character a, b \ n");
scanf ("% c% c", & a, & b);
printf ("% c% c \ n", a, b);
return0;
}
#include <stdio.h>
int main (void)
{
    char a, b;
    printf ("input character a, b \ n");
    scanf ("% c% c", & a, & b);
    printf ("% c% c \ n", a, b);
    return 0;
}
Since there are no spaces in the scanf function "% c% c", if you enter M N, the output is only M When the input is changed to MN, two characters of MN can be output.

example


#include <stdio.h>
intmain (void) {
chara, b;
printf ("input character a, b \ n");
scanf ("% c% c", & a, & b);
printf ("\ n% c% c \ n", a, b);
return0;
}
#include <stdio.h>
int main (void)
{
    char a, b;
    printf ("input character a, b \ n");
    scanf ("% c% c", & a, & b);
    printf ("\ n% c% c \ n", a, b);
    return 0;
}
This example indicates that when there is a space between the scanf format control string "% c% c", there can be a space between the input data.

5) If there is an unformatted character in the format control string, the unformatted character should also be input when entering.
E.g:


scanf ("% d,% d,% d", & a, & b, & c);
scanf ("% d,% d,% d", & a, & b, & c);
Among them, the non-format characters "," are used as interval characters, so the input should be: 5,6,7. Another example:


scanf ("a =% d, b =% d, c =% d", & a, & b, & c);
scanf ("a =% d, b =% d, c =% d", & a, & b, & c);
The input should be: a = 5, b = 6, c = 7.

6) If the input data is not the same as the output type, although the compilation can pass, the result will be incorrect.

example


#include <stdio.h>
intmain (void) {
inta;
printf ("input a number \ n");
scanf ("% d", & a);
printf ("% ld", a);
return0;
}
#include <stdio.h>
int main (void)
{
    int a;
    printf ("input a number \ n");
    scanf ("% d", & a);
    printf ("% ld", a);
    return 0;
}
Because the input data type is integer, and the format string of the output statement is described as a long integer, the output result does not match the input data. If the procedure is changed as follows

example


#include <stdio.h>
intmain (void) {
longa;
printf ("input a long integer \ n");
scanf ("% ld", & a);
printf ("% ld", a);
return0;
}
#include <stdio.h>
int main (void)
{
    long a;
    printf ("input a long integer \ n");
    scanf ("% ld", & a);
    printf ("% ld", a);
    return 0;
}
The result is:
input a long integer
1234567890
1234567890

When the input data is changed to a long integer, the input and output data are equal.
example

#include <stdio.h>
intmain (void) {
chara, b, c;
printf ("input character a, b, c \ n");
scanf ("% c% c% c", & a, & b, & c);
printf ("% d,% d,% d \ n% c,% c,% c \ n", a, b, c, a-32, b-32, c-32);
return0;
}
#include <stdio.h>
int main (void)
{
    char a, b, c;
    printf ("input character a, b, c \ n");
    scanf ("% c% c% c", & a, & b, & c);
    printf ("% d,% d,% d \ n% c,% c,% c \ n", a, b, c, a-32, b-32, c-32);
    return 0;
}
Enter three lowercase letters and output their ASCII code and corresponding uppercase letters.

example

#include <stdio.h>
intmain (void) {
inta;
longb;
floatf;
doubled;
charc;
printf ("\ nint:% d \ nlong:% d \ nfloat:% d \ ndouble:% d \ nchar:% d \ n", sizeof (a), sizeof (b), sizeof (f), sizeof (d), sizeof (c));
return0;
}
#include <stdio.h>
int main (void)
{
     int a;
     long b;
     float f;
     double d;
     char c;
     printf ("\ nint:% d \ nlong:% d \ nfloat:% d \ ndouble:% d \ nchar:% d \ n", sizeof (a), sizeof (b), sizeof (f), sizeof (d ), sizeof (c));
     return 0;
}
Output the byte length of various data types.





This article comes from "10910765" blog, please keep this source http://10920765.blog.51cto.com/10910765/1733488

C language scanf input format printf output format

Related Article

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.