-2

So I'm trying to learn C and I understand that it doesn't have classes. However, I have only been doing c++ programming and to create an object of a special type, let's say car, I would have to make a class for it. Something like this:

class Car {
     //code
};

void main() {
    Car c; //or Car c = new c(//constructor);
}

But I don't understand how C can't have classes when you something like this. You are declaring an variable of type FILE. So does that mean FILE is a struct in the stdio.h file? How do the data types work exactly in C?

Richard
  • 5,840
  • 36
  • 123
  • 208

3 Answers3

2

But I don't understand how C can't have classes

Because it's not strictly an object-oriented language.

So does that mean FILE is a struct in the stdio.h file?

Yes.

How do the data types work exactly in C?

There's an entire paper on the topic (Google, first hit).

Sidenote: you can emulate object-oriented programming style in C using structs, it's just a matter of point of view whether or not you call something OO. For example, consider the usage of the infamous libcurl networking library:

CURL *hndl = curl_easy_init();
curl_easy_setopt(hndl, CURLOPT_URL, "http://example.com");
curl_easy_perform(hndl);
curl_easy_cleanup(hndl);

This is essentially verbosely (explicitly) writing some OOP-code like this:

CURL hndl;
hndl->url = "http://example.com";
hndl->perform();

There also is a paper on this topic.

  • 2
    There's more than one paper in that subject, but alas not a single one was read before asking the question. – Mithrandir Nov 14 '12 at 19:19
  • @Mithrandir Of course I didn't mean "there's only a single paper on this, that's why you didn't find it by the exhaustive googling you have done so far"... :P –  Nov 14 '12 at 19:22
  • The papers i have on C and programming types predate google and are actually written on paper. But of course, that's no longer relevant knowledge. ;-) – Mithrandir Nov 14 '12 at 19:30
  • @Mithrandir Are you saying that K&R isn't anymore relevant? O.o –  Nov 14 '12 at 19:31
0

You can use the typedef keyword to create types in C. I usually do this sort of thing:

struct car_st {
    int numberOfSeats;
};
typedef struct car_st CAR;
typedef struct car_st * CAR_PTR;  

Something like that.

Actually C can have classes, kind of. Early implementations of C++ and Objective C were done with preprocessors, I think. The problem is that you don't get true data hiding and methods. You end up using function pointers for methods -- and that is both a maintenance nightmare and a lot of work to set up. Not sure how you'd do inheritance, but it would probably be a mess of pointers to other structs.

So, yeah, you can slap something like that together. But please don't.

Marvo
  • 17,845
  • 8
  • 50
  • 74
  • About OO-C, take a look at [OO best practices for C programs](http://programmers.stackexchange.com/q/118253/1033) on programmers SE. – mouviciel Nov 14 '12 at 19:42
0

The C equivalent would be to define a struct type for the Car's attributes, and then define functions to manipulate objects of type Car. That's essentially how the FILE type works:

FILE *f = fopen("afile.txt", "r"); // open a file for reading
fgets(buffer, sizeof buffer, f);   // read from a file
fprintf(f, "%d on the wall", bottles--); // write formatted text to the file

At no point to we manipulate objects of FILE type directly; we just pass pass pointers back and forth.

There's no access control; you can't make some parts of a C struct public and others private; you can hide the implementation like so:

/** Car.h */
...
struct Car; 

struct Car *createCar(void);
void deleteCar(struct Car **theCar);
int startCar(struct Car *theCar);
...

/** Car.c */

#include "Car.h"
...
struct Car {
   char *make;
   char *model;
   int  year;
   ...
};

struct Car *createCar(void)
{
  struct Car *p = malloc(sizeof *p);
  if (p)
  {
    ... // initialize attributes
  }
  return p;
}

void deleteCar(struct Car **theCar)
{
  free(*theCar);
  *theCar = NULL;
}

void startCar(struct Car *theCar)
{
  theCar->running = 1; 
}

/** main.c */
#include "Car.h"
...
int main(void)
{
  struct Car *c = createCar();
  startCar(c);
  ...
  deleteCar(&c);
}

In this example, main.c only sees the forward declaration struct Car; in Car.h; since the type is incomplete, main can only declare pointers to type struct Car. In Car.c, the struct type is completed with the definition struct Car {char *make; char *model; ...};, so functions within Car.c can access those members.

John Bode
  • 119,563
  • 19
  • 122
  • 198