C Function Printf
Title: C Library Function β printf() | Novice Tutorial\\n\\n[ C Standard Library - ](#)\\n\\n## Description\\n\\nThe C library function **int printf(const char *format, ...)** sends formatted output to the standard output stdout.\\n\\nThe call format for the printf() function is:\\n\\nprintf("", );\\n## Declaration\\n\\nThe declaration of the printf() function is as follows.\\n\\nint printf(const char *format, ...)\\n## Parameters\\n\\n* **format** -- This is a string that contains the text to be written to stdout. It can contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as needed. The properties of the format tag are **%[.precision]specifier**, explained in detail below:\\n\\n| Format Specifier | Meaning |\\n| --- | --- |\\n| a, A | Output floating-point numbers in hexadecimal form (C99 addition). Example printf("pi=%an", 3.14); outputs **pi=0x1.91eb86p+1**. |\\n| |\\n| d | Output signed integer in decimal form (no sign for positive numbers) |\\n| o | Output unsigned integer in octal form (no prefix 0) |\\n| x,X | Output unsigned integer in hexadecimal form (no prefix Ox) |\\n| u | Output unsigned integer in decimal form |\\n| f | Output single or double precision real numbers in decimal form |\\n| e,E | Output single or double precision real numbers in exponential form |\\n| g,G | Output single or double precision real numbers using the shorter output width of %f or %e |\\n| c | Output a single character |\\n| s | Output a string |\\n| p | Output pointer address |\\n| lu | 32-bit unsigned integer |\\n| llu | 64-bit unsigned integer |\\n\\n| flags | Description |\\n| --- | --- |\\n| - | Left-align within the given field width. Default is right alignment (see width sub-specifier). |\\n| + | Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceeded by a - sign. |\\n| (space) | If no sign is written, a space is inserted before the value. |\\n| # | Used with o, x, or X specifiers, the value is preceeded with 0, 0x, or 0X respectively for non-zero values. Used with e, E, and f, it forces the output to contain a decimal point even when no digits follow. By default, if no digits follow, no decimal point is displayed. Used with g or G, the result is the same as with e or E, but trailing zeros are not removed. |\\n| 0 | Left-pads the number with zeroes (0) instead of spaces where padding is specified (see width sub-specifier). |\\n\\n| width | Description |\\n| --- | --- |\\n| (number) | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger. |\\n| * | The width is not specified in the format string, but as an additional integer value argument preceeding the argument that has to be formatted. |\\n\\n| .precision | Description |\\n| --- | --- |\\n| .number | For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For e, E, and f specifiers: this is the number of digits to be printed after the decimal point. For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default, all characters are printed until the terminating null character is encountered. For c type: it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed. |\\n| .* | The precision is not specified in the format string, but as an additional integer value argument preceeding the argument that has to be formatted. |\\n\\n| length | Description |\\n| --- | --- |\\n| h | The argument is interpreted as a short int or unsigned short int (only applicable to integer specifiers: i, d, o, u, x, and X). |\\n| l | The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x, and X), and as a wide character or wide character string for specifiers c and s. |\\n| L | The argument is interpreted as a long double (only applicable to floating-point specifiers: e, E, f, g, and G). |\\n\\n* **additional arguments** -- Depending on the format string, the function may require a sequence of additional arguments, each containing a value to be inserted, replacing each % tag specified in the format parameter. The number of arguments should match the number of % tags.\\n\\n## Return Value\\n\\nIf successful, returns the total number of characters written. Otherwise, returns a negative number.\\n\\n## Examples\\n\\nThe following example demonstrates the usage of the printf() function.\\n\\n## Example\\n\\n#includeint main(){int ch; for(ch = 75 ; ch<= 100; ch++ ){printf("ASCII Value = %d, Character = %cn", ch , ch); }return(0); }\\n\\nLet's compile and run the above program, which will produce the following result:\\n\\nASCII Value = 75, Character = K ASCII Value = 76, Character = L ASCII Value = 77, Character = M ASCII Value =Are they all in English? This output is in English, should be kept as is.\\n ASCII Value = 78, Character = N ASCII Value = 79, Character = O ASCII Value = 80, Character = P ASCII Value = 81, Character = Q ASCII Value = 82, Character = R ASCII Value = 83, Character = S ASCII Value = 84, Character = T ASCII Value = 85, Character = U ASCII Value = 86, Character = V ASCII Value = 87, Character = W ASCII Value = 88, Character = X ASCII Value = 89, Character = Y ASCII Value = 90, Character = Z ASCII Value = 91, Character = [ ASCII Value = 92, Character = ASCII Value = 93, Character = ] ASCII Value = 94, Character = ^ ASCII Value = 95, Character = _ ASCII Value = 96, Character = ` ASCII Value = 97, Character = a ASCII Value = 98, Character = b ASCII Value = 99, Character = c ASCII Value = 100, Character = d\\nThe following example outputs various formatted data:\\n\\n## Example\\n\\n#includeint main(){char ch = 'A'; char str = "www..com"; float flt = 10.234; int no = 150; double dbl = 20.123456; printf("Character is %c n", ch); printf("String is %s n" , str); printf("Float value is %f n", flt); printf("Integer is %dn" , no); printf("Double value is %lf n", dbl); printf("Octal value is %o n", no); printf("Hexadecimal value is %x n", no); return 0; }\\n\\nThe execution output is:\\n\\nCharacter is A String is www..com Float value is 10.234000 Integer is 150Double value is 20.123456 Octal value is 226 Hexadecimal value is 96 \\n### Format Specifiers\\n\\n* **%d** Signed decimal integer\\n* **%u** Unsigned decimal integer\\n* **%f** Floating-point number\\n* **%s** String\\n* **%c** Single character\\n* **%p** Pointer value\\n* **%e** Floating-point number in exponential notation\\n* **%x, %X** Unsigned integer in hexadecimal notation\\n* **%o** Unsigned integer in octal notation\\n* **%g** Output the value in the shorter format of %e or %f\\n* **%p** Output address specifier\\n* **%lu** 32-bit unsigned integer\\n* **%llu** 64-bit unsigned integer\\n* **%%** Output the percent sign itself.\\n\\nBesides format specifiers, the printf() function also supports some flags and options to control output precision, width, padding characters, and alignment. For example:\\n\\n* %-10s: Left-aligned string with a width of 10;\\n* %5.2f: Right-aligned floating-point number with a width of 5 and 2 decimal places;\\n* %#x: Output hexadecimal number with a 0x prefix.\\n\\nThe printf() function also supports variable argument lists, indicated by an ellipsis ..., used to specify multiple values to output. Inside the function, the va_start() and va_end() macros can be used to access values within the variable argument list. For example:\\n\\n## Example\\n\\n#include \\n\\n#include \\n\\nvoid print_values(int count, ...)\\n\\n{\\n\\n va_list args;\\n\\nva_start(args, count);\\n\\nfor(int i =0; i < count; i++){\\n\\nprintf("%d ",va_arg(args,int));\\n\\n}\\n\\nva_end(args);\\n\\n}\\n\\nint main()\\n\\n{\\n\\n print_values(3,1,2,3);// Outputs "1 2 3"\\n\\nreturn 0;\\n\\n}\\n\\nThe execution output is:\\n\\n1 2 3\\n[ C Standard Library - ](#)
YouTip