标签: format-specifiers

(零)C/C++中的指针

我正在处理一个项目,我不断遇到这个错误,不允许我完成项目.当我初始化我的一个指针指向将在程序执行期间生成的对象时,我将其初始化为NULL.然后,当我检查它的设置时,它返回一个值nil.这样的事情怎么可能?我不相信C中存在零指针.有什么方法可以解决这个问题吗?

struct order_line *front = NULL;
...
printf("Head: %p\n", front);  // prints -> Head: (nil)
Run Code Online (Sandbox Code Playgroud)

c c++ printing pointers format-specifiers

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

Python 3的新格式规范语法优于`%`运算符使用的旧格式有什么优势?

Pep 3101提供了最终用%format方法替换操作员的基本原理.这个问题和接受的答案都在同一点上.

但是,我找不到新语法的基本原理,我不明白这种改变的好处.pep 3101列出了各种替代语法,其中还包括printfC99标准及其变体中所述的着名格式说明符.(对于文档例如去这里第7.19.6.1的页数"fprintf函数" 274ff).

对于新string.format()方法,它被认为重用了%运营商使用的相同格式规范语言.

使用旧语法无法完成的新语法可以做些什么?

编辑:参数重新排序也可以像添加到ANSI C标准一样添加到旧语法中.最近看到了man sprintf

python string formatting format-specifiers python-3.x

8
推荐指数
0
解决办法
958
查看次数

什么是hh和h格式说明符的需要?

使用hhx而不是x有什么用,在下面的代码中,mac_str是char指针macuint8_t数组,

 sscanf(mac_str,"%x:%x:%x:%x:%x:%x",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
Run Code Online (Sandbox Code Playgroud)

当我尝试上面的代码时它发出警告,

warning: format ‘%x’ expects argument of type ‘unsigned int *’, but argument 8 has type ‘uint8_t *’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)

但我在他们指定的一些代码中看到了

sscanf(str,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
Run Code Online (Sandbox Code Playgroud)

哪个不给任何警告

但两者都是一样的,使用hhx而不是x的需要是什么,我在网上搜索但没有直接回答

c scanf format-specifiers

8
推荐指数
2
解决办法
6813
查看次数

是否有格式说明符始终表示使用_tprintf的字符串?

当您使用Windows上开发应用TCHAR的支持,%s_tprintf()手段char *建立串ANSI和wchar_t *为Unicode版本,同时%S意味着相反.

但是有没有格式说明符总是意味着char *字符串无论是Ansi还是Unicode构建?因为即使在Windows上,UTF-16并不真正用于文件或网络,但事实上你仍然需要处理基于字节的字符串,无论你编译应用程序的本地字符类型如何.

windows printf tchar format-specifiers

7
推荐指数
1
解决办法
7298
查看次数

是否有一个printf说明符要求浮点数不是双倍?

当我使用"%f"说明符snprintf和类型参数时,我得到MISRA类型错误float.

根据我的研究,MISRA是正确的,因为"%f"是一种类型的double.

是否有一个浮点说明符或修饰符将使用float类型参数而不是double

我正在研究一个嵌入式系统,不想将32位浮点数转换为64位double只是为了取悦该snprintf功能.代码打印到调试/控制台端口,这是转换发生的唯一位置.

对于那些需要代码示例的人:

// This section is for those C++ purists and it fulfills the C++ tag.
#if __cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif

#define BUFFER_SIZE (128U)

int main(void)
{
    char buffer[BUFFER_SIZE];
    float my_float = 1.234F;

    // The compiler will promote the single precision "my_float"
    //   to double precision before passing to snprintf.
    (void)snprintf(buffer, BUFFER_SIZE, "%10.4f", my_float);
    puts(buffer);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我对SO和Web的所有研究都是关于打印浮点值,而不是关于哪些说明符需要float …

c c++ promotions misra format-specifiers

7
推荐指数
3
解决办法
2522
查看次数

什么`scanf("%*[^ \n]%*c")`是什么意思?

我想在C中创建一个循环,当程序要求一个整数并且用户键入一个非数字字符时,程序再次请求一个整数.

我刚刚找到了以下代码.但我不明白这意味着什么scanf("%*[^\n]%*c").什么^\n意思?什么是*之前^\nc意味着什么呢?

/*

 This program calculate the mean score of an user 4 individual scores,
 and outputs the mean and a final grade
 Input: score1, score2,score2, score3
 Output: Mean, FinalGrade

*/
#include <stdio.h>
//#include <stdlib.h>

int main(void){
  int userScore = 0; //Stores the scores that the user inputs
  float meanValue = 0.0f; //Stores the user mean of all the notes
  char testChar = 'f'; //Used to avoid that the code …
Run Code Online (Sandbox Code Playgroud)

c user-input scanf format-specifiers

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

这个%1 $ @%2 $ d格式说明符在目标c中是什么意思

我一直在使用格式说明符,但它们是通用的%d,%@但是今天在我看到这些%1$@%2$d并且不理解它们代表的教程中.
这是一个计算器示例,因此他们在此声明中使用它们:stack = [NSString stringWithFormat:@"%1$@%2$d",stack,number];

objective-c format-specifiers

7
推荐指数
1
解决办法
1199
查看次数

C中大写和小写双(浮点)类型说明符的区别

在 C 中 double/float 有一个集合类型说明符:%f %F %g %G %e %E. 有什么区别吗

  • %f并且%F
  • %g并且%G
  • %e%E

根据printfscanf输出是相等的。那为什么大写和小写都有效呢?

请注意, scanf double 的类型说明符以小写开头 l

c double types format-specifiers

7
推荐指数
2
解决办法
2889
查看次数

2 ways of accessing value of an array of pointers in C

The first block

#include <stdio.h>

const int MAX = 3;

int main() {

    int  var[] = { 10, 100, 200 };
    int i, *ptr[MAX];

    for (i = 0; i < MAX; i++) {
        ptr[i] = &var[i]; /* assign the address of integer. */
    }

    for (i = 0; i < MAX; i++) {
        printf("Value of var[%d] = %d\n", i, *ptr[i]);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Easy to understand, since ptr is an array of int pointers. So when you need to …

c arrays printf pointers format-specifiers

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

%g printf说明符的确切含义是什么?

%g说明符似乎并不在多数来源记录它运行得的方式行事.

根据我发现的大多数来源,在使用printf说明符的多种语言中,说明%g符应该等同于%f或者%e- 对于提供的值产生较短的输出.例如,在撰写此问题时,cplusplus.com表示说明g符意味着:

使用最短的表示:%e%f

PHP手册说这意味着:

g - %e%f中的较短者.

而且这里有一个堆栈溢出的答案是声称,

%g 使用最短的表示.

Quora的回答声称:

%g 以这两个表示中最短的方式打印数字

但这种行为不是我在现实中看到的.如果我编译并运行该程序(作为C或C++ - 它是一个在两者中具有相同行为的有效程序):

#include <stdio.h>

int main(void) {
    double x = 123456.0;
    printf("%e\n", x);
    printf("%f\n", x);
    printf("%g\n", x);
    printf("\n");

    double y = 1234567.0;
    printf("%e\n", y);
    printf("%f\n", y);
    printf("%g\n", y);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

...然后我看到这个输出:

1.234560e+05
123456.000000
123456

1.234567e+06
1234567.000000
1.23457e+06
Run Code Online (Sandbox Code Playgroud)

显然, …

c language-agnostic floating-point printf format-specifiers

7
推荐指数
1
解决办法
1388
查看次数