Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!cs.utexas.edu!uunet!microsoft!t-wader From: t-wader@microsoft.UUCP (Wade Richards) Newsgroups: comp.lang.c Subject: Re: printf Message-ID: <8174@microsoft.UUCP> Date: 24 Oct 89 01:52:13 GMT References: <543@uwm.edu> Reply-To: t-wader@microsoft.UUCP (Wade Richards) Distribution: na Organization: Microsoft Corp., Redmond WA Lines: 31 Summary: Expires: Sender: Prefered-Food: Pizza Shoe-Size: 10 1/2 Followup-To: Disclaimer: I don't always agree with my boss, why should he agree with me? In article <543@uwm.edu> zhao@csd4.csd.uwm.edu (T.C. Zhao) writes: =}I recently came across a piece of c code:(both a and b are integers) =}printf("%d"+(a),b); =}in passes compiler without any problem, what does this code mean ? =} =}-- =}---------------------------------------------- =}Internet: zhao@csd4.csd.uwm.edu =} BITNET: zhao%csd4.csd.uwm.edu@WISCMAC3.BITNET That code is the same as: switch( a ) { case 0: printf( "%d", b ); break; case 1: printf( "d", b ); break; case 2: printf( "", b ); break; default: printf( (char *)random(), b ); break; } The expression "%d"+(a) evaluates to a pointer to the string "%d\0" somewhere in memory, plus the integer a. The sum of a "pointer to type X" and an integer is another "pointer to type X", pointing to the a-th following object of type X. This is not at all clear. By way of example, if the pointer pointed to memory location 1000, and an object of type X took up 5 bytes, then the pointer plus 2 would be: 1000 + 2*5 = 1010. Hope this helps. --- Wade BYW, the (char *)random() simply means a (lot likely valid) pointer to anywhere.