Page 1 of 2
an extended printf function
Posted: Sun Mar 28, 2004 2:38 pm
by derek
Im not so sure if it is related with os development but anyway...
I have implemented my printf function..
int printf(unsigned char *);
indeed no problem occured. But now I want to be able to add two strings together. I mean I want + operator to overload. But as I know it is not possible in c. How can I implement or does someone implement some thing like operator overloading??
printf("This" + "is" + "really" + "difficult");
How can I make this work?Is this possible to implement this??
PS: Dont advise this:
printf("This", "is", "easy...");
Re:an extended printf function
Posted: Sun Mar 28, 2004 2:45 pm
by Tim
You can't do this in C or C++. You fundamentally cannot add two pointers together, and the compiler sees each "string" as a const char* pointer.
What you should do instead is implement printf properly, so it can do this:
Code: Select all
printf("%s %s %s %s", "This", "is", "really", "easy");
Re:an extended printf function
Posted: Sun Mar 28, 2004 3:31 pm
by Pype.Clicker
now you may like
bad-@$$ programming and use the kung-f00 approach:
Code: Select all
printl((char*[]){"This","is","not","that","ugly",NULL});
which can even turn nice with
Code: Select all
#define sl(x...) (char* []){x ,NULL}
printl(sl("This","is","not","that","ugly"));
I guess the reason why you disliked the ( , , , ) approach is that it has a fixed number of strings ... or is there anything else i missed ? ...
Re:an extended printf function
Posted: Sun Mar 28, 2004 4:42 pm
by Tim
Pype, maybe you missed that this isn't C#? :p
(Or is that some crazy gcc extension?)
Re:an extended printf function
Posted: Sun Mar 28, 2004 4:51 pm
by Candy
Tim Robinson wrote:
Pype, maybe you missed that this isn't C#? :p
(Or is that some crazy gcc extension?)
No, it only works at compile time.
Re:an extended printf function
Posted: Mon Mar 29, 2004 2:00 am
by Pype.Clicker
Tim Robinson wrote:
Pype, maybe you missed that this isn't C#? :p
I'm proud to know nothing about C#

this works on gcc 2.95.3 and gcc 3.x aswell, afaik. If you have evidences of a compiler that doesn't accept it or of some standardized language rule it breaks, i'd be interrested to know

Re:an extended printf function
Posted: Mon Mar 29, 2004 4:55 am
by Tim
It's a gcc extension:
http://gcc.gnu.org/onlinedocs/gcc-3.0.2 ... html#SEC88
(You should look at C#, you might like it

)
Re:an extended printf function
Posted: Mon Mar 29, 2004 5:10 am
by Pype.Clicker
ISO C99 supports compound literals. A compound literal looks like a cast containing an initializer. Its value is an object of the type specified in the cast, containing the elements specified in the initializer.
that sounds more like a 'standard' than an extension to me ...
As an extension, GCC supports compound literals in C89 mode and in C++.
Re:an extended printf function
Posted: Mon Mar 29, 2004 7:08 am
by Solar
You can't do this in C or C++.
He... he... he... *evilchuckle*
In C (and in C++), you can do this:
Extra spaces added for emphasis. Of course works only for compile-time constants.
In C++, you could set up an ostringstream(), adding substrings with operator<<(), and calling str().c_str() on the result... ;D
Re:an extended printf function
Posted: Mon Mar 29, 2004 7:26 am
by Therx
could you do that with normal pointers eg.
Will
be the same as
Code: Select all
char *hello, *world;
strcpy(hello, "hello");
strcpy(world, "world");
printf(hello world);
Pete
Re:an extended printf function
Posted: Mon Mar 29, 2004 7:28 am
by Candy
Solar wrote:
In C++, you could set up an ostringstream(), adding substrings with operator<<(), and calling str().c_str() on the result... ;D
And that with only coding the ostringstream class, the operator<< function and c_str(). Doesn't that equate quite a lot of work?
Re:an extended printf function
Posted: Mon Mar 29, 2004 7:38 am
by Solar
Pete wrote:
Will
be the same as
Code: Select all
char *hello, *world;
strcpy(hello, "hello");
strcpy(world, "world");
printf(hello world);
Huh? HUH?
Erm...
One, neither
hello nor
world point to allocated memory. In fact, where they point to is undefined. Both strcpy() calls are invalid and potentially lethal.
Two,
printf(char* char*) is illegal. Or, at least, not a function call. Well, perhaps if you overload the whitespace operator for char* parameters... just kidding.
No, it won't work. It won't even compile.
And that with only coding the ostringstream class, the operator<< function and c_str(). Doesn't that equate quite a lot of work?
Sure...

Re:an extended printf function
Posted: Mon Mar 29, 2004 7:49 am
by Therx
One, neither hello nor world point to allocated memory. In fact, where they point to is undefined. Both strcpy() calls are invalid and potentially lethal.
yeh ok that was just careless. Wasn't the point of the code.
Two, printf(char* char*) is illegal. Or, at least, not a function call. Well, perhaps if you overload the whitespace operator for char* parameters... just kidding.
Wasn't such a stupid question. "This is text" equates to a char* and if you can have printf("test" "test"); then you've essentially got printf(char* char*);
Pete
Re:an extended printf function
Posted: Mon Mar 29, 2004 7:55 am
by Pype.Clicker
well, actually "text" "text" is not char* char* ... When the compiler sees two successive litteral strings, it concatenates them ...
Re:an extended printf function
Posted: Mon Mar 29, 2004 7:57 am
by Therx
ah ok.
Pete