Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
as far as I know, C is not an object oriented programming language. how can we make the user defined data types and allow some operations to be performed on those data Types.Just like the primitive data types are defined and allow some operations to be performed on them.
There are no object-oriented languages. There are just languages with different degrees of OO feature support. OO is a program design method, and as such it is mostly language-agnostic.
OO can roughly be summarized in 3 things:
Autonomous modules who do their own designated task, with limited knowledge of the surrounding program. ("loose coupling")
Private encapsulation of data and functions, so that users of the class need not worry about which parts that are internal and which ones that are part of the API. Prevents accidental or intentional misuse of internals. Reduces "namespace clutter".
Inheritance and polymorphism, which can be used for code re-use and API design.
The 1st is pure program design and the one that people most often get wrong. The 2nd and 3rd are supported by C, but implicitly and not very elegantly.
For private encapsulation, you can use static file scope variables. It works fine in some situations like single core embedded systems. But it gets problematic in other situations - it makes your class "singleton" single instance and turns it thread-unsafe.
You can do more proper private encapsulation with the concept of opaque type where you forward declare a struct and then only define that struct in a file not visible to the caller. This gives you true, multi-instance private encapsulation and can also be used for polymorphism, when combined with function pointers. The down side is that it's somewhat cumbersome and not many people are aware of it. Schools fail to teach it.
So it is perfectly possible to do OO programs in C, just as it is perfectly possible to make broken, non-OO designs in C++ or Java. It's just a whole lot easier to do OO when you have language support for it.
For example the C++ concept of "RAII", which isn't strictly speaking an OO feature in itself, but it helps a whole lot when doing OO design, since it gives you implicit constructor/destructor calls and you don't have to rely on the caller doing them explicitly as is the case in C. Another example is the mighty handy this pointer, in C you have to pass it along manually to each function call.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
How is functional programming useful over normal procedural languages like c or object oriented programming languages like c++ and where does it shine?
C lacks several features of functional programming that need to be worked around (likewise, while you can write in an object-oriented style in C, you need to work around several missing features as well).
C functions are not first-class objects. You cannot return a function from a function, store a function in a variable, or pass a function to another function. You cannot nest functions, and you cannot create anonymous functions. The workaround is that C does allow you to use pointers to functions, so you can write a function that takes a pointer to a function as an argument, but this is not as clean as what you can do in a language oriented towards functional programming.
C lacks closures, which are a way of capturing the “environment” of execution at a particular point in a program (namely, what variable names are bound to).
C lacks generics, except in the most broad sense. In most functional languages, it is possible to write one function which applies to a large number of different types because they don’t depend on specific attributes of those types.
C is a low level language giving the programmer the full control over the program execution. It was newer designed to thigh level abstract language.
Functional programming is not popular and most languages used nowadays are object oriented.
If you need the language which gives you ability to control the program and the environment you should consider C++
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
All of my initial programming experience has been in object-oriented languages (Java, Python). I am now learning C, and it seems that there are still things that resemble objects.
Say for example, a FILE pointer created with the standard C library. This is just a pointer to the memory location of a struct. Is this essentially the same thing as an object in OO languages?
There are existing questions asking about the difference between a struct and a class, but I'm asking about how a program uses those things. A struct is used or accessed via a pointer, while an object is a particular instance of a class. In this sense, it seems that a class is more general than a struct. Though I really only added this paragraph to prevent this questions from being marked as a duplicate, and it strays from my original question.
If a FILE pointer is, indeed, comparable to some FILE object in a different language, then where is the key difference between how that "thing" called FILE will be handled in a object-oriented language vs a non-object-oriented language. Seems like the line starts to blur.
In the C programming language, an object is a “region of data storage in the execution environment, the contents of which can represent values” (cf ISO 9899:2011 §3.15). Almost everything is an object, including pointers, arrays, structures, and integers (but not functions).
This notion however is different from what you understand as an “object” in most object-oriented languages. Notably, objects in C don't have behaviour associated with them, don't have classes and don't have any guarantees whatsoever. There isn't even a guarantee that an object may represent any value at all. An object is only a bit of memory.
The typical API design pattern in procedural programming languages like C is that there is a set of functions (like fopen, fprintf, fclose, fwrite, etc.) and a set of structure types (like FILE) that collect data required for these functions. The association between these structures and the corresponding behaviour is made by passing structures to functions.
You can build all the things you have in an object-oriented language like that, including virtual function calls and classes, you just have to build all of this manually. I believe this is a strength of C as you are not forced into a certain program structure, you can apply the design pattern of object-orientation where appropriate and use other approaches where not.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It might be a silly question, but I'm interested in it very much. Is it possible to implement operator new, dynamically expanding arrays, classes in pure C?
Any links or code examples will be appreciated.
new: #define new(type) malloc(sizeof(type)) (have to call it using function syntax, like struct stat *st = new(struct stat))
dynamically expanding arrays: realloc plus some custom array-manipulation functions (like push_back, etc.) - this is commonly implemented by third-party C utility libraries (and, as #Mgetz points out, some compilers have built-in extensions for it)
classes: structs with function pointer members (this is very common in several projects, such as the Linux kernel)
You might want to look at GObject, which is a C library providing some object-oriented features to C. Also see the dozens of hits you get for googling "Object-Oriented C".
A quick google search revealed this:
http://ooc-coding.sourceforge.net/
Haven't read it through but it sounds like what you're after.
Yes, it is possible (common?) to implement object orientedness in C - or at least the bits that are especially needed.
An example is a once created a garbage collector by storing the pointers to malloced memory and the free function in linked lists.
The best thing about C is that it just works and there is almost zero overhead. The more work a language does for you automatically can mean there is a lot more overhead - though this is not always the case.
It depends if it is OK for you to reimplement the compiler.
If it's ok - you can do whatever you wish, otherwise:
new - as an operator - no, but you can define a function + macros that will simulate it.
classes - yep, you can. you may simulate it pretty closely with static functions and an array of pointers to functions. But there will be no overloading.
expanding arrays - yes, with the classes simulation above.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
So, across my programming experience I have come across two types of type annotations for statically typed languages: I call them 'before' and 'after'. C-Style languages use the format
int i = 5
While most non-c-family languages use the format
var c:int = 5
Examples of the former category would be C, C++, Java; examples of the latter category would be Scala, Haxe, Go.
This may seem to some to be superficial, but my question is: what are the advantages of each style? Why use one over the other? Why did C adopt that style in the first place?
The machine doesn't care - it's just that people who designed certain languages felt that some types of syntax are better or more easily readable than the others. Modern compilers usually have several stages of processing, and almost all of this syntactic differences are usually lost after the first stage, which parses text and converts then into compiler internal structures (AST - abstract syntax tree).
There are some historical precedences, e.g. the "prefix" vs "infix" vs "postfix" notation (http://en.wikipedia.org/wiki/Polish_notation, http://en.wikipedia.org/wiki/Infix_notation, http://en.wikipedia.org/wiki/Reverse_Polish_notation) which in the context of computer engineering history were used in edge cases - e.g. the "infix" notation is usually harder to parse and requires more memory than the postfix/RPN notation so it was not used where resources were really scarce (several KiB of memory or less), but most of those reasons are now obsolete as hardware is powerful enough.
Today, when designing a language, the choice of such syntax details is influenced by trying to make the language similar to some other popular language or group of languages for which there are already existing programmers, to avoid making a "language from Mars" which few people will use.
tl;dr: Depends on the person who created the language and what he though is more readable or "the right thing to do").
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What are the key differences between Ruby and C?
They are almost totally different.
Ruby
Strong, dynamic typing
Purely object oriented
Automatic garbage collection and no pointers
Interpreted (or JIT compilation with JRuby/IronRuby)
Reflective
Supports functional programming (closures, coroutines, etc.)
No preprocessor or macros
C
Weak, static typing
Procedural (not object oriented)
Not garbage collected and has pointers
Compiled
No reflection
Does not support functional programming
Has a preprocessor and supports macros
To Ruby From C and C++
Why do you ask? Do you have a specific project or goals in mind?
In addition to what others have already mentioned; I'd also say that some key differences to keep in mind is that the C family is much more portable....or rather, much easier to distribute the finished software. C programs will also be much faster than Ruby...whether that is important or not depends on what you are building (well, that's ALWAYS important, but it isn't a make or break proposition for a lot of programs).
Ruby is just simply a beautiful language to work with (do not underestimate the importance of a language that works with you); developing programs is much quicker in Ruby than C ( C is a compiled language, so that is to be expected )...Ruby is also a pretty simple language to learn; most people consider C to be fairly tough for newbies to pick up.
-- edit --
wow, just saw this was a 3 year old thread....my bad