an extended printf function
an extended printf function
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...");
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
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:
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");
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:an extended printf function
now you may like bad-@$$ programming and use the kung-f00 approach:
which can even turn nice with
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 ? ...
Code: Select all
printl((char*[]){"This","is","not","that","ugly",NULL});
Code: Select all
#define sl(x...) (char* []){x ,NULL}
printl(sl("This","is","not","that","ugly"));
Re:an extended printf function
Pype, maybe you missed that this isn't C#? :p
(Or is that some crazy gcc extension?)
(Or is that some crazy gcc extension?)
Re:an extended printf function
No, it only works at compile time.Tim Robinson wrote: Pype, maybe you missed that this isn't C#? :p
(Or is that some crazy gcc extension?)
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:an extended printf function
I'm proud to know nothing about C#Tim Robinson wrote: Pype, maybe you missed that this isn't C#? :p


Re:an extended printf function
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
)
http://gcc.gnu.org/onlinedocs/gcc-3.0.2 ... html#SEC88
(You should look at C#, you might like it

- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:an extended printf function
that sounds more like a 'standard' than an extension to me ...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.
As an extension, GCC supports compound literals in C89 mode and in C++.
Re:an extended printf function
He... he... he... *evilchuckle*You can't do this in C or C++.
In C (and in C++), you can do this:
Code: Select all
printf("This" " works!");
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.
Re:an extended printf function
could you do that with normal pointers eg.
Will
be the same as
Pete
Will
Code: Select all
printf("Hello" "world");
Code: Select all
char *hello, *world;
strcpy(hello, "hello");
strcpy(world, "world");
printf(hello world);
Re:an extended printf function
And that with only coding the ostringstream class, the operator<< function and c_str(). Doesn't that equate quite a lot of work?Solar wrote: 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
Huh? HUH?Pete wrote: Willbe the same asCode: Select all
printf("Hello" "world");
Code: Select all
char *hello, *world; strcpy(hello, "hello"); strcpy(world, "world"); printf(hello world);
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.
Sure...And that with only coding the ostringstream class, the operator<< function and c_str(). Doesn't that equate quite a lot of work?

Every good solution is obvious once you've found it.
Re:an extended printf function
yeh ok that was just careless. Wasn't the point of the code.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.
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*);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.
Pete
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:an extended printf function
well, actually "text" "text" is not char* char* ... When the compiler sees two successive litteral strings, it concatenates them ...