Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why would someone use c++ compiler on a C code?

Reply
Thread Tools

Why would someone use c++ compiler on a C code?

 
 
Ian Collins
Guest
Posts: n/a
 
      04-08-2008
Richard Heathfield wrote:
> Ian Collins said:
>
>> Richard Heathfield wrote:
>>> Ian Collins said:
>>>

> <snip>
>>>> "/tmp/x.c", line 81: warning: implicit function declaration: toupper
>>>> "/tmp/x.c", line 86: warning: implicit function declaration: tolower
>>> Neither of these diagnostic messages is mandatory: implicit int is legal
>>> in C...
>>>

>> Read them again "implicit function declaration". Your forgot <ctype.h>

>
> No, I didn't. I just mis-typed "implicit function declaration".
> Implicit function declaration is, of course, legal in C90 (C).
>

Now where did I leave my Tardis?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Kelsey Bjarnason
Guest
Posts: n/a
 
      04-08-2008
[snips]

On Mon, 07 Apr 2008 22:41:49 -0700, Don Bruder wrote:

> A C++ compiler *IS*, by definition, a C compiler.



#include <stdlib.h>

int main(void)
{
char *s = malloc(100);
if ( s ) free( s );
return 0;
}

gcc -Wall -ansi -pedantic test.c compiles clean. Note the use of the
proper C idiom of *not* casting the return of malloc, which is both
intentional and correct.


rename it to test.cpp, recompile with g++ and...

test.cpp: In function ‘int main()’:
test.cpp:5: error: invalid conversion from ‘void*’ to ‘char*’


Hmm. A trivial 8 line program - if you count braces and white space -
and it won't compile in C++.

Explain to us, then, how to get it to compile this perfectly valid C
program. No, not a modified version with casting in direct violation of
proper C programming practise, show us how to get a C++ compiler to
compile *that* program. If, as you assert, a C++ compiler is a C
compiler, the code should compile. It doesn't. It never will. It
cannot, as it is _C_ code, not _C++_ code and the two are not the same at
all.

Don't like that one? Try this:

#include <stdio.h>

int main(void)
{
int new = 3;
printf( "%d\n", new );
return 0;
}

Again, compiles clean with gcc. Save it as a C++ file and compile it
with g++ and...

test.cpp: In function ‘int main()’:
test.cpp:5: error: expected unqualified-id before ‘new’
test.cpp:6: error: expected type-specifier before ‘)’ token
test.cpp:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has
type ‘int*’

Err what? argument 2 has type int *? No it doesn't, it has type int;
says so right there: int new = 3; No pointers at all.

You know, for a C compiler, g++ is pretty damn bad. Makes a fine C++
compiler, but it absolutely *blows* at compiling C code. If, as you say,
it is by definition also a C compiler, why does it suck so badly at
compiling C code?

 
Reply With Quote
 
 
 
 
Charlton Wilbur
Guest
Posts: n/a
 
      04-08-2008

MMcL> "Martin Ambuhl" <(E-Mail Removed)> wrote in message
MMcL> news:(E-Mail Removed)...

>> Don Bruder wrote:


>> A C++ compiler *IS*, by definition, a C compiler. PLUS some
>> stuff.


> The above is complete nonsense. It is unbelievable that someone
> who knows so little can write so much with a presumed air of
> authority. Until we see evidence that Don Bruder has learned
> something, it is safer to ignore him. For example.


MMcL> Manners, manners, manners. Don Bruder is relatively new to
MMcL> the group and what he said is an easy mistake for a
MMcL> relatively well-informed person to make.

Only if your definition of "relatively well-informed" is so vague as
to be useless; someone who thinks C++ is a proper superset of C,
modulo keywords, isn't very well-informed about either language.

And calling it complete nonsense isn't rude; neither is pointing out
that people who write complete nonsense to this group are frequently
corrected but otherwise ignored.

Charlton


--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


--
Charlton Wilbur
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
dj3vande@csclub.uwaterloo.ca.invalid
Guest
Posts: n/a
 
      04-08-2008
<OT>

In article <(E-Mail Removed)>,
2005 <(E-Mail Removed)> wrote:
>On Apr 7, 9:56 pm, 2005 <(E-Mail Removed)> wrote:


[Subject: Why would someone use c++ compiler on a C code?]

>> Would it suppress errors?
>>
>> Is he trying to hide errors in his code?

>
>He is using g++ on a *.c code!


g++ will act as a C compiler when given the file 'foo.c', unless you
specifically ask it to do otherwise.
Any of the gcc or g++ manuals, a GCC-specific newsgroup, or possibly a
Linux programming newsgroup should be able to give you the details.


>Also what is so different between gcc & cc compiler - any advantages?


gcc will always give you the GNU C compiler, unless the system was
deliberately set up to be confusing and/or broken.
cc will usually give you the system C compiler, which, where it isn't
GCC (on Linux systems and some others it is), can probably not
reasonably be expected to understand GCC-specific language extensions
and command-line arguments.


</OT>

dave

