C语言 将 printf 与非空终止字符串一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3767284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using printf with a non-null terminated string
提问by whoi
Suppose you have a string which is NOT nullterminated and you know its exact size, so how can you print that string with printfin C? I recall such a method but I can not find out now...
假设您有一个未null终止的字符串并且您知道它的确切大小,那么您如何printf在 C 中打印该字符串?我记得这样的方法,但我现在找不到了......
回答by DarkDust
There is a possibility with printf, it goes like this:
printf 有一种可能性,它是这样的:
printf("%.*s", stringLength, pointerToString);
No need to copy anything, no need to modify the original string or buffer.
无需复制任何内容,无需修改原始字符串或缓冲区。
回答by Tobu
Here is an explanation of how %.*sworks, and where it's specified.
这是对%.*s工作原理及其指定位置的说明。
The conversion specifications in a printf template string have the general form:
% [ param-no $] flags width [ . precision ] type conversionor
% [ param-no $] flags width . * [ param-no $] type conversion
printf 模板字符串中的转换规范具有一般形式:
% [ param-no $] flags width [ . precision ] type conversion或者
% [ param-no $] flags width . * [ param-no $] type conversion
The second form is for getting the precision from the argument list:
第二种形式用于从参数列表中获取精度:
You can also specify a precision of ‘*'. This means that the next argument in the argument list (before the actual value to be printed) is used as the precision. The value must be an int, and is ignored if it is negative.
您还可以指定精度为“*”。这意味着参数列表中的下一个参数(在要打印的实际值之前)用作精度。该值必须是一个整数,如果它是负数则被忽略。
—?Output conversion syntaxin the glibc manual
——?glibc 手册中的输出转换语法
For %sstring formatting, precision has a special meaning:
对于%s字符串格式化,精度有特殊的意义:
A precision can be specified to indicate the maximum number of characters to write; otherwise characters in the string up to but not including the terminating null character are written to the output stream.
可以指定精度来指示要写入的最大字符数;否则字符串中直到但不包括终止空字符的字符将写入输出流。
—?Other output conversionsin the glibc manual
——?glibc 手册中的其他输出转换
Other useful variants:
其他有用的变体:
"%*.*s", maxlen, maxlen, valwill right-justify, inserting spaces before;"%-*.*s", maxlen, maxlen, valwill left-justify.
"%*.*s", maxlen, maxlen, val将右对齐,在前面插入空格;"%-*.*s", maxlen, maxlen, val将左对齐。
回答by Pedro Sousa
You can use an fwrite() to stdout!
您可以使用 fwrite() 到标准输出!
fwrite(your_string, sizeof(char), number_of_chars, stdout);
This way you will output the first chars (number defined in number_of_chars variable ) to a file, in this case to stdout (the standard output, your screen)!
通过这种方式,您将输出第一个字符(在 number_of_chars 变量中定义的数字)到一个文件,在这种情况下输出到 stdout(标准输出,您的屏幕)!
回答by Todd Freed
printf("%.*s", length, string)will NOT work.
printf("%.*s", length, string)不管用。
This means to print UP TO length bytes OR a null byte, whichever comes first. If your non-null-terminated array-of-char contains null bytes BEFORE the length, printf will stop on those, and not continue.
这意味着打印最多长度字节或空字节,以先到者为准。如果您的非空字符数组在长度之前包含空字节,则 printf 将在这些字节上停止,而不是继续。
回答by codeDom
printf("%.5s", pointerToNonNullTerminatedString);
The string length will be 5.
字符串长度将为 5。
回答by fuddin
#include<string.h>
int main()
{
/*suppose a string str which is not null terminated and n is its length*/
int i;
for(i=0;i<n;i++)
{
printf("%c",str[i]);
}
return 0;
}
I edited the code,heres another way:
我编辑了代码,这是另一种方式:
#include<stdio.h>
int main()
{
printf ("%.5s","fahaduddin");/*if 5 is the number of bytes to be printed and fahaduddin is the string.*/
return 0;
}

