Switch to full style
For C/C++ coders discussions and solutions
Post a reply

Format Specifier in C

Sat Nov 08, 2008 11:10 pm

Hi People,
I have a doubt in C program.
Suppose i have declared int i = 1234;
If I want to print only 12 (first 2 digits) using printf statement, please
suggest how do i implement it.



Re: Format Specifier in C

Sat Nov 08, 2008 11:11 pm

You can convert the int to a string with sprintf(), then truncate the
string after the first two characters, then display the string.

Alternatively, if you want to achieve a result of 12 mathematically,
you can use modulus (%) and division (/) :

(i - (i % 100)) / 100

== 12

Post a reply
  Related Posts  to : Format Specifier in C
 PDF Format     -  
 Format Debugging Messages     -  
 Number format for current     -  
 Number format function prototype     -  
 Handling m3g format (3D files for mobiles) in J2ME     -  
 Printing today+ current date with different format     -  
 i want to draw a circle on a image(any format bmp,jpeg etc.)     -  
 database contents display in table format in swing     -