SlideShare una empresa de Scribd logo
1 de 1
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
20 C programs
20 C programs20 C programs
20 C programs
navjoth
 

La actualidad más candente (19)

C programming
C programming C programming
C programming
 
miniLesson on the printf() function
miniLesson on the printf() functionminiLesson on the printf() function
miniLesson on the printf() function
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
2 data and c
2 data and c2 data and c
2 data and c
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
TDD in C - Recently Used List Kata
TDD in C - Recently Used List KataTDD in C - Recently Used List Kata
TDD in C - Recently Used List Kata
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Insecure coding in C (and C++)
Insecure coding in C (and C++)Insecure coding in C (and C++)
Insecure coding in C (and C++)
 
CP Handout#6
CP Handout#6CP Handout#6
CP Handout#6
 
20 C programs
20 C programs20 C programs
20 C programs
 
First c program
First c programFirst c program
First c program
 
1
11
1
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
Session 5-exersice
Session 5-exersiceSession 5-exersice
Session 5-exersice
 
C programming language
C programming languageC programming language
C programming language
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)
 
Semaphore
SemaphoreSemaphore
Semaphore
 

Destacado

Planeamiento Bimestral Xii
Planeamiento Bimestral XiiPlaneamiento Bimestral Xii
Planeamiento Bimestral Xii
guest97cd8d1
 
Saint patrick's day
Saint patrick's daySaint patrick's day
Saint patrick's day
Rafaarabit33
 
Salutacion Dia Del Periodista 2
Salutacion Dia Del Periodista 2Salutacion Dia Del Periodista 2
Salutacion Dia Del Periodista 2
guest2e5897
 

Destacado (16)

Youth Flash, April 2012
Youth Flash, April 2012Youth Flash, April 2012
Youth Flash, April 2012
 
Bylaws
BylawsBylaws
Bylaws
 
16 Jop April 08
16 Jop April 0816 Jop April 08
16 Jop April 08
 
2007 - Follow-up to the World Programme of Action for Youth to the Year 2000 ...
2007 - Follow-up to the World Programme of Action for Youth to the Year 2000 ...2007 - Follow-up to the World Programme of Action for Youth to the Year 2000 ...
2007 - Follow-up to the World Programme of Action for Youth to the Year 2000 ...
 
Opdracht 1
Opdracht 1Opdracht 1
Opdracht 1
 
USING PREDICTIONS TO POWER CUSTOMER SUCCESS
USING PREDICTIONS TO POWER CUSTOMER SUCCESSUSING PREDICTIONS TO POWER CUSTOMER SUCCESS
USING PREDICTIONS TO POWER CUSTOMER SUCCESS
 
2011 - Commission for Social Development, 49th Session Resolution on Policies...
2011 - Commission for Social Development, 49th Session Resolution on Policies...2011 - Commission for Social Development, 49th Session Resolution on Policies...
2011 - Commission for Social Development, 49th Session Resolution on Policies...
 
1999 - General Assembly resolution on Policies and Programmes Involving Youth...
1999 - General Assembly resolution on Policies and Programmes Involving Youth...1999 - General Assembly resolution on Policies and Programmes Involving Youth...
1999 - General Assembly resolution on Policies and Programmes Involving Youth...
 
Planeamiento Bimestral Xii
Planeamiento Bimestral XiiPlaneamiento Bimestral Xii
Planeamiento Bimestral Xii
 
05 Jo P May 07
05 Jo P May 0705 Jo P May 07
05 Jo P May 07
 
Roset 1
Roset 1Roset 1
Roset 1
 
Examen R 2
Examen R 2Examen R 2
Examen R 2
 
Sabia Usted Que
Sabia Usted QueSabia Usted Que
Sabia Usted Que
 
Saint patrick's day
Saint patrick's daySaint patrick's day
Saint patrick's day
 
Presentazione
PresentazionePresentazione
Presentazione
 
Salutacion Dia Del Periodista 2
Salutacion Dia Del Periodista 2Salutacion Dia Del Periodista 2
Salutacion Dia Del Periodista 2
 

Similar a 01 Jo P Jan 07

COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docxCOMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
donnajames55
 
Test flawfinder. This program wont compile or run; thats not
 Test flawfinder.  This program wont compile or run; thats not Test flawfinder.  This program wont compile or run; thats not
Test flawfinder. This program wont compile or run; thats not
MoseStaton39
 

Similar a 01 Jo P Jan 07 (20)

COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docxCOMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
C programming
C programmingC programming
C programming
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
Deep C Programming
Deep C ProgrammingDeep C Programming
Deep C Programming
 
The basics of c programming
The basics of c programmingThe basics of c programming
The basics of c programming
 
1.1 programming fundamentals
1.1 programming fundamentals1.1 programming fundamentals
1.1 programming fundamentals
 
Lab 2
Lab 2Lab 2
Lab 2
 
14 Jo P Feb 08
14 Jo P Feb 0814 Jo P Feb 08
14 Jo P Feb 08
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
25 Jo P Jan 09
25 Jo P Jan 0925 Jo P Jan 09
25 Jo P Jan 09
 
03 Jo P Mar 07
03 Jo P Mar 0703 Jo P Mar 07
03 Jo P Mar 07
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
2 debugging-c
2 debugging-c2 debugging-c
2 debugging-c
 
10 Jo P Oct 07
10 Jo P Oct 0710 Jo P Oct 07
10 Jo P Oct 07
 
Basic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.comBasic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.com
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
comp2
comp2comp2
comp2
 
Test flawfinder. This program wont compile or run; thats not
 Test flawfinder.  This program wont compile or run; thats not Test flawfinder.  This program wont compile or run; thats not
Test flawfinder. This program wont compile or run; thats not
 

Más de Ganesh Samarthyam

Más de Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

01 Jo P Jan 07

  • 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