标签: format-specifiers

正确的size_t:%zu或%Iu的printf格式说明符?

我想在C++中使用Microsoft Visual Studio 2010 打印出size_t变量的值printf(我想使用printf而不是<<在这段特定的代码中,所以请不要回答告诉我应该使用它<<).

根据帖子

平台无关size_t c中的格式说明符?

正确的平台无关方式是使用%zu,但这似乎在Visual Studio中不起作用.Visual Studio文档

http://msdn.microsoft.com/en-us/library/vstudio/tcxf1dw6.aspx

告诉我,我必须使用%Iu(使用大写i,而不是小写l).

微软是否不遵守这些标准?或者自C99以来标准是否已更改?或者C和C++之间的标准是不同的(这对我来说似乎很奇怪)?

c c++ printf visual-studio-2010 format-specifiers

41
推荐指数
4
解决办法
5万
查看次数

使用scanf()读取的字符串大小不超过

编辑:对于我的班级,我必须使用scanf.所以推荐其他输入方式不是我正在寻找的解决方案(如果有一个涉及scanf).


如果我正在读取小项目的用户输入(例如,游戏).让我们说我会问would you like to play? 这会接受yesno回答.所以我写了一些像这样的简单代码:

#include <stdio.h>

int main(void)
{
     char string[3]; //The max number of letters for "yes".

     printf("Would you like to play?");
     scanf("%s", string);
}
Run Code Online (Sandbox Code Playgroud)

所以这段代码应该简单地要求他们输入yesno.我正在设定我char array的大小3.这样它足够大yes,也可以no.但是如果有人要输入无效的输入,比如yesss,我知道如何比较字符串以后处理这样的事件,但是这不会在技术上/可能覆盖我声明的其他局部变量,因为它会扩展到我的数组长度之外?如果是这样,有没有办法处理这个限制3个输入字符或什么?如果没有,为什么/如何知道只输入3的大小?

*注意:我是C的新手,我找不到这个,所以即使这很简单,我也很感激.因为我显然缺少一些东西.我来自java背景,这甚至都不是问题.

c string scanf format-specifiers

31
推荐指数
4
解决办法
4万
查看次数

格式说明符%02x

我有一个简单的程序:

#include <stdio.h>
int main()
{
        long i = 16843009;
        printf ("%02x \n" ,i);
}
Run Code Online (Sandbox Code Playgroud)

我使用%02x格式说明符来获得2个char输出,但是,我得到的输出是:

1010101 
Run Code Online (Sandbox Code Playgroud)

虽然我期待它:01010101.

c printf format-specifiers

29
推荐指数
3
解决办法
9万
查看次数

scanf格式说明符中的星号是什么意思?

所以我偶然发现了这段代码,我无法弄清楚它的用途是什么,或者它是如何工作的:

int word_count;
scanf("%d%*c", &word_count);
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是%*d引用char指针或word_count不接受char变量.

有人可以对此有所了解吗?

c scanf char format-specifiers

29
推荐指数
3
解决办法
3745
查看次数

printf(s)和printf("%s",s)之间的根本区别是什么?

问题很简单,s是一个字符串,我突然想到试图用printf(s)它来查看它是否可行,我在一个案例中得到警告而在另一个案例中没有警告.

char* s = "abcdefghij\n";
printf(s);

// Warning raised with gcc -std=c11: 
// format not a string literal and no format arguments [-Wformat-security]

// On the other hand, if I use 

char* s = "abc %d efg\n";
printf(s, 99);

// I get no warning whatsoever, why is that?

// Update, I've tested this:
char* s = "random %d string\n";
printf(s, 99, 50);

// Results: no warning, output "random 99 string".
Run Code Online (Sandbox Code Playgroud)

那么,什么是之间的基本差异printf(s),并printf("%s", s)和为什么会在短短的一个案例一个警告?

c string printf compiler-warnings format-specifiers

27
推荐指数
3
解决办法
2791
查看次数

`printf()`中格式说明符"%qd"的用途是什么?

%qd在浏览github代码时看到了格式说明符.然后我检查了GCC编译器,它运行正常.

#include <stdio.h>

int main()
{  
    long long num = 1;
    printf("%qd\n", num);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

什么是格式说明目的%qdprintf()

c openbsd gcc format-specifiers

20
推荐指数
3
解决办法
1489
查看次数

sprintf for unsigned _int64

我有以下代码.sprintf中第二个%d的输出始终显示为零.我想我指的是错误的说明符.任何人都可以帮助我获得具有正确值的写字符串.这必须以posix标准实现.感谢您的投入

void main() {
    unsigned _int64 dbFileSize = 99;
    unsigned _int64 fileSize = 100;
    char buf[128];
    memset(buf, 0x00, 128);
    sprintf(buf, "\nOD DB File Size = %d bytes \t XML file size = %d bytes", fileSize, dbFileSize);
    printf("The string is %s ", buf);
    }
Run Code Online (Sandbox Code Playgroud)

输出:

The string is
OD DB File Size = 100 bytes      XML file size = 0 bytes 
Run Code Online (Sandbox Code Playgroud)

c c++ format-specifiers uint64 long-long

19
推荐指数
3
解决办法
4万
查看次数

为什么不是新的(特定于位宽)printf()格式选项字符串作为C99的一部分?

在研究如何printf()在C中执行跨平台格式字符串时(也就是说,考虑到我希望每个整数参数printf()应该是的位数),我在维基百科文章的这一部分中进行了讨论printf().本文讨论了可以传递给printf()格式化字符串的非标准选项,例如(似乎是特定于Microsoft的扩展):

printf("%I32d\n", my32bitInt);
Run Code Online (Sandbox Code Playgroud)

它接着说:

ISO C99包括inttypes.h头文件,其中包含许多用于独立于平台的printf编码的宏.

...然后列出一组可在所述标题中找到的宏.查看头文件,使用它们我必须写:

 printf("%"PRId32"\n", my32bitInt);
Run Code Online (Sandbox Code Playgroud)

我的问题是:我错过了什么吗?这真的是标准的C99方式吗?如果是这样,为什么?(虽然我从未见过以这种方式使用格式字符串的代码并不感到惊讶,因为它看起来很麻烦......)

c printf types c99 format-specifiers

17
推荐指数
2
解决办法
7350
查看次数

是否有一个使用布尔值的格式说明符?

我想做这样的事情:

NSLog(@"You got: %x", booleanValue);
Run Code Online (Sandbox Code Playgroud)

其中x是说明符.但我找不到一个!我想避免:

if (booleanValue) {
    NSLog(@"You got: YES");
}
else {
    NSLog(@"You got: NO");
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?文档没有布尔说明符. %@也没用.

string iphone format-specifiers ios

17
推荐指数
4
解决办法
1万
查看次数

为数字指定最大printf字段宽度(必要时截断)?

您可以使用printf字段宽度说明符截断字符串:

printf("%.5s", "abcdefgh");

> abcde
Run Code Online (Sandbox Code Playgroud)

遗憾的是它不换号(更换工作dx是一样的):

printf("%2d",   1234);  // for 34
printf("%.2d",  1234);  // for 34
printf("%-2d",  1234);  // for 12
printf("%-.2d", 1234);  // for 12

> 1234
Run Code Online (Sandbox Code Playgroud)

是否有一种简单/平凡的方式来指定要打印的位数,即使它意味着截断数字?

MSDN明确表示不会发生这种不必要的限制.(是的,它可以通过创建字符串等来完成,但我希望有一个"printf技巧"或聪明的kludge.)

printf truncate format-specifiers

17
推荐指数
2
解决办法
3万
查看次数