Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411576 Posts in 69386 Topics- by 58445 Members - Latest Member: Mansreign

May 05, 2024, 08:36:38 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Going to C from C++
Pages: [1] 2
Print
Author Topic: Going to C from C++  (Read 3346 times)
Problem Machine
Level 8
***

It's Not a Disaster


View Profile WWW
« on: August 23, 2009, 05:23:33 PM »

I just applied for a programmer position without paying too close attention to the description, and it turns out it's a C programming position. They sent me a link to a programming test thing I'm supposed to take to apply for the position, and I have a little bit of time to study up before I actually take it.

Now, I've been spending a lot of time brushing up on my C++, but I don't really know a lot about C. I got a lot of the basics from this article, which is a good start; but stuff like this is only good for the basic differences in language, and not so much for the best practices expected of a programmer using it. So basically, uh, what do you guys think I should know?

edit: This article is also useful, for anyone else trying to do the same thing.
« Last Edit: August 23, 2009, 05:59:47 PM by Kobel » Logged

mirosurabu
Guest
« Reply #1 on: August 23, 2009, 05:37:46 PM »

Various data-types/structures which are commonly not used in C++. Arrays, structs, unions, linked lists and so on.

Dynamic memory allocation. Malloc, realloc, free and bunch of other stuff.

C strings. Pain in the ass. Get to know that standard C library for string manipulation.
Logged
Problem Machine
Level 8
***

It's Not a Disaster


View Profile WWW
« Reply #2 on: August 23, 2009, 05:53:16 PM »

I'm familiar with most of this stuff, but what are unions?
Logged

mirosurabu
Guest
« Reply #3 on: August 23, 2009, 05:58:23 PM »

Sorry for my post, I actually realized I was missing your point after the post was approved, but oh well.

Anyways, unions are like structs except members share same address space. They act like variant data type. It's less used but I thought it's worth a mention.

As for best practices, procedural programming practices apply here.
Logged
Crackerblocks
Level 1
*



View Profile WWW
« Reply #4 on: August 23, 2009, 06:25:04 PM »

That article is a good.

I'd probably also make sure to know about function pointers. And for a design pattern: how to do OO programming techniques in C using function pointers, unions, void*, type ids, etc.

Best practices: I found that I used asserts a lot more in C. With all the pointers flying around, you get less compile time code validation.

That's all I can think of for now.

I'm curious what the job description is. Some kind of firmware programming?
Logged
Problem Machine
Level 8
***

It's Not a Disaster


View Profile WWW
« Reply #5 on: August 23, 2009, 07:16:30 PM »

Quote
I'd probably also make sure to know about function pointers.
Are they the same in C as in C++?
Quote
I'm curious what the job description is. Some kind of firmware programming?
No it's just gameplay programming for an MMO. No idea why they decided to go with C instead of C++.
Logged

Overkill
Level 3
***


Andrew G. Crowell


View Profile WWW
« Reply #6 on: August 23, 2009, 07:28:01 PM »

Function pointers are exactly the same in C++ as C. Except there's obviously no method pointers, since there is no object orientation.

Also, you should learn to mimic the class-style organization that C++ gives you by using functions that take a pointer as their first argument. It kept a lot of my C code organized, and it's always better to avoid global state when you can avoid it.

So in C++ you might have:
Code:
class Monster
{
    public:
        Monster(int x, int y);
        ~Monster()

        void Update();
        void Render();
        void Teleport(int x, int y);
    private:
        int x, y;
}

Translating this to C:
Code:
typedef struct Monster
{
    int x, y;
} Monster;

Monster* MonsterNew(int x, int y);
void MonsterFree(Monster* monster);

void MonsterRender(Monster* monster);
void MonsterUpdate(Monster* monster);
void MonsterTeleport(Monster* monster, int x, int y);
« Last Edit: August 23, 2009, 07:33:30 PM by Overkill » Logged

Problem Machine
Level 8
***

It's Not a Disaster


View Profile WWW
« Reply #7 on: August 23, 2009, 08:14:03 PM »

It just all seems so silly, if you're gonna be doing all that why not just use C++ in the first place  Shrug
Logged

