Ben Klein wrote:
2009/6/24 Michael Stefaniuc mstefani@redhat.com:
Pierre Bourdon wrote:
I think what Michael meant is that sprintf(a, "%s", b);
is doing exactly the same thing as strcpy(a, b);
Right, that's what I meant.
in a less efficient way.
I'm not that much concerned about efficiency as the compiler will optimize it. But a strcpy is definitely easier to read.
I tend to assume that the compiler is an idiot that doesn't know a thing about optimisation, but that's me (e.g. using fputs for printing
Nope, compiler are better than most humans at optimizations. Programmers should focus on using good data structures and algorithms and write readable code.
a string constant instead of fprintf). I find it makes for more readable/lean code.
sprintf/strcpy is a no-brainer.
fprintf/fputs is a prime example of a grown and inconsistent API! To decide if using fputs is cleaner and easier to read really depends on the context; and example where keeping the fprint is easier to read would be something like:
fprintf(fd, format1, ...); fprintf(fd, format2, ...); fprintf(fd, format3, ...); fprintf(fd, format4, ...); fprintf(fd, format5, ...); fputs(string, fd): fprintf(fd, format6, ...); fprintf(fd, format7, ...); fprintf(fd, format8, ...); fprintf(fd, format9, ...); fprintf(fd, format10, ...);
Note the swapped source and destination in fputs in regards to fprintf.
bye michael