> prakaashganth d <[EMAIL PROTECTED]> wrote:
> > Hi People,
> > 
> >                     I have a doubt in C program.

Sounds more like homework to me.

> > 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.

Rajath N R <[EMAIL PROTECTED]> wrote:
> ...
> main()

The use of implicit int is lazy, as is the lack of
include for <stdio.h> which is _required_ for the
correct use of the variadic function printf.

> {
>         int i = 1234;
>         char buff[256];
>         snprintf(buff, 256, "%d", i);

Note that snprintf is (unfortunately), not a standard
function in C90. It is standard in C99, but the specs
aren't the same as the extension offered by some C90
implementations.

It is possible to predetermine a buffer size that will
always be sufficient, based on sizeof(int) and CHAR_BIT.
[Left as an exercise.]

>         printf("%c%c\n", buff[0], buff[1]);

I suggest...

  printf("%.2s\n", buff);

...to at least do something more sensible if i is
less than 10. Although if i less than zero, the
behaviour may not be what's desired.

> }

-- 
Peter

Reply via email to