"5.4 Controlling Numeric Output with print
...
The built-in variable OFMT contains the default format specification that print uses with sprintf when it wants to convert a number to a string for printing. The default value of OFMT is "%.6g". The way print prints numbers can be changed by supplying different format specifications as the value of OFMT, as shown in the following example:
$ awk 'BEGIN {
> OFMT = "%.0f" # print numbers as integers (rounds)
> print 17.23, 17.54 }'
-| 17 18
According to the POSIX standard, awk's behavior is undefined if OFMT contains anything but a floating-point conversion specification. (d.c.)"
from The GNU Awk User's Guide
'via Blog this'
Showing posts with label printf. Show all posts
Showing posts with label printf. Show all posts
Saturday, March 9, 2013
Thursday, November 1, 2012
%#x (or %#llx), in C language format string - Stack Overflow and wpollock
I was looking up %#llx (which I found nowhere, even using SymbolHound). But this is close enough (for x, # causes value to be prepended by 0x). It's a new one on me :-)
Do you know about %#x, in C language format string - Stack Overflow
And more specifically related to %#llx (new as of C99):
http://wpollock.com/CPlus/PrintfRef.htm
'via Blog this'
It's weird. What does sharp mean?
| |||
feedback
|
The printf documentation says:
MSDN docs on the flags are here
so %#x the value is simply prefixed with 0x. Where %x would yield 34ab %#x would yield 0x34ab
| |||
Was this post useful to you?
|
Do you know about %#x, in C language format string - Stack Overflow
And more specifically related to %#llx (new as of C99):
| printf( "%#llX", 300 ); | 0X12C |
http://wpollock.com/CPlus/PrintfRef.htm
'via Blog this'
Wednesday, September 5, 2012
Should I use printf in my C++ code? - Stack Overflow
Should I use printf in my C++ code? - Stack Overflow
I was just wondering what people really do if they have to print a lot of hex in their C++ code, and if in addition, they say always want to see a minimum width, which I find ugly and very verbose and error-prone with the cout/streams format.
(e.g., std::cout << std::hex << std::setfill('0') << std::setw(8) << x << std::dec << std::endl; )
I feel like there must be a good third solution, or people just use printf, or ???. I read the above article, and about Boost.Format and all, which looks sorta interesting (although the examples they show don't seem all that brilliant for hex either, to me). And people point out that for streams, "endl" implies a flush(), which you would otherwise have to do manually... But maybe I liked this answer best :-) :
From the referenced link ( http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Streams#Streams ):
There are various pros and cons to using streams, but in this case, as in many other cases, consistency trumps the debate. Do not use streams in your code.
Proponents of streams have argued that streams are the obvious choice of the two, but the issue is not actually so clear. For every advantage of streams they point out, there is an equivalent disadvantage. The biggest advantage is that you do not need to know the type of the object to be printing. This is a fair point. But, there is a downside: you can easily use the wrong type, and the compiler will not warn you. It is easy to make this kind of mistake without knowing when using streams.
The compiler does not generate an error because
Some say
And so on and so forth for any issue you might bring up. (You could argue, "Things would be better with the right wrappers," but if it is true for one scheme, is it not also true for the other? Also, remember the goal is to make the language smaller, not add yet more machinery that someone has to learn.)
Either path would yield different advantages and disadvantages, and there is not a clearly superior solution. The simplicity doctrine mandates we settle on one of them though, and the majority decision was on
...
;-)
Happy Coding,
Connie
'via Blog this'
I was just wondering what people really do if they have to print a lot of hex in their C++ code, and if in addition, they say always want to see a minimum width, which I find ugly and very verbose and error-prone with the cout/streams format.
(e.g., std::cout << std::hex << std::setfill('0') << std::setw(8) << x << std::dec << std::endl; )
I feel like there must be a good third solution, or people just use printf, or ???. I read the above article, and about Boost.Format and all, which looks sorta interesting (although the examples they show don't seem all that brilliant for hex either, to me). And people point out that for streams, "endl" implies a flush(), which you would otherwise have to do manually... But maybe I liked this answer best :-) :
Use printf. Do not use C++ streams. printf gives you much better control (such as float precision etc.). The code is also usually shorter and more readable.
Google C++ style guide agrees.
| |||||||
|
Streams
link▽
Use streams only for logging.
Definition:Streams are a replacement for
printf() and scanf().
Pros:With streams, you do not need to know the type of the object you are printing. You do not have problems with format strings not matching the argument list. (Though with gcc, you do not have that problem with
printf either.) Streams have automatic constructors and destructors that open and close the relevant files.
Cons:Streams make it difficult to do functionality like
pread(). Some formatting (particularly the common format string idiom %.*s) is difficult if not impossible to do efficiently using streams without using printf-like hacks. Streams do not support operator reordering (the %1s directive), which is helpful for internationalization.
Decision:
Do not use streams, except where required by a logging interface. Use printf-like routines instead.There are various pros and cons to using streams, but in this case, as in many other cases, consistency trumps the debate. Do not use streams in your code.
Extended Discussion
There has been debate on this issue, so this explains the reasoning in greater depth. Recall the Only One Way guiding principle: we want to make sure that whenever we do a certain type of I/O, the code looks the same in all those places. Because of this, we do not want to allow users to decide between using streams or using printf plus Read/Write/etc. Instead, we should settle on one or the other. We made an exception for logging because it is a pretty specialized application, and for historical reasons.Proponents of streams have argued that streams are the obvious choice of the two, but the issue is not actually so clear. For every advantage of streams they point out, there is an equivalent disadvantage. The biggest advantage is that you do not need to know the type of the object to be printing. This is a fair point. But, there is a downside: you can easily use the wrong type, and the compiler will not warn you. It is easy to make this kind of mistake without knowing when using streams.
cout << this; // Prints the address cout << *this; // Prints the contents
<< has been overloaded. We discourage overloading for just this reason.Some say
printf formatting is ugly and hard to read, but streams are often no better. Consider the following two fragments, both with the same typo. Which is easier to discover?cerr << "Error connecting to '" << foo->bar()->hostname.first
<< ":" << foo->bar()->hostname.second << ": " << strerror(errno);
fprintf(stderr, "Error connecting to '%s:%u: %s",
foo->bar()->hostname.first, foo->bar()->hostname.second,
strerror(errno));
Either path would yield different advantages and disadvantages, and there is not a clearly superior solution. The simplicity doctrine mandates we settle on one of them though, and the majority decision was on
printf + read/write....
If you need some further ammo, I also liked this guy, he is like a C++ anarchist, but he has some fair points:
http://yosefk.com/c++fqa/io.html#fqa-15.1
...
http://yosefk.com/c++fqa/io.html#fqa-15.1
...
FQA: Why should I do this, why should I do that, you ask. What kind of manners are these? Do what you are told. Assignment Number 1 - convert all your evil printf("0x%08xn", x)statements to this:
std::cout << std::hex << std::setfill('0') << std::setw(8) << x << std::dec << std::endl;
Even if you commit the environmental crime of namespace pollution, adding a using namespace std and removing those pesky std::, the verbosity is still amazing. This achievement is not accidental. It follows from one of the basic flaws in the C++ way of thinking: the "everything is a type" axiom. For example, hex has a special type which hexes streams, and so does every other strange object sent to cout.
The FAQ explains why this thinking is good for you. Here's why the FAQ is wrong:
...
;-)
Happy Coding,
Connie
'via Blog this'
Monday, July 30, 2012
Using awk --non-decimal-data for hex calculations... - Stack Overflow
Converting hex to decimal in awk or sed - Stack Overflow:
...
'via Blog this'
I have a list of numbers, comma-separated:
I need to split the 3rd column into three as below:
And convert the digits in the last two columns into decimal:
| |||||
feedback
|
Here's a variation on Jonathan's answer:
I included a rather contorted way of adding the
--non-decimal-data option if it's needed. |
'via Blog this'
Subscribe to:
Posts (Atom)