Thread: Is there Data Abstraction in C Language?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    5

    Is there Data Abstraction in C Language?

    This was the question I was asked during my project review but still I am clueless about the answer.

    The question was, Is there Data Abstraction in C Language? I answered them No. Then, they asked me What is Structure and Union?. I told them, A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together. Now they asked me, If that is the case then how can we say that there is no Data abstraction in C? I was totally speechless at this. Still now, I don't have the answer for it.

    If you guys can help me explain me this, it would be of great help to me. Thank you very much!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your project reviewers are correct: a struct brings different pieces of data under a single name. This is certainly a form of data abstraction since one can then think about the struct instead of each of its components (except where necessary).

    To phrase the question in a more open ended way: what is data abstraction? Provide an example.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    5
    So, are you saying that C also supports Data Abstraction?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > So, are you saying that C also supports Data Abstraction?
    Example, the FILE struct, which you only ever see in your own code as FILE*, and which in your own code there is never any attempt to dereference a FILE* pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, are you saying that C also supports Data Abstraction?
    To some extent it does. You should answer my question since that is the heart of the issue.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    5

    The reply to your question

    Quote Originally Posted by laserlight View Post
    To phrase the question in a more open ended way: what is data abstraction? Provide an example.
    Data Abstraction is organizing access to a program's data (eg.) arranging variable declarations and methods for accessing data.

    So, what do you say now?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Data Abstraction is organizing access to a program's data (eg.) arranging variable declarations and methods for accessing data.

    So, what do you say now?
    C does indeed support your notion of data abstraction. As you pointed out, structs organise access to data by grouping them. By providing an appropriate set of functions, one can then organise the access and manipulation of the grouped data.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    5
    Quote Originally Posted by laserlight View Post
    C does indeed support your notion of data abstraction. As you pointed out, structs organise access to data by grouping them. By providing an appropriate set of functions, one can then organise the access and manipulation of the grouped data.
    So, then why is C not told as a Object Oriented Language? Data abstraction is a property of object oriented language.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, then why is C not told as a Object Oriented Language? Data abstraction is a property of object oriented language.
    Object oriented programming involves data abstraction, but data abstraction does not necessarily require object oriented programming. C does not have the kind of native support for some object oriented features such as inheritance and polymorphism that OOP languages have.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    5
    okay.... thank you very much for your help! this is a great forum!

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    C can mimic OOP style by usage of function pointers. In fact, I believe this is the simple mechanism that C++ uses under the hood (more detailed and sophisticated, but at the end of the day.... function pointers are used in some manner) to support virtual functions.

    C is more meant naturally as a procedural language and is more in line directly with how the computer handles and manipulates data. C++ allows new features, but uses the same old type of stuff to build said features.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can also do data abstraction as a matter of discipline
    http://en.wikipedia.org/wiki/Abstract_data_type

  13. #13
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24

    data abstarction is there in C and highly used.

    So finally, the answer to the question "Can we have data abstraction in C?" is YES.

    The data type like Stack, Queue...etc are perfect examples of data abstraction and those all are well implemented in C.

    In C++, we achieve data abstraction through "Class".

    This link might help you:
    http://www.itmweb.com/essay550.htm
    Last edited by Alexpo; 12-16-2009 at 07:31 AM. Reason: adding extra info

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MacGyver View Post
    C can mimic OOP style by usage of function pointers.
    Yes, but that just seems silly. I think it makes more sense to regard "method calls" in C as external to the object. Which probably allows for greater flexability but a bit more astuteness from the programmer. Affects like "inheritance" can be accomplished the same way -- by conceptualizing differently.

    "Proceduralism" and "Object Orientation" are not so mutually exclusive or anything.
    Last edited by MK27; 12-16-2009 at 09:03 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I think you haven't noticed but this thread is around 2yrs old.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Data Abstraction?
    By Matus in forum C Programming
    Replies: 9
    Last Post: 12-11-2008, 09:56 AM
  3. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  4. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM