C语言中printf()后的括号里面的怎么写,表示啥?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言中printf()后的括号里面的怎么写,表示啥?相关的知识,希望对你有一定的参考价值。

编完一个程序后要输入printf(),来输出字符,那后面括号里面的东西怎么写有什么规律没?总不能靠硬记吧!还有可不可以顺便讲几个指令代表什么(比如while后面是跟循环条件)
谢谢

  int printf(const char *format,[argument]);
  format 参数输出的格式,定义格式为:
  %[flags][width][.perc] [F|N|h|l]type
  规定数据输出方式,具体如下:
  1.type 含义如下:
  d 有符号10进制整数
  i 有符号10进制整数
  o 有符号8进制整数
  u 无符号10进制整数
  X/x 有符号16进制整数
  F/f 浮点数
  E/e 用科学表示格式的浮点数
  g 使用%f和%e表示中的总的位数表示最短的来表示浮点数 G 同g格式,但表示为指数
  c 单个字符
  s 字符串
  % 显示百分号本身
  p 显示一个指针,near指针表示为:XXXX
  far 指针表示为:XXXX:YYYY
  n 相连参量应是一个指针,其中存放已写字符的个数
  2.flags 规定输出格式,取值和含义如下:
  无 右对齐,左边填充0和空格
  + 左对齐,右边填充空格
  - 在数字前增加符号 + 或 -
  一个空格 只对负数显示符号
  # 当type=c,s,d,i,u时没有影响
  type=o,x,X时,在数值前增加'0'字符
  type=e,E,f时,总是使用小数点
  type=g,G时,除了数值为0外总是显示小数点 3.width 用于控制显示数值的宽度,取值和含义如下n(n=1,2,3...) 宽度至少为n位,不够以空格填充
  0n(n=1,2,3...) 宽度至少为n位,不够左边以0填充 * 格
  式列表中,下一个参数还是width 4.prec 用于控制小数点后面的位数,取值和含义如下:
  无 按缺省精度显示
  0 当type=d,i,o,u,x时,没有影响
  type=e,E,f时,不显示小数点
  n(n=1,2,3...) 当type=e,E,f时表示的最大小数位数
  type=其他,表示显示的最大宽度 .*
  格式列表中,下一个参数还是width
  5.F|N|h|l 表示指针是否是远指针或整数是否是长整数
  F 远指针
  n 近指针
  h短整数或单精度浮点数
  l 长整数或双精度浮点数
  1.一般格式
  printf(格式控制,输出表列)
  例如:printf("i=%d,ch=%c\n",i,ch);
  说明:
  (1)“格式控制”是用双撇号括起来的字符串,也称“转换控制字符串”,它包括两种信息:
  ①格式说明:由“%”和格式字符组成,它的作用是将输出的数据转换为指定的格式输出。
  ②普通字符,即需要原样输出的字符。
  (2)“输出表列”是需要输出的一些数据,可以是表达式
  (3)printf函数的一般形式可以表示为
  printf(参数1,参数2,……,参数n)
  功能是将参数2~参数n按参数1给定的格式输出
  2.格式字符(9种)
  (1)d(或i)格式符。用来输出十进制整数,有以下几种用法:
  ①%d,按整型数据的实际长度输出。
  ②%md,m为指定的输出字段的宽度。如果数据的位数小于m,则左端补以空格,若大于m,则按实际位数输出。
  ③%ld(%mld 也可),输出长整型数据。
  例如:long a=123456;
  printf("%ld",a);
  (2)o格式符,以八进制数形式输出整数。格式:%o,%mo,%lo,%mlo都可。
  (3)x(或X)格式符,以十六进制数形式输出整数。格式:%x,%mx,%lx,%mlx都可。
  (4)u格式符,用来输出unsigned型数据,即无符号数,以十进制数形式输出。格式:%u,%mu,%lu都可。
  参见:li4-3.c/*无符号数据的输出*/
  (5)c格式符,用来输出一个字符。格式:%c,%mc都可。
  (6)s格式符,用来输出一个字符串。格式:%s,%ms,%-ms,%m.ns,%-m.ns都可。
  参见:li4-5.c /*字符串的输出*/
  (7)f格式符,用来输出实数(包括单、双精度),以小数形式输出。格式:%f,%m.nf,%-m.nf都可。
  注意:单精度实数的有效位数一般为7位,双精度为16位。
  参见:li4-6.c/*输出单精度实数时的有效位数*/
  li4-7.c/*输出双精度实数时的有效位数*/
  li4-8.c/*输出实数时指定小数位数*/
  (8)e(或E)格式符,以指数形式输出实数。格式:%e,%m.ne,%-m.ne都可。
  (9)g(或G)格式符,用来输出实数,它根据数值的大小,自动选f格式或e格式(选择输出时占宽度较小的一种)。
  3.说明
  (1)除了X、E、G(用大写字母表示)外,其他格式字符必须用小写字母;
  (2)“格式控制”字符串内可以包含转义字符;
  (3)如果想输出字符“%”,则应该在“格式控制”字符串中用连续两个%表示,如:
  printf("%f%%",1.0/3);
  (4)格式字符表参见下表
  表4.1 printf格式字符
  格式字符 说 明
  d,i 以带符号的十进制形式输出整数(正数不输出符号)
  o 以八进制无符号形式输出整数(不输出前导符0)
  x,X 以十六进制无符号形式输出整数(不输出前导符0x),用x则输出十六进制数的a~f时以小写形式输出,用X时,则以大写字母输出
  u 以无符号十进制形式输出整数
  c 以字符形式输出,只输出一个字符
  s 输出字符串
  f 以小数形式输出单、双精度数,隐含输出6位小数
  e,E 以指数形式输出实数
  g,G 选用%f或%e格式中输出宽度较短的一种格式,不输出无意义的0
  表4.2 printf的附加格式说明字符
  字符
  说明
  字母l
  用于长整型整数,可加在格式符d、o、x、u前面
  m(代表一个正整数)
  数据最小宽度
  n(代表一个正整数)
  对实数,表示输出n位小数;对字符串,表示截取的字符个数
  -
  输出的数字或字符在域内向左靠
