Print numbers according to their native promoted type

J

John Reye

Hi,

the C library has weird number printing functions:
sprintf(str, "%d", num_signed)
sprintf(str, "%u", num_unsigned)


I've created a nice macro SPRINTNUM(STR, EXPR), that handles this
automatically, e.g. if EXPR is of type unsigned long long, then I
automatically get sprintf(STR, "%llu", NUM)

Is there something similar in one of the c standards?????

For me this has become a vital macro for working with c and debugging
purposes!

Regards,
J.

PS: why is this macro so important for me?? Well... Exercise for the
interested reader: create a macro MAX_RVALUE(EXPR) to generate the
maximum value that EXPR could possibly hold, i.e. typically for 32
bits processors... MAX_RVALUE(int) = 2147483647. Also create
MIN_RVALUE(EXPR)... i.e. for typically 32 bits processors
MIN_RVALUE(1+2) = -2147483648 .
Now one can test MAX_RVALUE and MIN_RVALUE with SPRINTNUM! ;)
 
J

John Reye

Of course C++ seems more and more attractive,
std::cout << expression;
automatically does what I want.

Lets face it: C++ is the better C. ;)
But it's also a bloated monster, but at least it has a decent library
for applications programming, something which C really is missing.
Example: If you want to work with sets (with their basic operatings of
AND'ing and OR'ing etc) C limits you to the few bits of "unsigned long
long". If the set has more elements than sizeof(unsigned long long) *
CHAR_BIT, the poor C programmer will have to implement set library
function. (I wonder if Jacob Navia's library has something
suitable...)
the best way seems: just use C++'s bitset!
 
J

John Reye

a decent library
for applications programming, something which C really is missing.
Example: If you want to work with sets (with their basic operatings of
AND'ing and OR'ing etc) C limits you to the few bits of "unsigned long
long". If the set has more elements than sizeof(unsigned long long) *
CHAR_BIT, the poor C programmer will have to implement set library
function. (I wonder if Jacob Navia's library has something
suitable...)

Ah Monsieur Navia's CCL has BitString. His library is probably good
reading after Plauger's work on the standard C library. :)
Still... in terms of actual usage (beyond learning): c++ is
standardized
 
B

BartC

John Reye said:
Of course C++ seems more and more attractive,
std::cout << expression;
automatically does what I want.

Actually most languages can do that. Only C requires the programmer to tell
the language what it already knows: the type of the expression!
Lets face it: C++ is the better C. ;)
But it's also a bloated monster, but at least it has a decent library
for applications programming, something which C really is missing.

Well, perhaps C is the wrong language for applications. And since there are
plenty of alternatives and people use them, then there's less motivation to
actually evolve the language. Certainly, from reading this group, there's
little interest in developing the language in that direction. Only with
tinkering with uninteresting aspects of it.

But maybe that is the right thing to do; keep the language stable, and
develop completely separate alternatives.
Example: If you want to work with sets (with their basic operatings of
AND'ing and OR'ing etc) C limits you to the few bits of "unsigned long
long". If the set has more elements than sizeof(unsigned long long) *
CHAR_BIT, the poor C programmer will have to implement set library
function. (I wonder if Jacob Navia's library has something
suitable...)

I've looked at the manual, and it looks comprehensive! However fixed-size
sets aren't that difficult to implement, if you're happy doing things with
functions. Of course this would all be much nicer if the language and syntax
were more accommodating, but that's not going to happen.

As it is, C is fine for the purposes of implementing all these things ... in
another language.
 
K

Kaz Kylheku

Of course C++ seems more and more attractive,
std::cout << expression;
automatically does what I want.

Lets face it: C++ is the better C. ;)

This is true. C++ can be regarded as the main branch in this family of
languages.

Continued development of the C language is pointless. It is basically just a
hobby of a bunch of people who keep meeting, never letting it cross their minds
that they ought to dissolve, or merge with C++.

This is like trying to evolve a better ape out of a chimpanzee, when there
already exist humans.
 
J

Jens Gustedt

Am 16.06.2012 00:44, schrieb Kaz Kylheku:
Continued development of the C language is pointless. It is basically just a
hobby of a bunch of people who keep meeting, never letting it cross their minds
that they ought to dissolve, or merge with C++.

