How much do i know C if i am really good at C++

Hello all ,
simple question. next semester I am going to take a computer organization class.
This class is taught using C language.
I have been programming in C++ for sometime.
How much of C i know already if I am programming C++ for this time ?
Thank you.
Last edited on
C and C++ are quite a lot alike. So you'll find you know most parts of the C language if you know C++. There are a few incompatibilities though:

First of all, anything related to classes, namespaces, inheritance etc. cannot be used in C. So you'll need to get into the habit of having a lot of global functions operating on structs and other primitive data types. It might also be a good idea to come up with a naming convention for these functions, since you might lose track of what a function does (although comments will help here).

Second of all, in C the keyword struct must be repeated each and every time you use a struct type. For example take this C++ code:

1
2
3
4
5
6
7
8
struct somestruct
{
    int a, b;
};

//Somewhere in a function
somestruct value;
value.a = 2;


Becomes this in C:
1
2
3
4
5
6
7
8
struct somestruct
{
    int a,b;
};

//Somewhere in a function
struct somestruct value;
value.a = 2;


Since it's annoying to repeat struct that much, C programmers usually tend to use a "typedef struct" to solve this:

1
2
3
4
5
6
7
8
typedef struct somestruct
{
    int a, b;
} SomeStruct;

//Somewhere in a function
SomeStruct value;
value.a = 2;


Third of all, C's standard library is a lot smaller than C++'s. You can take a look at the reference on this site. Go to the "C library" part of it. That's all you have in C. It takes a while to get used to not being able to use any form of standard container.

But most of the time you'll be able to understand C quite well. The primitive data types are the same, functions, pointers, arrays are all the same. You should be able to understand C well enough.

I don't know if with C you mean the C99 standard though. If not, there's one last thing to consider, the placing of variables. In C++ you can declare a variable nearly everywhere:

1
2
3
4
int someint = 5; //I need an int here
somefunction(4 + someint);
int anotherint = 8; //And another one here
somefunction(someint-anotherint);


Before the C99 standard this is illegal in C. Instead, all variables will have to be declared at the start of a block, meaning code looks more like this:

1
2
3
4
5
int someint = 5; //I need an int
int anotherint = 8; //I'll need this later on
//Now all variables are declared, write the actual code
somefunction(4 + someint);
somefunction(someint - anotherint);


There are a few annoying little incompatibilities between the languages. Like how C performs an implicit cast from void* whereas C++ doesn't. But most of the time, you should be fine when you know C++.
Somethings that you'll miss in C:
- there are no container classes. Want a list? Write it yourself.
- there is no String class. You have to use c strings.
- there is no streams library. You'll be doing I/O with read/write, fread/fwrite. and printf/scanf.

This may seem horible until you look at the size of your code. It will be much smaller in C than in C++
I'm working with C++, never paid attention to C, from experience I can tell, I understand what C does just by reading/debugging source code written in C so far, even though I never really do anything in C.

C is simple to learn and understand if you're good at C++.
codekiddy
Do I still need to learn C ?
Last edited on
closed account (E0p9LyTq)
Aziz as wrote:
Do I still need to learn C ?


No.

Should I learn C before learning C++?
https://www.quora.com/Should-I-learn-C-before-learning-C++?share=1

Somethings that you'll miss in C:
- there is no streams library. You'll be doing I/O with read/write, fread/fwrite. and printf/scanf.


Kind of a tangent. But why are streams so much better than printfs and scans? They do the same thing on the surface. Is it mostly error handling?
Last edited on
Type safety.

It's hard to accidentally treat a double like a pointer-to-char with a C++ style stream. Not so hard with the printf/scanf family of functions.

Type safety.

It's hard to accidentally treat a double like a pointer-to-char with a C++ style stream. Not so hard with the printf/scanf family of functions.


Yeah, that just sounds really annoying hearing about it.
FurryGuy wrote:

Do I still need to learn C ?

No.

I disagree. If you want to be valuable to an employer, you should be able to work in C just as easily as you work in C++. Many existing code bases are C, not C++.

A few of the things I miss most when working in C and don't have the option of rewriting in C++ or calling out to C++:
- Streams (already mentioned)
- Exceptions
- Operator overloading
Last edited on
Another thing you might run into when using C is linkage problems caused by C not mangling your names (one of the few examples name mangling is good for, and the entire reason it exists).

For example, if you declare this function somewhere in a C file:

 
int readFromFile(FILE* file);


And then declare this in another file:

 
int readFromFile(const char* filename);


you will get an error when linking, even though these functions are in different files. The linker will see readFromFile implemented twice and give you an error. Or even worse, if you forgot to implement one of them the linker will just let you compile it, and the const char* version will use the FILE* version or the other way around. It may seem like a minor detail, but on larger projects this might become a larger problem.

If you declare them in the same file, you'll get an error anyways since C doesn't have function overloading. Another advantage of C++ over C.
Last edited on
Kind of a tangent. But why are streams so much better than printfs and scans? They do the same thing on the surface. Is it mostly error handling?

To me, the really nice things about streams is that that they are so damn flexible. If you write code to deal with a stream, it will work with a file stream, a string stream, or a Debug stream in my particular line of work.

Separating the stream formatting (stream) from the I/O (streambuf) is definitely one of the more clever things in C++
closed account (E0p9LyTq)
AbstractionAnon wrote:
I disagree. If you want to be valuable to an employer, you should be able to work in C just as easily as you work in C++. Many existing code bases are C, not C++.


I was addressing the answer in terms of someone learning C++, a beginner.

After learning more than the basics of C++, then learning the differences between C and C++ approaches to programming is a plus when one is looking for employment.

You addressed the question in a different manner than I did. So really is there a disagreement? I don't think so. ;)
Topic archived. No new replies allowed.