rugabi94
rugabi94

Reputation: 3

Modifying printf output

#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?

Upvotes: 0

Views: 378

Answers (2)

kutyaMutya
kutyaMutya

Reputation: 30

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.

Upvotes: 0

Armfoot
Armfoot

Reputation: 4921

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.

Upvotes: 1

Related Questions