Am i a C programmer or C++?

Started by
8 comments, last by thankqwerty 20 years, 8 months ago
I''ve been learning C for 2 years(part of my course). And i dont know anything about C++ at all. Until recently i tried to write my own game, so i downloaded some of the online tutorials(from http://www.gametutorials.com/Tutorials/Win32/), got myself a copy of VC++ 6.0 and started my game in WIN32. I''m about 1/3 of my game now, and i did everything in my normal C fashion, but i save my code with .cpp extension so i thought i''m programming in C++. As a result, I thought C and C++ isn''t that much different after all, at least i when i do the same thing my program still works. Then i came to this forum and see lots of post about C/C++, how different they are, why u should learn one instead of the other, the bad habbits when u switch from one to another. I''m a bit confused now(my code is very much similar to those tutorials),so am i programming in C or C++? does the extension of the source code file matter? If i am programming in C now, should i change to C++? and how can i know i''m actually doing it in C++??? As english is not my 1st language, u may not be able to understand what i mean, but basically my main question is if u give me a piece of code how can i tell if it is a C++ code or C code?
Advertisement
Yes it is a bit confusing at first. The creator of the language, Bjarne Stroustrup, wanted to make C++ as compatible with C as he could in order to make converting over from C to C++ as easy as possible. I highly recommend his book "The C++ Programming Language." Have a look at Bjarne's FAQ, titled "What is the difference between C and C++."

Oh, as an additional note. You are probably compiling your code as C++ code (this is what VC++ sets as the default, though you can change it to compile as C code). I've seen a lot of so called C++ code that is actually just C code compiled as C++ code. Since C++ is closely compatible with C though and compiles most C code it makes the line between the two a little more fuzzy. Here is one example of code that compiles in C++:

C Code (uses the <stdio.h> library)

char* name = "John";
printf("%s", name);


C++ Code (uses the <string> and <iostream> libraries)

std::string name="John";
std::cout<<name;


Oops, html mistake.

[edited by - lshadow on August 8, 2003 4:41:50 PM]
Working on: DoP
When people say "C or C++?" most of the time what they're really refering to is "procedural or object-orientated?".[1] Since C++ is a superset of C, you could take any old C project and compile it with a C++ compiler, whether it complains about the file extensions is a irrelevency.

So the real question is, are you programming in an object orientated style? If you have to ask the question, odds are you're not - go and google it and look into the subject, even if you decide not to you'll know you've made the right choice.


[1] : Yes you can program procedural C++, or object orientated C, or several other options, but thats what the typical meaning is.

[edited by - OrangyTang on August 8, 2003 2:03:05 PM]
quote:Original post by OrangyTang
Since C++ is a superset of C


Nitpick... but try compiling this (pointless) C program with a C++ compiler.

#include <stdbool.h>#include <stdio.h>struct my_struct{  int a;  int b;  int c[50];}int main(int argc, char** argv){  _Bool someBool = 1;  long long bigNumber;  int a,b;  struct my_struct stuff = {.b = 50, .a = 123};  struct my_struct moreStuff = {.c[20] = 33};    a = b = 5;  int myArray[a*b];  int * class = (int[]){1,2};  int * restrict class_ptr = class;  printf("Hi, welcome to %s.\n", __func__);  return 0; } 

quote:Original post by OrangyTang
When people say "C or C++?" most of the time what they''re really refering to is "procedural or object-orientated?".
"Object-oriented." "Orientated" has to do with sense of direction.

C++ is not a strict superset of C, unfortunately. C99 introduced features that break C++, and I expect the two languages to continue to grow apart, so don''t get too used to the notion. Furthermore, the differences in C and C++ programming don''t refer simply to procedural vs OO but also to notions of type safety, common language constructs (do you need to typedef a struct? yes in C, no in C++) and conventions (C supports some interesting function prototype syntaxes; doesn''t require that a function declared with a return type necessarily return a value - an assumption is made, which is anathema to C++''s focus on typesafety; widely performs implicit type conversions and so forth). Standards-enforcing C++ compilers will spew dozens of warnings and errors on standard C.
C++ is "multi-paradigm" - OO isn't a requirement to write C++ code. You could easily write procedural code and it would still be C++ code (an example is above using the iostream & string libraries).


A .c extenion is supose to indicate that it is C code, and cpp or cxx indicates it is C++ source code.

If the code compiles with a C compiler, it's C code. If it compiles with a C++ compiler, it's C++ code. (If it compiles with lots of different compilers, it's portable code.).

The same code can be both C and C++ code.

[edited by - Magmai Kai Holmlor on August 8, 2003 4:06:42 PM]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:Original post by Oluseyi
quote:Original post by OrangyTang
When people say "C or C++?" most of the time what they''re really refering to is "procedural or object-orientated?".
"Object-oriented." "Orientated" has to do with sense of direction.


/me slaps forehead.
Damn it, i can''t believe i let that silly typo in. Consistant thoughout though :S

I get the impression that everyones nitpicking (valid points, but none the less nitpicking ) will probably confuse the OP even more though. I guess the conclusion you could draw is that its not a black and white issue.
quote:Original post by Magmai Kai Holmlor
C++ is "multi-paradigm" - OO isn''t a requirement to write C++ code. You could easily write procedural code and it would still be C++ code (an example is above using the iostream & string libraries).


A .c extenion is supose to indicate that it is C code, and cpp or cxx indicates it is C++ source code.

If the code compiles with a C compiler, it''s C code. If it compiles with a C++ compiler, it''s C++ code. (If it compiles with lots of different compilers, it''s portable code.).

The same code can be both C and C++ code.


I totally agree with you, my C AsciiRis game (in my sig) turned out to be C++ by accident and now its technically C++ because it doesn''t compile with the .c extension.

(i realize i could have avoided this by using the .c extension to begin with)

An ASCII tetris clone... | AsciiRis
An ASCII tetris clone... | AsciiRis
dunno about your compilers guys, but as soon as I compile a file named *.c it''s automatically compiled as a C program and not a C++ program...

Oh, and Ive always thought of what you call C++ as C/C++ and not C and C++ as two separate languages. When the day comes when you can''t write a program with C syntax in C++ I will gladly call "C/C++" only C++...

Also std::string and anything inside the std namespaces has nothing to do with the language C++ except that it''s written in C++. std::string or whatever isn''t part of the C++ language until they are keywords or considered as datatypes just like char or int or whatever....
quote:Original post by Anonymous Poster
When the day comes when you can''t write a program with C syntax in C++ I will gladly call "C/C++" only C++...


That day has come.

#include <stdlib.h>#include <string.h>typedef struct {  int n;  int array[];} MyStruct;int main(void) {  long double _Complex a = {1, 1};  _Bool b = 1;  long long c = 1;  int n = 50;  long array[n];  for (size_t i = 0; i < n; i++) { array[i] = i; }  int template = 5;  int bool = 5;  int new = 5;  int class = 5;  MyStruct *something = malloc((sizeof *something) + n * sizeof *something->array);  *something = (MyStruct){ .n = n };  memcpy(something->array, array, sizeof array);}


All perfectly legal C (syntax and otherwise), but I don''t think it''s legal C++.

This topic is closed to new replies.

Advertisement