tags:

views:

186

answers:

5

what are the major differences between C and Objective C. Is abstraction possible in C?

A: 

http://goo.gl/pvba

Short answer is that there is a huge difference. That link is an okay starting point.

Andrew Hubbs
Link refers to C++ vs Objective-C. C++ is not C.
Kirk Broadhurst
The link compares C++ and ObjC. The question is between C and ObjC. Truth of the matter is, ObjC is a small addition over C, giving it classes. C++ is a huge addition over C in a somewhat different direction, giving it a shit-ton of different features almost no-one fully understands. So while C++ and ObjC differ a *lot*, C and ObjC do only slightly. If C was Latin, ObjC would be Old Italian, and C++ African American Vernacular English, thousands of years and several absorbed cultures later.
Amadan
Oh, sorry. Misread the question. Disregard my answer then.
Andrew Hubbs
Hey all you people out there who have answered my questions..i thank you all from heart for helping me..may be its a small question..but the help is great
New Beginner
A: 

Yes, abstraction is certainly possible in C (see below), as it is in virtually any programming language. The difference between C and Objective-C is not about abstraction but about dependencies. The explicit purpose of Objective-C is to decouple dependent modules so that large, complex systems don't become so unweildy for developers. In particular, Objective-C's runtime-bound message passing (instead of compile/link-time bound function calls) allows modules to be less tightly coupled. The calling module needs to know only the name, not the binary representation of a method to call it. Modules can thus be updated individually, replaced, or even modified at runtime without breaking all of the dependencies in the system.

For completeness, the classic approach to creating encapsulation and abstraction in C is the "abstract data type" pattern. In applying this pattern, C's declaration vs. definition split allows you to both encapsulate and abstract the implementation of a data type.

For example, in the public header

// MyADT.h
typedef struct _myADT *myADT;

myADT myadt_create();
void myadt_perform(myADT adt);
void myadt_free(myADT);

Clients could use the API without knowlege of the implementation of my data type. Definition of the struct and of the functions is in a separate (and potentially private) module:

// MyADT.c

typedef struct _myADT {
 ...
};

void myadt_create()
{
   struct _myADT adt = malloc(sizeof(struct _myADT));
   ...
   return adt;
}

//etc...
Barry Wark
+8  A: 

Of course abstraction is possible in C. It's possible if you're writing code by setting bits with a "magnetized needle and a steady hand".

A function is a unit of abstraction. If I am representing a deck of cards as an array of char[52], then I can write a function to shuffle it. After I've written that function, I don't ever have to think about how to shuffle it again. I can just say, shuffle(deck);

Note that I could do that writing in assembly, a hex editor or with a magnetized needle.

aaronasterling
Correct answer, but you get the upvote for the magnetized needle.
Andrew Hubbs
if that's the case, then I should give credit where [credit is due](http://xkcd.com/378/).
aaronasterling
Awesome. It was that comic that came to mind initiating my reaction to begin with. ;-]
Andrew Hubbs
@Andrew Hubbs : Excuse me, but REAL programmers use butterflies :)
Max
@AaronMcSmooth: I'd down vote xkcd for use of vim instead of vi if I could.
JeremyP
+4  A: 

Briefly, Objective-C adds object-oriented features to C.

Abstraction is possible in pretty much every programming language in common use. All that abstraction really means is "Hiding the details of how something is implemented".

Programming is all about creating abstractions. When you design a function, that's an abstraction. For example, if the function adds up all of the elements of the array, the abstraction is letting you think "Hey, I'm adding up the elements of this array" instead of thinking "Ok, let me create a counter variable, and then set it to the first element in the array, and then grab that value, and add it to the sum, now increment the counter and keep going if I haven't gotten to the end of the array".

Barry's answer is an example of a slightly more complicated abstraction, where you create an abstract data type. For example, struct _myADT might be struct circle and you might have functions to create a circle, measure its area, resize it, etc., etc. In reality, you're passing around a pointer to a location in memory that contains the circle's center, radius, etc and so on, but the abstraction is that you're passing around a representation of a circle.

The difference between Objective C and C is that C isn't very good at creating abstract data types. You have to go through all kinds of trouble to hide the details of what's going on inside a struct, and someone can always decide "I don't really need your abstractions, let me reach in and twiddle these bits because I know better than you". In object-oriented languages like Objective-C, you can actually enforce your abstractions by deciding what members of your objects can be accessed, and how they can be accessed.

Jacques
It should be noted that there exist OO languages that don't let short sighted programmers tell _me_ what _i_ can and can't do with code. Python is a great example.
aaronasterling
Couldn't resist evangelizing for the snake could you. ;-] At least you didn't say ruby I guess. -(secret python lover)
Andrew Hubbs
+1 for how the two differ. In particular, ObjC adds a Smalltalk-style OO layer on top of C.
Frank Shearar
+7  A: 

If anyone ever asks you, "Can A done by language B be done in C?" The answer is always "yes", because all programs are really C programs. Allow me to illustrate.

Objective-C is C, with a library, written in C, to handle message passing that make abstraction easy.

Ruby, Python, Perl? They are all C programs interpreting large text files that define how to process other text files. Ergo, all Perl, Python and Ruby programs are C programs with insanely large data sets.

LISP started off as a C program to count parentheses that went to college to get a PhD in Abstract Math, then shacked up with a plus-sized editor and now distracts programmers from writing C programs with in-editor Tetris, e-mail and IRC.

Java is an Abomination. Written in C. But written by multiple groups of people who didn't bother talking to each other and thus came up with several C programs all named Java to processes the same Java data sets into different, incompatible results. Web browsers, fearing a "write-once, test-everywehre" gap, created JavaScript (in C) to ensure no project could ever be simple ever again.

Yacc is a C program to create more C programs. Some scientists believe Yacc to be a proto living organism, as evidenced by its ability to recreate itself. Others believe Yacc to be the natural results of a C program spun at 5400 RPM until it gets dizzy and makes a mess on the carpet.

Of course, C itself is written in C to convert C source to assembly which is assembled into executable machine code by an assembler.

Written in C.

John Franklin
+1 for the awesome rant about C. That just brightened up my mood after reading the other answers.
cool_me5000
Lisp started off as a C program? I know Lisp is powerful, but I don't think even it is capable of time travel.
Chuck
@Chuck: That is true. Lisp was written in 1960, C was born in 1972. Even today, GCL compiles itself. (GCL does use the system C compiler to create the object code, but that's for performance and portability reasons.) +1 for the history lesson.
John Franklin