Crackerblocks
Level 1
*



View Profile WWW
« Reply #8 on: August 23, 2009, 09:12:15 PM »

Are they the same in C as in C++?
err yup.

It just all seems so silly, if you're gonna be doing all that why not just use C++ in the first place  Shrug

Pretty much sums up why this is a confusing conversation.

Maybe it actually is a C++ job, but they want to make sure you have a good grasp of lower level concepts.
Logged
Overkill
Level 3
***


Andrew G. Crowell


View Profile WWW
« Reply #9 on: August 23, 2009, 09:37:21 PM »

It just all seems so silly, if you're gonna be doing all that why not just use C++ in the first place  Shrug

Well, I definitely agree that C++ is a much better way to go. Sadly, some people are wankers and consider some of C++ flexibilities "dangerous" and horrible, like operator overloading and templates and stuff.

On the other hand, C++ provides better opportunity for compile-time error checking, inlining, private/public scoping, object-oriented virtual dispatch (can be mimicked in C, but very very unrecommended), type-safe polymorphism, built-in data structures with way nicer time/memory constraints.

C is littered with bad design decisions.

C strings are terrible. Getting the length of a string is an O(n) operation, and buffer overflow is very easy to have happen due to all char* operations looking for a \0, instead of length-delimiting which is much more friendly. C++ fixes this with std::string.

malloc is lame because it's not typesafe, you just get a void*. So, if you refactor, and somehow misspecify the size of the data you're allocating, you're boned. C++ has new instead, which resolves to a type at compile-time and figures out the size for you!

printf/scanf are admittedly handy, but they're plagued with problems too.

The way I see it, you only use C when you're system-level code (or job mandate, as in this case), or writing for some sort of platform that doesn't have a good C++ compiler. Otherwise, use C++ code, mixing in C only when full-on C++ becomes cumbersome.
« Last Edit: August 23, 2009, 09:40:36 PM by Overkill » Logged

mcc
Level 10
*****


glitch


View Profile WWW
« Reply #10 on: August 23, 2009, 10:54:58 PM »

