scanf(),%[-A-B234abc]或的正确扫描集格式说明符语法是什么%[A-B234abc-]?我在 StackOverflow 上找不到答案,以下两个链接给出了相互矛盾的答案:
cplusplusreference: 不是第一个字符的破折号 (-) 可能会在某些库实现中产生不可移植的行为。
beez: 要匹配连字符,请将其作为集合中的最后一个字符:%[AC-]。
尝试将宽度说明符与%@. 他们的工作罚款NSLog,但不与NSString stringWithFormat:。
例子:
NSString *rightAligned = @"foo";
NSString *leftAligned = @"1";
NSLog(@"| %15@ | %-15@ |", rightAligned, leftAligned);
Run Code Online (Sandbox Code Playgroud)
你会得到预期的输出:
| foo | 1 |
Run Code Online (Sandbox Code Playgroud)
但更换NSLog同stringWithFormat::
NSString *test = [NSString stringWithFormat:@"| %15@ | %-15@ |", rightAligned, leftAligned];
Run Code Online (Sandbox Code Playgroud)
并且 的值test不正确:
| foo | 1 |
Run Code Online (Sandbox Code Playgroud)
如果我将其更改为使用%s,cStringUsingEncoding:然后它可以工作:
NSString *test2 = [NSString stringWithFormat:@"| %15s | %-15s |", [rightAligned cStringUsingEncoding:NSUTF8StringEncoding], [leftAligned cStringUsingEncoding:NSUTF8StringEncoding]];
Run Code Online (Sandbox Code Playgroud)
结果与 相同NSLog。
真正奇怪的是它NSLog …
为什么操作符 ( *) 需要访问int*变量的值而不是 for char*?
char *char_ptr;
int *int_ptr;
int mem_size = 50;
char_ptr = (char *) malloc(mem_size);
strcpy(char_ptr, "This is memory is located on the heap.");
printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);
int_ptr = (int *) malloc(12);
*int_ptr = 31337;
printf("int_ptr (%p) --> %d\n", int_ptr, *int_ptr);
Run Code Online (Sandbox Code Playgroud)
输出:
char_ptr (0x8742008) --> 'This is memory is located on the heap.'
int_ptr (0x8742040) --> 31337
Run Code Online (Sandbox Code Playgroud) 以下是Beez C指南 (LINK)讲述%[]格式说明符的内容:
It allows you to specify a set of characters to be stored away (likely in an array of chars). Conversion stops when a character that is not in the set is matched.
如果您能澄清一些基于这个前提的基本问题,我将不胜感激:
1)存储在参数(类型char*)中的那两个格式说明符作为字符数组或带有\0终止字符(字符串)的字符数组获取输入吗?如果不是字符串,如何将其存储为字符串,在下面的程序中我们想要将字符序列作为字符串获取并在遇到特定字符(在否定字符集中)时停止?
2)我的程序似乎建议%[^|]当|遇到否定字符时处理停止为说明符.但是当它再次为下一个格式说明符启动时,它是否从之前停止的否定字符开始?在我的程序中我打算忽略|因此我使用过%*c.但我测试并发现如果我使用%c和另一个类型的参数char,那么该字符|确实存储在该参数中.
3)最后但对我来说至关重要的是,为%s格式说明符传递字符数组printf()和字符串(NULL终止字符数组)之间的区别是什么?在我的另一个程序标题中character array vs string,我传递了一个字符数组(不是NULL终止) )对于%s格式说明符printf(),它就像字符串一样打印出来.有什么区别?
//用于说明%[^]说明符的程序
#include<stdio.h> …Run Code Online (Sandbox Code Playgroud) 请解释输出.是什么%.#s在printf()意思?
#include<stdio.h>
#include <stdlib.h>
int main(int argc,char*argv[]){
char *A="HELLO";
printf("%.#s %.2s\n",A,A);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
#s HE
Run Code Online (Sandbox Code Playgroud) 在c中,我们可以使用各种格式说明符
那么有什么方法可以将这些用于std :: cout吗?
我在课程中得到了一些负面反馈(c ++程序员的c ++),使用printf而不是cout因为我想要一些格式化:(
下面我试图设置一个格式说明符,但每次调用该函数时我都想以不同的方式进行.关键是打印浮点数Data_Ave.在一个实例中,我想要说明符%2.3f,因此我将为sig_figs_before传递函数a 2,为sig_figs_after传递3.在下一个例子中,我想要%1.4f,所以我会通过那些整数.
char * Send_Data(float Data[10], const char* SUBJECT_SEND_NAME, char *Data_Backup, int sig_figs_before, int sig_figs_after)
{
float Data_Ave = ((Data[0] + Data[1] + Data[2] + Data[3] + Data[4] +
Data[5] + Data[6] + Data[7] + Data[8] + Data[9])/10);
sprintf(msgBody,"%%i.%if, ", before_decimal, after_decimal, Data_Ave);
... //Some more code
}
Run Code Online (Sandbox Code Playgroud)
我很确定这不会起作用,所以我想将浮动分成两个整数并按照这样打印:
sprintf(msgBody,"%i.%i, ", int_value, decimal_value);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何正确拆分浮子.有什么建议?
想知道是否有人可以帮我解决我在Xcode中收到的错误警告.我认为它与32 V 64bit有关.我希望代码能够在32位和64位上运行.相关的代码部分是:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"expected:%ld, got:%ld", (long)UIWebViewNavigationTypeLinkClicked, navigationType);
NSLog(@"Main Doc URL:%@", [[request mainDocumentURL] absoluteString]);
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request mainDocumentURL]];
return NO;
Run Code Online (Sandbox Code Playgroud)
非常感谢
printf("%5s\n", "#");
Run Code Online (Sandbox Code Playgroud)
给出:
#
Run Code Online (Sandbox Code Playgroud)
他们是否可以使用整数格式说明符设置此字符串的字段宽度?
像这样的东西,
printf("%%ds\n", 5, "#");
Run Code Online (Sandbox Code Playgroud) 表达这个问题的另一种方式可能是“如何在 printf 中结束格式说明符”
如果我想100us使用以下代码以这种格式打印微秒......
long microseconds = 100L;
printf("%lus", microseconds);
Run Code Online (Sandbox Code Playgroud)
只打印,100s因为 theu与 the 组合%l,并且它将格式说明符解释为 anunsigned long而不是 along
c printf format-specifiers conversion-specifier length-modifiers