首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

格式化字符串,该怎么处理

2013-10-24 
格式化字符串什么是格式话字符串?为什么要格式化?有什么好处吗?一般都用在什么地方?像C语言的printf(),sca

格式化字符串
什么是格式话字符串?为什么要格式化?有什么好处吗?一般都用在什么地方?像C语言的printf(),scanf()这些格式化输出函数起什么作用?谢谢各位高手解疑答惑! 格式化字符串 C语言
[解决办法]
简单说,你定义了一个整型变量i用来计算一个东西,最后需要打印一句话,比如一共有i个这样的东西,你用printf怎么输出呢?你不能这样printf("一共有"+i+"个");这里就需要把整型变量i格式化到输出字符串中,就需要这样printf("一共有%d个",i);
[解决办法]
本来以为你说的是字符串的初始化,但看了1楼的回答感觉好像我错了
lz可能想知道printf(“%d”,i)里面这个“%d”是什么意思对么
这个“%d”是告诉printf按照什么格式去打印这个i
比如,%c就是按字符打印,%d就是按整形打印,%s就是按字符串打印
除了%d关键字以外的字符就照样输出
[解决办法]
因为计算机中的变量均是以二进制存储,而变量类型也只是规定了它们有无符号以及占多少字节,并没有规定他们输出是什么形式。。另外在具体问题中,使用者需要更具需要进行特定形式的输入输出,所以就产生了格式化输入输出。
[解决办法]
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).


[解决办法]
 l 
[解决办法]
 I64 
[解决办法]
 L

Optional prefixes to type-that specify the size of argument (see Table R.6).

[解决办法]
这是定义一个统一的标准,这样你的变量才知道怎么输出。
printf()是打印输出到终端,scanf()是获取终端输入
[解决办法]
3楼+1

都是二进制,看你想解释成什么东西了。
%d是整数,%s是字符串,%c是字符,%x是。。。
[解决办法]
就是表示输出“字符串”的格式

热点排行