我需要使用类似NSLog但没有时间戳和换行符的东西,所以我正在使用printf.我怎么用这个NSString?
我正在尝试使用printf()以下方法将数字写入两位小数:
#include <cstdio>
int main()
{
printf("When this number: %d is assigned to 2 dp, it will be: 2%f ", 94.9456, 94.9456);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行该程序时,我得到以下输出:
# ./printf
When this number: -1243822529 is assigned to 2 db, it will be: 2-0.000000
Run Code Online (Sandbox Code Playgroud)
这是为什么?
谢谢.
毕竟,这两个陈述都做同样的事情......
int a = 10;
int *b = &a;
printf("%p\n",b);
printf("%08X\n",b);
Run Code Online (Sandbox Code Playgroud)
例如(具有不同的地址):
0012FEE0
0012FEE0
Run Code Online (Sandbox Code Playgroud)
根据需要格式化指针是微不足道的%x,那么是否有一些很好的%p选择?
除了%hn和%hhn(指定h或hh指定指向对象的大小),格式说明符的h和hh修饰符有printf什么意义?
由于标准要求的默认促销适用于可变函数,因此不可能将类型char或short(或其任何有符号/无符号变体)的参数传递给printf.
根据7.19.6.1(7),h修饰符:
指定以下d,i,o,u,x或X转换规范适用于short int或unsigned short int参数(该参数将根据整数提升进行提升,但其值应转换为short int或打印前的unsigned short int); 或者后续的n转换规范适用于指向short int参数的指针.
如果参数实际上是类型short或unsigned short,则促销int后转换回short或unsigned short将产生与促销相同的值int而不进行任何转换.因此,对于类型的参数short或unsigned short,%d,%u等应给予相同的结果到%hd,%hu等.(并且同样地对于char类型和hh).
据我所知,h或者hh修饰符可能有用的唯一情况是当参数将其传递int到范围之外时,short或者unsigned short …
可能重复:
c/c ++函数的源代码
我想知道在哪里可以找到使用的C代码,这样当我写printf("Hello World!")时; 在我的C程序中知道它必须将该字符串打印到STDOUT.我查看了<stdio.h>,但在那里我只能找到它的原型int printf(const char*format,...),但不是内部的样子.
我是C的新手,我遇到了:
#include <stdio.h>
#include <inttypes.h>
int main(void)
{
uint64_t foo = 10;
printf("foo is equal to %" PRIu64 "!\n", foo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理!我不明白为什么?有人可以帮我这个吗?非常感谢!托
我正在尝试将char打印为正值:
char ch = 212;
printf("%u", ch);
Run Code Online (Sandbox Code Playgroud)
但我得到:
4294967252
Run Code Online (Sandbox Code Playgroud)
我如何212进入输出?
随着iOS版的64位版本,我们不能使用%d和%u再格式化NSInteger和NSUInteger.因为对于64位,这些是typedef'd long而unsigned long不是int和unsigned int.
因此,如果您尝试使用%d格式化NSInteger,Xcode将发出警告.Xcode对我们很好,并提供了这两种情况的替代品,它包括一个带有l前缀的格式说明符和一个长的类型转换.然后我们的代码基本上是这样的:
NSLog(@"%ld", (long)i);
NSLog(@"%lu", (unsigned long)u);
Run Code Online (Sandbox Code Playgroud)
如果你问我,这是一个痛苦的眼睛.
几天前,Twitter上有人提到格式说明符%zd来格式化签名变量和%tu在32位和64位平台上格式化无符号变量.
NSLog(@"%zd", i);
NSLog(@"%tu", u);
Run Code Online (Sandbox Code Playgroud)
这似乎有效.而且我更喜欢类型转换.
但老实说,我不知道为什么这些工作.现在两者对我来说基本上都是神奇的价值观.
我做了一些研究,并发现z前缀意味着以下格式说明符具有相同的大小size_t.但我完全不知道前缀是什么t意味着.所以我有两个问题:
究竟是什么%zd和%tu意味着什么呢?
是否可以安全使用%zd,%tu而不是苹果建议长期使用?
我知道类似的问题和Apples 64位过渡指南,它们都推荐这种%lu (unsigned long)方法.我要求替代铸造.
我在Windows上使用最新的gcc和Netbeans.为什么不起作用long double?是printf符%lf错了吗?
码:
#include <stdio.h>
int main(void)
{
float aboat = 32000.0;
double abet = 5.32e-5;
long double dip = 5.32e-5;
printf("%f can be written %e\n", aboat, aboat);
printf("%f can be written %e\n", abet, abet);
printf("%lf can be written %le\n", dip, dip);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
32000.000000 can be written 3.200000e+004
0.000053 can be written 5.320000e-005
-1950228512509697500000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000.000000
can be written 2.725000e+002
Press [Enter] to close the terminal ...
Run Code Online (Sandbox Code Playgroud) printf ×10
c ×7
c++ ×2
objective-c ×2
cocoa-touch ×1
decimal ×1
formatting ×1
gcc ×1
go ×1
ios ×1
long-double ×1
macros ×1
nsinteger ×1
nslog ×1
nsstring ×1
promotions ×1
stdio ×1
string ×1