C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C)



  • සුභ අලුත් අවුරුද්දක් වේවා හැමෝටම!

    සමාවෙන්න මේ දවස් ටිකේ පෝස්ට් දාන්න බැරු උනාට. පොඩි වැඩ වගයක හිටියේ. ලොකු වෙලාවක් තිබ්බේ නැති නිසා මන් මේක ඉංග්‍රීසි වලින් ලිව්වේ. ඊලඟ ලිපි අනිවාර්‍යයෙන් සිංහලෙන් එනවා. මේක තේරෙන් නැත්තම් පොඩ්ඩක් කියන්න. මන් සිංහලට පරිවර්තනය කරන්නම්.

    Many peoples have lot of problems with C format specifiers. Most of them don't know how to use it,
    when can use it, ranges of data types and etc. I have studied these format specifiers and it takes few hours to
    complete this article.

    I would like to show these using table and give an example to each format specifier by explain them.

    char

    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
    

    Examples

    #include <stdio.h>
    
    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;
    }
    

    Output

    letter 1 = a
    letter 2 = a
    
    Hello World!
    Hello Guys!
    
    char          = -1
    Unsigned char = 255
    

    Integer(short)

    Format specifier	Description			Type				range(Uses)	
    	%hd		Integer				short int			-32,768 to 32,767
    	%hu		"				unsigned short int	              0 to 65,535
    

    Examples

    #include <stdio.h>
    
    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;
    }
    

    Output

    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
    

    Integer

    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.
    

    Examples

    #include <stdio.h>
    
    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;
    }
    

    Output

    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
    

    Integer(long and long long)

    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 
    

    Examples

    #include <stdio.h>
    
    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;
    }
    

    Output

    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
    

    Float, Double and Long Double

    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
    

    Examples

    #include <stdio.h>
    
    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;
    }
    

    Output

    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
    

    Other Specifiers

    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.
    

    Examples

    #include <stdio.h>
    
    int main()
    {
        int num1 = 65;
        int num2 = 67;
        int num3 = 15;
        
        int* ptr =&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 =  ", &num4, &num5); //Uses of %n
        printf("%d   %d", num4, num5); 
        
        printf("\n\nI got 50%% discount when I was shopping"); //Uses of %%
        
        return 0;
    }
    

    Output

    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
    

    I hope this will help who are stuck with format specifiers in c. If I have done any mistakes please mentioned that.

    ඔබට අවශ්‍යනම් ඔබේ බ්‍රව්සර් එක හරහා online C programms run කරන්න පුලුවන්. එහෙම කරන්න පුලුවන් ලින්ක්ස් මන් පහතින් දාන්නම්

    සරලව මුල ඉදන් C programming ඉගෙන ගන්න පුලුවන් හොදම site දෙකත් මන් දාන්නම්

    මගේ කලින් ලිපි

    • C Programming මුල සිට ඉගෙනගනිමු(part 1 - Introduction) -: https://bit.ly/2O6rLXR
    • C Programming මුල සිට ඉගෙනගනිමු (part 2 - Variables) -: https://bit.ly/2spD6Kn
    • C Programming මුල සිට ඉගෙනගනිමු (part 3 - Operators) -: https://bit.ly/2ruMH22
    • C Programming මුල සිට ඉගෙනගනිමු (part 4 - Input & Output functions) -: https://bit.ly/2qU8IaK
    • C Programming මුල සිට ඉගෙනගනිමු (part 5 - create simple applications) -: https://bit.ly/2DDfy7b
    • C Programming මුල සිට ඉගෙනගනිමු (part 6 - Decision making(if-else statement - part 1)) -: https://bit.ly/2LiVsmZ
    • C Programming මුල සිට ඉගෙනගනිමු (part 6 - Decision making(if-else statement - part 2)) -: https://bit.ly/2SJAEJQ

    මන් ඊළග ලිපියෙන් කියලා දෙන්නම් switch statement ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.
    තව ලිපියකින් හම්බෙමු ජය වේවා.



  • @Kalana-Eranda-Jayasuriya

    niyamai bro, keep it up


  • Linux Help

    good work bro. url short karanna api haduwa shortener ekak eka use karanna. https://link.lankadevelopers.com/



  • @root Thank you for sharing with me



  • good work bro...


  • Web Development

    Happy new year bro , thanks


Log in to reply