C printf Format Specifier %_fi - objective-c

Does anyone know if an underscore as format specifier (like %_f) does anything in C code?
I+ve found some code in a book that uses it, but I've googled and found nothing. I've also tested the following Objective-C code in Xcode and Xcode seems not to support this specifier. Is it valid in C?
-(void) print {
printf( "%_f + %_fi", real, imaginary );
}
Thanks!

The OS X man page for printf() says this:
An optional separator character ( , | ; | : | _ ) used for separating multiple values when printing an AltiVec or SSE vector,
or other multi-value unit.
NOTE: This is an extension to the printf() specification.
Behaviour of these values for printf() is only defined for operating
systems conforming to the AltiVec Technology Programming Interface
Manual. (At time of writing this includes only Mac OS X 10.2 and
later.)

Related

How do I print text in Common Lisp so that I could format it with escape sequences (akin to display in Racket)?

Ho do I print a formatted output in Common Lisp?
In Racket I do it with display, like so:
(display "\33[3min italics\33[m\n")
I've tried with (format t "~ain italics~a" "\33[3m" "\33[m") but it does not work. Neither does this: (format t "~cin italics~c" #\33[3m #\33[m).
The main issue here is how to get the proper sequence of characters. \33 is octal for ascii char 27 or #\Esc in Common Lisp.
(format t "~C[3min italics~C[m~%" #\Esc #\Esc)
would do what you want.
But you could do better than that. There is a library called cl-interpol which demonstrates flexibility of Common Lisp by modifying the reader so you could use the already familiar syntax.
For example:
* (ql:quickload 'cl-interpol)
To load "cl-interpol":
Load 1 ASDF system:
cl-interpol
; Loading "cl-interpol"
...
(CL-INTERPOL)
* (named-readtables:in-readtable :interpol-syntax)
#<NAMED-READTABLE :INTERPOL-SYNTAX {1002E6D6B3}>
* (format t #?"\33[3min italics\33[m\n")
in italics
NIL

End-of-line conversion during Input/Output for text files

How to write strings (&str and String) containing newlines to text files?
In C you can switch between writing text as is or converting '\n' to proper end of line symbol for the OS via fopen flags, "w" or "wb". For example in Windows '\n' is converted to "\r\n" during I/O.
How can I achieve this with Rust? I cannot find corresponding API in std::fs::File.
There is no such API in the standard library (there might be a crate for this, though). The simplest way to write lines to a file is with the writeln! macro and it only uses \n for newlines.
It was probably considered (by the Rust developers) not useful enough because I'm pretty sure that nowadays \r\n is used only for Microsoft Notepad compatibility.
There once was an issue related to write not using CRLF on Windows, but it was concluded that:
the raw io::File will likely not handle it by default but would instead require a wrapper
(note: since Rust 1.0 it is no longer io::File, but fs::File)

How to set lang env to "en_US.UTF-8" in awk script?

I am using awkc to generate an executable file from an awk script. I have the following line in an awk script abc.awk:
BEGIN{printf "Value=%s\n",(3.13+3.26)}
I have generated an executable file (abc.exe), which I have executed on different systems. It gives different outputs in floating point operations.
On one local system it gives the output 6.39 but it gives the output 6 on another system located in a different time zone.
When I searched in various sites I am able to see to set the LANG environmental variable, but how?
I'm not sure that this to do with the locale settings on your different systems but if you are looking for a floating-point number, you should use the "%f" format specifier. To get an answer to 2 decimal places, use "%.2f":
BEGIN{printf "Value=%.2f\n",(3.13+3.26)}
This should give the same result, regardless of the system it is run on.
Edit: based on the linked question, perhaps you should try using LC_NUMERIC=C to explicitly set the locale:
LC_NUMERIC=C awk 'BEGIN{printf "Value=%.2f\n",(3.13+3.26)}'
should work regardless of the system it is being run on.
Environment variables are typically something that you do not control if you hand a program over to someone else. I would look into being more specific in your printf-calls. You can format the numbers into strings in to an astonishing detail. You are probably looking for the float (%f) (you are now using a string (%s).
If you notice that you are making always the same trick with your printf-calls, you can control the number-to-string conversion settings with a variable CONVFMT in a BEGIN-block. I got introduced to this one through gawk, apparently it is however in the POSIX standard. Whether awkc supports this, I don't know.
From the gawk manual:
This string controls conversion of numbers to strings (see
Conversion). It works by being passed, in effect, as the first
argument to the sprintf() function (see String Functions). Its default
value is "%.6g". CONVFMT was introduced by the POSIX standard.

Can I make printf format floats like C++ streams

I am comparing the output of two programs, one C the other C++, using diff, so the output must be identical.
Is there any way to printf a double so that it is formatted as though it was printed using << mydouble.
I am currently using printf("%g",mydouble)
Here are some examples of the differences:
c: 3.24769e-05 c++: 3.2477e-05
c: 0.0026572 c++: 0.00265721
Interestingly the scientific notation has more digits in c, and the decimal notation has more in c++.
You can solve this by using the format specifiers in C.
For example, say you would like to print out only 3 places after the decimal, you could make your printf like so:
printf("%.3lf", dub);
With a value of double dub = .0137; the output would be 0.014
This would fix the issue with your 2nd case if you want more precision printed you could write:
printf("%.8lf", dub);
Your output for double dub = 0.00265721; would then be 0.00265721
The case for %g works the same way except the number on the left is included in the calculation. If you wanted the C++ version (the lesser precision I assume) then your code would look like this:
double dub = .0000324769;
printf("%.5g", dub);
Which yields 3.2477e-05

How to print formatted float in obj-c?

How to print a float in Objective-C as, for example, 3.45 instead of 3.45555555555?
Try formatting the float like this:
NSLog(#"%.2f", myFloat);
The % sign means this will be replaced by the corresponding argument following (myFloat). The .2 means 2 decimal places, and f means a float datatype.
Take a look here for more detail.
Objective-C's NSLog is very similar to C's printf, with the main exceptions being that you must use an Objective-C string literal (#"…") and you should use %# for Objective-C strings (NSStrings) rather than %s, which is for "Plain C strings".
Depends on how you're printing it. If you want to show it in a GUI (which is probably the common case for Cocoa and Cocoa Touch apps), use an NSNumberFormatter and set it to have two decimal places. If you're printing it through NSLog() or printf(), you'd use a format specifier along the lines of "%.2f".