我正在处理一个项目,我不断遇到这个错误,不允许我完成项目.当我初始化我的一个指针指向将在程序执行期间生成的对象时,我将其初始化为NULL.然后,当我检查它的设置时,它返回一个值nil.这样的事情怎么可能?我不相信C中存在零指针.有什么方法可以解决这个问题吗?
struct order_line *front = NULL;
...
printf("Head: %p\n", front); // prints -> Head: (nil)
Run Code Online (Sandbox Code Playgroud) Pep 3101提供了最终用%该format方法替换操作员的基本原理.这个问题和接受的答案都在同一点上.
但是,我找不到新语法的基本原理,我不明白这种改变的好处.pep 3101列出了各种替代语法,其中还包括printfC99标准及其变体中所述的着名格式说明符.(对于文档例如去这里第7.19.6.1的页数"fprintf函数" 274ff).
对于新string.format()方法,它被认为重用了%运营商使用的相同格式规范语言.
使用旧语法无法完成的新语法可以做些什么?
编辑:参数重新排序也可以像添加到ANSI C标准一样添加到旧语法中.最近看到了man sprintf
使用hhx而不是x有什么用,在下面的代码中,mac_str是char指针而mac是uint8_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的需要是什么,我在网上搜索但没有直接回答
当您使用Windows上开发应用TCHAR的支持,%s在_tprintf()手段char *建立串ANSI和wchar_t *为Unicode版本,同时%S意味着相反.
但是有没有格式说明符总是意味着char *字符串无论是Ansi还是Unicode构建?因为即使在Windows上,UTF-16并不真正用于文件或网络,但事实上你仍然需要处理基于字节的字符串,无论你编译应用程序的本地字符类型如何.
当我使用"%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中创建一个循环,当程序要求一个整数并且用户键入一个非数字字符时,程序再次请求一个整数.
我刚刚找到了以下代码.但我不明白这意味着什么scanf("%*[^\n]%*c").什么^\n意思?什么是*之前^\n和c意味着什么呢?
/*
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) 我一直在使用格式说明符,但它们是通用的%d,%@但是今天在我看到这些%1$@%2$d并且不理解它们代表的教程中.
这是一个计算器示例,因此他们在此声明中使用它们:stack = [NSString stringWithFormat:@"%1$@%2$d",stack,number];
在 C 中 double/float 有一个集合类型说明符:%f %F %g %G %e %E. 有什么区别吗
%f并且%F,%g并且%G,%e和%E?根据printf和scanf输出是相等的。那为什么大写和小写都有效呢?
请注意, scanf double 的类型说明符以小写开头 l
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 …
该%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 ×7
printf ×3
c++ ×2
pointers ×2
scanf ×2
arrays ×1
double ×1
formatting ×1
misra ×1
objective-c ×1
printing ×1
promotions ×1
python ×1
python-3.x ×1
string ×1
tchar ×1
types ×1
user-input ×1
windows ×1