29

Okay, you may call me a noob but I'm realling confused.

My ex classmate paid me to write a program in C. She gave me the task and it said something like "blah blah blah make at least TWO CLASSES, write at least ONE CONSTRUCTOR and rewrite at least ONE METHOD" it says that word by word.

And then I told her "this is C++ not C" she said "but we're learning C"

I ignored it and wrote the program in c++ and sent to her as I thought she didn't know what she was talking about. She said "it doesn't work on code blocks, and wtf is cout <<" and then she sent me a chunk of code that they write and instead of cout and cin there was printf and scanf. It had to be C. So, I rewrote the program with printf and scanf and she still says codeblocks throw errors (I still left classes as task demanded).

I want to ask wtf? Does C have classes? Or is there a misunderstanding or something?

EDIT: I've come back to the question after so many years and noticed some a*****es took time to remove 99% text from the question. Get a life, this is not 1984 yet.

Vanilla Face
  • 908
  • 1
  • 7
  • 17
  • 3
    See [how-do-you-implement-a-class-in-c](http://stackoverflow.com/questions/1403890/how-do-you-implement-a-class-in-c) – nawfal Jan 02 '14 at 15:34

8 Answers8

23

No, C doesn't have classes. That said, there are ways of simulating object-oriented programming in C - a quick Google search should yield some useful results.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
20

No, C has no classes per se, only C++ (which started out as "C with classes" back then...). But you can use the standard C library in C++ code, even if it is often not considered good practice (where C++ has its own, higher level constructs, e.g. cout vs printf).

You can sort of emulate the behaviour of classes, inheritance and virtual functions in C too, but it's not worth the pain.

You should probably buy/get your ex classmate a C programming book :-)

Péter Török
  • 114,404
  • 31
  • 268
  • 329
4

C does not have the formal construct of a class. You can produce modules with module-level data that by your own agreement you will not extern anywhere else, or static data, and write functions to get, set, and otherwise manipulate that data. You can even go to the point of using function pointers to manipulate similar data types as if they were in a class.

However, you won't be protected by class semantics or other rules by the C compiler, because the C compiler does not know about classes. However, structuring your data is quite powerful.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
3

C does not have classes.

But one can approximate a class by using static globals as private class members, and static functions as private member functions. extern members as public. In this case an entire file could be viewed as a class.

Probably this is not what you want.

phoxis
  • 60,131
  • 14
  • 81
  • 117
3

C doesn't support classes, but we do have tricky hacky workaround. Read entire explanation or simply scroll down to code section.

struct declaration:

I declared an struct with name class. Inside, I put uninitialized function with type void, name Print and one parameter char. This will look like void class.Print(char);

struct don't allow initialization inside:

But struct doesn't allow initialization of variables inside. So we will init it outside. We created an a function with type of class, name Printer. It inits that struct and return initialized struct which we can easily make use of.

call the class:

Now we included helper header and declared variable with type class, name printer1, value Printer(). After I called member of class and print string using printer1.Print("Hello from class function");

main.c

#include "helper.h"
void main()
{
    class printer1 = Printer();
    printer1.Print("Hello from class function");
}

helper.h

#include <stdio.h> //imports int printf(...);
void print(const char* text)
{
    printf(text);
}

typedef struct 
{
    void (*Print) (const char*);
} class;

class Printer()
{
    class Printer;
    Printer.Print = &print;
    return Printer;
}

Note:

  • This exact example was compiled and tested successfully with VC and tcc compiler.
  • class is example name. You can use any other name as well.
Gray Programmerz
  • 479
  • 1
  • 5
  • 22
  • This shows me the errors "expected ';' after struct definition", "missing type in typedef-declaration" and "expected unqualified-id before ')' token", I've tried naming the struct, but didn't help, I am using Dev-C++ 5.11 – Erick de Vathaire Oct 02 '21 at 04:37
  • `Dev C++` is IDE, whats compiler u use ? May be you did some mistake. This same code without modification works on `Microsoft Visual C++`, `gnu c compiler`, `tiny c compiler`. – Gray Programmerz Oct 02 '21 at 06:12
  • I think defining the name of the struct as 'class' is not really a reusable trick. – Motomotes Oct 27 '22 at 08:10
2

A classic case of conflicting requirements, it seems :-)

The terminology of her requirements CLASS, CONSTRUCTOR, METHOD are all C++ terminology, while none of them is C terminology (the closest of which would arguably be STRUCT, INITIALIZATION, FUNCTION). Your friend is confusing something here. I doubt that her teacher is confusing something, though...

Jens
  • 69,818
  • 15
  • 125
  • 179
2

C does not have classes, but you can emulate it with structures and pointers to a function. C99 is a little bit (just a bit) based on C++, so it's easy to reproduce classes with C.

md5
  • 23,373
  • 3
  • 44
  • 93
  • C had structs and function pointers for long time and it was in fact possible to create something like classes. There is no overloading atd variable number of function params though also no portable typechecking. I would not call this easy. You can create either nonportable classes (typeof, container_of) or crippled implementation of classes. – Tomas Pruzina Apr 25 '12 at 16:49
0

C mostly uses functional/structural programming instead of implementing Object Oriented Programming as in languages like C++ , Java , Python etc. which use classes . But in few instances we use classes like in:

typedef struct {
    ShapeClass shape;
    float width, height;
}
RectangleClass;

Hope it helped.

Michael Kotzjan
  • 2,093
  • 2
  • 14
  • 23
Mbote-Joseph
  • 73
  • 1
  • 4
  • But how to put function like `void` inside a `struct` ? – Gray Programmerz Aug 12 '21 at 15:48
  • @GrayProgrammerz ideally it is impossiblen struct can help you in working with related data : #include #include typedef struct { string name; string number; }person; int main(void){ person people[0]; people[0].name="Jose"; people[0].number="+25417"; people[1].name="Mbote"; people[1].number="+254-7926-22515"; for(int i=0, n=2;i – Mbote-Joseph Aug 14 '21 at 19:10