Newbie C++-to-C Q...

E

Erica

Hi all,

I apologize in advance for the newbishness of my questions. However, I've
done quite a bit of googling and couldn't find any answers.

I have lots of experience with C++, but very little experience with regular
ol' C. I am using gcc on Fedora core 1, if it matters. I am having problems
finding conversions for two C++ conventions:

1. In C++ I can declare a variable as a bool, wheras that gives me an error
with gcc
2. Is there a way of using iostream.h in C? So that I can use cin, cout,
endl, etc...

Thanks for any help you can throw my way!!
--Erica
 
J

jacob navia

Erica said:
Hi all,

I apologize in advance for the newbishness of my questions. However, I've
done quite a bit of googling and couldn't find any answers.

I have lots of experience with C++, but very little experience with regular
ol' C. I am using gcc on Fedora core 1, if it matters. I am having problems
finding conversions for two C++ conventions:

1. In C++ I can declare a variable as a bool, wheras that gives me an error
with gcc
2. Is there a way of using iostream.h in C? So that I can use cin, cout,
endl, etc...

Thanks for any help you can throw my way!!
--Erica

In C you use the header file <stdbool.h> and declare variables
as bool too.

For iostream you have to rewrite the code into printf statements.
This is boring but quite trivial.
 
K

Keith Thompson

Erica said:
I apologize in advance for the newbishness of my questions. However, I've
done quite a bit of googling and couldn't find any answers.

I have lots of experience with C++, but very little experience with regular
ol' C. I am using gcc on Fedora core 1, if it matters. I am having problems
finding conversions for two C++ conventions:

1. In C++ I can declare a variable as a bool, wheras that gives me an error
with gcc

Recent versions of gcc support the C99-style boolean types. If you
have a "#include <stdbool.h>", you can use the type bool and the
constants true and false. Without that, you can use _Bool and the
constants 1 and 0. (This may also depend on the runtime library,
which is separate from the compiler.)

Since this is probably a fairly recent addition to gcc, it shouldn't
be considered portable. C90 doesn't explicitly support boolean
variables, but it's easy enough to roll your own. See section 9 of
the C FAQ said:
2. Is there a way of using iostream.h in C? So that I can use cin, cout,
endl, etc...

Not really. You might conceivable define a C interface to C++'s
iostream, but using cin and cout really depends on overloading the
"<<" and ">>" operators. A C interface would have to use different
names for operators to print an int, a string, a double, etc; the
result would be much more unwieldy than C's stdio.
 
M

Michael Mair

Hiho,

Erica wrote:
[snip]
I have lots of experience with C++, but very little experience with regular
ol' C. I am using gcc on Fedora core 1, if it matters. I am having problems
finding conversions for two C++ conventions:

1. In C++ I can declare a variable as a bool, wheras that gives me an error
with gcc

In C89/90, there is no Boolean type.
In C99, there is _Bool which can have the values 0 and 1.
If you #include <stdbool.h>, then bool, false and true are defined,
respectively.
<OT>
Note that this works in newer gcc versions but gcc still is not
C99 standard compliant, see also
http://gcc.gnu.org/c99status.html
You will get the closest approximation gcc is able to provide
by the options "-std=c99 -pedantic".
</OT>
For C89, you can just use another integer type.

2. Is there a way of using iostream.h in C? So that I can use cin, cout,
endl, etc...

Unfortunately not. From the C++ point of view, C input and output
are just a terribly oldfashioned and crude desaster. You will have to
make do with what you have in C's stdio.
<OT>
On your system, "man stdio" may give you an overview over definitions
and functions you get by #include <stdio.h>.
Otherwise, get a C book.
</OT>


Cheers
Michael
 
P

pete

Erica said:
2. Is there a way of using iostream.h in C?
So that I can use cin, cout, endl, etc...

No.
iostream.h and cin, cout, endl, etc...
are all very much C++ features.

You need stdio.h to even just write the Hello world program in C.
 
B

bd

Erica said:
Hi all,

I apologize in advance for the newbishness of my questions. However, I've
done quite a bit of googling and couldn't find any answers.

I have lots of experience with C++, but very little experience with
regular ol' C. I am using gcc on Fedora core 1, if it matters. I am having
problems finding conversions for two C++ conventions:

1. In C++ I can declare a variable as a bool, wheras that gives me an
error with gcc

There is no bool type in C. But, you can do, for instance:
typedef int bool;

Or just use an int.
2. Is there a way of using iostream.h in C? So that I can use cin, cout,
endl, etc...

Nope. That's C++ only - C doesn't support things like classes or operator
overloading. Use stdio instead.
 
J

John Bode

Erica said:
Hi all,

I apologize in advance for the newbishness of my questions. However, I've
done quite a bit of googling and couldn't find any answers.

I have lots of experience with C++, but very little experience with regular
ol' C. I am using gcc on Fedora core 1, if it matters. I am having problems
finding conversions for two C++ conventions:

1. In C++ I can declare a variable as a bool, wheras that gives me an error
with gcc

C89 doesn't have a boolean type. I think C99 does, but I don't think
gcc is C99 compliant yet. Traditionally, boolean values are
implemented with an integral type, with 0 indicating false and non-0
indicating true. There used to be preprocessor macros TRUE and FALSE
that were defined as 1 and 0, respectively, but apparently they've
been deprecated (if they were ever standard).
2. Is there a way of using iostream.h in C? So that I can use cin, cout,
endl, etc...

No. Stream I/O in C is nothing like stream I/O in C++. You're stuck
with stdin, stdout, stderr, and the I/O functions *printf(), *scanf(),
gets(), fgets(), fgetc(), puts() fputs(), fputc(), etc.
 
M

Malcolm

Erica said:
1. In C++ I can declare a variable as a bool, wheras that gives me an error
with gcc
You can include a header, or typedef a type as a boolean.

However bool breaks libraries. Why? Because it introduces dependencies for
something that should be a fundamental language feature. Pretty soon you
have "bool" in your maths library, "BOOL" in your windowing API, bool_t in
some file handling package, and when you add the compression module it
complains about a redefinition of "BOOL". A nightmare. So declaring any
boolean type publicly is a really bad idea.
2. Is there a way of using iostream.h in C? So that I can use cin, cout,
endl, etc...
No, unless you want to call C++ mangled names as C functions. You can call C
from C++ very easily, but it is extremely difficult to get C code to call
C++ code.
 

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

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top