Unless you really make a lot of use of either the STL, templates, or object polymorphism, you're going to find C and C++ mostly indistinguishable. C++ is a superset of C, remember. As Overkill says you're mostly going to find yourself just saying monster_iterate(monsterstruct) instead of monster->iterate() a lot and malloc instead of new. The theory that this is really a C++ job but they just listed it as C (either by accident or because they don't make heavy use of C++ features) is believable.

Use of unions in the real world is pretty rare. Function pointers get used in some development teams but not others, but they're still a good thing to know about going into a job interview. Although they're the same in C as C++ pure-C houses are more likely to use them because when you don't have polymorphism you wind up more tempted to use callbacks. Do be familiar with void *.

The interviewer might not even mind you outright saying "well my experience is mostly with C++, but that leaves me well equipped to do C" etc.
Logged

My projects:<br />Games: Jumpman Retro-futuristic platforming iJumpman iPhone version Drumcircle PC+smartphone music toy<br />More: RUN HELLO
Problem Machine
Level 8
***

It's Not a Disaster


View Profile WWW
« Reply #11 on: August 23, 2009, 11:01:30 PM »

Quote
Unless you really make a lot of use of either the STL, templates, or object polymorphism
Ech, I do use a fair amount of STL and polymorphism. STL is no big deal, because it's really not too difficult to implement one's own linked list or use an array instead of a vector, but polymorphism is a pain. I guess I just need to get the feel for more procedural style programming...
Logged

Schtee
Level 0
***


View Profile
« Reply #12 on: August 24, 2009, 01:48:49 AM »

God. Hope for your sake that their HR department just used C as short-hand for C++. I saw your code Grin
Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #13 on: August 24, 2009, 05:29:50 AM »

C++ is a superset of C, remember.

Actually it isn't, and knowing this might be important for a job interview.  There are many examples of C89 code that are not legal C++ code, and the C99 standard deviates significantly from C++.  The upcoming C1X standard will likely increase the divide even further.
Logged



What would John Carmack do?
Problem Machine
Level 8
***

It's Not a Disaster


View Profile WWW
« Reply #14 on: August 24, 2009, 09:54:34 AM »

God. Hope for your sake that their HR department just used C as short-hand for C++. I saw your code Grin
Now that's not a very nice thing to say!  Big Laff
I'm hoping I'm flexible enough to write C++ like C++ and write C like C, but obviously I'm a lot more used to the former than the latter.

Hey, does anyone have some exemplary examples of C code I could look at? I think that's the missing piece here for me.
Logged

Glaiel-Gamer
Guest
« Reply #15 on: August 24, 2009, 01:02:48 PM »

C++ is a superset of C, remember.

Actually it isn't, and knowing this might be important for a job interview.  There are many examples of C89 code that are not legal C++ code, and the C99 standard deviates significantly from C++.  The upcoming C1X standard will likely increase the divide even further.

I'm a bit curious about this too... the only C i remember that isn't compatible with c++ is stuff that's a bad idea in the first place (calling a function before defining it, etc)
Logged
Gold Cray
Level 10
*****


Gold Cray


View Profile WWW
« Reply #16 on: August 24, 2009, 01:13:54 PM »

C++ is a superset of C, remember.

Actually it isn't, and knowing this might be important for a job interview.  There are many examples of C89 code that are not legal C++ code, and the C99 standard deviates significantly from C++.  The upcoming C1X standard will likely increase the divide even further.

I'm a bit curious about this too... the only C i remember that isn't compatible with c++ is stuff that's a bad idea in the first place (calling a function before defining it, etc)

Stroustrup has a website full of information.
Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #17 on: August 24, 2009, 01:41:12 PM »

C++ is a superset of C, remember.

Actually it isn't, and knowing this might be important for a job interview.  There are many examples of C89 code that are not legal C++ code, and the C99 standard deviates significantly from C++.  The upcoming C1X standard will likely increase the divide even further.

I'm a bit curious about this too... the only C i remember that isn't compatible with c++ is stuff that's a bad idea in the first place (calling a function before defining it, etc)

Common C89 "C that is not C++" things I run into:

C allows implicit conversions from void* to any other pointer type.  C++ requires a reinterpret_cast.

Code:
void f(void)
{
    /* Legal C, C++ requires a cast. */
    int *p = malloc(sizeof(int));
}

The behavior of const is slightly different in C and C++.  C++ const is much more useful and flexible.

Code:
const int n = 5;

void f(int x)
{
    switch (x)
    {
        case n: /* C++ can do this, C cannot */
            break;
    }
}

Other minor things:

C enum types freely convert back and forth with int.  C++ enums do not.
A C function requires a parameter list of (void) to indicate that it takes no parameters, C++ does not.

C99 adds a number of new features that C++ does not have.  These include variable length arrays:

Code:
void f(unsigned size)
{
    int array[size]; /* variable length array */
}

Designated initializers for structs: (I may have this syntax wrong)

Code:
struct point
{
    int x;
    int y;
};

void f(void)
{
    struct point p = { .x = 3, .y = 4};
}

C99 adds the restrict keyword, which indicates that a group a pointers all refer to different objects.  This aids in compiler optimization.  For example, C99's fopen is declared like so:

Code:
FILE *fopen(const char *restrict filename, const char *restrict mode);

There are other things too, but that all I can remember off the top of my head.
Logged



What would John Carmack do?
Problem Machine
Level 8
***

It's Not a Disaster


View Profile WWW
« Reply #18 on: August 24, 2009, 01:45:04 PM »

Good stuff! Thanks!
Logged

Ann Ishman
Level 0
**


View Profile
« Reply #19 on: August 24, 2009, 06:12:39 PM »

It just all seems so silly, if you're gonna be doing all that why not just use C++ in the first place

Two most common reasons:
  • Person calling the technical shots is stubborn and has way too much power. (Commonly surrounded by a bunch of underlings convinced he is a genius.)
  • Dependency on old code/hardware that the people who call the shots on budgets refuse to pony up the cash to upgrade. (Despite probably losing tons of money keeping the old stuff running.)
Logged
Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic