Thread: printf advanced!!

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    58

    printf advanced!!

    ok, as far as i know this is really advanced and i shouldnt be treading this path. but the curiosity gets the better of me and i am asking it anyway.

    every function has an address, right? so now for basics let us consider the printf. now i want to know how to manipulate the values at this address so that:

    each time somebody calls printf the same thing should be printed.
    think this has got something to do with compiler design.

    it would be nice if some real "punter" explained this to all us "pretty much" newbies. would be nicer if u could give the code snippet too.

    thanx.
    even a fish would not have been in danger had it kept its mouth shut.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Can you be more specific?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    ok here is what i wanted to achieve.
    can u make printf to always print "hello world " no matter what the arguments u give to printf.
    say suppose i call printf in any of these ways (included but not the only ones):
    printf("this is a real world");
    printf("%d",val);
    printf(buf);

    anyway u call printf anytime it should always print "hello world".
    Last edited by gooddevil; 05-24-2004 at 11:46 AM.
    even a fish would not have been in danger had it kept its mouth shut.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <stdio.h>
    
    #define printf printf("Hello, world\n");
    
    int
    main(void)
    {
      int i = 0;
      char *buf = "test";
    
      printf("this is a real world");
      printf("%d", i);
      printf(buf);
    
      return 0;
    }
    My best code is written with the delete key.

  5. #5
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    do you need to #undef printf before doing that?

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    ok prelude ,
    i din know that, thanx.
    but actually prelude i wanted u to do that by changing things in the address of printf, man.
    dont all functions have addresses like variables do (but unfortunately i dont know how to access them). i wanted u to make manipulations in those parts so that u get this effect.

    ok becoz of ur previous reply one more question pops up. now what u r doing there is: each time the preprocessor sees printf it replaces ur printf with this one. ok now my question is what happens to the other part i.e the one in the brackets. is it ignored just like that? is everything that is inside two brackets and that is followed by a ; just ignored. if it is why dont we use them for commenting. thanx.
    even a fish would not have been in danger had it kept its mouth shut.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    another addition:
    you call printf from any program with any arguments it should print "Hello world". is that possible.
    even a fish would not have been in danger had it kept its mouth shut.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but actually prelude i wanted u to do that by changing things in the address of printf, man.
    I know what you wanted.

    >dont all functions have addresses like variables do
    Yes. You can have pointers to those addresses too.

    >i wanted u to make manipulations in those parts so that u get this effect.
    Let's look at this another way. Say you have an integer i with the value 0 and and integer j with the value 1. Using only the address of i and without changing the contents of that address, have any use of i actually use j. I suppose it's possible if you somehow switch the relative addresses of i and j with their symbols in the executable, but that's really beyond my present capability.

    >ok now my question is what happens to the other part i.e the one in the brackets.
    It's evaluated for a value and the value is discarded. Take printf("%d", i); for example. After preprocessing, it would be:
    Code:
    printf("Hello, world\n");("%d", i);
    The ("%d", i); part is evaluated, returns i because of the comma operator, and the value is discarded because it isn't assigned anywhere. The statement is basically a no-op.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    23
    You can pass a function name acting as a pointer to that function.

    For example:

    Suppose you have two functions--printme and feedme.

    Code:
    int feedme(void (*function)(char*)){
       (*function)("hi");
       return 0;
    }
    void printme(char* value){
       printf(value);
    }
    feedme(printme);
    someone double-check me on that

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >someone double-check me on that
    I don't see a problem. But the original question was that any call to printf, not a pointer to a function resembling printf, would print "Hello, world". He basically wanted to rebind the address of printf to his own function defined elsewhere.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM