My recommendation: just use %g, and it will usually do what you want: Okay, that wasn't too bad was it? But that chart is kind of complicated. Like g, except uses the shorter of f or E Use shorter of the two representations: f or e Like e, but with a capital E in the output Conversion specifiers for floating point numbersĭisplaying floating point numbers has a ton of different options, best shown in a table: Specifierĭisplay the floating point number using decimal representationĭisplay the floating point number using scientific notation with e Want capital letters (A instead of a when printing out decimal 10) then you can Hexadecimal you'd use o for octal, or x for hexadecimal. ![]() If you want to print an integer in octal or If you want to print a decimal integer number in base 0, you'd use eitherĭ or i: %d or %i. The conversion specifier is the part of the format specifier that determines the basic formatting of the value that is to be printed. So let's begin at the end! Conversion Specifier Understanding this formatting is best done by working backward, starting with the conversion specifier and working outward. Most of these fields are optional, other than providing a conversion specifier, which you've already seen (for example, using %d to print out a decimal number). The format for what appears about a % sign is: You put after the % sign, which is the actual format. In order to actually get that number, we need toĪll of the interesting formatting that you can do involves changing the values The part of the string that begins with % isĬalled the format specifier. The % sign indicates that weĪre splicing some data into the string, and the d character indicates that weĪre splicing in a decimal number. In the string needs to be taken from a variable. In this string, the %d indicates that the value to be displayed at that point If you want to introduce some variance into the output, you do so by indicating that external data is needed: For example, \130 will print out an character (in some cases, depending on what your machine is set up to do with extended ASCII characters.) Format Specifiers This is useful if you want to display a character that you can't easily type on your keyboard, such as accented letters. There's one other advanced trick, which is that you can write \ to display the ASCII character represented by the value num. Is how you'd write a Windows path in C . Since a backslash normally indicates the start of an escape sequence, if you want to put in an escape sequence you need to use \\ to display a backslash: That you'll use are: \n, to put a newline, and \t, to put in a tab. ![]() ![]() ![]() Sequence starts with a backslash ('\') character. Here, I've entered the newlines between each letter, a, b and c. These are called escape sequences and look like this: These are characters like a newline, which must be represented using some special syntax. There are some characters that you cannot directly enter into a string. While it is sometimes enough to literally write into your code exactly what you want to print, you usually want to do something fancier-either introducing special characters using escape sequences or introducing variable values using format specifiers. When you make a call to printf, the basic idea is that you are going to provide a string of characters that has some literal characters and some elements that are to be replaced. I won't include every single possible option-instead, my goal is to make it easy to understand the mini-language that you can use for creating format strings and teach you how to use the common formatting you're most likely to need. Let's look at the anatomy of a format string followed by some short example programs to show the different settings in action.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |