karthikbalaguru said:
karthikbalaguru said:
I came across a strange code snippet.
int main(void)
{
printf("%d"+1,987);
return 0;
}
The output is d.
But, where should we need to use
the above method of printf ?
Hardy ever. Just because something is possible you don't
have to use it. You can also bite into your own finger
until it bleeds, but you rather likely don't do so most
of the time;-) It's just a demonstration that a format
string is just a string, including conversion to a poin-
ter to the first element when used in value context.
There are all kinds of such little tricks like writing
'"abc"[1]' when you want the character 'b' (or even
'1["abc"]') but just because you can do so doesn't make
it very useful in most everyday situations.
If it is not useful, then i think those stuffs
should be removed or come under some
warnings or some error checking conditions
should be added to throw error inside printf .
If you want a different language than C there are enough.
There are some rules in C that makes certain less common
constructs possible, but you don't have to use them (and
some of them can, in very special situations, become use-
ful). A compiler is free to spit out a warning if such an
unusual construct is used, but it's not mandated by the
C standard and, IMHO, shouldn't. C isn't a language that
holds your hand and keeps you from doing something stupid.
It tends to gives you enough rope to hang yourself - or
to make something extremely useful out of it. That may
make it a bit more difficult to learn and use C correctly
but, on the other hand, allows you to write effective,
fast code. That's why e.g. so many operating systems are
written in C.
I think, 'printf' has not been thoroughly tested
before release or some scenarios have been
overlooked while design/implementation ?
printf() can't be tested or released, it is a function
whose behaviour is defined by the C standard. There are
a number of *implementations* of this functions (in more
or less every libc) and some of those implementation may
have been more or less well tested. If you find a bug in
one of those implementations you have to take it up with
the writers of that implementation of printf(), it's not
a C issue.
BTW, if you're looking for more strange constructs and
(mis-?) uses of C look up the International Obfuscated
C Code Contest (IOCCC). Decide for yourself if you want
to learn/use a language that allows such things;-)
Regards, Jens