According to Petzold:

printf("The sum of %i and %i is %i", 5, 3, 5+3);

and

char szBuffer[100];
sprintf(szBuffer, "The sum of %i and %i is %i", 5, 3, 5+3);
puts(szBuffer);

are functionally equivalent.

Consider this small c program:

/* lines3.c */
void main(void){
int x, y;
char szBuffer[100];
for (y =3D 1; y < 24; y++){
for (x =3D 1; x < 24; x++)
if ((x =3D=3D y || x =3D=3D 24 - y) && y !=3D 12){
// sprintf(szBuffer, "\xDB");
printf("\xDB");
// puts(szBuffer);
} else
printf("\xB0");
printf("\n");
}
return;
}

This is a command console application which makes a design when run. cl =
lines3.c should get the executable.
But if we uncomment the sprintf and puts statements and comment out the =
accompanying printf the result is very different.

So that tells me they are not functionally equivalent. What's the =
trouble here?

--=20
George Hester
_________________________________

Re: functionally equivalent by Wessam

Wessam
Sat Feb 26 23:33:13 CST 2005

"George Hester" <hesterloli@hotmail.com> wrote in message
news:OelN7IJHFHA.2736@TK2MSFTNGP09.phx.gbl...

> According to Petzold:
> printf("The sum of %i and %i is %i", 5, 3, 5+3);
> and
> char szBuffer[100];
> sprintf(szBuffer, "The sum of %i and %i is %i", 5, 3, 5+3);
> puts(szBuffer);
> are functionally equivalent.

Well not really exactly! puts() automatically adds a line break to the end
of the string, so several puts() calls will show print the strings each on
its own line. This is not the case with printf() as you have to explicitly
add a '\n' to the end of the string to get a line break, otherwise you're
still accumulating to the same line.

So in your program, you can substitute puts(szBuffer) with printf(szBuffer)
and keep the sprintf for the format. Though as you might have noticed,
you've already used printf(), so why not use it from the beginning?

Wessam Bahnassi
Microsoft DirectX MVP,
Lead Programmer
In|Framez