--
Dave Vandervies dj3vande at eskimo dot com
But, Greg, this thread just kicked out /classes/ because they were too big and
clumsy and bloated, so how do you think we're likely to feel about *templates*?
--Richard Heathfield in comp.lang.c
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-08-2008
(E-Mail Removed)lid writes:

> <OT>
> In article <(E-Mail Removed)>,
> 2005 <(E-Mail Removed)> wrote:
>>On Apr 7, 9:56 pm, 2005 <(E-Mail Removed)> wrote:

>
> [Subject: Why would someone use c++ compiler on a C code?]

<snip>
>>He is using g++ on a *.c code!

>
> g++ will act as a C compiler when given the file 'foo.c', unless you
> specifically ask it to do otherwise.

<snip>
> gcc will always give you the GNU C compiler, unless the system was
> deliberately set up to be confusing and/or broken.


gcc will return the favour -- i.e. it will compile a C++ program if
the source is called, for example, x.cc.

I don't think you meant to imply otherwise, but some people might read
what you said as suggesting gcc is always a C compiler. I find it
helps always to use -std=c89 or -std=c99 etc. That way you get told
if you feed it a file that it takes to be in another language.

> </OT>


--
Ben.
 
Reply With Quote
 
Kelsey Bjarnason
Guest
Posts: n/a
 
      04-08-2008
[snips]

On Tue, 08 Apr 2008 15:32:47 +0100, Malcolm McLean wrote:

>> gcc -Wall -ansi -pedantic test.c compiles clean. Note the use of the
>> proper C idiom of *not* casting the return of malloc, which is both
>> intentional and correct.
>>
>>

> Actually that idiom was retained, reluctantly, to avoid breaking reams
> of legacy code, as committee members have informed us in previous posts
> to this ng.


The committee has precisely zero authority in terms of defining this as
good practise or otherwise. What defines it as good practise or bad is
whether it works, and why.

Out there in the real world, there exist compilers which do *not* warn
you about failure to include appropriate headers, but which will - are
required to - warn about invalid assignments, such as from int to pointer.

If and when one's code can be reasonably certain of never, ever
encountering such a compiler in the code's entire lifetime, then and only
then will this cease to be a useful idiom.

Maybe this will come when C90 compilers are dead and buried, but given
the sickly snail's pace with which C99 has been adopted, I wouldn't count
on it happening tomorrow.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      04-08-2008
Malcolm McLean wrote, On 08/04/08 15:32:
>
> "Kelsey Bjarnason" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> [snips]
>>
>> On Mon, 07 Apr 2008 22:41:49 -0700, Don Bruder wrote:
>>
>>> A C++ compiler *IS*, by definition, a C compiler.

>>
>>
>> #include <stdlib.h>
>>
>> int main(void)
>> {
>> char *s = malloc(100);
>> if ( s ) free( s );
>> return 0;
>> }
>>
>> gcc -Wall -ansi -pedantic test.c compiles clean. Note the use of the
>> proper C idiom of *not* casting the return of malloc, which is both
>> intentional and correct.
>>

> Actually that idiom was retained, reluctantly, to avoid breaking reams
> of legacy code, as committee members have informed us in previous posts
> to this ng.


I can't remember any such post. In fact, void* was *invented* by the
standards committee specifically to get rid of the need for casts in a
number of places http://www.lysator.liu.se/c/rat/c2.html#3-2-2-3
Note this quote from there...
| ... Adoption of this type was stimulated by the desire to specify
| function prototype arguments that either quietly convert arbitrary
| pointers...

> One effect is that if used in new code, the program will no longer
> compile under C++.


Well, seeing as new C code is C code rather than C++ this is not a problem.
--
Flash Gordon
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      04-08-2008
In article <(E-Mail Removed)>,
Martin Ambuhl <(E-Mail Removed)> wrote:
>Don Bruder wrote:
>> In article
>> <(E-Mail Removed)>,
>> 2005 <(E-Mail Removed)> wrote:
>>
>>> Would it suppress errors?
>>>
>>> Is he trying to hide errors in his code?

>>
>> A C++ compiler *IS*, by definition, a C compiler. PLUS some stuff.

>
>The above is complete nonsense. It is unbelievable that someone who
>knows so little can write so much with a presumed air of authority.
>Until we see evidence that Don Bruder has learned something, it is safer
>to ignore him. For example.


It really ****es the regs off when intelligent people come on here and
express views not in accord with the party line. They are soon branded
"trolls" and most (but not all) leave soon after.

>> In general, unless you're trying to compile C++ code (in which case, a
>> "plain C" compiler just can't cope) it really doesn't much matter if you
>> use a "plain" C compiler, or a C++ compiler on a straight C source.

>
>this is obvious bullshit.


Yup, Marty. Always the charmer, eh?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why is a JIT compiler faster than a byte-compiler RickMuller Python 4 03-26-2005 04:30 PM
why would someone WANT the Bogen 3029 head? Jimmy Smith Digital Photography 2 08-03-2004 05:13 AM
Can we use <compiler> tag to avoid RunTime Compiler error? Jack Wright ASP .Net 5 01-19-2004 04:36 PM



Advertisments