This is like trying to evolve a better ape out of a chimpanzee, when there
already exist humans.

Your blub doesn't speak in advantage of the human, then.

Jens
 
B

BartC

Kaz Kylheku said:
This is true. C++ can be regarded as the main branch in this family of
languages.

Continued development of the C language is pointless. It is basically
just a
hobby of a bunch of people who keep meeting, never letting it cross their
minds
that they ought to dissolve, or merge with C++.

But not everyone likes C++. And there are advantages to having a simple,
low-level language like C (although successive standards have tried hard to
make it more complex than it really is).

Take the main implementation of Python for example, which seems to be
written in C. What would be the point of doing it in C++? Most C++ features
would be wasted, because all you need will be in Python. Any implementation
of mutable lists in Python, would not be helped by C++ having it's own very
different version of the same thing; it would just get in the way.

I suppose it's possible to use C++ and simply not make use of features that
aren't needed, but I don't know how well that would work. I
imagine it'd be like driving your car, and trying to pretend that 40-ton
trailer attached to the back isn't having any effect.
 
I

Ian Collins

I suppose it's possible to use C++ and simply not make use of features that
aren't needed, but I don't know how well that would work. I
imagine it'd be like driving your car, and trying to pretend that 40-ton
trailer attached to the back isn't having any effect.

It can work very well.

I while back I was coaching a DSP team who like most DSP programmers
were strong in C, but not so in C++. They successfully adopted a
minimal set of C++ features (function overloading, managed resources and
other small classes). That was the closest I've seen to C++ being used
as a "better C".
 
K

Kenny McCormack

I suppose it's possible to use C++ and simply not make use of features that
aren't needed, but I don't know how well that would work. I
imagine it'd be like driving your car, and trying to pretend that 40-ton
trailer attached to the back isn't having any effect.

I think that what others have been arguing is that it really is OK to
program in C and compile it with a C++ compiler. Yes, I know that the
dogmatic position of this newsgroup is that neither language is a subset of
the other - and of course that it is true in some odd edge cases, that very
few working programmers know or care about. The fact is that, in the real
world, it is fine to do that.

Among other things, it probably makes you write cleaner C code. But let's
not get started on casting the return value of malloc()...
 
B

BartC

Kenny McCormack said:
I think that what others have been arguing is that it really is OK to
program in C and compile it with a C++ compiler.

I can't even get that far. I'm using mingw, which AFAIK ought to compile
C++, and it seems to recognise I'm trying to do so, but I get:

gcc: CreateProcess: No such file or directory

Typically terse, would it hurt to say which actual file or directory it's
looking for, and where it's tried looking for it, so that I would have a
clue what's happening? And what is CreateProcess, the file it's trying to
find? It doesn't look like a filename.
 
B

Ben Bacarisse

BartC said:
I suppose it's possible to use C++ and simply not make use of features
that aren't needed, but I don't know how well that would work.

People in a C++ group may have experience of exactly that. You could ask.
I
imagine it'd be like driving your car, and trying to pretend that
40-ton trailer attached to the back isn't having any effect.

The general idea of C++ (and one reason it is the way it is) is that you
don't pay for what you don't use. I don't think it has been possible to
make that always the case, but I think you'd be hard-pressed to find an
unused C++ feature that would have a car to car+40-ton-trailer cost
ratio. I.e. your imagination has run-amok!
 
B

Ben Bacarisse

BartC said:
I can't even get that far. I'm using mingw, which AFAIK ought to
compile C++, and it seems to recognise I'm trying to do so, but I get:

gcc: CreateProcess: No such file or directory

Typically terse, would it hurt to say which actual file or directory
it's looking for, and where it's tried looking for it, so that I would
have a clue what's happening? And what is CreateProcess, the file it's
trying to find? It doesn't look like a filename.

You might try, for example, gmane.comp.gnu.mingw.user. If you do, you
should say a lot more about what's happening. A general moan like this
is fine to vent your frustration, but if you actually want to solve the
problem, a lot more information would be needed. (But not here of
course.)
 
