printf()


I jsut want to use printf() lib function as user defined function which function should I use to print data and string
Could you please elaborate? I do not understand correctly. Do you wish to print std::string or do you want to modify printf-function somehow?
I think May be you can use putchar() function for printing data.
ex
int clrscr(int,int);
int min(void)
{
int x=0,y=0,z=0;
printf("enter two number");
scanf("%d%d",&x,&y);
z=clrscr(x,y);
printf("%d",z);
while(!kbhit());
return 0;
}
int clrscr(int x,int y)
{
return x+y;
}
@Henri korpela & Hitesh vaghani I just want to use printf in place of clrscr then what lib function should I use to print data and string through stdout



Last edited on
Could you possibly show an example of the flow of the program, please? Like this:

Enter two numbers: 5, 10


And so forth.
@Henri Korpela

the flow of the prograam
enter two number 5 10
z=15
you can define printf in starting using
#define printf xyz
now your printf is xyz that means
xyz("%d",z);
use std::cout; <iostream> include.
First of all, just one question: Do you know what C++ I/O streams are?
If so, and you still want that,
1
2
3
4
5
6
7
8
9
10
11
void myprintf(string format)
{
	cout<<format;
}

template<typename _Tp, typename... _Tps>
void myprintf(string format, _Tp arg, _Tps... args)
{
	cout<<format.substr(0, format.find('%'))<<arg;
	myprintf(format.substr(format.find('%')+1), args...);
}

You use the '%'s without the format character since we know it's type eg.
1
2
3
4
int main()
{
      myprintf("% %!", "Hello", string("World"));
      }
Hello World!

If you don't know what C++ I/O streams are, read this: http://www.cplusplus.com/doc/tutorial/basic_io/
Last edited on
bump
> I jsut want to use printf() lib function as user defined function
> which function should I use to print data and string

printf() is a function, not a keyword. Like any other function, printf() too can be overloaded.

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
using namespace std ;

int printf( int a, int b ) { return a+b ; } // another printf
int printf( int a, int b, int c ) { return a + b*c ; } // yet another printf

int main()
{
    printf( "%s %d and %d\n", "the values are: ", printf(1,2), printf(3,4,5) ) ;
}


Though the principle of least surprise would imply that you should use a name other than printf() for your own functions.
Topic archived. No new replies allowed.