<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C)]]></title><description><![CDATA[<p dir="auto">සුභ අලුත් අවුරුද්දක් වේවා හැමෝටම!</p>
<p dir="auto">සමාවෙන්න මේ දවස් ටිකේ පෝස්ට් දාන්න බැරු උනාට. පොඩි වැඩ වගයක හිටියේ. ලොකු වෙලාවක් තිබ්බේ නැති නිසා මන් මේක ඉංග්‍රීසි වලින් ලිව්වේ. ඊලඟ ලිපි අනිවාර්‍යයෙන් සිංහලෙන් එනවා. මේක තේරෙන් නැත්තම් පොඩ්ඩක් කියන්න. මන් සිංහලට පරිවර්තනය කරන්නම්.</p>
<p dir="auto">Many peoples have lot of problems with <code>C</code> format specifiers. Most of them don't know how to use it,<br />
when can use it, ranges of data types and etc. I have studied these format specifiers and it takes few hours to<br />
complete this article.</p>
<p dir="auto">I would like to show these using table and give an example to each format specifier by explain them.</p>
<p dir="auto"><strong>char</strong></p>
<pre><code>Format specifier	Description			Type				range					Uses
	%c		Character			char				-128 to 128				Use to output single character
	%c		Character			unsigned char		        0 to 255				Use to output single character
	%s		String				char * , char []		-					Use to output any range of strings
</code></pre>
<p dir="auto"><strong>Examples</strong></p>
<pre><code>#include &lt;stdio.h&gt;

int main()
{
    char letter1 = 'a';
    unsigned char letter2 = 'a';
    
    char *words1 = "Hello World!";
    char words2[] = "Hello Guys!";
    
    printf("letter 1 = %c", letter1); //Output only one character
    printf("\nletter 2 = %c\n", letter2);

    printf("\n%s", words1); //Output any range of strings
    printf("\n%s", words2);
    
    letter1 = 255;
    letter2 = 255;
    
    printf("\n\nchar          = %d\n", letter1); //Diferrence between char and unsigned char
    printf("Unsigned char = %d\n", letter2);
    
    return 0;
}
</code></pre>
<p dir="auto"><strong>Output</strong></p>
<pre><code>letter 1 = a
letter 2 = a

Hello World!
Hello Guys!

char          = -1
Unsigned char = 255
</code></pre>
<hr />
<p dir="auto"><strong>Integer(short)</strong></p>
<pre><code>Format specifier	Description			Type				range(Uses)	
	%hd		Integer				short int			-32,768 to 32,767
	%hu		"				unsigned short int	              0 to 65,535
</code></pre>
<p dir="auto"><strong>Examples</strong></p>
<pre><code>#include &lt;stdio.h&gt;

int main()
{
    short int min1 = -32768;
    short int max1 = 32767;
    
    unsigned short int min2 = 0;
    unsigned short int max2 = 65535;
  
    printf("Minimum value of short int = %hd", min1); 
    printf("\nMaximum value of short int = %hd", max1); 
    
    printf("\n\nMinimum value of unsigned short int = %hu", min2); 
    printf("\nMaximum value of unsigned short int = %hu", max2); 
    
    return 0;
}
</code></pre>
<p dir="auto"><strong>Output</strong></p>
<pre><code>Minimum value of short int = -32768
Maximum value of short int = 32767

Minimum value of unsigned short int = 0
Maximum value of unsigned short int = 65535
</code></pre>
<hr />
<p dir="auto"><strong>Integer</strong></p>
<pre><code>Format specifier	Description		Type					range						Uses
	%d		Integer			int				-2,147,483,648 to 2,147,483,647		Can take integers as decimals
	%u		"			unsigned int				 0 to 4,294,967,295		Output only positive numbers
	%i		"			unsigned int	                -2,147,483,648 to 2,147,483,647		Can take integers as decimals, 
															hexadecimals and octals type.
</code></pre>
<p dir="auto"><strong>Examples</strong></p>
<pre><code>#include &lt;stdio.h&gt;

int main()
{
    int min1 = -2147483648;
    int max1 = 2147483647;
    
    unsigned int min2 = 0;
    unsigned int max2 = 4294967295;
    
    unsigned int min3 = -2147483648;
    unsigned int max3 = 2147483647;
    
    unsigned int decimal = 12;
    unsigned int octal = 012;
    unsigned int hexadecimal = 0X12;
    
    
    printf("Minimum value of int = %d", min1); 
    printf("\nMaximum value of int = %d", max1);
    
    printf("\n\nMinimum value of unsigned int(u) = %u", min2); 
    printf("\nMaximum value of unsigned int(u) = %u", max2);
    
    printf("\n\nMinimum value of unsigned int(i) = %i", min3); 
    printf("\nMaximum value of unsigned int(i) = %i", max3);
    
    printf("\n\n12 in decimal format       = %i", decimal); 
    printf("\n012 in octal format        = %i", octal);
    printf("\n0X12 in hexadecimal format = %i", hexadecimal);
    
    return 0;
}
</code></pre>
<p dir="auto"><strong>Output</strong></p>
<pre><code>Minimum value of int = -2147483648
Maximum value of int = 2147483647

Minimum value of unsigned int(u) = 0
Maximum value of unsigned int(u) = 4294967295

Minimum value of unsigned int(i) = -2147483648
Maximum value of unsigned int(i) = 2147483647

12 in decimal format       = 12
012 in octal format        = 10
0X12 in hexadecimal format = 18
</code></pre>
<hr />
<p dir="auto"><strong>Integer(long and long long)</strong></p>
<pre><code>Format specifier	Description		Type											range(Uses)
	%ld or %li	Integer			long int							-2,147,483,648 to 2,147,483,647
	%lu		"			unsigned long int					 		     0 to 4,294,967,295
	%lld or %lli	"			long long int			          -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807(-(2^63) to (2^63)-1)
	%llu		"			unsigned long long int				 			 0 to 18,446,744,073,709,551,615 
</code></pre>
<p dir="auto"><strong>Examples</strong></p>
<pre><code>#include &lt;stdio.h&gt;

int main()
{
    long int min1 = -2147483648;
    long int max1 = 2147483647;
    
    unsigned long int min2 = 0;
    unsigned long int max2 = 4294967295;
    
    long long int min3 = -9223372036854775808; // 2^63
    long long int max3 = 9223372036854775807; // 2^63 - 1
    
    unsigned long long int min4 = 0;
    unsigned long long int max4 = 18446744073709551615;

    
    printf("Minimum value of long int(ld) = %ld", min1); 
    printf("\nMaximum value of long int(ld) = %ld", max1);
    
    printf("\n\nMinimum value of long int(li) = %li", min1); 
    printf("\nMaximum value of long int(li) = %li", max1);
    
    printf("\n\nMinimum value of unsigned long int(lu) = %lu", min2); 
    printf("\nMaximum value of unsigned long int(lu) = %lu", max2);
    
    printf("\n\nMinimum value of long long int(lld) = %lld", min3); 
    printf("\nMaximum value of long long int(lld) = %lld", max3);
    
    printf("\n\nMinimum value of long long int(lli) = %lli", min3); 
    printf("\nMaximum value of long long int(lli) = %lli", max3);
    
    printf("\n\nMinimum value of unsigned long long int(llu) = %llu", min4); 
    printf("\nMaximum value of unsigned long long int(llu) = %llu", max4);
    
    return 0;
}
</code></pre>
<p dir="auto"><strong>Output</strong></p>
<pre><code>Minimum value of long int(ld) = -2147483648
Maximum value of long int(ld) = 2147483647

Minimum value of long int(li) = -2147483648
Maximum value of long int(li) = 2147483647

Minimum value of unsigned long int(lu) = 0
Maximum value of unsigned long int(lu) = 4294967295

Minimum value of long long int(lld) = -9223372036854775808
Maximum value of long long int(lld) = 9223372036854775807

Minimum value of long long int(lli) = -9223372036854775808
Maximum value of long long int(lli) = 9223372036854775807

Minimum value of unsigned long long int(llu) = 0
Maximum value of unsigned long long int(llu) = 18446744073709551615
</code></pre>
<hr />
<p dir="auto"><strong>Float, Double and Long Double</strong></p>
<pre><code>Format specifier	Description		Type			Range					Uses
	%f		Float			float			1.2E-38 to 3.4E+38		Use when number has 6 decimal places
	%lf		Double			double			2.3E-308 to 1.7E+308	Use when number has 15 decimal places
	%Lf		Long Double		long Double		3.4E-4932 to 1.1E+4932	Use when number has 19 decimal places
	%e or %E	Float, Double	        float, double					Scientific notation of float values
	%g or %G	Float, Double	        float, double					Scientific notation of float values
												Accept integers and float double also
</code></pre>
<p dir="auto"><strong>Examples</strong></p>
<pre><code>#include &lt;stdio.h&gt;

int main()
{
    float number1 = 5.12;
    double number2 = 5;
    long double number3 = 5;
    
    printf("Float number is(f)        = %f", number1);
    printf("\nDouble number is(lf)      = %lf", number2);
    printf("\nLong Double number is(Lf) = %Lf", number3);
    
    printf("\n\nFloat number is(e)       = %e", number1);
    printf("\nDouble number is(e)      = %e", number2);
    
    printf("\n\nFloat number is(E)       = %E", number1);
    printf("\nDouble number is(E)      = %E", number2);
    
    printf("\n\nFloat number is(g)       = %g", number1); //Support for both integers and floats, doubles
    printf("\nDouble number is(g)      = %g", number2);
    
    printf("\n\nFloat number is(G)       = %G", number1); //Support for both integers and floats, doubles
    printf("\nDouble number is(G)      = %G", number2);
    
    return 0;
}
</code></pre>
<p dir="auto"><strong>Output</strong></p>
<pre><code>Float number is(f)        = 5.120000
Double number is(lf)      = 5.000000
Long Double number is(Lf) = 5.000000

Float number is(e)       = 5.120000e+00
Double number is(e)      = 5.000000e+00

Float number is(E)       = 5.120000E+00
Double number is(E)      = 5.000000E+00

Float number is(g)       = 5.12
Double number is(g)      = 5

Float number is(G)       = 5.12
Double number is(G)      = 5
</code></pre>
<p dir="auto"><strong>Other Specifiers</strong></p>
<pre><code>Format specifier	Description		Type							Uses
	%o		Integer			short int, unsigned short int	Octal representation of Integer.
						int, unsigned int, long int
									
	%x or %X	Integer			short int, unsigned short int   Hexadecimal representation of an Integer.
						int, unsigned int, long int
									
	%p		Void *			void *				Use to get address of pointer or any other variable
										Address of pointer to void void *
																	
	%n		-				-			Prints nothing.It cause printf() to load the variable 
										pointed by corresponding argument. The loading is done with
										a value which is equal to the number of characters printed 
										by printf() before the occurrence of %n.
																	
	%%		-				-			Prints % character.
</code></pre>
<p dir="auto"><strong>Examples</strong></p>
<pre><code>#include &lt;stdio.h&gt;

int main()
{
    int num1 = 65;
    int num2 = 67;
    int num3 = 15;
    
    int* ptr =&amp;num3;
    
    int num4;
    int num5 = 20;
    
    printf("Octal representation of 65 = %o", num1);
    printf("\nOctal representation of 67 = %o", num2);
    
    printf("\n\nHexadecimal representation of 15(x) = %x", num3);
    printf("\nHexadecimal representation of 15(X) = %X", num3);
    
    printf("\n\nAddress of pointer = %p",ptr);
    
    printf("\n\nThe value of %nnum4 and %nnum5 is =  ", &amp;num4, &amp;num5); //Uses of %n
    printf("%d   %d", num4, num5); 
    
    printf("\n\nI got 50%% discount when I was shopping"); //Uses of %%
    
    return 0;
}
</code></pre>
<p dir="auto"><strong>Output</strong></p>
<pre><code>Octal representation of 65 = 101
Octal representation of 67 = 103

Hexadecimal representation of 15(x) = f
Hexadecimal representation of 15(X) = F

Address of pointer = 0x7ffe402adda4 

The value of num4 and num5 is =  15   24

I got 50% discount when I was shopping
</code></pre>
<p dir="auto">I hope this will help who are stuck with format specifiers in c. If I have done any mistakes please mentioned that.</p>
<ul>
<li>C programm එකක් windows වල  run කරන විදිහ --&gt; <a href="https://bit.ly/2O6rLXR" target="_blank" rel="noopener noreferrer nofollow ugc">https://bit.ly/2O6rLXR</a></li>
</ul>
<p dir="auto">ඔබට අවශ්‍යනම් ඔබේ බ්‍රව්සර් එක හරහා online C programms run කරන්න පුලුවන්. එහෙම කරන්න පුලුවන් ලින්ක්ස් මන් පහතින් දාන්නම්</p>
<ul>
<li><a href="https://www.onlinegdb.com/online_c_compiler" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.onlinegdb.com/online_c_compiler</a></li>
<li><a href="https://www.tutorialspoint.com/compile_c_online.php" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.tutorialspoint.com/compile_c_online.php</a></li>
<li><a href="https://repl.it/languages/c" target="_blank" rel="noopener noreferrer nofollow ugc">https://repl.it/languages/c</a></li>
</ul>
<p dir="auto">සරලව මුල ඉදන්  C programming ඉගෙන ගන්න පුලුවන් හොදම site දෙකත් මන් දාන්නම්</p>
<ul>
<li><a href="https://www.geeksforgeeks.org/c-programming-language/" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.geeksforgeeks.org/c-programming-language/</a></li>
<li><a href="https://www.tutorialspoint.com/cprogramming/index.htm" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.tutorialspoint.com/cprogramming/index.htm</a></li>
</ul>
<p dir="auto"><strong>මගේ කලින් ලිපි</strong></p>
<ul>
<li>C Programming මුල සිට ඉගෙනගනිමු(part 1 - Introduction) -: <a href="https://bit.ly/2O6rLXR" target="_blank" rel="noopener noreferrer nofollow ugc">https://bit.ly/2O6rLXR</a></li>
<li>C Programming මුල සිට ඉගෙනගනිමු (part 2 - Variables) -: <a href="https://bit.ly/2spD6Kn" target="_blank" rel="noopener noreferrer nofollow ugc">https://bit.ly/2spD6Kn</a></li>
<li>C Programming මුල සිට ඉගෙනගනිමු (part 3 - Operators) -: <a href="https://bit.ly/2ruMH22" target="_blank" rel="noopener noreferrer nofollow ugc">https://bit.ly/2ruMH22</a></li>
<li>C Programming මුල සිට ඉගෙනගනිමු (part 4 - Input &amp; Output functions) -: <a href="https://bit.ly/2qU8IaK" target="_blank" rel="noopener noreferrer nofollow ugc">https://bit.ly/2qU8IaK</a></li>
<li>C Programming මුල සිට ඉගෙනගනිමු (part 5 - create simple applications) -: <a href="https://bit.ly/2DDfy7b" target="_blank" rel="noopener noreferrer nofollow ugc">https://bit.ly/2DDfy7b</a></li>
<li>C Programming මුල සිට ඉගෙනගනිමු (part 6 - Decision making(if-else statement - part 1)) -: <a href="https://bit.ly/2LiVsmZ" target="_blank" rel="noopener noreferrer nofollow ugc">https://bit.ly/2LiVsmZ</a></li>
<li>C Programming මුල සිට ඉගෙනගනිමු (part 6 - Decision making(if-else statement - part 2)) -: <a href="https://bit.ly/2SJAEJQ" target="_blank" rel="noopener noreferrer nofollow ugc">https://bit.ly/2SJAEJQ</a></li>
</ul>
<p dir="auto">මන් ඊළග ලිපියෙන් කියලා   දෙන්නම් switch statement ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.<br />
තව ලිපියකින් හම්බෙමු ජය වේවා.</p>
]]></description><link>https://lankadevelopers.lk/topic/466/c-programming-ම-ල-ස-ට-ඉග-නගන-ම-part-7-format-specifiers-in-c</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 00:06:34 GMT</lastBuildDate><atom:link href="https://lankadevelopers.lk/topic/466.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 03 Jan 2020 10:13:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C) on Sun, 05 Jan 2020 12:57:13 GMT]]></title><description><![CDATA[<p dir="auto">Happy new year bro , thanks</p>
]]></description><link>https://lankadevelopers.lk/post/2740</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2740</guid><dc:creator><![CDATA[Nubelle]]></dc:creator><pubDate>Sun, 05 Jan 2020 12:57:13 GMT</pubDate></item><item><title><![CDATA[Reply to C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C) on Sun, 05 Jan 2020 08:51:50 GMT]]></title><description><![CDATA[<p dir="auto">good work bro...</p>
]]></description><link>https://lankadevelopers.lk/post/2737</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2737</guid><dc:creator><![CDATA[dev_lak]]></dc:creator><pubDate>Sun, 05 Jan 2020 08:51:50 GMT</pubDate></item><item><title><![CDATA[Reply to C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C) on Fri, 03 Jan 2020 13:32:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://lankadevelopers.lk/uid/27">@root</a> Thank you for sharing with me</p>
]]></description><link>https://lankadevelopers.lk/post/2725</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2725</guid><dc:creator><![CDATA[Kalana]]></dc:creator><pubDate>Fri, 03 Jan 2020 13:32:32 GMT</pubDate></item><item><title><![CDATA[Reply to C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C) on Fri, 03 Jan 2020 12:47:46 GMT]]></title><description><![CDATA[<p dir="auto">good work bro. url short karanna api haduwa shortener ekak eka use karanna. <a href="https://link.lankadevelopers.com/" target="_blank" rel="noopener noreferrer nofollow ugc">https://link.lankadevelopers.com/</a></p>
]]></description><link>https://lankadevelopers.lk/post/2724</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2724</guid><dc:creator><![CDATA[root]]></dc:creator><pubDate>Fri, 03 Jan 2020 12:47:46 GMT</pubDate></item><item><title><![CDATA[Reply to C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C) on Fri, 03 Jan 2020 12:46:02 GMT]]></title><description><![CDATA[<p dir="auto">@Kalana-Eranda-Jayasuriya</p>
<p dir="auto">niyamai bro, keep it up</p>
]]></description><link>https://lankadevelopers.lk/post/2723</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2723</guid><dc:creator><![CDATA[lkdev]]></dc:creator><pubDate>Fri, 03 Jan 2020 12:46:02 GMT</pubDate></item></channel></rss>