参考技术A

    printf("任意文字或字符");

    方法一直接显示双引号内的全部字符,如printf("This is an apple");输出时便会是This is an apple

    printf("任意字符1 %d 任意字符2 %s",整型变量1,字符型变量2);

    方法二是在显示任意字符的基础上,再显示相应变量所代表的值或者字符,变量可以是其他类型,只要与前面的输出类型相互对应即可。如a=5;b=“on the table”;printf("There are %d apples %s",a,s);显示的即There are 5 apples on the table

    或者举个更简单的例子a=1;b=2;printf("a=%d,b=%d",a,b);显示的结果就是a=1,b=2

参考技术B printf([字符串][,变量])
字符串:如"aabb","aaa%defdk","dsf%feadi"
变量:a,b,d,3,5,6...
可以只有字符串无变量,但是只有变量没有字符串,那就好无意义了。
为什么?不知道你没有没看到在上面的三个字符串中后面两个字符串中都有一个%,重点就在这里
%d表示int类型的值,看下面的例子你就会明白:
a=1,b=2
printf("a=%d,b=%d",a,b)
结果为:a=1,b=2
其中第一个%d会用第一个变量的值来代替,第二个%d会用第二个变量的值来代替。如果还有,则以此类推就可以了。
关于%d是什么,你就可以去参考第二位朋友的回答了!
参考技术C printf, wprintf
Print formatted output to the standard output stream.

int printf( const char *format [, argument]... );

int wprintf( const wchar_t *format [, argument]... );

Routine Required Header Compatibility
printf <stdio.h> ANSI, Win 95, Win NT
wprintf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

Each of these functions returns the number of characters printed, or a negative value if an error occurs.

Parameters

format

Format control

argument

Optional arguments

Remarks

The printf function formats and prints a series of characters and values to the standard output stream, stdout. If arguments follow the format string, the format string must contain specifications that determine the output format for the arguments. printf and fprintf behave identically except that printf writes output to stdout rather than to a destination of type FILE.

