01 Jo P Jan 07

279 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
279
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

01 Jo P Jan 07

  1. 1. The Joy of Programming S.G. GANESH How ‘C’mart Are You? Here's a new feature on programming puzzles in C. Check out the solutions and have some fun. Quines Write a program that prints the source code of that For the last %c, the argument is the new line, which program itself (such programs are known as ‘quines’). The gets printed. Now you can verify the fact that the source solution need not be generic; it is enough that the program program and the output are same, character-by-character! written prints itself. Note: You should not resort to shortcuts such as file Find the bug I/O operations to open the source file and print the The following program intends to print a paragraph text, contents. but the program didn’t work as expected. Can you find out Here is a well-known and clever solution: why? GUEST COLUMN char*f=”char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c”;main() #include <stdio.h> {printf(f,34,f,34,10);} int main() { The ASCII values for double-quotes and new- printf(“C’s free-formatted nature and features n” line characters are 34 and 10. The basic idea “like preprocessor can make the debugging n”, behind this program is to have a formatting string “process complex; typos can lead to nasty bugs, n” and print it twice with formatting characters. “not being caught at compile time. Though n” “its frustrating at times, the power and n” To understand the program, start from the printf in “flexibility that C provides is also a source n” main(). It has a formatting string f (which is “of joy in the hands of an expert C programmer.n”); “char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c”). } The printf routine takes ‘f’ as the argument Two continuous string literals separated by white space and starts printing the characters. While printing characters are concatenated by the compiler and treated the characters, it encounters the double-quote as a single string literal (known as ‘stringization’ character and prints the “. operation). After that it sees %s in the formatting string The programmer expected the whole text to get for which the value of f is to be printed. So the printed as the list of string literals provided as the output looks like: argument to printf would get concatenated. However, there is a comma (possibly a typo) at the end of the string char*f=”char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c “like preprocessor can make the debugging n”. So only the first part of the printf gets printed! Now, it again sees a %c and it prints a “: char*f=”char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c” By: S.G. Ganesh is an engineer in Hewlett-Packard’s C++ compiler team. He has authored a book “Deep C” (ISBN 81- Then it prints the rest of the string till it encounters 7656-501-6). He is also a member of ANSI/ISO C++ another %c: Standardization committee (JTC1/SC22/WG21) representing HP. He has a master’s degree in computer char*f=”char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c”;main(){printf(f,34,f,34,10);} applications from PSG College of Technology, Coimbatore. www.linuxforu.com | LINUX FOR YOU | JANUARY 2007 119 CMYK

×