format-specifiers

Is there a format specifier that works with Boolean values?

寵の児 提交于 2020-01-12 06:38:08
问题 I want to do something like this: NSLog(@"You got: %x", booleanValue); where x is the specifier. But I can't find one! I want to avoid: if (booleanValue) { NSLog(@"You got: YES"); } else { NSLog(@"You got: NO"); } Any ideas? The docs didn't have a Boolean specifier. %@ didn't work either. 回答1: Here are two things that work: NSLog(@"You got: %@",booleanValue ? @"YES" : @"NO"); or you can cast: NSLog(@"You got: %d", (int)booleanValue); Which will output 0 or 1 回答2: You can cast it to an int and

How Do I Parse a Date Time String That Includes Fractional Time?

匆匆过客 提交于 2020-01-10 05:11:10
问题 I have a date time string: 20:48:01.469 UTC MAR 31 2016 I would like to convert this string representation of time to a struct tm using strptime , but my format string isn't working. Is there a format specifier for fractional seconds? Perhaps %S , %s , or something else? Code snippet is below: tm tmbuf; const char *str = "20:48:01.469 UTC MAR 31 2016" const char *fmt = "%H:%M:%s %Z %b %d %Y"; strptime(str,fmt,&tmbuf); 回答1: Using this free, open source C++11/14 library, here is another way to

What does an 'F' in the Format specifier of printf mean?

爷,独闯天下 提交于 2020-01-06 02:59:33
问题 What does %.8Ff format specifier in printf do? What does F mean? 回答1: According to the manual: The F conversion specifier produces "INF", "INFINITY", or "NAN" instead of "inf", "infinity", or "nan", respectively. In your format string %.8Ff , the f is treated as a literal character and printed as an f . 回答2: One possible use of putting a literal f after the format specifier could be to print a string that's later to be parsed by a C or C++ compiler as a float constant instead of double

About printf format string in C

和自甴很熟 提交于 2020-01-03 19:00:56
问题 Let's take the following program: #include <stdio.h> int main() { long t =57 ; printf("[%+03ld]", t); } and it's output: [+57] I am somehow confused: I told him to pad the output to width 3 ( 03ld ), with zeroes, however it seems that if I force the output to put a plus sign before the number ( + ) it will not add the required zeroes if the length of the number is already 2 digits (as in 57). For numbers <10 it pads with 1 zero. From http://www.cplusplus.com/reference/cstdio/printf/ (0) ->

What happens if I forget to close a scanset?

老子叫甜甜 提交于 2020-01-03 07:18:13
问题 Suppose I forgot to close the right square bracket ] of a scanset. What will happen then? Does it invoke Undefined Behavior? Example: char str[] = "Hello! One Two Three"; char s1[50] = {0}, s2[50] = {0}; sscanf(str, "%s %[^h", s1, s2); /* UB? */ printf("s1='%s' s2='%s'\n", s1, s2); I get a warning from GCC when compiling: source_file.c: In function ‘main’: source_file.c:11:5: warning: no closing ‘]’ for ‘%[’ format [-Wformat=] sscanf(str, "%s %[^h", s1, s2); /* UB? */ and the output as s1=

float variables in C [duplicate]

偶尔善良 提交于 2019-12-31 04:15:08
问题 This question already has answers here : Using %f to print an integer variable (6 answers) Closed 5 months ago . May be this is a simple question but I am not sure about how float variables are stored in memory and why it is behaving in this way, can someone please explain about the following behavior. #include<stdio.h> int main () { int a = 9/5; printf("%f\n", a); return 0; } Output: 0.000000 I have looked at some information on how float variables are stored in memory, it has stuff about

Wrong number of parameters to printf leads to strange results

一笑奈何 提交于 2019-12-31 04:12:07
问题 #include <stdio.h> int main() { int i=10,j=20; printf("%d%d%d",i,j); printf("%d",i,j); return 0; } Using the Turbo C compiler, the output is like: 10 10 garbageValue 20 Can someone explain why this is? 回答1: Which compiler you are using? I tested it on turbo c v2.0 and turbo c++ v 4.5 compilers and the output is 10 20 garbage value 10 This is the actual output you will get as you are using only two variables and three format specifiers. So it will print the values stored in the two variables

Why weren't new (bit width specific) printf() format option strings adoped as part of C99?

别来无恙 提交于 2019-12-29 04:40:26
问题 While researching how to do cross-platform printf() format strings in C (that is, taking into account the number of bits I expect each integer argument to printf() should be) I ran across this section of the Wikipedia article on printf(). The article discusses non-standard options that can be passed to printf() format strings, such as (what seems to be a Microsoft-specific extension): printf("%I32d\n", my32bitInt); It goes on to state that: ISO C99 includes the inttypes.h header file that

Why are the int and float passed in printf going to the wrong positions in the format string?

旧时模样 提交于 2019-12-25 09:29:21
问题 printf function int to %f , float to %d trying to experiment #include<stdio.h> int main(){ int i=10; float x=43.2892f; printf("i=%f x=%d \n",i,x); return 0; } OUTPUT: i=43.289200 x=10 Need help to understand why these variables are interchanging ? 回答1: What you're doing invokes undefined behavior 1 , but looking at the resulting assembly using GCC on a platform with the System V AMD64 ABI we might formulate a hypothesis. The floating-point value is passed in the xmm0 register (an SSE register

Format specifier for templated type

梦想与她 提交于 2019-12-24 13:33:43
问题 I've got a C++ class that's templated on the integer type, e.g., template<typename int_type> Say that somewhere in that class, I'd like to use sscanf for reading some values from a file, e.g., int_type num_rows; fgets( buffer, BUFSIZE, in_file ); sscanf( buffer, "%d", &num_rows); The format specifier only works correctly if the int_type is the intrinsic int . Is there a better way of treating the format specifier for general int_type ? 回答1: Instead of using sscanf() and a format specifier use