wprintf is a wide-character version of printf; format is a wide-character string. wprintf and printf behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tprintf printf printf wprintf

The format argument consists of ordinary characters, escape sequences, and (if arguments follow format) format specifications. The ordinary characters and escape sequences are copied to stdout in order of their appearance. For example, the line

printf("Line one\n\t\tLine two\n");

produces the output

Line one
Line two

Format specifications always begin with a percent sign (%) and are read left to right. When printf encounters the first format specification (if any), it converts the value of the first argument after format and outputs it accordingly. The second format specification causes the second argument to be converted and output, and so on. If there are more arguments than there are format specifications, the extra arguments are ignored. The results are undefined if there are not enough arguments for all the format specifications.

Example

/* PRINTF.C: This program uses the printf and wprintf functions
* to produce formatted output.
*/

#include <stdio.h>

void main( void )

char ch = 'h', *string = "computer";
int count = -9234;
double fp = 251.7366;
wchar_t wch = L'w', *wstring = L"Unicode";

/* Display integers. */
printf( "Integer formats:\n"
"\tDecimal: %d Justified: %.6d Unsigned: %u\n",
count, count, count, count );

printf( "Decimal %d as:\n\tHex: %Xh C hex: 0x%x Octal: %o\n",
count, count, count, count );

/* Display in different radixes. */
printf( "Digits 10 equal:\n\tHex: %i Octal: %i Decimal: %i\n",
0x10, 010, 10 );

/* Display characters. */

printf("Characters in field (1):\n%10c%5hc%5C%5lc\n", ch, ch, wch, wch);
wprintf(L"Characters in field (2):\n%10C%5hc%5c%5lc\n", ch, ch, wch, wch);

/* Display strings. */

printf("Strings in field (1):\n%25s\n%25.4hs\n\t%S%25.3ls\n",
string, string, wstring, wstring);
wprintf(L"Strings in field (2):\n%25S\n%25.4hs\n\t%s%25.3ls\n",
string, string, wstring, wstring);

/* Display real numbers. */
printf( "Real numbers:\n\t%f %.2f %e %E\n", fp, fp, fp, fp );

/* Display pointer. */
printf( "\nAddress as:\t%p\n", &count);

/* Count characters printed. */
printf( "\nDisplay to here:\n" );
printf( "1234567890123456%n78901234567890\n", &count );
printf( "\tNumber displayed: %d\n\n", count );
参考技术D printf表示输出
printf("%d\n",m)是其一般形式。

c语言中,printf,print,input,都表示输出,他们有啥区别

print--是函数,可以返回一个值,只能有一个参数。用起来最简单

printf--函数,把文字格式化以后输出,直接调用系统调用进行IO的,他是非缓冲的。

input--是从指定的已经打开的文件里面读取。

如:
$name="hunte";
$age=25;
printf("my name is %s, age %d", $name, $age);

sprintf--跟printf相似,但不打印,而是返回格式化后的文字,其他的与printf一样。
如:
char sql[256];
sprintf(sql,"select * from table where no = '%s'",bankno);
它的功能只是把""里面的语句赋给了变量sql。

参考技术A printf是c的一个库函数,而print和input都不是吧!只是后两个我们常习惯用来作为自己定义的函数的名称!应该是这样吧! 参考技术B printf() 是标准输出 ,输出在stdout上
input()是从指定的已经打开的文件里面读取
print()....不知道...好像没有吧
参考技术C C语言中标准函数只有printf是输出吧.
print和input都不是C的标准函数,input从名字上看是输入而不是输出.
参考技术D printf(“”);是输出函数
print 应该是输出打印的意思
input()调用输入

以上是关于C语言中printf()后的括号里面的怎么写,表示啥?的主要内容,如果未能解决你的问题,请参考以下文章

在keil里printf

VC++里printf();

c语言中2的次方怎么写怎么表示?

c语言中,printf,print,input,都表示输出,他们有啥区别

为啥visual+studio+code里面用c语言写的printf输出中终端显示问号?

C语言得到的数保留两位小数应该怎么写语句?