Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the following program compile?

Tags:

c++

#include<iostream>

using namespace std;

int main()
{
    int abc();
    return 0;
}

When the compiler reaches the line int abc();, it rightly thinks that we are declaring a function named abc which does not take any arguments and whose return type is of type int. Then why is the compiler not throwing me an error because I have not defined a function named abc?

like image 694
Abascus Avatar asked Mar 10 '26 16:03

Abascus


1 Answers

It is not an error to declare a function without defining it. The function could have been defined in another file. In C++, each compilation unit (C++ file) is compiled individually, and linked together after that.

Linker does not show error either, because you don't attempt to use the function. If you attempted to use it, linker would search all compilation units for the definition, and show error when it does not find a definition.

like image 191
VLL Avatar answered Mar 13 '26 05:03

VLL