answersLogoWhite

0

What is an interface in C?

Updated: 8/11/2023
User Avatar

Wiki User

16y ago

Best Answer

C doesn't have interfaces like Java does. In fact, it doesn't even have classes. The closes thing it has to interfaces might be a pointer to a function, which could point to one of several different functions without the caller having to know which one exactly. C does not have interfaces or classes. However it's quite easy to simulate interfaces and classes in C. Before I get into doing that, let me explain what interfaces are and how it's done in C++, which supports multiple inheritance which is much more powerful then interface inheritance.

Interface inheritance is basically the same thing as multiple inheritance. In interface inheritance, the interfaces are classes that cannot have method implementations and variables. They may only have function prototypes. You are only allowed to inherit from one full blown class as a child in the interface inheritance model.

However, in multiple inheritance (which C++ supports), you're allowed to inherit from as many full classes as you want. This allows you to make those full blown classes as slim as a bunch of pure virtual functions (interface) or as full classes with method implementations and variables. For example, to implement something similar to a comparable interface in C++:

class Comparable

{

public:

virtual int compareTo(void* x) = 0;

};

class Foobar: public Comparable

{

public:

virtual int compareTo(void* x) { /*compare implementation here */ }

//rest of class

};

That would be pretty much the same thing as interface inheritance in Java. Any function that takes a Comparable pointer or reference will also be able to take Foobar. In C, you'd have to use function pointers to achieve the same effect. Basically in your structure, you'd have to have a pseudo v-table... a bunch of function pointers:

struct Comparable

{

int (*compareTo)(void* x) = 0;

};

struct Foobar

{

int (*compareTo)(void* x);

//rest of structure

int i;

int j;

int k;

};

void InitFoobar(struct Foobar* this)

{

this->compareTo = /*compare to function*/;

//rest of the initialization

this->i = 0;

this->j = 1;

this->k = 2;

}

If you want to pass Foobar into a function that takes a Comparable pointer, just cast the Foobar pointer to a Comparable pointer. The C standard guarantees structures Comparable and Foobar will be laid out exactly the same as long as the variables are declared in the same order. So the first 4 bytes, the function pointer, will be at the exact same offsets in both Comparable and Foobar. However, after the first 4 bytes, Foobar will have extra 12 bytes of stuff where Comparable will not. This memory arrangement lets you treat Foobar exactly like a Comparable. This is also how single inheritance is normally implemented in most languages.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

A derived interface is: an interface extends another.

An interface extends another interface means the "derived one" contains all the APIs from both interfaces.

For example:

public interface I1 { void m1(); }

public interface I2 { void m2(); }

To have a new interface with m1() and m2(), you may define:

public interface I1_2 : I1, I2 {} // multiple inheritance?!

or

public interface I2Derived : I1 { void m2(); } // derived way, but duplicate of I2

in both ways, the instance of I1_2 or I2Derived understands m1() and m2(), no more, no less.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

born Aug. 26, 1906, Biaystok, Poland, Russian Empiredied March 3, 1993, Washington, D.C., U.S.) Polish-born U.S. physician and microbiologist. He immigrated to the U.S. with his parents in 1921 and received an M.D. from New York University. He grew poliovirus in human nerve tissue outside the body, showed that it does not enter the body through the respiratory system, and proved that poliomyelitisis primarily an infection of the digestive tract. He postulated that an oral vaccine would work longer than jonas-salk's injections of killed virus, and he isolated weakened strains of each of the three types of poliovirus that would stimulate antibody production but not produce disease. The Sabin oral polio vaccine, approved for use in the U.S. in 1960, became the main defense against polio throughout the world.

Read more: albert-sabin

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The interface concept of Java doesn't exist in C. If you mean something else by interface, be more specific.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is an interface in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

In C plus plus Interface is also known as?

I guess you mean Java, there is no interface in C++.


What is the difference between the Linux shell and C?

C is a programming language. A shell is an interface.


Which languages support multiple interface?

If by interface you mean, a mechanism to achieve abstraction and create a blueprint for future implementation. Java/C#/C++ all allow for multiple interfaces(abstract classes in C++) to be implemented.


When you define a c plus plus class what items are considered part of the interface?

The interface of a C++ class is the public methods and attributes that are exposed by the class. In a pure abstract base class, the interface is enforced by the compiler in each of the derived child classes.


What programming language can be use in programmable interface controller?

C


What is the difference between class inheritance and interface inheritance?

interface inheritance is a misleading term. Interface inheritance would be equivalent to the union of the method signatures of interfaces ( no typo here, an interface may implment multiple other interfaces) Class inheritance - single hierarchy (in C#), and not only the methods are inherited, but also the data members. (interface in C# cannot define data members)


Why you need an interface in the c sharp?

Not just C#, any computer languages that provides the interface features, such as Java, C++ would benefit from applying interface approach - the interface provides the method signatures (contracts), not the implementation (the code). For those who do the programming in 1-person way, this may not make sense. But for those who do the project in 2 or more developers, the interface would provide a way to do the development in 2 or more ways without waiting the rest of the team to finish the coding.


What has the author Julia C Hunter written?

Julia C. Hunter has written: 'Interface design for electronic retailing'


Why you use public mode in c plus plus?

To expose an interface to the class members. Without an interface of some kind, an object would be useless.


Which is the most common encapsulation in use on BRI Interface?

Which is the most common encapsulation in use on BRI Interface ? A. SDLC B. ATN C. HDLC D. PPP


What is JNI?

Java Native Interface JNI is an interface between java and applications and libraries written in other languages. As an example, JNI enables Java programs to use C libraries and also enables C programs to use Java classes.


Who is Douglas C Englebart?

Inventor of the mouse and several other human-computer interface devices.