an extended printf function

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
derek

an extended printf function

Post 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...");
Tim

Re:an extended printf function

Post 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");
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:an extended printf function

Post 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 ? ...
Tim

Re:an extended printf function

Post by Tim »

Pype, maybe you missed that this isn't C#? :p

(Or is that some crazy gcc extension?)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:an extended printf function

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:an extended printf function

Post 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 :)
Tim

Re:an extended printf function

Post 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 :) )
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:an extended printf function

Post 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++.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:an extended printf function

Post by Solar »

You can't do this in C or C++.
He... he... he... *evilchuckle*

In C (and in C++), you can do this:

Code: Select all

printf("This"     " works!");
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
Every good solution is obvious once you've found it.
Therx

Re:an extended printf function

Post by Therx »

could you do that with normal pointers eg.

Will

Code: Select all

printf("Hello"  "world");
be the same as

Code: Select all

char *hello, *world;
strcpy(hello, "hello");
strcpy(world, "world");
printf(hello world);
Pete
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:an extended printf function

Post 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?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:an extended printf function

Post by Solar »

Pete wrote: Will

Code: Select all

printf("Hello"  "world");
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... :D
Every good solution is obvious once you've found it.
Therx

Re:an extended printf function

Post 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
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:an extended printf function

Post by Pype.Clicker »

well, actually "text" "text" is not char* char* ... When the compiler sees two successive litteral strings, it concatenates them ...
Therx

Re:an extended printf function

Post by Therx »

ah ok.

Pete
Post Reply