C语言printf和scanf,求大神!

jkjkjkjjjkjk 2014-06-30 04:17:07
谁能给讲讲printf 和scanf 的数据输出和输入
比如 scanf(“%d”,&a); printf("%df",a);输入1
【计算机按整形类型读入数据,然后在以a为名的4个字节的内存单元中以二进制存入数据,然后printf 从内存读取数据,按整形输出到屏幕】。。。。是否可以这样理解?师否可以分的更细?输出到屏幕的1 是什么?是字符吗?
主要是看到sprintf函数 ,如下
char first[MAX];
char last[MAX];
char formal[2 * MAX + 10];
float prize;
puts("Enter first name");
gets(first);
puts("enter last name");
gets(last);
puts("enter prize");
scanf("%f", &prize);
sprintf(formal, "%s %-19s:$%6.2f\n", last, first, prize);//数据类型不一致,prize和formal
puts(formal);
为什么可以将实行数据存入 字符型数组????
不太理解了。
...全文
110 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-06-30
  • 打赏
  • 举报
回复
查MSDN是Windows程序员必须掌握的技能之一。 英语也是一门计算机语言的说。 scanf Type Field Characters The type character is the only required format field; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. Table R.8 Type Characters for scanf functions Character Type of Input Expected Type of Argument c When used with scanf functions, specifies single-byte character; when used with wscanf functions, specifies wide character. White-space characters that are ordinarily skipped are read when c is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws. Pointer to char when used with scanf functions, pointer to wchar_t when used with wscanf functions. C When used with scanf functions, specifies wide character; when used with wscanf functions, specifies single-byte character. White-space characters that are ordinarily skipped are read when C is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws. Pointer to wchar_t when used with scanf functions, pointer to char when used with wscanf functions. d Decimal integer. Pointer to int. i Decimal, hexadecimal, or octal integer. Pointer to int. o Octal integer. Pointer to int. u Unsigned decimal integer. Pointer to unsigned int. x Hexadecimal integer. Pointer to int. e, E, f, g, G Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent (“e” or “E”) followed by an optionally signed integer value. Pointer to float. n No input read from stream or buffer. Pointer to int, into which is stored number of characters successfully read from stream or buffer up to that point in current call to scanf functions or wscanf functions. s String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed following Table R.7. When used with scanf functions, signifies single-byte character array; when used with wscanf functions, signifies wide-character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended. S String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed preceding this table. When used with scanf functions, signifies wide-character array; when used with wscanf functions, signifies single-byte–character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended. The types c, C, s, and S are Microsoft extensions and are not ANSI-compatible. Thus, to read single-byte or wide characters with scanf functions and wscanf functions, use format specifiers as follows. To Read Character As Use This Function With These Format Specifiers single byte scanf functions c, hc, or hC single byte wscanf functions C, hc, or hC wide wscanf functions c, lc, or lC wide scanf functions C, lc, or lC To scan strings with scanf functions, and wscanf functions, use the prefixes h and l analogously with format type-specifiers s and S.
jkjkjkjjjkjk 2014-06-30
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
printf Type Field Characters The type character is the only required format field ; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of c and s with printf functions, are Microsoft extensions and are not ANSI-compatible. Table R.3 printf Type Field Characters Character Type Output Format c int or wint_t When used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character. C int or wint_t When used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character. d int Signed decimal integer. i int Signed decimal integer. o int Unsigned octal integer. u int Unsigned decimal integer. x int Unsigned hexadecimal integer, using “abcdef.” X int Unsigned hexadecimal integer, using “ABCDEF.” e double Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –. E double Identical to the e format except that E rather than e introduces the exponent. f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). n Pointer to integer Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. p Pointer to void Prints the address pointed to by the argument in the form xxxx:yyyy where xxxx is the segment and yyyy is the offset, and the digits x and y are uppercase hexadecimal digits. s String When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached. S String When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.
大神,我是小菜,看不懂,能通俗的讲讲么
赵4老师 2014-06-30
  • 打赏
  • 举报
回复
printf Type Field Characters The type character is the only required format field ; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of c and s with printf functions, are Microsoft extensions and are not ANSI-compatible. Table R.3 printf Type Field Characters Character Type Output Format c int or wint_t When used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character. C int or wint_t When used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character. d int Signed decimal integer. i int Signed decimal integer. o int Unsigned octal integer. u int Unsigned decimal integer. x int Unsigned hexadecimal integer, using “abcdef.” X int Unsigned hexadecimal integer, using “ABCDEF.” e double Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –. E double Identical to the e format except that E rather than e introduces the exponent. f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). n Pointer to integer Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. p Pointer to void Prints the address pointed to by the argument in the form xxxx:yyyy where xxxx is the segment and yyyy is the offset, and the digits x and y are uppercase hexadecimal digits. s String When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached. S String When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.
赵4老师 2014-06-30
  • 打赏
  • 举报
回复
Format Specification Fields: scanf and wscanf Functions A format specification has the following form: %
  • [width] [{h | l | I64 | L}]type The format argument specifies the interpretation of the input and can contain one or more of the following: White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input. Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates. Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list. The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read. When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string. An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification. Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%. An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.
赵4老师 2014-06-30
  • 打赏
  • 举报
回复
Format Specification Fields: printf and wprintf Functions A format specification, which consists of optional and required fields, has the following form: %[flags] [width] [.precision] [{h | l | I64 | L}]type Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%. The optional fields, which appear before the type character, control other aspects of the formatting, as follows: type Required character that determines whether the associated argument is interpreted as a character, a string, or a number (see Table R.3). flags Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see Table R.4). More than one flag can appear in a format specification. width Optional number that specifies the minimum number of characters output. (See printf Width Specification.) precision Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values (see Table R.5). h | l | I64 | L Optional prefixes to type-that specify the size of argument (see Table R.6).

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