B

BartC

You might try, for example, gmane.comp.gnu.mingw.user. If you do, you
should say a lot more about what's happening. A general moan like this
is fine to vent your frustration, but if you actually want to solve the
problem, a lot more information would be needed. (But not here of
course.)

I've already googled the error message. It seems a common problem. It even
comes up as a bug report itself:

"...This error message is unhelpful because it does not say *which* file was
not found; as a result, the underlying configuration problem is difficult to
diagnose and resolve..."
 
B

BartC

Ben Bacarisse said:

[Using C++ for C programs]
The general idea of C++ (and one reason it is the way it is) is that you
don't pay for what you don't use. I don't think it has been possible to
make that always the case, but I think you'd be hard-pressed to find an
unused C++ feature that would have a car to car+40-ton-trailer cost
ratio. I.e. your imagination has run-amok!

I was thinking more of the language, and the development process, that has
all that extra baggage, and the compiler having a lot more things on it's
mind. The resulting executables could well be reasonable.
 
L

Les Cargill

BartC said:
I can't even get that far. I'm using mingw, which AFAIK ought to compile
C++, and it seems to recognise I'm trying to do so, but I get:

gcc: CreateProcess: No such file or directory

Typically terse, would it hurt to say which actual file or directory
it's looking for, and where it's tried looking for it, so that I would
have a clue what's happening? And what is CreateProcess, the file it's
trying to find? It doesn't look like a filename.

This sounds like a make problem.


--- BEGIN EXCERPT ---
C:\c\usenet>make
gcc -o t.exe t.c
process_begin: CreateProcess((null), gcc -o t.exe t.c, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [t.exe] Error 2
C:\c\usenet>

C:\c\usenet>cat makefile


t.exe : t.c
gcc -o t.exe t.c

C:\c\usenet>
C:\c\usenet>gcc -o t.exe t.c

C:\c\usenet>
C:\c\usenet>make -v
GNU Make version 3.78.1, by Richard Stallman and Roland McGrath.
Built for Windows32
Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

Report bugs to <[email protected]>.

C:\c\usenet>\unixutils\UnxUtils\usr\local\wbin\make -v
GNU Make version 3.78.1, by Richard Stallman and Roland McGrath.
Built for Windows32
Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

Report bugs to <[email protected]>.


C:\c\usenet>

--- END EXCERPT ---

Fully qualifying gcc fixes this, as do other strategies. I believe
MingW offers a make tool. There's also make 3.8x which may fix it.

i just haven't fully fixed this on my system.
 
J

Jorgen Grahn

Of course C++ seems more and more attractive,
std::cout << expression;
automatically does what I want.

Lets face it: C++ is the better C. ;)

How ironic. Over at comp.lang.c++ we have C people explaining at
length how much better C printf() is compared to C++ iostreams.
But it's also a bloated monster

I happen to disagree, but this is not the place for C++ advocacy.

/Jorgen
 
T

Tim Rentsch

John Reye said:
Hi,

the C library has weird number printing functions:
sprintf(str, "%d", num_signed)
sprintf(str, "%u", num_unsigned)


I've created a nice macro SPRINTNUM(STR, EXPR), that handles this
automatically, e.g. if EXPR is of type unsigned long long, then I
automatically get sprintf(STR, "%llu", NUM)

Are you sure that it works? I believe it isn't possible to do
this in general, ie, so that it gives correct answers on all
conforming implementations (even some not-so-pathological ones).
Is there something similar in one of the c standards?????

For me this has become a vital macro for working with c and debugging
purposes!

Regards,
J.

PS: why is this macro so important for me?? Well... Exercise for the
interested reader: create a macro MAX_RVALUE(EXPR) to generate the
maximum value that EXPR could possibly hold, i.e. typically for 32
bits processors... MAX_RVALUE(int) = 2147483647. Also create
MIN_RVALUE(EXPR)... i.e. for typically 32 bits processors
MIN_RVALUE(1+2) = -2147483648 .
Now one can test MAX_RVALUE and MIN_RVALUE with SPRINTNUM! ;)

Here again, IIANM this cannot be done so that it works on all
conforming implementations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top