Thread: A puzzle in C:

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    1

    Lightbulb A puzzle in C:

    Any solution of this prolbem:

    Code:
    #include<stdio.h>
    
    void change()
    {
    
    /* Write something in this function so that the output of printf in mainfunction should give 5 . Do not change the main function */
    
    }
    
    void main()
    {
          int i=5;
    
          change();
    
           i=10;
    
            printf("%d",i);
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Sure.
    Code:
    #include<stdio.h>
    
    void change()
    {
    #define printf i=5,printf
    }
    
    void main()
    {
    	int i=5;
    	
    	change();
    	
    	i=10;
    	
    	printf("%d",i);
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by pianorain
    Sure.
    Code:
    #include<stdio.h>
    
    void change()
    {
    #define printf i=5,printf
    }
    
    void main()
    {
    	int i=5;
    	
    	change();
    	
    	i=10;
    	
    	printf("%d",i);
    }
    Wow, would of never thought of that one. I guess that's one of those classic obfuscation tricks.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Only way I knew how to do it. I don't usually use #define's in obfuscation. I think it's kind of cheap.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void change( void )
    {
        printf("5");
        fflush( stdout );
        exit( 0 );
    }
    :P


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Your solution won't work, quzah, because the statement requires that the printf statment in main() will result in printing "5." Nice try though.

    Here's my thought, along the same lines as pianorain's:
    Code:
    #include <stdio.h>
    
    void change() {
    /* Write something in this function so that the output of printf
       in mainfunction should give 5 . Do not change the main function */
      #define printf(a, x) putchar('5')
    }
    
    int main() {
      int i = 5;
      change();
      i = 10;
      
      printf("%d",i);
      
      return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I know. I was faking it. Hence the ':P'.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Any solution of this prolbem:
    Yeah, find a better book or teacher.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The puzzle again...Swapping elements of 2D array
    By crazygopedder in forum C Programming
    Replies: 44
    Last Post: 11-05-2008, 01:53 PM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2006, 11:08 PM
  4. Solution to Google Puzzle 3,3,8,8=24
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-01-2006, 09:12 AM