How To Set Precision In C++
The field width and precision setting may be used for characters and strings every bit well. All the same, these have different meaning. See the following code:
char kh [] ="character String";
printf("%-xxx.10s\north",kh);
The above code would print x characters of variable kh in width 30 with left justification. It will print the following:
character
Program illustrates the width and precision setting with numerical data besides as characters and strings:
Illustrates width and precision specification with character and string type data.
#include <stdio.h> void chief() { char kh = 'X'; char ch []= "You should master C language"; clrscr(); printf("|%20c|\n", kh); //field width twenty, right justification. printf("|%-20c|\n", kh); // field width 20, left justification. printf("|%10c|\n", kh);//field width ten, correct justification printf("|%-40s %5c|\northward", ch, kh); printf("|%-40s %-5c|\northward", ch, kh); printf("|%40s|\n", ch); printf ("|%-forty.l7s |\n", ch); printf("|%-40.10s|\n", ch); /*fieldwidth 40, precision 10, left justified*/ printf("|%-twoscore.4s|\north", ch); //field width forty,precision 4, left justified printf("|%-.10s|\northward", ch); //no field width, precision 10 left }
The output is given below.
Explanation of the output: Vertical confined are introduced so that the extent of width setting can be noticed. The showtime line of the output is the result of width setting 20 with correct (default) justification. The second line of the output is result of the same width setting with left justification and the 3rd line is the event of the width setting 10 with default justification. The 4th line of output is the result of width setting -40 for string ch and width setting v for kh with value 10. And so, afterwards display of ch, Ten is displayed with right justification on the side by side five spaces. For the fifth line, the justification of kh is changed to left so it is displayed immediately after xl spaces. Similarly, in sixth line of the output, the cord ch is displayed in forty spaces with right justification.
The next four lines of output are the result of different precision settings. The seventh line of the output is the upshot of"%-40.17s". It displays 17 characters in width of 40 spaces with left justification. Similarly, in the side by side line the setting "%-40.10s"displays only 10 characters in width twoscore and left justification due to –ve sign. For the concluding merely one line of the output the precision setting is .4 so only 4 characters, i.e., "You" are displayed. If the setting is but"%-. x southward \northward", the brandish consists of x characters, i.due east., "You should" and information technology fills the entire space and there is no empty space because no width specification has been specified.
Program is another example of output of characters and strings values with formatting office.
Illustrates formatting with characters and strings.
#include<stdio.h> #include<stdlib.h> int main() { char ch = 'x'; char kh[] ="grapheme string"; clrscr(); printf("%5c%5c\north",ch,ch); printf("%-5c%-5c\n",ch,ch); printf ("%s\north", kh); printf("%20.4s\n",kh); /*print four characters of kh on right.*/ printf("%-20.4s\northward",kh); /*print 4 characters of kh on left.*/ printf("%xxx.10s\n",kh); /*The above code means print first10 characters of kh in width 30, with right justification.*/ printf("%-xxx.10s\n",kh); /*The above code means print x characters of kh in width 30 with left justification.*/ return 0; }
The expected output is given below
Caption of the output: The first line of the output is the effect of setting field width v and right justification. The second line of the output is due to same field width but with left justification. For this, -ve sign is put between % and conversion character c.
The tertiary line is simply an output of the string with the default setting in the calculator. For the quaternary line of the output, the field width set up is 20; still, due to the precision setting .4, it displays simply four characters with right justification. The next line of the output is the same setting simply with negative sign, i.e., left justification. The last but one and the final lines of output are with width setting of 30 and to print 10 characters with correct and left justification, respectively.
Source: https://ecomputernotes.com/what-is-c/types-and-variables/precision-for-characters-and-strings

0 Response to "How To Set Precision In C++"
Post a Comment