Is 'Private' a C Keyword?

Is 'Private' a C Keyword?

Are 'private' or 'public' keywords in ANSI C (or any other C for that matter), or were they only added in C++ (and Java, C#, ...)?

3 Answers

private is not a C89 or C99 keyword. See C Programming/Reference Tables on Wikibooks*.

Also, C has nothing** to do with Java and C# (and, really, not C++ either). However, the converse is not true -- C++ grew from C, for example.

* Better reference needed!
** Actually, C89 "borrowed" the const and volatile keywords from C++. Likewise, C99 "borrowed" the inline keyword, and also added _Bool and _Complex (like C++'s bool andcomplex, respectively) [citation-needed].

5

Neither are C keywords, but some people do the following:

#define public 
#define private static

Update:

For those who think it is a bad idea to do the above, I would agree. But it does explain why someone might think public or private are C keywords.

For those who think it won't compile in C, try this:

#include <stdio.h>
#include <stdlib.h>

#define public
#define private static

private void sayHello(void);

public int main(void) {
    sayHello();

    return (EXIT_SUCCESS);
}

private void sayHello(void) {
   printf("Hello, world\n");
}

For those who think it won't compile in C++, yes the above program will.

Update:

Well actually it is undefined behaviour due to this part of the C++ standard:

A translation unit that includes a header shall not contain any macros that define names declared or defined in that header. Nor shall such a translation unit define macros for names lexically identical to keywords.

So the example above and below are not required to do anything sane in C++, which is a good thing. My answer still is completely valid for C (until it is proven to be wrong! :-) ).

In the case of a C++ class with private members, you can do something similar (considered an abuse) like this:

main.c:

#include <cstdlib>
#define private public
#include "message.hpp"

int main() {
    Message msg;

    msg.available_method();
    msg.hidden_method();

    return (EXIT_SUCCESS);
}

message.hpp:

#ifndef MESSAGE_H
#define MESSAGE_H

#include <iostream>

class Message {
  private: 
      void hidden_method();

  public: 
      void available_method();
};

inline void Message::hidden_method() {
    std::cout << "this is a private method" << std::endl;
}

inline void Message::available_method() {
    std::cout << "this is a public method" << std::endl;
}

#endif
9

static isn't like private, given that you can't read a static variable even in the constructor of the class (the function which inits members of a struct in C language).

You only can use static variables in the part of the code where they were defined (in a function, a struct, ...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.