473,378 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Portable printf extension ?

Hi,

I was wondering whether there is a standard-compliant way to extend
printf with a new %code that is not yet reserved (e.g. %r) with a
given argument type.

My guess would be to write a function that looks like « int
my_printf(const char *fmt, ...); », with a special processing of fmt
to replace the new %code with the correct data, and then hand over the
processed fmt and the remaining arguments to vprintf.

However I don't know much about variable arguments, but the standards
seems pretty light about what can be done with them. So I have no idea
of how to write such a my_printf function.

Is there a protable way to do this within the C? If anybody knows,
although it's a bit off-topic, would POSIX allow it if standard C
doesn't? (btw, what is the topical newsgroup for such POSIX-related
questions?)

On a side note, although I'm interested in the answers to the
questions above for my general knowledge of C, the problem I'm trying
to solve is fprintf-ing and snprintf-ing a non-C string, represented
by a struct containing a char* (that is not zero-terminated) and a
size_t length. So I'm also interested in a solution for that
particular problem. For now I'm using a sequence of fprintf/fwrite/
fprintf/... or snprintf/memcpy/snprintf/... but that looks ugly
(especially with the size checks in the second case).
Jun 27 '08 #1
5 1709
If your char array is not null terminated, you will have to use
various formatted input/output function that you are already using.
There isn't a way to extend printf and the thing you are suggesting is
to wrap the call in your own user-defined function. Here is one
introductory tutorial about variable number of arguments:
http://publications.gbdirect.co.uk/c...r9/stdarg.html

Jun 27 '08 #2
rahul <ra*********@gmail.comwrote:

[ Do _not_ snip all context from posts - quote enough material to make
it clear what you are talking about.
No, using Google Broken Groups Beta is _not_ an excuse. ]
If your char array is not null terminated, you will have to use
various formatted input/output function that you are already using.
There isn't a way to extend printf and the thing you are suggesting is
to wrap the call in your own user-defined function.
It is quite possible to output non-null-terminated char arrays using the
Standard printf() only, _if_ you know how many characters you want to
print, _and_ there are enough characters in the array.

Hint: precision specifier.

Richard
Jun 27 '08 #3
On Jun 4, 12:54 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
rahul <rahulsin...@gmail.comwrote:
It is quite possible to output non-null-terminated char arrays using the
Standard printf() only, _if_ you know how many characters you want to
print, _and_ there are enough characters in the array.

Hint: precision specifier.
Thanks a lot for that hint, I had completely forgotten about that
specifier. I did thought about the field width specifier (using a star
to get the width from the argument), but that doesn't do what I need.
I guess I forgot the precision because I have never used it.

So the call would look like that:

char *char_array_without_nul;
size_t char_array_size;
[proper initialization]
printf("Here is the string: \"%.*s\"\n", (int)char_array_size,
char_array_without_nul);

If I understand correctly, the cast is needed because int and size_t
might not have the same size (signedness shouldn't an issue as long as
char_array_size is not too large to fit in an int). Right?
Jun 27 '08 #4
li********@gmail.com wrote:
So the call would look like that:

char *char_array_without_nul;
size_t char_array_size;
[proper initialization]
printf("Here is the string: \"%.*s\"\n", (int)char_array_size,
char_array_without_nul);

If I understand correctly, the cast is needed because int and size_t
might not have the same size (signedness shouldn't an issue as long as
char_array_size is not too large to fit in an int). Right?
Right.

With the proviso that if char_array_size is not correct, but is in fact
larger than the real size of your array, you invoke undefined behaviour;
so just don't do that. But in that, it is no different from, say,
memcpy(). Also, if there _is_ by chance a nul character in your array,
nothing beyond it gets printed.

Richard
Jun 27 '08 #5
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
li********@gmail.com wrote:
>So the call would look like that:

char *char_array_without_nul;
size_t char_array_size;
[proper initialization]
printf("Here is the string: \"%.*s\"\n", (int)char_array_size,
char_array_without_nul);

If I understand correctly, the cast is needed because int and size_t
might not have the same size (signedness shouldn't an issue as long as
char_array_size is not too large to fit in an int). Right?

Right.

With the proviso that if char_array_size is not correct, but is in fact
larger than the real size of your array, you invoke undefined behaviour;
so just don't do that. But in that, it is no different from, say,
memcpy(). Also, if there _is_ by chance a nul character in your array,
nothing beyond it gets printed.
To be precise, neither the nul character nor anything beyond it gets
printed.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Arnaud Legrand | last post by:
Hello, I have a question about portability and I have not found the answer in the FAQ. I have different modules to build. All of them have the same private part and a common public part (though...
25
by: Joakim Hove | last post by:
Hello, I have code which makses use of variables of type size_t. The code is originally developed on a 32 bit machine, but now it is run on both a 32 bit and a 64 bit machine. In the code...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
8
by: ais523 | last post by:
I've checked the FAQ for this and couldn't find the answer. Is the following code snippet portable? int a; a=6; printf("%d\n",(*a)); This prints "6" on my compiler. I've been told it's...
1
by: Alexandre Guimond | last post by:
Hi. I want to create a portable setup.py file for windows / linux for an extension package that i need to link with external libraries (gsl and boost). on windows i do something like this: ...
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
25
by: jacob navia | last post by:
The C99 standard forgot to define the printf equivalent for complex numbers Since I am revising the lcc-win implementation of complex numbers I decided to fill this hole with "Z" for...
23
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.