Thread: Is "public" valid in C?

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    Is "public" valid in C?

    I'm looking through the source code for the linux command "less" and I see some confusing syntax. Some functions have "public" in front and I thought public is only used in C++ for access permissions. Here's an example of what I saw.

    public void ch_flush()

    Is this valid in C and what is the purpose of it if it's not an object-oriented language?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's probably something like
    #define public

    It's basically a hint to the reader that the function is a global function which can be called from anywhere.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    2
    You are right. I found that public is just a macro that does nothing. So putting public in front of a function has no effect. Thanks

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Usually code like:

    Code:
    public void doSomething(int someParameter);
    Is just used to indicate that it will be exported in a library.

    ...whereas

    Code:
    private void doSomethingInternally(int someParameter);
    Is usually used to indicate the function is not going to be part of the program. In my example there will be the following somewhere in some header:

    Code:
    #define public
    #define private static

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Such tricks guarantee incompatibility with C++ (as redefining of keywords yields undefined behaviour in both C and C++). Whether you view that as an advantage or not, depends on viewpoint (eg I've seen C fanatics who hate C++ advocate redefining of C++ keywords in C code to ensure incompatibility). I view it as a disadvantage as it would take code, which would otherwise compile with either a C or C++ compiler, incompatible with C++ compilers.

    The technique is fine for it's intended purpose, but I would recommend avoiding use of macros named "public" or "private". A simple rename (eg "Public" and "Private") so they do not clash with C++ keywords would be enough.

  6. #6
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    I use "this" in C. It's a reserved word in C++. Does that mean I'm evil?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Trying to make something compile in both languages means you're using neither language to it's best advantage.
    http://david.tribble.com/text/cdiffs.htm

    For example, making malloc work in C++ requires a cast, and that's just plain dumb in a C program.

    But I also regard this as being dumb as well - using the pre-processor to rewrite the language to make it look different.
    Code:
    #define public
    #define private static
    This says it all.
    http://www.eskimo.com/~scs/C-faq/q10.2.html

    Even if you manage to make it compile with C and C++, there's still a set of programs which run differently depending on which compiler you choose to compile with (this is not good).

    If you're that desperate to mix C and C++, then read this
    http://www.parashift.com/c++-faq-lit...c-and-cpp.html
    don't just compile it all as C++, ignoring whatever warnings you get and hope.
    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.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    What I found amazing is that 'and' can replace '&&' in standard C++ as well as 'xor' and '^' amongst others. IMO that will just make the program not look right and until I read about it a few days ago I had no idea that it was possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minimum valid address value
    By BMintern in forum C Programming
    Replies: 6
    Last Post: 04-12-2008, 08:21 AM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  3. Are these valid operators
    By Morgan in forum C Programming
    Replies: 3
    Last Post: 08-12-2003, 11:56 AM
  4. valid test expression?
    By Draco in forum C Programming
    Replies: 11
    Last Post: 08-07-2003, 05:25 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM