0

I know that tentative definition is like

int i;
int i;

and they are combined while linking and real definition is like

int i=5;

and used once ,but what is the point of having many tentative definitions in the code (why I'll need to declare it twice?)
and why repeated tentative definition give errors in C++ and works fine in C

Hussien Mostafa
  • 159
  • 2
  • 18

1 Answers1

3

Because C++ doesn't have tentative definitions. That is a C language feature that C++ does not have.

So when you write int i;, that is a real definition (without an initialiser attached). Per the one definition rule, you may only have one definition (!).

As for an example of why you'd want to use them in C, honestly I can't think of one. Maybe that's why C++ didn't bother!

Note that you can still declare stuff as often as you like. For objects, you'll need to use extern to signal this intention:

extern int x;
extern int x;
extern int x;

int x = 0;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055