# What are the key differences between C and C++, and between struct and class?

> What are the main differences between C and C++? Also, how does a struct differ from a class in C++?

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-c-vs-cpp-struct-class
Last updated: 2026-11-28

Difficulty: Beginner

## Question

What are the main differences between C and C++? Also, how does a struct differ from a class in C++?

## Answer

Let's start with the question that opens almost every C++ interview at service companies and a lot of product companies too. It sounds trivial, but a crisp answer sets the tone for the rest of the round, so let's make sure yours is crisp.

Think of C as a very sharp toolbox: functions, structs, pointers, and manual memory management. C++ began as 'C with Classes' and kept that toolbox fully intact, then added machinery for organising and protecting larger programs. The headline difference is the programming model. C is procedural: data and functions live separately. C++ is multi-paradigm: it supports procedural, object-oriented (encapsulation, inheritance, polymorphism), generic (templates), and to a good extent functional style (lambdas, algorithms).

Beyond the paradigm, the practical differences are these. C++ has classes with access control, constructors and destructors, function and operator overloading, references, namespaces, exceptions, templates, and the Standard Library with containers such as vector and map. C has none of these; you use malloc and free instead of new and delete, printf instead of streams, and raw arrays instead of vectors. C++ is also stricter about types: converting void\* to another pointer type needs an explicit cast in C++ but is implicit in C, and C++ has a real bool type. A fun detail interviewers like: sizeof('a') is 1 in C++ (character literals are char) but 4 in C (they are int).

Also mention that C++ is not a strict superset of C. Things like implicit void\* conversion, variable-length arrays, and using 'new' as an identifier make some valid C programs invalid C++. Saying this shows you know the languages are cousins, not parent and child.

Now struct versus class. In C, a struct is only a bundle of data; it cannot contain member functions. In C++, a struct is almost the same as a class: it can have methods, constructors, destructors, inheritance, and virtual functions. There are exactly two differences. First, members of a struct are public by default, while members of a class are private by default. Second, when inheriting, a struct inherits publicly by default and a class inherits privately by default. That is it. There is no difference in memory layout, performance, or capabilities.

So why do we have both? Convention. Programmers use struct for plain data aggregates (a Point with x and y, an API request record) and class for types that maintain invariants and hide their representation. Following that convention makes code easier to read, and interviewers appreciate when you state it.

A common follow-up is 'can a struct have a virtual function?' Yes, absolutely. Another is 'what does an empty struct's sizeof give?' It gives 1, because every distinct object needs a unique address. Keep these tiny facts handy; they are the kind of thing that turns a good answer into a memorable one.

## Code examples

### Struct vs class defaults

```
#include <iostream>
struct S { int x = 1; };          // public by default
class C { int x = 2; public: int get() const { return x; } };  // private by default

struct D1 : S {};   // public inheritance by default
class  D2 : C {};   // private inheritance by default

int main() {
    S s;
    C c;
    std::cout << s.x << " " << c.get() << "\n";
    // c.x;          // error: private
    D1 d1;
    std::cout << d1.x << "\n";   // OK: inherited publicly
    // D2 d2; d2.get();  // error: get() is private via private inheritance
    std::cout << sizeof('a') << "\n";  // 1 in C++, 4 in C
}
```

Only the default access and default inheritance mode differ between struct and class. The sizeof('a') line is a classic C vs C++ difference.

## Key points

- C is procedural; C++ is multi-paradigm (OOP, generic, functional-style).
- C++ adds classes, references, templates, namespaces, exceptions and the STL; C does not have them.
- struct vs class differ only in default access (public vs private) and default inheritance.
- C++ is not a strict superset of C; implicit void\* conversion is one example that breaks.
- sizeof an empty struct is 1 in C++ so that distinct objects have distinct addresses.

## Common follow-up questions

- Can a struct have constructors, destructors and virtual functions in C++?
- Why does C++ require an explicit cast from void\* but C does not?
- What does extern "C" do and why do we need it?

## Interview tips

- Lead with the paradigm difference, then give 3 to 4 concrete features, then stop; do not recite a 15-item list.
- State clearly that struct vs class differ only in default access and default inheritance.

## Concepts covered

C vs C++, OOP, struct vs class, namespaces

Next: [References vs pointers in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-references-vs-pointers) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [const pointer vs pointer to const, and const correctness](https://www.internhack.in/learn/interview/cpp-interview/cpp-const-correctness)
- [Stack vs heap memory in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-stack-vs-heap)
- [new/delete vs malloc/free](https://www.internhack.in/learn/interview/cpp-interview/cpp-new-delete-vs-malloc-free)
- [Memory leaks, dangling pointers and double free](https://www.internhack.in/learn/interview/cpp-interview/cpp-memory-leaks-dangling-pointers)
- [RAII: Resource Acquisition Is Initialization](https://www.internhack.in/learn/interview/cpp-interview/cpp-raii)
- [Constructors in C++: default, copy and move](https://www.internhack.in/learn/interview/cpp-interview/cpp-constructors-copy-move)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)
