0
#include <stdio.h>
int main(){
   printf("asd");
return 0;
}

The task is without modifying this program, overwriting or using define, in windows environment, this program have to write out : Yourname asd Yourname. Any idea?

Armfoot
  • 4,663
  • 5
  • 45
  • 60
rugabi94
  • 3
  • 1

2 Answers2

1

You can do it like this (edited to read Yourname from stdin):

#include <stdio.h>
#include <string.h>
int main(){
    printf("asd");
    return 0;
}

int printf(const char *__restrict __format, ...)
{
    char str[106], scs[50];
    scanf("%s", &scs);
    strcpy(str,scs);
    puts(strcat(strcat(strcat(strcat(str," "), __format)," "),scs));
    return 1;
}

Here's a working demo.

Armfoot
  • 4,663
  • 5
  • 45
  • 60
  • But in your code you are overwriting the printf's function – rugabi94 Oct 06 '15 at 11:03
  • @GáborRuszcsák yes, but you're not overriding the main function... I believe the task asked not to override the program but didn't say anything about overriding base functions in C. – Armfoot Oct 06 '15 at 11:06
  • This is a good answer. Plus one. It would be even better if you were to explain how you can get away with replacing `printf`, in the context of the *one definition rule* (as your linked answer in the comment does). – Bathsheba Oct 06 '15 at 11:09
  • @Bathsheba thanks, I believe I got away with it in the same manner as [this guy got away with it when he replaced `malloc`](http://stackoverflow.com/a/24894383/1326147) ;) I'm not sure about the inner workings though... – Armfoot Oct 06 '15 at 11:15
0

You can do it in a simple way. Global objects are created on the static memory area, they are allocated and initialized before the execution of main, and are freed after the execution of main. Here's the simple answer to your problem:

#include<iostream>

struct MyName {
public: 
    MyName() { printf("%s", "GaborRuszcsak\n"); }
    ~MyName() { printf("%s", "\nGaborRuszcsak\n"); }
};

MyName isGaborRuszcsak;

int main(int argc, char** argv){
    printf("asd");
    return 0;
}

Hope that helps.

kutyaMutya
  • 30
  • 5