C?

Started by
28 comments, last by elis-cool 22 years ago
Considering I write almost solely in C (a little ASM and a little C++), the example

  	char *pc = malloc( 100 );  

is bad C, IMO.

and the previous example, defining foo with no type parameter type information (thus making it assume variable parameters) is also bad C.
I am a proponent of C, but the C code I write will compile fine with a C++ compiler (which is what it does at home, since all my files are cpp files since I have to use DX''s classes). Does that mean I write in C++?

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Advertisement
Standard C can be compiled on Standard c++ compilers (unless there are variable names that are standard c++ keywords, which can be handled by simple #defines )
daerid@gmail.com
quote:Original post by daerid
Standard C can be compiled on Standard c++ compilers (unless there are variable names that are standard c++ keywords, which can be handled by simple #defines )


No, it can''t, because there are valid C programs that are ill-formed C++ programs.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Those are specific cases. Saying that "C can''t be compiled on a c++ compiler" is just too generalized. More correctly it would be: "There are some C programs that can''t be compiled with a C++ compiler".

The C++ standard basically says you should be able to compile a C program on a C++ compiler.

But like everything, there are exceptions to the rule.
daerid@gmail.com
quote:Original post by daerid
Those are specific cases.

They''re extremely common. For instance, any code that uses malloc() has a high probability of breaking (in C, it''s never correct to cast malloc()''s return value; in C++, it''s mandatory except for when assigning to void* pointers).

quote:Saying that "C can''t be compiled on a c++ compiler" is just too generalized.

Fine. ANSI C can''t be compiled on a C++ compiler. Better?

quote:More correctly it would be: "There are some C programs that can''t be compiled with a C++ compiler".

IOW, a C++ compiler cannot be used as a C compiler.

quote:The C++ standard basically says you should be able to compile a C program on a C++ compiler.

It says nothing of the sort.

quote:But like everything, there are exceptions to the rule.

Exceptions, such as writing ANSI C code.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
quote:Original post by BeerNutts
Considering I write almost solely in C (a little ASM and a little C++), the example
[...]
is bad C, IMO.

Since it was set forward for exposition purposes only, I don''t see how that invalidates the point.
quote:
Does that mean I write in C++?

With the proviso that your compiler does not define the C++ language, yes it does. Whether it means that the C++ code is the best match to the problem at hand, given the full range of C++ features, is another question entirely.

[C++ FAQ Lite | ACCU | Boost | Learning C++]
SabreMan:

Fine. Then, I write C++ code and compile it with a C compiler.

Sounds kinda strange, but according to you (since my C code will compile under C++ (I always call malloc with a cast)) it''s all true.

DrPizza
quote:ANSI C can''t be compiled on a C++ compiler.


To say you can''t compile ansi C code with C++ is absurd.

  int Function(void){    printf("I suck\n");    return 1;}  

is that ansi C code? yes. Will it compile under a C++ compiler? yes.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

quote:Original post by BeerNutts
Fine. Then, I write C++ code and compile it with a C compiler.

Sounds kinda strange, but according to you (since my C code will compile under C++ (I always call malloc with a cast)) it's all true.

If you are writing code that adheres to the common subset of C and C++, then it's technically correct to say you are simultaneously writing C *and* C++ code.
quote:
To say you can't compile ansi C code with C++ is absurd.

DrPizza should have said *not all* ANSI C can be compiled on a C++ compiler. That has been the point all along, and was intended as a refutation of an earlier comment, which said "C++ is superset of C and C can be compiled on a C++ compiler so the differences cannot be too vast". I really don't think it's worth belabouring the "superset" point any further. It's not *that* important.

What is important, however, is to recognise that C++ offers some very different features to C, and a program written in terms of C++ is likely to be very different indeed to a program written in C that achieves the same end. Drawing the conclusion that the differences between the 2 languages is not "too vast" because they share a common subset is not logically sound, and it's a view that's damaging to the s/w industry in general.

[C++ FAQ Lite | ACCU | Boost | Learning C++]

[edited by - SabreMan on April 17, 2002 4:52:58 PM]
Uh, answer his question.

My guess would be is that coders that code in C really dont care what you think and would rather code in what they prefer or think would suit them better. If they are an extraordinary C programmer I dont see why they would use C++ if they are only good at it, see my point? Obviously it has its advantages but whatever they felt C had over C++ was certainly enough to make them use C. No need to take their language choice personal.
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
quote:Original post by ImmaGNUman
[... stuff ...]

Err... the question was "what are exactly the differences between C and C++". The best answer that can be given here is that there have been many books written about the topic, and we can''t possibly cover the details in this forum. Saying "answer his question" does not make it any easier to actually answer the question.

quote:
My guess would be is that coders that code in C really dont care what you think

Nobody''s attacking C.

[C++ FAQ Lite | ACCU | Boost | Learning C++]

This topic is closed to new replies.

Advertisement