{"id":2094,"date":"2024-05-10T07:10:02","date_gmt":"2024-05-10T07:10:02","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2094"},"modified":"2024-05-10T07:10:02","modified_gmt":"2024-05-10T07:10:02","slug":"format-specifiers-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/format-specifiers-in-c\/","title":{"rendered":"Format Specifiers In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#format-specifiers-in-c\">Format Specifiers In C<\/a><ul><li ><a href=\"#list-of-format-specifiers-in-c\">List of Format Specifiers in C<\/a><\/li><li ><a href=\"#examples-of-format-specifiers-in-c\">Examples of Format Specifiers in C<\/a><ul><li ><a href=\"#1-character-format-specifier-c-in-c\">1. Character Format Specifier \u2013 %c in C<\/a><\/li><li ><a href=\"#2-integer-format-specifier-signed-d-in-c\">2. Integer Format Specifier (signed) \u2013 %d in C<\/a><\/li><li ><a href=\"#3-unsigned-integer-format-specifier-u-in-c\">3. Unsigned Integer Format Specifier \u2013 \u00a0%u in C<\/a><\/li><li ><a href=\"#4-floating-point-format-specifier-f-in-c\">4. Floating-point format specifier \u2013 %f in C<\/a><\/li><li ><a href=\"#5-unsigned-octal-number-for-integer-o-in-c\">5. Unsigned Octal number for integer \u2013 %o in C<\/a><\/li><li ><a href=\"#6-unsigned-hexadecimal-for-integer-x-in-c\">6. Unsigned Hexadecimal for integer \u2013 %x in C<\/a><\/li><li ><a href=\"#7-string-format-specifier-s-in-c\">7. String Format Specifier \u2013 %s in C<\/a><\/li><li ><a href=\"#8-address-format-specifier-p-in-c\">8. Address Format Specifier \u2013 %p in C<\/a><\/li><\/ul><\/li><li ><a href=\"#input-and-output-formatting\">Input and Output Formatting<\/a><\/li><li ><a href=\"#example-of-i-o-formatting\">Example Of I\/O Formatting<\/a><\/li><li ><a href=\"#faq-format-specifiers-in-c\">FAQ- Format Specifiers In C<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"format-specifiers-in-c\">Format Specifiers In C<\/h1>\n\n\n\n<p>In the programming world, we often need to be very precise about how data looks and behaves. This is where format specifiers in C come in handy. They are like codes that tell the computer how to show data when we use functions like printf and scanf. These format specifiers help us display different types of data, like numbers or characters, in the right way. Understanding format specifiers in C is essential for anyone who wants to use the language effectively. In this article, we&#8217;ll explore what format specifiers are, what they do, and how to use them to make our programs work just the way we want.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"list-of-format-specifiers-in-c\">List of Format Specifiers in C<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Format Specifier<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><strong>%c<\/strong><\/td><td>For b type.<\/td><\/tr><tr><td><strong>%d<\/strong><\/td><td>For signed integer type.<\/td><\/tr><tr><td><strong>%e or %E<\/strong><\/td><td>For scientific notation of floats.<\/td><\/tr><tr><td><strong>%f<\/strong><\/td><td>For float type.<\/td><\/tr><tr><td><strong>%g or %G<\/strong><\/td><td>For float type with the current precision.<\/td><\/tr><tr><td><strong>%i<\/strong><\/td><td>Unsigned integer<\/td><\/tr><tr><td><strong>%ld or %li<\/strong><\/td><td>Long<\/td><\/tr><tr><td><strong>%lf<\/strong><\/td><td>Double<\/td><\/tr><tr><td><strong>%Lf<\/strong><br><\/td><td>Long double<br><br><\/td><\/tr><tr><td><strong>%lu<\/strong><br><\/td><td>Unsigned int or unsigned long<br><br><\/td><\/tr><tr><td><strong>%lli or %lld<\/strong><br><\/td><td>Long long<br><br><\/td><\/tr><tr><td><strong>%llu<\/strong><br><\/td><td>Unsigned long long<br><br><\/td><\/tr><tr><td><strong>%o<\/strong><br><\/td><td>Octal representation<br><br><\/td><\/tr><tr><td><strong>%p<\/strong><br><\/td><td>Pointer<br><br><\/td><\/tr><tr><td><strong>%s<\/strong><br><\/td><td>String<br><br><\/td><\/tr><tr><td><strong>%u<\/strong><br><\/td><td>Unsigned int<br><br><\/td><\/tr><tr><td><strong>%x or %X<\/strong><br><\/td><td>Hexadecimal representation<br><br><\/td><\/tr><tr><td><strong>%n<\/strong><br><\/td><td>Prints nothing<br><br><\/td><\/tr><tr><td><strong>%%<\/strong><br><\/td><td>Prints % character<br><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"examples-of-format-specifiers-in-c\">Examples of Format Specifiers in C<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-character-format-specifier-c-in-c\">1. Character Format Specifier \u2013 %c in C<\/h3>\n\n\n\n<p>In C language, %c is the code used to specify the format for the char data type. It&#8217;s versatile because you can use it to both input and output char data in a specific format when working with C programs.<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scanf(\"%d...\", ...);\nprintf(\"%d...\", ...);<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to illustrate the %c format specifier.\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    char c;\n    \/\/ using %c for character input\n    scanf(\"Enter some character: %c\", &amp;c);\n \n    \/\/ using %c for character output\n    printf(\"The entered character: %c\", &amp;c);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Input <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter some character: A\n<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The entered character: A\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-integer-format-specifier-signed-d-in-c\">2. Integer Format Specifier (signed) \u2013 %d in C<\/h3>\n\n\n\n<p>You can use the signed integer format specifier %d in functions like scanf() and printf(), as well as in other functions that rely on formatted strings for input and output when working with integer (int) data types.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scanf(\"%d...\", ...);\nprintf(\"%i...\", ...);<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate the use of %d and %i\n#include &lt;stdio.h&gt;\n \n\/\/ Driver code\nint main()\n{\n    int x;\n    \/\/ taking integer input\n    scanf(\"Enter the two integers: %d\", &amp;x);\n \n    \/\/ printing integer output\n    printf(\"Printed using %%d: %d\\n\", x);\n    printf(\"Printed using %%i: %3i\\n\", x);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Input <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the integer: 45\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Printed using %d: 45\nPrinted using %i:    45<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-unsigned-integer-format-specifier-u-in-c\">3. Unsigned Integer Format Specifier \u2013 &nbsp;%u in C<\/h3>\n\n\n\n<p>The %u format specifier is used for unsigned integer data types in C. When you use %u with a negative integer value, it doesn&#8217;t treat it as a negative number but instead converts it to its two&#8217;s complement representation. This means it interprets the value as if it were unsigned.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf(\"%u...\", ...);\nscanf(\"%u...\", ...);<\/code><\/pre>\n\n\n\n<p><strong>Example:&nbsp;<\/strong>The following C Program demonstrates how to use %u in C.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to illustrate the how to use %u\n#include &lt;stdio.h&gt;\n \n\/\/ driver code\nint main()\n{\n    unsigned int var;\n \n    scanf(\"Enter an integer: %u\", &amp;var);\n \n    printf(\"Entered Unsigned Integer: %u\", var);\n \n    \/\/ trying to print negative value using %u\n    printf(\"Printing -10 using %%u: %u\\n\", -10);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Input<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter an integer: 25<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Entered unsigned integer: 25\nPrinting -10 using %u: 4294967286<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-floating-point-format-specifier-f-in-c\">4. Floating-point format specifier \u2013 %f in C<\/h3>\n\n\n\n<p>In the C language, %f is the format specifier used for floating-point data types. You can use it within formatted strings for both input and output when working with float data. Besides %f, you also have the option to use %e or %E as format specifiers to display floating-point values in exponential notation. These format specifiers offer different ways to represent floating-point numbers in your C programs.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf(\"%f...\", ...);\nscanf(\"%e...\", ...);\nprintf(\"%E...\", ...);<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to demonstrate the use of %f, %e  and %E\n#include &lt;stdio.h&gt;\n \n\/\/ driver code\nint main()\n{\n    float a = 12.67;\n    printf(\"Using %%f: %f\\n\", a);\n    printf(\"Using %%e: %e\\n\", a);\n    printf(\"Using %%E, %E\", a);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Using %f: 12.670000\nUsing %e: 1.267000e+01\nUsing %E, 1.267000E+01<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-unsigned-octal-number-for-integer-o-in-c\">5. Unsigned Octal number for integer \u2013 %o in C<\/h3>\n\n\n\n<p>In a C program, you can use the %o format specifier to print or read input for unsigned octal integer numbers. This format specifier is particularly useful when you want to work with numbers in octal (base-8) representation. It allows you to display and handle octal integers effectively in your C code.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf(\"%o...\", ...);\nscanf(\"%o...\", ...);<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\nint main()\n{\n    int a = 67;\n    printf(\"%o\\n\", a);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>103<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-unsigned-hexadecimal-for-integer-x-in-c\">6. Unsigned Hexadecimal for integer \u2013 %x in C<\/h3>\n\n\n\n<p>The %x format specifier is employed in formatted strings for displaying hexadecimal integers. When you use %x, the letters in the hexadecimal representation will be in lowercase. If you want the hexadecimal letters to be in uppercase, you can use %X instead. These format specifiers provide control over the case of letters in hexadecimal numbers when working with C programs.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf(\"%x...\", ...);\nscanf(\"%X...\", ...);<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate the use of %x and %X\n#include &lt;stdio.h&gt;\nint main()\n{\n    int a = 15454;\n    printf(\"%x\\n\", a);\n    printf(\"%X\", a);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3c5e\n3C5E<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-string-format-specifier-s-in-c\">7. String Format Specifier \u2013 %s in C<\/h3>\n\n\n\n<p>In C, the %s format specifier is used for working with strings. You can use it to print strings or read strings as input. It&#8217;s a versatile format specifier for handling character arrays, which represent strings in C, allowing you to display and manipulate textual data in your programs.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf(\"%s...\", ...);\nscanf(\"%s...\", ...);<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint main() {\n    char name&#91;50]; \/\/ Declare a character array to store the string\n    \n    \/\/ Input a string\n    printf(\"Enter your name: \");\n    scanf(\"%s\", name); \/\/ %s format specifier is used to input a string\n    \n    \/\/ Display the entered string\n    printf(\"Hello, %s!\\n\", name); \/\/ %s format specifier is used to print the string\n    \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter your name\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-address-format-specifier-p-in-c\">8. Address Format Specifier \u2013 %p in C<\/h3>\n\n\n\n<p>in the C language, you can use the %p format specifier to print addresses and pointers. This format specifier is particularly useful when you need to display memory addresses or the values stored in pointers in your C programs. <\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf(\"%p...\", ...);\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\nint main()\n{\n    int a = 10;\n    printf(\"The Memory Address of a: %p\\n\",(void*)&amp;a);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The Memory Address of a: 0x7ffe9645b3fc\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"input-and-output-formatting\">Input and Output Formatting<\/h2>\n\n\n\n<p>In C language, you have the tools to format input and output effectively. These formatting options are typically inserted between the % sign and the format specifier symbol in printf and scanf functions. Here are some of these formatting tools:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Minus (-) Sign for Left Alignment<\/strong>: When you include a minus (-) sign, it signifies left alignment. This means that the data will be printed or read with the left edge as the reference point, and any extra space will be added on the right side if needed.<\/li>\n\n\n\n<li><strong>Number for Minimum Field Width<\/strong>: You can specify a number after the % sign, which sets the minimum field width for the printed or read data. If the characters in the data are fewer than the specified width, spaces will be added to meet the minimum width. Conversely, if the data is larger, it will be printed or read as is without truncation.<\/li>\n\n\n\n<li><strong>Period (.) for Precision<\/strong>: The period (.) symbol is used to separate the field width from precision. This is often used with format specifiers for floating-point numbers (e.g., %f). Precision allows you to specify the number of decimal places to display for floating-point values.<\/li>\n<\/ol>\n\n\n\n<p>These formatting options in C provide control over how data is presented and handled in input and output operations, making it easier to create well-formatted and readable programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-i-o-formatting\">Example Of I\/O Formatting<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate the formatting methods.\n#include &lt;stdio.h&gt;\nint main()\n{\n    char str&#91;] = \"skill vertex\";\n    printf(\"%20s\\n\", str);\n    printf(\"%-20s\\n\", str);\n    printf(\"%20.5s\\n\", str);\n    printf(\"%-20.5s\\n\", str);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skill vertex\nSkill vertex    \n               skill vertex \nSkill vertex          <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-format-specifiers-in-c\">FAQ- Format Specifiers In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1695638051182\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are format specifiers in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. They are indeed used to specify the type of data that should be input or output in a program. The % symbol, followed by a character that represents the data type (e.g., %d for integers, %f for floating-point numbers), is used to define these format specifiers. They play a crucial role in informing the compiler about how to handle and display different types of data in C programs.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695638061707\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How many types of format specifiers are there in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. There are basically six types of format specifiers in C<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695638070416\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is %F and %d in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>%d<\/strong>: This format specifier is used for integers. It is used when you want to print or read integer values, which can be positive or negative whole numbers (e.g., -3, -100, 3, 100).<br \/><strong>%f<\/strong>: This format specifier is used for floating-point numbers. It is used for numbers with decimal points (e.g., 10.6, -39.0).<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Format Specifiers In C In the programming world, we often need to be very precise about how data looks and behaves. This is where format specifiers in C come in handy. They are like codes that tell the computer how to show data when we use functions like printf and scanf. These format specifiers help &#8230; <a title=\"Format Specifiers In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/format-specifiers-in-c\/\" aria-label=\"More on Format Specifiers In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5312,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[370],"class_list":["post-2094","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-format-specifiers-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2094"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2094\/revisions"}],"predecessor-version":[{"id":10627,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2094\/revisions\/10627"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5312"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}